numpy.cpp 186 KB

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