subview_meat.hpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup subview
  16. //! @{
  17. template<typename eT>
  18. inline
  19. subview<eT>::~subview()
  20. {
  21. arma_extra_debug_sigprint();
  22. }
  23. template<typename eT>
  24. inline
  25. subview<eT>::subview(const Mat<eT>& in_m, const uword in_row1, const uword in_col1, const uword in_n_rows, const uword in_n_cols)
  26. : m(in_m)
  27. , aux_row1(in_row1)
  28. , aux_col1(in_col1)
  29. , n_rows(in_n_rows)
  30. , n_cols(in_n_cols)
  31. , n_elem(in_n_rows*in_n_cols)
  32. {
  33. arma_extra_debug_sigprint();
  34. }
  35. template<typename eT>
  36. inline
  37. void
  38. subview<eT>::operator= (const eT val)
  39. {
  40. arma_extra_debug_sigprint();
  41. if(n_elem != 1)
  42. {
  43. arma_debug_assert_same_size(n_rows, n_cols, 1, 1, "copy into submatrix");
  44. }
  45. Mat<eT>& X = const_cast< Mat<eT>& >(m);
  46. X.at(aux_row1, aux_col1) = val;
  47. }
  48. template<typename eT>
  49. template<typename op_type>
  50. inline
  51. void
  52. subview<eT>::inplace_op(const eT val)
  53. {
  54. arma_extra_debug_sigprint();
  55. subview<eT>& s = *this;
  56. const uword s_n_rows = s.n_rows;
  57. const uword s_n_cols = s.n_cols;
  58. if(s_n_rows == 1)
  59. {
  60. Mat<eT>& A = const_cast< Mat<eT>& >(s.m);
  61. const uword A_n_rows = A.n_rows;
  62. eT* Aptr = &(A.at(s.aux_row1,s.aux_col1));
  63. uword jj;
  64. for(jj=1; jj < s_n_cols; jj+=2)
  65. {
  66. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += val; Aptr += A_n_rows; (*Aptr) += val; Aptr += A_n_rows; }
  67. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= val; Aptr += A_n_rows; (*Aptr) -= val; Aptr += A_n_rows; }
  68. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= val; Aptr += A_n_rows; (*Aptr) *= val; Aptr += A_n_rows; }
  69. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= val; Aptr += A_n_rows; (*Aptr) /= val; Aptr += A_n_rows; }
  70. }
  71. if((jj-1) < s_n_cols)
  72. {
  73. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += val; }
  74. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= val; }
  75. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= val; }
  76. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= val; }
  77. }
  78. }
  79. else
  80. {
  81. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  82. {
  83. if(is_same_type<op_type, op_internal_plus >::yes) { arrayops::inplace_plus ( colptr(ucol), val, s_n_rows ); }
  84. if(is_same_type<op_type, op_internal_minus>::yes) { arrayops::inplace_minus( colptr(ucol), val, s_n_rows ); }
  85. if(is_same_type<op_type, op_internal_schur>::yes) { arrayops::inplace_mul ( colptr(ucol), val, s_n_rows ); }
  86. if(is_same_type<op_type, op_internal_div >::yes) { arrayops::inplace_div ( colptr(ucol), val, s_n_rows ); }
  87. }
  88. }
  89. }
  90. template<typename eT>
  91. template<typename op_type, typename T1>
  92. inline
  93. void
  94. subview<eT>::inplace_op(const Base<eT,T1>& in, const char* identifier)
  95. {
  96. arma_extra_debug_sigprint();
  97. const Proxy<T1> P(in.get_ref());
  98. subview<eT>& s = *this;
  99. const uword s_n_rows = s.n_rows;
  100. const uword s_n_cols = s.n_cols;
  101. arma_debug_assert_same_size(s, P, identifier);
  102. const bool use_mp = arma_config::cxx11 && arma_config::openmp && Proxy<T1>::use_mp && mp_gate<eT>::eval(s.n_elem);
  103. const bool has_overlap = P.has_overlap(s);
  104. if(has_overlap) { arma_extra_debug_print("aliasing or overlap detected"); }
  105. if( (is_Mat<typename Proxy<T1>::stored_type>::value) || (use_mp) || (has_overlap) )
  106. {
  107. const unwrap_check<typename Proxy<T1>::stored_type> tmp(P.Q, has_overlap);
  108. const Mat<eT>& B = tmp.M;
  109. if(s_n_rows == 1)
  110. {
  111. Mat<eT>& A = const_cast< Mat<eT>& >(m);
  112. const uword A_n_rows = A.n_rows;
  113. eT* Aptr = &(A.at(aux_row1,aux_col1));
  114. const eT* Bptr = B.memptr();
  115. uword jj;
  116. for(jj=1; jj < s_n_cols; jj+=2)
  117. {
  118. const eT tmp1 = (*Bptr); Bptr++;
  119. const eT tmp2 = (*Bptr); Bptr++;
  120. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = tmp1; Aptr += A_n_rows; (*Aptr) = tmp2; Aptr += A_n_rows; }
  121. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += tmp1; Aptr += A_n_rows; (*Aptr) += tmp2; Aptr += A_n_rows; }
  122. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= tmp1; Aptr += A_n_rows; (*Aptr) -= tmp2; Aptr += A_n_rows; }
  123. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= tmp1; Aptr += A_n_rows; (*Aptr) *= tmp2; Aptr += A_n_rows; }
  124. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= tmp1; Aptr += A_n_rows; (*Aptr) /= tmp2; Aptr += A_n_rows; }
  125. }
  126. if((jj-1) < s_n_cols)
  127. {
  128. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = (*Bptr); }
  129. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += (*Bptr); }
  130. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= (*Bptr); }
  131. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= (*Bptr); }
  132. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= (*Bptr); }
  133. }
  134. }
  135. else // not a row vector
  136. {
  137. if( (is_same_type<op_type, op_internal_equ>::yes) && (s.aux_row1 == 0) && (s_n_rows == s.m.n_rows) )
  138. {
  139. arrayops::copy( s.colptr(0), B.memptr(), s.n_elem );
  140. }
  141. else
  142. {
  143. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  144. {
  145. if(is_same_type<op_type, op_internal_equ >::yes) { arrayops::copy ( s.colptr(ucol), B.colptr(ucol), s_n_rows ); }
  146. if(is_same_type<op_type, op_internal_plus >::yes) { arrayops::inplace_plus ( s.colptr(ucol), B.colptr(ucol), s_n_rows ); }
  147. if(is_same_type<op_type, op_internal_minus>::yes) { arrayops::inplace_minus( s.colptr(ucol), B.colptr(ucol), s_n_rows ); }
  148. if(is_same_type<op_type, op_internal_schur>::yes) { arrayops::inplace_mul ( s.colptr(ucol), B.colptr(ucol), s_n_rows ); }
  149. if(is_same_type<op_type, op_internal_div >::yes) { arrayops::inplace_div ( s.colptr(ucol), B.colptr(ucol), s_n_rows ); }
  150. }
  151. }
  152. }
  153. }
  154. else // use the Proxy
  155. {
  156. if(s_n_rows == 1)
  157. {
  158. Mat<eT>& A = const_cast< Mat<eT>& >(m);
  159. const uword A_n_rows = A.n_rows;
  160. eT* Aptr = &(A.at(aux_row1,aux_col1));
  161. uword jj;
  162. for(jj=1; jj < s_n_cols; jj+=2)
  163. {
  164. const uword ii = (jj-1);
  165. const eT tmp1 = (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii];
  166. const eT tmp2 = (Proxy<T1>::use_at) ? P.at(0,jj) : P[jj];
  167. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = tmp1; Aptr += A_n_rows; (*Aptr) = tmp2; Aptr += A_n_rows; }
  168. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += tmp1; Aptr += A_n_rows; (*Aptr) += tmp2; Aptr += A_n_rows; }
  169. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= tmp1; Aptr += A_n_rows; (*Aptr) -= tmp2; Aptr += A_n_rows; }
  170. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= tmp1; Aptr += A_n_rows; (*Aptr) *= tmp2; Aptr += A_n_rows; }
  171. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= tmp1; Aptr += A_n_rows; (*Aptr) /= tmp2; Aptr += A_n_rows; }
  172. }
  173. const uword ii = (jj-1);
  174. if(ii < s_n_cols)
  175. {
  176. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii]; }
  177. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii]; }
  178. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii]; }
  179. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii]; }
  180. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= (Proxy<T1>::use_at) ? P.at(0,ii) : P[ii]; }
  181. }
  182. }
  183. else // not a row vector
  184. {
  185. if(Proxy<T1>::use_at)
  186. {
  187. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  188. {
  189. eT* s_col_data = s.colptr(ucol);
  190. uword jj;
  191. for(jj=1; jj < s_n_rows; jj+=2)
  192. {
  193. const uword ii = (jj-1);
  194. const eT tmp1 = P.at(ii,ucol);
  195. const eT tmp2 = P.at(jj,ucol);
  196. if(is_same_type<op_type, op_internal_equ >::yes) { (*s_col_data) = tmp1; s_col_data++; (*s_col_data) = tmp2; s_col_data++; }
  197. if(is_same_type<op_type, op_internal_plus >::yes) { (*s_col_data) += tmp1; s_col_data++; (*s_col_data) += tmp2; s_col_data++; }
  198. if(is_same_type<op_type, op_internal_minus>::yes) { (*s_col_data) -= tmp1; s_col_data++; (*s_col_data) -= tmp2; s_col_data++; }
  199. if(is_same_type<op_type, op_internal_schur>::yes) { (*s_col_data) *= tmp1; s_col_data++; (*s_col_data) *= tmp2; s_col_data++; }
  200. if(is_same_type<op_type, op_internal_div >::yes) { (*s_col_data) /= tmp1; s_col_data++; (*s_col_data) /= tmp2; s_col_data++; }
  201. }
  202. const uword ii = (jj-1);
  203. if(ii < s_n_rows)
  204. {
  205. if(is_same_type<op_type, op_internal_equ >::yes) { (*s_col_data) = P.at(ii,ucol); }
  206. if(is_same_type<op_type, op_internal_plus >::yes) { (*s_col_data) += P.at(ii,ucol); }
  207. if(is_same_type<op_type, op_internal_minus>::yes) { (*s_col_data) -= P.at(ii,ucol); }
  208. if(is_same_type<op_type, op_internal_schur>::yes) { (*s_col_data) *= P.at(ii,ucol); }
  209. if(is_same_type<op_type, op_internal_div >::yes) { (*s_col_data) /= P.at(ii,ucol); }
  210. }
  211. }
  212. }
  213. else
  214. {
  215. typename Proxy<T1>::ea_type Pea = P.get_ea();
  216. uword count = 0;
  217. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  218. {
  219. eT* s_col_data = s.colptr(ucol);
  220. uword jj;
  221. for(jj=1; jj < s_n_rows; jj+=2)
  222. {
  223. const eT tmp1 = Pea[count]; count++;
  224. const eT tmp2 = Pea[count]; count++;
  225. if(is_same_type<op_type, op_internal_equ >::yes) { (*s_col_data) = tmp1; s_col_data++; (*s_col_data) = tmp2; s_col_data++; }
  226. if(is_same_type<op_type, op_internal_plus >::yes) { (*s_col_data) += tmp1; s_col_data++; (*s_col_data) += tmp2; s_col_data++; }
  227. if(is_same_type<op_type, op_internal_minus>::yes) { (*s_col_data) -= tmp1; s_col_data++; (*s_col_data) -= tmp2; s_col_data++; }
  228. if(is_same_type<op_type, op_internal_schur>::yes) { (*s_col_data) *= tmp1; s_col_data++; (*s_col_data) *= tmp2; s_col_data++; }
  229. if(is_same_type<op_type, op_internal_div >::yes) { (*s_col_data) /= tmp1; s_col_data++; (*s_col_data) /= tmp2; s_col_data++; }
  230. }
  231. if((jj-1) < s_n_rows)
  232. {
  233. if(is_same_type<op_type, op_internal_equ >::yes) { (*s_col_data) = Pea[count]; count++; }
  234. if(is_same_type<op_type, op_internal_plus >::yes) { (*s_col_data) += Pea[count]; count++; }
  235. if(is_same_type<op_type, op_internal_minus>::yes) { (*s_col_data) -= Pea[count]; count++; }
  236. if(is_same_type<op_type, op_internal_schur>::yes) { (*s_col_data) *= Pea[count]; count++; }
  237. if(is_same_type<op_type, op_internal_div >::yes) { (*s_col_data) /= Pea[count]; count++; }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. template<typename eT>
  245. template<typename op_type>
  246. inline
  247. void
  248. subview<eT>::inplace_op(const subview<eT>& x, const char* identifier)
  249. {
  250. arma_extra_debug_sigprint();
  251. if(check_overlap(x))
  252. {
  253. const Mat<eT> tmp(x);
  254. if(is_same_type<op_type, op_internal_equ >::yes) { (*this).operator= (tmp); }
  255. if(is_same_type<op_type, op_internal_plus >::yes) { (*this).operator+=(tmp); }
  256. if(is_same_type<op_type, op_internal_minus>::yes) { (*this).operator-=(tmp); }
  257. if(is_same_type<op_type, op_internal_schur>::yes) { (*this).operator%=(tmp); }
  258. if(is_same_type<op_type, op_internal_div >::yes) { (*this).operator/=(tmp); }
  259. return;
  260. }
  261. subview<eT>& s = *this;
  262. arma_debug_assert_same_size(s, x, identifier);
  263. const uword s_n_cols = s.n_cols;
  264. const uword s_n_rows = s.n_rows;
  265. if(s_n_rows == 1)
  266. {
  267. Mat<eT>& A = const_cast< Mat<eT>& >(s.m);
  268. const Mat<eT>& B = x.m;
  269. const uword A_n_rows = A.n_rows;
  270. const uword B_n_rows = B.n_rows;
  271. eT* Aptr = &(A.at(s.aux_row1,s.aux_col1));
  272. const eT* Bptr = &(B.at(x.aux_row1,x.aux_col1));
  273. uword jj;
  274. for(jj=1; jj < s_n_cols; jj+=2)
  275. {
  276. const eT tmp1 = (*Bptr); Bptr += B_n_rows;
  277. const eT tmp2 = (*Bptr); Bptr += B_n_rows;
  278. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = tmp1; Aptr += A_n_rows; (*Aptr) = tmp2; Aptr += A_n_rows; }
  279. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += tmp1; Aptr += A_n_rows; (*Aptr) += tmp2; Aptr += A_n_rows; }
  280. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= tmp1; Aptr += A_n_rows; (*Aptr) -= tmp2; Aptr += A_n_rows; }
  281. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= tmp1; Aptr += A_n_rows; (*Aptr) *= tmp2; Aptr += A_n_rows; }
  282. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= tmp1; Aptr += A_n_rows; (*Aptr) /= tmp2; Aptr += A_n_rows; }
  283. }
  284. if((jj-1) < s_n_cols)
  285. {
  286. if(is_same_type<op_type, op_internal_equ >::yes) { (*Aptr) = (*Bptr); }
  287. if(is_same_type<op_type, op_internal_plus >::yes) { (*Aptr) += (*Bptr); }
  288. if(is_same_type<op_type, op_internal_minus>::yes) { (*Aptr) -= (*Bptr); }
  289. if(is_same_type<op_type, op_internal_schur>::yes) { (*Aptr) *= (*Bptr); }
  290. if(is_same_type<op_type, op_internal_div >::yes) { (*Aptr) /= (*Bptr); }
  291. }
  292. }
  293. else
  294. {
  295. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  296. {
  297. if(is_same_type<op_type, op_internal_equ >::yes) { arrayops::copy ( s.colptr(ucol), x.colptr(ucol), s_n_rows ); }
  298. if(is_same_type<op_type, op_internal_plus >::yes) { arrayops::inplace_plus ( s.colptr(ucol), x.colptr(ucol), s_n_rows ); }
  299. if(is_same_type<op_type, op_internal_minus>::yes) { arrayops::inplace_minus( s.colptr(ucol), x.colptr(ucol), s_n_rows ); }
  300. if(is_same_type<op_type, op_internal_schur>::yes) { arrayops::inplace_mul ( s.colptr(ucol), x.colptr(ucol), s_n_rows ); }
  301. if(is_same_type<op_type, op_internal_div >::yes) { arrayops::inplace_div ( s.colptr(ucol), x.colptr(ucol), s_n_rows ); }
  302. }
  303. }
  304. }
  305. template<typename eT>
  306. inline
  307. void
  308. subview<eT>::operator+= (const eT val)
  309. {
  310. arma_extra_debug_sigprint();
  311. inplace_op<op_internal_plus>(val);
  312. }
  313. template<typename eT>
  314. inline
  315. void
  316. subview<eT>::operator-= (const eT val)
  317. {
  318. arma_extra_debug_sigprint();
  319. inplace_op<op_internal_minus>(val);
  320. }
  321. template<typename eT>
  322. inline
  323. void
  324. subview<eT>::operator*= (const eT val)
  325. {
  326. arma_extra_debug_sigprint();
  327. inplace_op<op_internal_schur>(val);
  328. }
  329. template<typename eT>
  330. inline
  331. void
  332. subview<eT>::operator/= (const eT val)
  333. {
  334. arma_extra_debug_sigprint();
  335. inplace_op<op_internal_div>(val);
  336. }
  337. template<typename eT>
  338. inline
  339. void
  340. subview<eT>::operator= (const subview<eT>& x)
  341. {
  342. arma_extra_debug_sigprint();
  343. inplace_op<op_internal_equ>(x, "copy into submatrix");
  344. }
  345. template<typename eT>
  346. inline
  347. void
  348. subview<eT>::operator+= (const subview<eT>& x)
  349. {
  350. arma_extra_debug_sigprint();
  351. inplace_op<op_internal_plus>(x, "addition");
  352. }
  353. template<typename eT>
  354. inline
  355. void
  356. subview<eT>::operator-= (const subview<eT>& x)
  357. {
  358. arma_extra_debug_sigprint();
  359. inplace_op<op_internal_minus>(x, "subtraction");
  360. }
  361. template<typename eT>
  362. inline
  363. void
  364. subview<eT>::operator%= (const subview& x)
  365. {
  366. arma_extra_debug_sigprint();
  367. inplace_op<op_internal_schur>(x, "element-wise multiplication");
  368. }
  369. template<typename eT>
  370. inline
  371. void
  372. subview<eT>::operator/= (const subview& x)
  373. {
  374. arma_extra_debug_sigprint();
  375. inplace_op<op_internal_div>(x, "element-wise division");
  376. }
  377. template<typename eT>
  378. template<typename T1>
  379. inline
  380. void
  381. subview<eT>::operator= (const Base<eT,T1>& in)
  382. {
  383. arma_extra_debug_sigprint();
  384. inplace_op<op_internal_equ>(in, "copy into submatrix");
  385. }
  386. template<typename eT>
  387. template<typename T1>
  388. inline
  389. void
  390. subview<eT>::operator+= (const Base<eT,T1>& in)
  391. {
  392. arma_extra_debug_sigprint();
  393. inplace_op<op_internal_plus>(in, "addition");
  394. }
  395. template<typename eT>
  396. template<typename T1>
  397. inline
  398. void
  399. subview<eT>::operator-= (const Base<eT,T1>& in)
  400. {
  401. arma_extra_debug_sigprint();
  402. inplace_op<op_internal_minus>(in, "subtraction");
  403. }
  404. template<typename eT>
  405. template<typename T1>
  406. inline
  407. void
  408. subview<eT>::operator%= (const Base<eT,T1>& in)
  409. {
  410. arma_extra_debug_sigprint();
  411. inplace_op<op_internal_schur>(in, "element-wise multiplication");
  412. }
  413. template<typename eT>
  414. template<typename T1>
  415. inline
  416. void
  417. subview<eT>::operator/= (const Base<eT,T1>& in)
  418. {
  419. arma_extra_debug_sigprint();
  420. inplace_op<op_internal_div>(in, "element-wise division");
  421. }
  422. template<typename eT>
  423. template<typename T1>
  424. inline
  425. void
  426. subview<eT>::operator=(const SpBase<eT, T1>& x)
  427. {
  428. arma_extra_debug_sigprint();
  429. const SpProxy<T1> p(x.get_ref());
  430. arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(), "copy into submatrix");
  431. // Clear the subview.
  432. zeros();
  433. // Iterate through the sparse subview and set the nonzero values appropriately.
  434. typename SpProxy<T1>::const_iterator_type cit = p.begin();
  435. typename SpProxy<T1>::const_iterator_type cit_end = p.end();
  436. while(cit != cit_end)
  437. {
  438. at(cit.row(), cit.col()) = *cit;
  439. ++cit;
  440. }
  441. }
  442. template<typename eT>
  443. template<typename T1>
  444. inline
  445. void
  446. subview<eT>::operator+=(const SpBase<eT, T1>& x)
  447. {
  448. arma_extra_debug_sigprint();
  449. const SpProxy<T1> p(x.get_ref());
  450. arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(), "addition");
  451. // Iterate through the sparse subview and add its values.
  452. typename SpProxy<T1>::const_iterator_type cit = p.begin();
  453. typename SpProxy<T1>::const_iterator_type cit_end = p.end();
  454. while(cit != cit_end)
  455. {
  456. at(cit.row(), cit.col()) += *cit;
  457. ++cit;
  458. }
  459. }
  460. template<typename eT>
  461. template<typename T1>
  462. inline
  463. void
  464. subview<eT>::operator-=(const SpBase<eT, T1>& x)
  465. {
  466. arma_extra_debug_sigprint();
  467. const SpProxy<T1> p(x.get_ref());
  468. arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(), "subtraction");
  469. // Iterate through the sparse subview and subtract its values.
  470. typename SpProxy<T1>::const_iterator_type cit = p.begin();
  471. typename SpProxy<T1>::const_iterator_type cit_end = p.end();
  472. while(cit != cit_end)
  473. {
  474. at(cit.row(), cit.col()) -= *cit;
  475. ++cit;
  476. }
  477. }
  478. template<typename eT>
  479. template<typename T1>
  480. inline
  481. void
  482. subview<eT>::operator%=(const SpBase<eT, T1>& x)
  483. {
  484. arma_extra_debug_sigprint();
  485. const uword s_n_rows = (*this).n_rows;
  486. const uword s_n_cols = (*this).n_cols;
  487. const SpProxy<T1> p(x.get_ref());
  488. arma_debug_assert_same_size(s_n_rows, s_n_cols, p.get_n_rows(), p.get_n_cols(), "element-wise multiplication");
  489. if(n_elem == 0) { return; }
  490. if(p.get_n_nonzero() == 0) { (*this).zeros(); return; }
  491. // Iterate over nonzero values.
  492. // Any zero values in the sparse expression will result in a zero in our subview.
  493. typename SpProxy<T1>::const_iterator_type cit = p.begin();
  494. typename SpProxy<T1>::const_iterator_type cit_end = p.end();
  495. uword r = 0;
  496. uword c = 0;
  497. while(cit != cit_end)
  498. {
  499. const uword cit_row = cit.row();
  500. const uword cit_col = cit.col();
  501. while( ((r == cit_row) && (c == cit_col)) == false )
  502. {
  503. at(r,c) = eT(0);
  504. r++; if(r >= s_n_rows) { r = 0; c++; }
  505. }
  506. at(r, c) *= (*cit);
  507. ++cit;
  508. r++; if(r >= s_n_rows) { r = 0; c++; }
  509. }
  510. }
  511. template<typename eT>
  512. template<typename T1>
  513. inline
  514. void
  515. subview<eT>::operator/=(const SpBase<eT, T1>& x)
  516. {
  517. arma_extra_debug_sigprint();
  518. const SpProxy<T1> p(x.get_ref());
  519. arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(), "element-wise division");
  520. // This is probably going to fill your subview with a bunch of NaNs,
  521. // so I'm not going to bother to implement it fast.
  522. // You can have slow NaNs. They're fine too.
  523. for (uword c = 0; c < n_cols; ++c)
  524. for (uword r = 0; r < n_rows; ++r)
  525. {
  526. at(r, c) /= p.at(r, c);
  527. }
  528. }
  529. template<typename eT>
  530. template<typename T1, typename gen_type>
  531. inline
  532. typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result
  533. subview<eT>::operator= (const Gen<T1,gen_type>& in)
  534. {
  535. arma_extra_debug_sigprint();
  536. arma_debug_assert_same_size(n_rows, n_cols, in.n_rows, in.n_cols, "copy into submatrix");
  537. in.apply(*this);
  538. }
  539. //! apply a functor to each element
  540. template<typename eT>
  541. template<typename functor>
  542. inline
  543. void
  544. subview<eT>::for_each(functor F)
  545. {
  546. arma_extra_debug_sigprint();
  547. Mat<eT>& X = const_cast< Mat<eT>& >(m);
  548. if(n_rows == 1)
  549. {
  550. const uword urow = aux_row1;
  551. const uword start_col = aux_col1;
  552. const uword end_col_plus1 = start_col + n_cols;
  553. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  554. {
  555. F( X.at(urow, ucol) );
  556. }
  557. }
  558. else
  559. {
  560. const uword start_col = aux_col1;
  561. const uword start_row = aux_row1;
  562. const uword end_col_plus1 = start_col + n_cols;
  563. const uword end_row_plus1 = start_row + n_rows;
  564. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  565. for(uword urow = start_row; urow < end_row_plus1; ++urow)
  566. {
  567. F( X.at(urow, ucol) );
  568. }
  569. }
  570. }
  571. template<typename eT>
  572. template<typename functor>
  573. inline
  574. void
  575. subview<eT>::for_each(functor F) const
  576. {
  577. arma_extra_debug_sigprint();
  578. const Mat<eT>& X = m;
  579. if(n_rows == 1)
  580. {
  581. const uword urow = aux_row1;
  582. const uword start_col = aux_col1;
  583. const uword end_col_plus1 = start_col + n_cols;
  584. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  585. {
  586. F( X.at(urow, ucol) );
  587. }
  588. }
  589. else
  590. {
  591. const uword start_col = aux_col1;
  592. const uword start_row = aux_row1;
  593. const uword end_col_plus1 = start_col + n_cols;
  594. const uword end_row_plus1 = start_row + n_rows;
  595. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  596. for(uword urow = start_row; urow < end_row_plus1; ++urow)
  597. {
  598. F( X.at(urow, ucol) );
  599. }
  600. }
  601. }
  602. //! transform each element in the subview using a functor
  603. template<typename eT>
  604. template<typename functor>
  605. inline
  606. void
  607. subview<eT>::transform(functor F)
  608. {
  609. arma_extra_debug_sigprint();
  610. Mat<eT>& X = const_cast< Mat<eT>& >(m);
  611. if(n_rows == 1)
  612. {
  613. const uword urow = aux_row1;
  614. const uword start_col = aux_col1;
  615. const uword end_col_plus1 = start_col + n_cols;
  616. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  617. {
  618. X.at(urow, ucol) = eT( F( X.at(urow, ucol) ) );
  619. }
  620. }
  621. else
  622. {
  623. const uword start_col = aux_col1;
  624. const uword start_row = aux_row1;
  625. const uword end_col_plus1 = start_col + n_cols;
  626. const uword end_row_plus1 = start_row + n_rows;
  627. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  628. for(uword urow = start_row; urow < end_row_plus1; ++urow)
  629. {
  630. X.at(urow, ucol) = eT( F( X.at(urow, ucol) ) );
  631. }
  632. }
  633. }
  634. //! imbue (fill) the subview with values provided by a functor
  635. template<typename eT>
  636. template<typename functor>
  637. inline
  638. void
  639. subview<eT>::imbue(functor F)
  640. {
  641. arma_extra_debug_sigprint();
  642. Mat<eT>& X = const_cast< Mat<eT>& >(m);
  643. if(n_rows == 1)
  644. {
  645. const uword urow = aux_row1;
  646. const uword start_col = aux_col1;
  647. const uword end_col_plus1 = start_col + n_cols;
  648. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  649. {
  650. X.at(urow, ucol) = eT( F() );
  651. }
  652. }
  653. else
  654. {
  655. const uword start_col = aux_col1;
  656. const uword start_row = aux_row1;
  657. const uword end_col_plus1 = start_col + n_cols;
  658. const uword end_row_plus1 = start_row + n_rows;
  659. for(uword ucol = start_col; ucol < end_col_plus1; ++ucol)
  660. for(uword urow = start_row; urow < end_row_plus1; ++urow)
  661. {
  662. X.at(urow, ucol) = eT( F() );
  663. }
  664. }
  665. }
  666. template<typename eT>
  667. inline
  668. void
  669. subview<eT>::replace(const eT old_val, const eT new_val)
  670. {
  671. arma_extra_debug_sigprint();
  672. subview<eT>& s = *this;
  673. const uword s_n_cols = s.n_cols;
  674. const uword s_n_rows = s.n_rows;
  675. if(s_n_rows == 1)
  676. {
  677. Mat<eT>& A = const_cast< Mat<eT>& >(s.m);
  678. const uword A_n_rows = A.n_rows;
  679. eT* Aptr = &(A.at(s.aux_row1,s.aux_col1));
  680. if(arma_isnan(old_val))
  681. {
  682. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  683. {
  684. (*Aptr) = (arma_isnan(*Aptr)) ? new_val : (*Aptr);
  685. Aptr += A_n_rows;
  686. }
  687. }
  688. else
  689. {
  690. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  691. {
  692. (*Aptr) = ((*Aptr) == old_val) ? new_val : (*Aptr);
  693. Aptr += A_n_rows;
  694. }
  695. }
  696. }
  697. else
  698. {
  699. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  700. {
  701. arrayops::replace(s.colptr(ucol), s_n_rows, old_val, new_val);
  702. }
  703. }
  704. }
  705. template<typename eT>
  706. inline
  707. void
  708. subview<eT>::clean(const typename get_pod_type<eT>::result threshold)
  709. {
  710. arma_extra_debug_sigprint();
  711. subview<eT>& s = *this;
  712. const uword s_n_cols = s.n_cols;
  713. const uword s_n_rows = s.n_rows;
  714. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  715. {
  716. arrayops::clean( s.colptr(ucol), s_n_rows, threshold );
  717. }
  718. }
  719. template<typename eT>
  720. inline
  721. void
  722. subview<eT>::fill(const eT val)
  723. {
  724. arma_extra_debug_sigprint();
  725. subview<eT>& s = *this;
  726. const uword s_n_cols = s.n_cols;
  727. const uword s_n_rows = s.n_rows;
  728. if(s_n_rows == 1)
  729. {
  730. Mat<eT>& A = const_cast< Mat<eT>& >(s.m);
  731. const uword A_n_rows = A.n_rows;
  732. eT* Aptr = &(A.at(s.aux_row1,s.aux_col1));
  733. uword jj;
  734. for(jj=1; jj < s_n_cols; jj+=2)
  735. {
  736. (*Aptr) = val; Aptr += A_n_rows;
  737. (*Aptr) = val; Aptr += A_n_rows;
  738. }
  739. if((jj-1) < s_n_cols)
  740. {
  741. (*Aptr) = val;
  742. }
  743. }
  744. else
  745. {
  746. if( (s.aux_row1 == 0) && (s_n_rows == s.m.n_rows) )
  747. {
  748. arrayops::inplace_set( s.colptr(0), val, s.n_elem );
  749. return;
  750. }
  751. for(uword ucol=0; ucol < s_n_cols; ++ucol)
  752. {
  753. arrayops::inplace_set( s.colptr(ucol), val, s_n_rows );
  754. }
  755. }
  756. }
  757. template<typename eT>
  758. inline
  759. void
  760. subview<eT>::zeros()
  761. {
  762. arma_extra_debug_sigprint();
  763. (*this).fill(eT(0));
  764. }
  765. template<typename eT>
  766. inline
  767. void
  768. subview<eT>::ones()
  769. {
  770. arma_extra_debug_sigprint();
  771. (*this).fill(eT(1));
  772. }
  773. template<typename eT>
  774. inline
  775. void
  776. subview<eT>::eye()
  777. {
  778. arma_extra_debug_sigprint();
  779. (*this).zeros();
  780. const uword N = (std::min)(n_rows, n_cols);
  781. for(uword ii=0; ii < N; ++ii)
  782. {
  783. at(ii,ii) = eT(1);
  784. }
  785. }
  786. template<typename eT>
  787. inline
  788. void
  789. subview<eT>::randu()
  790. {
  791. arma_extra_debug_sigprint();
  792. const uword local_n_rows = n_rows;
  793. const uword local_n_cols = n_cols;
  794. if(local_n_rows == 1)
  795. {
  796. for(uword ii=0; ii < local_n_cols; ++ii)
  797. {
  798. at(0,ii) = eT(arma_rng::randu<eT>());
  799. }
  800. }
  801. else
  802. {
  803. for(uword ii=0; ii < local_n_cols; ++ii)
  804. {
  805. arma_rng::randu<eT>::fill( colptr(ii), local_n_rows );
  806. }
  807. }
  808. }
  809. template<typename eT>
  810. inline
  811. void
  812. subview<eT>::randn()
  813. {
  814. arma_extra_debug_sigprint();
  815. const uword local_n_rows = n_rows;
  816. const uword local_n_cols = n_cols;
  817. if(local_n_rows == 1)
  818. {
  819. for(uword ii=0; ii < local_n_cols; ++ii)
  820. {
  821. at(0,ii) = eT(arma_rng::randn<eT>());
  822. }
  823. }
  824. else
  825. {
  826. for(uword ii=0; ii < local_n_cols; ++ii)
  827. {
  828. arma_rng::randn<eT>::fill( colptr(ii), local_n_rows );
  829. }
  830. }
  831. }
  832. template<typename eT>
  833. inline
  834. eT
  835. subview<eT>::at_alt(const uword ii) const
  836. {
  837. return operator[](ii);
  838. }
  839. template<typename eT>
  840. inline
  841. eT&
  842. subview<eT>::operator[](const uword ii)
  843. {
  844. const uword in_col = ii / n_rows;
  845. const uword in_row = ii % n_rows;
  846. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  847. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  848. }
  849. template<typename eT>
  850. inline
  851. eT
  852. subview<eT>::operator[](const uword ii) const
  853. {
  854. const uword in_col = ii / n_rows;
  855. const uword in_row = ii % n_rows;
  856. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  857. return m.mem[index];
  858. }
  859. template<typename eT>
  860. inline
  861. eT&
  862. subview<eT>::operator()(const uword ii)
  863. {
  864. arma_debug_check( (ii >= n_elem), "subview::operator(): index out of bounds");
  865. const uword in_col = ii / n_rows;
  866. const uword in_row = ii % n_rows;
  867. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  868. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  869. }
  870. template<typename eT>
  871. inline
  872. eT
  873. subview<eT>::operator()(const uword ii) const
  874. {
  875. arma_debug_check( (ii >= n_elem), "subview::operator(): index out of bounds");
  876. const uword in_col = ii / n_rows;
  877. const uword in_row = ii % n_rows;
  878. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  879. return m.mem[index];
  880. }
  881. template<typename eT>
  882. inline
  883. eT&
  884. subview<eT>::operator()(const uword in_row, const uword in_col)
  885. {
  886. arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview::operator(): index out of bounds");
  887. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  888. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  889. }
  890. template<typename eT>
  891. inline
  892. eT
  893. subview<eT>::operator()(const uword in_row, const uword in_col) const
  894. {
  895. arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview::operator(): index out of bounds");
  896. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  897. return m.mem[index];
  898. }
  899. template<typename eT>
  900. inline
  901. eT&
  902. subview<eT>::at(const uword in_row, const uword in_col)
  903. {
  904. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  905. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  906. }
  907. template<typename eT>
  908. inline
  909. eT
  910. subview<eT>::at(const uword in_row, const uword in_col) const
  911. {
  912. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  913. return m.mem[index];
  914. }
  915. template<typename eT>
  916. inline
  917. eT&
  918. subview<eT>::front()
  919. {
  920. const uword index = aux_col1*m.n_rows + aux_row1;
  921. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  922. }
  923. template<typename eT>
  924. inline
  925. eT
  926. subview<eT>::front() const
  927. {
  928. const uword index = aux_col1*m.n_rows + aux_row1;
  929. return m.mem[index];
  930. }
  931. template<typename eT>
  932. inline
  933. eT&
  934. subview<eT>::back()
  935. {
  936. const uword in_row = n_rows - 1;
  937. const uword in_col = n_cols - 1;
  938. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  939. return access::rw( (const_cast< Mat<eT>& >(m)).mem[index] );
  940. }
  941. template<typename eT>
  942. inline
  943. eT
  944. subview<eT>::back() const
  945. {
  946. const uword in_row = n_rows - 1;
  947. const uword in_col = n_cols - 1;
  948. const uword index = (in_col + aux_col1)*m.n_rows + aux_row1 + in_row;
  949. return m.mem[index];
  950. }
  951. template<typename eT>
  952. arma_inline
  953. eT*
  954. subview<eT>::colptr(const uword in_col)
  955. {
  956. return & access::rw((const_cast< Mat<eT>& >(m)).mem[ (in_col + aux_col1)*m.n_rows + aux_row1 ]);
  957. }
  958. template<typename eT>
  959. arma_inline
  960. const eT*
  961. subview<eT>::colptr(const uword in_col) const
  962. {
  963. return & m.mem[ (in_col + aux_col1)*m.n_rows + aux_row1 ];
  964. }
  965. template<typename eT>
  966. template<typename eT2>
  967. inline
  968. bool
  969. subview<eT>::check_overlap(const subview<eT2>& x) const
  970. {
  971. if(is_same_type<eT,eT2>::value == false) { return false; }
  972. const subview<eT>& s = (*this);
  973. if(void_ptr(&(s.m)) != void_ptr(&(x.m))) { return false; }
  974. if( (s.n_elem == 0) || (x.n_elem == 0) ) { return false; }
  975. const uword s_row_start = s.aux_row1;
  976. const uword s_row_end_p1 = s_row_start + s.n_rows;
  977. const uword s_col_start = s.aux_col1;
  978. const uword s_col_end_p1 = s_col_start + s.n_cols;
  979. const uword x_row_start = x.aux_row1;
  980. const uword x_row_end_p1 = x_row_start + x.n_rows;
  981. const uword x_col_start = x.aux_col1;
  982. const uword x_col_end_p1 = x_col_start + x.n_cols;
  983. const bool outside_rows = ( (x_row_start >= s_row_end_p1) || (s_row_start >= x_row_end_p1) );
  984. const bool outside_cols = ( (x_col_start >= s_col_end_p1) || (s_col_start >= x_col_end_p1) );
  985. return ( (outside_rows == false) && (outside_cols == false) );
  986. }
  987. template<typename eT>
  988. inline
  989. arma_warn_unused
  990. bool
  991. subview<eT>::is_vec() const
  992. {
  993. return ( (n_rows == 1) || (n_cols == 1) );
  994. }
  995. template<typename eT>
  996. inline
  997. arma_warn_unused
  998. bool
  999. subview<eT>::is_finite() const
  1000. {
  1001. arma_extra_debug_sigprint();
  1002. const uword local_n_rows = n_rows;
  1003. const uword local_n_cols = n_cols;
  1004. for(uword ii=0; ii<local_n_cols; ++ii)
  1005. {
  1006. if(arrayops::is_finite(colptr(ii), local_n_rows) == false) { return false; }
  1007. }
  1008. return true;
  1009. }
  1010. template<typename eT>
  1011. inline
  1012. arma_warn_unused
  1013. bool
  1014. subview<eT>::is_zero(const typename get_pod_type<eT>::result tol) const
  1015. {
  1016. arma_extra_debug_sigprint();
  1017. const uword local_n_rows = n_rows;
  1018. const uword local_n_cols = n_cols;
  1019. for(uword ii=0; ii<local_n_cols; ++ii)
  1020. {
  1021. if(arrayops::is_zero(colptr(ii), local_n_rows, tol) == false) { return false; }
  1022. }
  1023. return true;
  1024. }
  1025. template<typename eT>
  1026. inline
  1027. arma_warn_unused
  1028. bool
  1029. subview<eT>::has_inf() const
  1030. {
  1031. arma_extra_debug_sigprint();
  1032. const uword local_n_rows = n_rows;
  1033. const uword local_n_cols = n_cols;
  1034. for(uword ii=0; ii<local_n_cols; ++ii)
  1035. {
  1036. if(arrayops::has_inf(colptr(ii), local_n_rows)) { return true; }
  1037. }
  1038. return false;
  1039. }
  1040. template<typename eT>
  1041. inline
  1042. arma_warn_unused
  1043. bool
  1044. subview<eT>::has_nan() const
  1045. {
  1046. arma_extra_debug_sigprint();
  1047. const uword local_n_rows = n_rows;
  1048. const uword local_n_cols = n_cols;
  1049. for(uword ii=0; ii<local_n_cols; ++ii)
  1050. {
  1051. if(arrayops::has_nan(colptr(ii), local_n_rows)) { return true; }
  1052. }
  1053. return false;
  1054. }
  1055. //! X = Y.submat(...)
  1056. template<typename eT>
  1057. inline
  1058. void
  1059. subview<eT>::extract(Mat<eT>& out, const subview<eT>& in)
  1060. {
  1061. arma_extra_debug_sigprint();
  1062. // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing;
  1063. // size setting and alias checking is done by either the Mat contructor or operator=()
  1064. const uword n_rows = in.n_rows; // number of rows in the subview
  1065. const uword n_cols = in.n_cols; // number of columns in the subview
  1066. arma_extra_debug_print(arma_str::format("out.n_rows = %d out.n_cols = %d in.m.n_rows = %d in.m.n_cols = %d") % out.n_rows % out.n_cols % in.m.n_rows % in.m.n_cols );
  1067. if(in.is_vec() == true)
  1068. {
  1069. if(n_cols == 1) // a column vector
  1070. {
  1071. arma_extra_debug_print("subview::extract(): copying col (going across rows)");
  1072. // in.colptr(0) the first column of the subview, taking into account any row offset
  1073. arrayops::copy( out.memptr(), in.colptr(0), n_rows );
  1074. }
  1075. else // a row vector (possibly empty)
  1076. {
  1077. arma_extra_debug_print("subview::extract(): copying row (going across columns)");
  1078. eT* out_mem = out.memptr();
  1079. const uword X_n_rows = in.m.n_rows;
  1080. const eT* Xptr = &(in.m.at(in.aux_row1,in.aux_col1));
  1081. uword j;
  1082. for(j=1; j < n_cols; j+=2)
  1083. {
  1084. const eT tmp1 = (*Xptr); Xptr += X_n_rows;
  1085. const eT tmp2 = (*Xptr); Xptr += X_n_rows;
  1086. (*out_mem) = tmp1; out_mem++;
  1087. (*out_mem) = tmp2; out_mem++;
  1088. }
  1089. if((j-1) < n_cols)
  1090. {
  1091. (*out_mem) = (*Xptr);
  1092. }
  1093. }
  1094. }
  1095. else // general submatrix
  1096. {
  1097. arma_extra_debug_print("subview::extract(): general submatrix");
  1098. if( (in.aux_row1 == 0) && (n_rows == in.m.n_rows) )
  1099. {
  1100. arrayops::copy( out.memptr(), in.colptr(0), in.n_elem );
  1101. return;
  1102. }
  1103. for(uword col=0; col < n_cols; ++col)
  1104. {
  1105. arrayops::copy( out.colptr(col), in.colptr(col), n_rows );
  1106. }
  1107. }
  1108. }
  1109. //! X += Y.submat(...)
  1110. template<typename eT>
  1111. inline
  1112. void
  1113. subview<eT>::plus_inplace(Mat<eT>& out, const subview<eT>& in)
  1114. {
  1115. arma_extra_debug_sigprint();
  1116. arma_debug_assert_same_size(out, in, "addition");
  1117. const uword n_rows = in.n_rows;
  1118. const uword n_cols = in.n_cols;
  1119. if(n_rows == 1)
  1120. {
  1121. eT* out_mem = out.memptr();
  1122. const Mat<eT>& X = in.m;
  1123. const uword row = in.aux_row1;
  1124. const uword start_col = in.aux_col1;
  1125. uword i,j;
  1126. for(i=0, j=1; j < n_cols; i+=2, j+=2)
  1127. {
  1128. const eT tmp1 = X.at(row, start_col+i);
  1129. const eT tmp2 = X.at(row, start_col+j);
  1130. out_mem[i] += tmp1;
  1131. out_mem[j] += tmp2;
  1132. }
  1133. if(i < n_cols)
  1134. {
  1135. out_mem[i] += X.at(row, start_col+i);
  1136. }
  1137. }
  1138. else
  1139. {
  1140. for(uword col=0; col < n_cols; ++col)
  1141. {
  1142. arrayops::inplace_plus(out.colptr(col), in.colptr(col), n_rows);
  1143. }
  1144. }
  1145. }
  1146. //! X -= Y.submat(...)
  1147. template<typename eT>
  1148. inline
  1149. void
  1150. subview<eT>::minus_inplace(Mat<eT>& out, const subview<eT>& in)
  1151. {
  1152. arma_extra_debug_sigprint();
  1153. arma_debug_assert_same_size(out, in, "subtraction");
  1154. const uword n_rows = in.n_rows;
  1155. const uword n_cols = in.n_cols;
  1156. if(n_rows == 1)
  1157. {
  1158. eT* out_mem = out.memptr();
  1159. const Mat<eT>& X = in.m;
  1160. const uword row = in.aux_row1;
  1161. const uword start_col = in.aux_col1;
  1162. uword i,j;
  1163. for(i=0, j=1; j < n_cols; i+=2, j+=2)
  1164. {
  1165. const eT tmp1 = X.at(row, start_col+i);
  1166. const eT tmp2 = X.at(row, start_col+j);
  1167. out_mem[i] -= tmp1;
  1168. out_mem[j] -= tmp2;
  1169. }
  1170. if(i < n_cols)
  1171. {
  1172. out_mem[i] -= X.at(row, start_col+i);
  1173. }
  1174. }
  1175. else
  1176. {
  1177. for(uword col=0; col < n_cols; ++col)
  1178. {
  1179. arrayops::inplace_minus(out.colptr(col), in.colptr(col), n_rows);
  1180. }
  1181. }
  1182. }
  1183. //! X %= Y.submat(...)
  1184. template<typename eT>
  1185. inline
  1186. void
  1187. subview<eT>::schur_inplace(Mat<eT>& out, const subview<eT>& in)
  1188. {
  1189. arma_extra_debug_sigprint();
  1190. arma_debug_assert_same_size(out, in, "element-wise multiplication");
  1191. const uword n_rows = in.n_rows;
  1192. const uword n_cols = in.n_cols;
  1193. if(n_rows == 1)
  1194. {
  1195. eT* out_mem = out.memptr();
  1196. const Mat<eT>& X = in.m;
  1197. const uword row = in.aux_row1;
  1198. const uword start_col = in.aux_col1;
  1199. uword i,j;
  1200. for(i=0, j=1; j < n_cols; i+=2, j+=2)
  1201. {
  1202. const eT tmp1 = X.at(row, start_col+i);
  1203. const eT tmp2 = X.at(row, start_col+j);
  1204. out_mem[i] *= tmp1;
  1205. out_mem[j] *= tmp2;
  1206. }
  1207. if(i < n_cols)
  1208. {
  1209. out_mem[i] *= X.at(row, start_col+i);
  1210. }
  1211. }
  1212. else
  1213. {
  1214. for(uword col=0; col < n_cols; ++col)
  1215. {
  1216. arrayops::inplace_mul(out.colptr(col), in.colptr(col), n_rows);
  1217. }
  1218. }
  1219. }
  1220. //! X /= Y.submat(...)
  1221. template<typename eT>
  1222. inline
  1223. void
  1224. subview<eT>::div_inplace(Mat<eT>& out, const subview<eT>& in)
  1225. {
  1226. arma_extra_debug_sigprint();
  1227. arma_debug_assert_same_size(out, in, "element-wise division");
  1228. const uword n_rows = in.n_rows;
  1229. const uword n_cols = in.n_cols;
  1230. if(n_rows == 1)
  1231. {
  1232. eT* out_mem = out.memptr();
  1233. const Mat<eT>& X = in.m;
  1234. const uword row = in.aux_row1;
  1235. const uword start_col = in.aux_col1;
  1236. uword i,j;
  1237. for(i=0, j=1; j < n_cols; i+=2, j+=2)
  1238. {
  1239. const eT tmp1 = X.at(row, start_col+i);
  1240. const eT tmp2 = X.at(row, start_col+j);
  1241. out_mem[i] /= tmp1;
  1242. out_mem[j] /= tmp2;
  1243. }
  1244. if(i < n_cols)
  1245. {
  1246. out_mem[i] /= X.at(row, start_col+i);
  1247. }
  1248. }
  1249. else
  1250. {
  1251. for(uword col=0; col < n_cols; ++col)
  1252. {
  1253. arrayops::inplace_div(out.colptr(col), in.colptr(col), n_rows);
  1254. }
  1255. }
  1256. }
  1257. //! creation of subview (row vector)
  1258. template<typename eT>
  1259. inline
  1260. subview_row<eT>
  1261. subview<eT>::row(const uword row_num)
  1262. {
  1263. arma_extra_debug_sigprint();
  1264. arma_debug_check( row_num >= n_rows, "subview::row(): out of bounds" );
  1265. const uword base_row = aux_row1 + row_num;
  1266. return subview_row<eT>(m, base_row, aux_col1, n_cols);
  1267. }
  1268. //! creation of subview (row vector)
  1269. template<typename eT>
  1270. inline
  1271. const subview_row<eT>
  1272. subview<eT>::row(const uword row_num) const
  1273. {
  1274. arma_extra_debug_sigprint();
  1275. arma_debug_check( row_num >= n_rows, "subview::row(): out of bounds" );
  1276. const uword base_row = aux_row1 + row_num;
  1277. return subview_row<eT>(m, base_row, aux_col1, n_cols);
  1278. }
  1279. template<typename eT>
  1280. inline
  1281. subview_row<eT>
  1282. subview<eT>::operator()(const uword row_num, const span& col_span)
  1283. {
  1284. arma_extra_debug_sigprint();
  1285. const bool col_all = col_span.whole;
  1286. const uword local_n_cols = n_cols;
  1287. const uword in_col1 = col_all ? 0 : col_span.a;
  1288. const uword in_col2 = col_span.b;
  1289. const uword submat_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1290. const uword base_col1 = aux_col1 + in_col1;
  1291. const uword base_row = aux_row1 + row_num;
  1292. arma_debug_check
  1293. (
  1294. (row_num >= n_rows)
  1295. ||
  1296. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1297. ,
  1298. "subview::operator(): indices out of bounds or incorrectly used"
  1299. );
  1300. return subview_row<eT>(m, base_row, base_col1, submat_n_cols);
  1301. }
  1302. template<typename eT>
  1303. inline
  1304. const subview_row<eT>
  1305. subview<eT>::operator()(const uword row_num, const span& col_span) const
  1306. {
  1307. arma_extra_debug_sigprint();
  1308. const bool col_all = col_span.whole;
  1309. const uword local_n_cols = n_cols;
  1310. const uword in_col1 = col_all ? 0 : col_span.a;
  1311. const uword in_col2 = col_span.b;
  1312. const uword submat_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1313. const uword base_col1 = aux_col1 + in_col1;
  1314. const uword base_row = aux_row1 + row_num;
  1315. arma_debug_check
  1316. (
  1317. (row_num >= n_rows)
  1318. ||
  1319. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1320. ,
  1321. "subview::operator(): indices out of bounds or incorrectly used"
  1322. );
  1323. return subview_row<eT>(m, base_row, base_col1, submat_n_cols);
  1324. }
  1325. //! creation of subview (column vector)
  1326. template<typename eT>
  1327. inline
  1328. subview_col<eT>
  1329. subview<eT>::col(const uword col_num)
  1330. {
  1331. arma_extra_debug_sigprint();
  1332. arma_debug_check( col_num >= n_cols, "subview::col(): out of bounds");
  1333. const uword base_col = aux_col1 + col_num;
  1334. return subview_col<eT>(m, base_col, aux_row1, n_rows);
  1335. }
  1336. //! creation of subview (column vector)
  1337. template<typename eT>
  1338. inline
  1339. const subview_col<eT>
  1340. subview<eT>::col(const uword col_num) const
  1341. {
  1342. arma_extra_debug_sigprint();
  1343. arma_debug_check( col_num >= n_cols, "subview::col(): out of bounds");
  1344. const uword base_col = aux_col1 + col_num;
  1345. return subview_col<eT>(m, base_col, aux_row1, n_rows);
  1346. }
  1347. template<typename eT>
  1348. inline
  1349. subview_col<eT>
  1350. subview<eT>::operator()(const span& row_span, const uword col_num)
  1351. {
  1352. arma_extra_debug_sigprint();
  1353. const bool row_all = row_span.whole;
  1354. const uword local_n_rows = n_rows;
  1355. const uword in_row1 = row_all ? 0 : row_span.a;
  1356. const uword in_row2 = row_span.b;
  1357. const uword submat_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1358. const uword base_row1 = aux_row1 + in_row1;
  1359. const uword base_col = aux_col1 + col_num;
  1360. arma_debug_check
  1361. (
  1362. (col_num >= n_cols)
  1363. ||
  1364. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1365. ,
  1366. "subview::operator(): indices out of bounds or incorrectly used"
  1367. );
  1368. return subview_col<eT>(m, base_col, base_row1, submat_n_rows);
  1369. }
  1370. template<typename eT>
  1371. inline
  1372. const subview_col<eT>
  1373. subview<eT>::operator()(const span& row_span, const uword col_num) const
  1374. {
  1375. arma_extra_debug_sigprint();
  1376. const bool row_all = row_span.whole;
  1377. const uword local_n_rows = n_rows;
  1378. const uword in_row1 = row_all ? 0 : row_span.a;
  1379. const uword in_row2 = row_span.b;
  1380. const uword submat_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1381. const uword base_row1 = aux_row1 + in_row1;
  1382. const uword base_col = aux_col1 + col_num;
  1383. arma_debug_check
  1384. (
  1385. (col_num >= n_cols)
  1386. ||
  1387. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1388. ,
  1389. "subview::operator(): indices out of bounds or incorrectly used"
  1390. );
  1391. return subview_col<eT>(m, base_col, base_row1, submat_n_rows);
  1392. }
  1393. //! create a Col object which uses memory from an existing matrix object.
  1394. //! this approach is currently not alias safe
  1395. //! and does not take into account that the parent matrix object could be deleted.
  1396. //! if deleted memory is accessed by the created Col object,
  1397. //! it will cause memory corruption and/or a crash
  1398. template<typename eT>
  1399. inline
  1400. Col<eT>
  1401. subview<eT>::unsafe_col(const uword col_num)
  1402. {
  1403. arma_extra_debug_sigprint();
  1404. arma_debug_check( col_num >= n_cols, "subview::unsafe_col(): out of bounds");
  1405. return Col<eT>(colptr(col_num), n_rows, false, true);
  1406. }
  1407. //! create a Col object which uses memory from an existing matrix object.
  1408. //! this approach is currently not alias safe
  1409. //! and does not take into account that the parent matrix object could be deleted.
  1410. //! if deleted memory is accessed by the created Col object,
  1411. //! it will cause memory corruption and/or a crash
  1412. template<typename eT>
  1413. inline
  1414. const Col<eT>
  1415. subview<eT>::unsafe_col(const uword col_num) const
  1416. {
  1417. arma_extra_debug_sigprint();
  1418. arma_debug_check( col_num >= n_cols, "subview::unsafe_col(): out of bounds");
  1419. return Col<eT>(const_cast<eT*>(colptr(col_num)), n_rows, false, true);
  1420. }
  1421. //! creation of subview (submatrix comprised of specified row vectors)
  1422. template<typename eT>
  1423. inline
  1424. subview<eT>
  1425. subview<eT>::rows(const uword in_row1, const uword in_row2)
  1426. {
  1427. arma_extra_debug_sigprint();
  1428. arma_debug_check
  1429. (
  1430. (in_row1 > in_row2) || (in_row2 >= n_rows),
  1431. "subview::rows(): indices out of bounds or incorrectly used"
  1432. );
  1433. const uword subview_n_rows = in_row2 - in_row1 + 1;
  1434. const uword base_row1 = aux_row1 + in_row1;
  1435. return subview<eT>(m, base_row1, aux_col1, subview_n_rows, n_cols );
  1436. }
  1437. //! creation of subview (submatrix comprised of specified row vectors)
  1438. template<typename eT>
  1439. inline
  1440. const subview<eT>
  1441. subview<eT>::rows(const uword in_row1, const uword in_row2) const
  1442. {
  1443. arma_extra_debug_sigprint();
  1444. arma_debug_check
  1445. (
  1446. (in_row1 > in_row2) || (in_row2 >= n_rows),
  1447. "subview::rows(): indices out of bounds or incorrectly used"
  1448. );
  1449. const uword subview_n_rows = in_row2 - in_row1 + 1;
  1450. const uword base_row1 = aux_row1 + in_row1;
  1451. return subview<eT>(m, base_row1, aux_col1, subview_n_rows, n_cols );
  1452. }
  1453. //! creation of subview (submatrix comprised of specified column vectors)
  1454. template<typename eT>
  1455. inline
  1456. subview<eT>
  1457. subview<eT>::cols(const uword in_col1, const uword in_col2)
  1458. {
  1459. arma_extra_debug_sigprint();
  1460. arma_debug_check
  1461. (
  1462. (in_col1 > in_col2) || (in_col2 >= n_cols),
  1463. "subview::cols(): indices out of bounds or incorrectly used"
  1464. );
  1465. const uword subview_n_cols = in_col2 - in_col1 + 1;
  1466. const uword base_col1 = aux_col1 + in_col1;
  1467. return subview<eT>(m, aux_row1, base_col1, n_rows, subview_n_cols);
  1468. }
  1469. //! creation of subview (submatrix comprised of specified column vectors)
  1470. template<typename eT>
  1471. inline
  1472. const subview<eT>
  1473. subview<eT>::cols(const uword in_col1, const uword in_col2) const
  1474. {
  1475. arma_extra_debug_sigprint();
  1476. arma_debug_check
  1477. (
  1478. (in_col1 > in_col2) || (in_col2 >= n_cols),
  1479. "subview::cols(): indices out of bounds or incorrectly used"
  1480. );
  1481. const uword subview_n_cols = in_col2 - in_col1 + 1;
  1482. const uword base_col1 = aux_col1 + in_col1;
  1483. return subview<eT>(m, aux_row1, base_col1, n_rows, subview_n_cols);
  1484. }
  1485. //! creation of subview (submatrix)
  1486. template<typename eT>
  1487. inline
  1488. subview<eT>
  1489. subview<eT>::submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2)
  1490. {
  1491. arma_extra_debug_sigprint();
  1492. arma_debug_check
  1493. (
  1494. (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
  1495. "subview::submat(): indices out of bounds or incorrectly used"
  1496. );
  1497. const uword subview_n_rows = in_row2 - in_row1 + 1;
  1498. const uword subview_n_cols = in_col2 - in_col1 + 1;
  1499. const uword base_row1 = aux_row1 + in_row1;
  1500. const uword base_col1 = aux_col1 + in_col1;
  1501. return subview<eT>(m, base_row1, base_col1, subview_n_rows, subview_n_cols);
  1502. }
  1503. //! creation of subview (generic submatrix)
  1504. template<typename eT>
  1505. inline
  1506. const subview<eT>
  1507. subview<eT>::submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const
  1508. {
  1509. arma_extra_debug_sigprint();
  1510. arma_debug_check
  1511. (
  1512. (in_row1 > in_row2) || (in_col1 > in_col2) || (in_row2 >= n_rows) || (in_col2 >= n_cols),
  1513. "subview::submat(): indices out of bounds or incorrectly used"
  1514. );
  1515. const uword subview_n_rows = in_row2 - in_row1 + 1;
  1516. const uword subview_n_cols = in_col2 - in_col1 + 1;
  1517. const uword base_row1 = aux_row1 + in_row1;
  1518. const uword base_col1 = aux_col1 + in_col1;
  1519. return subview<eT>(m, base_row1, base_col1, subview_n_rows, subview_n_cols);
  1520. }
  1521. //! creation of subview (submatrix)
  1522. template<typename eT>
  1523. inline
  1524. subview<eT>
  1525. subview<eT>::submat(const span& row_span, const span& col_span)
  1526. {
  1527. arma_extra_debug_sigprint();
  1528. const bool row_all = row_span.whole;
  1529. const bool col_all = col_span.whole;
  1530. const uword local_n_rows = n_rows;
  1531. const uword local_n_cols = n_cols;
  1532. const uword in_row1 = row_all ? 0 : row_span.a;
  1533. const uword in_row2 = row_span.b;
  1534. const uword submat_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1535. const uword in_col1 = col_all ? 0 : col_span.a;
  1536. const uword in_col2 = col_span.b;
  1537. const uword submat_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1538. arma_debug_check
  1539. (
  1540. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1541. ||
  1542. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1543. ,
  1544. "subview::submat(): indices out of bounds or incorrectly used"
  1545. );
  1546. const uword base_row1 = aux_row1 + in_row1;
  1547. const uword base_col1 = aux_col1 + in_col1;
  1548. return subview<eT>(m, base_row1, base_col1, submat_n_rows, submat_n_cols);
  1549. }
  1550. //! creation of subview (generic submatrix)
  1551. template<typename eT>
  1552. inline
  1553. const subview<eT>
  1554. subview<eT>::submat(const span& row_span, const span& col_span) const
  1555. {
  1556. arma_extra_debug_sigprint();
  1557. const bool row_all = row_span.whole;
  1558. const bool col_all = col_span.whole;
  1559. const uword local_n_rows = n_rows;
  1560. const uword local_n_cols = n_cols;
  1561. const uword in_row1 = row_all ? 0 : row_span.a;
  1562. const uword in_row2 = row_span.b;
  1563. const uword submat_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1564. const uword in_col1 = col_all ? 0 : col_span.a;
  1565. const uword in_col2 = col_span.b;
  1566. const uword submat_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1567. arma_debug_check
  1568. (
  1569. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1570. ||
  1571. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1572. ,
  1573. "subview::submat(): indices out of bounds or incorrectly used"
  1574. );
  1575. const uword base_row1 = aux_row1 + in_row1;
  1576. const uword base_col1 = aux_col1 + in_col1;
  1577. return subview<eT>(m, base_row1, base_col1, submat_n_rows, submat_n_cols);
  1578. }
  1579. template<typename eT>
  1580. inline
  1581. subview<eT>
  1582. subview<eT>::operator()(const span& row_span, const span& col_span)
  1583. {
  1584. arma_extra_debug_sigprint();
  1585. return (*this).submat(row_span, col_span);
  1586. }
  1587. template<typename eT>
  1588. inline
  1589. const subview<eT>
  1590. subview<eT>::operator()(const span& row_span, const span& col_span) const
  1591. {
  1592. arma_extra_debug_sigprint();
  1593. return (*this).submat(row_span, col_span);
  1594. }
  1595. template<typename eT>
  1596. inline
  1597. subview_each1< subview<eT>, 0 >
  1598. subview<eT>::each_col()
  1599. {
  1600. arma_extra_debug_sigprint();
  1601. return subview_each1< subview<eT>, 0 >(*this);
  1602. }
  1603. template<typename eT>
  1604. inline
  1605. subview_each1< subview<eT>, 1 >
  1606. subview<eT>::each_row()
  1607. {
  1608. arma_extra_debug_sigprint();
  1609. return subview_each1< subview<eT>, 1 >(*this);
  1610. }
  1611. template<typename eT>
  1612. template<typename T1>
  1613. inline
  1614. subview_each2< subview<eT>, 0, T1 >
  1615. subview<eT>::each_col(const Base<uword,T1>& indices)
  1616. {
  1617. arma_extra_debug_sigprint();
  1618. return subview_each2< subview<eT>, 0, T1 >(*this, indices);
  1619. }
  1620. template<typename eT>
  1621. template<typename T1>
  1622. inline
  1623. subview_each2< subview<eT>, 1, T1 >
  1624. subview<eT>::each_row(const Base<uword,T1>& indices)
  1625. {
  1626. arma_extra_debug_sigprint();
  1627. return subview_each2< subview<eT>, 1, T1 >(*this, indices);
  1628. }
  1629. #if defined(ARMA_USE_CXX11)
  1630. //! apply a lambda function to each column, where each column is interpreted as a column vector
  1631. template<typename eT>
  1632. inline
  1633. void
  1634. subview<eT>::each_col(const std::function< void(Col<eT>&) >& F)
  1635. {
  1636. arma_extra_debug_sigprint();
  1637. for(uword ii=0; ii < n_cols; ++ii)
  1638. {
  1639. Col<eT> tmp(colptr(ii), n_rows, false, true);
  1640. F(tmp);
  1641. }
  1642. }
  1643. template<typename eT>
  1644. inline
  1645. void
  1646. subview<eT>::each_col(const std::function< void(const Col<eT>&) >& F) const
  1647. {
  1648. arma_extra_debug_sigprint();
  1649. for(uword ii=0; ii < n_cols; ++ii)
  1650. {
  1651. const Col<eT> tmp(colptr(ii), n_rows, false, true);
  1652. F(tmp);
  1653. }
  1654. }
  1655. //! apply a lambda function to each row, where each row is interpreted as a row vector
  1656. template<typename eT>
  1657. inline
  1658. void
  1659. subview<eT>::each_row(const std::function< void(Row<eT>&) >& F)
  1660. {
  1661. arma_extra_debug_sigprint();
  1662. podarray<eT> array1(n_cols);
  1663. podarray<eT> array2(n_cols);
  1664. Row<eT> tmp1( array1.memptr(), n_cols, false, true );
  1665. Row<eT> tmp2( array2.memptr(), n_cols, false, true );
  1666. eT* tmp1_mem = tmp1.memptr();
  1667. eT* tmp2_mem = tmp2.memptr();
  1668. uword ii, jj;
  1669. for(ii=0, jj=1; jj < n_rows; ii+=2, jj+=2)
  1670. {
  1671. for(uword col_id = 0; col_id < n_cols; ++col_id)
  1672. {
  1673. const eT* col_mem = colptr(col_id);
  1674. tmp1_mem[col_id] = col_mem[ii];
  1675. tmp2_mem[col_id] = col_mem[jj];
  1676. }
  1677. F(tmp1);
  1678. F(tmp2);
  1679. for(uword col_id = 0; col_id < n_cols; ++col_id)
  1680. {
  1681. eT* col_mem = colptr(col_id);
  1682. col_mem[ii] = tmp1_mem[col_id];
  1683. col_mem[jj] = tmp2_mem[col_id];
  1684. }
  1685. }
  1686. if(ii < n_rows)
  1687. {
  1688. tmp1 = (*this).row(ii);
  1689. F(tmp1);
  1690. (*this).row(ii) = tmp1;
  1691. }
  1692. }
  1693. template<typename eT>
  1694. inline
  1695. void
  1696. subview<eT>::each_row(const std::function< void(const Row<eT>&) >& F) const
  1697. {
  1698. arma_extra_debug_sigprint();
  1699. podarray<eT> array1(n_cols);
  1700. podarray<eT> array2(n_cols);
  1701. Row<eT> tmp1( array1.memptr(), n_cols, false, true );
  1702. Row<eT> tmp2( array2.memptr(), n_cols, false, true );
  1703. eT* tmp1_mem = tmp1.memptr();
  1704. eT* tmp2_mem = tmp2.memptr();
  1705. uword ii, jj;
  1706. for(ii=0, jj=1; jj < n_rows; ii+=2, jj+=2)
  1707. {
  1708. for(uword col_id = 0; col_id < n_cols; ++col_id)
  1709. {
  1710. const eT* col_mem = colptr(col_id);
  1711. tmp1_mem[col_id] = col_mem[ii];
  1712. tmp2_mem[col_id] = col_mem[jj];
  1713. }
  1714. F(tmp1);
  1715. F(tmp2);
  1716. }
  1717. if(ii < n_rows)
  1718. {
  1719. tmp1 = (*this).row(ii);
  1720. F(tmp1);
  1721. }
  1722. }
  1723. #endif
  1724. //! creation of diagview (diagonal)
  1725. template<typename eT>
  1726. inline
  1727. diagview<eT>
  1728. subview<eT>::diag(const sword in_id)
  1729. {
  1730. arma_extra_debug_sigprint();
  1731. const uword row_offset = (in_id < 0) ? uword(-in_id) : 0;
  1732. const uword col_offset = (in_id > 0) ? uword( in_id) : 0;
  1733. arma_debug_check
  1734. (
  1735. ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)),
  1736. "subview::diag(): requested diagonal out of bounds"
  1737. );
  1738. const uword len = (std::min)(n_rows - row_offset, n_cols - col_offset);
  1739. const uword base_row_offset = aux_row1 + row_offset;
  1740. const uword base_col_offset = aux_col1 + col_offset;
  1741. return diagview<eT>(m, base_row_offset, base_col_offset, len);
  1742. }
  1743. //! creation of diagview (diagonal)
  1744. template<typename eT>
  1745. inline
  1746. const diagview<eT>
  1747. subview<eT>::diag(const sword in_id) const
  1748. {
  1749. arma_extra_debug_sigprint();
  1750. const uword row_offset = uword( (in_id < 0) ? -in_id : 0 );
  1751. const uword col_offset = uword( (in_id > 0) ? in_id : 0 );
  1752. arma_debug_check
  1753. (
  1754. ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)),
  1755. "subview::diag(): requested diagonal out of bounds"
  1756. );
  1757. const uword len = (std::min)(n_rows - row_offset, n_cols - col_offset);
  1758. const uword base_row_offset = aux_row1 + row_offset;
  1759. const uword base_col_offset = aux_col1 + col_offset;
  1760. return diagview<eT>(m, base_row_offset, base_col_offset, len);
  1761. }
  1762. template<typename eT>
  1763. inline
  1764. void
  1765. subview<eT>::swap_rows(const uword in_row1, const uword in_row2)
  1766. {
  1767. arma_extra_debug_sigprint();
  1768. arma_debug_check
  1769. (
  1770. (in_row1 >= n_rows) || (in_row2 >= n_rows),
  1771. "subview::swap_rows(): out of bounds"
  1772. );
  1773. eT* mem = (const_cast< Mat<eT>& >(m)).memptr();
  1774. if(n_elem > 0)
  1775. {
  1776. const uword m_n_rows = m.n_rows;
  1777. for(uword ucol=0; ucol < n_cols; ++ucol)
  1778. {
  1779. const uword offset = (aux_col1 + ucol) * m_n_rows;
  1780. const uword pos1 = aux_row1 + in_row1 + offset;
  1781. const uword pos2 = aux_row1 + in_row2 + offset;
  1782. std::swap( access::rw(mem[pos1]), access::rw(mem[pos2]) );
  1783. }
  1784. }
  1785. }
  1786. template<typename eT>
  1787. inline
  1788. void
  1789. subview<eT>::swap_cols(const uword in_col1, const uword in_col2)
  1790. {
  1791. arma_extra_debug_sigprint();
  1792. arma_debug_check
  1793. (
  1794. (in_col1 >= n_cols) || (in_col2 >= n_cols),
  1795. "subview::swap_cols(): out of bounds"
  1796. );
  1797. if(n_elem > 0)
  1798. {
  1799. eT* ptr1 = colptr(in_col1);
  1800. eT* ptr2 = colptr(in_col2);
  1801. for(uword urow=0; urow < n_rows; ++urow)
  1802. {
  1803. std::swap( ptr1[urow], ptr2[urow] );
  1804. }
  1805. }
  1806. }
  1807. template<typename eT>
  1808. inline
  1809. typename subview<eT>::iterator
  1810. subview<eT>::begin()
  1811. {
  1812. return iterator(*this, aux_row1, aux_col1);
  1813. }
  1814. template<typename eT>
  1815. inline
  1816. typename subview<eT>::const_iterator
  1817. subview<eT>::begin() const
  1818. {
  1819. return const_iterator(*this, aux_row1, aux_col1);
  1820. }
  1821. template<typename eT>
  1822. inline
  1823. typename subview<eT>::const_iterator
  1824. subview<eT>::cbegin() const
  1825. {
  1826. return const_iterator(*this, aux_row1, aux_col1);
  1827. }
  1828. template<typename eT>
  1829. inline
  1830. typename subview<eT>::iterator
  1831. subview<eT>::end()
  1832. {
  1833. return iterator(*this, aux_row1, aux_col1 + n_cols);
  1834. }
  1835. template<typename eT>
  1836. inline
  1837. typename subview<eT>::const_iterator
  1838. subview<eT>::end() const
  1839. {
  1840. return const_iterator(*this, aux_row1, aux_col1 + n_cols);
  1841. }
  1842. template<typename eT>
  1843. inline
  1844. typename subview<eT>::const_iterator
  1845. subview<eT>::cend() const
  1846. {
  1847. return const_iterator(*this, aux_row1, aux_col1 + n_cols);
  1848. }
  1849. //
  1850. //
  1851. //
  1852. template<typename eT>
  1853. inline
  1854. subview<eT>::iterator::iterator()
  1855. : M (NULL)
  1856. , current_ptr(NULL)
  1857. , current_row(0 )
  1858. , current_col(0 )
  1859. , aux_row1 (0 )
  1860. , aux_row2_p1(0 )
  1861. {
  1862. arma_extra_debug_sigprint();
  1863. // Technically this iterator is invalid (it does not point to a valid element)
  1864. }
  1865. template<typename eT>
  1866. inline
  1867. subview<eT>::iterator::iterator(const iterator& X)
  1868. : M (X.M )
  1869. , current_ptr(X.current_ptr)
  1870. , current_row(X.current_row)
  1871. , current_col(X.current_col)
  1872. , aux_row1 (X.aux_row1 )
  1873. , aux_row2_p1(X.aux_row2_p1)
  1874. {
  1875. arma_extra_debug_sigprint();
  1876. }
  1877. template<typename eT>
  1878. inline
  1879. subview<eT>::iterator::iterator(subview<eT>& in_sv, const uword in_row, const uword in_col)
  1880. : M (&(const_cast< Mat<eT>& >(in_sv.m)))
  1881. , current_ptr(&(M->at(in_row,in_col)) )
  1882. , current_row(in_row )
  1883. , current_col(in_col )
  1884. , aux_row1 (in_sv.aux_row1 )
  1885. , aux_row2_p1(in_sv.aux_row1 + in_sv.n_rows )
  1886. {
  1887. arma_extra_debug_sigprint();
  1888. }
  1889. template<typename eT>
  1890. inline
  1891. arma_warn_unused
  1892. eT&
  1893. subview<eT>::iterator::operator*()
  1894. {
  1895. return (*current_ptr);
  1896. }
  1897. template<typename eT>
  1898. inline
  1899. typename subview<eT>::iterator&
  1900. subview<eT>::iterator::operator++()
  1901. {
  1902. current_row++;
  1903. if(current_row == aux_row2_p1)
  1904. {
  1905. current_row = aux_row1;
  1906. current_col++;
  1907. current_ptr = &( (*M).at(current_row,current_col) );
  1908. }
  1909. else
  1910. {
  1911. current_ptr++;
  1912. }
  1913. return *this;
  1914. }
  1915. template<typename eT>
  1916. inline
  1917. arma_warn_unused
  1918. typename subview<eT>::iterator
  1919. subview<eT>::iterator::operator++(int)
  1920. {
  1921. typename subview<eT>::iterator temp(*this);
  1922. ++(*this);
  1923. return temp;
  1924. }
  1925. template<typename eT>
  1926. inline
  1927. arma_warn_unused
  1928. bool
  1929. subview<eT>::iterator::operator==(const iterator& rhs) const
  1930. {
  1931. return (current_ptr == rhs.current_ptr);
  1932. }
  1933. template<typename eT>
  1934. inline
  1935. arma_warn_unused
  1936. bool
  1937. subview<eT>::iterator::operator!=(const iterator& rhs) const
  1938. {
  1939. return (current_ptr != rhs.current_ptr);
  1940. }
  1941. template<typename eT>
  1942. inline
  1943. arma_warn_unused
  1944. bool
  1945. subview<eT>::iterator::operator==(const const_iterator& rhs) const
  1946. {
  1947. return (current_ptr == rhs.current_ptr);
  1948. }
  1949. template<typename eT>
  1950. inline
  1951. arma_warn_unused
  1952. bool
  1953. subview<eT>::iterator::operator!=(const const_iterator& rhs) const
  1954. {
  1955. return (current_ptr != rhs.current_ptr);
  1956. }
  1957. //
  1958. //
  1959. //
  1960. template<typename eT>
  1961. inline
  1962. subview<eT>::const_iterator::const_iterator()
  1963. : M (NULL)
  1964. , current_ptr(NULL)
  1965. , current_row(0 )
  1966. , current_col(0 )
  1967. , aux_row1 (0 )
  1968. , aux_row2_p1(0 )
  1969. {
  1970. arma_extra_debug_sigprint();
  1971. // Technically this iterator is invalid (it does not point to a valid element)
  1972. }
  1973. template<typename eT>
  1974. inline
  1975. subview<eT>::const_iterator::const_iterator(const iterator& X)
  1976. : M (X.M )
  1977. , current_ptr(X.current_ptr)
  1978. , current_row(X.current_row)
  1979. , current_col(X.current_col)
  1980. , aux_row1 (X.aux_row1 )
  1981. , aux_row2_p1(X.aux_row2_p1)
  1982. {
  1983. arma_extra_debug_sigprint();
  1984. }
  1985. template<typename eT>
  1986. inline
  1987. subview<eT>::const_iterator::const_iterator(const const_iterator& X)
  1988. : M (X.M )
  1989. , current_ptr(X.current_ptr)
  1990. , current_row(X.current_row)
  1991. , current_col(X.current_col)
  1992. , aux_row1 (X.aux_row1 )
  1993. , aux_row2_p1(X.aux_row2_p1)
  1994. {
  1995. arma_extra_debug_sigprint();
  1996. }
  1997. template<typename eT>
  1998. inline
  1999. subview<eT>::const_iterator::const_iterator(const subview<eT>& in_sv, const uword in_row, const uword in_col)
  2000. : M (&(in_sv.m) )
  2001. , current_ptr(&(M->at(in_row,in_col)) )
  2002. , current_row(in_row )
  2003. , current_col(in_col )
  2004. , aux_row1 (in_sv.aux_row1 )
  2005. , aux_row2_p1(in_sv.aux_row1 + in_sv.n_rows)
  2006. {
  2007. arma_extra_debug_sigprint();
  2008. }
  2009. template<typename eT>
  2010. inline
  2011. arma_warn_unused
  2012. const eT&
  2013. subview<eT>::const_iterator::operator*()
  2014. {
  2015. return (*current_ptr);
  2016. }
  2017. template<typename eT>
  2018. inline
  2019. typename subview<eT>::const_iterator&
  2020. subview<eT>::const_iterator::operator++()
  2021. {
  2022. current_row++;
  2023. if(current_row == aux_row2_p1)
  2024. {
  2025. current_row = aux_row1;
  2026. current_col++;
  2027. current_ptr = &( (*M).at(current_row,current_col) );
  2028. }
  2029. else
  2030. {
  2031. current_ptr++;
  2032. }
  2033. return *this;
  2034. }
  2035. template<typename eT>
  2036. inline
  2037. arma_warn_unused
  2038. typename subview<eT>::const_iterator
  2039. subview<eT>::const_iterator::operator++(int)
  2040. {
  2041. typename subview<eT>::const_iterator temp(*this);
  2042. ++(*this);
  2043. return temp;
  2044. }
  2045. template<typename eT>
  2046. inline
  2047. arma_warn_unused
  2048. bool
  2049. subview<eT>::const_iterator::operator==(const iterator& rhs) const
  2050. {
  2051. return (current_ptr == rhs.current_ptr);
  2052. }
  2053. template<typename eT>
  2054. inline
  2055. arma_warn_unused
  2056. bool
  2057. subview<eT>::const_iterator::operator!=(const iterator& rhs) const
  2058. {
  2059. return (current_ptr != rhs.current_ptr);
  2060. }
  2061. template<typename eT>
  2062. inline
  2063. arma_warn_unused
  2064. bool
  2065. subview<eT>::const_iterator::operator==(const const_iterator& rhs) const
  2066. {
  2067. return (current_ptr == rhs.current_ptr);
  2068. }
  2069. template<typename eT>
  2070. inline
  2071. arma_warn_unused
  2072. bool
  2073. subview<eT>::const_iterator::operator!=(const const_iterator& rhs) const
  2074. {
  2075. return (current_ptr != rhs.current_ptr);
  2076. }
  2077. //
  2078. //
  2079. //
  2080. template<typename eT>
  2081. inline
  2082. subview<eT>::row_iterator::row_iterator()
  2083. : M (NULL)
  2084. , current_row(0 )
  2085. , current_col(0 )
  2086. , aux_col1 (0 )
  2087. , aux_col2_p1(0 )
  2088. {
  2089. arma_extra_debug_sigprint();
  2090. // Technically this iterator is invalid (it does not point to a valid element)
  2091. }
  2092. template<typename eT>
  2093. inline
  2094. subview<eT>::row_iterator::row_iterator(const row_iterator& X)
  2095. : M (X.M )
  2096. , current_row(X.current_row)
  2097. , current_col(X.current_col)
  2098. , aux_col1 (X.aux_col1 )
  2099. , aux_col2_p1(X.aux_col2_p1)
  2100. {
  2101. arma_extra_debug_sigprint();
  2102. }
  2103. template<typename eT>
  2104. inline
  2105. subview<eT>::row_iterator::row_iterator(subview<eT>& in_sv, const uword in_row, const uword in_col)
  2106. : M (&(const_cast< Mat<eT>& >(in_sv.m)))
  2107. , current_row(in_row )
  2108. , current_col(in_col )
  2109. , aux_col1 (in_sv.aux_col1 )
  2110. , aux_col2_p1(in_sv.aux_col1 + in_sv.n_cols )
  2111. {
  2112. arma_extra_debug_sigprint();
  2113. }
  2114. template<typename eT>
  2115. inline
  2116. arma_warn_unused
  2117. eT&
  2118. subview<eT>::row_iterator::operator*()
  2119. {
  2120. return M->at(current_row,current_col);
  2121. }
  2122. template<typename eT>
  2123. inline
  2124. typename subview<eT>::row_iterator&
  2125. subview<eT>::row_iterator::operator++()
  2126. {
  2127. current_col++;
  2128. if(current_col == aux_col2_p1)
  2129. {
  2130. current_col = aux_col1;
  2131. current_row++;
  2132. }
  2133. return *this;
  2134. }
  2135. template<typename eT>
  2136. inline
  2137. arma_warn_unused
  2138. typename subview<eT>::row_iterator
  2139. subview<eT>::row_iterator::operator++(int)
  2140. {
  2141. typename subview<eT>::row_iterator temp(*this);
  2142. ++(*this);
  2143. return temp;
  2144. }
  2145. template<typename eT>
  2146. inline
  2147. arma_warn_unused
  2148. bool
  2149. subview<eT>::row_iterator::operator==(const row_iterator& rhs) const
  2150. {
  2151. return ( (current_row == rhs.current_row) && (current_col == rhs.current_col) );
  2152. }
  2153. template<typename eT>
  2154. inline
  2155. arma_warn_unused
  2156. bool
  2157. subview<eT>::row_iterator::operator!=(const row_iterator& rhs) const
  2158. {
  2159. return ( (current_row != rhs.current_row) || (current_col != rhs.current_col) );
  2160. }
  2161. template<typename eT>
  2162. inline
  2163. arma_warn_unused
  2164. bool
  2165. subview<eT>::row_iterator::operator==(const const_row_iterator& rhs) const
  2166. {
  2167. return ( (current_row == rhs.current_row) && (current_col == rhs.current_col) );
  2168. }
  2169. template<typename eT>
  2170. inline
  2171. arma_warn_unused
  2172. bool
  2173. subview<eT>::row_iterator::operator!=(const const_row_iterator& rhs) const
  2174. {
  2175. return ( (current_row != rhs.current_row) || (current_col != rhs.current_col) );
  2176. }
  2177. //
  2178. //
  2179. //
  2180. template<typename eT>
  2181. inline
  2182. subview<eT>::const_row_iterator::const_row_iterator()
  2183. : M (NULL)
  2184. , current_row(0 )
  2185. , current_col(0 )
  2186. , aux_col1 (0 )
  2187. , aux_col2_p1(0 )
  2188. {
  2189. arma_extra_debug_sigprint();
  2190. // Technically this iterator is invalid (it does not point to a valid element)
  2191. }
  2192. template<typename eT>
  2193. inline
  2194. subview<eT>::const_row_iterator::const_row_iterator(const row_iterator& X)
  2195. : M (X.M )
  2196. , current_row(X.current_row)
  2197. , current_col(X.current_col)
  2198. , aux_col1 (X.aux_col1 )
  2199. , aux_col2_p1(X.aux_col2_p1)
  2200. {
  2201. arma_extra_debug_sigprint();
  2202. }
  2203. template<typename eT>
  2204. inline
  2205. subview<eT>::const_row_iterator::const_row_iterator(const const_row_iterator& X)
  2206. : M (X.M )
  2207. , current_row(X.current_row)
  2208. , current_col(X.current_col)
  2209. , aux_col1 (X.aux_col1 )
  2210. , aux_col2_p1(X.aux_col2_p1)
  2211. {
  2212. arma_extra_debug_sigprint();
  2213. }
  2214. template<typename eT>
  2215. inline
  2216. subview<eT>::const_row_iterator::const_row_iterator(const subview<eT>& in_sv, const uword in_row, const uword in_col)
  2217. : M (&(in_sv.m) )
  2218. , current_row(in_row )
  2219. , current_col(in_col )
  2220. , aux_col1 (in_sv.aux_col1 )
  2221. , aux_col2_p1(in_sv.aux_col1 + in_sv.n_cols)
  2222. {
  2223. arma_extra_debug_sigprint();
  2224. }
  2225. template<typename eT>
  2226. inline
  2227. arma_warn_unused
  2228. const eT&
  2229. subview<eT>::const_row_iterator::operator*() const
  2230. {
  2231. return M->at(current_row,current_col);
  2232. }
  2233. template<typename eT>
  2234. inline
  2235. typename subview<eT>::const_row_iterator&
  2236. subview<eT>::const_row_iterator::operator++()
  2237. {
  2238. current_col++;
  2239. if(current_col == aux_col2_p1)
  2240. {
  2241. current_col = aux_col1;
  2242. current_row++;
  2243. }
  2244. return *this;
  2245. }
  2246. template<typename eT>
  2247. inline
  2248. arma_warn_unused
  2249. typename subview<eT>::const_row_iterator
  2250. subview<eT>::const_row_iterator::operator++(int)
  2251. {
  2252. typename subview<eT>::const_row_iterator temp(*this);
  2253. ++(*this);
  2254. return temp;
  2255. }
  2256. template<typename eT>
  2257. inline
  2258. arma_warn_unused
  2259. bool
  2260. subview<eT>::const_row_iterator::operator==(const row_iterator& rhs) const
  2261. {
  2262. return ( (current_row == rhs.current_row) && (current_col == rhs.current_col) );
  2263. }
  2264. template<typename eT>
  2265. inline
  2266. arma_warn_unused
  2267. bool
  2268. subview<eT>::const_row_iterator::operator!=(const row_iterator& rhs) const
  2269. {
  2270. return ( (current_row != rhs.current_row) || (current_col != rhs.current_col) );
  2271. }
  2272. template<typename eT>
  2273. inline
  2274. arma_warn_unused
  2275. bool
  2276. subview<eT>::const_row_iterator::operator==(const const_row_iterator& rhs) const
  2277. {
  2278. return ( (current_row == rhs.current_row) && (current_col == rhs.current_col) );
  2279. }
  2280. template<typename eT>
  2281. inline
  2282. arma_warn_unused
  2283. bool
  2284. subview<eT>::const_row_iterator::operator!=(const const_row_iterator& rhs) const
  2285. {
  2286. return ( (current_row != rhs.current_row) || (current_col != rhs.current_col) );
  2287. }
  2288. //
  2289. //
  2290. //
  2291. template<typename eT>
  2292. inline
  2293. subview_col<eT>::subview_col(const Mat<eT>& in_m, const uword in_col)
  2294. : subview<eT>(in_m, 0, in_col, in_m.n_rows, 1)
  2295. , colmem(subview<eT>::colptr(0))
  2296. {
  2297. arma_extra_debug_sigprint();
  2298. }
  2299. template<typename eT>
  2300. inline
  2301. subview_col<eT>::subview_col(const Mat<eT>& in_m, const uword in_col, const uword in_row1, const uword in_n_rows)
  2302. : subview<eT>(in_m, in_row1, in_col, in_n_rows, 1)
  2303. , colmem(subview<eT>::colptr(0))
  2304. {
  2305. arma_extra_debug_sigprint();
  2306. }
  2307. template<typename eT>
  2308. inline
  2309. void
  2310. subview_col<eT>::operator=(const subview<eT>& X)
  2311. {
  2312. arma_extra_debug_sigprint();
  2313. subview<eT>::operator=(X);
  2314. }
  2315. template<typename eT>
  2316. inline
  2317. void
  2318. subview_col<eT>::operator=(const subview_col<eT>& X)
  2319. {
  2320. arma_extra_debug_sigprint();
  2321. subview<eT>::operator=(X); // interprets 'subview_col' as 'subview'
  2322. }
  2323. template<typename eT>
  2324. inline
  2325. void
  2326. subview_col<eT>::operator=(const eT val)
  2327. {
  2328. arma_extra_debug_sigprint();
  2329. if(subview<eT>::n_elem != 1)
  2330. {
  2331. arma_debug_assert_same_size(subview<eT>::n_rows, subview<eT>::n_cols, 1, 1, "copy into submatrix");
  2332. }
  2333. access::rw( colmem[0] ) = val;
  2334. }
  2335. template<typename eT>
  2336. template<typename T1>
  2337. inline
  2338. void
  2339. subview_col<eT>::operator=(const Base<eT,T1>& X)
  2340. {
  2341. arma_extra_debug_sigprint();
  2342. subview<eT>::operator=(X);
  2343. }
  2344. template<typename eT>
  2345. template<typename T1, typename gen_type>
  2346. inline
  2347. typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result
  2348. subview_col<eT>::operator= (const Gen<T1,gen_type>& in)
  2349. {
  2350. arma_extra_debug_sigprint();
  2351. arma_debug_assert_same_size(subview<eT>::n_rows, uword(1), in.n_rows, (in.is_col ? uword(1) : in.n_cols), "copy into submatrix");
  2352. in.apply(*this);
  2353. }
  2354. template<typename eT>
  2355. arma_inline
  2356. const Op<subview_col<eT>,op_htrans>
  2357. subview_col<eT>::t() const
  2358. {
  2359. return Op<subview_col<eT>,op_htrans>(*this);
  2360. }
  2361. template<typename eT>
  2362. arma_inline
  2363. const Op<subview_col<eT>,op_htrans>
  2364. subview_col<eT>::ht() const
  2365. {
  2366. return Op<subview_col<eT>,op_htrans>(*this);
  2367. }
  2368. template<typename eT>
  2369. arma_inline
  2370. const Op<subview_col<eT>,op_strans>
  2371. subview_col<eT>::st() const
  2372. {
  2373. return Op<subview_col<eT>,op_strans>(*this);
  2374. }
  2375. template<typename eT>
  2376. arma_inline
  2377. const Op<subview_col<eT>,op_strans>
  2378. subview_col<eT>::as_row() const
  2379. {
  2380. return Op<subview_col<eT>,op_strans>(*this);
  2381. }
  2382. template<typename eT>
  2383. inline
  2384. void
  2385. subview_col<eT>::fill(const eT val)
  2386. {
  2387. arma_extra_debug_sigprint();
  2388. arrayops::inplace_set( access::rwp(colmem), val, subview<eT>::n_rows );
  2389. }
  2390. template<typename eT>
  2391. inline
  2392. void
  2393. subview_col<eT>::zeros()
  2394. {
  2395. arma_extra_debug_sigprint();
  2396. arrayops::fill_zeros( access::rwp(colmem), subview<eT>::n_rows );
  2397. }
  2398. template<typename eT>
  2399. inline
  2400. void
  2401. subview_col<eT>::ones()
  2402. {
  2403. arma_extra_debug_sigprint();
  2404. arrayops::inplace_set( access::rwp(colmem), eT(1), subview<eT>::n_rows );
  2405. }
  2406. template<typename eT>
  2407. arma_inline
  2408. eT
  2409. subview_col<eT>::at_alt(const uword ii) const
  2410. {
  2411. const eT* colmem_aligned = colmem;
  2412. memory::mark_as_aligned(colmem_aligned);
  2413. return colmem_aligned[ii];
  2414. }
  2415. template<typename eT>
  2416. arma_inline
  2417. eT&
  2418. subview_col<eT>::operator[](const uword ii)
  2419. {
  2420. return access::rw( colmem[ii] );
  2421. }
  2422. template<typename eT>
  2423. arma_inline
  2424. eT
  2425. subview_col<eT>::operator[](const uword ii) const
  2426. {
  2427. return colmem[ii];
  2428. }
  2429. template<typename eT>
  2430. inline
  2431. eT&
  2432. subview_col<eT>::operator()(const uword ii)
  2433. {
  2434. arma_debug_check( (ii >= subview<eT>::n_elem), "subview::operator(): index out of bounds");
  2435. return access::rw( colmem[ii] );
  2436. }
  2437. template<typename eT>
  2438. inline
  2439. eT
  2440. subview_col<eT>::operator()(const uword ii) const
  2441. {
  2442. arma_debug_check( (ii >= subview<eT>::n_elem), "subview::operator(): index out of bounds");
  2443. return colmem[ii];
  2444. }
  2445. template<typename eT>
  2446. inline
  2447. eT&
  2448. subview_col<eT>::operator()(const uword in_row, const uword in_col)
  2449. {
  2450. arma_debug_check( ((in_row >= subview<eT>::n_rows) || (in_col > 0)), "subview::operator(): index out of bounds");
  2451. return access::rw( colmem[in_row] );
  2452. }
  2453. template<typename eT>
  2454. inline
  2455. eT
  2456. subview_col<eT>::operator()(const uword in_row, const uword in_col) const
  2457. {
  2458. arma_debug_check( ((in_row >= subview<eT>::n_rows) || (in_col > 0)), "subview::operator(): index out of bounds");
  2459. return colmem[in_row];
  2460. }
  2461. template<typename eT>
  2462. inline
  2463. eT&
  2464. subview_col<eT>::at(const uword in_row, const uword)
  2465. {
  2466. return access::rw( colmem[in_row] );
  2467. }
  2468. template<typename eT>
  2469. inline
  2470. eT
  2471. subview_col<eT>::at(const uword in_row, const uword) const
  2472. {
  2473. return colmem[in_row];
  2474. }
  2475. template<typename eT>
  2476. arma_inline
  2477. eT*
  2478. subview_col<eT>::colptr(const uword)
  2479. {
  2480. return const_cast<eT*>(colmem);
  2481. }
  2482. template<typename eT>
  2483. arma_inline
  2484. const eT*
  2485. subview_col<eT>::colptr(const uword) const
  2486. {
  2487. return colmem;
  2488. }
  2489. template<typename eT>
  2490. inline
  2491. subview_col<eT>
  2492. subview_col<eT>::rows(const uword in_row1, const uword in_row2)
  2493. {
  2494. arma_extra_debug_sigprint();
  2495. arma_debug_check( ( (in_row1 > in_row2) || (in_row2 >= subview<eT>::n_rows) ), "subview_col::rows(): indices out of bounds or incorrectly used");
  2496. const uword subview_n_rows = in_row2 - in_row1 + 1;
  2497. const uword base_row1 = this->aux_row1 + in_row1;
  2498. return subview_col<eT>(this->m, this->aux_col1, base_row1, subview_n_rows);
  2499. }
  2500. template<typename eT>
  2501. inline
  2502. const subview_col<eT>
  2503. subview_col<eT>::rows(const uword in_row1, const uword in_row2) const
  2504. {
  2505. arma_extra_debug_sigprint();
  2506. arma_debug_check( ( (in_row1 > in_row2) || (in_row2 >= subview<eT>::n_rows) ), "subview_col::rows(): indices out of bounds or incorrectly used");
  2507. const uword subview_n_rows = in_row2 - in_row1 + 1;
  2508. const uword base_row1 = this->aux_row1 + in_row1;
  2509. return subview_col<eT>(this->m, this->aux_col1, base_row1, subview_n_rows);
  2510. }
  2511. template<typename eT>
  2512. inline
  2513. subview_col<eT>
  2514. subview_col<eT>::subvec(const uword in_row1, const uword in_row2)
  2515. {
  2516. arma_extra_debug_sigprint();
  2517. arma_debug_check( ( (in_row1 > in_row2) || (in_row2 >= subview<eT>::n_rows) ), "subview_col::subvec(): indices out of bounds or incorrectly used");
  2518. const uword subview_n_rows = in_row2 - in_row1 + 1;
  2519. const uword base_row1 = this->aux_row1 + in_row1;
  2520. return subview_col<eT>(this->m, this->aux_col1, base_row1, subview_n_rows);
  2521. }
  2522. template<typename eT>
  2523. inline
  2524. const subview_col<eT>
  2525. subview_col<eT>::subvec(const uword in_row1, const uword in_row2) const
  2526. {
  2527. arma_extra_debug_sigprint();
  2528. arma_debug_check( ( (in_row1 > in_row2) || (in_row2 >= subview<eT>::n_rows) ), "subview_col::subvec(): indices out of bounds or incorrectly used");
  2529. const uword subview_n_rows = in_row2 - in_row1 + 1;
  2530. const uword base_row1 = this->aux_row1 + in_row1;
  2531. return subview_col<eT>(this->m, this->aux_col1, base_row1, subview_n_rows);
  2532. }
  2533. template<typename eT>
  2534. inline
  2535. subview_col<eT>
  2536. subview_col<eT>::subvec(const uword start_row, const SizeMat& s)
  2537. {
  2538. arma_extra_debug_sigprint();
  2539. arma_debug_check( (s.n_cols != 1), "subview_col::subvec(): given size does not specify a column vector" );
  2540. arma_debug_check( ( (start_row >= subview<eT>::n_rows) || ((start_row + s.n_rows) > subview<eT>::n_rows) ), "subview_col::subvec(): size out of bounds" );
  2541. const uword base_row1 = this->aux_row1 + start_row;
  2542. return subview_col<eT>(this->m, this->aux_col1, base_row1, s.n_rows);
  2543. }
  2544. template<typename eT>
  2545. inline
  2546. const subview_col<eT>
  2547. subview_col<eT>::subvec(const uword start_row, const SizeMat& s) const
  2548. {
  2549. arma_extra_debug_sigprint();
  2550. arma_debug_check( (s.n_cols != 1), "subview_col::subvec(): given size does not specify a column vector" );
  2551. arma_debug_check( ( (start_row >= subview<eT>::n_rows) || ((start_row + s.n_rows) > subview<eT>::n_rows) ), "subview_col::subvec(): size out of bounds" );
  2552. const uword base_row1 = this->aux_row1 + start_row;
  2553. return subview_col<eT>(this->m, this->aux_col1, base_row1, s.n_rows);
  2554. }
  2555. template<typename eT>
  2556. inline
  2557. subview_col<eT>
  2558. subview_col<eT>::head(const uword N)
  2559. {
  2560. arma_extra_debug_sigprint();
  2561. arma_debug_check( (N > subview<eT>::n_rows), "subview_col::head(): size out of bounds");
  2562. return subview_col<eT>(this->m, this->aux_col1, this->aux_row1, N);
  2563. }
  2564. template<typename eT>
  2565. inline
  2566. const subview_col<eT>
  2567. subview_col<eT>::head(const uword N) const
  2568. {
  2569. arma_extra_debug_sigprint();
  2570. arma_debug_check( (N > subview<eT>::n_rows), "subview_col::head(): size out of bounds");
  2571. return subview_col<eT>(this->m, this->aux_col1, this->aux_row1, N);
  2572. }
  2573. template<typename eT>
  2574. inline
  2575. subview_col<eT>
  2576. subview_col<eT>::tail(const uword N)
  2577. {
  2578. arma_extra_debug_sigprint();
  2579. arma_debug_check( (N > subview<eT>::n_rows), "subview_col::tail(): size out of bounds");
  2580. const uword start_row = subview<eT>::aux_row1 + subview<eT>::n_rows - N;
  2581. return subview_col<eT>(this->m, this->aux_col1, start_row, N);
  2582. }
  2583. template<typename eT>
  2584. inline
  2585. const subview_col<eT>
  2586. subview_col<eT>::tail(const uword N) const
  2587. {
  2588. arma_extra_debug_sigprint();
  2589. arma_debug_check( (N > subview<eT>::n_rows), "subview_col::tail(): size out of bounds");
  2590. const uword start_row = subview<eT>::aux_row1 + subview<eT>::n_rows - N;
  2591. return subview_col<eT>(this->m, this->aux_col1, start_row, N);
  2592. }
  2593. template<typename eT>
  2594. inline
  2595. arma_warn_unused
  2596. eT
  2597. subview_col<eT>::min() const
  2598. {
  2599. arma_extra_debug_sigprint();
  2600. if(subview<eT>::n_elem == 0)
  2601. {
  2602. arma_debug_check(true, "min(): object has no elements");
  2603. return Datum<eT>::nan;
  2604. }
  2605. return op_min::direct_min(colmem, subview<eT>::n_elem);
  2606. }
  2607. template<typename eT>
  2608. inline
  2609. arma_warn_unused
  2610. eT
  2611. subview_col<eT>::max() const
  2612. {
  2613. arma_extra_debug_sigprint();
  2614. if(subview<eT>::n_elem == 0)
  2615. {
  2616. arma_debug_check(true, "max(): object has no elements");
  2617. return Datum<eT>::nan;
  2618. }
  2619. return op_max::direct_max(colmem, subview<eT>::n_elem);
  2620. }
  2621. template<typename eT>
  2622. inline
  2623. eT
  2624. subview_col<eT>::min(uword& index_of_min_val) const
  2625. {
  2626. arma_extra_debug_sigprint();
  2627. if(subview<eT>::n_elem == 0)
  2628. {
  2629. arma_debug_check(true, "min(): object has no elements");
  2630. index_of_min_val = uword(0);
  2631. return Datum<eT>::nan;
  2632. }
  2633. else
  2634. {
  2635. return op_min::direct_min(colmem, subview<eT>::n_elem, index_of_min_val);
  2636. }
  2637. }
  2638. template<typename eT>
  2639. inline
  2640. eT
  2641. subview_col<eT>::max(uword& index_of_max_val) const
  2642. {
  2643. arma_extra_debug_sigprint();
  2644. if(subview<eT>::n_elem == 0)
  2645. {
  2646. arma_debug_check(true, "max(): object has no elements");
  2647. index_of_max_val = uword(0);
  2648. return Datum<eT>::nan;
  2649. }
  2650. else
  2651. {
  2652. return op_max::direct_max(colmem, subview<eT>::n_elem, index_of_max_val);
  2653. }
  2654. }
  2655. template<typename eT>
  2656. inline
  2657. arma_warn_unused
  2658. uword
  2659. subview_col<eT>::index_min() const
  2660. {
  2661. arma_extra_debug_sigprint();
  2662. uword index = 0;
  2663. if(subview<eT>::n_elem == 0)
  2664. {
  2665. arma_debug_check(true, "index_min(): object has no elements");
  2666. }
  2667. else
  2668. {
  2669. op_min::direct_min(colmem, subview<eT>::n_elem, index);
  2670. }
  2671. return index;
  2672. }
  2673. template<typename eT>
  2674. inline
  2675. arma_warn_unused
  2676. uword
  2677. subview_col<eT>::index_max() const
  2678. {
  2679. arma_extra_debug_sigprint();
  2680. uword index = 0;
  2681. if(subview<eT>::n_elem == 0)
  2682. {
  2683. arma_debug_check(true, "index_max(): object has no elements");
  2684. }
  2685. else
  2686. {
  2687. op_max::direct_max(colmem, subview<eT>::n_elem, index);
  2688. }
  2689. return index;
  2690. }
  2691. //
  2692. //
  2693. //
  2694. template<typename eT>
  2695. inline
  2696. subview_row<eT>::subview_row(const Mat<eT>& in_m, const uword in_row)
  2697. : subview<eT>(in_m, in_row, 0, 1, in_m.n_cols)
  2698. {
  2699. arma_extra_debug_sigprint();
  2700. }
  2701. template<typename eT>
  2702. inline
  2703. subview_row<eT>::subview_row(const Mat<eT>& in_m, const uword in_row, const uword in_col1, const uword in_n_cols)
  2704. : subview<eT>(in_m, in_row, in_col1, 1, in_n_cols)
  2705. {
  2706. arma_extra_debug_sigprint();
  2707. }
  2708. template<typename eT>
  2709. inline
  2710. void
  2711. subview_row<eT>::operator=(const subview<eT>& X)
  2712. {
  2713. arma_extra_debug_sigprint();
  2714. subview<eT>::operator=(X);
  2715. }
  2716. template<typename eT>
  2717. inline
  2718. void
  2719. subview_row<eT>::operator=(const subview_row<eT>& X)
  2720. {
  2721. arma_extra_debug_sigprint();
  2722. subview<eT>::operator=(X); // interprets 'subview_row' as 'subview'
  2723. }
  2724. template<typename eT>
  2725. inline
  2726. void
  2727. subview_row<eT>::operator=(const eT val)
  2728. {
  2729. arma_extra_debug_sigprint();
  2730. subview<eT>::operator=(val); // interprets 'subview_row' as 'subview'
  2731. }
  2732. template<typename eT>
  2733. template<typename T1>
  2734. inline
  2735. void
  2736. subview_row<eT>::operator=(const Base<eT,T1>& X)
  2737. {
  2738. arma_extra_debug_sigprint();
  2739. subview<eT>::operator=(X);
  2740. }
  2741. template<typename eT>
  2742. template<typename T1, typename gen_type>
  2743. inline
  2744. typename enable_if2< is_same_type<typename T1::elem_type, eT>::value, void>::result
  2745. subview_row<eT>::operator= (const Gen<T1,gen_type>& in)
  2746. {
  2747. arma_extra_debug_sigprint();
  2748. arma_debug_assert_same_size(uword(1), subview<eT>::n_cols, (in.is_row ? uword(1) : in.n_rows), in.n_cols, "copy into submatrix");
  2749. in.apply(*this);
  2750. }
  2751. template<typename eT>
  2752. arma_inline
  2753. const Op<subview_row<eT>,op_htrans>
  2754. subview_row<eT>::t() const
  2755. {
  2756. return Op<subview_row<eT>,op_htrans>(*this);
  2757. }
  2758. template<typename eT>
  2759. arma_inline
  2760. const Op<subview_row<eT>,op_htrans>
  2761. subview_row<eT>::ht() const
  2762. {
  2763. return Op<subview_row<eT>,op_htrans>(*this);
  2764. }
  2765. template<typename eT>
  2766. arma_inline
  2767. const Op<subview_row<eT>,op_strans>
  2768. subview_row<eT>::st() const
  2769. {
  2770. return Op<subview_row<eT>,op_strans>(*this);
  2771. }
  2772. template<typename eT>
  2773. arma_inline
  2774. const Op<subview_row<eT>,op_strans>
  2775. subview_row<eT>::as_col() const
  2776. {
  2777. return Op<subview_row<eT>,op_strans>(*this);
  2778. }
  2779. template<typename eT>
  2780. inline
  2781. eT
  2782. subview_row<eT>::at_alt(const uword ii) const
  2783. {
  2784. const uword index = (ii + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2785. return subview<eT>::m.mem[index];
  2786. }
  2787. template<typename eT>
  2788. inline
  2789. eT&
  2790. subview_row<eT>::operator[](const uword ii)
  2791. {
  2792. const uword index = (ii + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2793. return access::rw( (const_cast< Mat<eT>& >(subview<eT>::m)).mem[index] );
  2794. }
  2795. template<typename eT>
  2796. inline
  2797. eT
  2798. subview_row<eT>::operator[](const uword ii) const
  2799. {
  2800. const uword index = (ii + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2801. return subview<eT>::m.mem[index];
  2802. }
  2803. template<typename eT>
  2804. inline
  2805. eT&
  2806. subview_row<eT>::operator()(const uword ii)
  2807. {
  2808. arma_debug_check( (ii >= subview<eT>::n_elem), "subview::operator(): index out of bounds");
  2809. const uword index = (ii + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2810. return access::rw( (const_cast< Mat<eT>& >(subview<eT>::m)).mem[index] );
  2811. }
  2812. template<typename eT>
  2813. inline
  2814. eT
  2815. subview_row<eT>::operator()(const uword ii) const
  2816. {
  2817. arma_debug_check( (ii >= subview<eT>::n_elem), "subview::operator(): index out of bounds");
  2818. const uword index = (ii + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2819. return subview<eT>::m.mem[index];
  2820. }
  2821. template<typename eT>
  2822. inline
  2823. eT&
  2824. subview_row<eT>::operator()(const uword in_row, const uword in_col)
  2825. {
  2826. arma_debug_check( ((in_row > 0) || (in_col >= subview<eT>::n_cols)), "subview::operator(): index out of bounds");
  2827. const uword index = (in_col + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2828. return access::rw( (const_cast< Mat<eT>& >(subview<eT>::m)).mem[index] );
  2829. }
  2830. template<typename eT>
  2831. inline
  2832. eT
  2833. subview_row<eT>::operator()(const uword in_row, const uword in_col) const
  2834. {
  2835. arma_debug_check( ((in_row > 0) || (in_col >= subview<eT>::n_cols)), "subview::operator(): index out of bounds");
  2836. const uword index = (in_col + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2837. return subview<eT>::m.mem[index];
  2838. }
  2839. template<typename eT>
  2840. inline
  2841. eT&
  2842. subview_row<eT>::at(const uword, const uword in_col)
  2843. {
  2844. const uword index = (in_col + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2845. return access::rw( (const_cast< Mat<eT>& >(subview<eT>::m)).mem[index] );
  2846. }
  2847. template<typename eT>
  2848. inline
  2849. eT
  2850. subview_row<eT>::at(const uword, const uword in_col) const
  2851. {
  2852. const uword index = (in_col + (subview<eT>::aux_col1))*(subview<eT>::m).n_rows + (subview<eT>::aux_row1);
  2853. return subview<eT>::m.mem[index];
  2854. }
  2855. template<typename eT>
  2856. inline
  2857. subview_row<eT>
  2858. subview_row<eT>::cols(const uword in_col1, const uword in_col2)
  2859. {
  2860. arma_extra_debug_sigprint();
  2861. arma_debug_check( ( (in_col1 > in_col2) || (in_col2 >= subview<eT>::n_cols) ), "subview_row::cols(): indices out of bounds or incorrectly used" );
  2862. const uword subview_n_cols = in_col2 - in_col1 + 1;
  2863. const uword base_col1 = this->aux_col1 + in_col1;
  2864. return subview_row<eT>(this->m, this->aux_row1, base_col1, subview_n_cols);
  2865. }
  2866. template<typename eT>
  2867. inline
  2868. const subview_row<eT>
  2869. subview_row<eT>::cols(const uword in_col1, const uword in_col2) const
  2870. {
  2871. arma_extra_debug_sigprint();
  2872. arma_debug_check( ( (in_col1 > in_col2) || (in_col2 >= subview<eT>::n_cols) ), "subview_row::cols(): indices out of bounds or incorrectly used");
  2873. const uword subview_n_cols = in_col2 - in_col1 + 1;
  2874. const uword base_col1 = this->aux_col1 + in_col1;
  2875. return subview_row<eT>(this->m, this->aux_row1, base_col1, subview_n_cols);
  2876. }
  2877. template<typename eT>
  2878. inline
  2879. subview_row<eT>
  2880. subview_row<eT>::subvec(const uword in_col1, const uword in_col2)
  2881. {
  2882. arma_extra_debug_sigprint();
  2883. arma_debug_check( ( (in_col1 > in_col2) || (in_col2 >= subview<eT>::n_cols) ), "subview_row::subvec(): indices out of bounds or incorrectly used");
  2884. const uword subview_n_cols = in_col2 - in_col1 + 1;
  2885. const uword base_col1 = this->aux_col1 + in_col1;
  2886. return subview_row<eT>(this->m, this->aux_row1, base_col1, subview_n_cols);
  2887. }
  2888. template<typename eT>
  2889. inline
  2890. const subview_row<eT>
  2891. subview_row<eT>::subvec(const uword in_col1, const uword in_col2) const
  2892. {
  2893. arma_extra_debug_sigprint();
  2894. arma_debug_check( ( (in_col1 > in_col2) || (in_col2 >= subview<eT>::n_cols) ), "subview_row::subvec(): indices out of bounds or incorrectly used");
  2895. const uword subview_n_cols = in_col2 - in_col1 + 1;
  2896. const uword base_col1 = this->aux_col1 + in_col1;
  2897. return subview_row<eT>(this->m, this->aux_row1, base_col1, subview_n_cols);
  2898. }
  2899. template<typename eT>
  2900. inline
  2901. subview_row<eT>
  2902. subview_row<eT>::subvec(const uword start_col, const SizeMat& s)
  2903. {
  2904. arma_extra_debug_sigprint();
  2905. arma_debug_check( (s.n_rows != 1), "subview_row::subvec(): given size does not specify a row vector" );
  2906. arma_debug_check( ( (start_col >= subview<eT>::n_cols) || ((start_col + s.n_cols) > subview<eT>::n_cols) ), "subview_row::subvec(): size out of bounds" );
  2907. const uword base_col1 = this->aux_col1 + start_col;
  2908. return subview_row<eT>(this->m, this->aux_row1, base_col1, s.n_cols);
  2909. }
  2910. template<typename eT>
  2911. inline
  2912. const subview_row<eT>
  2913. subview_row<eT>::subvec(const uword start_col, const SizeMat& s) const
  2914. {
  2915. arma_extra_debug_sigprint();
  2916. arma_debug_check( (s.n_rows != 1), "subview_row::subvec(): given size does not specify a row vector" );
  2917. arma_debug_check( ( (start_col >= subview<eT>::n_cols) || ((start_col + s.n_cols) > subview<eT>::n_cols) ), "subview_row::subvec(): size out of bounds" );
  2918. const uword base_col1 = this->aux_col1 + start_col;
  2919. return subview_row<eT>(this->m, this->aux_row1, base_col1, s.n_cols);
  2920. }
  2921. template<typename eT>
  2922. inline
  2923. subview_row<eT>
  2924. subview_row<eT>::head(const uword N)
  2925. {
  2926. arma_extra_debug_sigprint();
  2927. arma_debug_check( (N > subview<eT>::n_cols), "subview_row::head(): size out of bounds");
  2928. return subview_row<eT>(this->m, this->aux_row1, this->aux_col1, N);
  2929. }
  2930. template<typename eT>
  2931. inline
  2932. const subview_row<eT>
  2933. subview_row<eT>::head(const uword N) const
  2934. {
  2935. arma_extra_debug_sigprint();
  2936. arma_debug_check( (N > subview<eT>::n_cols), "subview_row::head(): size out of bounds");
  2937. return subview_row<eT>(this->m, this->aux_row1, this->aux_col1, N);
  2938. }
  2939. template<typename eT>
  2940. inline
  2941. subview_row<eT>
  2942. subview_row<eT>::tail(const uword N)
  2943. {
  2944. arma_extra_debug_sigprint();
  2945. arma_debug_check( (N > subview<eT>::n_cols), "subview_row::tail(): size out of bounds");
  2946. const uword start_col = subview<eT>::aux_col1 + subview<eT>::n_cols - N;
  2947. return subview_row<eT>(this->m, this->aux_row1, start_col, N);
  2948. }
  2949. template<typename eT>
  2950. inline
  2951. const subview_row<eT>
  2952. subview_row<eT>::tail(const uword N) const
  2953. {
  2954. arma_extra_debug_sigprint();
  2955. arma_debug_check( (N > subview<eT>::n_cols), "subview_row::tail(): size out of bounds");
  2956. const uword start_col = subview<eT>::aux_col1 + subview<eT>::n_cols - N;
  2957. return subview_row<eT>(this->m, this->aux_row1, start_col, N);
  2958. }
  2959. template<typename eT>
  2960. inline
  2961. arma_warn_unused
  2962. uword
  2963. subview_row<eT>::index_min() const
  2964. {
  2965. const Proxy< subview_row<eT> > P(*this);
  2966. uword index = 0;
  2967. if(P.get_n_elem() == 0)
  2968. {
  2969. arma_debug_check(true, "index_min(): object has no elements");
  2970. }
  2971. else
  2972. {
  2973. op_min::min_with_index(P, index);
  2974. }
  2975. return index;
  2976. }
  2977. template<typename eT>
  2978. inline
  2979. arma_warn_unused
  2980. uword
  2981. subview_row<eT>::index_max() const
  2982. {
  2983. const Proxy< subview_row<eT> > P(*this);
  2984. uword index = 0;
  2985. if(P.get_n_elem() == 0)
  2986. {
  2987. arma_debug_check(true, "index_max(): object has no elements");
  2988. }
  2989. else
  2990. {
  2991. op_max::max_with_index(P, index);
  2992. }
  2993. return index;
  2994. }
  2995. template<typename eT>
  2996. inline
  2997. typename subview<eT>::row_iterator
  2998. subview_row<eT>::begin()
  2999. {
  3000. return typename subview<eT>::row_iterator(*this, subview<eT>::aux_row1, subview<eT>::aux_col1);
  3001. }
  3002. template<typename eT>
  3003. inline
  3004. typename subview<eT>::const_row_iterator
  3005. subview_row<eT>::begin() const
  3006. {
  3007. return typename subview<eT>::const_row_iterator(*this, subview<eT>::aux_row1, subview<eT>::aux_col1);
  3008. }
  3009. template<typename eT>
  3010. inline
  3011. typename subview<eT>::const_row_iterator
  3012. subview_row<eT>::cbegin() const
  3013. {
  3014. return typename subview<eT>::const_row_iterator(*this, subview<eT>::aux_row1, subview<eT>::aux_col1);
  3015. }
  3016. template<typename eT>
  3017. inline
  3018. typename subview<eT>::row_iterator
  3019. subview_row<eT>::end()
  3020. {
  3021. return typename subview<eT>::row_iterator(*this, subview<eT>::aux_row1 + subview<eT>::n_rows, subview<eT>::aux_col1);
  3022. }
  3023. template<typename eT>
  3024. inline
  3025. typename subview<eT>::const_row_iterator
  3026. subview_row<eT>::end() const
  3027. {
  3028. return typename subview<eT>::const_row_iterator(*this, subview<eT>::aux_row1 + subview<eT>::n_rows, subview<eT>::aux_col1);
  3029. }
  3030. template<typename eT>
  3031. inline
  3032. typename subview<eT>::const_row_iterator
  3033. subview_row<eT>::cend() const
  3034. {
  3035. return typename subview<eT>::const_row_iterator(*this, subview<eT>::aux_row1 + subview<eT>::n_rows, subview<eT>::aux_col1);
  3036. }
  3037. //
  3038. //
  3039. //
  3040. template<typename eT>
  3041. inline
  3042. subview_row_strans<eT>::subview_row_strans(const subview_row<eT>& in_sv_row)
  3043. : sv_row(in_sv_row )
  3044. , n_rows(in_sv_row.n_cols)
  3045. , n_elem(in_sv_row.n_elem)
  3046. {
  3047. arma_extra_debug_sigprint();
  3048. }
  3049. template<typename eT>
  3050. inline
  3051. void
  3052. subview_row_strans<eT>::extract(Mat<eT>& out) const
  3053. {
  3054. arma_extra_debug_sigprint();
  3055. // NOTE: this function assumes that matrix 'out' has already been set to the correct size
  3056. const Mat<eT>& X = sv_row.m;
  3057. eT* out_mem = out.memptr();
  3058. const uword row = sv_row.aux_row1;
  3059. const uword start_col = sv_row.aux_col1;
  3060. const uword sv_row_n_cols = sv_row.n_cols;
  3061. uword ii,jj;
  3062. for(ii=0, jj=1; jj < sv_row_n_cols; ii+=2, jj+=2)
  3063. {
  3064. const eT tmp1 = X.at(row, start_col+ii);
  3065. const eT tmp2 = X.at(row, start_col+jj);
  3066. out_mem[ii] = tmp1;
  3067. out_mem[jj] = tmp2;
  3068. }
  3069. if(ii < sv_row_n_cols)
  3070. {
  3071. out_mem[ii] = X.at(row, start_col+ii);
  3072. }
  3073. }
  3074. template<typename eT>
  3075. inline
  3076. eT
  3077. subview_row_strans<eT>::at_alt(const uword ii) const
  3078. {
  3079. return sv_row[ii];
  3080. }
  3081. template<typename eT>
  3082. inline
  3083. eT
  3084. subview_row_strans<eT>::operator[](const uword ii) const
  3085. {
  3086. return sv_row[ii];
  3087. }
  3088. template<typename eT>
  3089. inline
  3090. eT
  3091. subview_row_strans<eT>::operator()(const uword ii) const
  3092. {
  3093. return sv_row(ii);
  3094. }
  3095. template<typename eT>
  3096. inline
  3097. eT
  3098. subview_row_strans<eT>::operator()(const uword in_row, const uword in_col) const
  3099. {
  3100. return sv_row(in_col, in_row); // deliberately swapped
  3101. }
  3102. template<typename eT>
  3103. inline
  3104. eT
  3105. subview_row_strans<eT>::at(const uword in_row, const uword) const
  3106. {
  3107. return sv_row.at(0, in_row); // deliberately swapped
  3108. }
  3109. //
  3110. //
  3111. //
  3112. template<typename eT>
  3113. inline
  3114. subview_row_htrans<eT>::subview_row_htrans(const subview_row<eT>& in_sv_row)
  3115. : sv_row(in_sv_row )
  3116. , n_rows(in_sv_row.n_cols)
  3117. , n_elem(in_sv_row.n_elem)
  3118. {
  3119. arma_extra_debug_sigprint();
  3120. }
  3121. template<typename eT>
  3122. inline
  3123. void
  3124. subview_row_htrans<eT>::extract(Mat<eT>& out) const
  3125. {
  3126. arma_extra_debug_sigprint();
  3127. // NOTE: this function assumes that matrix 'out' has already been set to the correct size
  3128. const Mat<eT>& X = sv_row.m;
  3129. eT* out_mem = out.memptr();
  3130. const uword row = sv_row.aux_row1;
  3131. const uword start_col = sv_row.aux_col1;
  3132. const uword sv_row_n_cols = sv_row.n_cols;
  3133. for(uword ii=0; ii < sv_row_n_cols; ++ii)
  3134. {
  3135. out_mem[ii] = access::alt_conj( X.at(row, start_col+ii) );
  3136. }
  3137. }
  3138. template<typename eT>
  3139. inline
  3140. eT
  3141. subview_row_htrans<eT>::at_alt(const uword ii) const
  3142. {
  3143. return access::alt_conj( sv_row[ii] );
  3144. }
  3145. template<typename eT>
  3146. inline
  3147. eT
  3148. subview_row_htrans<eT>::operator[](const uword ii) const
  3149. {
  3150. return access::alt_conj( sv_row[ii] );
  3151. }
  3152. template<typename eT>
  3153. inline
  3154. eT
  3155. subview_row_htrans<eT>::operator()(const uword ii) const
  3156. {
  3157. return access::alt_conj( sv_row(ii) );
  3158. }
  3159. template<typename eT>
  3160. inline
  3161. eT
  3162. subview_row_htrans<eT>::operator()(const uword in_row, const uword in_col) const
  3163. {
  3164. return access::alt_conj( sv_row(in_col, in_row) ); // deliberately swapped
  3165. }
  3166. template<typename eT>
  3167. inline
  3168. eT
  3169. subview_row_htrans<eT>::at(const uword in_row, const uword) const
  3170. {
  3171. return access::alt_conj( sv_row.at(0, in_row) ); // deliberately swapped
  3172. }
  3173. //! @}