Cube_meat.hpp 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702
  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 Cube
  16. //! @{
  17. template<typename eT>
  18. inline
  19. Cube<eT>::~Cube()
  20. {
  21. arma_extra_debug_sigprint_this(this);
  22. delete_mat();
  23. if( (mem_state == 0) && (n_elem > Cube_prealloc::mem_n_elem) )
  24. {
  25. arma_extra_debug_print("Cube::destructor: releasing memory");
  26. memory::release( access::rw(mem) );
  27. }
  28. // try to expose buggy user code that accesses deleted objects
  29. if(arma_config::debug)
  30. {
  31. access::rw(mem) = 0;
  32. access::rw(mat_ptrs) = 0;
  33. }
  34. arma_type_check(( is_supported_elem_type<eT>::value == false ));
  35. }
  36. template<typename eT>
  37. inline
  38. Cube<eT>::Cube()
  39. : n_rows(0)
  40. , n_cols(0)
  41. , n_elem_slice(0)
  42. , n_slices(0)
  43. , n_elem(0)
  44. , mem_state(0)
  45. , mem()
  46. , mat_ptrs(0)
  47. {
  48. arma_extra_debug_sigprint_this(this);
  49. }
  50. //! construct the cube to have user specified dimensions
  51. template<typename eT>
  52. inline
  53. Cube<eT>::Cube(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices)
  54. : n_rows(in_n_rows)
  55. , n_cols(in_n_cols)
  56. , n_elem_slice(in_n_rows*in_n_cols)
  57. , n_slices(in_n_slices)
  58. , n_elem(in_n_rows*in_n_cols*in_n_slices)
  59. , mem_state(0)
  60. , mem()
  61. , mat_ptrs(0)
  62. {
  63. arma_extra_debug_sigprint_this(this);
  64. init_cold();
  65. }
  66. template<typename eT>
  67. inline
  68. Cube<eT>::Cube(const SizeCube& s)
  69. : n_rows(s.n_rows)
  70. , n_cols(s.n_cols)
  71. , n_elem_slice(s.n_rows*s.n_cols)
  72. , n_slices(s.n_slices)
  73. , n_elem(s.n_rows*s.n_cols*s.n_slices)
  74. , mem_state(0)
  75. , mem()
  76. , mat_ptrs(0)
  77. {
  78. arma_extra_debug_sigprint_this(this);
  79. init_cold();
  80. }
  81. //! construct the cube to have user specified dimensions and fill with specified pattern
  82. template<typename eT>
  83. template<typename fill_type>
  84. inline
  85. Cube<eT>::Cube(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices, const fill::fill_class<fill_type>&)
  86. : n_rows(in_n_rows)
  87. , n_cols(in_n_cols)
  88. , n_elem_slice(in_n_rows*in_n_cols)
  89. , n_slices(in_n_slices)
  90. , n_elem(in_n_rows*in_n_cols*in_n_slices)
  91. , mem_state(0)
  92. , mem()
  93. , mat_ptrs(0)
  94. {
  95. arma_extra_debug_sigprint_this(this);
  96. init_cold();
  97. if(is_same_type<fill_type, fill::fill_zeros>::yes) (*this).zeros();
  98. if(is_same_type<fill_type, fill::fill_ones >::yes) (*this).ones();
  99. if(is_same_type<fill_type, fill::fill_randu>::yes) (*this).randu();
  100. if(is_same_type<fill_type, fill::fill_randn>::yes) (*this).randn();
  101. if(is_same_type<fill_type, fill::fill_eye >::yes) { arma_debug_check(true, "Cube::Cube(): unsupported fill type"); }
  102. }
  103. template<typename eT>
  104. template<typename fill_type>
  105. inline
  106. Cube<eT>::Cube(const SizeCube& s, const fill::fill_class<fill_type>&)
  107. : n_rows(s.n_rows)
  108. , n_cols(s.n_cols)
  109. , n_elem_slice(s.n_rows*s.n_cols)
  110. , n_slices(s.n_slices)
  111. , n_elem(s.n_rows*s.n_cols*s.n_slices)
  112. , mem_state(0)
  113. , mem()
  114. , mat_ptrs(0)
  115. {
  116. arma_extra_debug_sigprint_this(this);
  117. init_cold();
  118. if(is_same_type<fill_type, fill::fill_zeros>::yes) (*this).zeros();
  119. if(is_same_type<fill_type, fill::fill_ones >::yes) (*this).ones();
  120. if(is_same_type<fill_type, fill::fill_randu>::yes) (*this).randu();
  121. if(is_same_type<fill_type, fill::fill_randn>::yes) (*this).randn();
  122. if(is_same_type<fill_type, fill::fill_eye >::yes) { arma_debug_check(true, "Cube::Cube(): unsupported fill type"); }
  123. }
  124. #if defined(ARMA_USE_CXX11)
  125. template<typename eT>
  126. inline
  127. Cube<eT>::Cube(Cube<eT>&& in_cube)
  128. : n_rows(0)
  129. , n_cols(0)
  130. , n_elem_slice(0)
  131. , n_slices(0)
  132. , n_elem(0)
  133. , mem_state(0)
  134. , mem()
  135. , mat_ptrs(0)
  136. {
  137. arma_extra_debug_sigprint_this(this);
  138. arma_extra_debug_sigprint(arma_str::format("this = %x in_cube = %x") % this % &in_cube);
  139. (*this).steal_mem(in_cube);
  140. }
  141. template<typename eT>
  142. inline
  143. Cube<eT>&
  144. Cube<eT>::operator=(Cube<eT>&& in_cube)
  145. {
  146. arma_extra_debug_sigprint(arma_str::format("this = %x in_cube = %x") % this % &in_cube);
  147. (*this).steal_mem(in_cube);
  148. return *this;
  149. }
  150. #endif
  151. template<typename eT>
  152. inline
  153. void
  154. Cube<eT>::init_cold()
  155. {
  156. arma_extra_debug_sigprint( arma_str::format("n_rows = %d, n_cols = %d, n_slices = %d") % n_rows % n_cols % n_slices );
  157. #if defined(ARMA_64BIT_WORD)
  158. const char* error_message = "Cube::init(): requested size is too large";
  159. #else
  160. const char* error_message = "Cube::init(): requested size is too large; suggest to compile in C++11 mode and/or enable ARMA_64BIT_WORD";
  161. #endif
  162. arma_debug_check
  163. (
  164. (
  165. ( (n_rows > 0x0FFF) || (n_cols > 0x0FFF) || (n_slices > 0xFF) )
  166. ? ( (double(n_rows) * double(n_cols) * double(n_slices)) > double(ARMA_MAX_UWORD) )
  167. : false
  168. ),
  169. error_message
  170. );
  171. if(n_elem <= Cube_prealloc::mem_n_elem)
  172. {
  173. if(n_elem == 0)
  174. {
  175. access::rw(mem) = NULL;
  176. }
  177. else
  178. {
  179. arma_extra_debug_print("Cube::init(): using local memory");
  180. access::rw(mem) = mem_local;
  181. }
  182. }
  183. else
  184. {
  185. arma_extra_debug_print("Cube::init(): acquiring memory");
  186. access::rw(mem) = memory::acquire<eT>(n_elem);
  187. }
  188. create_mat();
  189. }
  190. template<typename eT>
  191. inline
  192. void
  193. Cube<eT>::init_warm(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices)
  194. {
  195. arma_extra_debug_sigprint( arma_str::format("in_n_rows = %d, in_n_cols = %d, in_n_slices = %d") % in_n_rows % in_n_cols % in_n_slices );
  196. if( (n_rows == in_n_rows) && (n_cols == in_n_cols) && (n_slices == in_n_slices) ) { return; }
  197. const uword t_mem_state = mem_state;
  198. bool err_state = false;
  199. char* err_msg = 0;
  200. arma_debug_set_error( err_state, err_msg, (t_mem_state == 3), "Cube::init(): size is fixed and hence cannot be changed" );
  201. #if defined(ARMA_64BIT_WORD)
  202. const char* error_message = "Cube::init(): requested size is too large";
  203. #else
  204. const char* error_message = "Cube::init(): requested size is too large; suggest to compile in C++11 mode and/or enable ARMA_64BIT_WORD";
  205. #endif
  206. arma_debug_set_error
  207. (
  208. err_state,
  209. err_msg,
  210. (
  211. ( (in_n_rows > 0x0FFF) || (in_n_cols > 0x0FFF) || (in_n_slices > 0xFF) )
  212. ? ( (double(in_n_rows) * double(in_n_cols) * double(in_n_slices)) > double(ARMA_MAX_UWORD) )
  213. : false
  214. ),
  215. error_message
  216. );
  217. arma_debug_check(err_state, err_msg);
  218. const uword old_n_elem = n_elem;
  219. const uword new_n_elem = in_n_rows * in_n_cols * in_n_slices;
  220. if(old_n_elem == new_n_elem)
  221. {
  222. arma_extra_debug_print("Cube::init(): reusing memory");
  223. delete_mat();
  224. access::rw(n_rows) = in_n_rows;
  225. access::rw(n_cols) = in_n_cols;
  226. access::rw(n_elem_slice) = in_n_rows*in_n_cols;
  227. access::rw(n_slices) = in_n_slices;
  228. create_mat();
  229. }
  230. else // condition: old_n_elem != new_n_elem
  231. {
  232. arma_debug_check( (t_mem_state == 2), "Cube::init(): requested size is not compatible with the size of auxiliary memory" );
  233. delete_mat();
  234. if(new_n_elem < old_n_elem) // reuse existing memory if possible
  235. {
  236. if( (t_mem_state == 0) && (new_n_elem <= Cube_prealloc::mem_n_elem) )
  237. {
  238. if(old_n_elem > Cube_prealloc::mem_n_elem)
  239. {
  240. arma_extra_debug_print("Cube::init(): releasing memory");
  241. memory::release( access::rw(mem) );
  242. }
  243. if(new_n_elem == 0)
  244. {
  245. access::rw(mem) = NULL;
  246. }
  247. else
  248. {
  249. arma_extra_debug_print("Cube::init(): using local memory");
  250. access::rw(mem) = mem_local;
  251. }
  252. }
  253. else
  254. {
  255. arma_extra_debug_print("Cube::init(): reusing memory");
  256. }
  257. }
  258. else // condition: new_n_elem > old_n_elem
  259. {
  260. if( (t_mem_state == 0) && (old_n_elem > Cube_prealloc::mem_n_elem) )
  261. {
  262. arma_extra_debug_print("Cube::init(): releasing memory");
  263. memory::release( access::rw(mem) );
  264. }
  265. if(new_n_elem <= Cube_prealloc::mem_n_elem)
  266. {
  267. arma_extra_debug_print("Cube::init(): using local memory");
  268. access::rw(mem) = mem_local;
  269. }
  270. else
  271. {
  272. arma_extra_debug_print("Cube::init(): acquiring memory");
  273. access::rw(mem) = memory::acquire<eT>(new_n_elem);
  274. }
  275. access::rw(mem_state) = 0;
  276. }
  277. access::rw(n_rows) = in_n_rows;
  278. access::rw(n_cols) = in_n_cols;
  279. access::rw(n_elem_slice) = in_n_rows*in_n_cols;
  280. access::rw(n_slices) = in_n_slices;
  281. access::rw(n_elem) = new_n_elem;
  282. create_mat();
  283. }
  284. }
  285. //! for constructing a complex cube out of two non-complex cubes
  286. template<typename eT>
  287. template<typename T1, typename T2>
  288. inline
  289. void
  290. Cube<eT>::init
  291. (
  292. const BaseCube<typename Cube<eT>::pod_type,T1>& X,
  293. const BaseCube<typename Cube<eT>::pod_type,T2>& Y
  294. )
  295. {
  296. arma_extra_debug_sigprint();
  297. typedef typename T1::elem_type T;
  298. arma_type_check(( is_cx<eT>::no )); //!< compile-time abort if eT is not std::complex
  299. arma_type_check(( is_cx< T>::yes )); //!< compile-time abort if T is std::complex
  300. arma_type_check(( is_same_type< std::complex<T>, eT >::no )); //!< compile-time abort if types are not compatible
  301. const ProxyCube<T1> PX(X.get_ref());
  302. const ProxyCube<T2> PY(Y.get_ref());
  303. arma_debug_assert_same_size(PX, PY, "Cube()");
  304. const uword local_n_rows = PX.get_n_rows();
  305. const uword local_n_cols = PX.get_n_cols();
  306. const uword local_n_slices = PX.get_n_slices();
  307. init_warm(local_n_rows, local_n_cols, local_n_slices);
  308. eT* out_mem = (*this).memptr();
  309. const bool use_at = ( ProxyCube<T1>::use_at || ProxyCube<T2>::use_at );
  310. if(use_at == false)
  311. {
  312. typedef typename ProxyCube<T1>::ea_type ea_type1;
  313. typedef typename ProxyCube<T2>::ea_type ea_type2;
  314. const uword N = n_elem;
  315. ea_type1 A = PX.get_ea();
  316. ea_type2 B = PY.get_ea();
  317. for(uword i=0; i<N; ++i)
  318. {
  319. out_mem[i] = std::complex<T>(A[i], B[i]);
  320. }
  321. }
  322. else
  323. {
  324. for(uword uslice = 0; uslice < local_n_slices; ++uslice)
  325. for(uword ucol = 0; ucol < local_n_cols; ++ucol )
  326. for(uword urow = 0; urow < local_n_rows; ++urow )
  327. {
  328. *out_mem = std::complex<T>( PX.at(urow,ucol,uslice), PY.at(urow,ucol,uslice) );
  329. out_mem++;
  330. }
  331. }
  332. }
  333. template<typename eT>
  334. inline
  335. void
  336. Cube<eT>::delete_mat()
  337. {
  338. arma_extra_debug_sigprint();
  339. if((n_slices > 0) && (mat_ptrs != NULL))
  340. {
  341. for(uword uslice = 0; uslice < n_slices; ++uslice)
  342. {
  343. if(mat_ptrs[uslice] != NULL) { delete access::rw(mat_ptrs[uslice]); }
  344. }
  345. if( (mem_state <= 2) && (n_slices > Cube_prealloc::mat_ptrs_size) )
  346. {
  347. delete [] mat_ptrs;
  348. }
  349. }
  350. }
  351. template<typename eT>
  352. inline
  353. void
  354. Cube<eT>::create_mat()
  355. {
  356. arma_extra_debug_sigprint();
  357. if(n_slices == 0)
  358. {
  359. access::rw(mat_ptrs) = NULL;
  360. }
  361. else
  362. {
  363. if(mem_state <= 2)
  364. {
  365. if(n_slices <= Cube_prealloc::mat_ptrs_size)
  366. {
  367. access::rw(mat_ptrs) = const_cast< const Mat<eT>** >(mat_ptrs_local);
  368. }
  369. else
  370. {
  371. access::rw(mat_ptrs) = new(std::nothrow) const Mat<eT>*[n_slices];
  372. arma_check_bad_alloc( (mat_ptrs == 0), "Cube::create_mat(): out of memory" );
  373. }
  374. }
  375. for(uword uslice = 0; uslice < n_slices; ++uslice)
  376. {
  377. mat_ptrs[uslice] = NULL;
  378. }
  379. }
  380. }
  381. //! Set the cube to be equal to the specified scalar.
  382. //! NOTE: the size of the cube will be 1x1x1
  383. template<typename eT>
  384. inline
  385. Cube<eT>&
  386. Cube<eT>::operator=(const eT val)
  387. {
  388. arma_extra_debug_sigprint();
  389. init_warm(1,1,1);
  390. access::rw(mem[0]) = val;
  391. return *this;
  392. }
  393. //! In-place addition of a scalar to all elements of the cube
  394. template<typename eT>
  395. inline
  396. Cube<eT>&
  397. Cube<eT>::operator+=(const eT val)
  398. {
  399. arma_extra_debug_sigprint();
  400. arrayops::inplace_plus( memptr(), val, n_elem );
  401. return *this;
  402. }
  403. //! In-place subtraction of a scalar from all elements of the cube
  404. template<typename eT>
  405. inline
  406. Cube<eT>&
  407. Cube<eT>::operator-=(const eT val)
  408. {
  409. arma_extra_debug_sigprint();
  410. arrayops::inplace_minus( memptr(), val, n_elem );
  411. return *this;
  412. }
  413. //! In-place multiplication of all elements of the cube with a scalar
  414. template<typename eT>
  415. inline
  416. Cube<eT>&
  417. Cube<eT>::operator*=(const eT val)
  418. {
  419. arma_extra_debug_sigprint();
  420. arrayops::inplace_mul( memptr(), val, n_elem );
  421. return *this;
  422. }
  423. //! In-place division of all elements of the cube with a scalar
  424. template<typename eT>
  425. inline
  426. Cube<eT>&
  427. Cube<eT>::operator/=(const eT val)
  428. {
  429. arma_extra_debug_sigprint();
  430. arrayops::inplace_div( memptr(), val, n_elem );
  431. return *this;
  432. }
  433. //! construct a cube from a given cube
  434. template<typename eT>
  435. inline
  436. Cube<eT>::Cube(const Cube<eT>& x)
  437. : n_rows(x.n_rows)
  438. , n_cols(x.n_cols)
  439. , n_elem_slice(x.n_elem_slice)
  440. , n_slices(x.n_slices)
  441. , n_elem(x.n_elem)
  442. , mem_state(0)
  443. , mem()
  444. , mat_ptrs(0)
  445. {
  446. arma_extra_debug_sigprint_this(this);
  447. arma_extra_debug_sigprint(arma_str::format("this = %x in_cube = %x") % this % &x);
  448. init_cold();
  449. arrayops::copy( memptr(), x.mem, n_elem );
  450. }
  451. //! construct a cube from a given cube
  452. template<typename eT>
  453. inline
  454. Cube<eT>&
  455. Cube<eT>::operator=(const Cube<eT>& x)
  456. {
  457. arma_extra_debug_sigprint(arma_str::format("this = %x in_cube = %x") % this % &x);
  458. if(this != &x)
  459. {
  460. init_warm(x.n_rows, x.n_cols, x.n_slices);
  461. arrayops::copy( memptr(), x.mem, n_elem );
  462. }
  463. return *this;
  464. }
  465. //! construct a cube from a given auxiliary array of eTs.
  466. //! if copy_aux_mem is true, new memory is allocated and the array is copied.
  467. //! if copy_aux_mem is false, the auxiliary array is used directly (without allocating memory and copying).
  468. template<typename eT>
  469. inline
  470. Cube<eT>::Cube(eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices, const bool copy_aux_mem, const bool strict, const bool prealloc_mat)
  471. : n_rows ( aux_n_rows )
  472. , n_cols ( aux_n_cols )
  473. , n_elem_slice( aux_n_rows*aux_n_cols )
  474. , n_slices ( aux_n_slices )
  475. , n_elem ( aux_n_rows*aux_n_cols*aux_n_slices )
  476. , mem_state ( copy_aux_mem ? 0 : (strict ? 2 : 1) )
  477. , mem ( copy_aux_mem ? 0 : aux_mem )
  478. , mat_ptrs ( 0 )
  479. {
  480. arma_extra_debug_sigprint_this(this);
  481. if(prealloc_mat == true) { arma_debug_warn("Cube::Cube(): parameter 'prealloc_mat' ignored as it's no longer used"); }
  482. if(copy_aux_mem == true)
  483. {
  484. init_cold();
  485. arrayops::copy( memptr(), aux_mem, n_elem );
  486. }
  487. else
  488. {
  489. create_mat();
  490. }
  491. }
  492. //! construct a cube from a given auxiliary read-only array of eTs.
  493. //! the array is copied.
  494. template<typename eT>
  495. inline
  496. Cube<eT>::Cube(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices)
  497. : n_rows(aux_n_rows)
  498. , n_cols(aux_n_cols)
  499. , n_elem_slice(aux_n_rows*aux_n_cols)
  500. , n_slices(aux_n_slices)
  501. , n_elem(aux_n_rows*aux_n_cols*aux_n_slices)
  502. , mem_state(0)
  503. , mem()
  504. , mat_ptrs(0)
  505. {
  506. arma_extra_debug_sigprint_this(this);
  507. init_cold();
  508. arrayops::copy( memptr(), aux_mem, n_elem );
  509. }
  510. //! in-place cube addition
  511. template<typename eT>
  512. inline
  513. Cube<eT>&
  514. Cube<eT>::operator+=(const Cube<eT>& m)
  515. {
  516. arma_extra_debug_sigprint();
  517. arma_debug_assert_same_size(*this, m, "addition");
  518. arrayops::inplace_plus( memptr(), m.memptr(), n_elem );
  519. return *this;
  520. }
  521. //! in-place cube subtraction
  522. template<typename eT>
  523. inline
  524. Cube<eT>&
  525. Cube<eT>::operator-=(const Cube<eT>& m)
  526. {
  527. arma_extra_debug_sigprint();
  528. arma_debug_assert_same_size(*this, m, "subtraction");
  529. arrayops::inplace_minus( memptr(), m.memptr(), n_elem );
  530. return *this;
  531. }
  532. //! in-place element-wise cube multiplication
  533. template<typename eT>
  534. inline
  535. Cube<eT>&
  536. Cube<eT>::operator%=(const Cube<eT>& m)
  537. {
  538. arma_extra_debug_sigprint();
  539. arma_debug_assert_same_size(*this, m, "element-wise multiplication");
  540. arrayops::inplace_mul( memptr(), m.memptr(), n_elem );
  541. return *this;
  542. }
  543. //! in-place element-wise cube division
  544. template<typename eT>
  545. inline
  546. Cube<eT>&
  547. Cube<eT>::operator/=(const Cube<eT>& m)
  548. {
  549. arma_extra_debug_sigprint();
  550. arma_debug_assert_same_size(*this, m, "element-wise division");
  551. arrayops::inplace_div( memptr(), m.memptr(), n_elem );
  552. return *this;
  553. }
  554. //! for constructing a complex cube out of two non-complex cubes
  555. template<typename eT>
  556. template<typename T1, typename T2>
  557. inline
  558. Cube<eT>::Cube
  559. (
  560. const BaseCube<typename Cube<eT>::pod_type,T1>& A,
  561. const BaseCube<typename Cube<eT>::pod_type,T2>& B
  562. )
  563. : n_rows(0)
  564. , n_cols(0)
  565. , n_elem_slice(0)
  566. , n_slices(0)
  567. , n_elem(0)
  568. , mem_state(0)
  569. , mem()
  570. , mat_ptrs(0)
  571. {
  572. arma_extra_debug_sigprint_this(this);
  573. init(A,B);
  574. }
  575. //! construct a cube from a subview_cube instance (e.g. construct a cube from a delayed subcube operation)
  576. template<typename eT>
  577. inline
  578. Cube<eT>::Cube(const subview_cube<eT>& X)
  579. : n_rows(X.n_rows)
  580. , n_cols(X.n_cols)
  581. , n_elem_slice(X.n_elem_slice)
  582. , n_slices(X.n_slices)
  583. , n_elem(X.n_elem)
  584. , mem_state(0)
  585. , mem()
  586. , mat_ptrs(0)
  587. {
  588. arma_extra_debug_sigprint_this(this);
  589. init_cold();
  590. subview_cube<eT>::extract(*this, X);
  591. }
  592. //! construct a cube from a subview_cube instance (e.g. construct a cube from a delayed subcube operation)
  593. template<typename eT>
  594. inline
  595. Cube<eT>&
  596. Cube<eT>::operator=(const subview_cube<eT>& X)
  597. {
  598. arma_extra_debug_sigprint();
  599. const bool alias = (this == &(X.m));
  600. if(alias == false)
  601. {
  602. init_warm(X.n_rows, X.n_cols, X.n_slices);
  603. subview_cube<eT>::extract(*this, X);
  604. }
  605. else
  606. {
  607. Cube<eT> tmp(X);
  608. steal_mem(tmp);
  609. }
  610. return *this;
  611. }
  612. //! in-place cube addition (using a subcube on the right-hand-side)
  613. template<typename eT>
  614. inline
  615. Cube<eT>&
  616. Cube<eT>::operator+=(const subview_cube<eT>& X)
  617. {
  618. arma_extra_debug_sigprint();
  619. subview_cube<eT>::plus_inplace(*this, X);
  620. return *this;
  621. }
  622. //! in-place cube subtraction (using a subcube on the right-hand-side)
  623. template<typename eT>
  624. inline
  625. Cube<eT>&
  626. Cube<eT>::operator-=(const subview_cube<eT>& X)
  627. {
  628. arma_extra_debug_sigprint();
  629. subview_cube<eT>::minus_inplace(*this, X);
  630. return *this;
  631. }
  632. //! in-place element-wise cube mutiplication (using a subcube on the right-hand-side)
  633. template<typename eT>
  634. inline
  635. Cube<eT>&
  636. Cube<eT>::operator%=(const subview_cube<eT>& X)
  637. {
  638. arma_extra_debug_sigprint();
  639. subview_cube<eT>::schur_inplace(*this, X);
  640. return *this;
  641. }
  642. //! in-place element-wise cube division (using a subcube on the right-hand-side)
  643. template<typename eT>
  644. inline
  645. Cube<eT>&
  646. Cube<eT>::operator/=(const subview_cube<eT>& X)
  647. {
  648. arma_extra_debug_sigprint();
  649. subview_cube<eT>::div_inplace(*this, X);
  650. return *this;
  651. }
  652. template<typename eT>
  653. template<typename T1>
  654. inline
  655. Cube<eT>::Cube(const subview_cube_slices<eT,T1>& X)
  656. : n_rows(0)
  657. , n_cols(0)
  658. , n_elem_slice(0)
  659. , n_slices(0)
  660. , n_elem(0)
  661. , mem_state(0)
  662. , mem()
  663. , mat_ptrs(0)
  664. {
  665. arma_extra_debug_sigprint_this(this);
  666. subview_cube_slices<eT,T1>::extract(*this, X);
  667. }
  668. template<typename eT>
  669. template<typename T1>
  670. inline
  671. Cube<eT>&
  672. Cube<eT>::operator=(const subview_cube_slices<eT,T1>& X)
  673. {
  674. arma_extra_debug_sigprint();
  675. const bool alias = (this == &(X.m));
  676. if(alias == false)
  677. {
  678. subview_cube_slices<eT,T1>::extract(*this, X);
  679. }
  680. else
  681. {
  682. Cube<eT> tmp(X);
  683. steal_mem(tmp);
  684. }
  685. return *this;
  686. }
  687. template<typename eT>
  688. template<typename T1>
  689. inline
  690. Cube<eT>&
  691. Cube<eT>::operator+=(const subview_cube_slices<eT,T1>& X)
  692. {
  693. arma_extra_debug_sigprint();
  694. subview_cube_slices<eT,T1>::plus_inplace(*this, X);
  695. return *this;
  696. }
  697. template<typename eT>
  698. template<typename T1>
  699. inline
  700. Cube<eT>&
  701. Cube<eT>::operator-=(const subview_cube_slices<eT,T1>& X)
  702. {
  703. arma_extra_debug_sigprint();
  704. subview_cube_slices<eT,T1>::minus_inplace(*this, X);
  705. return *this;
  706. }
  707. template<typename eT>
  708. template<typename T1>
  709. inline
  710. Cube<eT>&
  711. Cube<eT>::operator%=(const subview_cube_slices<eT,T1>& X)
  712. {
  713. arma_extra_debug_sigprint();
  714. subview_cube_slices<eT,T1>::schur_inplace(*this, X);
  715. return *this;
  716. }
  717. template<typename eT>
  718. template<typename T1>
  719. inline
  720. Cube<eT>&
  721. Cube<eT>::operator/=(const subview_cube_slices<eT,T1>& X)
  722. {
  723. arma_extra_debug_sigprint();
  724. subview_cube_slices<eT,T1>::div_inplace(*this, X);
  725. return *this;
  726. }
  727. //! creation of subview_cube (subcube comprised of specified row)
  728. template<typename eT>
  729. arma_inline
  730. subview_cube<eT>
  731. Cube<eT>::row(const uword in_row)
  732. {
  733. arma_extra_debug_sigprint();
  734. arma_debug_check( (in_row >= n_rows), "Cube::row(): index out of bounds" );
  735. return (*this).rows(in_row, in_row);
  736. }
  737. //! creation of subview_cube (subcube comprised of specified row)
  738. template<typename eT>
  739. arma_inline
  740. const subview_cube<eT>
  741. Cube<eT>::row(const uword in_row) const
  742. {
  743. arma_extra_debug_sigprint();
  744. arma_debug_check( (in_row >= n_rows), "Cube::row(): index out of bounds" );
  745. return (*this).rows(in_row, in_row);
  746. }
  747. //! creation of subview_cube (subcube comprised of specified column)
  748. template<typename eT>
  749. arma_inline
  750. subview_cube<eT>
  751. Cube<eT>::col(const uword in_col)
  752. {
  753. arma_extra_debug_sigprint();
  754. arma_debug_check( (in_col >= n_cols), "Cube::col(): index out of bounds" );
  755. return (*this).cols(in_col, in_col);
  756. }
  757. //! creation of subview_cube (subcube comprised of specified column)
  758. template<typename eT>
  759. arma_inline
  760. const subview_cube<eT>
  761. Cube<eT>::col(const uword in_col) const
  762. {
  763. arma_extra_debug_sigprint();
  764. arma_debug_check( (in_col >= n_cols), "Cube::col(): index out of bounds" );
  765. return (*this).cols(in_col, in_col);
  766. }
  767. //! provide the reference to the matrix representing a single slice
  768. template<typename eT>
  769. inline
  770. Mat<eT>&
  771. Cube<eT>::slice(const uword in_slice)
  772. {
  773. arma_extra_debug_sigprint();
  774. arma_debug_check( (in_slice >= n_slices), "Cube::slice(): index out of bounds" );
  775. if(mat_ptrs[in_slice] == NULL)
  776. {
  777. const eT* ptr = (n_elem_slice > 0) ? slice_memptr(in_slice) : NULL;
  778. mat_ptrs[in_slice] = new Mat<eT>('j', ptr, n_rows, n_cols);
  779. }
  780. return const_cast< Mat<eT>& >( *(mat_ptrs[in_slice]) );
  781. }
  782. //! provide the reference to the matrix representing a single slice
  783. template<typename eT>
  784. inline
  785. const Mat<eT>&
  786. Cube<eT>::slice(const uword in_slice) const
  787. {
  788. arma_extra_debug_sigprint();
  789. arma_debug_check( (in_slice >= n_slices), "Cube::slice(): index out of bounds" );
  790. if(mat_ptrs[in_slice] == NULL)
  791. {
  792. const eT* ptr = (n_elem_slice > 0) ? slice_memptr(in_slice) : NULL;
  793. mat_ptrs[in_slice] = new Mat<eT>('j', ptr, n_rows, n_cols);
  794. }
  795. return *(mat_ptrs[in_slice]);
  796. }
  797. //! creation of subview_cube (subcube comprised of specified rows)
  798. template<typename eT>
  799. arma_inline
  800. subview_cube<eT>
  801. Cube<eT>::rows(const uword in_row1, const uword in_row2)
  802. {
  803. arma_extra_debug_sigprint();
  804. arma_debug_check
  805. (
  806. (in_row1 > in_row2) || (in_row2 >= n_rows),
  807. "Cube::rows(): indices out of bounds or incorrectly used"
  808. );
  809. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  810. return subview_cube<eT>(*this, in_row1, 0, 0, subcube_n_rows, n_cols, n_slices);
  811. }
  812. //! creation of subview_cube (subcube comprised of specified rows)
  813. template<typename eT>
  814. arma_inline
  815. const subview_cube<eT>
  816. Cube<eT>::rows(const uword in_row1, const uword in_row2) const
  817. {
  818. arma_extra_debug_sigprint();
  819. arma_debug_check
  820. (
  821. (in_row1 > in_row2) || (in_row2 >= n_rows),
  822. "Cube::rows(): indices out of bounds or incorrectly used"
  823. );
  824. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  825. return subview_cube<eT>(*this, in_row1, 0, 0, subcube_n_rows, n_cols, n_slices);
  826. }
  827. //! creation of subview_cube (subcube comprised of specified columns)
  828. template<typename eT>
  829. arma_inline
  830. subview_cube<eT>
  831. Cube<eT>::cols(const uword in_col1, const uword in_col2)
  832. {
  833. arma_extra_debug_sigprint();
  834. arma_debug_check
  835. (
  836. (in_col1 > in_col2) || (in_col2 >= n_cols),
  837. "Cube::cols(): indices out of bounds or incorrectly used"
  838. );
  839. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  840. return subview_cube<eT>(*this, 0, in_col1, 0, n_rows, subcube_n_cols, n_slices);
  841. }
  842. //! creation of subview_cube (subcube comprised of specified columns)
  843. template<typename eT>
  844. arma_inline
  845. const subview_cube<eT>
  846. Cube<eT>::cols(const uword in_col1, const uword in_col2) const
  847. {
  848. arma_extra_debug_sigprint();
  849. arma_debug_check
  850. (
  851. (in_col1 > in_col2) || (in_col2 >= n_cols),
  852. "Cube::cols(): indices out of bounds or incorrectly used"
  853. );
  854. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  855. return subview_cube<eT>(*this, 0, in_col1, 0, n_rows, subcube_n_cols, n_slices);
  856. }
  857. //! creation of subview_cube (subcube comprised of specified slices)
  858. template<typename eT>
  859. arma_inline
  860. subview_cube<eT>
  861. Cube<eT>::slices(const uword in_slice1, const uword in_slice2)
  862. {
  863. arma_extra_debug_sigprint();
  864. arma_debug_check
  865. (
  866. (in_slice1 > in_slice2) || (in_slice2 >= n_slices),
  867. "Cube::slices(): indices out of bounds or incorrectly used"
  868. );
  869. const uword subcube_n_slices = in_slice2 - in_slice1 + 1;
  870. return subview_cube<eT>(*this, 0, 0, in_slice1, n_rows, n_cols, subcube_n_slices);
  871. }
  872. //! creation of subview_cube (subcube comprised of specified slices)
  873. template<typename eT>
  874. arma_inline
  875. const subview_cube<eT>
  876. Cube<eT>::slices(const uword in_slice1, const uword in_slice2) const
  877. {
  878. arma_extra_debug_sigprint();
  879. arma_debug_check
  880. (
  881. (in_slice1 > in_slice2) || (in_slice2 >= n_slices),
  882. "Cube::slices(): indices out of bounds or incorrectly used"
  883. );
  884. const uword subcube_n_slices = in_slice2 - in_slice1 + 1;
  885. return subview_cube<eT>(*this, 0, 0, in_slice1, n_rows, n_cols, subcube_n_slices);
  886. }
  887. //! creation of subview_cube (generic subcube)
  888. template<typename eT>
  889. arma_inline
  890. subview_cube<eT>
  891. Cube<eT>::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2)
  892. {
  893. arma_extra_debug_sigprint();
  894. arma_debug_check
  895. (
  896. (in_row1 > in_row2) || (in_col1 > in_col2) || (in_slice1 > in_slice2) ||
  897. (in_row2 >= n_rows) || (in_col2 >= n_cols) || (in_slice2 >= n_slices),
  898. "Cube::subcube(): indices out of bounds or incorrectly used"
  899. );
  900. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  901. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  902. const uword subcube_n_slices = in_slice2 - in_slice1 + 1;
  903. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices);
  904. }
  905. //! creation of subview_cube (generic subcube)
  906. template<typename eT>
  907. arma_inline
  908. const subview_cube<eT>
  909. Cube<eT>::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2) const
  910. {
  911. arma_extra_debug_sigprint();
  912. arma_debug_check
  913. (
  914. (in_row1 > in_row2) || (in_col1 > in_col2) || (in_slice1 > in_slice2) ||
  915. (in_row2 >= n_rows) || (in_col2 >= n_cols) || (in_slice2 >= n_slices),
  916. "Cube::subcube(): indices out of bounds or incorrectly used"
  917. );
  918. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  919. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  920. const uword subcube_n_slices = in_slice2 - in_slice1 + 1;
  921. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices);
  922. }
  923. //! creation of subview_cube (generic subcube)
  924. template<typename eT>
  925. inline
  926. subview_cube<eT>
  927. Cube<eT>::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s)
  928. {
  929. arma_extra_debug_sigprint();
  930. const uword l_n_rows = n_rows;
  931. const uword l_n_cols = n_cols;
  932. const uword l_n_slices = n_slices;
  933. const uword s_n_rows = s.n_rows;
  934. const uword s_n_cols = s.n_cols;
  935. const uword s_n_slices = s.n_slices;
  936. arma_debug_check
  937. (
  938. ( in_row1 >= l_n_rows) || ( in_col1 >= l_n_cols) || ( in_slice1 >= l_n_slices)
  939. || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols) || ((in_slice1 + s_n_slices) > l_n_slices),
  940. "Cube::subcube(): indices or size out of bounds"
  941. );
  942. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, s_n_rows, s_n_cols, s_n_slices);
  943. }
  944. //! creation of subview_cube (generic subcube)
  945. template<typename eT>
  946. inline
  947. const subview_cube<eT>
  948. Cube<eT>::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s) const
  949. {
  950. arma_extra_debug_sigprint();
  951. const uword l_n_rows = n_rows;
  952. const uword l_n_cols = n_cols;
  953. const uword l_n_slices = n_slices;
  954. const uword s_n_rows = s.n_rows;
  955. const uword s_n_cols = s.n_cols;
  956. const uword s_n_slices = s.n_slices;
  957. arma_debug_check
  958. (
  959. ( in_row1 >= l_n_rows) || ( in_col1 >= l_n_cols) || ( in_slice1 >= l_n_slices)
  960. || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols) || ((in_slice1 + s_n_slices) > l_n_slices),
  961. "Cube::subcube(): indices or size out of bounds"
  962. );
  963. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, s_n_rows, s_n_cols, s_n_slices);
  964. }
  965. //! creation of subview_cube (generic subcube)
  966. template<typename eT>
  967. inline
  968. subview_cube<eT>
  969. Cube<eT>::subcube(const span& row_span, const span& col_span, const span& slice_span)
  970. {
  971. arma_extra_debug_sigprint();
  972. const bool row_all = row_span.whole;
  973. const bool col_all = col_span.whole;
  974. const bool slice_all = slice_span.whole;
  975. const uword local_n_rows = n_rows;
  976. const uword local_n_cols = n_cols;
  977. const uword local_n_slices = n_slices;
  978. const uword in_row1 = row_all ? 0 : row_span.a;
  979. const uword in_row2 = row_span.b;
  980. const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  981. const uword in_col1 = col_all ? 0 : col_span.a;
  982. const uword in_col2 = col_span.b;
  983. const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  984. const uword in_slice1 = slice_all ? 0 : slice_span.a;
  985. const uword in_slice2 = slice_span.b;
  986. const uword subcube_n_slices = slice_all ? local_n_slices : in_slice2 - in_slice1 + 1;
  987. arma_debug_check
  988. (
  989. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  990. ||
  991. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  992. ||
  993. ( slice_all ? false : ((in_slice1 > in_slice2) || (in_slice2 >= local_n_slices)) )
  994. ,
  995. "Cube::subcube(): indices out of bounds or incorrectly used"
  996. );
  997. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices);
  998. }
  999. //! creation of subview_cube (generic subcube)
  1000. template<typename eT>
  1001. inline
  1002. const subview_cube<eT>
  1003. Cube<eT>::subcube(const span& row_span, const span& col_span, const span& slice_span) const
  1004. {
  1005. arma_extra_debug_sigprint();
  1006. const bool row_all = row_span.whole;
  1007. const bool col_all = col_span.whole;
  1008. const bool slice_all = slice_span.whole;
  1009. const uword local_n_rows = n_rows;
  1010. const uword local_n_cols = n_cols;
  1011. const uword local_n_slices = n_slices;
  1012. const uword in_row1 = row_all ? 0 : row_span.a;
  1013. const uword in_row2 = row_span.b;
  1014. const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1015. const uword in_col1 = col_all ? 0 : col_span.a;
  1016. const uword in_col2 = col_span.b;
  1017. const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1018. const uword in_slice1 = slice_all ? 0 : slice_span.a;
  1019. const uword in_slice2 = slice_span.b;
  1020. const uword subcube_n_slices = slice_all ? local_n_slices : in_slice2 - in_slice1 + 1;
  1021. arma_debug_check
  1022. (
  1023. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1024. ||
  1025. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1026. ||
  1027. ( slice_all ? false : ((in_slice1 > in_slice2) || (in_slice2 >= local_n_slices)) )
  1028. ,
  1029. "Cube::subcube(): indices out of bounds or incorrectly used"
  1030. );
  1031. return subview_cube<eT>(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices);
  1032. }
  1033. template<typename eT>
  1034. inline
  1035. subview_cube<eT>
  1036. Cube<eT>::operator()(const span& row_span, const span& col_span, const span& slice_span)
  1037. {
  1038. arma_extra_debug_sigprint();
  1039. return (*this).subcube(row_span, col_span, slice_span);
  1040. }
  1041. template<typename eT>
  1042. inline
  1043. const subview_cube<eT>
  1044. Cube<eT>::operator()(const span& row_span, const span& col_span, const span& slice_span) const
  1045. {
  1046. arma_extra_debug_sigprint();
  1047. return (*this).subcube(row_span, col_span, slice_span);
  1048. }
  1049. template<typename eT>
  1050. inline
  1051. subview_cube<eT>
  1052. Cube<eT>::operator()(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s)
  1053. {
  1054. arma_extra_debug_sigprint();
  1055. return (*this).subcube(in_row1, in_col1, in_slice1, s);
  1056. }
  1057. template<typename eT>
  1058. inline
  1059. const subview_cube<eT>
  1060. Cube<eT>::operator()(const uword in_row1, const uword in_col1, const uword in_slice1, const SizeCube& s) const
  1061. {
  1062. arma_extra_debug_sigprint();
  1063. return (*this).subcube(in_row1, in_col1, in_slice1, s);
  1064. }
  1065. template<typename eT>
  1066. arma_inline
  1067. subview_cube<eT>
  1068. Cube<eT>::tube(const uword in_row1, const uword in_col1)
  1069. {
  1070. arma_extra_debug_sigprint();
  1071. arma_debug_check
  1072. (
  1073. ((in_row1 >= n_rows) || (in_col1 >= n_cols)),
  1074. "Cube::tube(): indices out of bounds"
  1075. );
  1076. return subview_cube<eT>(*this, in_row1, in_col1, 0, 1, 1, n_slices);
  1077. }
  1078. template<typename eT>
  1079. arma_inline
  1080. const subview_cube<eT>
  1081. Cube<eT>::tube(const uword in_row1, const uword in_col1) const
  1082. {
  1083. arma_extra_debug_sigprint();
  1084. arma_debug_check
  1085. (
  1086. ((in_row1 >= n_rows) || (in_col1 >= n_cols)),
  1087. "Cube::tube(): indices out of bounds"
  1088. );
  1089. return subview_cube<eT>(*this, in_row1, in_col1, 0, 1, 1, n_slices);
  1090. }
  1091. template<typename eT>
  1092. arma_inline
  1093. subview_cube<eT>
  1094. Cube<eT>::tube(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2)
  1095. {
  1096. arma_extra_debug_sigprint();
  1097. arma_debug_check
  1098. (
  1099. (in_row1 > in_row2) || (in_col1 > in_col2) ||
  1100. (in_row2 >= n_rows) || (in_col2 >= n_cols),
  1101. "Cube::tube(): indices out of bounds or incorrectly used"
  1102. );
  1103. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  1104. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  1105. return subview_cube<eT>(*this, in_row1, in_col1, 0, subcube_n_rows, subcube_n_cols, n_slices);
  1106. }
  1107. template<typename eT>
  1108. arma_inline
  1109. const subview_cube<eT>
  1110. Cube<eT>::tube(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const
  1111. {
  1112. arma_extra_debug_sigprint();
  1113. arma_debug_check
  1114. (
  1115. (in_row1 > in_row2) || (in_col1 > in_col2) ||
  1116. (in_row2 >= n_rows) || (in_col2 >= n_cols),
  1117. "Cube::tube(): indices out of bounds or incorrectly used"
  1118. );
  1119. const uword subcube_n_rows = in_row2 - in_row1 + 1;
  1120. const uword subcube_n_cols = in_col2 - in_col1 + 1;
  1121. return subview_cube<eT>(*this, in_row1, in_col1, 0, subcube_n_rows, subcube_n_cols, n_slices);
  1122. }
  1123. template<typename eT>
  1124. arma_inline
  1125. subview_cube<eT>
  1126. Cube<eT>::tube(const uword in_row1, const uword in_col1, const SizeMat& s)
  1127. {
  1128. arma_extra_debug_sigprint();
  1129. const uword l_n_rows = n_rows;
  1130. const uword l_n_cols = n_cols;
  1131. const uword s_n_rows = s.n_rows;
  1132. const uword s_n_cols = s.n_cols;
  1133. arma_debug_check
  1134. (
  1135. ((in_row1 >= l_n_rows) || (in_col1 >= l_n_cols) || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols)),
  1136. "Cube::tube(): indices or size out of bounds"
  1137. );
  1138. return subview_cube<eT>(*this, in_row1, in_col1, 0, s_n_rows, s_n_cols, n_slices);
  1139. }
  1140. template<typename eT>
  1141. arma_inline
  1142. const subview_cube<eT>
  1143. Cube<eT>::tube(const uword in_row1, const uword in_col1, const SizeMat& s) const
  1144. {
  1145. arma_extra_debug_sigprint();
  1146. const uword l_n_rows = n_rows;
  1147. const uword l_n_cols = n_cols;
  1148. const uword s_n_rows = s.n_rows;
  1149. const uword s_n_cols = s.n_cols;
  1150. arma_debug_check
  1151. (
  1152. ((in_row1 >= l_n_rows) || (in_col1 >= l_n_cols) || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols)),
  1153. "Cube::tube(): indices or size out of bounds"
  1154. );
  1155. return subview_cube<eT>(*this, in_row1, in_col1, 0, s_n_rows, s_n_cols, n_slices);
  1156. }
  1157. template<typename eT>
  1158. inline
  1159. subview_cube<eT>
  1160. Cube<eT>::tube(const span& row_span, const span& col_span)
  1161. {
  1162. arma_extra_debug_sigprint();
  1163. const bool row_all = row_span.whole;
  1164. const bool col_all = col_span.whole;
  1165. const uword local_n_rows = n_rows;
  1166. const uword local_n_cols = n_cols;
  1167. const uword in_row1 = row_all ? 0 : row_span.a;
  1168. const uword in_row2 = row_span.b;
  1169. const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1170. const uword in_col1 = col_all ? 0 : col_span.a;
  1171. const uword in_col2 = col_span.b;
  1172. const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1173. arma_debug_check
  1174. (
  1175. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1176. ||
  1177. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1178. ,
  1179. "Cube::tube(): indices out of bounds or incorrectly used"
  1180. );
  1181. return subview_cube<eT>(*this, in_row1, in_col1, 0, subcube_n_rows, subcube_n_cols, n_slices);
  1182. }
  1183. template<typename eT>
  1184. inline
  1185. const subview_cube<eT>
  1186. Cube<eT>::tube(const span& row_span, const span& col_span) const
  1187. {
  1188. arma_extra_debug_sigprint();
  1189. const bool row_all = row_span.whole;
  1190. const bool col_all = col_span.whole;
  1191. const uword local_n_rows = n_rows;
  1192. const uword local_n_cols = n_cols;
  1193. const uword in_row1 = row_all ? 0 : row_span.a;
  1194. const uword in_row2 = row_span.b;
  1195. const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1;
  1196. const uword in_col1 = col_all ? 0 : col_span.a;
  1197. const uword in_col2 = col_span.b;
  1198. const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1;
  1199. arma_debug_check
  1200. (
  1201. ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) )
  1202. ||
  1203. ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) )
  1204. ,
  1205. "Cube::tube(): indices out of bounds or incorrectly used"
  1206. );
  1207. return subview_cube<eT>(*this, in_row1, in_col1, 0, subcube_n_rows, subcube_n_cols, n_slices);
  1208. }
  1209. template<typename eT>
  1210. inline
  1211. subview_cube<eT>
  1212. Cube<eT>::head_slices(const uword N)
  1213. {
  1214. arma_extra_debug_sigprint();
  1215. arma_debug_check( (N > n_slices), "Cube::head_slices(): size out of bounds" );
  1216. return subview_cube<eT>(*this, 0, 0, 0, n_rows, n_cols, N);
  1217. }
  1218. template<typename eT>
  1219. inline
  1220. const subview_cube<eT>
  1221. Cube<eT>::head_slices(const uword N) const
  1222. {
  1223. arma_extra_debug_sigprint();
  1224. arma_debug_check( (N > n_slices), "Cube::head_slices(): size out of bounds" );
  1225. return subview_cube<eT>(*this, 0, 0, 0, n_rows, n_cols, N);
  1226. }
  1227. template<typename eT>
  1228. inline
  1229. subview_cube<eT>
  1230. Cube<eT>::tail_slices(const uword N)
  1231. {
  1232. arma_extra_debug_sigprint();
  1233. arma_debug_check( (N > n_slices), "Cube::tail_slices(): size out of bounds" );
  1234. const uword start_slice = n_slices - N;
  1235. return subview_cube<eT>(*this, 0, 0, start_slice, n_rows, n_cols, N);
  1236. }
  1237. template<typename eT>
  1238. inline
  1239. const subview_cube<eT>
  1240. Cube<eT>::tail_slices(const uword N) const
  1241. {
  1242. arma_extra_debug_sigprint();
  1243. arma_debug_check( (N > n_slices), "Cube::tail_slices(): size out of bounds" );
  1244. const uword start_slice = n_slices - N;
  1245. return subview_cube<eT>(*this, 0, 0, start_slice, n_rows, n_cols, N);
  1246. }
  1247. template<typename eT>
  1248. template<typename T1>
  1249. arma_inline
  1250. subview_elem1<eT,T1>
  1251. Cube<eT>::elem(const Base<uword,T1>& a)
  1252. {
  1253. arma_extra_debug_sigprint();
  1254. return subview_elem1<eT,T1>(*this, a);
  1255. }
  1256. template<typename eT>
  1257. template<typename T1>
  1258. arma_inline
  1259. const subview_elem1<eT,T1>
  1260. Cube<eT>::elem(const Base<uword,T1>& a) const
  1261. {
  1262. arma_extra_debug_sigprint();
  1263. return subview_elem1<eT,T1>(*this, a);
  1264. }
  1265. template<typename eT>
  1266. template<typename T1>
  1267. arma_inline
  1268. subview_elem1<eT,T1>
  1269. Cube<eT>::operator()(const Base<uword,T1>& a)
  1270. {
  1271. arma_extra_debug_sigprint();
  1272. return subview_elem1<eT,T1>(*this, a);
  1273. }
  1274. template<typename eT>
  1275. template<typename T1>
  1276. arma_inline
  1277. const subview_elem1<eT,T1>
  1278. Cube<eT>::operator()(const Base<uword,T1>& a) const
  1279. {
  1280. arma_extra_debug_sigprint();
  1281. return subview_elem1<eT,T1>(*this, a);
  1282. }
  1283. template<typename eT>
  1284. arma_inline
  1285. subview_cube_each1<eT>
  1286. Cube<eT>::each_slice()
  1287. {
  1288. arma_extra_debug_sigprint();
  1289. return subview_cube_each1<eT>(*this);
  1290. }
  1291. template<typename eT>
  1292. arma_inline
  1293. const subview_cube_each1<eT>
  1294. Cube<eT>::each_slice() const
  1295. {
  1296. arma_extra_debug_sigprint();
  1297. return subview_cube_each1<eT>(*this);
  1298. }
  1299. template<typename eT>
  1300. template<typename T1>
  1301. inline
  1302. subview_cube_each2<eT, T1>
  1303. Cube<eT>::each_slice(const Base<uword, T1>& indices)
  1304. {
  1305. arma_extra_debug_sigprint();
  1306. return subview_cube_each2<eT, T1>(*this, indices);
  1307. }
  1308. template<typename eT>
  1309. template<typename T1>
  1310. inline
  1311. const subview_cube_each2<eT, T1>
  1312. Cube<eT>::each_slice(const Base<uword, T1>& indices) const
  1313. {
  1314. arma_extra_debug_sigprint();
  1315. return subview_cube_each2<eT, T1>(*this, indices);
  1316. }
  1317. #if defined(ARMA_USE_CXX11)
  1318. //! apply a lambda function to each slice, where each slice is interpreted as a matrix
  1319. template<typename eT>
  1320. inline
  1321. const Cube<eT>&
  1322. Cube<eT>::each_slice(const std::function< void(Mat<eT>&) >& F)
  1323. {
  1324. arma_extra_debug_sigprint();
  1325. for(uword slice_id=0; slice_id < n_slices; ++slice_id)
  1326. {
  1327. Mat<eT> tmp('j', slice_memptr(slice_id), n_rows, n_cols);
  1328. F(tmp);
  1329. }
  1330. return *this;
  1331. }
  1332. template<typename eT>
  1333. inline
  1334. const Cube<eT>&
  1335. Cube<eT>::each_slice(const std::function< void(const Mat<eT>&) >& F) const
  1336. {
  1337. arma_extra_debug_sigprint();
  1338. for(uword slice_id=0; slice_id < n_slices; ++slice_id)
  1339. {
  1340. const Mat<eT> tmp('j', slice_memptr(slice_id), n_rows, n_cols);
  1341. F(tmp);
  1342. }
  1343. return *this;
  1344. }
  1345. template<typename eT>
  1346. inline
  1347. const Cube<eT>&
  1348. Cube<eT>::each_slice(const std::function< void(Mat<eT>&) >& F, const bool use_mp)
  1349. {
  1350. arma_extra_debug_sigprint();
  1351. if((use_mp == false) || (arma_config::openmp == false))
  1352. {
  1353. return (*this).each_slice(F);
  1354. }
  1355. #if defined(ARMA_USE_OPENMP)
  1356. {
  1357. const uword local_n_slices = n_slices;
  1358. const int n_threads = mp_thread_limit::get();
  1359. #pragma omp parallel for schedule(static) num_threads(n_threads)
  1360. for(uword slice_id=0; slice_id < local_n_slices; ++slice_id)
  1361. {
  1362. Mat<eT> tmp('j', slice_memptr(slice_id), n_rows, n_cols);
  1363. F(tmp);
  1364. }
  1365. }
  1366. #endif
  1367. return *this;
  1368. }
  1369. template<typename eT>
  1370. inline
  1371. const Cube<eT>&
  1372. Cube<eT>::each_slice(const std::function< void(const Mat<eT>&) >& F, const bool use_mp) const
  1373. {
  1374. arma_extra_debug_sigprint();
  1375. if((use_mp == false) || (arma_config::openmp == false))
  1376. {
  1377. return (*this).each_slice(F);
  1378. }
  1379. #if defined(ARMA_USE_OPENMP)
  1380. {
  1381. const uword local_n_slices = n_slices;
  1382. const int n_threads = mp_thread_limit::get();
  1383. #pragma omp parallel for schedule(static) num_threads(n_threads)
  1384. for(uword slice_id=0; slice_id < local_n_slices; ++slice_id)
  1385. {
  1386. Mat<eT> tmp('j', slice_memptr(slice_id), n_rows, n_cols);
  1387. F(tmp);
  1388. }
  1389. }
  1390. #endif
  1391. return *this;
  1392. }
  1393. #endif
  1394. template<typename eT>
  1395. template<typename T1>
  1396. inline
  1397. subview_cube_slices<eT, T1>
  1398. Cube<eT>::slices(const Base<uword, T1>& indices)
  1399. {
  1400. arma_extra_debug_sigprint();
  1401. return subview_cube_slices<eT, T1>(*this, indices);
  1402. }
  1403. template<typename eT>
  1404. template<typename T1>
  1405. inline
  1406. const subview_cube_slices<eT, T1>
  1407. Cube<eT>::slices(const Base<uword, T1>& indices) const
  1408. {
  1409. arma_extra_debug_sigprint();
  1410. return subview_cube_slices<eT, T1>(*this, indices);
  1411. }
  1412. //! remove specified row
  1413. template<typename eT>
  1414. inline
  1415. void
  1416. Cube<eT>::shed_row(const uword row_num)
  1417. {
  1418. arma_extra_debug_sigprint();
  1419. arma_debug_check( row_num >= n_rows, "Cube::shed_row(): index out of bounds");
  1420. shed_rows(row_num, row_num);
  1421. }
  1422. //! remove specified column
  1423. template<typename eT>
  1424. inline
  1425. void
  1426. Cube<eT>::shed_col(const uword col_num)
  1427. {
  1428. arma_extra_debug_sigprint();
  1429. arma_debug_check( col_num >= n_cols, "Cube::shed_col(): index out of bounds");
  1430. shed_cols(col_num, col_num);
  1431. }
  1432. //! remove specified slice
  1433. template<typename eT>
  1434. inline
  1435. void
  1436. Cube<eT>::shed_slice(const uword slice_num)
  1437. {
  1438. arma_extra_debug_sigprint();
  1439. arma_debug_check( slice_num >= n_slices, "Cube::shed_slice(): index out of bounds");
  1440. shed_slices(slice_num, slice_num);
  1441. }
  1442. //! remove specified rows
  1443. template<typename eT>
  1444. inline
  1445. void
  1446. Cube<eT>::shed_rows(const uword in_row1, const uword in_row2)
  1447. {
  1448. arma_extra_debug_sigprint();
  1449. arma_debug_check
  1450. (
  1451. (in_row1 > in_row2) || (in_row2 >= n_rows),
  1452. "Cube::shed_rows(): indices out of bounds or incorrectly used"
  1453. );
  1454. const uword n_keep_front = in_row1;
  1455. const uword n_keep_back = n_rows - (in_row2 + 1);
  1456. Cube<eT> X(n_keep_front + n_keep_back, n_cols, n_slices);
  1457. if(n_keep_front > 0)
  1458. {
  1459. X.rows( 0, (n_keep_front-1) ) = rows( 0, (in_row1-1) );
  1460. }
  1461. if(n_keep_back > 0)
  1462. {
  1463. X.rows( n_keep_front, (n_keep_front+n_keep_back-1) ) = rows( (in_row2+1), (n_rows-1) );
  1464. }
  1465. steal_mem(X);
  1466. }
  1467. //! remove specified columns
  1468. template<typename eT>
  1469. inline
  1470. void
  1471. Cube<eT>::shed_cols(const uword in_col1, const uword in_col2)
  1472. {
  1473. arma_extra_debug_sigprint();
  1474. arma_debug_check
  1475. (
  1476. (in_col1 > in_col2) || (in_col2 >= n_cols),
  1477. "Cube::shed_cols(): indices out of bounds or incorrectly used"
  1478. );
  1479. const uword n_keep_front = in_col1;
  1480. const uword n_keep_back = n_cols - (in_col2 + 1);
  1481. Cube<eT> X(n_rows, n_keep_front + n_keep_back, n_slices);
  1482. if(n_keep_front > 0)
  1483. {
  1484. X.cols( 0, (n_keep_front-1) ) = cols( 0, (in_col1-1) );
  1485. }
  1486. if(n_keep_back > 0)
  1487. {
  1488. X.cols( n_keep_front, (n_keep_front+n_keep_back-1) ) = cols( (in_col2+1), (n_cols-1) );
  1489. }
  1490. steal_mem(X);
  1491. }
  1492. //! remove specified slices
  1493. template<typename eT>
  1494. inline
  1495. void
  1496. Cube<eT>::shed_slices(const uword in_slice1, const uword in_slice2)
  1497. {
  1498. arma_extra_debug_sigprint();
  1499. arma_debug_check
  1500. (
  1501. (in_slice1 > in_slice2) || (in_slice2 >= n_slices),
  1502. "Cube::shed_slices(): indices out of bounds or incorrectly used"
  1503. );
  1504. const uword n_keep_front = in_slice1;
  1505. const uword n_keep_back = n_slices - (in_slice2 + 1);
  1506. Cube<eT> X(n_rows, n_cols, n_keep_front + n_keep_back);
  1507. if(n_keep_front > 0)
  1508. {
  1509. X.slices( 0, (n_keep_front-1) ) = slices( 0, (in_slice1-1) );
  1510. }
  1511. if(n_keep_back > 0)
  1512. {
  1513. X.slices( n_keep_front, (n_keep_front+n_keep_back-1) ) = slices( (in_slice2+1), (n_slices-1) );
  1514. }
  1515. steal_mem(X);
  1516. }
  1517. //! remove specified slices
  1518. template<typename eT>
  1519. template<typename T1>
  1520. inline
  1521. void
  1522. Cube<eT>::shed_slices(const Base<uword, T1>& indices)
  1523. {
  1524. arma_extra_debug_sigprint();
  1525. const quasi_unwrap<T1> U(indices.get_ref());
  1526. const Mat<uword>& tmp1 = U.M;
  1527. arma_debug_check( ((tmp1.is_vec() == false) && (tmp1.is_empty() == false)), "Cube::shed_slices(): list of indices must be a vector" );
  1528. if(tmp1.is_empty()) { return; }
  1529. const Col<uword> tmp2(const_cast<uword*>(tmp1.memptr()), tmp1.n_elem, false, false);
  1530. const Col<uword>& slices_to_shed = (tmp2.is_sorted("strictascend") == false)
  1531. ? Col<uword>(unique(tmp2))
  1532. : Col<uword>(const_cast<uword*>(tmp2.memptr()), tmp2.n_elem, false, false);
  1533. const uword* slices_to_shed_mem = slices_to_shed.memptr();
  1534. const uword N = slices_to_shed.n_elem;
  1535. if(arma_config::debug)
  1536. {
  1537. for(uword i=0; i<N; ++i)
  1538. {
  1539. arma_debug_check( (slices_to_shed_mem[i] >= n_slices), "Cube::shed_slices(): indices out of bounds" );
  1540. }
  1541. }
  1542. Col<uword> tmp3(n_slices);
  1543. uword* tmp3_mem = tmp3.memptr();
  1544. uword i = 0;
  1545. uword count = 0;
  1546. for(uword j=0; j < n_slices; ++j)
  1547. {
  1548. if(i < N)
  1549. {
  1550. if( j != slices_to_shed_mem[i] )
  1551. {
  1552. tmp3_mem[count] = j;
  1553. ++count;
  1554. }
  1555. else
  1556. {
  1557. ++i;
  1558. }
  1559. }
  1560. else
  1561. {
  1562. tmp3_mem[count] = j;
  1563. ++count;
  1564. }
  1565. }
  1566. const Col<uword> slices_to_keep(tmp3.memptr(), count, false, false);
  1567. Cube<eT> X = (*this).slices(slices_to_keep);
  1568. steal_mem(X);
  1569. }
  1570. template<typename eT>
  1571. inline
  1572. void
  1573. Cube<eT>::insert_rows(const uword row_num, const uword N, const bool set_to_zero)
  1574. {
  1575. arma_extra_debug_sigprint();
  1576. const uword t_n_rows = n_rows;
  1577. const uword A_n_rows = row_num;
  1578. const uword B_n_rows = t_n_rows - row_num;
  1579. // insertion at row_num == n_rows is in effect an append operation
  1580. arma_debug_check( (row_num > t_n_rows), "Cube::insert_rows(): index out of bounds");
  1581. if(N > 0)
  1582. {
  1583. Cube<eT> out(t_n_rows + N, n_cols, n_slices);
  1584. if(A_n_rows > 0)
  1585. {
  1586. out.rows(0, A_n_rows-1) = rows(0, A_n_rows-1);
  1587. }
  1588. if(B_n_rows > 0)
  1589. {
  1590. out.rows(row_num + N, t_n_rows + N - 1) = rows(row_num, t_n_rows-1);
  1591. }
  1592. if(set_to_zero == true)
  1593. {
  1594. out.rows(row_num, row_num + N - 1).zeros();
  1595. }
  1596. steal_mem(out);
  1597. }
  1598. }
  1599. template<typename eT>
  1600. inline
  1601. void
  1602. Cube<eT>::insert_cols(const uword col_num, const uword N, const bool set_to_zero)
  1603. {
  1604. arma_extra_debug_sigprint();
  1605. const uword t_n_cols = n_cols;
  1606. const uword A_n_cols = col_num;
  1607. const uword B_n_cols = t_n_cols - col_num;
  1608. // insertion at col_num == n_cols is in effect an append operation
  1609. arma_debug_check( (col_num > t_n_cols), "Cube::insert_cols(): index out of bounds");
  1610. if(N > 0)
  1611. {
  1612. Cube<eT> out(n_rows, t_n_cols + N, n_slices);
  1613. if(A_n_cols > 0)
  1614. {
  1615. out.cols(0, A_n_cols-1) = cols(0, A_n_cols-1);
  1616. }
  1617. if(B_n_cols > 0)
  1618. {
  1619. out.cols(col_num + N, t_n_cols + N - 1) = cols(col_num, t_n_cols-1);
  1620. }
  1621. if(set_to_zero == true)
  1622. {
  1623. out.cols(col_num, col_num + N - 1).zeros();
  1624. }
  1625. steal_mem(out);
  1626. }
  1627. }
  1628. //! insert N slices at the specified slice position,
  1629. //! optionally setting the elements of the inserted slices to zero
  1630. template<typename eT>
  1631. inline
  1632. void
  1633. Cube<eT>::insert_slices(const uword slice_num, const uword N, const bool set_to_zero)
  1634. {
  1635. arma_extra_debug_sigprint();
  1636. const uword t_n_slices = n_slices;
  1637. const uword A_n_slices = slice_num;
  1638. const uword B_n_slices = t_n_slices - slice_num;
  1639. // insertion at slice_num == n_slices is in effect an append operation
  1640. arma_debug_check( (slice_num > t_n_slices), "Cube::insert_slices(): index out of bounds");
  1641. if(N > 0)
  1642. {
  1643. Cube<eT> out(n_rows, n_cols, t_n_slices + N);
  1644. if(A_n_slices > 0)
  1645. {
  1646. out.slices(0, A_n_slices-1) = slices(0, A_n_slices-1);
  1647. }
  1648. if(B_n_slices > 0)
  1649. {
  1650. out.slices(slice_num + N, t_n_slices + N - 1) = slices(slice_num, t_n_slices-1);
  1651. }
  1652. if(set_to_zero == true)
  1653. {
  1654. //out.slices(slice_num, slice_num + N - 1).zeros();
  1655. for(uword i=slice_num; i < (slice_num + N); ++i)
  1656. {
  1657. arrayops::fill_zeros(out.slice_memptr(i), out.n_elem_slice);
  1658. }
  1659. }
  1660. steal_mem(out);
  1661. }
  1662. }
  1663. template<typename eT>
  1664. template<typename T1>
  1665. inline
  1666. void
  1667. Cube<eT>::insert_rows(const uword row_num, const BaseCube<eT,T1>& X)
  1668. {
  1669. arma_extra_debug_sigprint();
  1670. const unwrap_cube<T1> tmp(X.get_ref());
  1671. const Cube<eT>& C = tmp.M;
  1672. const uword N = C.n_rows;
  1673. const uword t_n_rows = n_rows;
  1674. const uword A_n_rows = row_num;
  1675. const uword B_n_rows = t_n_rows - row_num;
  1676. // insertion at row_num == n_rows is in effect an append operation
  1677. arma_debug_check( (row_num > t_n_rows), "Cube::insert_rows(): index out of bounds");
  1678. arma_debug_check
  1679. (
  1680. ( (C.n_cols != n_cols) || (C.n_slices != n_slices) ),
  1681. "Cube::insert_rows(): given object has incompatible dimensions"
  1682. );
  1683. if(N > 0)
  1684. {
  1685. Cube<eT> out(t_n_rows + N, n_cols, n_slices);
  1686. if(A_n_rows > 0)
  1687. {
  1688. out.rows(0, A_n_rows-1) = rows(0, A_n_rows-1);
  1689. }
  1690. if(B_n_rows > 0)
  1691. {
  1692. out.rows(row_num + N, t_n_rows + N - 1) = rows(row_num, t_n_rows - 1);
  1693. }
  1694. out.rows(row_num, row_num + N - 1) = C;
  1695. steal_mem(out);
  1696. }
  1697. }
  1698. template<typename eT>
  1699. template<typename T1>
  1700. inline
  1701. void
  1702. Cube<eT>::insert_cols(const uword col_num, const BaseCube<eT,T1>& X)
  1703. {
  1704. arma_extra_debug_sigprint();
  1705. const unwrap_cube<T1> tmp(X.get_ref());
  1706. const Cube<eT>& C = tmp.M;
  1707. const uword N = C.n_cols;
  1708. const uword t_n_cols = n_cols;
  1709. const uword A_n_cols = col_num;
  1710. const uword B_n_cols = t_n_cols - col_num;
  1711. // insertion at col_num == n_cols is in effect an append operation
  1712. arma_debug_check( (col_num > t_n_cols), "Cube::insert_cols(): index out of bounds");
  1713. arma_debug_check
  1714. (
  1715. ( (C.n_rows != n_rows) || (C.n_slices != n_slices) ),
  1716. "Cube::insert_cols(): given object has incompatible dimensions"
  1717. );
  1718. if(N > 0)
  1719. {
  1720. Cube<eT> out(n_rows, t_n_cols + N, n_slices);
  1721. if(A_n_cols > 0)
  1722. {
  1723. out.cols(0, A_n_cols-1) = cols(0, A_n_cols-1);
  1724. }
  1725. if(B_n_cols > 0)
  1726. {
  1727. out.cols(col_num + N, t_n_cols + N - 1) = cols(col_num, t_n_cols - 1);
  1728. }
  1729. out.cols(col_num, col_num + N - 1) = C;
  1730. steal_mem(out);
  1731. }
  1732. }
  1733. //! insert the given object at the specified slice position;
  1734. //! the given object must have the same number of rows and columns as the cube
  1735. template<typename eT>
  1736. template<typename T1>
  1737. inline
  1738. void
  1739. Cube<eT>::insert_slices(const uword slice_num, const BaseCube<eT,T1>& X)
  1740. {
  1741. arma_extra_debug_sigprint();
  1742. const unwrap_cube<T1> tmp(X.get_ref());
  1743. const Cube<eT>& C = tmp.M;
  1744. const uword N = C.n_slices;
  1745. const uword t_n_slices = n_slices;
  1746. const uword A_n_slices = slice_num;
  1747. const uword B_n_slices = t_n_slices - slice_num;
  1748. // insertion at slice_num == n_slices is in effect an append operation
  1749. arma_debug_check( (slice_num > t_n_slices), "Cube::insert_slices(): index out of bounds");
  1750. arma_debug_check
  1751. (
  1752. ( (C.n_rows != n_rows) || (C.n_cols != n_cols) ),
  1753. "Cube::insert_slices(): given object has incompatible dimensions"
  1754. );
  1755. if(N > 0)
  1756. {
  1757. Cube<eT> out(n_rows, n_cols, t_n_slices + N);
  1758. if(A_n_slices > 0)
  1759. {
  1760. out.slices(0, A_n_slices-1) = slices(0, A_n_slices-1);
  1761. }
  1762. if(B_n_slices > 0)
  1763. {
  1764. out.slices(slice_num + N, t_n_slices + N - 1) = slices(slice_num, t_n_slices - 1);
  1765. }
  1766. out.slices(slice_num, slice_num + N - 1) = C;
  1767. steal_mem(out);
  1768. }
  1769. }
  1770. //! create a cube from GenCube, i.e. run the previously delayed element generation operations
  1771. template<typename eT>
  1772. template<typename gen_type>
  1773. inline
  1774. Cube<eT>::Cube(const GenCube<eT, gen_type>& X)
  1775. : n_rows(X.n_rows)
  1776. , n_cols(X.n_cols)
  1777. , n_elem_slice(X.n_rows*X.n_cols)
  1778. , n_slices(X.n_slices)
  1779. , n_elem(X.n_rows*X.n_cols*X.n_slices)
  1780. , mem_state(0)
  1781. , mem()
  1782. , mat_ptrs(0)
  1783. {
  1784. arma_extra_debug_sigprint_this(this);
  1785. init_cold();
  1786. X.apply(*this);
  1787. }
  1788. template<typename eT>
  1789. template<typename gen_type>
  1790. inline
  1791. Cube<eT>&
  1792. Cube<eT>::operator=(const GenCube<eT, gen_type>& X)
  1793. {
  1794. arma_extra_debug_sigprint();
  1795. init_warm(X.n_rows, X.n_cols, X.n_slices);
  1796. X.apply(*this);
  1797. return *this;
  1798. }
  1799. template<typename eT>
  1800. template<typename gen_type>
  1801. inline
  1802. Cube<eT>&
  1803. Cube<eT>::operator+=(const GenCube<eT, gen_type>& X)
  1804. {
  1805. arma_extra_debug_sigprint();
  1806. X.apply_inplace_plus(*this);
  1807. return *this;
  1808. }
  1809. template<typename eT>
  1810. template<typename gen_type>
  1811. inline
  1812. Cube<eT>&
  1813. Cube<eT>::operator-=(const GenCube<eT, gen_type>& X)
  1814. {
  1815. arma_extra_debug_sigprint();
  1816. X.apply_inplace_minus(*this);
  1817. return *this;
  1818. }
  1819. template<typename eT>
  1820. template<typename gen_type>
  1821. inline
  1822. Cube<eT>&
  1823. Cube<eT>::operator%=(const GenCube<eT, gen_type>& X)
  1824. {
  1825. arma_extra_debug_sigprint();
  1826. X.apply_inplace_schur(*this);
  1827. return *this;
  1828. }
  1829. template<typename eT>
  1830. template<typename gen_type>
  1831. inline
  1832. Cube<eT>&
  1833. Cube<eT>::operator/=(const GenCube<eT, gen_type>& X)
  1834. {
  1835. arma_extra_debug_sigprint();
  1836. X.apply_inplace_div(*this);
  1837. return *this;
  1838. }
  1839. //! create a cube from OpCube, i.e. run the previously delayed unary operations
  1840. template<typename eT>
  1841. template<typename T1, typename op_type>
  1842. inline
  1843. Cube<eT>::Cube(const OpCube<T1, op_type>& X)
  1844. : n_rows(0)
  1845. , n_cols(0)
  1846. , n_elem_slice(0)
  1847. , n_slices(0)
  1848. , n_elem(0)
  1849. , mem_state(0)
  1850. , mem()
  1851. , mat_ptrs(0)
  1852. {
  1853. arma_extra_debug_sigprint_this(this);
  1854. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1855. op_type::apply(*this, X);
  1856. }
  1857. //! create a cube from OpCube, i.e. run the previously delayed unary operations
  1858. template<typename eT>
  1859. template<typename T1, typename op_type>
  1860. inline
  1861. Cube<eT>&
  1862. Cube<eT>::operator=(const OpCube<T1, op_type>& X)
  1863. {
  1864. arma_extra_debug_sigprint();
  1865. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1866. op_type::apply(*this, X);
  1867. return *this;
  1868. }
  1869. //! in-place cube addition, with the right-hand-side operand having delayed operations
  1870. template<typename eT>
  1871. template<typename T1, typename op_type>
  1872. inline
  1873. Cube<eT>&
  1874. Cube<eT>::operator+=(const OpCube<T1, op_type>& X)
  1875. {
  1876. arma_extra_debug_sigprint();
  1877. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1878. const Cube<eT> m(X);
  1879. return (*this).operator+=(m);
  1880. }
  1881. //! in-place cube subtraction, with the right-hand-side operand having delayed operations
  1882. template<typename eT>
  1883. template<typename T1, typename op_type>
  1884. inline
  1885. Cube<eT>&
  1886. Cube<eT>::operator-=(const OpCube<T1, op_type>& X)
  1887. {
  1888. arma_extra_debug_sigprint();
  1889. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1890. const Cube<eT> m(X);
  1891. return (*this).operator-=(m);
  1892. }
  1893. //! in-place cube element-wise multiplication, with the right-hand-side operand having delayed operations
  1894. template<typename eT>
  1895. template<typename T1, typename op_type>
  1896. inline
  1897. Cube<eT>&
  1898. Cube<eT>::operator%=(const OpCube<T1, op_type>& X)
  1899. {
  1900. arma_extra_debug_sigprint();
  1901. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1902. const Cube<eT> m(X);
  1903. return (*this).operator%=(m);
  1904. }
  1905. //! in-place cube element-wise division, with the right-hand-side operand having delayed operations
  1906. template<typename eT>
  1907. template<typename T1, typename op_type>
  1908. inline
  1909. Cube<eT>&
  1910. Cube<eT>::operator/=(const OpCube<T1, op_type>& X)
  1911. {
  1912. arma_extra_debug_sigprint();
  1913. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1914. const Cube<eT> m(X);
  1915. return (*this).operator/=(m);
  1916. }
  1917. //! create a cube from eOpCube, i.e. run the previously delayed unary operations
  1918. template<typename eT>
  1919. template<typename T1, typename eop_type>
  1920. inline
  1921. Cube<eT>::Cube(const eOpCube<T1, eop_type>& X)
  1922. : n_rows(X.get_n_rows())
  1923. , n_cols(X.get_n_cols())
  1924. , n_elem_slice(X.get_n_elem_slice())
  1925. , n_slices(X.get_n_slices())
  1926. , n_elem(X.get_n_elem())
  1927. , mem_state(0)
  1928. , mem()
  1929. , mat_ptrs(0)
  1930. {
  1931. arma_extra_debug_sigprint_this(this);
  1932. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1933. init_cold();
  1934. eop_type::apply(*this, X);
  1935. }
  1936. //! create a cube from eOpCube, i.e. run the previously delayed unary operations
  1937. template<typename eT>
  1938. template<typename T1, typename eop_type>
  1939. inline
  1940. Cube<eT>&
  1941. Cube<eT>::operator=(const eOpCube<T1, eop_type>& X)
  1942. {
  1943. arma_extra_debug_sigprint();
  1944. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1945. const bool bad_alias = ( X.P.has_subview && X.P.is_alias(*this) );
  1946. if(bad_alias == false)
  1947. {
  1948. init_warm(X.get_n_rows(), X.get_n_cols(), X.get_n_slices());
  1949. eop_type::apply(*this, X);
  1950. }
  1951. else
  1952. {
  1953. Cube<eT> tmp(X);
  1954. steal_mem(tmp);
  1955. }
  1956. return *this;
  1957. }
  1958. //! in-place cube addition, with the right-hand-side operand having delayed operations
  1959. template<typename eT>
  1960. template<typename T1, typename eop_type>
  1961. inline
  1962. Cube<eT>&
  1963. Cube<eT>::operator+=(const eOpCube<T1, eop_type>& X)
  1964. {
  1965. arma_extra_debug_sigprint();
  1966. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1967. eop_type::apply_inplace_plus(*this, X);
  1968. return *this;
  1969. }
  1970. //! in-place cube subtraction, with the right-hand-side operand having delayed operations
  1971. template<typename eT>
  1972. template<typename T1, typename eop_type>
  1973. inline
  1974. Cube<eT>&
  1975. Cube<eT>::operator-=(const eOpCube<T1, eop_type>& X)
  1976. {
  1977. arma_extra_debug_sigprint();
  1978. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1979. eop_type::apply_inplace_minus(*this, X);
  1980. return *this;
  1981. }
  1982. //! in-place cube element-wise multiplication, with the right-hand-side operand having delayed operations
  1983. template<typename eT>
  1984. template<typename T1, typename eop_type>
  1985. inline
  1986. Cube<eT>&
  1987. Cube<eT>::operator%=(const eOpCube<T1, eop_type>& X)
  1988. {
  1989. arma_extra_debug_sigprint();
  1990. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  1991. eop_type::apply_inplace_schur(*this, X);
  1992. return *this;
  1993. }
  1994. //! in-place cube element-wise division, with the right-hand-side operand having delayed operations
  1995. template<typename eT>
  1996. template<typename T1, typename eop_type>
  1997. inline
  1998. Cube<eT>&
  1999. Cube<eT>::operator/=(const eOpCube<T1, eop_type>& X)
  2000. {
  2001. arma_extra_debug_sigprint();
  2002. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2003. eop_type::apply_inplace_div(*this, X);
  2004. return *this;
  2005. }
  2006. template<typename eT>
  2007. template<typename T1, typename op_type>
  2008. inline
  2009. Cube<eT>::Cube(const mtOpCube<eT, T1, op_type>& X)
  2010. : n_rows(0)
  2011. , n_cols(0)
  2012. , n_elem_slice(0)
  2013. , n_slices(0)
  2014. , n_elem(0)
  2015. , mem_state(0)
  2016. , mem()
  2017. , mat_ptrs(0)
  2018. {
  2019. arma_extra_debug_sigprint_this(this);
  2020. op_type::apply(*this, X);
  2021. }
  2022. template<typename eT>
  2023. template<typename T1, typename op_type>
  2024. inline
  2025. Cube<eT>&
  2026. Cube<eT>::operator=(const mtOpCube<eT, T1, op_type>& X)
  2027. {
  2028. arma_extra_debug_sigprint();
  2029. op_type::apply(*this, X);
  2030. return *this;
  2031. }
  2032. template<typename eT>
  2033. template<typename T1, typename op_type>
  2034. inline
  2035. Cube<eT>&
  2036. Cube<eT>::operator+=(const mtOpCube<eT, T1, op_type>& X)
  2037. {
  2038. arma_extra_debug_sigprint();
  2039. const Cube<eT> m(X);
  2040. return (*this).operator+=(m);
  2041. }
  2042. template<typename eT>
  2043. template<typename T1, typename op_type>
  2044. inline
  2045. Cube<eT>&
  2046. Cube<eT>::operator-=(const mtOpCube<eT, T1, op_type>& X)
  2047. {
  2048. arma_extra_debug_sigprint();
  2049. const Cube<eT> m(X);
  2050. return (*this).operator-=(m);
  2051. }
  2052. template<typename eT>
  2053. template<typename T1, typename op_type>
  2054. inline
  2055. Cube<eT>&
  2056. Cube<eT>::operator%=(const mtOpCube<eT, T1, op_type>& X)
  2057. {
  2058. arma_extra_debug_sigprint();
  2059. const Cube<eT> m(X);
  2060. return (*this).operator%=(m);
  2061. }
  2062. template<typename eT>
  2063. template<typename T1, typename op_type>
  2064. inline
  2065. Cube<eT>&
  2066. Cube<eT>::operator/=(const mtOpCube<eT, T1, op_type>& X)
  2067. {
  2068. arma_extra_debug_sigprint();
  2069. const Cube<eT> m(X);
  2070. return (*this).operator/=(m);
  2071. }
  2072. //! create a cube from GlueCube, i.e. run the previously delayed binary operations
  2073. template<typename eT>
  2074. template<typename T1, typename T2, typename glue_type>
  2075. inline
  2076. Cube<eT>::Cube(const GlueCube<T1, T2, glue_type>& X)
  2077. : n_rows(0)
  2078. , n_cols(0)
  2079. , n_elem_slice(0)
  2080. , n_slices(0)
  2081. , n_elem(0)
  2082. , mem_state(0)
  2083. , mem()
  2084. , mat_ptrs(0)
  2085. {
  2086. arma_extra_debug_sigprint_this(this);
  2087. this->operator=(X);
  2088. }
  2089. //! create a cube from GlueCube, i.e. run the previously delayed binary operations
  2090. template<typename eT>
  2091. template<typename T1, typename T2, typename glue_type>
  2092. inline
  2093. Cube<eT>&
  2094. Cube<eT>::operator=(const GlueCube<T1, T2, glue_type>& X)
  2095. {
  2096. arma_extra_debug_sigprint();
  2097. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2098. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2099. glue_type::apply(*this, X);
  2100. return *this;
  2101. }
  2102. //! in-place cube addition, with the right-hand-side operands having delayed operations
  2103. template<typename eT>
  2104. template<typename T1, typename T2, typename glue_type>
  2105. inline
  2106. Cube<eT>&
  2107. Cube<eT>::operator+=(const GlueCube<T1, T2, glue_type>& X)
  2108. {
  2109. arma_extra_debug_sigprint();
  2110. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2111. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2112. const Cube<eT> m(X);
  2113. return (*this).operator+=(m);
  2114. }
  2115. //! in-place cube subtraction, with the right-hand-side operands having delayed operations
  2116. template<typename eT>
  2117. template<typename T1, typename T2, typename glue_type>
  2118. inline
  2119. Cube<eT>&
  2120. Cube<eT>::operator-=(const GlueCube<T1, T2, glue_type>& X)
  2121. {
  2122. arma_extra_debug_sigprint();
  2123. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2124. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2125. const Cube<eT> m(X);
  2126. return (*this).operator-=(m);
  2127. }
  2128. //! in-place cube element-wise multiplication, with the right-hand-side operands having delayed operations
  2129. template<typename eT>
  2130. template<typename T1, typename T2, typename glue_type>
  2131. inline
  2132. Cube<eT>&
  2133. Cube<eT>::operator%=(const GlueCube<T1, T2, glue_type>& X)
  2134. {
  2135. arma_extra_debug_sigprint();
  2136. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2137. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2138. const Cube<eT> m(X);
  2139. return (*this).operator%=(m);
  2140. }
  2141. //! in-place cube element-wise division, with the right-hand-side operands having delayed operations
  2142. template<typename eT>
  2143. template<typename T1, typename T2, typename glue_type>
  2144. inline
  2145. Cube<eT>&
  2146. Cube<eT>::operator/=(const GlueCube<T1, T2, glue_type>& X)
  2147. {
  2148. arma_extra_debug_sigprint();
  2149. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2150. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2151. const Cube<eT> m(X);
  2152. return (*this).operator/=(m);
  2153. }
  2154. //! create a cube from eGlueCube, i.e. run the previously delayed binary operations
  2155. template<typename eT>
  2156. template<typename T1, typename T2, typename eglue_type>
  2157. inline
  2158. Cube<eT>::Cube(const eGlueCube<T1, T2, eglue_type>& X)
  2159. : n_rows(X.get_n_rows())
  2160. , n_cols(X.get_n_cols())
  2161. , n_elem_slice(X.get_n_elem_slice())
  2162. , n_slices(X.get_n_slices())
  2163. , n_elem(X.get_n_elem())
  2164. , mem_state(0)
  2165. , mem()
  2166. , mat_ptrs(0)
  2167. {
  2168. arma_extra_debug_sigprint_this(this);
  2169. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2170. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2171. init_cold();
  2172. eglue_type::apply(*this, X);
  2173. }
  2174. //! create a cube from eGlueCube, i.e. run the previously delayed binary operations
  2175. template<typename eT>
  2176. template<typename T1, typename T2, typename eglue_type>
  2177. inline
  2178. Cube<eT>&
  2179. Cube<eT>::operator=(const eGlueCube<T1, T2, eglue_type>& X)
  2180. {
  2181. arma_extra_debug_sigprint();
  2182. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2183. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2184. const bool bad_alias = ( (X.P1.has_subview && X.P1.is_alias(*this)) || (X.P2.has_subview && X.P2.is_alias(*this)) );
  2185. if(bad_alias == false)
  2186. {
  2187. init_warm(X.get_n_rows(), X.get_n_cols(), X.get_n_slices());
  2188. eglue_type::apply(*this, X);
  2189. }
  2190. else
  2191. {
  2192. Cube<eT> tmp(X);
  2193. steal_mem(tmp);
  2194. }
  2195. return *this;
  2196. }
  2197. //! in-place cube addition, with the right-hand-side operands having delayed operations
  2198. template<typename eT>
  2199. template<typename T1, typename T2, typename eglue_type>
  2200. inline
  2201. Cube<eT>&
  2202. Cube<eT>::operator+=(const eGlueCube<T1, T2, eglue_type>& X)
  2203. {
  2204. arma_extra_debug_sigprint();
  2205. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2206. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2207. eglue_type::apply_inplace_plus(*this, X);
  2208. return *this;
  2209. }
  2210. //! in-place cube subtraction, with the right-hand-side operands having delayed operations
  2211. template<typename eT>
  2212. template<typename T1, typename T2, typename eglue_type>
  2213. inline
  2214. Cube<eT>&
  2215. Cube<eT>::operator-=(const eGlueCube<T1, T2, eglue_type>& X)
  2216. {
  2217. arma_extra_debug_sigprint();
  2218. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2219. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2220. eglue_type::apply_inplace_minus(*this, X);
  2221. return *this;
  2222. }
  2223. //! in-place cube element-wise multiplication, with the right-hand-side operands having delayed operations
  2224. template<typename eT>
  2225. template<typename T1, typename T2, typename eglue_type>
  2226. inline
  2227. Cube<eT>&
  2228. Cube<eT>::operator%=(const eGlueCube<T1, T2, eglue_type>& X)
  2229. {
  2230. arma_extra_debug_sigprint();
  2231. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2232. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2233. eglue_type::apply_inplace_schur(*this, X);
  2234. return *this;
  2235. }
  2236. //! in-place cube element-wise division, with the right-hand-side operands having delayed operations
  2237. template<typename eT>
  2238. template<typename T1, typename T2, typename eglue_type>
  2239. inline
  2240. Cube<eT>&
  2241. Cube<eT>::operator/=(const eGlueCube<T1, T2, eglue_type>& X)
  2242. {
  2243. arma_extra_debug_sigprint();
  2244. arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));
  2245. arma_type_check(( is_same_type< eT, typename T2::elem_type >::no ));
  2246. eglue_type::apply_inplace_div(*this, X);
  2247. return *this;
  2248. }
  2249. template<typename eT>
  2250. template<typename T1, typename T2, typename glue_type>
  2251. inline
  2252. Cube<eT>::Cube(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2253. : n_rows(0)
  2254. , n_cols(0)
  2255. , n_elem_slice(0)
  2256. , n_slices(0)
  2257. , n_elem(0)
  2258. , mem_state(0)
  2259. , mem()
  2260. , mat_ptrs(0)
  2261. {
  2262. arma_extra_debug_sigprint_this(this);
  2263. glue_type::apply(*this, X);
  2264. }
  2265. template<typename eT>
  2266. template<typename T1, typename T2, typename glue_type>
  2267. inline
  2268. Cube<eT>&
  2269. Cube<eT>::operator=(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2270. {
  2271. arma_extra_debug_sigprint();
  2272. glue_type::apply(*this, X);
  2273. return *this;
  2274. }
  2275. template<typename eT>
  2276. template<typename T1, typename T2, typename glue_type>
  2277. inline
  2278. Cube<eT>&
  2279. Cube<eT>::operator+=(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2280. {
  2281. arma_extra_debug_sigprint();
  2282. const Cube<eT> m(X);
  2283. return (*this).operator+=(m);
  2284. }
  2285. template<typename eT>
  2286. template<typename T1, typename T2, typename glue_type>
  2287. inline
  2288. Cube<eT>&
  2289. Cube<eT>::operator-=(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2290. {
  2291. arma_extra_debug_sigprint();
  2292. const Cube<eT> m(X);
  2293. return (*this).operator-=(m);
  2294. }
  2295. template<typename eT>
  2296. template<typename T1, typename T2, typename glue_type>
  2297. inline
  2298. Cube<eT>&
  2299. Cube<eT>::operator%=(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2300. {
  2301. arma_extra_debug_sigprint();
  2302. const Cube<eT> m(X);
  2303. return (*this).operator%=(m);
  2304. }
  2305. template<typename eT>
  2306. template<typename T1, typename T2, typename glue_type>
  2307. inline
  2308. Cube<eT>&
  2309. Cube<eT>::operator/=(const mtGlueCube<eT, T1, T2, glue_type>& X)
  2310. {
  2311. arma_extra_debug_sigprint();
  2312. const Cube<eT> m(X);
  2313. return (*this).operator/=(m);
  2314. }
  2315. //! linear element accessor (treats the cube as a vector); no bounds check; assumes memory is aligned
  2316. template<typename eT>
  2317. arma_inline
  2318. arma_warn_unused
  2319. const eT&
  2320. Cube<eT>::at_alt(const uword i) const
  2321. {
  2322. const eT* mem_aligned = mem;
  2323. memory::mark_as_aligned(mem_aligned);
  2324. return mem_aligned[i];
  2325. }
  2326. //! linear element accessor (treats the cube as a vector); bounds checking not done when ARMA_NO_DEBUG is defined
  2327. template<typename eT>
  2328. arma_inline
  2329. arma_warn_unused
  2330. eT&
  2331. Cube<eT>::operator() (const uword i)
  2332. {
  2333. arma_debug_check( (i >= n_elem), "Cube::operator(): index out of bounds");
  2334. return access::rw(mem[i]);
  2335. }
  2336. //! linear element accessor (treats the cube as a vector); bounds checking not done when ARMA_NO_DEBUG is defined
  2337. template<typename eT>
  2338. arma_inline
  2339. arma_warn_unused
  2340. const eT&
  2341. Cube<eT>::operator() (const uword i) const
  2342. {
  2343. arma_debug_check( (i >= n_elem), "Cube::operator(): index out of bounds");
  2344. return mem[i];
  2345. }
  2346. //! linear element accessor (treats the cube as a vector); no bounds check.
  2347. template<typename eT>
  2348. arma_inline
  2349. arma_warn_unused
  2350. eT&
  2351. Cube<eT>::operator[] (const uword i)
  2352. {
  2353. return access::rw(mem[i]);
  2354. }
  2355. //! linear element accessor (treats the cube as a vector); no bounds check
  2356. template<typename eT>
  2357. arma_inline
  2358. arma_warn_unused
  2359. const eT&
  2360. Cube<eT>::operator[] (const uword i) const
  2361. {
  2362. return mem[i];
  2363. }
  2364. //! linear element accessor (treats the cube as a vector); no bounds check.
  2365. template<typename eT>
  2366. arma_inline
  2367. arma_warn_unused
  2368. eT&
  2369. Cube<eT>::at(const uword i)
  2370. {
  2371. return access::rw(mem[i]);
  2372. }
  2373. //! linear element accessor (treats the cube as a vector); no bounds check
  2374. template<typename eT>
  2375. arma_inline
  2376. arma_warn_unused
  2377. const eT&
  2378. Cube<eT>::at(const uword i) const
  2379. {
  2380. return mem[i];
  2381. }
  2382. //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined
  2383. template<typename eT>
  2384. arma_inline
  2385. arma_warn_unused
  2386. eT&
  2387. Cube<eT>::operator() (const uword in_row, const uword in_col, const uword in_slice)
  2388. {
  2389. arma_debug_check
  2390. (
  2391. (in_row >= n_rows) ||
  2392. (in_col >= n_cols) ||
  2393. (in_slice >= n_slices)
  2394. ,
  2395. "Cube::operator(): index out of bounds"
  2396. );
  2397. return access::rw(mem[in_slice*n_elem_slice + in_col*n_rows + in_row]);
  2398. }
  2399. //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined
  2400. template<typename eT>
  2401. arma_inline
  2402. arma_warn_unused
  2403. const eT&
  2404. Cube<eT>::operator() (const uword in_row, const uword in_col, const uword in_slice) const
  2405. {
  2406. arma_debug_check
  2407. (
  2408. (in_row >= n_rows) ||
  2409. (in_col >= n_cols) ||
  2410. (in_slice >= n_slices)
  2411. ,
  2412. "Cube::operator(): index out of bounds"
  2413. );
  2414. return mem[in_slice*n_elem_slice + in_col*n_rows + in_row];
  2415. }
  2416. //! element accessor; no bounds check
  2417. template<typename eT>
  2418. arma_inline
  2419. arma_warn_unused
  2420. eT&
  2421. Cube<eT>::at(const uword in_row, const uword in_col, const uword in_slice)
  2422. {
  2423. return access::rw( mem[in_slice*n_elem_slice + in_col*n_rows + in_row] );
  2424. }
  2425. //! element accessor; no bounds check
  2426. template<typename eT>
  2427. arma_inline
  2428. arma_warn_unused
  2429. const eT&
  2430. Cube<eT>::at(const uword in_row, const uword in_col, const uword in_slice) const
  2431. {
  2432. return mem[in_slice*n_elem_slice + in_col*n_rows + in_row];
  2433. }
  2434. //! prefix ++
  2435. template<typename eT>
  2436. arma_inline
  2437. const Cube<eT>&
  2438. Cube<eT>::operator++()
  2439. {
  2440. Cube_aux::prefix_pp(*this);
  2441. return *this;
  2442. }
  2443. //! postfix ++ (must not return the object by reference)
  2444. template<typename eT>
  2445. arma_inline
  2446. void
  2447. Cube<eT>::operator++(int)
  2448. {
  2449. Cube_aux::postfix_pp(*this);
  2450. }
  2451. //! prefix --
  2452. template<typename eT>
  2453. arma_inline
  2454. const Cube<eT>&
  2455. Cube<eT>::operator--()
  2456. {
  2457. Cube_aux::prefix_mm(*this);
  2458. return *this;
  2459. }
  2460. //! postfix -- (must not return the object by reference)
  2461. template<typename eT>
  2462. arma_inline
  2463. void
  2464. Cube<eT>::operator--(int)
  2465. {
  2466. Cube_aux::postfix_mm(*this);
  2467. }
  2468. //! returns true if all of the elements are finite
  2469. template<typename eT>
  2470. inline
  2471. arma_warn_unused
  2472. bool
  2473. Cube<eT>::is_finite() const
  2474. {
  2475. arma_extra_debug_sigprint();
  2476. return arrayops::is_finite( memptr(), n_elem );
  2477. }
  2478. //! returns true if the cube has no elements
  2479. template<typename eT>
  2480. arma_inline
  2481. arma_warn_unused
  2482. bool
  2483. Cube<eT>::is_empty() const
  2484. {
  2485. return (n_elem == 0);
  2486. }
  2487. template<typename eT>
  2488. inline
  2489. arma_warn_unused
  2490. bool
  2491. Cube<eT>::has_inf() const
  2492. {
  2493. arma_extra_debug_sigprint();
  2494. return arrayops::has_inf( memptr(), n_elem );
  2495. }
  2496. template<typename eT>
  2497. inline
  2498. arma_warn_unused
  2499. bool
  2500. Cube<eT>::has_nan() const
  2501. {
  2502. arma_extra_debug_sigprint();
  2503. return arrayops::has_nan( memptr(), n_elem );
  2504. }
  2505. //! returns true if the given index is currently in range
  2506. template<typename eT>
  2507. arma_inline
  2508. arma_warn_unused
  2509. bool
  2510. Cube<eT>::in_range(const uword i) const
  2511. {
  2512. return (i < n_elem);
  2513. }
  2514. //! returns true if the given start and end indices are currently in range
  2515. template<typename eT>
  2516. arma_inline
  2517. arma_warn_unused
  2518. bool
  2519. Cube<eT>::in_range(const span& x) const
  2520. {
  2521. arma_extra_debug_sigprint();
  2522. if(x.whole == true)
  2523. {
  2524. return true;
  2525. }
  2526. else
  2527. {
  2528. const uword a = x.a;
  2529. const uword b = x.b;
  2530. return ( (a <= b) && (b < n_elem) );
  2531. }
  2532. }
  2533. //! returns true if the given location is currently in range
  2534. template<typename eT>
  2535. arma_inline
  2536. arma_warn_unused
  2537. bool
  2538. Cube<eT>::in_range(const uword in_row, const uword in_col, const uword in_slice) const
  2539. {
  2540. return ( (in_row < n_rows) && (in_col < n_cols) && (in_slice < n_slices) );
  2541. }
  2542. template<typename eT>
  2543. inline
  2544. arma_warn_unused
  2545. bool
  2546. Cube<eT>::in_range(const span& row_span, const span& col_span, const span& slice_span) const
  2547. {
  2548. arma_extra_debug_sigprint();
  2549. const uword in_row1 = row_span.a;
  2550. const uword in_row2 = row_span.b;
  2551. const uword in_col1 = col_span.a;
  2552. const uword in_col2 = col_span.b;
  2553. const uword in_slice1 = slice_span.a;
  2554. const uword in_slice2 = slice_span.b;
  2555. const bool rows_ok = row_span.whole ? true : ( (in_row1 <= in_row2) && (in_row2 < n_rows) );
  2556. const bool cols_ok = col_span.whole ? true : ( (in_col1 <= in_col2) && (in_col2 < n_cols) );
  2557. const bool slices_ok = slice_span.whole ? true : ( (in_slice1 <= in_slice2) && (in_slice2 < n_slices) );
  2558. return ( (rows_ok == true) && (cols_ok == true) && (slices_ok == true) );
  2559. }
  2560. template<typename eT>
  2561. inline
  2562. arma_warn_unused
  2563. bool
  2564. Cube<eT>::in_range(const uword in_row, const uword in_col, const uword in_slice, const SizeCube& s) const
  2565. {
  2566. const uword l_n_rows = n_rows;
  2567. const uword l_n_cols = n_cols;
  2568. const uword l_n_slices = n_slices;
  2569. if(
  2570. ( in_row >= l_n_rows) || ( in_col >= l_n_cols) || ( in_slice >= l_n_slices)
  2571. || ((in_row + s.n_rows) > l_n_rows) || ((in_col + s.n_cols) > l_n_cols) || ((in_slice + s.n_slices) > l_n_slices)
  2572. )
  2573. {
  2574. return false;
  2575. }
  2576. else
  2577. {
  2578. return true;
  2579. }
  2580. }
  2581. //! returns a pointer to array of eTs used by the cube
  2582. template<typename eT>
  2583. arma_inline
  2584. arma_warn_unused
  2585. eT*
  2586. Cube<eT>::memptr()
  2587. {
  2588. return const_cast<eT*>(mem);
  2589. }
  2590. //! returns a pointer to array of eTs used by the cube
  2591. template<typename eT>
  2592. arma_inline
  2593. arma_warn_unused
  2594. const eT*
  2595. Cube<eT>::memptr() const
  2596. {
  2597. return mem;
  2598. }
  2599. //! returns a pointer to array of eTs used by the specified slice in the cube
  2600. template<typename eT>
  2601. arma_inline
  2602. arma_warn_unused
  2603. eT*
  2604. Cube<eT>::slice_memptr(const uword uslice)
  2605. {
  2606. return const_cast<eT*>( &mem[ uslice*n_elem_slice ] );
  2607. }
  2608. //! returns a pointer to array of eTs used by the specified slice in the cube
  2609. template<typename eT>
  2610. arma_inline
  2611. arma_warn_unused
  2612. const eT*
  2613. Cube<eT>::slice_memptr(const uword uslice) const
  2614. {
  2615. return &mem[ uslice*n_elem_slice ];
  2616. }
  2617. //! returns a pointer to array of eTs used by the specified slice in the cube
  2618. template<typename eT>
  2619. arma_inline
  2620. arma_warn_unused
  2621. eT*
  2622. Cube<eT>::slice_colptr(const uword uslice, const uword col)
  2623. {
  2624. return const_cast<eT*>( &mem[ uslice*n_elem_slice + col*n_rows] );
  2625. }
  2626. //! returns a pointer to array of eTs used by the specified slice in the cube
  2627. template<typename eT>
  2628. arma_inline
  2629. arma_warn_unused
  2630. const eT*
  2631. Cube<eT>::slice_colptr(const uword uslice, const uword col) const
  2632. {
  2633. return &mem[ uslice*n_elem_slice + col*n_rows ];
  2634. }
  2635. //! print contents of the cube (to the cout stream),
  2636. //! optionally preceding with a user specified line of text.
  2637. //! the precision and cell width are modified.
  2638. //! on return, the stream's state are restored to their original values.
  2639. template<typename eT>
  2640. arma_cold
  2641. inline
  2642. void
  2643. Cube<eT>::impl_print(const std::string& extra_text) const
  2644. {
  2645. arma_extra_debug_sigprint();
  2646. if(extra_text.length() != 0)
  2647. {
  2648. get_cout_stream() << extra_text << '\n';
  2649. }
  2650. arma_ostream::print(get_cout_stream(), *this, true);
  2651. }
  2652. //! print contents of the cube to a user specified stream,
  2653. //! optionally preceding with a user specified line of text.
  2654. //! the precision and cell width are modified.
  2655. //! on return, the stream's state are restored to their original values.
  2656. template<typename eT>
  2657. arma_cold
  2658. inline
  2659. void
  2660. Cube<eT>::impl_print(std::ostream& user_stream, const std::string& extra_text) const
  2661. {
  2662. arma_extra_debug_sigprint();
  2663. if(extra_text.length() != 0)
  2664. {
  2665. user_stream << extra_text << '\n';
  2666. }
  2667. arma_ostream::print(user_stream, *this, true);
  2668. }
  2669. //! print contents of the cube (to the cout stream),
  2670. //! optionally preceding with a user specified line of text.
  2671. //! the stream's state are used as is and are not modified
  2672. //! (i.e. the precision and cell width are not modified).
  2673. template<typename eT>
  2674. arma_cold
  2675. inline
  2676. void
  2677. Cube<eT>::impl_raw_print(const std::string& extra_text) const
  2678. {
  2679. arma_extra_debug_sigprint();
  2680. if(extra_text.length() != 0)
  2681. {
  2682. get_cout_stream() << extra_text << '\n';
  2683. }
  2684. arma_ostream::print(get_cout_stream(), *this, false);
  2685. }
  2686. //! print contents of the cube to a user specified stream,
  2687. //! optionally preceding with a user specified line of text.
  2688. //! the stream's state are used as is and are not modified.
  2689. //! (i.e. the precision and cell width are not modified).
  2690. template<typename eT>
  2691. arma_cold
  2692. inline
  2693. void
  2694. Cube<eT>::impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const
  2695. {
  2696. arma_extra_debug_sigprint();
  2697. if(extra_text.length() != 0)
  2698. {
  2699. user_stream << extra_text << '\n';
  2700. }
  2701. arma_ostream::print(user_stream, *this, false);
  2702. }
  2703. //! change the cube to have user specified dimensions (data is not preserved)
  2704. template<typename eT>
  2705. inline
  2706. void
  2707. Cube<eT>::set_size(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices)
  2708. {
  2709. arma_extra_debug_sigprint();
  2710. init_warm(in_n_rows, in_n_cols, in_n_slices);
  2711. }
  2712. //! change the cube to have user specified dimensions (data is preserved)
  2713. template<typename eT>
  2714. inline
  2715. void
  2716. Cube<eT>::reshape(const uword in_rows, const uword in_cols, const uword in_slices)
  2717. {
  2718. arma_extra_debug_sigprint();
  2719. *this = arma::reshape(*this, in_rows, in_cols, in_slices);
  2720. }
  2721. //! NOTE: don't use this form; it's deprecated and will be removed
  2722. template<typename eT>
  2723. arma_deprecated
  2724. inline
  2725. void
  2726. Cube<eT>::reshape(const uword in_rows, const uword in_cols, const uword in_slices, const uword dim)
  2727. {
  2728. arma_extra_debug_sigprint();
  2729. *this = arma::reshape(*this, in_rows, in_cols, in_slices, dim);
  2730. }
  2731. //! change the cube to have user specified dimensions (data is preserved)
  2732. template<typename eT>
  2733. inline
  2734. void
  2735. Cube<eT>::resize(const uword in_rows, const uword in_cols, const uword in_slices)
  2736. {
  2737. arma_extra_debug_sigprint();
  2738. *this = arma::resize(*this, in_rows, in_cols, in_slices);
  2739. }
  2740. template<typename eT>
  2741. inline
  2742. void
  2743. Cube<eT>::set_size(const SizeCube& s)
  2744. {
  2745. arma_extra_debug_sigprint();
  2746. init_warm(s.n_rows, s.n_cols, s.n_slices);
  2747. }
  2748. template<typename eT>
  2749. inline
  2750. void
  2751. Cube<eT>::reshape(const SizeCube& s)
  2752. {
  2753. arma_extra_debug_sigprint();
  2754. *this = arma::reshape(*this, s.n_rows, s.n_cols, s.n_slices, 0);
  2755. }
  2756. template<typename eT>
  2757. inline
  2758. void
  2759. Cube<eT>::resize(const SizeCube& s)
  2760. {
  2761. arma_extra_debug_sigprint();
  2762. *this = arma::resize(*this, s.n_rows, s.n_cols, s.n_slices);
  2763. }
  2764. //! change the cube (without preserving data) to have the same dimensions as the given cube
  2765. template<typename eT>
  2766. template<typename eT2>
  2767. inline
  2768. void
  2769. Cube<eT>::copy_size(const Cube<eT2>& m)
  2770. {
  2771. arma_extra_debug_sigprint();
  2772. init_warm(m.n_rows, m.n_cols, m.n_slices);
  2773. }
  2774. //! apply a functor to each element
  2775. template<typename eT>
  2776. template<typename functor>
  2777. inline
  2778. const Cube<eT>&
  2779. Cube<eT>::for_each(functor F)
  2780. {
  2781. arma_extra_debug_sigprint();
  2782. eT* data = memptr();
  2783. const uword N = n_elem;
  2784. uword ii, jj;
  2785. for(ii=0, jj=1; jj < N; ii+=2, jj+=2)
  2786. {
  2787. F(data[ii]);
  2788. F(data[jj]);
  2789. }
  2790. if(ii < N)
  2791. {
  2792. F(data[ii]);
  2793. }
  2794. return *this;
  2795. }
  2796. template<typename eT>
  2797. template<typename functor>
  2798. inline
  2799. const Cube<eT>&
  2800. Cube<eT>::for_each(functor F) const
  2801. {
  2802. arma_extra_debug_sigprint();
  2803. const eT* data = memptr();
  2804. const uword N = n_elem;
  2805. uword ii, jj;
  2806. for(ii=0, jj=1; jj < N; ii+=2, jj+=2)
  2807. {
  2808. F(data[ii]);
  2809. F(data[jj]);
  2810. }
  2811. if(ii < N)
  2812. {
  2813. F(data[ii]);
  2814. }
  2815. return *this;
  2816. }
  2817. //! transform each element in the cube using a functor
  2818. template<typename eT>
  2819. template<typename functor>
  2820. inline
  2821. const Cube<eT>&
  2822. Cube<eT>::transform(functor F)
  2823. {
  2824. arma_extra_debug_sigprint();
  2825. eT* out_mem = memptr();
  2826. const uword N = n_elem;
  2827. uword ii, jj;
  2828. for(ii=0, jj=1; jj < N; ii+=2, jj+=2)
  2829. {
  2830. eT tmp_ii = out_mem[ii];
  2831. eT tmp_jj = out_mem[jj];
  2832. tmp_ii = eT( F(tmp_ii) );
  2833. tmp_jj = eT( F(tmp_jj) );
  2834. out_mem[ii] = tmp_ii;
  2835. out_mem[jj] = tmp_jj;
  2836. }
  2837. if(ii < N)
  2838. {
  2839. out_mem[ii] = eT( F(out_mem[ii]) );
  2840. }
  2841. return *this;
  2842. }
  2843. //! imbue (fill) the cube with values provided by a functor
  2844. template<typename eT>
  2845. template<typename functor>
  2846. inline
  2847. const Cube<eT>&
  2848. Cube<eT>::imbue(functor F)
  2849. {
  2850. arma_extra_debug_sigprint();
  2851. eT* out_mem = memptr();
  2852. const uword N = n_elem;
  2853. uword ii, jj;
  2854. for(ii=0, jj=1; jj < N; ii+=2, jj+=2)
  2855. {
  2856. const eT tmp_ii = eT( F() );
  2857. const eT tmp_jj = eT( F() );
  2858. out_mem[ii] = tmp_ii;
  2859. out_mem[jj] = tmp_jj;
  2860. }
  2861. if(ii < N)
  2862. {
  2863. out_mem[ii] = eT( F() );
  2864. }
  2865. return *this;
  2866. }
  2867. template<typename eT>
  2868. inline
  2869. const Cube<eT>&
  2870. Cube<eT>::replace(const eT old_val, const eT new_val)
  2871. {
  2872. arma_extra_debug_sigprint();
  2873. arrayops::replace(memptr(), n_elem, old_val, new_val);
  2874. return *this;
  2875. }
  2876. template<typename eT>
  2877. inline
  2878. const Cube<eT>&
  2879. Cube<eT>::clean(const typename get_pod_type<eT>::result threshold)
  2880. {
  2881. arma_extra_debug_sigprint();
  2882. arrayops::clean(memptr(), n_elem, threshold);
  2883. return *this;
  2884. }
  2885. //! fill the cube with the specified value
  2886. template<typename eT>
  2887. inline
  2888. const Cube<eT>&
  2889. Cube<eT>::fill(const eT val)
  2890. {
  2891. arma_extra_debug_sigprint();
  2892. arrayops::inplace_set( memptr(), val, n_elem );
  2893. return *this;
  2894. }
  2895. template<typename eT>
  2896. inline
  2897. const Cube<eT>&
  2898. Cube<eT>::zeros()
  2899. {
  2900. arma_extra_debug_sigprint();
  2901. arrayops::fill_zeros(memptr(), n_elem);
  2902. return *this;
  2903. }
  2904. template<typename eT>
  2905. inline
  2906. const Cube<eT>&
  2907. Cube<eT>::zeros(const uword in_rows, const uword in_cols, const uword in_slices)
  2908. {
  2909. arma_extra_debug_sigprint();
  2910. set_size(in_rows, in_cols, in_slices);
  2911. return (*this).zeros();
  2912. }
  2913. template<typename eT>
  2914. inline
  2915. const Cube<eT>&
  2916. Cube<eT>::zeros(const SizeCube& s)
  2917. {
  2918. arma_extra_debug_sigprint();
  2919. return (*this).zeros(s.n_rows, s.n_cols, s.n_slices);
  2920. }
  2921. template<typename eT>
  2922. inline
  2923. const Cube<eT>&
  2924. Cube<eT>::ones()
  2925. {
  2926. arma_extra_debug_sigprint();
  2927. return (*this).fill(eT(1));
  2928. }
  2929. template<typename eT>
  2930. inline
  2931. const Cube<eT>&
  2932. Cube<eT>::ones(const uword in_rows, const uword in_cols, const uword in_slices)
  2933. {
  2934. arma_extra_debug_sigprint();
  2935. set_size(in_rows, in_cols, in_slices);
  2936. return (*this).fill(eT(1));
  2937. }
  2938. template<typename eT>
  2939. inline
  2940. const Cube<eT>&
  2941. Cube<eT>::ones(const SizeCube& s)
  2942. {
  2943. arma_extra_debug_sigprint();
  2944. return (*this).ones(s.n_rows, s.n_cols, s.n_slices);
  2945. }
  2946. template<typename eT>
  2947. inline
  2948. const Cube<eT>&
  2949. Cube<eT>::randu()
  2950. {
  2951. arma_extra_debug_sigprint();
  2952. arma_rng::randu<eT>::fill( memptr(), n_elem );
  2953. return *this;
  2954. }
  2955. template<typename eT>
  2956. inline
  2957. const Cube<eT>&
  2958. Cube<eT>::randu(const uword in_rows, const uword in_cols, const uword in_slices)
  2959. {
  2960. arma_extra_debug_sigprint();
  2961. set_size(in_rows, in_cols, in_slices);
  2962. return (*this).randu();
  2963. }
  2964. template<typename eT>
  2965. inline
  2966. const Cube<eT>&
  2967. Cube<eT>::randu(const SizeCube& s)
  2968. {
  2969. arma_extra_debug_sigprint();
  2970. return (*this).randu(s.n_rows, s.n_cols, s.n_slices);
  2971. }
  2972. template<typename eT>
  2973. inline
  2974. const Cube<eT>&
  2975. Cube<eT>::randn()
  2976. {
  2977. arma_extra_debug_sigprint();
  2978. arma_rng::randn<eT>::fill( memptr(), n_elem );
  2979. return *this;
  2980. }
  2981. template<typename eT>
  2982. inline
  2983. const Cube<eT>&
  2984. Cube<eT>::randn(const uword in_rows, const uword in_cols, const uword in_slices)
  2985. {
  2986. arma_extra_debug_sigprint();
  2987. set_size(in_rows, in_cols, in_slices);
  2988. return (*this).randn();
  2989. }
  2990. template<typename eT>
  2991. inline
  2992. const Cube<eT>&
  2993. Cube<eT>::randn(const SizeCube& s)
  2994. {
  2995. arma_extra_debug_sigprint();
  2996. return (*this).randn(s.n_rows, s.n_cols, s.n_slices);
  2997. }
  2998. template<typename eT>
  2999. inline
  3000. void
  3001. Cube<eT>::reset()
  3002. {
  3003. arma_extra_debug_sigprint();
  3004. init_warm(0,0,0);
  3005. }
  3006. template<typename eT>
  3007. inline
  3008. void
  3009. Cube<eT>::soft_reset()
  3010. {
  3011. arma_extra_debug_sigprint();
  3012. // don't change the size if the cube has a fixed size
  3013. if(mem_state <= 1)
  3014. {
  3015. reset();
  3016. }
  3017. else
  3018. {
  3019. fill(Datum<eT>::nan);
  3020. }
  3021. }
  3022. template<typename eT>
  3023. template<typename T1>
  3024. inline
  3025. void
  3026. Cube<eT>::set_real(const BaseCube<typename Cube<eT>::pod_type,T1>& X)
  3027. {
  3028. arma_extra_debug_sigprint();
  3029. Cube_aux::set_real(*this, X);
  3030. }
  3031. template<typename eT>
  3032. template<typename T1>
  3033. inline
  3034. void
  3035. Cube<eT>::set_imag(const BaseCube<typename Cube<eT>::pod_type,T1>& X)
  3036. {
  3037. arma_extra_debug_sigprint();
  3038. Cube_aux::set_imag(*this, X);
  3039. }
  3040. template<typename eT>
  3041. inline
  3042. arma_warn_unused
  3043. eT
  3044. Cube<eT>::min() const
  3045. {
  3046. arma_extra_debug_sigprint();
  3047. if(n_elem == 0)
  3048. {
  3049. arma_debug_check(true, "Cube::min(): object has no elements");
  3050. return Datum<eT>::nan;
  3051. }
  3052. return op_min::direct_min(memptr(), n_elem);
  3053. }
  3054. template<typename eT>
  3055. inline
  3056. arma_warn_unused
  3057. eT
  3058. Cube<eT>::max() const
  3059. {
  3060. arma_extra_debug_sigprint();
  3061. if(n_elem == 0)
  3062. {
  3063. arma_debug_check(true, "Cube::max(): object has no elements");
  3064. return Datum<eT>::nan;
  3065. }
  3066. return op_max::direct_max(memptr(), n_elem);
  3067. }
  3068. template<typename eT>
  3069. inline
  3070. eT
  3071. Cube<eT>::min(uword& index_of_min_val) const
  3072. {
  3073. arma_extra_debug_sigprint();
  3074. if(n_elem == 0)
  3075. {
  3076. arma_debug_check(true, "Cube::min(): object has no elements");
  3077. index_of_min_val = uword(0);
  3078. return Datum<eT>::nan;
  3079. }
  3080. return op_min::direct_min(memptr(), n_elem, index_of_min_val);
  3081. }
  3082. template<typename eT>
  3083. inline
  3084. eT
  3085. Cube<eT>::max(uword& index_of_max_val) const
  3086. {
  3087. arma_extra_debug_sigprint();
  3088. if(n_elem == 0)
  3089. {
  3090. arma_debug_check(true, "Cube::max(): object has no elements");
  3091. index_of_max_val = uword(0);
  3092. return Datum<eT>::nan;
  3093. }
  3094. return op_max::direct_max(memptr(), n_elem, index_of_max_val);
  3095. }
  3096. template<typename eT>
  3097. inline
  3098. eT
  3099. Cube<eT>::min(uword& row_of_min_val, uword& col_of_min_val, uword& slice_of_min_val) const
  3100. {
  3101. arma_extra_debug_sigprint();
  3102. if(n_elem == 0)
  3103. {
  3104. arma_debug_check(true, "Cube::min(): object has no elements");
  3105. row_of_min_val = uword(0);
  3106. col_of_min_val = uword(0);
  3107. slice_of_min_val = uword(0);
  3108. return Datum<eT>::nan;
  3109. }
  3110. uword i;
  3111. eT val = op_min::direct_min(memptr(), n_elem, i);
  3112. const uword in_slice = i / n_elem_slice;
  3113. const uword offset = in_slice * n_elem_slice;
  3114. const uword j = i - offset;
  3115. row_of_min_val = j % n_rows;
  3116. col_of_min_val = j / n_rows;
  3117. slice_of_min_val = in_slice;
  3118. return val;
  3119. }
  3120. template<typename eT>
  3121. inline
  3122. eT
  3123. Cube<eT>::max(uword& row_of_max_val, uword& col_of_max_val, uword& slice_of_max_val) const
  3124. {
  3125. arma_extra_debug_sigprint();
  3126. if(n_elem == 0)
  3127. {
  3128. arma_debug_check(true, "Cube::max(): object has no elements");
  3129. row_of_max_val = uword(0);
  3130. col_of_max_val = uword(0);
  3131. slice_of_max_val = uword(0);
  3132. return Datum<eT>::nan;
  3133. }
  3134. uword i;
  3135. eT val = op_max::direct_max(memptr(), n_elem, i);
  3136. const uword in_slice = i / n_elem_slice;
  3137. const uword offset = in_slice * n_elem_slice;
  3138. const uword j = i - offset;
  3139. row_of_max_val = j % n_rows;
  3140. col_of_max_val = j / n_rows;
  3141. slice_of_max_val = in_slice;
  3142. return val;
  3143. }
  3144. //! save the cube to a file
  3145. template<typename eT>
  3146. inline
  3147. arma_cold
  3148. bool
  3149. Cube<eT>::save(const std::string name, const file_type type, const bool print_status) const
  3150. {
  3151. arma_extra_debug_sigprint();
  3152. bool save_okay = false;
  3153. switch(type)
  3154. {
  3155. case raw_ascii:
  3156. save_okay = diskio::save_raw_ascii(*this, name);
  3157. break;
  3158. case arma_ascii:
  3159. save_okay = diskio::save_arma_ascii(*this, name);
  3160. break;
  3161. case raw_binary:
  3162. save_okay = diskio::save_raw_binary(*this, name);
  3163. break;
  3164. case arma_binary:
  3165. save_okay = diskio::save_arma_binary(*this, name);
  3166. break;
  3167. case ppm_binary:
  3168. save_okay = diskio::save_ppm_binary(*this, name);
  3169. break;
  3170. case hdf5_binary:
  3171. return (*this).save(hdf5_name(name));
  3172. break;
  3173. case hdf5_binary_trans: // kept for compatibility with earlier versions of Armadillo
  3174. return (*this).save(hdf5_name(name, std::string(), hdf5_opts::trans));
  3175. break;
  3176. default:
  3177. if(print_status) { arma_debug_warn("Cube::save(): unsupported file type"); }
  3178. save_okay = false;
  3179. }
  3180. if(print_status && (save_okay == false)) { arma_debug_warn("Cube::save(): couldn't write to ", name); }
  3181. return save_okay;
  3182. }
  3183. template<typename eT>
  3184. inline
  3185. arma_cold
  3186. bool
  3187. Cube<eT>::save(const hdf5_name& spec, const file_type type, const bool print_status) const
  3188. {
  3189. arma_extra_debug_sigprint();
  3190. // handling of hdf5_binary_trans kept for compatibility with earlier versions of Armadillo
  3191. if( (type != hdf5_binary) && (type != hdf5_binary_trans) )
  3192. {
  3193. arma_debug_check(true, "Cube::save(): unsupported file type for hdf5_name()");
  3194. return false;
  3195. }
  3196. const bool do_trans = bool(spec.opts.flags & hdf5_opts::flag_trans ) || (type == hdf5_binary_trans);
  3197. const bool append = bool(spec.opts.flags & hdf5_opts::flag_append );
  3198. const bool replace = bool(spec.opts.flags & hdf5_opts::flag_replace);
  3199. if(append && replace)
  3200. {
  3201. arma_debug_check(true, "Cube::save(): only one of 'append' or 'replace' options can be used");
  3202. return false;
  3203. }
  3204. bool save_okay = false;
  3205. std::string err_msg;
  3206. if(do_trans)
  3207. {
  3208. Cube<eT> tmp;
  3209. op_strans_cube::apply_noalias(tmp, (*this));
  3210. save_okay = diskio::save_hdf5_binary(tmp, spec, err_msg);
  3211. }
  3212. else
  3213. {
  3214. save_okay = diskio::save_hdf5_binary(*this, spec, err_msg);
  3215. }
  3216. if((print_status == true) && (save_okay == false))
  3217. {
  3218. if(err_msg.length() > 0)
  3219. {
  3220. arma_debug_warn("Cube::save(): ", err_msg, spec.filename);
  3221. }
  3222. else
  3223. {
  3224. arma_debug_warn("Cube::save(): couldn't write to ", spec.filename);
  3225. }
  3226. }
  3227. return save_okay;
  3228. }
  3229. //! save the cube to a stream
  3230. template<typename eT>
  3231. inline
  3232. arma_cold
  3233. bool
  3234. Cube<eT>::save(std::ostream& os, const file_type type, const bool print_status) const
  3235. {
  3236. arma_extra_debug_sigprint();
  3237. bool save_okay = false;
  3238. switch(type)
  3239. {
  3240. case raw_ascii:
  3241. save_okay = diskio::save_raw_ascii(*this, os);
  3242. break;
  3243. case arma_ascii:
  3244. save_okay = diskio::save_arma_ascii(*this, os);
  3245. break;
  3246. case raw_binary:
  3247. save_okay = diskio::save_raw_binary(*this, os);
  3248. break;
  3249. case arma_binary:
  3250. save_okay = diskio::save_arma_binary(*this, os);
  3251. break;
  3252. case ppm_binary:
  3253. save_okay = diskio::save_ppm_binary(*this, os);
  3254. break;
  3255. default:
  3256. if(print_status) { arma_debug_warn("Cube::save(): unsupported file type"); }
  3257. save_okay = false;
  3258. }
  3259. if(print_status && (save_okay == false)) { arma_debug_warn("Cube::save(): couldn't write to given stream"); }
  3260. return save_okay;
  3261. }
  3262. //! load a cube from a file
  3263. template<typename eT>
  3264. inline
  3265. arma_cold
  3266. bool
  3267. Cube<eT>::load(const std::string name, const file_type type, const bool print_status)
  3268. {
  3269. arma_extra_debug_sigprint();
  3270. bool load_okay = false;
  3271. std::string err_msg;
  3272. switch(type)
  3273. {
  3274. case auto_detect:
  3275. load_okay = diskio::load_auto_detect(*this, name, err_msg);
  3276. break;
  3277. case raw_ascii:
  3278. load_okay = diskio::load_raw_ascii(*this, name, err_msg);
  3279. break;
  3280. case arma_ascii:
  3281. load_okay = diskio::load_arma_ascii(*this, name, err_msg);
  3282. break;
  3283. case raw_binary:
  3284. load_okay = diskio::load_raw_binary(*this, name, err_msg);
  3285. break;
  3286. case arma_binary:
  3287. load_okay = diskio::load_arma_binary(*this, name, err_msg);
  3288. break;
  3289. case ppm_binary:
  3290. load_okay = diskio::load_ppm_binary(*this, name, err_msg);
  3291. break;
  3292. case hdf5_binary:
  3293. return (*this).load(hdf5_name(name));
  3294. break;
  3295. case hdf5_binary_trans: // kept for compatibility with earlier versions of Armadillo
  3296. return (*this).load(hdf5_name(name, std::string(), hdf5_opts::trans));
  3297. break;
  3298. default:
  3299. if(print_status) { arma_debug_warn("Cube::load(): unsupported file type"); }
  3300. load_okay = false;
  3301. }
  3302. if( (print_status == true) && (load_okay == false) )
  3303. {
  3304. if(err_msg.length() > 0)
  3305. {
  3306. arma_debug_warn("Cube::load(): ", err_msg, name);
  3307. }
  3308. else
  3309. {
  3310. arma_debug_warn("Cube::load(): couldn't read ", name);
  3311. }
  3312. }
  3313. if(load_okay == false)
  3314. {
  3315. (*this).soft_reset();
  3316. }
  3317. return load_okay;
  3318. }
  3319. template<typename eT>
  3320. inline
  3321. arma_cold
  3322. bool
  3323. Cube<eT>::load(const hdf5_name& spec, const file_type type, const bool print_status)
  3324. {
  3325. arma_extra_debug_sigprint();
  3326. if( (type != hdf5_binary) && (type != hdf5_binary_trans) )
  3327. {
  3328. if(print_status) { arma_debug_warn("Cube::load(): unsupported file type for hdf5_name()"); }
  3329. (*this).soft_reset();
  3330. return false;
  3331. }
  3332. bool load_okay = false;
  3333. std::string err_msg;
  3334. const bool do_trans = bool(spec.opts.flags & hdf5_opts::flag_trans) || (type == hdf5_binary_trans);
  3335. if(do_trans)
  3336. {
  3337. Cube<eT> tmp;
  3338. load_okay = diskio::load_hdf5_binary(tmp, spec, err_msg);
  3339. if(load_okay) { op_strans_cube::apply_noalias((*this), tmp); }
  3340. }
  3341. else
  3342. {
  3343. load_okay = diskio::load_hdf5_binary(*this, spec, err_msg);
  3344. }
  3345. if( (print_status == true) && (load_okay == false) )
  3346. {
  3347. if(err_msg.length() > 0)
  3348. {
  3349. arma_debug_warn("Cube::load(): ", err_msg, spec.filename);
  3350. }
  3351. else
  3352. {
  3353. arma_debug_warn("Cube::load(): couldn't read ", spec.filename);
  3354. }
  3355. }
  3356. if(load_okay == false)
  3357. {
  3358. (*this).soft_reset();
  3359. }
  3360. return load_okay;
  3361. }
  3362. //! load a cube from a stream
  3363. template<typename eT>
  3364. inline
  3365. arma_cold
  3366. bool
  3367. Cube<eT>::load(std::istream& is, const file_type type, const bool print_status)
  3368. {
  3369. arma_extra_debug_sigprint();
  3370. bool load_okay = false;
  3371. std::string err_msg;
  3372. switch(type)
  3373. {
  3374. case auto_detect:
  3375. load_okay = diskio::load_auto_detect(*this, is, err_msg);
  3376. break;
  3377. case raw_ascii:
  3378. load_okay = diskio::load_raw_ascii(*this, is, err_msg);
  3379. break;
  3380. case arma_ascii:
  3381. load_okay = diskio::load_arma_ascii(*this, is, err_msg);
  3382. break;
  3383. case raw_binary:
  3384. load_okay = diskio::load_raw_binary(*this, is, err_msg);
  3385. break;
  3386. case arma_binary:
  3387. load_okay = diskio::load_arma_binary(*this, is, err_msg);
  3388. break;
  3389. case ppm_binary:
  3390. load_okay = diskio::load_ppm_binary(*this, is, err_msg);
  3391. break;
  3392. default:
  3393. if(print_status) { arma_debug_warn("Cube::load(): unsupported file type"); }
  3394. load_okay = false;
  3395. }
  3396. if( (print_status == true) && (load_okay == false) )
  3397. {
  3398. if(err_msg.length() > 0)
  3399. {
  3400. arma_debug_warn("Cube::load(): ", err_msg, "the given stream");
  3401. }
  3402. else
  3403. {
  3404. arma_debug_warn("Cube::load(): couldn't load from the given stream");
  3405. }
  3406. }
  3407. if(load_okay == false)
  3408. {
  3409. (*this).soft_reset();
  3410. }
  3411. return load_okay;
  3412. }
  3413. //! save the cube to a file, without printing any error messages
  3414. template<typename eT>
  3415. inline
  3416. arma_cold
  3417. bool
  3418. Cube<eT>::quiet_save(const std::string name, const file_type type) const
  3419. {
  3420. arma_extra_debug_sigprint();
  3421. return (*this).save(name, type, false);
  3422. }
  3423. template<typename eT>
  3424. inline
  3425. arma_cold
  3426. bool
  3427. Cube<eT>::quiet_save(const hdf5_name& spec, const file_type type) const
  3428. {
  3429. arma_extra_debug_sigprint();
  3430. return (*this).save(spec, type, false);
  3431. }
  3432. //! save the cube to a stream, without printing any error messages
  3433. template<typename eT>
  3434. inline
  3435. arma_cold
  3436. bool
  3437. Cube<eT>::quiet_save(std::ostream& os, const file_type type) const
  3438. {
  3439. arma_extra_debug_sigprint();
  3440. return (*this).save(os, type, false);
  3441. }
  3442. //! load a cube from a file, without printing any error messages
  3443. template<typename eT>
  3444. inline
  3445. arma_cold
  3446. bool
  3447. Cube<eT>::quiet_load(const std::string name, const file_type type)
  3448. {
  3449. arma_extra_debug_sigprint();
  3450. return (*this).load(name, type, false);
  3451. }
  3452. template<typename eT>
  3453. inline
  3454. arma_cold
  3455. bool
  3456. Cube<eT>::quiet_load(const hdf5_name& spec, const file_type type)
  3457. {
  3458. arma_extra_debug_sigprint();
  3459. return (*this).load(spec, type, false);
  3460. }
  3461. //! load a cube from a stream, without printing any error messages
  3462. template<typename eT>
  3463. inline
  3464. arma_cold
  3465. bool
  3466. Cube<eT>::quiet_load(std::istream& is, const file_type type)
  3467. {
  3468. arma_extra_debug_sigprint();
  3469. return (*this).load(is, type, false);
  3470. }
  3471. template<typename eT>
  3472. inline
  3473. typename Cube<eT>::iterator
  3474. Cube<eT>::begin()
  3475. {
  3476. arma_extra_debug_sigprint();
  3477. return memptr();
  3478. }
  3479. template<typename eT>
  3480. inline
  3481. typename Cube<eT>::const_iterator
  3482. Cube<eT>::begin() const
  3483. {
  3484. arma_extra_debug_sigprint();
  3485. return memptr();
  3486. }
  3487. template<typename eT>
  3488. inline
  3489. typename Cube<eT>::const_iterator
  3490. Cube<eT>::cbegin() const
  3491. {
  3492. arma_extra_debug_sigprint();
  3493. return memptr();
  3494. }
  3495. template<typename eT>
  3496. inline
  3497. typename Cube<eT>::iterator
  3498. Cube<eT>::end()
  3499. {
  3500. arma_extra_debug_sigprint();
  3501. return memptr() + n_elem;
  3502. }
  3503. template<typename eT>
  3504. inline
  3505. typename Cube<eT>::const_iterator
  3506. Cube<eT>::end() const
  3507. {
  3508. arma_extra_debug_sigprint();
  3509. return memptr() + n_elem;
  3510. }
  3511. template<typename eT>
  3512. inline
  3513. typename Cube<eT>::const_iterator
  3514. Cube<eT>::cend() const
  3515. {
  3516. arma_extra_debug_sigprint();
  3517. return memptr() + n_elem;
  3518. }
  3519. template<typename eT>
  3520. inline
  3521. typename Cube<eT>::slice_iterator
  3522. Cube<eT>::begin_slice(const uword slice_num)
  3523. {
  3524. arma_extra_debug_sigprint();
  3525. arma_debug_check( (slice_num >= n_slices), "begin_slice(): index out of bounds");
  3526. return slice_memptr(slice_num);
  3527. }
  3528. template<typename eT>
  3529. inline
  3530. typename Cube<eT>::const_slice_iterator
  3531. Cube<eT>::begin_slice(const uword slice_num) const
  3532. {
  3533. arma_extra_debug_sigprint();
  3534. arma_debug_check( (slice_num >= n_slices), "begin_slice(): index out of bounds");
  3535. return slice_memptr(slice_num);
  3536. }
  3537. template<typename eT>
  3538. inline
  3539. typename Cube<eT>::slice_iterator
  3540. Cube<eT>::end_slice(const uword slice_num)
  3541. {
  3542. arma_extra_debug_sigprint();
  3543. arma_debug_check( (slice_num >= n_slices), "end_slice(): index out of bounds");
  3544. return slice_memptr(slice_num) + n_elem_slice;
  3545. }
  3546. template<typename eT>
  3547. inline
  3548. typename Cube<eT>::const_slice_iterator
  3549. Cube<eT>::end_slice(const uword slice_num) const
  3550. {
  3551. arma_extra_debug_sigprint();
  3552. arma_debug_check( (slice_num >= n_slices), "end_slice(): index out of bounds");
  3553. return slice_memptr(slice_num) + n_elem_slice;
  3554. }
  3555. //! resets this cube to an empty matrix
  3556. template<typename eT>
  3557. inline
  3558. void
  3559. Cube<eT>::clear()
  3560. {
  3561. reset();
  3562. }
  3563. //! returns true if the cube has no elements
  3564. template<typename eT>
  3565. inline
  3566. bool
  3567. Cube<eT>::empty() const
  3568. {
  3569. return (n_elem == 0);
  3570. }
  3571. //! returns the number of elements in this cube
  3572. template<typename eT>
  3573. inline
  3574. uword
  3575. Cube<eT>::size() const
  3576. {
  3577. return n_elem;
  3578. }
  3579. template<typename eT>
  3580. inline
  3581. eT&
  3582. Cube<eT>::front()
  3583. {
  3584. arma_debug_check( (n_elem == 0), "Cube::front(): cube is empty" );
  3585. return access::rw(mem[0]);
  3586. }
  3587. template<typename eT>
  3588. inline
  3589. const eT&
  3590. Cube<eT>::front() const
  3591. {
  3592. arma_debug_check( (n_elem == 0), "Cube::front(): cube is empty" );
  3593. return mem[0];
  3594. }
  3595. template<typename eT>
  3596. inline
  3597. eT&
  3598. Cube<eT>::back()
  3599. {
  3600. arma_debug_check( (n_elem == 0), "Cube::back(): cube is empty" );
  3601. return access::rw(mem[n_elem-1]);
  3602. }
  3603. template<typename eT>
  3604. inline
  3605. const eT&
  3606. Cube<eT>::back() const
  3607. {
  3608. arma_debug_check( (n_elem == 0), "Cube::back(): cube is empty" );
  3609. return mem[n_elem-1];
  3610. }
  3611. template<typename eT>
  3612. inline
  3613. void
  3614. Cube<eT>::swap(Cube<eT>& B)
  3615. {
  3616. Cube<eT>& A = (*this);
  3617. arma_extra_debug_sigprint(arma_str::format("A = %x B = %x") % &A % &B);
  3618. if( (A.mem_state == 0) && (B.mem_state == 0) && (A.n_elem > Cube_prealloc::mem_n_elem) && (B.n_elem > Cube_prealloc::mem_n_elem) )
  3619. {
  3620. A.delete_mat();
  3621. B.delete_mat();
  3622. std::swap( access::rw(A.n_rows), access::rw(B.n_rows) );
  3623. std::swap( access::rw(A.n_cols), access::rw(B.n_cols) );
  3624. std::swap( access::rw(A.n_elem_slice), access::rw(B.n_elem_slice) );
  3625. std::swap( access::rw(A.n_slices), access::rw(B.n_slices) );
  3626. std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
  3627. std::swap( access::rw(A.mem), access::rw(B.mem) );
  3628. A.create_mat();
  3629. B.create_mat();
  3630. }
  3631. else
  3632. if( (A.mem_state == 0) && (B.mem_state == 0) && (A.n_elem <= Cube_prealloc::mem_n_elem) && (B.n_elem <= Cube_prealloc::mem_n_elem) )
  3633. {
  3634. A.delete_mat();
  3635. B.delete_mat();
  3636. std::swap( access::rw(A.n_rows), access::rw(B.n_rows) );
  3637. std::swap( access::rw(A.n_cols), access::rw(B.n_cols) );
  3638. std::swap( access::rw(A.n_elem_slice), access::rw(B.n_elem_slice) );
  3639. std::swap( access::rw(A.n_slices), access::rw(B.n_slices) );
  3640. std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
  3641. const uword N = (std::max)(A.n_elem, B.n_elem);
  3642. eT* A_mem = A.memptr();
  3643. eT* B_mem = B.memptr();
  3644. for(uword i=0; i<N; ++i) { std::swap( A_mem[i], B_mem[i] ); }
  3645. A.create_mat();
  3646. B.create_mat();
  3647. }
  3648. else
  3649. {
  3650. // generic swap
  3651. if(A.n_elem <= B.n_elem)
  3652. {
  3653. Cube<eT> C = A;
  3654. A.steal_mem(B);
  3655. B.steal_mem(C);
  3656. }
  3657. else
  3658. {
  3659. Cube<eT> C = B;
  3660. B.steal_mem(A);
  3661. A.steal_mem(C);
  3662. }
  3663. }
  3664. }
  3665. //! try to steal the memory from a given cube;
  3666. //! if memory can't be stolen, copy the given cube
  3667. template<typename eT>
  3668. inline
  3669. void
  3670. Cube<eT>::steal_mem(Cube<eT>& x)
  3671. {
  3672. arma_extra_debug_sigprint();
  3673. if(this == &x) { return; }
  3674. if( (mem_state <= 1) && ( ((x.mem_state == 0) && (x.n_elem > Cube_prealloc::mem_n_elem)) || (x.mem_state == 1) ) )
  3675. {
  3676. reset();
  3677. const uword x_n_slices = x.n_slices;
  3678. access::rw(n_rows) = x.n_rows;
  3679. access::rw(n_cols) = x.n_cols;
  3680. access::rw(n_elem_slice) = x.n_elem_slice;
  3681. access::rw(n_slices) = x_n_slices;
  3682. access::rw(n_elem) = x.n_elem;
  3683. access::rw(mem_state) = x.mem_state;
  3684. access::rw(mem) = x.mem;
  3685. if(x_n_slices > Cube_prealloc::mat_ptrs_size)
  3686. {
  3687. access::rw( mat_ptrs) = x.mat_ptrs;
  3688. access::rw(x.mat_ptrs) = 0;
  3689. }
  3690. else
  3691. {
  3692. access::rw(mat_ptrs) = const_cast< const Mat<eT>** >(mat_ptrs_local);
  3693. for(uword i=0; i < x_n_slices; ++i)
  3694. {
  3695. mat_ptrs[i] = x.mat_ptrs[i];
  3696. x.mat_ptrs[i] = 0;
  3697. }
  3698. }
  3699. access::rw(x.n_rows) = 0;
  3700. access::rw(x.n_cols) = 0;
  3701. access::rw(x.n_elem_slice) = 0;
  3702. access::rw(x.n_slices) = 0;
  3703. access::rw(x.n_elem) = 0;
  3704. access::rw(x.mem_state) = 0;
  3705. access::rw(x.mem) = 0;
  3706. }
  3707. else
  3708. {
  3709. (*this).operator=(x);
  3710. }
  3711. }
  3712. //
  3713. // Cube::fixed
  3714. template<typename eT>
  3715. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3716. arma_inline
  3717. void
  3718. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::mem_setup()
  3719. {
  3720. arma_extra_debug_sigprint();
  3721. if(fixed_n_elem > 0)
  3722. {
  3723. access::rw(Cube<eT>::n_rows) = fixed_n_rows;
  3724. access::rw(Cube<eT>::n_cols) = fixed_n_cols;
  3725. access::rw(Cube<eT>::n_elem_slice) = fixed_n_rows * fixed_n_cols;
  3726. access::rw(Cube<eT>::n_slices) = fixed_n_slices;
  3727. access::rw(Cube<eT>::n_elem) = fixed_n_elem;
  3728. access::rw(Cube<eT>::mem_state) = 3;
  3729. access::rw(Cube<eT>::mem) = (fixed_n_elem > Cube_prealloc::mem_n_elem) ? mem_local_extra : mem_local;
  3730. access::rw(Cube<eT>::mat_ptrs) = const_cast< const Mat<eT>** >( \
  3731. (fixed_n_slices > Cube_prealloc::mat_ptrs_size) ? mat_ptrs_local_extra : mat_ptrs_local );
  3732. create_mat();
  3733. }
  3734. else
  3735. {
  3736. access::rw(Cube<eT>::n_rows) = 0;
  3737. access::rw(Cube<eT>::n_cols) = 0;
  3738. access::rw(Cube<eT>::n_elem_slice) = 0;
  3739. access::rw(Cube<eT>::n_slices) = 0;
  3740. access::rw(Cube<eT>::n_elem) = 0;
  3741. access::rw(Cube<eT>::mem_state) = 3;
  3742. access::rw(Cube<eT>::mem) = 0;
  3743. access::rw(Cube<eT>::mat_ptrs) = 0;
  3744. }
  3745. }
  3746. template<typename eT>
  3747. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3748. inline
  3749. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::fixed()
  3750. {
  3751. arma_extra_debug_sigprint_this(this);
  3752. mem_setup();
  3753. }
  3754. template<typename eT>
  3755. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3756. inline
  3757. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::fixed(const fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>& X)
  3758. {
  3759. arma_extra_debug_sigprint_this(this);
  3760. mem_setup();
  3761. eT* dest = (use_extra) ? mem_local_extra : mem_local;
  3762. const eT* src = (use_extra) ? X.mem_local_extra : X.mem_local;
  3763. arrayops::copy( dest, src, fixed_n_elem );
  3764. }
  3765. template<typename eT>
  3766. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3767. template<typename fill_type>
  3768. inline
  3769. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::fixed(const fill::fill_class<fill_type>&)
  3770. {
  3771. arma_extra_debug_sigprint_this(this);
  3772. mem_setup();
  3773. if(is_same_type<fill_type, fill::fill_zeros>::yes) (*this).zeros();
  3774. if(is_same_type<fill_type, fill::fill_ones >::yes) (*this).ones();
  3775. if(is_same_type<fill_type, fill::fill_randu>::yes) (*this).randu();
  3776. if(is_same_type<fill_type, fill::fill_randn>::yes) (*this).randn();
  3777. if(is_same_type<fill_type, fill::fill_eye >::yes) { arma_debug_check(true, "Cube::fixed::fixed(): unsupported fill type"); }
  3778. }
  3779. template<typename eT>
  3780. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3781. template<typename T1>
  3782. inline
  3783. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::fixed(const BaseCube<eT,T1>& A)
  3784. {
  3785. arma_extra_debug_sigprint_this(this);
  3786. mem_setup();
  3787. Cube<eT>::operator=(A.get_ref());
  3788. }
  3789. template<typename eT>
  3790. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3791. template<typename T1, typename T2>
  3792. inline
  3793. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::fixed(const BaseCube<pod_type,T1>& A, const BaseCube<pod_type,T2>& B)
  3794. {
  3795. arma_extra_debug_sigprint_this(this);
  3796. mem_setup();
  3797. Cube<eT>::init(A,B);
  3798. }
  3799. template<typename eT>
  3800. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3801. inline
  3802. Cube<eT>&
  3803. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator=(const fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>& X)
  3804. {
  3805. arma_extra_debug_sigprint();
  3806. eT* dest = (use_extra) ? mem_local_extra : mem_local;
  3807. const eT* src = (use_extra) ? X.mem_local_extra : X.mem_local;
  3808. arrayops::copy( dest, src, fixed_n_elem );
  3809. return *this;
  3810. }
  3811. template<typename eT>
  3812. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3813. arma_inline
  3814. arma_warn_unused
  3815. eT&
  3816. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator[] (const uword i)
  3817. {
  3818. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3819. }
  3820. template<typename eT>
  3821. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3822. arma_inline
  3823. arma_warn_unused
  3824. const eT&
  3825. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator[] (const uword i) const
  3826. {
  3827. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3828. }
  3829. template<typename eT>
  3830. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3831. arma_inline
  3832. arma_warn_unused
  3833. eT&
  3834. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::at(const uword i)
  3835. {
  3836. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3837. }
  3838. template<typename eT>
  3839. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3840. arma_inline
  3841. arma_warn_unused
  3842. const eT&
  3843. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::at(const uword i) const
  3844. {
  3845. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3846. }
  3847. template<typename eT>
  3848. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3849. arma_inline
  3850. arma_warn_unused
  3851. eT&
  3852. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator() (const uword i)
  3853. {
  3854. arma_debug_check( (i >= fixed_n_elem), "Cube::operator(): index out of bounds");
  3855. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3856. }
  3857. template<typename eT>
  3858. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3859. arma_inline
  3860. arma_warn_unused
  3861. const eT&
  3862. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator() (const uword i) const
  3863. {
  3864. arma_debug_check( (i >= fixed_n_elem), "Cube::operator(): index out of bounds");
  3865. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3866. }
  3867. template<typename eT>
  3868. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3869. arma_inline
  3870. arma_warn_unused
  3871. eT&
  3872. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::at(const uword in_row, const uword in_col, const uword in_slice)
  3873. {
  3874. const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row;
  3875. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3876. }
  3877. template<typename eT>
  3878. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3879. arma_inline
  3880. arma_warn_unused
  3881. const eT&
  3882. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::at(const uword in_row, const uword in_col, const uword in_slice) const
  3883. {
  3884. const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row;
  3885. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3886. }
  3887. template<typename eT>
  3888. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3889. arma_inline
  3890. arma_warn_unused
  3891. eT&
  3892. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator() (const uword in_row, const uword in_col, const uword in_slice)
  3893. {
  3894. arma_debug_check
  3895. (
  3896. (in_row >= fixed_n_rows ) ||
  3897. (in_col >= fixed_n_cols ) ||
  3898. (in_slice >= fixed_n_slices)
  3899. ,
  3900. "operator(): index out of bounds"
  3901. );
  3902. const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row;
  3903. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3904. }
  3905. template<typename eT>
  3906. template<uword fixed_n_rows, uword fixed_n_cols, uword fixed_n_slices>
  3907. arma_inline
  3908. arma_warn_unused
  3909. const eT&
  3910. Cube<eT>::fixed<fixed_n_rows, fixed_n_cols, fixed_n_slices>::operator() (const uword in_row, const uword in_col, const uword in_slice) const
  3911. {
  3912. arma_debug_check
  3913. (
  3914. (in_row >= fixed_n_rows ) ||
  3915. (in_col >= fixed_n_cols ) ||
  3916. (in_slice >= fixed_n_slices)
  3917. ,
  3918. "Cube::operator(): index out of bounds"
  3919. );
  3920. const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row;
  3921. return (use_extra) ? mem_local_extra[i] : mem_local[i];
  3922. }
  3923. //
  3924. // Cube_aux
  3925. //! prefix ++
  3926. template<typename eT>
  3927. arma_inline
  3928. void
  3929. Cube_aux::prefix_pp(Cube<eT>& x)
  3930. {
  3931. eT* memptr = x.memptr();
  3932. const uword n_elem = x.n_elem;
  3933. uword i,j;
  3934. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  3935. {
  3936. ++(memptr[i]);
  3937. ++(memptr[j]);
  3938. }
  3939. if(i < n_elem)
  3940. {
  3941. ++(memptr[i]);
  3942. }
  3943. }
  3944. //! prefix ++ for complex numbers (work around for limitations of the std::complex class)
  3945. template<typename T>
  3946. arma_inline
  3947. void
  3948. Cube_aux::prefix_pp(Cube< std::complex<T> >& x)
  3949. {
  3950. x += T(1);
  3951. }
  3952. //! postfix ++
  3953. template<typename eT>
  3954. arma_inline
  3955. void
  3956. Cube_aux::postfix_pp(Cube<eT>& x)
  3957. {
  3958. eT* memptr = x.memptr();
  3959. const uword n_elem = x.n_elem;
  3960. uword i,j;
  3961. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  3962. {
  3963. (memptr[i])++;
  3964. (memptr[j])++;
  3965. }
  3966. if(i < n_elem)
  3967. {
  3968. (memptr[i])++;
  3969. }
  3970. }
  3971. //! postfix ++ for complex numbers (work around for limitations of the std::complex class)
  3972. template<typename T>
  3973. arma_inline
  3974. void
  3975. Cube_aux::postfix_pp(Cube< std::complex<T> >& x)
  3976. {
  3977. x += T(1);
  3978. }
  3979. //! prefix --
  3980. template<typename eT>
  3981. arma_inline
  3982. void
  3983. Cube_aux::prefix_mm(Cube<eT>& x)
  3984. {
  3985. eT* memptr = x.memptr();
  3986. const uword n_elem = x.n_elem;
  3987. uword i,j;
  3988. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  3989. {
  3990. --(memptr[i]);
  3991. --(memptr[j]);
  3992. }
  3993. if(i < n_elem)
  3994. {
  3995. --(memptr[i]);
  3996. }
  3997. }
  3998. //! prefix -- for complex numbers (work around for limitations of the std::complex class)
  3999. template<typename T>
  4000. arma_inline
  4001. void
  4002. Cube_aux::prefix_mm(Cube< std::complex<T> >& x)
  4003. {
  4004. x -= T(1);
  4005. }
  4006. //! postfix --
  4007. template<typename eT>
  4008. arma_inline
  4009. void
  4010. Cube_aux::postfix_mm(Cube<eT>& x)
  4011. {
  4012. eT* memptr = x.memptr();
  4013. const uword n_elem = x.n_elem;
  4014. uword i,j;
  4015. for(i=0, j=1; j<n_elem; i+=2, j+=2)
  4016. {
  4017. (memptr[i])--;
  4018. (memptr[j])--;
  4019. }
  4020. if(i < n_elem)
  4021. {
  4022. (memptr[i])--;
  4023. }
  4024. }
  4025. //! postfix ++ for complex numbers (work around for limitations of the std::complex class)
  4026. template<typename T>
  4027. arma_inline
  4028. void
  4029. Cube_aux::postfix_mm(Cube< std::complex<T> >& x)
  4030. {
  4031. x -= T(1);
  4032. }
  4033. template<typename eT, typename T1>
  4034. inline
  4035. void
  4036. Cube_aux::set_real(Cube<eT>& out, const BaseCube<eT,T1>& X)
  4037. {
  4038. arma_extra_debug_sigprint();
  4039. const unwrap_cube<T1> tmp(X.get_ref());
  4040. const Cube<eT>& A = tmp.M;
  4041. arma_debug_assert_same_size( out, A, "Cube::set_real()" );
  4042. out = A;
  4043. }
  4044. template<typename eT, typename T1>
  4045. inline
  4046. void
  4047. Cube_aux::set_imag(Cube<eT>&, const BaseCube<eT,T1>&)
  4048. {
  4049. arma_extra_debug_sigprint();
  4050. }
  4051. template<typename T, typename T1>
  4052. inline
  4053. void
  4054. Cube_aux::set_real(Cube< std::complex<T> >& out, const BaseCube<T,T1>& X)
  4055. {
  4056. arma_extra_debug_sigprint();
  4057. typedef typename std::complex<T> eT;
  4058. const ProxyCube<T1> P(X.get_ref());
  4059. const uword local_n_rows = P.get_n_rows();
  4060. const uword local_n_cols = P.get_n_cols();
  4061. const uword local_n_slices = P.get_n_slices();
  4062. arma_debug_assert_same_size
  4063. (
  4064. out.n_rows, out.n_cols, out.n_slices,
  4065. local_n_rows, local_n_cols, local_n_slices,
  4066. "Cube::set_real()"
  4067. );
  4068. eT* out_mem = out.memptr();
  4069. if(ProxyCube<T1>::use_at == false)
  4070. {
  4071. typedef typename ProxyCube<T1>::ea_type ea_type;
  4072. ea_type A = P.get_ea();
  4073. const uword N = out.n_elem;
  4074. for(uword i=0; i<N; ++i)
  4075. {
  4076. //out_mem[i].real() = PA[i];
  4077. out_mem[i] = std::complex<T>( A[i], out_mem[i].imag() );
  4078. }
  4079. }
  4080. else
  4081. {
  4082. for(uword slice = 0; slice < local_n_slices; ++slice)
  4083. for(uword col = 0; col < local_n_cols; ++col )
  4084. for(uword row = 0; row < local_n_rows; ++row )
  4085. {
  4086. (*out_mem) = std::complex<T>( P.at(row,col,slice), (*out_mem).imag() );
  4087. out_mem++;
  4088. }
  4089. }
  4090. }
  4091. template<typename T, typename T1>
  4092. inline
  4093. void
  4094. Cube_aux::set_imag(Cube< std::complex<T> >& out, const BaseCube<T,T1>& X)
  4095. {
  4096. arma_extra_debug_sigprint();
  4097. typedef typename std::complex<T> eT;
  4098. const ProxyCube<T1> P(X.get_ref());
  4099. const uword local_n_rows = P.get_n_rows();
  4100. const uword local_n_cols = P.get_n_cols();
  4101. const uword local_n_slices = P.get_n_slices();
  4102. arma_debug_assert_same_size
  4103. (
  4104. out.n_rows, out.n_cols, out.n_slices,
  4105. local_n_rows, local_n_cols, local_n_slices,
  4106. "Cube::set_imag()"
  4107. );
  4108. eT* out_mem = out.memptr();
  4109. if(ProxyCube<T1>::use_at == false)
  4110. {
  4111. typedef typename ProxyCube<T1>::ea_type ea_type;
  4112. ea_type A = P.get_ea();
  4113. const uword N = out.n_elem;
  4114. for(uword i=0; i<N; ++i)
  4115. {
  4116. //out_mem[i].imag() = PA[i];
  4117. out_mem[i] = std::complex<T>( out_mem[i].real(), A[i] );
  4118. }
  4119. }
  4120. else
  4121. {
  4122. for(uword slice = 0; slice < local_n_slices; ++slice)
  4123. for(uword col = 0; col < local_n_cols; ++col )
  4124. for(uword row = 0; row < local_n_rows; ++row )
  4125. {
  4126. (*out_mem) = std::complex<T>( (*out_mem).real(), P.at(row,col,slice) );
  4127. out_mem++;
  4128. }
  4129. }
  4130. }
  4131. #ifdef ARMA_EXTRA_CUBE_MEAT
  4132. #include ARMA_INCFILE_WRAP(ARMA_EXTRA_CUBE_MEAT)
  4133. #endif
  4134. //! @}