octonion.hpp 241 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251
  1. // boost octonion.hpp header file
  2. // (C) Copyright Hubert Holin 2001.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #ifndef BOOST_OCTONION_HPP
  8. #define BOOST_OCTONION_HPP
  9. #include <boost/math/quaternion.hpp>
  10. #include <valarray>
  11. namespace boost
  12. {
  13. namespace math
  14. {
  15. #define BOOST_OCTONION_ACCESSOR_GENERATOR(type) \
  16. type real() const \
  17. { \
  18. return(a); \
  19. } \
  20. \
  21. octonion<type> unreal() const \
  22. { \
  23. return( octonion<type>(static_cast<type>(0),b,c,d,e,f,g,h)); \
  24. } \
  25. \
  26. type R_component_1() const \
  27. { \
  28. return(a); \
  29. } \
  30. \
  31. type R_component_2() const \
  32. { \
  33. return(b); \
  34. } \
  35. \
  36. type R_component_3() const \
  37. { \
  38. return(c); \
  39. } \
  40. \
  41. type R_component_4() const \
  42. { \
  43. return(d); \
  44. } \
  45. \
  46. type R_component_5() const \
  47. { \
  48. return(e); \
  49. } \
  50. \
  51. type R_component_6() const \
  52. { \
  53. return(f); \
  54. } \
  55. \
  56. type R_component_7() const \
  57. { \
  58. return(g); \
  59. } \
  60. \
  61. type R_component_8() const \
  62. { \
  63. return(h); \
  64. } \
  65. \
  66. ::std::complex<type> C_component_1() const \
  67. { \
  68. return(::std::complex<type>(a,b)); \
  69. } \
  70. \
  71. ::std::complex<type> C_component_2() const \
  72. { \
  73. return(::std::complex<type>(c,d)); \
  74. } \
  75. \
  76. ::std::complex<type> C_component_3() const \
  77. { \
  78. return(::std::complex<type>(e,f)); \
  79. } \
  80. \
  81. ::std::complex<type> C_component_4() const \
  82. { \
  83. return(::std::complex<type>(g,h)); \
  84. } \
  85. \
  86. ::boost::math::quaternion<type> H_component_1() const \
  87. { \
  88. return(::boost::math::quaternion<type>(a,b,c,d)); \
  89. } \
  90. \
  91. ::boost::math::quaternion<type> H_component_2() const \
  92. { \
  93. return(::boost::math::quaternion<type>(e,f,g,h)); \
  94. }
  95. #define BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR(type) \
  96. template<typename X> \
  97. octonion<type> & operator = (octonion<X> const & a_affecter) \
  98. { \
  99. a = static_cast<type>(a_affecter.R_component_1()); \
  100. b = static_cast<type>(a_affecter.R_component_2()); \
  101. c = static_cast<type>(a_affecter.R_component_3()); \
  102. d = static_cast<type>(a_affecter.R_component_4()); \
  103. e = static_cast<type>(a_affecter.R_component_5()); \
  104. f = static_cast<type>(a_affecter.R_component_6()); \
  105. g = static_cast<type>(a_affecter.R_component_7()); \
  106. h = static_cast<type>(a_affecter.R_component_8()); \
  107. \
  108. return(*this); \
  109. } \
  110. \
  111. octonion<type> & operator = (octonion<type> const & a_affecter) \
  112. { \
  113. a = a_affecter.a; \
  114. b = a_affecter.b; \
  115. c = a_affecter.c; \
  116. d = a_affecter.d; \
  117. e = a_affecter.e; \
  118. f = a_affecter.f; \
  119. g = a_affecter.g; \
  120. h = a_affecter.h; \
  121. \
  122. return(*this); \
  123. } \
  124. \
  125. octonion<type> & operator = (type const & a_affecter) \
  126. { \
  127. a = a_affecter; \
  128. \
  129. b = c = d = e = f= g = h = static_cast<type>(0); \
  130. \
  131. return(*this); \
  132. } \
  133. \
  134. octonion<type> & operator = (::std::complex<type> const & a_affecter) \
  135. { \
  136. a = a_affecter.real(); \
  137. b = a_affecter.imag(); \
  138. \
  139. c = d = e = f = g = h = static_cast<type>(0); \
  140. \
  141. return(*this); \
  142. } \
  143. \
  144. octonion<type> & operator = (::boost::math::quaternion<type> const & a_affecter) \
  145. { \
  146. a = a_affecter.R_component_1(); \
  147. b = a_affecter.R_component_2(); \
  148. c = a_affecter.R_component_3(); \
  149. d = a_affecter.R_component_4(); \
  150. \
  151. e = f = g = h = static_cast<type>(0); \
  152. \
  153. return(*this); \
  154. }
  155. #define BOOST_OCTONION_MEMBER_DATA_GENERATOR(type) \
  156. type a; \
  157. type b; \
  158. type c; \
  159. type d; \
  160. type e; \
  161. type f; \
  162. type g; \
  163. type h; \
  164. template<typename T>
  165. class octonion
  166. {
  167. public:
  168. typedef T value_type;
  169. // constructor for O seen as R^8
  170. // (also default constructor)
  171. explicit octonion( T const & requested_a = T(),
  172. T const & requested_b = T(),
  173. T const & requested_c = T(),
  174. T const & requested_d = T(),
  175. T const & requested_e = T(),
  176. T const & requested_f = T(),
  177. T const & requested_g = T(),
  178. T const & requested_h = T())
  179. : a(requested_a),
  180. b(requested_b),
  181. c(requested_c),
  182. d(requested_d),
  183. e(requested_e),
  184. f(requested_f),
  185. g(requested_g),
  186. h(requested_h)
  187. {
  188. // nothing to do!
  189. }
  190. // constructor for H seen as C^4
  191. explicit octonion( ::std::complex<T> const & z0,
  192. ::std::complex<T> const & z1 = ::std::complex<T>(),
  193. ::std::complex<T> const & z2 = ::std::complex<T>(),
  194. ::std::complex<T> const & z3 = ::std::complex<T>())
  195. : a(z0.real()),
  196. b(z0.imag()),
  197. c(z1.real()),
  198. d(z1.imag()),
  199. e(z2.real()),
  200. f(z2.imag()),
  201. g(z3.real()),
  202. h(z3.imag())
  203. {
  204. // nothing to do!
  205. }
  206. // constructor for O seen as H^2
  207. explicit octonion( ::boost::math::quaternion<T> const & q0,
  208. ::boost::math::quaternion<T> const & q1 = ::boost::math::quaternion<T>())
  209. : a(q0.R_component_1()),
  210. b(q0.R_component_2()),
  211. c(q0.R_component_3()),
  212. d(q0.R_component_4()),
  213. e(q1.R_component_1()),
  214. f(q1.R_component_2()),
  215. g(q1.R_component_3()),
  216. h(q1.R_component_4())
  217. {
  218. // nothing to do!
  219. }
  220. // UNtemplated copy constructor
  221. // (this is taken care of by the compiler itself)
  222. // templated copy constructor
  223. template<typename X>
  224. explicit octonion(octonion<X> const & a_recopier)
  225. : a(static_cast<T>(a_recopier.R_component_1())),
  226. b(static_cast<T>(a_recopier.R_component_2())),
  227. c(static_cast<T>(a_recopier.R_component_3())),
  228. d(static_cast<T>(a_recopier.R_component_4())),
  229. e(static_cast<T>(a_recopier.R_component_5())),
  230. f(static_cast<T>(a_recopier.R_component_6())),
  231. g(static_cast<T>(a_recopier.R_component_7())),
  232. h(static_cast<T>(a_recopier.R_component_8()))
  233. {
  234. // nothing to do!
  235. }
  236. // destructor
  237. // (this is taken care of by the compiler itself)
  238. // accessors
  239. //
  240. // Note: Like complex number, octonions do have a meaningful notion of "real part",
  241. // but unlike them there is no meaningful notion of "imaginary part".
  242. // Instead there is an "unreal part" which itself is an octonion, and usually
  243. // nothing simpler (as opposed to the complex number case).
  244. // However, for practicallity, there are accessors for the other components
  245. // (these are necessary for the templated copy constructor, for instance).
  246. BOOST_OCTONION_ACCESSOR_GENERATOR(T)
  247. // assignment operators
  248. BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR(T)
  249. // other assignment-related operators
  250. //
  251. // NOTE: Octonion multiplication is *NOT* commutative;
  252. // symbolically, "q *= rhs;" means "q = q * rhs;"
  253. // and "q /= rhs;" means "q = q * inverse_of(rhs);";
  254. // octonion multiplication is also *NOT* associative
  255. octonion<T> & operator += (T const & rhs)
  256. {
  257. T at = a + rhs; // exception guard
  258. a = at;
  259. return(*this);
  260. }
  261. octonion<T> & operator += (::std::complex<T> const & rhs)
  262. {
  263. T at = a + rhs.real(); // exception guard
  264. T bt = b + rhs.imag(); // exception guard
  265. a = at;
  266. b = bt;
  267. return(*this);
  268. }
  269. octonion<T> & operator += (::boost::math::quaternion<T> const & rhs)
  270. {
  271. T at = a + rhs.R_component_1(); // exception guard
  272. T bt = b + rhs.R_component_2(); // exception guard
  273. T ct = c + rhs.R_component_3(); // exception guard
  274. T dt = d + rhs.R_component_4(); // exception guard
  275. a = at;
  276. b = bt;
  277. c = ct;
  278. d = dt;
  279. return(*this);
  280. }
  281. template<typename X>
  282. octonion<T> & operator += (octonion<X> const & rhs)
  283. {
  284. T at = a + static_cast<T>(rhs.R_component_1()); // exception guard
  285. T bt = b + static_cast<T>(rhs.R_component_2()); // exception guard
  286. T ct = c + static_cast<T>(rhs.R_component_3()); // exception guard
  287. T dt = d + static_cast<T>(rhs.R_component_4()); // exception guard
  288. T et = e + static_cast<T>(rhs.R_component_5()); // exception guard
  289. T ft = f + static_cast<T>(rhs.R_component_6()); // exception guard
  290. T gt = g + static_cast<T>(rhs.R_component_7()); // exception guard
  291. T ht = h + static_cast<T>(rhs.R_component_8()); // exception guard
  292. a = at;
  293. b = bt;
  294. c = ct;
  295. d = dt;
  296. e = et;
  297. f = ft;
  298. g = gt;
  299. h = ht;
  300. return(*this);
  301. }
  302. octonion<T> & operator -= (T const & rhs)
  303. {
  304. T at = a - rhs; // exception guard
  305. a = at;
  306. return(*this);
  307. }
  308. octonion<T> & operator -= (::std::complex<T> const & rhs)
  309. {
  310. T at = a - rhs.real(); // exception guard
  311. T bt = b - rhs.imag(); // exception guard
  312. a = at;
  313. b = bt;
  314. return(*this);
  315. }
  316. octonion<T> & operator -= (::boost::math::quaternion<T> const & rhs)
  317. {
  318. T at = a - rhs.R_component_1(); // exception guard
  319. T bt = b - rhs.R_component_2(); // exception guard
  320. T ct = c - rhs.R_component_3(); // exception guard
  321. T dt = d - rhs.R_component_4(); // exception guard
  322. a = at;
  323. b = bt;
  324. c = ct;
  325. d = dt;
  326. return(*this);
  327. }
  328. template<typename X>
  329. octonion<T> & operator -= (octonion<X> const & rhs)
  330. {
  331. T at = a - static_cast<T>(rhs.R_component_1()); // exception guard
  332. T bt = b - static_cast<T>(rhs.R_component_2()); // exception guard
  333. T ct = c - static_cast<T>(rhs.R_component_3()); // exception guard
  334. T dt = d - static_cast<T>(rhs.R_component_4()); // exception guard
  335. T et = e - static_cast<T>(rhs.R_component_5()); // exception guard
  336. T ft = f - static_cast<T>(rhs.R_component_6()); // exception guard
  337. T gt = g - static_cast<T>(rhs.R_component_7()); // exception guard
  338. T ht = h - static_cast<T>(rhs.R_component_8()); // exception guard
  339. a = at;
  340. b = bt;
  341. c = ct;
  342. d = dt;
  343. e = et;
  344. f = ft;
  345. g = gt;
  346. h = ht;
  347. return(*this);
  348. }
  349. octonion<T> & operator *= (T const & rhs)
  350. {
  351. T at = a * rhs; // exception guard
  352. T bt = b * rhs; // exception guard
  353. T ct = c * rhs; // exception guard
  354. T dt = d * rhs; // exception guard
  355. T et = e * rhs; // exception guard
  356. T ft = f * rhs; // exception guard
  357. T gt = g * rhs; // exception guard
  358. T ht = h * rhs; // exception guard
  359. a = at;
  360. b = bt;
  361. c = ct;
  362. d = dt;
  363. e = et;
  364. f = ft;
  365. g = gt;
  366. h = ht;
  367. return(*this);
  368. }
  369. octonion<T> & operator *= (::std::complex<T> const & rhs)
  370. {
  371. T ar = rhs.real();
  372. T br = rhs.imag();
  373. T at = +a*ar-b*br;
  374. T bt = +a*br+b*ar;
  375. T ct = +c*ar+d*br;
  376. T dt = -c*br+d*ar;
  377. T et = +e*ar+f*br;
  378. T ft = -e*br+f*ar;
  379. T gt = +g*ar-h*br;
  380. T ht = +g*br+h*ar;
  381. a = at;
  382. b = bt;
  383. c = ct;
  384. d = dt;
  385. e = et;
  386. f = ft;
  387. g = gt;
  388. h = ht;
  389. return(*this);
  390. }
  391. octonion<T> & operator *= (::boost::math::quaternion<T> const & rhs)
  392. {
  393. T ar = rhs.R_component_1();
  394. T br = rhs.R_component_2();
  395. T cr = rhs.R_component_2();
  396. T dr = rhs.R_component_2();
  397. T at = +a*ar-b*br-c*cr-d*dr;
  398. T bt = +a*br+b*ar+c*dr-d*cr;
  399. T ct = +a*cr-b*dr+c*ar+d*br;
  400. T dt = +a*dr+b*cr-c*br+d*ar;
  401. T et = +e*ar+f*br+g*cr+h*dr;
  402. T ft = -e*br+f*ar-g*dr+h*cr;
  403. T gt = -e*cr+f*dr+g*ar-h*br;
  404. T ht = -e*dr-f*cr+g*br+h*ar;
  405. a = at;
  406. b = bt;
  407. c = ct;
  408. d = dt;
  409. e = et;
  410. f = ft;
  411. g = gt;
  412. h = ht;
  413. return(*this);
  414. }
  415. template<typename X>
  416. octonion<T> & operator *= (octonion<X> const & rhs)
  417. {
  418. T ar = static_cast<T>(rhs.R_component_1());
  419. T br = static_cast<T>(rhs.R_component_2());
  420. T cr = static_cast<T>(rhs.R_component_3());
  421. T dr = static_cast<T>(rhs.R_component_4());
  422. T er = static_cast<T>(rhs.R_component_5());
  423. T fr = static_cast<T>(rhs.R_component_6());
  424. T gr = static_cast<T>(rhs.R_component_7());
  425. T hr = static_cast<T>(rhs.R_component_8());
  426. T at = +a*ar-b*br-c*cr-d*dr-e*er-f*fr-g*gr-h*hr;
  427. T bt = +a*br+b*ar+c*dr-d*cr+e*fr-f*er-g*hr+h*gr;
  428. T ct = +a*cr-b*dr+c*ar+d*br+e*gr+f*hr-g*er-h*fr;
  429. T dt = +a*dr+b*cr-c*br+d*ar+e*hr-f*gr+g*fr-h*er;
  430. T et = +a*er-b*fr-c*gr-d*hr+e*ar+f*br+g*cr+h*dr;
  431. T ft = +a*fr+b*er-c*hr+d*gr-e*br+f*ar-g*dr+h*cr;
  432. T gt = +a*gr+b*hr+c*er-d*fr-e*cr+f*dr+g*ar-h*br;
  433. T ht = +a*hr-b*gr+c*fr+d*er-e*dr-f*cr+g*br+h*ar;
  434. a = at;
  435. b = bt;
  436. c = ct;
  437. d = dt;
  438. e = et;
  439. f = ft;
  440. g = gt;
  441. h = ht;
  442. return(*this);
  443. }
  444. octonion<T> & operator /= (T const & rhs)
  445. {
  446. T at = a / rhs; // exception guard
  447. T bt = b / rhs; // exception guard
  448. T ct = c / rhs; // exception guard
  449. T dt = d / rhs; // exception guard
  450. T et = e / rhs; // exception guard
  451. T ft = f / rhs; // exception guard
  452. T gt = g / rhs; // exception guard
  453. T ht = h / rhs; // exception guard
  454. a = at;
  455. b = bt;
  456. c = ct;
  457. d = dt;
  458. e = et;
  459. f = ft;
  460. g = gt;
  461. h = ht;
  462. return(*this);
  463. }
  464. octonion<T> & operator /= (::std::complex<T> const & rhs)
  465. {
  466. T ar = rhs.real();
  467. T br = rhs.imag();
  468. T denominator = ar*ar+br*br;
  469. T at = (+a*ar-b*br)/denominator;
  470. T bt = (-a*br+b*ar)/denominator;
  471. T ct = (+c*ar-d*br)/denominator;
  472. T dt = (+c*br+d*ar)/denominator;
  473. T et = (+e*ar-f*br)/denominator;
  474. T ft = (+e*br+f*ar)/denominator;
  475. T gt = (+g*ar+h*br)/denominator;
  476. T ht = (+g*br+h*ar)/denominator;
  477. a = at;
  478. b = bt;
  479. c = ct;
  480. d = dt;
  481. e = et;
  482. f = ft;
  483. g = gt;
  484. h = ht;
  485. return(*this);
  486. }
  487. octonion<T> & operator /= (::boost::math::quaternion<T> const & rhs)
  488. {
  489. T ar = rhs.R_component_1();
  490. T br = rhs.R_component_2();
  491. T cr = rhs.R_component_2();
  492. T dr = rhs.R_component_2();
  493. T denominator = ar*ar+br*br+cr*cr+dr*dr;
  494. T at = (+a*ar+b*br+c*cr+d*dr)/denominator;
  495. T bt = (-a*br+b*ar-c*dr+d*cr)/denominator;
  496. T ct = (-a*cr+b*dr+c*ar-d*br)/denominator;
  497. T dt = (-a*dr-b*cr+c*br+d*ar)/denominator;
  498. T et = (+e*ar-f*br-g*cr-h*dr)/denominator;
  499. T ft = (+e*br+f*ar+g*dr-h*cr)/denominator;
  500. T gt = (+e*cr-f*dr+g*ar+h*br)/denominator;
  501. T ht = (+e*dr+f*cr-g*br+h*ar)/denominator;
  502. a = at;
  503. b = bt;
  504. c = ct;
  505. d = dt;
  506. e = et;
  507. f = ft;
  508. g = gt;
  509. h = ht;
  510. return(*this);
  511. }
  512. template<typename X>
  513. octonion<T> & operator /= (octonion<X> const & rhs)
  514. {
  515. T ar = static_cast<T>(rhs.R_component_1());
  516. T br = static_cast<T>(rhs.R_component_2());
  517. T cr = static_cast<T>(rhs.R_component_3());
  518. T dr = static_cast<T>(rhs.R_component_4());
  519. T er = static_cast<T>(rhs.R_component_5());
  520. T fr = static_cast<T>(rhs.R_component_6());
  521. T gr = static_cast<T>(rhs.R_component_7());
  522. T hr = static_cast<T>(rhs.R_component_8());
  523. T denominator = ar*ar+br*br+cr*cr+dr*dr+er*er+fr*fr+gr*gr+hr*hr;
  524. T at = (+a*ar+b*br+c*cr+d*dr+e*er+f*fr+g*gr+h*hr)/denominator;
  525. T bt = (-a*br+b*ar-c*dr+d*cr-e*fr+f*er+g*hr-h*gr)/denominator;
  526. T ct = (-a*cr+b*dr+c*ar-d*br-e*gr-f*hr+g*er+h*fr)/denominator;
  527. T dt = (-a*dr-b*cr+c*br+d*ar-e*hr+f*gr-g*fr+h*er)/denominator;
  528. T et = (-a*er+b*fr+c*gr+d*hr+e*ar-f*br-g*cr-h*dr)/denominator;
  529. T ft = (-a*fr-b*er+c*hr-d*gr+e*br+f*ar+g*dr-h*cr)/denominator;
  530. T gt = (-a*gr-b*hr-c*er+d*fr+e*cr-f*dr+g*ar+h*br)/denominator;
  531. T ht = (-a*hr+b*gr-c*fr-d*er+e*dr+f*cr-g*br+h*ar)/denominator;
  532. a = at;
  533. b = bt;
  534. c = ct;
  535. d = dt;
  536. e = et;
  537. f = ft;
  538. g = gt;
  539. h = ht;
  540. return(*this);
  541. }
  542. protected:
  543. BOOST_OCTONION_MEMBER_DATA_GENERATOR(T)
  544. private:
  545. };
  546. // declaration of octonion specialization
  547. template<> class octonion<float>;
  548. template<> class octonion<double>;
  549. template<> class octonion<long double>;
  550. // helper templates for converting copy constructors (declaration)
  551. namespace detail
  552. {
  553. template< typename T,
  554. typename U
  555. >
  556. octonion<T> octonion_type_converter(octonion<U> const & rhs);
  557. }
  558. // implementation of octonion specialization
  559. #define BOOST_OCTONION_CONSTRUCTOR_GENERATOR(type) \
  560. explicit octonion( type const & requested_a = static_cast<type>(0), \
  561. type const & requested_b = static_cast<type>(0), \
  562. type const & requested_c = static_cast<type>(0), \
  563. type const & requested_d = static_cast<type>(0), \
  564. type const & requested_e = static_cast<type>(0), \
  565. type const & requested_f = static_cast<type>(0), \
  566. type const & requested_g = static_cast<type>(0), \
  567. type const & requested_h = static_cast<type>(0)) \
  568. : a(requested_a), \
  569. b(requested_b), \
  570. c(requested_c), \
  571. d(requested_d), \
  572. e(requested_e), \
  573. f(requested_f), \
  574. g(requested_g), \
  575. h(requested_h) \
  576. { \
  577. } \
  578. \
  579. explicit octonion( ::std::complex<type> const & z0, \
  580. ::std::complex<type> const & z1 = ::std::complex<type>(), \
  581. ::std::complex<type> const & z2 = ::std::complex<type>(), \
  582. ::std::complex<type> const & z3 = ::std::complex<type>()) \
  583. : a(z0.real()), \
  584. b(z0.imag()), \
  585. c(z1.real()), \
  586. d(z1.imag()), \
  587. e(z2.real()), \
  588. f(z2.imag()), \
  589. g(z3.real()), \
  590. h(z3.imag()) \
  591. { \
  592. } \
  593. \
  594. explicit octonion( ::boost::math::quaternion<type> const & q0, \
  595. ::boost::math::quaternion<type> const & q1 = ::boost::math::quaternion<type>()) \
  596. : a(q0.R_component_1()), \
  597. b(q0.R_component_2()), \
  598. c(q0.R_component_3()), \
  599. d(q0.R_component_4()), \
  600. e(q1.R_component_1()), \
  601. f(q1.R_component_2()), \
  602. g(q1.R_component_3()), \
  603. h(q1.R_component_4()) \
  604. { \
  605. }
  606. #define BOOST_OCTONION_MEMBER_ADD_GENERATOR_1(type) \
  607. octonion<type> & operator += (type const & rhs) \
  608. { \
  609. a += rhs; \
  610. \
  611. return(*this); \
  612. }
  613. #define BOOST_OCTONION_MEMBER_ADD_GENERATOR_2(type) \
  614. octonion<type> & operator += (::std::complex<type> const & rhs) \
  615. { \
  616. a += rhs.real(); \
  617. b += rhs.imag(); \
  618. \
  619. return(*this); \
  620. }
  621. #define BOOST_OCTONION_MEMBER_ADD_GENERATOR_3(type) \
  622. octonion<type> & operator += (::boost::math::quaternion<type> const & rhs) \
  623. { \
  624. a += rhs.R_component_1(); \
  625. b += rhs.R_component_2(); \
  626. c += rhs.R_component_3(); \
  627. d += rhs.R_component_4(); \
  628. \
  629. return(*this); \
  630. }
  631. #define BOOST_OCTONION_MEMBER_ADD_GENERATOR_4(type) \
  632. template<typename X> \
  633. octonion<type> & operator += (octonion<X> const & rhs) \
  634. { \
  635. a += static_cast<type>(rhs.R_component_1()); \
  636. b += static_cast<type>(rhs.R_component_2()); \
  637. c += static_cast<type>(rhs.R_component_3()); \
  638. d += static_cast<type>(rhs.R_component_4()); \
  639. e += static_cast<type>(rhs.R_component_5()); \
  640. f += static_cast<type>(rhs.R_component_6()); \
  641. g += static_cast<type>(rhs.R_component_7()); \
  642. h += static_cast<type>(rhs.R_component_8()); \
  643. \
  644. return(*this); \
  645. }
  646. #define BOOST_OCTONION_MEMBER_SUB_GENERATOR_1(type) \
  647. octonion<type> & operator -= (type const & rhs) \
  648. { \
  649. a -= rhs; \
  650. \
  651. return(*this); \
  652. }
  653. #define BOOST_OCTONION_MEMBER_SUB_GENERATOR_2(type) \
  654. octonion<type> & operator -= (::std::complex<type> const & rhs) \
  655. { \
  656. a -= rhs.real(); \
  657. b -= rhs.imag(); \
  658. \
  659. return(*this); \
  660. }
  661. #define BOOST_OCTONION_MEMBER_SUB_GENERATOR_3(type) \
  662. octonion<type> & operator -= (::boost::math::quaternion<type> const & rhs) \
  663. { \
  664. a -= rhs.R_component_1(); \
  665. b -= rhs.R_component_2(); \
  666. c -= rhs.R_component_3(); \
  667. d -= rhs.R_component_4(); \
  668. \
  669. return(*this); \
  670. }
  671. #define BOOST_OCTONION_MEMBER_SUB_GENERATOR_4(type) \
  672. template<typename X> \
  673. octonion<type> & operator -= (octonion<X> const & rhs) \
  674. { \
  675. a -= static_cast<type>(rhs.R_component_1()); \
  676. b -= static_cast<type>(rhs.R_component_2()); \
  677. c -= static_cast<type>(rhs.R_component_3()); \
  678. d -= static_cast<type>(rhs.R_component_4()); \
  679. e -= static_cast<type>(rhs.R_component_5()); \
  680. f -= static_cast<type>(rhs.R_component_6()); \
  681. g -= static_cast<type>(rhs.R_component_7()); \
  682. h -= static_cast<type>(rhs.R_component_8()); \
  683. \
  684. return(*this); \
  685. }
  686. #define BOOST_OCTONION_MEMBER_MUL_GENERATOR_1(type) \
  687. octonion<type> & operator *= (type const & rhs) \
  688. { \
  689. a *= rhs; \
  690. b *= rhs; \
  691. c *= rhs; \
  692. d *= rhs; \
  693. e *= rhs; \
  694. f *= rhs; \
  695. g *= rhs; \
  696. h *= rhs; \
  697. \
  698. return(*this); \
  699. }
  700. #define BOOST_OCTONION_MEMBER_MUL_GENERATOR_2(type) \
  701. octonion<type> & operator *= (::std::complex<type> const & rhs) \
  702. { \
  703. type ar = rhs.real(); \
  704. type br = rhs.imag(); \
  705. \
  706. type at = +a*ar-b*br; \
  707. type bt = +a*br+b*ar; \
  708. type ct = +c*ar+d*br; \
  709. type dt = -c*br+d*ar; \
  710. type et = +e*ar+f*br; \
  711. type ft = -e*br+f*ar; \
  712. type gt = +g*ar-h*br; \
  713. type ht = +g*br+h*ar; \
  714. \
  715. a = at; \
  716. b = bt; \
  717. c = ct; \
  718. d = dt; \
  719. e = et; \
  720. f = ft; \
  721. g = gt; \
  722. h = ht; \
  723. \
  724. return(*this); \
  725. }
  726. #define BOOST_OCTONION_MEMBER_MUL_GENERATOR_3(type) \
  727. octonion<type> & operator *= (::boost::math::quaternion<type> const & rhs) \
  728. { \
  729. type ar = rhs.R_component_1(); \
  730. type br = rhs.R_component_2(); \
  731. type cr = rhs.R_component_2(); \
  732. type dr = rhs.R_component_2(); \
  733. \
  734. type at = +a*ar-b*br-c*cr-d*dr; \
  735. type bt = +a*br+b*ar+c*dr-d*cr; \
  736. type ct = +a*cr-b*dr+c*ar+d*br; \
  737. type dt = +a*dr+b*cr-c*br+d*ar; \
  738. type et = +e*ar+f*br+g*cr+h*dr; \
  739. type ft = -e*br+f*ar-g*dr+h*cr; \
  740. type gt = -e*cr+f*dr+g*ar-h*br; \
  741. type ht = -e*dr-f*cr+g*br+h*ar; \
  742. \
  743. a = at; \
  744. b = bt; \
  745. c = ct; \
  746. d = dt; \
  747. e = et; \
  748. f = ft; \
  749. g = gt; \
  750. h = ht; \
  751. \
  752. return(*this); \
  753. }
  754. #define BOOST_OCTONION_MEMBER_MUL_GENERATOR_4(type) \
  755. template<typename X> \
  756. octonion<type> & operator *= (octonion<X> const & rhs) \
  757. { \
  758. type ar = static_cast<type>(rhs.R_component_1()); \
  759. type br = static_cast<type>(rhs.R_component_2()); \
  760. type cr = static_cast<type>(rhs.R_component_3()); \
  761. type dr = static_cast<type>(rhs.R_component_4()); \
  762. type er = static_cast<type>(rhs.R_component_5()); \
  763. type fr = static_cast<type>(rhs.R_component_6()); \
  764. type gr = static_cast<type>(rhs.R_component_7()); \
  765. type hr = static_cast<type>(rhs.R_component_8()); \
  766. \
  767. type at = +a*ar-b*br-c*cr-d*dr-e*er-f*fr-g*gr-h*hr; \
  768. type bt = +a*br+b*ar+c*dr-d*cr+e*fr-f*er-g*hr+h*gr; \
  769. type ct = +a*cr-b*dr+c*ar+d*br+e*gr+f*hr-g*er-h*fr; \
  770. type dt = +a*dr+b*cr-c*br+d*ar+e*hr-f*gr+g*fr-h*er; \
  771. type et = +a*er-b*fr-c*gr-d*hr+e*ar+f*br+g*cr+h*dr; \
  772. type ft = +a*fr+b*er-c*hr+d*gr-e*br+f*ar-g*dr+h*cr; \
  773. type gt = +a*gr+b*hr+c*er-d*fr-e*cr+f*dr+g*ar-h*br; \
  774. type ht = +a*hr-b*gr+c*fr+d*er-e*dr-f*cr+g*br+h*ar; \
  775. \
  776. a = at; \
  777. b = bt; \
  778. c = ct; \
  779. d = dt; \
  780. e = et; \
  781. f = ft; \
  782. g = gt; \
  783. h = ht; \
  784. \
  785. return(*this); \
  786. }
  787. // There is quite a lot of repetition in the code below. This is intentional.
  788. // The last conditional block is the normal form, and the others merely
  789. // consist of workarounds for various compiler deficiencies. Hopefuly, when
  790. // more compilers are conformant and we can retire support for those that are
  791. // not, we will be able to remove the clutter. This is makes the situation
  792. // (painfully) explicit.
  793. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_1(type) \
  794. octonion<type> & operator /= (type const & rhs) \
  795. { \
  796. a /= rhs; \
  797. b /= rhs; \
  798. c /= rhs; \
  799. d /= rhs; \
  800. \
  801. return(*this); \
  802. }
  803. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  804. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_2(type) \
  805. octonion<type> & operator /= (::std::complex<type> const & rhs) \
  806. { \
  807. using ::std::valarray; \
  808. using ::std::abs; \
  809. \
  810. valarray<type> tr(2); \
  811. \
  812. tr[0] = rhs.real(); \
  813. tr[1] = rhs.imag(); \
  814. \
  815. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  816. \
  817. tr *= mixam; \
  818. \
  819. valarray<type> tt(8); \
  820. \
  821. tt[0] = +a*tr[0]-b*tr[1]; \
  822. tt[1] = -a*tr[1]+b*tr[0]; \
  823. tt[2] = +c*tr[0]-d*tr[1]; \
  824. tt[3] = +c*tr[1]+d*tr[0]; \
  825. tt[4] = +e*tr[0]-f*tr[1]; \
  826. tt[5] = +e*tr[1]+f*tr[0]; \
  827. tt[6] = +g*tr[0]+h*tr[1]; \
  828. tt[7] = +g*tr[1]+h*tr[0]; \
  829. \
  830. tr *= tr; \
  831. \
  832. tt *= (mixam/tr.sum()); \
  833. \
  834. a = tt[0]; \
  835. b = tt[1]; \
  836. c = tt[2]; \
  837. d = tt[3]; \
  838. e = tt[4]; \
  839. f = tt[5]; \
  840. g = tt[6]; \
  841. h = tt[7]; \
  842. \
  843. return(*this); \
  844. }
  845. #else
  846. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_2(type) \
  847. octonion<type> & operator /= (::std::complex<type> const & rhs) \
  848. { \
  849. using ::std::valarray; \
  850. \
  851. valarray<type> tr(2); \
  852. \
  853. tr[0] = rhs.real(); \
  854. tr[1] = rhs.imag(); \
  855. \
  856. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  857. \
  858. tr *= mixam; \
  859. \
  860. valarray<type> tt(8); \
  861. \
  862. tt[0] = +a*tr[0]-b*tr[1]; \
  863. tt[1] = -a*tr[1]+b*tr[0]; \
  864. tt[2] = +c*tr[0]-d*tr[1]; \
  865. tt[3] = +c*tr[1]+d*tr[0]; \
  866. tt[4] = +e*tr[0]-f*tr[1]; \
  867. tt[5] = +e*tr[1]+f*tr[0]; \
  868. tt[6] = +g*tr[0]+h*tr[1]; \
  869. tt[7] = +g*tr[1]+h*tr[0]; \
  870. \
  871. tr *= tr; \
  872. \
  873. tt *= (mixam/tr.sum()); \
  874. \
  875. a = tt[0]; \
  876. b = tt[1]; \
  877. c = tt[2]; \
  878. d = tt[3]; \
  879. e = tt[4]; \
  880. f = tt[5]; \
  881. g = tt[6]; \
  882. h = tt[7]; \
  883. \
  884. return(*this); \
  885. }
  886. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  887. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  888. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_3(type) \
  889. octonion<type> & operator /= (::boost::math::quaternion<type> const & rhs) \
  890. { \
  891. using ::std::valarray; \
  892. using ::std::abs; \
  893. \
  894. valarray<type> tr(4); \
  895. \
  896. tr[0] = static_cast<type>(rhs.R_component_1()); \
  897. tr[1] = static_cast<type>(rhs.R_component_2()); \
  898. tr[2] = static_cast<type>(rhs.R_component_3()); \
  899. tr[3] = static_cast<type>(rhs.R_component_4()); \
  900. \
  901. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  902. \
  903. tr *= mixam; \
  904. \
  905. valarray<type> tt(8); \
  906. \
  907. tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \
  908. tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \
  909. tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \
  910. tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \
  911. tt[4] = +e*tr[0]-f*tr[1]-g*tr[2]-h*tr[3]; \
  912. tt[5] = +e*tr[1]+f*tr[0]+g*tr[3]-h*tr[2]; \
  913. tt[6] = +e*tr[2]-f*tr[3]+g*tr[0]+h*tr[1]; \
  914. tt[7] = +e*tr[3]+f*tr[2]-g*tr[1]+h*tr[0]; \
  915. \
  916. tr *= tr; \
  917. \
  918. tt *= (mixam/tr.sum()); \
  919. \
  920. a = tt[0]; \
  921. b = tt[1]; \
  922. c = tt[2]; \
  923. d = tt[3]; \
  924. e = tt[4]; \
  925. f = tt[5]; \
  926. g = tt[6]; \
  927. h = tt[7]; \
  928. \
  929. return(*this); \
  930. }
  931. #else
  932. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_3(type) \
  933. octonion<type> & operator /= (::boost::math::quaternion<type> const & rhs) \
  934. { \
  935. using ::std::valarray; \
  936. \
  937. valarray<type> tr(4); \
  938. \
  939. tr[0] = static_cast<type>(rhs.R_component_1()); \
  940. tr[1] = static_cast<type>(rhs.R_component_2()); \
  941. tr[2] = static_cast<type>(rhs.R_component_3()); \
  942. tr[3] = static_cast<type>(rhs.R_component_4()); \
  943. \
  944. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  945. \
  946. tr *= mixam; \
  947. \
  948. valarray<type> tt(8); \
  949. \
  950. tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \
  951. tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \
  952. tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \
  953. tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \
  954. tt[4] = +e*tr[0]-f*tr[1]-g*tr[2]-h*tr[3]; \
  955. tt[5] = +e*tr[1]+f*tr[0]+g*tr[3]-h*tr[2]; \
  956. tt[6] = +e*tr[2]-f*tr[3]+g*tr[0]+h*tr[1]; \
  957. tt[7] = +e*tr[3]+f*tr[2]-g*tr[1]+h*tr[0]; \
  958. \
  959. tr *= tr; \
  960. \
  961. tt *= (mixam/tr.sum()); \
  962. \
  963. a = tt[0]; \
  964. b = tt[1]; \
  965. c = tt[2]; \
  966. d = tt[3]; \
  967. e = tt[4]; \
  968. f = tt[5]; \
  969. g = tt[6]; \
  970. h = tt[7]; \
  971. \
  972. return(*this); \
  973. }
  974. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  975. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  976. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_4(type) \
  977. template<typename X> \
  978. octonion<type> & operator /= (octonion<X> const & rhs) \
  979. { \
  980. using ::std::valarray; \
  981. using ::std::abs; \
  982. \
  983. valarray<type> tr(8); \
  984. \
  985. tr[0] = static_cast<type>(rhs.R_component_1()); \
  986. tr[1] = static_cast<type>(rhs.R_component_2()); \
  987. tr[2] = static_cast<type>(rhs.R_component_3()); \
  988. tr[3] = static_cast<type>(rhs.R_component_4()); \
  989. tr[4] = static_cast<type>(rhs.R_component_5()); \
  990. tr[5] = static_cast<type>(rhs.R_component_6()); \
  991. tr[6] = static_cast<type>(rhs.R_component_7()); \
  992. tr[7] = static_cast<type>(rhs.R_component_8()); \
  993. \
  994. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  995. \
  996. tr *= mixam; \
  997. \
  998. valarray<type> tt(8); \
  999. \
  1000. tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]+e*tr[4]+f*tr[5]+g*tr[6]+h*tr[7]; \
  1001. tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]-e*tr[5]+f*tr[4]+g*tr[7]-h*tr[6]; \
  1002. tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]-e*tr[6]-f*tr[7]+g*tr[4]+h*tr[5]; \
  1003. tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]-e*tr[7]+f*tr[6]-g*tr[5]+h*tr[4]; \
  1004. tt[4] = -a*tr[4]+b*tr[5]+c*tr[6]+d*tr[7]+e*tr[0]-f*tr[1]-g*tr[2]-h*tr[3]; \
  1005. tt[5] = -a*tr[5]-b*tr[4]+c*tr[7]-d*tr[6]+e*tr[1]+f*tr[0]+g*tr[3]-h*tr[2]; \
  1006. tt[6] = -a*tr[6]-b*tr[7]-c*tr[4]+d*tr[5]+e*tr[2]-f*tr[3]+g*tr[0]+h*tr[1]; \
  1007. tt[7] = -a*tr[7]+b*tr[6]-c*tr[5]-d*tr[4]+e*tr[3]+f*tr[2]-g*tr[1]+h*tr[0]; \
  1008. \
  1009. tr *= tr; \
  1010. \
  1011. tt *= (mixam/tr.sum()); \
  1012. \
  1013. a = tt[0]; \
  1014. b = tt[1]; \
  1015. c = tt[2]; \
  1016. d = tt[3]; \
  1017. e = tt[4]; \
  1018. f = tt[5]; \
  1019. g = tt[6]; \
  1020. h = tt[7]; \
  1021. \
  1022. return(*this); \
  1023. }
  1024. #else
  1025. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR_4(type) \
  1026. template<typename X> \
  1027. octonion<type> & operator /= (octonion<X> const & rhs) \
  1028. { \
  1029. using ::std::valarray; \
  1030. \
  1031. valarray<type> tr(8); \
  1032. \
  1033. tr[0] = static_cast<type>(rhs.R_component_1()); \
  1034. tr[1] = static_cast<type>(rhs.R_component_2()); \
  1035. tr[2] = static_cast<type>(rhs.R_component_3()); \
  1036. tr[3] = static_cast<type>(rhs.R_component_4()); \
  1037. tr[4] = static_cast<type>(rhs.R_component_5()); \
  1038. tr[5] = static_cast<type>(rhs.R_component_6()); \
  1039. tr[6] = static_cast<type>(rhs.R_component_7()); \
  1040. tr[7] = static_cast<type>(rhs.R_component_8()); \
  1041. \
  1042. type mixam = static_cast<type>(1)/(abs(tr).max)(); \
  1043. \
  1044. tr *= mixam; \
  1045. \
  1046. valarray<type> tt(8); \
  1047. \
  1048. tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]+e*tr[4]+f*tr[5]+g*tr[6]+h*tr[7]; \
  1049. tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]-e*tr[5]+f*tr[4]+g*tr[7]-h*tr[6]; \
  1050. tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]-e*tr[6]-f*tr[7]+g*tr[4]+h*tr[5]; \
  1051. tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]-e*tr[7]+f*tr[6]-g*tr[5]+h*tr[4]; \
  1052. tt[4] = -a*tr[4]+b*tr[5]+c*tr[6]+d*tr[7]+e*tr[0]-f*tr[1]-g*tr[2]-h*tr[3]; \
  1053. tt[5] = -a*tr[5]-b*tr[4]+c*tr[7]-d*tr[6]+e*tr[1]+f*tr[0]+g*tr[3]-h*tr[2]; \
  1054. tt[6] = -a*tr[6]-b*tr[7]-c*tr[4]+d*tr[5]+e*tr[2]-f*tr[3]+g*tr[0]+h*tr[1]; \
  1055. tt[7] = -a*tr[7]+b*tr[6]-c*tr[5]-d*tr[4]+e*tr[3]+f*tr[2]-g*tr[1]+h*tr[0]; \
  1056. \
  1057. tr *= tr; \
  1058. \
  1059. tt *= (mixam/tr.sum()); \
  1060. \
  1061. a = tt[0]; \
  1062. b = tt[1]; \
  1063. c = tt[2]; \
  1064. d = tt[3]; \
  1065. e = tt[4]; \
  1066. f = tt[5]; \
  1067. g = tt[6]; \
  1068. h = tt[7]; \
  1069. \
  1070. return(*this); \
  1071. }
  1072. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  1073. #define BOOST_OCTONION_MEMBER_ADD_GENERATOR(type) \
  1074. BOOST_OCTONION_MEMBER_ADD_GENERATOR_1(type) \
  1075. BOOST_OCTONION_MEMBER_ADD_GENERATOR_2(type) \
  1076. BOOST_OCTONION_MEMBER_ADD_GENERATOR_3(type) \
  1077. BOOST_OCTONION_MEMBER_ADD_GENERATOR_4(type)
  1078. #define BOOST_OCTONION_MEMBER_SUB_GENERATOR(type) \
  1079. BOOST_OCTONION_MEMBER_SUB_GENERATOR_1(type) \
  1080. BOOST_OCTONION_MEMBER_SUB_GENERATOR_2(type) \
  1081. BOOST_OCTONION_MEMBER_SUB_GENERATOR_3(type) \
  1082. BOOST_OCTONION_MEMBER_SUB_GENERATOR_4(type)
  1083. #define BOOST_OCTONION_MEMBER_MUL_GENERATOR(type) \
  1084. BOOST_OCTONION_MEMBER_MUL_GENERATOR_1(type) \
  1085. BOOST_OCTONION_MEMBER_MUL_GENERATOR_2(type) \
  1086. BOOST_OCTONION_MEMBER_MUL_GENERATOR_3(type) \
  1087. BOOST_OCTONION_MEMBER_MUL_GENERATOR_4(type)
  1088. #define BOOST_OCTONION_MEMBER_DIV_GENERATOR(type) \
  1089. BOOST_OCTONION_MEMBER_DIV_GENERATOR_1(type) \
  1090. BOOST_OCTONION_MEMBER_DIV_GENERATOR_2(type) \
  1091. BOOST_OCTONION_MEMBER_DIV_GENERATOR_3(type) \
  1092. BOOST_OCTONION_MEMBER_DIV_GENERATOR_4(type)
  1093. #define BOOST_OCTONION_MEMBER_ALGEBRAIC_GENERATOR(type) \
  1094. BOOST_OCTONION_MEMBER_ADD_GENERATOR(type) \
  1095. BOOST_OCTONION_MEMBER_SUB_GENERATOR(type) \
  1096. BOOST_OCTONION_MEMBER_MUL_GENERATOR(type) \
  1097. BOOST_OCTONION_MEMBER_DIV_GENERATOR(type)
  1098. template<>
  1099. class octonion<float>
  1100. {
  1101. public:
  1102. typedef float value_type;
  1103. BOOST_OCTONION_CONSTRUCTOR_GENERATOR(float)
  1104. // UNtemplated copy constructor
  1105. // (this is taken care of by the compiler itself)
  1106. // explicit copy constructors (precision-loosing converters)
  1107. explicit octonion(octonion<double> const & a_recopier)
  1108. {
  1109. *this = detail::octonion_type_converter<float, double>(a_recopier);
  1110. }
  1111. explicit octonion(octonion<long double> const & a_recopier)
  1112. {
  1113. *this = detail::octonion_type_converter<float, long double>(a_recopier);
  1114. }
  1115. // destructor
  1116. // (this is taken care of by the compiler itself)
  1117. // accessors
  1118. //
  1119. // Note: Like complex number, octonions do have a meaningful notion of "real part",
  1120. // but unlike them there is no meaningful notion of "imaginary part".
  1121. // Instead there is an "unreal part" which itself is an octonion, and usually
  1122. // nothing simpler (as opposed to the complex number case).
  1123. // However, for practicallity, there are accessors for the other components
  1124. // (these are necessary for the templated copy constructor, for instance).
  1125. BOOST_OCTONION_ACCESSOR_GENERATOR(float)
  1126. // assignment operators
  1127. BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR(float)
  1128. // other assignment-related operators
  1129. //
  1130. // NOTE: Octonion multiplication is *NOT* commutative;
  1131. // symbolically, "q *= rhs;" means "q = q * rhs;"
  1132. // and "q /= rhs;" means "q = q * inverse_of(rhs);";
  1133. // octonion multiplication is also *NOT* associative
  1134. BOOST_OCTONION_MEMBER_ALGEBRAIC_GENERATOR(float)
  1135. protected:
  1136. BOOST_OCTONION_MEMBER_DATA_GENERATOR(float)
  1137. private:
  1138. };
  1139. template<>
  1140. class octonion<double>
  1141. {
  1142. public:
  1143. typedef double value_type;
  1144. BOOST_OCTONION_CONSTRUCTOR_GENERATOR(double)
  1145. // UNtemplated copy constructor
  1146. // (this is taken care of by the compiler itself)
  1147. // converting copy constructor
  1148. explicit octonion(octonion<float> const & a_recopier)
  1149. {
  1150. *this = detail::octonion_type_converter<double, float>(a_recopier);
  1151. }
  1152. // explicit copy constructors (precision-loosing converters)
  1153. explicit octonion(octonion<long double> const & a_recopier)
  1154. {
  1155. *this = detail::octonion_type_converter<double, long double>(a_recopier);
  1156. }
  1157. // destructor
  1158. // (this is taken care of by the compiler itself)
  1159. // accessors
  1160. //
  1161. // Note: Like complex number, octonions do have a meaningful notion of "real part",
  1162. // but unlike them there is no meaningful notion of "imaginary part".
  1163. // Instead there is an "unreal part" which itself is an octonion, and usually
  1164. // nothing simpler (as opposed to the complex number case).
  1165. // However, for practicallity, there are accessors for the other components
  1166. // (these are necessary for the templated copy constructor, for instance).
  1167. BOOST_OCTONION_ACCESSOR_GENERATOR(double)
  1168. // assignment operators
  1169. BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR(double)
  1170. // other assignment-related operators
  1171. //
  1172. // NOTE: Octonion multiplication is *NOT* commutative;
  1173. // symbolically, "q *= rhs;" means "q = q * rhs;"
  1174. // and "q /= rhs;" means "q = q * inverse_of(rhs);";
  1175. // octonion multiplication is also *NOT* associative
  1176. BOOST_OCTONION_MEMBER_ALGEBRAIC_GENERATOR(double)
  1177. protected:
  1178. BOOST_OCTONION_MEMBER_DATA_GENERATOR(double)
  1179. private:
  1180. };
  1181. template<>
  1182. class octonion<long double>
  1183. {
  1184. public:
  1185. typedef long double value_type;
  1186. BOOST_OCTONION_CONSTRUCTOR_GENERATOR(long double)
  1187. // UNtemplated copy constructor
  1188. // (this is taken care of by the compiler itself)
  1189. // converting copy constructor
  1190. explicit octonion(octonion<float> const & a_recopier)
  1191. {
  1192. *this = detail::octonion_type_converter<long double, float>(a_recopier);
  1193. }
  1194. explicit octonion(octonion<double> const & a_recopier)
  1195. {
  1196. *this = detail::octonion_type_converter<long double, double>(a_recopier);
  1197. }
  1198. // destructor
  1199. // (this is taken care of by the compiler itself)
  1200. // accessors
  1201. //
  1202. // Note: Like complex number, octonions do have a meaningful notion of "real part",
  1203. // but unlike them there is no meaningful notion of "imaginary part".
  1204. // Instead there is an "unreal part" which itself is an octonion, and usually
  1205. // nothing simpler (as opposed to the complex number case).
  1206. // However, for practicallity, there are accessors for the other components
  1207. // (these are necessary for the templated copy constructor, for instance).
  1208. BOOST_OCTONION_ACCESSOR_GENERATOR(long double)
  1209. // assignment operators
  1210. BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR(long double)
  1211. // other assignment-related operators
  1212. //
  1213. // NOTE: Octonion multiplication is *NOT* commutative;
  1214. // symbolically, "q *= rhs;" means "q = q * rhs;"
  1215. // and "q /= rhs;" means "q = q * inverse_of(rhs);";
  1216. // octonion multiplication is also *NOT* associative
  1217. BOOST_OCTONION_MEMBER_ALGEBRAIC_GENERATOR(long double)
  1218. protected:
  1219. BOOST_OCTONION_MEMBER_DATA_GENERATOR(long double)
  1220. private:
  1221. };
  1222. #undef BOOST_OCTONION_CONSTRUCTOR_GENERATOR
  1223. #undef BOOST_OCTONION_MEMBER_ALGEBRAIC_GENERATOR
  1224. #undef BOOST_OCTONION_MEMBER_ADD_GENERATOR
  1225. #undef BOOST_OCTONION_MEMBER_SUB_GENERATOR
  1226. #undef BOOST_OCTONION_MEMBER_MUL_GENERATOR
  1227. #undef BOOST_OCTONION_MEMBER_DIV_GENERATOR
  1228. #undef BOOST_OCTONION_MEMBER_ADD_GENERATOR_1
  1229. #undef BOOST_OCTONION_MEMBER_ADD_GENERATOR_2
  1230. #undef BOOST_OCTONION_MEMBER_ADD_GENERATOR_3
  1231. #undef BOOST_OCTONION_MEMBER_ADD_GENERATOR_4
  1232. #undef BOOST_OCTONION_MEMBER_SUB_GENERATOR_1
  1233. #undef BOOST_OCTONION_MEMBER_SUB_GENERATOR_2
  1234. #undef BOOST_OCTONION_MEMBER_SUB_GENERATOR_3
  1235. #undef BOOST_OCTONION_MEMBER_SUB_GENERATOR_4
  1236. #undef BOOST_OCTONION_MEMBER_MUL_GENERATOR_1
  1237. #undef BOOST_OCTONION_MEMBER_MUL_GENERATOR_2
  1238. #undef BOOST_OCTONION_MEMBER_MUL_GENERATOR_3
  1239. #undef BOOST_OCTONION_MEMBER_MUL_GENERATOR_4
  1240. #undef BOOST_OCTONION_MEMBER_DIV_GENERATOR_1
  1241. #undef BOOST_OCTONION_MEMBER_DIV_GENERATOR_2
  1242. #undef BOOST_OCTONION_MEMBER_DIV_GENERATOR_3
  1243. #undef BOOST_OCTONION_MEMBER_DIV_GENERATOR_4
  1244. #undef BOOST_OCTONION_MEMBER_DATA_GENERATOR
  1245. #undef BOOST_OCTONION_MEMBER_ASSIGNMENT_GENERATOR
  1246. #undef BOOST_OCTONION_ACCESSOR_GENERATOR
  1247. // operators
  1248. #define BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op) \
  1249. { \
  1250. octonion<T> res(lhs); \
  1251. res op##= rhs; \
  1252. return(res); \
  1253. }
  1254. #define BOOST_OCTONION_OPERATOR_GENERATOR_1_L(op) \
  1255. template<typename T> \
  1256. inline octonion<T> operator op (T const & lhs, octonion<T> const & rhs) \
  1257. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1258. #define BOOST_OCTONION_OPERATOR_GENERATOR_1_R(op) \
  1259. template<typename T> \
  1260. inline octonion<T> operator op (octonion<T> const & lhs, T const & rhs) \
  1261. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1262. #define BOOST_OCTONION_OPERATOR_GENERATOR_2_L(op) \
  1263. template<typename T> \
  1264. inline octonion<T> operator op (::std::complex<T> const & lhs, octonion<T> const & rhs) \
  1265. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1266. #define BOOST_OCTONION_OPERATOR_GENERATOR_2_R(op) \
  1267. template<typename T> \
  1268. inline octonion<T> operator op (octonion<T> const & lhs, ::std::complex<T> const & rhs) \
  1269. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1270. #define BOOST_OCTONION_OPERATOR_GENERATOR_3_L(op) \
  1271. template<typename T> \
  1272. inline octonion<T> operator op (::boost::math::quaternion<T> const & lhs, octonion<T> const & rhs) \
  1273. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1274. #define BOOST_OCTONION_OPERATOR_GENERATOR_3_R(op) \
  1275. template<typename T> \
  1276. inline octonion<T> operator op (octonion<T> const & lhs, ::boost::math::quaternion<T> const & rhs) \
  1277. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1278. #define BOOST_OCTONION_OPERATOR_GENERATOR_4(op) \
  1279. template<typename T> \
  1280. inline octonion<T> operator op (octonion<T> const & lhs, octonion<T> const & rhs) \
  1281. BOOST_OCTONION_OPERATOR_GENERATOR_BODY(op)
  1282. #define BOOST_OCTONION_OPERATOR_GENERATOR(op) \
  1283. BOOST_OCTONION_OPERATOR_GENERATOR_1_L(op) \
  1284. BOOST_OCTONION_OPERATOR_GENERATOR_1_R(op) \
  1285. BOOST_OCTONION_OPERATOR_GENERATOR_2_L(op) \
  1286. BOOST_OCTONION_OPERATOR_GENERATOR_2_R(op) \
  1287. BOOST_OCTONION_OPERATOR_GENERATOR_3_L(op) \
  1288. BOOST_OCTONION_OPERATOR_GENERATOR_3_R(op) \
  1289. BOOST_OCTONION_OPERATOR_GENERATOR_4(op)
  1290. BOOST_OCTONION_OPERATOR_GENERATOR(+)
  1291. BOOST_OCTONION_OPERATOR_GENERATOR(-)
  1292. BOOST_OCTONION_OPERATOR_GENERATOR(*)
  1293. BOOST_OCTONION_OPERATOR_GENERATOR(/)
  1294. #undef BOOST_OCTONION_OPERATOR_GENERATOR
  1295. #undef BOOST_OCTONION_OPERATOR_GENERATOR_1_L
  1296. #undef BOOST_OCTONION_OPERATOR_GENERATOR_1_R
  1297. #undef BOOST_OCTONION_OPERATOR_GENERATOR_2_L
  1298. #undef BOOST_OCTONION_OPERATOR_GENERATOR_2_R
  1299. #undef BOOST_OCTONION_OPERATOR_GENERATOR_3_L
  1300. #undef BOOST_OCTONION_OPERATOR_GENERATOR_3_R
  1301. #undef BOOST_OCTONION_OPERATOR_GENERATOR_4
  1302. #undef BOOST_OCTONION_OPERATOR_GENERATOR_BODY
  1303. template<typename T>
  1304. inline octonion<T> operator + (octonion<T> const & o)
  1305. {
  1306. return(o);
  1307. }
  1308. template<typename T>
  1309. inline octonion<T> operator - (octonion<T> const & o)
  1310. {
  1311. return(octonion<T>(-o.R_component_1(),-o.R_component_2(),-o.R_component_3(),-o.R_component_4(),-o.R_component_5(),-o.R_component_6(),-o.R_component_7(),-o.R_component_8()));
  1312. }
  1313. template<typename T>
  1314. inline bool operator == (T const & lhs, octonion<T> const & rhs)
  1315. {
  1316. return(
  1317. (rhs.R_component_1() == lhs)&&
  1318. (rhs.R_component_2() == static_cast<T>(0))&&
  1319. (rhs.R_component_3() == static_cast<T>(0))&&
  1320. (rhs.R_component_4() == static_cast<T>(0))&&
  1321. (rhs.R_component_5() == static_cast<T>(0))&&
  1322. (rhs.R_component_6() == static_cast<T>(0))&&
  1323. (rhs.R_component_7() == static_cast<T>(0))&&
  1324. (rhs.R_component_8() == static_cast<T>(0))
  1325. );
  1326. }
  1327. template<typename T>
  1328. inline bool operator == (octonion<T> const & lhs, T const & rhs)
  1329. {
  1330. return(
  1331. (lhs.R_component_1() == rhs)&&
  1332. (lhs.R_component_2() == static_cast<T>(0))&&
  1333. (lhs.R_component_3() == static_cast<T>(0))&&
  1334. (lhs.R_component_4() == static_cast<T>(0))&&
  1335. (lhs.R_component_5() == static_cast<T>(0))&&
  1336. (lhs.R_component_6() == static_cast<T>(0))&&
  1337. (lhs.R_component_7() == static_cast<T>(0))&&
  1338. (lhs.R_component_8() == static_cast<T>(0))
  1339. );
  1340. }
  1341. template<typename T>
  1342. inline bool operator == (::std::complex<T> const & lhs, octonion<T> const & rhs)
  1343. {
  1344. return(
  1345. (rhs.R_component_1() == lhs.real())&&
  1346. (rhs.R_component_2() == lhs.imag())&&
  1347. (rhs.R_component_3() == static_cast<T>(0))&&
  1348. (rhs.R_component_4() == static_cast<T>(0))&&
  1349. (rhs.R_component_5() == static_cast<T>(0))&&
  1350. (rhs.R_component_6() == static_cast<T>(0))&&
  1351. (rhs.R_component_7() == static_cast<T>(0))&&
  1352. (rhs.R_component_8() == static_cast<T>(0))
  1353. );
  1354. }
  1355. template<typename T>
  1356. inline bool operator == (octonion<T> const & lhs, ::std::complex<T> const & rhs)
  1357. {
  1358. return(
  1359. (lhs.R_component_1() == rhs.real())&&
  1360. (lhs.R_component_2() == rhs.imag())&&
  1361. (lhs.R_component_3() == static_cast<T>(0))&&
  1362. (lhs.R_component_4() == static_cast<T>(0))&&
  1363. (lhs.R_component_5() == static_cast<T>(0))&&
  1364. (lhs.R_component_6() == static_cast<T>(0))&&
  1365. (lhs.R_component_7() == static_cast<T>(0))&&
  1366. (lhs.R_component_8() == static_cast<T>(0))
  1367. );
  1368. }
  1369. template<typename T>
  1370. inline bool operator == (::boost::math::quaternion<T> const & lhs, octonion<T> const & rhs)
  1371. {
  1372. return(
  1373. (rhs.R_component_1() == lhs.R_component_1())&&
  1374. (rhs.R_component_2() == lhs.R_component_2())&&
  1375. (rhs.R_component_3() == lhs.R_component_3())&&
  1376. (rhs.R_component_4() == lhs.R_component_4())&&
  1377. (rhs.R_component_5() == static_cast<T>(0))&&
  1378. (rhs.R_component_6() == static_cast<T>(0))&&
  1379. (rhs.R_component_7() == static_cast<T>(0))&&
  1380. (rhs.R_component_8() == static_cast<T>(0))
  1381. );
  1382. }
  1383. template<typename T>
  1384. inline bool operator == (octonion<T> const & lhs, ::boost::math::quaternion<T> const & rhs)
  1385. {
  1386. return(
  1387. (lhs.R_component_1() == rhs.R_component_1())&&
  1388. (lhs.R_component_2() == rhs.R_component_2())&&
  1389. (lhs.R_component_3() == rhs.R_component_3())&&
  1390. (lhs.R_component_4() == rhs.R_component_4())&&
  1391. (lhs.R_component_5() == static_cast<T>(0))&&
  1392. (lhs.R_component_6() == static_cast<T>(0))&&
  1393. (lhs.R_component_7() == static_cast<T>(0))&&
  1394. (lhs.R_component_8() == static_cast<T>(0))
  1395. );
  1396. }
  1397. template<typename T>
  1398. inline bool operator == (octonion<T> const & lhs, octonion<T> const & rhs)
  1399. {
  1400. return(
  1401. (rhs.R_component_1() == lhs.R_component_1())&&
  1402. (rhs.R_component_2() == lhs.R_component_2())&&
  1403. (rhs.R_component_3() == lhs.R_component_3())&&
  1404. (rhs.R_component_4() == lhs.R_component_4())&&
  1405. (rhs.R_component_5() == lhs.R_component_5())&&
  1406. (rhs.R_component_6() == lhs.R_component_6())&&
  1407. (rhs.R_component_7() == lhs.R_component_7())&&
  1408. (rhs.R_component_8() == lhs.R_component_8())
  1409. );
  1410. }
  1411. #define BOOST_OCTONION_NOT_EQUAL_GENERATOR \
  1412. { \
  1413. return(!(lhs == rhs)); \
  1414. }
  1415. template<typename T>
  1416. inline bool operator != (T const & lhs, octonion<T> const & rhs)
  1417. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1418. template<typename T>
  1419. inline bool operator != (octonion<T> const & lhs, T const & rhs)
  1420. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1421. template<typename T>
  1422. inline bool operator != (::std::complex<T> const & lhs, octonion<T> const & rhs)
  1423. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1424. template<typename T>
  1425. inline bool operator != (octonion<T> const & lhs, ::std::complex<T> const & rhs)
  1426. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1427. template<typename T>
  1428. inline bool operator != (::boost::math::quaternion<T> const & lhs, octonion<T> const & rhs)
  1429. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1430. template<typename T>
  1431. inline bool operator != (octonion<T> const & lhs, ::boost::math::quaternion<T> const & rhs)
  1432. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1433. template<typename T>
  1434. inline bool operator != (octonion<T> const & lhs, octonion<T> const & rhs)
  1435. BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1436. #undef BOOST_OCTONION_NOT_EQUAL_GENERATOR
  1437. // Note: the default values in the constructors of the complex and quaternions make for
  1438. // a very complex and ambiguous situation; we have made choices to disambiguate.
  1439. template<typename T, typename charT, class traits>
  1440. ::std::basic_istream<charT,traits> & operator >> ( ::std::basic_istream<charT,traits> & is,
  1441. octonion<T> & o)
  1442. {
  1443. #ifdef BOOST_NO_STD_LOCALE
  1444. #else
  1445. const ::std::ctype<charT> & ct = ::std::use_facet< ::std::ctype<charT> >(is.getloc());
  1446. #endif /* BOOST_NO_STD_LOCALE */
  1447. T a = T();
  1448. T b = T();
  1449. T c = T();
  1450. T d = T();
  1451. T e = T();
  1452. T f = T();
  1453. T g = T();
  1454. T h = T();
  1455. ::std::complex<T> u = ::std::complex<T>();
  1456. ::std::complex<T> v = ::std::complex<T>();
  1457. ::std::complex<T> x = ::std::complex<T>();
  1458. ::std::complex<T> y = ::std::complex<T>();
  1459. ::boost::math::quaternion<T> p = ::boost::math::quaternion<T>();
  1460. ::boost::math::quaternion<T> q = ::boost::math::quaternion<T>();
  1461. charT ch = charT();
  1462. char cc;
  1463. is >> ch; // get the first lexeme
  1464. if (!is.good()) goto finish;
  1465. #ifdef BOOST_NO_STD_LOCALE
  1466. cc = ch;
  1467. #else
  1468. cc = ct.narrow(ch, char());
  1469. #endif /* BOOST_NO_STD_LOCALE */
  1470. if (cc == '(') // read "("
  1471. {
  1472. is >> ch; // get the second lexeme
  1473. if (!is.good()) goto finish;
  1474. #ifdef BOOST_NO_STD_LOCALE
  1475. cc = ch;
  1476. #else
  1477. cc = ct.narrow(ch, char());
  1478. #endif /* BOOST_NO_STD_LOCALE */
  1479. if (cc == '(') // read "(("
  1480. {
  1481. is >> ch; // get the third lexeme
  1482. if (!is.good()) goto finish;
  1483. #ifdef BOOST_NO_STD_LOCALE
  1484. cc = ch;
  1485. #else
  1486. cc = ct.narrow(ch, char());
  1487. #endif /* BOOST_NO_STD_LOCALE */
  1488. if (cc == '(') // read "((("
  1489. {
  1490. is.putback(ch);
  1491. is >> u; // read "((u"
  1492. if (!is.good()) goto finish;
  1493. is >> ch; // get the next lexeme
  1494. if (!is.good()) goto finish;
  1495. #ifdef BOOST_NO_STD_LOCALE
  1496. cc = ch;
  1497. #else
  1498. cc = ct.narrow(ch, char());
  1499. #endif /* BOOST_NO_STD_LOCALE */
  1500. if (cc == ')') // read "((u)"
  1501. {
  1502. is >> ch; // get the next lexeme
  1503. if (!is.good()) goto finish;
  1504. #ifdef BOOST_NO_STD_LOCALE
  1505. cc = ch;
  1506. #else
  1507. cc = ct.narrow(ch, char());
  1508. #endif /* BOOST_NO_STD_LOCALE */
  1509. if (cc == ')') // format: (((a))), (((a,b)))
  1510. {
  1511. o = octonion<T>(u);
  1512. }
  1513. else if (cc == ',') // read "((u),"
  1514. {
  1515. p = ::boost::math::quaternion<T>(u);
  1516. is >> q; // read "((u),q"
  1517. if (!is.good()) goto finish;
  1518. is >> ch; // get the next lexeme
  1519. if (!is.good()) goto finish;
  1520. #ifdef BOOST_NO_STD_LOCALE
  1521. cc = ch;
  1522. #else
  1523. cc = ct.narrow(ch, char());
  1524. #endif /* BOOST_NO_STD_LOCALE */
  1525. if (cc == ')') // format: (((a)),q), (((a,b)),q)
  1526. {
  1527. o = octonion<T>(p,q);
  1528. }
  1529. else // error
  1530. {
  1531. is.setstate(::std::ios_base::failbit);
  1532. }
  1533. }
  1534. else // error
  1535. {
  1536. is.setstate(::std::ios_base::failbit);
  1537. }
  1538. }
  1539. else if (cc ==',') // read "((u,"
  1540. {
  1541. is >> v; // read "((u,v"
  1542. if (!is.good()) goto finish;
  1543. is >> ch; // get the next lexeme
  1544. if (!is.good()) goto finish;
  1545. #ifdef BOOST_NO_STD_LOCALE
  1546. cc = ch;
  1547. #else
  1548. cc = ct.narrow(ch, char());
  1549. #endif /* BOOST_NO_STD_LOCALE */
  1550. if (cc == ')') // read "((u,v)"
  1551. {
  1552. p = ::boost::math::quaternion<T>(u,v);
  1553. is >> ch; // get the next lexeme
  1554. if (!is.good()) goto finish;
  1555. #ifdef BOOST_NO_STD_LOCALE
  1556. cc = ch;
  1557. #else
  1558. cc = ct.narrow(ch, char());
  1559. #endif /* BOOST_NO_STD_LOCALE */
  1560. if (cc == ')') // format: (((a),v)), (((a,b),v))
  1561. {
  1562. o = octonion<T>(p);
  1563. }
  1564. else if (cc == ',') // read "((u,v),"
  1565. {
  1566. is >> q; // read "(p,q"
  1567. if (!is.good()) goto finish;
  1568. is >> ch; // get the next lexeme
  1569. if (!is.good()) goto finish;
  1570. #ifdef BOOST_NO_STD_LOCALE
  1571. cc = ch;
  1572. #else
  1573. cc = ct.narrow(ch, char());
  1574. #endif /* BOOST_NO_STD_LOCALE */
  1575. if (cc == ')') // format: (((a),v),q), (((a,b),v),q)
  1576. {
  1577. o = octonion<T>(p,q);
  1578. }
  1579. else // error
  1580. {
  1581. is.setstate(::std::ios_base::failbit);
  1582. }
  1583. }
  1584. else // error
  1585. {
  1586. is.setstate(::std::ios_base::failbit);
  1587. }
  1588. }
  1589. else // error
  1590. {
  1591. is.setstate(::std::ios_base::failbit);
  1592. }
  1593. }
  1594. else // error
  1595. {
  1596. is.setstate(::std::ios_base::failbit);
  1597. }
  1598. }
  1599. else // read "((a"
  1600. {
  1601. is.putback(ch);
  1602. is >> a; // we extract the first component
  1603. if (!is.good()) goto finish;
  1604. is >> ch; // get the next lexeme
  1605. if (!is.good()) goto finish;
  1606. #ifdef BOOST_NO_STD_LOCALE
  1607. cc = ch;
  1608. #else
  1609. cc = ct.narrow(ch, char());
  1610. #endif /* BOOST_NO_STD_LOCALE */
  1611. if (cc == ')') // read "((a)"
  1612. {
  1613. is >> ch; // get the next lexeme
  1614. if (!is.good()) goto finish;
  1615. #ifdef BOOST_NO_STD_LOCALE
  1616. cc = ch;
  1617. #else
  1618. cc = ct.narrow(ch, char());
  1619. #endif /* BOOST_NO_STD_LOCALE */
  1620. if (cc == ')') // read "((a))"
  1621. {
  1622. o = octonion<T>(a);
  1623. }
  1624. else if (cc == ',') // read "((a),"
  1625. {
  1626. is >> ch; // get the next lexeme
  1627. if (!is.good()) goto finish;
  1628. #ifdef BOOST_NO_STD_LOCALE
  1629. cc = ch;
  1630. #else
  1631. cc = ct.narrow(ch, char());
  1632. #endif /* BOOST_NO_STD_LOCALE */
  1633. if (cc == '(') // read "((a),("
  1634. {
  1635. is >> ch; // get the next lexeme
  1636. if (!is.good()) goto finish;
  1637. #ifdef BOOST_NO_STD_LOCALE
  1638. cc = ch;
  1639. #else
  1640. cc = ct.narrow(ch, char());
  1641. #endif /* BOOST_NO_STD_LOCALE */
  1642. if (cc == '(') // read "((a),(("
  1643. {
  1644. is.putback(ch);
  1645. is.putback(ch); // we backtrack twice, with the same value!
  1646. is >> q; // read "((a),q"
  1647. if (!is.good()) goto finish;
  1648. is >> ch; // get the next lexeme
  1649. if (!is.good()) goto finish;
  1650. #ifdef BOOST_NO_STD_LOCALE
  1651. cc = ch;
  1652. #else
  1653. cc = ct.narrow(ch, char());
  1654. #endif /* BOOST_NO_STD_LOCALE */
  1655. if (cc == ')') // read "((a),q)"
  1656. {
  1657. p = ::boost::math::quaternion<T>(a);
  1658. o = octonion<T>(p,q);
  1659. }
  1660. else // error
  1661. {
  1662. is.setstate(::std::ios_base::failbit);
  1663. }
  1664. }
  1665. else // read "((a),(c" or "((a),(e"
  1666. {
  1667. is.putback(ch);
  1668. is >> c;
  1669. if (!is.good()) goto finish;
  1670. is >> ch; // get the next lexeme
  1671. if (!is.good()) goto finish;
  1672. #ifdef BOOST_NO_STD_LOCALE
  1673. cc = ch;
  1674. #else
  1675. cc = ct.narrow(ch, char());
  1676. #endif /* BOOST_NO_STD_LOCALE */
  1677. if (cc == ')') // read "((a),(c)" (ambiguity resolution)
  1678. {
  1679. is >> ch; // get the next lexeme
  1680. if (!is.good()) goto finish;
  1681. #ifdef BOOST_NO_STD_LOCALE
  1682. cc = ch;
  1683. #else
  1684. cc = ct.narrow(ch, char());
  1685. #endif /* BOOST_NO_STD_LOCALE */
  1686. if (cc == ')') // read "((a),(c))"
  1687. {
  1688. o = octonion<T>(a,b,c);
  1689. }
  1690. else if (cc == ',') // read "((a),(c),"
  1691. {
  1692. u = ::std::complex<T>(a);
  1693. v = ::std::complex<T>(c);
  1694. is >> x; // read "((a),(c),x"
  1695. if (!is.good()) goto finish;
  1696. is >> ch; // get the next lexeme
  1697. if (!is.good()) goto finish;
  1698. #ifdef BOOST_NO_STD_LOCALE
  1699. cc = ch;
  1700. #else
  1701. cc = ct.narrow(ch, char());
  1702. #endif /* BOOST_NO_STD_LOCALE */
  1703. if (cc == ')') // read "((a),(c),x)"
  1704. {
  1705. o = octonion<T>(u,v,x);
  1706. }
  1707. else if (cc == ',') // read "((a),(c),x,"
  1708. {
  1709. is >> y; // read "((a),(c),x,y"
  1710. if (!is.good()) goto finish;
  1711. is >> ch; // get the next lexeme
  1712. if (!is.good()) goto finish;
  1713. #ifdef BOOST_NO_STD_LOCALE
  1714. cc = ch;
  1715. #else
  1716. cc = ct.narrow(ch, char());
  1717. #endif /* BOOST_NO_STD_LOCALE */
  1718. if (cc == ')') // read "((a),(c),x,y)"
  1719. {
  1720. o = octonion<T>(u,v,x,y);
  1721. }
  1722. else // error
  1723. {
  1724. is.setstate(::std::ios_base::failbit);
  1725. }
  1726. }
  1727. else // error
  1728. {
  1729. is.setstate(::std::ios_base::failbit);
  1730. }
  1731. }
  1732. else // error
  1733. {
  1734. is.setstate(::std::ios_base::failbit);
  1735. }
  1736. }
  1737. else if (cc == ',') // read "((a),(c," or "((a),(e,"
  1738. {
  1739. is >> ch; // get the next lexeme
  1740. if (!is.good()) goto finish;
  1741. #ifdef BOOST_NO_STD_LOCALE
  1742. cc = ch;
  1743. #else
  1744. cc = ct.narrow(ch, char());
  1745. #endif /* BOOST_NO_STD_LOCALE */
  1746. if (cc == '(') // read "((a),(e,(" (ambiguity resolution)
  1747. {
  1748. p = ::boost::math::quaternion<T>(a);
  1749. x = ::std::complex<T>(c); // "c" was actually "e"
  1750. is.putback(ch); // we can only backtrace once
  1751. is >> y; // read "((a),(e,y"
  1752. if (!is.good()) goto finish;
  1753. is >> ch; // get the next lexeme
  1754. #ifdef BOOST_NO_STD_LOCALE
  1755. cc = ch;
  1756. #else
  1757. cc = ct.narrow(ch, char());
  1758. #endif /* BOOST_NO_STD_LOCALE */
  1759. if (cc == ')') // read "((a),(e,y)"
  1760. {
  1761. q = ::boost::math::quaternion<T>(x,y);
  1762. is >> ch; // get the next lexeme
  1763. #ifdef BOOST_NO_STD_LOCALE
  1764. cc = ch;
  1765. #else
  1766. cc = ct.narrow(ch, char());
  1767. #endif /* BOOST_NO_STD_LOCALE */
  1768. if (cc == ')') // read "((a),(e,y))"
  1769. {
  1770. o = octonion<T>(p,q);
  1771. }
  1772. else // error
  1773. {
  1774. is.setstate(::std::ios_base::failbit);
  1775. }
  1776. }
  1777. else // error
  1778. {
  1779. is.setstate(::std::ios_base::failbit);
  1780. }
  1781. }
  1782. else // read "((a),(c,d" or "((a),(e,f"
  1783. {
  1784. is.putback(ch);
  1785. is >> d;
  1786. if (!is.good()) goto finish;
  1787. is >> ch; // get the next lexeme
  1788. if (!is.good()) goto finish;
  1789. #ifdef BOOST_NO_STD_LOCALE
  1790. cc = ch;
  1791. #else
  1792. cc = ct.narrow(ch, char());
  1793. #endif /* BOOST_NO_STD_LOCALE */
  1794. if (cc == ')') // read "((a),(c,d)" (ambiguity resolution)
  1795. {
  1796. is >> ch; // get the next lexeme
  1797. if (!is.good()) goto finish;
  1798. #ifdef BOOST_NO_STD_LOCALE
  1799. cc = ch;
  1800. #else
  1801. cc = ct.narrow(ch, char());
  1802. #endif /* BOOST_NO_STD_LOCALE */
  1803. if (cc == ')') // read "((a),(c,d))"
  1804. {
  1805. o = octonion<T>(a,b,c,d);
  1806. }
  1807. else if (cc == ',') // read "((a),(c,d),"
  1808. {
  1809. u = ::std::complex<T>(a);
  1810. v = ::std::complex<T>(c,d);
  1811. is >> x; // read "((a),(c,d),x"
  1812. if (!is.good()) goto finish;
  1813. is >> ch; // get the next lexeme
  1814. if (!is.good()) goto finish;
  1815. #ifdef BOOST_NO_STD_LOCALE
  1816. cc = ch;
  1817. #else
  1818. cc = ct.narrow(ch, char());
  1819. #endif /* BOOST_NO_STD_LOCALE */
  1820. if (cc == ')') // read "((a),(c,d),x)"
  1821. {
  1822. o = octonion<T>(u,v,x);
  1823. }
  1824. else if (cc == ',') // read "((a),(c,d),x,"
  1825. {
  1826. is >> y; // read "((a),(c,d),x,y"
  1827. if (!is.good()) goto finish;
  1828. is >> ch; // get the next lexeme
  1829. if (!is.good()) goto finish;
  1830. #ifdef BOOST_NO_STD_LOCALE
  1831. cc = ch;
  1832. #else
  1833. cc = ct.narrow(ch, char());
  1834. #endif /* BOOST_NO_STD_LOCALE */
  1835. if (cc == ')') // read "((a),(c,d),x,y)"
  1836. {
  1837. o = octonion<T>(u,v,x,y);
  1838. }
  1839. else // error
  1840. {
  1841. is.setstate(::std::ios_base::failbit);
  1842. }
  1843. }
  1844. else // error
  1845. {
  1846. is.setstate(::std::ios_base::failbit);
  1847. }
  1848. }
  1849. else // error
  1850. {
  1851. is.setstate(::std::ios_base::failbit);
  1852. }
  1853. }
  1854. else if (cc == ',') // read "((a),(e,f," (ambiguity resolution)
  1855. {
  1856. p = ::boost::math::quaternion<T>(a);
  1857. is >> g; // read "((a),(e,f,g" (too late to backtrack)
  1858. if (!is.good()) goto finish;
  1859. is >> ch; // get the next lexeme
  1860. if (!is.good()) goto finish;
  1861. #ifdef BOOST_NO_STD_LOCALE
  1862. cc = ch;
  1863. #else
  1864. cc = ct.narrow(ch, char());
  1865. #endif /* BOOST_NO_STD_LOCALE */
  1866. if (cc == ')') // read "((a),(e,f,g)"
  1867. {
  1868. q = ::boost::math::quaternion<T>(c,d,g); // "c" was actually "e", and "d" was actually "f"
  1869. is >> ch; // get the next lexeme
  1870. if (!is.good()) goto finish;
  1871. #ifdef BOOST_NO_STD_LOCALE
  1872. cc = ch;
  1873. #else
  1874. cc = ct.narrow(ch, char());
  1875. #endif /* BOOST_NO_STD_LOCALE */
  1876. if (cc == ')') // read "((a),(e,f,g))"
  1877. {
  1878. o = octonion<T>(p,q);
  1879. }
  1880. else // error
  1881. {
  1882. is.setstate(::std::ios_base::failbit);
  1883. }
  1884. }
  1885. else if (cc == ',') // read "((a),(e,f,g,"
  1886. {
  1887. is >> h; // read "((a),(e,f,g,h"
  1888. if (!is.good()) goto finish;
  1889. is >> ch; // get the next lexeme
  1890. if (!is.good()) goto finish;
  1891. #ifdef BOOST_NO_STD_LOCALE
  1892. cc = ch;
  1893. #else
  1894. cc = ct.narrow(ch, char());
  1895. #endif /* BOOST_NO_STD_LOCALE */
  1896. if (cc == ')') // read "((a),(e,f,g,h)"
  1897. {
  1898. q = ::boost::math::quaternion<T>(c,d,g,h); // "c" was actually "e", and "d" was actually "f"
  1899. is >> ch; // get the next lexeme
  1900. if (!is.good()) goto finish;
  1901. #ifdef BOOST_NO_STD_LOCALE
  1902. cc = ch;
  1903. #else
  1904. cc = ct.narrow(ch, char());
  1905. #endif /* BOOST_NO_STD_LOCALE */
  1906. if (cc == ')') // read "((a),(e,f,g,h))"
  1907. {
  1908. o = octonion<T>(p,q);
  1909. }
  1910. else // error
  1911. {
  1912. is.setstate(::std::ios_base::failbit);
  1913. }
  1914. }
  1915. else // error
  1916. {
  1917. is.setstate(::std::ios_base::failbit);
  1918. }
  1919. }
  1920. else // error
  1921. {
  1922. is.setstate(::std::ios_base::failbit);
  1923. }
  1924. }
  1925. else // error
  1926. {
  1927. is.setstate(::std::ios_base::failbit);
  1928. }
  1929. }
  1930. }
  1931. else // error
  1932. {
  1933. is.setstate(::std::ios_base::failbit);
  1934. }
  1935. }
  1936. }
  1937. else // read "((a),c" (ambiguity resolution)
  1938. {
  1939. is.putback(ch);
  1940. is >> c; // we extract the third component
  1941. if (!is.good()) goto finish;
  1942. is >> ch; // get the next lexeme
  1943. if (!is.good()) goto finish;
  1944. #ifdef BOOST_NO_STD_LOCALE
  1945. cc = ch;
  1946. #else
  1947. cc = ct.narrow(ch, char());
  1948. #endif /* BOOST_NO_STD_LOCALE */
  1949. if (cc == ')') // read "((a),c)"
  1950. {
  1951. o = octonion<T>(a,b,c);
  1952. }
  1953. else if (cc == ',') // read "((a),c,"
  1954. {
  1955. is >> x; // read "((a),c,x"
  1956. if (!is.good()) goto finish;
  1957. is >> ch; // get the next lexeme
  1958. if (!is.good()) goto finish;
  1959. #ifdef BOOST_NO_STD_LOCALE
  1960. cc = ch;
  1961. #else
  1962. cc = ct.narrow(ch, char());
  1963. #endif /* BOOST_NO_STD_LOCALE */
  1964. if (cc == ')') // read "((a),c,x)"
  1965. {
  1966. o = octonion<T>(a,b,c,d,x.real(),x.imag());
  1967. }
  1968. else if (cc == ',') // read "((a),c,x,"
  1969. {
  1970. is >> y;if (!is.good()) goto finish; // read "((a),c,x,y"
  1971. is >> ch; // get the next lexeme
  1972. if (!is.good()) goto finish;
  1973. #ifdef BOOST_NO_STD_LOCALE
  1974. cc = ch;
  1975. #else
  1976. cc = ct.narrow(ch, char());
  1977. #endif /* BOOST_NO_STD_LOCALE */
  1978. if (cc == ')') // read "((a),c,x,y)"
  1979. {
  1980. o = octonion<T>(a,b,c,d,x.real(),x.imag(),y.real(),y.imag());
  1981. }
  1982. else // error
  1983. {
  1984. is.setstate(::std::ios_base::failbit);
  1985. }
  1986. }
  1987. else // error
  1988. {
  1989. is.setstate(::std::ios_base::failbit);
  1990. }
  1991. }
  1992. else // error
  1993. {
  1994. is.setstate(::std::ios_base::failbit);
  1995. }
  1996. }
  1997. }
  1998. else // error
  1999. {
  2000. is.setstate(::std::ios_base::failbit);
  2001. }
  2002. }
  2003. else if (cc ==',') // read "((a,"
  2004. {
  2005. is >> ch; // get the next lexeme
  2006. if (!is.good()) goto finish;
  2007. #ifdef BOOST_NO_STD_LOCALE
  2008. cc = ch;
  2009. #else
  2010. cc = ct.narrow(ch, char());
  2011. #endif /* BOOST_NO_STD_LOCALE */
  2012. if (cc == '(') // read "((a,("
  2013. {
  2014. u = ::std::complex<T>(a);
  2015. is.putback(ch); // can only backtrack so much
  2016. is >> v; // read "((a,v"
  2017. if (!is.good()) goto finish;
  2018. is >> ch; // get the next lexeme
  2019. if (!is.good()) goto finish;
  2020. #ifdef BOOST_NO_STD_LOCALE
  2021. cc = ch;
  2022. #else
  2023. cc = ct.narrow(ch, char());
  2024. #endif /* BOOST_NO_STD_LOCALE */
  2025. if (cc == ')') // read "((a,v)"
  2026. {
  2027. is >> ch; // get the next lexeme
  2028. if (!is.good()) goto finish;
  2029. #ifdef BOOST_NO_STD_LOCALE
  2030. cc = ch;
  2031. #else
  2032. cc = ct.narrow(ch, char());
  2033. #endif /* BOOST_NO_STD_LOCALE */
  2034. if (cc == ')') // read "((a,v))"
  2035. {
  2036. o = octonion<T>(u,v);
  2037. }
  2038. else if (cc == ',') // read "((a,v),"
  2039. {
  2040. p = ::boost::math::quaternion<T>(u,v);
  2041. is >> q; // read "((a,v),q"
  2042. if (!is.good()) goto finish;
  2043. is >> ch; // get the next lexeme
  2044. if (!is.good()) goto finish;
  2045. #ifdef BOOST_NO_STD_LOCALE
  2046. cc = ch;
  2047. #else
  2048. cc = ct.narrow(ch, char());
  2049. #endif /* BOOST_NO_STD_LOCALE */
  2050. if (cc == ')') // read "((a,v),q)"
  2051. {
  2052. o = octonion<T>(p,q);
  2053. }
  2054. else // error
  2055. {
  2056. is.setstate(::std::ios_base::failbit);
  2057. }
  2058. }
  2059. else // error
  2060. {
  2061. is.setstate(::std::ios_base::failbit);
  2062. }
  2063. }
  2064. else // error
  2065. {
  2066. is.setstate(::std::ios_base::failbit);
  2067. }
  2068. }
  2069. else
  2070. {
  2071. is.putback(ch);
  2072. is >> b; // read "((a,b"
  2073. if (!is.good()) goto finish;
  2074. is >> ch; // get the next lexeme
  2075. if (!is.good()) goto finish;
  2076. #ifdef BOOST_NO_STD_LOCALE
  2077. cc = ch;
  2078. #else
  2079. cc = ct.narrow(ch, char());
  2080. #endif /* BOOST_NO_STD_LOCALE */
  2081. if (cc == ')') // read "((a,b)"
  2082. {
  2083. is >> ch; // get the next lexeme
  2084. if (!is.good()) goto finish;
  2085. #ifdef BOOST_NO_STD_LOCALE
  2086. cc = ch;
  2087. #else
  2088. cc = ct.narrow(ch, char());
  2089. #endif /* BOOST_NO_STD_LOCALE */
  2090. if (cc == ')') // read "((a,b))"
  2091. {
  2092. o = octonion<T>(a,b);
  2093. }
  2094. else if (cc == ',') // read "((a,b),"
  2095. {
  2096. is >> ch; // get the next lexeme
  2097. if (!is.good()) goto finish;
  2098. #ifdef BOOST_NO_STD_LOCALE
  2099. cc = ch;
  2100. #else
  2101. cc = ct.narrow(ch, char());
  2102. #endif /* BOOST_NO_STD_LOCALE */
  2103. if (cc == '(') // read "((a,b),("
  2104. {
  2105. is >> ch; // get the next lexeme
  2106. if (!is.good()) goto finish;
  2107. #ifdef BOOST_NO_STD_LOCALE
  2108. cc = ch;
  2109. #else
  2110. cc = ct.narrow(ch, char());
  2111. #endif /* BOOST_NO_STD_LOCALE */
  2112. if (cc == '(') // read "((a,b),(("
  2113. {
  2114. p = ::boost::math::quaternion<T>(a,b);
  2115. is.putback(ch);
  2116. is.putback(ch); // we backtrack twice, with the same value
  2117. is >> q; // read "((a,b),q"
  2118. if (!is.good()) goto finish;
  2119. is >> ch; // get the next lexeme
  2120. if (!is.good()) goto finish;
  2121. #ifdef BOOST_NO_STD_LOCALE
  2122. cc = ch;
  2123. #else
  2124. cc = ct.narrow(ch, char());
  2125. #endif /* BOOST_NO_STD_LOCALE */
  2126. if (cc == ')') // read "((a,b),q)"
  2127. {
  2128. o = octonion<T>(p,q);
  2129. }
  2130. else // error
  2131. {
  2132. is.setstate(::std::ios_base::failbit);
  2133. }
  2134. }
  2135. else // read "((a,b),(c" or "((a,b),(e"
  2136. {
  2137. is.putback(ch);
  2138. is >> c;
  2139. if (!is.good()) goto finish;
  2140. is >> ch; // get the next lexeme
  2141. if (!is.good()) goto finish;
  2142. #ifdef BOOST_NO_STD_LOCALE
  2143. cc = ch;
  2144. #else
  2145. cc = ct.narrow(ch, char());
  2146. #endif /* BOOST_NO_STD_LOCALE */
  2147. if (cc == ')') // read "((a,b),(c)" (ambiguity resolution)
  2148. {
  2149. is >> ch; // get the next lexeme
  2150. if (!is.good()) goto finish;
  2151. #ifdef BOOST_NO_STD_LOCALE
  2152. cc = ch;
  2153. #else
  2154. cc = ct.narrow(ch, char());
  2155. #endif /* BOOST_NO_STD_LOCALE */
  2156. if (cc == ')') // read "((a,b),(c))"
  2157. {
  2158. o = octonion<T>(a,b,c);
  2159. }
  2160. else if (cc == ',') // read "((a,b),(c),"
  2161. {
  2162. u = ::std::complex<T>(a,b);
  2163. v = ::std::complex<T>(c);
  2164. is >> x; // read "((a,b),(c),x"
  2165. if (!is.good()) goto finish;
  2166. is >> ch; // get the next lexeme
  2167. if (!is.good()) goto finish;
  2168. #ifdef BOOST_NO_STD_LOCALE
  2169. cc = ch;
  2170. #else
  2171. cc = ct.narrow(ch, char());
  2172. #endif /* BOOST_NO_STD_LOCALE */
  2173. if (cc == ')') // read "((a,b),(c),x)"
  2174. {
  2175. o = octonion<T>(u,v,x);
  2176. }
  2177. else if (cc == ',') // read "((a,b),(c),x,"
  2178. {
  2179. is >> y; // read "((a,b),(c),x,y"
  2180. if (!is.good()) goto finish;
  2181. is >> ch; // get the next lexeme
  2182. if (!is.good()) goto finish;
  2183. #ifdef BOOST_NO_STD_LOCALE
  2184. cc = ch;
  2185. #else
  2186. cc = ct.narrow(ch, char());
  2187. #endif /* BOOST_NO_STD_LOCALE */
  2188. if (cc == ')') // read "((a,b),(c),x,y)"
  2189. {
  2190. o = octonion<T>(u,v,x,y);
  2191. }
  2192. else // error
  2193. {
  2194. is.setstate(::std::ios_base::failbit);
  2195. }
  2196. }
  2197. else // error
  2198. {
  2199. is.setstate(::std::ios_base::failbit);
  2200. }
  2201. }
  2202. else // error
  2203. {
  2204. is.setstate(::std::ios_base::failbit);
  2205. }
  2206. }
  2207. else if (cc == ',') // read "((a,b),(c," or "((a,b),(e,"
  2208. {
  2209. is >> ch; // get the next lexeme
  2210. if (!is.good()) goto finish;
  2211. #ifdef BOOST_NO_STD_LOCALE
  2212. cc = ch;
  2213. #else
  2214. cc = ct.narrow(ch, char());
  2215. #endif /* BOOST_NO_STD_LOCALE */
  2216. if (cc == '(') // read "((a,b),(e,(" (ambiguity resolution)
  2217. {
  2218. u = ::std::complex<T>(a,b);
  2219. x = ::std::complex<T>(c); // "c" is actually "e"
  2220. is.putback(ch);
  2221. is >> y; // read "((a,b),(e,y"
  2222. if (!is.good()) goto finish;
  2223. is >> ch; // get the next lexeme
  2224. if (!is.good()) goto finish;
  2225. #ifdef BOOST_NO_STD_LOCALE
  2226. cc = ch;
  2227. #else
  2228. cc = ct.narrow(ch, char());
  2229. #endif /* BOOST_NO_STD_LOCALE */
  2230. if (cc == ')') // read "((a,b),(e,y)"
  2231. {
  2232. is >> ch; // get the next lexeme
  2233. if (!is.good()) goto finish;
  2234. #ifdef BOOST_NO_STD_LOCALE
  2235. cc = ch;
  2236. #else
  2237. cc = ct.narrow(ch, char());
  2238. #endif /* BOOST_NO_STD_LOCALE */
  2239. if (cc == ')') // read "((a,b),(e,y))"
  2240. {
  2241. o = octonion<T>(u,v,x,y);
  2242. }
  2243. else // error
  2244. {
  2245. is.setstate(::std::ios_base::failbit);
  2246. }
  2247. }
  2248. else // error
  2249. {
  2250. is.setstate(::std::ios_base::failbit);
  2251. }
  2252. }
  2253. else // read "((a,b),(c,d" or "((a,b),(e,f"
  2254. {
  2255. is.putback(ch);
  2256. is >> d;
  2257. if (!is.good()) goto finish;
  2258. is >> ch; // get the next lexeme
  2259. if (!is.good()) goto finish;
  2260. #ifdef BOOST_NO_STD_LOCALE
  2261. cc = ch;
  2262. #else
  2263. cc = ct.narrow(ch, char());
  2264. #endif /* BOOST_NO_STD_LOCALE */
  2265. if (cc == ')') // read "((a,b),(c,d)" (ambiguity resolution)
  2266. {
  2267. u = ::std::complex<T>(a,b);
  2268. v = ::std::complex<T>(c,d);
  2269. is >> ch; // get the next lexeme
  2270. if (!is.good()) goto finish;
  2271. #ifdef BOOST_NO_STD_LOCALE
  2272. cc = ch;
  2273. #else
  2274. cc = ct.narrow(ch, char());
  2275. #endif /* BOOST_NO_STD_LOCALE */
  2276. if (cc == ')') // read "((a,b),(c,d))"
  2277. {
  2278. o = octonion<T>(u,v);
  2279. }
  2280. else if (cc == ',') // read "((a,b),(c,d),"
  2281. {
  2282. is >> x; // read "((a,b),(c,d),x
  2283. if (!is.good()) goto finish;
  2284. is >> ch; // get the next lexeme
  2285. if (!is.good()) goto finish;
  2286. #ifdef BOOST_NO_STD_LOCALE
  2287. cc = ch;
  2288. #else
  2289. cc = ct.narrow(ch, char());
  2290. #endif /* BOOST_NO_STD_LOCALE */
  2291. if (cc == ')') // read "((a,b),(c,d),x)"
  2292. {
  2293. o = octonion<T>(u,v,x);
  2294. }
  2295. else if (cc == ',') // read "((a,b),(c,d),x,"
  2296. {
  2297. is >> y; // read "((a,b),(c,d),x,y"
  2298. if (!is.good()) goto finish;
  2299. is >> ch; // get the next lexeme
  2300. if (!is.good()) goto finish;
  2301. #ifdef BOOST_NO_STD_LOCALE
  2302. cc = ch;
  2303. #else
  2304. cc = ct.narrow(ch, char());
  2305. #endif /* BOOST_NO_STD_LOCALE */
  2306. if (cc == ')') // read "((a,b),(c,d),x,y)"
  2307. {
  2308. o = octonion<T>(u,v,x,y);
  2309. }
  2310. else // error
  2311. {
  2312. is.setstate(::std::ios_base::failbit);
  2313. }
  2314. }
  2315. else // error
  2316. {
  2317. is.setstate(::std::ios_base::failbit);
  2318. }
  2319. }
  2320. else // error
  2321. {
  2322. is.setstate(::std::ios_base::failbit);
  2323. }
  2324. }
  2325. else if (cc == ',') // read "((a,b),(e,f," (ambiguity resolution)
  2326. {
  2327. p = ::boost::math::quaternion<T>(a,b); // too late to backtrack
  2328. is >> g; // read "((a,b),(e,f,g"
  2329. if (!is.good()) goto finish;
  2330. is >> ch; // get the next lexeme
  2331. if (!is.good()) goto finish;
  2332. #ifdef BOOST_NO_STD_LOCALE
  2333. cc = ch;
  2334. #else
  2335. cc = ct.narrow(ch, char());
  2336. #endif /* BOOST_NO_STD_LOCALE */
  2337. if (cc == ')') // read "((a,b),(e,f,g)"
  2338. {
  2339. is >> ch; // get the next lexeme
  2340. if (!is.good()) goto finish;
  2341. #ifdef BOOST_NO_STD_LOCALE
  2342. cc = ch;
  2343. #else
  2344. cc = ct.narrow(ch, char());
  2345. #endif /* BOOST_NO_STD_LOCALE */
  2346. if (cc == ')') // read "((a,b),(e,f,g))"
  2347. {
  2348. q = ::boost::math::quaternion<T>(c,d,g); // "c" is actually "e" and "d" is actually "f"
  2349. o = octonion<T>(p,q);
  2350. }
  2351. else // error
  2352. {
  2353. is.setstate(::std::ios_base::failbit);
  2354. }
  2355. }
  2356. else if (cc == ',') // read "((a,b),(e,f,g,"
  2357. {
  2358. is >> h; // read "((a,b),(e,f,g,h"
  2359. if (!is.good()) goto finish;
  2360. is >> ch; // get the next lexeme
  2361. if (!is.good()) goto finish;
  2362. #ifdef BOOST_NO_STD_LOCALE
  2363. cc = ch;
  2364. #else
  2365. cc = ct.narrow(ch, char());
  2366. #endif /* BOOST_NO_STD_LOCALE */
  2367. if (cc == ')') // read "((a,b),(e,f,g,h)"
  2368. {
  2369. is >> ch; // get the next lexeme
  2370. if (!is.good()) goto finish;
  2371. #ifdef BOOST_NO_STD_LOCALE
  2372. cc = ch;
  2373. #else
  2374. cc = ct.narrow(ch, char());
  2375. #endif /* BOOST_NO_STD_LOCALE */
  2376. if (cc == ')') // read ((a,b),(e,f,g,h))"
  2377. {
  2378. q = ::boost::math::quaternion<T>(c,d,g,h); // "c" is actually "e" and "d" is actually "f"
  2379. o = octonion<T>(p,q);
  2380. }
  2381. else // error
  2382. {
  2383. is.setstate(::std::ios_base::failbit);
  2384. }
  2385. }
  2386. else // error
  2387. {
  2388. is.setstate(::std::ios_base::failbit);
  2389. }
  2390. }
  2391. else // error
  2392. {
  2393. is.setstate(::std::ios_base::failbit);
  2394. }
  2395. }
  2396. else // error
  2397. {
  2398. is.setstate(::std::ios_base::failbit);
  2399. }
  2400. }
  2401. }
  2402. else // error
  2403. {
  2404. is.setstate(::std::ios_base::failbit);
  2405. }
  2406. }
  2407. }
  2408. else // error
  2409. {
  2410. is.setstate(::std::ios_base::failbit);
  2411. }
  2412. }
  2413. else // error
  2414. {
  2415. is.setstate(::std::ios_base::failbit);
  2416. }
  2417. }
  2418. else if (cc == ',') // read "((a,b,"
  2419. {
  2420. is >> c; // read "((a,b,c"
  2421. if (!is.good()) goto finish;
  2422. is >> ch; // get the next lexeme
  2423. if (!is.good()) goto finish;
  2424. #ifdef BOOST_NO_STD_LOCALE
  2425. cc = ch;
  2426. #else
  2427. cc = ct.narrow(ch, char());
  2428. #endif /* BOOST_NO_STD_LOCALE */
  2429. if (cc == ')') // read "((a,b,c)"
  2430. {
  2431. is >> ch; // get the next lexeme
  2432. if (!is.good()) goto finish;
  2433. #ifdef BOOST_NO_STD_LOCALE
  2434. cc = ch;
  2435. #else
  2436. cc = ct.narrow(ch, char());
  2437. #endif /* BOOST_NO_STD_LOCALE */
  2438. if (cc == ')') // read "((a,b,c))"
  2439. {
  2440. o = octonion<T>(a,b,c);
  2441. }
  2442. else if (cc == ',') // read "((a,b,c),"
  2443. {
  2444. p = ::boost::math::quaternion<T>(a,b,c);
  2445. is >> q; // read "((a,b,c),q"
  2446. if (!is.good()) goto finish;
  2447. is >> ch; // get the next lexeme
  2448. if (!is.good()) goto finish;
  2449. #ifdef BOOST_NO_STD_LOCALE
  2450. cc = ch;
  2451. #else
  2452. cc = ct.narrow(ch, char());
  2453. #endif /* BOOST_NO_STD_LOCALE */
  2454. if (cc == ')') // read "((a,b,c),q)"
  2455. {
  2456. o = octonion<T>(p,q);
  2457. }
  2458. else // error
  2459. {
  2460. is.setstate(::std::ios_base::failbit);
  2461. }
  2462. }
  2463. else // error
  2464. {
  2465. is.setstate(::std::ios_base::failbit);
  2466. }
  2467. }
  2468. else if (cc == ',') // read "((a,b,c,"
  2469. {
  2470. is >> d; // read "((a,b,c,d"
  2471. if (!is.good()) goto finish;
  2472. is >> ch; // get the next lexeme
  2473. if (!is.good()) goto finish;
  2474. #ifdef BOOST_NO_STD_LOCALE
  2475. cc = ch;
  2476. #else
  2477. cc = ct.narrow(ch, char());
  2478. #endif /* BOOST_NO_STD_LOCALE */
  2479. if (cc == ')') // read "((a,b,c,d)"
  2480. {
  2481. is >> ch; // get the next lexeme
  2482. if (!is.good()) goto finish;
  2483. #ifdef BOOST_NO_STD_LOCALE
  2484. cc = ch;
  2485. #else
  2486. cc = ct.narrow(ch, char());
  2487. #endif /* BOOST_NO_STD_LOCALE */
  2488. if (cc == ')') // read "((a,b,c,d))"
  2489. {
  2490. o = octonion<T>(a,b,c,d);
  2491. }
  2492. else if (cc == ',') // read "((a,b,c,d),"
  2493. {
  2494. p = ::boost::math::quaternion<T>(a,b,c,d);
  2495. is >> q; // read "((a,b,c,d),q"
  2496. if (!is.good()) goto finish;
  2497. is >> ch; // get the next lexeme
  2498. if (!is.good()) goto finish;
  2499. #ifdef BOOST_NO_STD_LOCALE
  2500. cc = ch;
  2501. #else
  2502. cc = ct.narrow(ch, char());
  2503. #endif /* BOOST_NO_STD_LOCALE */
  2504. if (cc == ')') // read "((a,b,c,d),q)"
  2505. {
  2506. o = octonion<T>(p,q);
  2507. }
  2508. else // error
  2509. {
  2510. is.setstate(::std::ios_base::failbit);
  2511. }
  2512. }
  2513. else // error
  2514. {
  2515. is.setstate(::std::ios_base::failbit);
  2516. }
  2517. }
  2518. else // error
  2519. {
  2520. is.setstate(::std::ios_base::failbit);
  2521. }
  2522. }
  2523. else // error
  2524. {
  2525. is.setstate(::std::ios_base::failbit);
  2526. }
  2527. }
  2528. else // error
  2529. {
  2530. is.setstate(::std::ios_base::failbit);
  2531. }
  2532. }
  2533. }
  2534. else // error
  2535. {
  2536. is.setstate(::std::ios_base::failbit);
  2537. }
  2538. }
  2539. }
  2540. else // read "(a"
  2541. {
  2542. is.putback(ch);
  2543. is >> a; // we extract the first component
  2544. if (!is.good()) goto finish;
  2545. is >> ch; // get the next lexeme
  2546. if (!is.good()) goto finish;
  2547. #ifdef BOOST_NO_STD_LOCALE
  2548. cc = ch;
  2549. #else
  2550. cc = ct.narrow(ch, char());
  2551. #endif /* BOOST_NO_STD_LOCALE */
  2552. if (cc == ')') // read "(a)"
  2553. {
  2554. o = octonion<T>(a);
  2555. }
  2556. else if (cc == ',') // read "(a,"
  2557. {
  2558. is >> ch; // get the next lexeme
  2559. if (!is.good()) goto finish;
  2560. #ifdef BOOST_NO_STD_LOCALE
  2561. cc = ch;
  2562. #else
  2563. cc = ct.narrow(ch, char());
  2564. #endif /* BOOST_NO_STD_LOCALE */
  2565. if (cc == '(') // read "(a,("
  2566. {
  2567. is >> ch; // get the next lexeme
  2568. if (!is.good()) goto finish;
  2569. #ifdef BOOST_NO_STD_LOCALE
  2570. cc = ch;
  2571. #else
  2572. cc = ct.narrow(ch, char());
  2573. #endif /* BOOST_NO_STD_LOCALE */
  2574. if (cc == '(') // read "(a,(("
  2575. {
  2576. p = ::boost::math::quaternion<T>(a);
  2577. is.putback(ch);
  2578. is.putback(ch); // we backtrack twice, with the same value
  2579. is >> q; // read "(a,q"
  2580. if (!is.good()) goto finish;
  2581. is >> ch; // get the next lexeme
  2582. if (!is.good()) goto finish;
  2583. #ifdef BOOST_NO_STD_LOCALE
  2584. cc = ch;
  2585. #else
  2586. cc = ct.narrow(ch, char());
  2587. #endif /* BOOST_NO_STD_LOCALE */
  2588. if (cc == ')') // read "(a,q)"
  2589. {
  2590. o = octonion<T>(p,q);
  2591. }
  2592. else // error
  2593. {
  2594. is.setstate(::std::ios_base::failbit);
  2595. }
  2596. }
  2597. else // read "(a,(c" or "(a,(e"
  2598. {
  2599. is.putback(ch);
  2600. is >> c;
  2601. if (!is.good()) goto finish;
  2602. is >> ch; // get the next lexeme
  2603. if (!is.good()) goto finish;
  2604. #ifdef BOOST_NO_STD_LOCALE
  2605. cc = ch;
  2606. #else
  2607. cc = ct.narrow(ch, char());
  2608. #endif /* BOOST_NO_STD_LOCALE */
  2609. if (cc == ')') // read "(a,(c)" (ambiguity resolution)
  2610. {
  2611. is >> ch; // get the next lexeme
  2612. if (!is.good()) goto finish;
  2613. #ifdef BOOST_NO_STD_LOCALE
  2614. cc = ch;
  2615. #else
  2616. cc = ct.narrow(ch, char());
  2617. #endif /* BOOST_NO_STD_LOCALE */
  2618. if (cc == ')') // read "(a,(c))"
  2619. {
  2620. o = octonion<T>(a,b,c);
  2621. }
  2622. else if (cc == ',') // read "(a,(c),"
  2623. {
  2624. u = ::std::complex<T>(a);
  2625. v = ::std::complex<T>(c);
  2626. is >> x; // read "(a,(c),x"
  2627. if (!is.good()) goto finish;
  2628. is >> ch; // get the next lexeme
  2629. if (!is.good()) goto finish;
  2630. #ifdef BOOST_NO_STD_LOCALE
  2631. cc = ch;
  2632. #else
  2633. cc = ct.narrow(ch, char());
  2634. #endif /* BOOST_NO_STD_LOCALE */
  2635. if (cc == ')') // read "(a,(c),x)"
  2636. {
  2637. o = octonion<T>(u,v,x);
  2638. }
  2639. else if (cc == ',') // read "(a,(c),x,"
  2640. {
  2641. is >> y; // read "(a,(c),x,y"
  2642. if (!is.good()) goto finish;
  2643. is >> ch; // get the next lexeme
  2644. if (!is.good()) goto finish;
  2645. #ifdef BOOST_NO_STD_LOCALE
  2646. cc = ch;
  2647. #else
  2648. cc = ct.narrow(ch, char());
  2649. #endif /* BOOST_NO_STD_LOCALE */
  2650. if (cc == ')') // read "(a,(c),x,y)"
  2651. {
  2652. o = octonion<T>(u,v,x,y);
  2653. }
  2654. else // error
  2655. {
  2656. is.setstate(::std::ios_base::failbit);
  2657. }
  2658. }
  2659. else // error
  2660. {
  2661. is.setstate(::std::ios_base::failbit);
  2662. }
  2663. }
  2664. else // error
  2665. {
  2666. is.setstate(::std::ios_base::failbit);
  2667. }
  2668. }
  2669. else if (cc == ',') // read "(a,(c," or "(a,(e,"
  2670. {
  2671. is >> ch; // get the next lexeme
  2672. if (!is.good()) goto finish;
  2673. #ifdef BOOST_NO_STD_LOCALE
  2674. cc = ch;
  2675. #else
  2676. cc = ct.narrow(ch, char());
  2677. #endif /* BOOST_NO_STD_LOCALE */
  2678. if (cc == '(') // read "(a,(e,(" (ambiguity resolution)
  2679. {
  2680. u = ::std::complex<T>(a);
  2681. x = ::std::complex<T>(c); // "c" is actually "e"
  2682. is.putback(ch); // we backtrack
  2683. is >> y; // read "(a,(e,y"
  2684. if (!is.good()) goto finish;
  2685. is >> ch; // get the next lexeme
  2686. if (!is.good()) goto finish;
  2687. #ifdef BOOST_NO_STD_LOCALE
  2688. cc = ch;
  2689. #else
  2690. cc = ct.narrow(ch, char());
  2691. #endif /* BOOST_NO_STD_LOCALE */
  2692. if (cc == ')') // read "(a,(e,y)"
  2693. {
  2694. is >> ch; // get the next lexeme
  2695. if (!is.good()) goto finish;
  2696. #ifdef BOOST_NO_STD_LOCALE
  2697. cc = ch;
  2698. #else
  2699. cc = ct.narrow(ch, char());
  2700. #endif /* BOOST_NO_STD_LOCALE */
  2701. if (cc == ')') // read "(a,(e,y))"
  2702. {
  2703. o = octonion<T>(u,v,x,y);
  2704. }
  2705. else // error
  2706. {
  2707. is.setstate(::std::ios_base::failbit);
  2708. }
  2709. }
  2710. else // error
  2711. {
  2712. is.setstate(::std::ios_base::failbit);
  2713. }
  2714. }
  2715. else // read "(a,(c,d" or "(a,(e,f"
  2716. {
  2717. is.putback(ch);
  2718. is >> d;
  2719. if (!is.good()) goto finish;
  2720. is >> ch; // get the next lexeme
  2721. if (!is.good()) goto finish;
  2722. #ifdef BOOST_NO_STD_LOCALE
  2723. cc = ch;
  2724. #else
  2725. cc = ct.narrow(ch, char());
  2726. #endif /* BOOST_NO_STD_LOCALE */
  2727. if (cc == ')') // read "(a,(c,d)" (ambiguity resolution)
  2728. {
  2729. is >> ch; // get the next lexeme
  2730. if (!is.good()) goto finish;
  2731. #ifdef BOOST_NO_STD_LOCALE
  2732. cc = ch;
  2733. #else
  2734. cc = ct.narrow(ch, char());
  2735. #endif /* BOOST_NO_STD_LOCALE */
  2736. if (cc == ')') // read "(a,(c,d))"
  2737. {
  2738. o = octonion<T>(a,b,c,d);
  2739. }
  2740. else if (cc == ',') // read "(a,(c,d),"
  2741. {
  2742. u = ::std::complex<T>(a);
  2743. v = ::std::complex<T>(c,d);
  2744. is >> x; // read "(a,(c,d),x"
  2745. if (!is.good()) goto finish;
  2746. is >> ch; // get the next lexeme
  2747. if (!is.good()) goto finish;
  2748. #ifdef BOOST_NO_STD_LOCALE
  2749. cc = ch;
  2750. #else
  2751. cc = ct.narrow(ch, char());
  2752. #endif /* BOOST_NO_STD_LOCALE */
  2753. if (cc == ')') // read "(a,(c,d),x)"
  2754. {
  2755. o = octonion<T>(u,v,x);
  2756. }
  2757. else if (cc == ',') // read "(a,(c,d),x,"
  2758. {
  2759. is >> y; // read "(a,(c,d),x,y"
  2760. if (!is.good()) goto finish;
  2761. is >> ch; // get the next lexeme
  2762. if (!is.good()) goto finish;
  2763. #ifdef BOOST_NO_STD_LOCALE
  2764. cc = ch;
  2765. #else
  2766. cc = ct.narrow(ch, char());
  2767. #endif /* BOOST_NO_STD_LOCALE */
  2768. if (cc == ')') // read "(a,(c,d),x,y)"
  2769. {
  2770. o = octonion<T>(u,v,x,y);
  2771. }
  2772. else // error
  2773. {
  2774. is.setstate(::std::ios_base::failbit);
  2775. }
  2776. }
  2777. else // error
  2778. {
  2779. is.setstate(::std::ios_base::failbit);
  2780. }
  2781. }
  2782. else // error
  2783. {
  2784. is.setstate(::std::ios_base::failbit);
  2785. }
  2786. }
  2787. else if (cc == ',') // read "(a,(e,f," (ambiguity resolution)
  2788. {
  2789. p = ::boost::math::quaternion<T>(a);
  2790. is >> g; // read "(a,(e,f,g"
  2791. if (!is.good()) goto finish;
  2792. is >> ch; // get the next lexeme
  2793. if (!is.good()) goto finish;
  2794. #ifdef BOOST_NO_STD_LOCALE
  2795. cc = ch;
  2796. #else
  2797. cc = ct.narrow(ch, char());
  2798. #endif /* BOOST_NO_STD_LOCALE */
  2799. if (cc == ')') // read "(a,(e,f,g)"
  2800. {
  2801. is >> ch; // get the next lexeme
  2802. if (!is.good()) goto finish;
  2803. #ifdef BOOST_NO_STD_LOCALE
  2804. cc = ch;
  2805. #else
  2806. cc = ct.narrow(ch, char());
  2807. #endif /* BOOST_NO_STD_LOCALE */
  2808. if (cc == ')') // read "(a,(e,f,g))"
  2809. {
  2810. q = ::boost::math::quaternion<T>(c,d,g); // "c" is actually "e" and "d" is actually "f"
  2811. o = octonion<T>(p,q);
  2812. }
  2813. else // error
  2814. {
  2815. is.setstate(::std::ios_base::failbit);
  2816. }
  2817. }
  2818. else if (cc == ',') // read "(a,(e,f,g,"
  2819. {
  2820. is >> h; // read "(a,(e,f,g,h"
  2821. if (!is.good()) goto finish;
  2822. is >> ch; // get the next lexeme
  2823. if (!is.good()) goto finish;
  2824. #ifdef BOOST_NO_STD_LOCALE
  2825. cc = ch;
  2826. #else
  2827. cc = ct.narrow(ch, char());
  2828. #endif /* BOOST_NO_STD_LOCALE */
  2829. if (cc == ')') // read "(a,(e,f,g,h)"
  2830. {
  2831. is >> ch; // get the next lexeme
  2832. if (!is.good()) goto finish;
  2833. #ifdef BOOST_NO_STD_LOCALE
  2834. cc = ch;
  2835. #else
  2836. cc = ct.narrow(ch, char());
  2837. #endif /* BOOST_NO_STD_LOCALE */
  2838. if (cc == ')') // read "(a,(e,f,g,h))"
  2839. {
  2840. q = ::boost::math::quaternion<T>(c,d,g,h); // "c" is actually "e" and "d" is actually "f"
  2841. o = octonion<T>(p,q);
  2842. }
  2843. else // error
  2844. {
  2845. is.setstate(::std::ios_base::failbit);
  2846. }
  2847. }
  2848. else // error
  2849. {
  2850. is.setstate(::std::ios_base::failbit);
  2851. }
  2852. }
  2853. else // error
  2854. {
  2855. is.setstate(::std::ios_base::failbit);
  2856. }
  2857. }
  2858. else // error
  2859. {
  2860. is.setstate(::std::ios_base::failbit);
  2861. }
  2862. }
  2863. }
  2864. else // error
  2865. {
  2866. is.setstate(::std::ios_base::failbit);
  2867. }
  2868. }
  2869. }
  2870. else // read "(a,b" or "(a,c" (ambiguity resolution)
  2871. {
  2872. is.putback(ch);
  2873. is >> b;
  2874. if (!is.good()) goto finish;
  2875. is >> ch; // get the next lexeme
  2876. if (!is.good()) goto finish;
  2877. #ifdef BOOST_NO_STD_LOCALE
  2878. cc = ch;
  2879. #else
  2880. cc = ct.narrow(ch, char());
  2881. #endif /* BOOST_NO_STD_LOCALE */
  2882. if (cc == ')') // read "(a,b)" (ambiguity resolution)
  2883. {
  2884. o = octonion<T>(a,b);
  2885. }
  2886. else if (cc == ',') // read "(a,b," or "(a,c,"
  2887. {
  2888. is >> ch; // get the next lexeme
  2889. if (!is.good()) goto finish;
  2890. #ifdef BOOST_NO_STD_LOCALE
  2891. cc = ch;
  2892. #else
  2893. cc = ct.narrow(ch, char());
  2894. #endif /* BOOST_NO_STD_LOCALE */
  2895. if (cc == '(') // read "(a,c,(" (ambiguity resolution)
  2896. {
  2897. u = ::std::complex<T>(a);
  2898. v = ::std::complex<T>(b); // "b" is actually "c"
  2899. is.putback(ch); // we backtrack
  2900. is >> x; // read "(a,c,x"
  2901. if (!is.good()) goto finish;
  2902. is >> ch; // get the next lexeme
  2903. if (!is.good()) goto finish;
  2904. #ifdef BOOST_NO_STD_LOCALE
  2905. cc = ch;
  2906. #else
  2907. cc = ct.narrow(ch, char());
  2908. #endif /* BOOST_NO_STD_LOCALE */
  2909. if (cc == ')') // read "(a,c,x)"
  2910. {
  2911. o = octonion<T>(u,v,x);
  2912. }
  2913. else if (cc == ',') // read "(a,c,x,"
  2914. {
  2915. is >> y; // read "(a,c,x,y" // read "(a,c,x"
  2916. if (!is.good()) goto finish;
  2917. is >> ch; // get the next lexeme
  2918. if (!is.good()) goto finish;
  2919. #ifdef BOOST_NO_STD_LOCALE
  2920. cc = ch;
  2921. #else
  2922. cc = ct.narrow(ch, char());
  2923. #endif /* BOOST_NO_STD_LOCALE */
  2924. if (cc == ')') // read "(a,c,x,y)"
  2925. {
  2926. o = octonion<T>(u,v,x,y);
  2927. }
  2928. else // error
  2929. {
  2930. is.setstate(::std::ios_base::failbit);
  2931. }
  2932. }
  2933. else // error
  2934. {
  2935. is.setstate(::std::ios_base::failbit);
  2936. }
  2937. }
  2938. else // read "(a,b,c" or "(a,c,e"
  2939. {
  2940. is.putback(ch);
  2941. is >> c;
  2942. if (!is.good()) goto finish;
  2943. is >> ch; // get the next lexeme
  2944. if (!is.good()) goto finish;
  2945. #ifdef BOOST_NO_STD_LOCALE
  2946. cc = ch;
  2947. #else
  2948. cc = ct.narrow(ch, char());
  2949. #endif /* BOOST_NO_STD_LOCALE */
  2950. if (cc == ')') // read "(a,b,c)" (ambiguity resolution)
  2951. {
  2952. o = octonion<T>(a,b,c);
  2953. }
  2954. else if (cc == ',') // read "(a,b,c," or "(a,c,e,"
  2955. {
  2956. is >> ch; // get the next lexeme
  2957. if (!is.good()) goto finish;
  2958. #ifdef BOOST_NO_STD_LOCALE
  2959. cc = ch;
  2960. #else
  2961. cc = ct.narrow(ch, char());
  2962. #endif /* BOOST_NO_STD_LOCALE */
  2963. if (cc == '(') // read "(a,c,e,(") (ambiguity resolution)
  2964. {
  2965. u = ::std::complex<T>(a);
  2966. v = ::std::complex<T>(b); // "b" is actually "c"
  2967. x = ::std::complex<T>(c); // "c" is actually "e"
  2968. is.putback(ch); // we backtrack
  2969. is >> y; // read "(a,c,e,y"
  2970. if (!is.good()) goto finish;
  2971. is >> ch; // get the next lexeme
  2972. if (!is.good()) goto finish;
  2973. #ifdef BOOST_NO_STD_LOCALE
  2974. cc = ch;
  2975. #else
  2976. cc = ct.narrow(ch, char());
  2977. #endif /* BOOST_NO_STD_LOCALE */
  2978. if (cc == ')') // read "(a,c,e,y)"
  2979. {
  2980. o = octonion<T>(u,v,x,y);
  2981. }
  2982. else // error
  2983. {
  2984. is.setstate(::std::ios_base::failbit);
  2985. }
  2986. }
  2987. else // read "(a,b,c,d" (ambiguity resolution)
  2988. {
  2989. is.putback(ch); // we backtrack
  2990. is >> d;
  2991. if (!is.good()) goto finish;
  2992. is >> ch; // get the next lexeme
  2993. if (!is.good()) goto finish;
  2994. #ifdef BOOST_NO_STD_LOCALE
  2995. cc = ch;
  2996. #else
  2997. cc = ct.narrow(ch, char());
  2998. #endif /* BOOST_NO_STD_LOCALE */
  2999. if (cc == ')') // read "(a,b,c,d)"
  3000. {
  3001. o = octonion<T>(a,b,c,d);
  3002. }
  3003. else if (cc == ',') // read "(a,b,c,d,"
  3004. {
  3005. is >> e; // read "(a,b,c,d,e"
  3006. if (!is.good()) goto finish;
  3007. is >> ch; // get the next lexeme
  3008. if (!is.good()) goto finish;
  3009. #ifdef BOOST_NO_STD_LOCALE
  3010. cc = ch;
  3011. #else
  3012. cc = ct.narrow(ch, char());
  3013. #endif /* BOOST_NO_STD_LOCALE */
  3014. if (cc == ')') // read "(a,b,c,d,e)"
  3015. {
  3016. o = octonion<T>(a,b,c,d,e);
  3017. }
  3018. else if (cc == ',') // read "(a,b,c,d,e,"
  3019. {
  3020. is >> f; // read "(a,b,c,d,e,f"
  3021. if (!is.good()) goto finish;
  3022. is >> ch; // get the next lexeme
  3023. if (!is.good()) goto finish;
  3024. #ifdef BOOST_NO_STD_LOCALE
  3025. cc = ch;
  3026. #else
  3027. cc = ct.narrow(ch, char());
  3028. #endif /* BOOST_NO_STD_LOCALE */
  3029. if (cc == ')') // read "(a,b,c,d,e,f)"
  3030. {
  3031. o = octonion<T>(a,b,c,d,e,f);
  3032. }
  3033. else if (cc == ',') // read "(a,b,c,d,e,f,"
  3034. {
  3035. is >> g; // read "(a,b,c,d,e,f,g" // read "(a,b,c,d,e,f"
  3036. if (!is.good()) goto finish;
  3037. is >> ch; // get the next lexeme
  3038. if (!is.good()) goto finish;
  3039. #ifdef BOOST_NO_STD_LOCALE
  3040. cc = ch;
  3041. #else
  3042. cc = ct.narrow(ch, char());
  3043. #endif /* BOOST_NO_STD_LOCALE */
  3044. if (cc == ')') // read "(a,b,c,d,e,f,g)"
  3045. {
  3046. o = octonion<T>(a,b,c,d,e,f,g);
  3047. }
  3048. else if (cc == ',') // read "(a,b,c,d,e,f,g,"
  3049. {
  3050. is >> h; // read "(a,b,c,d,e,f,g,h" // read "(a,b,c,d,e,f,g" // read "(a,b,c,d,e,f"
  3051. if (!is.good()) goto finish;
  3052. is >> ch; // get the next lexeme
  3053. if (!is.good()) goto finish;
  3054. #ifdef BOOST_NO_STD_LOCALE
  3055. cc = ch;
  3056. #else
  3057. cc = ct.narrow(ch, char());
  3058. #endif /* BOOST_NO_STD_LOCALE */
  3059. if (cc == ')') // read "(a,b,c,d,e,f,g,h)"
  3060. {
  3061. o = octonion<T>(a,b,c,d,e,f,g,h);
  3062. }
  3063. else // error
  3064. {
  3065. is.setstate(::std::ios_base::failbit);
  3066. }
  3067. }
  3068. else // error
  3069. {
  3070. is.setstate(::std::ios_base::failbit);
  3071. }
  3072. }
  3073. else // error
  3074. {
  3075. is.setstate(::std::ios_base::failbit);
  3076. }
  3077. }
  3078. else // error
  3079. {
  3080. is.setstate(::std::ios_base::failbit);
  3081. }
  3082. }
  3083. else // error
  3084. {
  3085. is.setstate(::std::ios_base::failbit);
  3086. }
  3087. }
  3088. }
  3089. else // error
  3090. {
  3091. is.setstate(::std::ios_base::failbit);
  3092. }
  3093. }
  3094. }
  3095. else // error
  3096. {
  3097. is.setstate(::std::ios_base::failbit);
  3098. }
  3099. }
  3100. }
  3101. else // error
  3102. {
  3103. is.setstate(::std::ios_base::failbit);
  3104. }
  3105. }
  3106. }
  3107. else // format: a
  3108. {
  3109. is.putback(ch);
  3110. is >> a; // we extract the first component
  3111. if (!is.good()) goto finish;
  3112. o = octonion<T>(a);
  3113. }
  3114. finish:
  3115. return(is);
  3116. }
  3117. template<typename T, typename charT, class traits>
  3118. ::std::basic_ostream<charT,traits> & operator << ( ::std::basic_ostream<charT,traits> & os,
  3119. octonion<T> const & o)
  3120. {
  3121. ::std::basic_ostringstream<charT,traits> s;
  3122. s.flags(os.flags());
  3123. #ifdef BOOST_NO_STD_LOCALE
  3124. #else
  3125. s.imbue(os.getloc());
  3126. #endif /* BOOST_NO_STD_LOCALE */
  3127. s.precision(os.precision());
  3128. s << '(' << o.R_component_1() << ','
  3129. << o.R_component_2() << ','
  3130. << o.R_component_3() << ','
  3131. << o.R_component_4() << ','
  3132. << o.R_component_5() << ','
  3133. << o.R_component_6() << ','
  3134. << o.R_component_7() << ','
  3135. << o.R_component_8() << ')';
  3136. return os << s.str();
  3137. }
  3138. // values
  3139. template<typename T>
  3140. inline T real(octonion<T> const & o)
  3141. {
  3142. return(o.real());
  3143. }
  3144. template<typename T>
  3145. inline octonion<T> unreal(octonion<T> const & o)
  3146. {
  3147. return(o.unreal());
  3148. }
  3149. #define BOOST_OCTONION_VALARRAY_LOADER \
  3150. using ::std::valarray; \
  3151. \
  3152. valarray<T> temp(8); \
  3153. \
  3154. temp[0] = o.R_component_1(); \
  3155. temp[1] = o.R_component_2(); \
  3156. temp[2] = o.R_component_3(); \
  3157. temp[3] = o.R_component_4(); \
  3158. temp[4] = o.R_component_5(); \
  3159. temp[5] = o.R_component_6(); \
  3160. temp[6] = o.R_component_7(); \
  3161. temp[7] = o.R_component_8();
  3162. template<typename T>
  3163. inline T sup(octonion<T> const & o)
  3164. {
  3165. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  3166. using ::std::abs;
  3167. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  3168. BOOST_OCTONION_VALARRAY_LOADER
  3169. return((abs(temp).max)());
  3170. }
  3171. template<typename T>
  3172. inline T l1(octonion<T> const & o)
  3173. {
  3174. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  3175. using ::std::abs;
  3176. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  3177. BOOST_OCTONION_VALARRAY_LOADER
  3178. return(abs(temp).sum());
  3179. }
  3180. template<typename T>
  3181. inline T abs(const octonion<T> & o)
  3182. {
  3183. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  3184. using ::std::abs;
  3185. #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
  3186. using ::std::sqrt;
  3187. BOOST_OCTONION_VALARRAY_LOADER
  3188. T maxim = (abs(temp).max)(); // overflow protection
  3189. if (maxim == static_cast<T>(0))
  3190. {
  3191. return(maxim);
  3192. }
  3193. else
  3194. {
  3195. T mixam = static_cast<T>(1)/maxim; // prefer multiplications over divisions
  3196. temp *= mixam;
  3197. temp *= temp;
  3198. return(maxim*sqrt(temp.sum()));
  3199. }
  3200. //return(::std::sqrt(norm(o)));
  3201. }
  3202. #undef BOOST_OCTONION_VALARRAY_LOADER
  3203. // Note: This is the Cayley norm, not the Euclidian norm...
  3204. template<typename T>
  3205. inline T norm(octonion<T> const & o)
  3206. {
  3207. return(real(o*conj(o)));
  3208. }
  3209. template<typename T>
  3210. inline octonion<T> conj(octonion<T> const & o)
  3211. {
  3212. return(octonion<T>( +o.R_component_1(),
  3213. -o.R_component_2(),
  3214. -o.R_component_3(),
  3215. -o.R_component_4(),
  3216. -o.R_component_5(),
  3217. -o.R_component_6(),
  3218. -o.R_component_7(),
  3219. -o.R_component_8()));
  3220. }
  3221. // Note: There is little point, for the octonions, to introduce the equivalents
  3222. // to the complex "arg" and the quaternionic "cylindropolar".
  3223. template<typename T>
  3224. inline octonion<T> spherical(T const & rho,
  3225. T const & theta,
  3226. T const & phi1,
  3227. T const & phi2,
  3228. T const & phi3,
  3229. T const & phi4,
  3230. T const & phi5,
  3231. T const & phi6)
  3232. {
  3233. using ::std::cos;
  3234. using ::std::sin;
  3235. //T a = cos(theta)*cos(phi1)*cos(phi2)*cos(phi3)*cos(phi4)*cos(phi5)*cos(phi6);
  3236. //T b = sin(theta)*cos(phi1)*cos(phi2)*cos(phi3)*cos(phi4)*cos(phi5)*cos(phi6);
  3237. //T c = sin(phi1)*cos(phi2)*cos(phi3)*cos(phi4)*cos(phi5)*cos(phi6);
  3238. //T d = sin(phi2)*cos(phi3)*cos(phi4)*cos(phi5)*cos(phi6);
  3239. //T e = sin(phi3)*cos(phi4)*cos(phi5)*cos(phi6);
  3240. //T f = sin(phi4)*cos(phi5)*cos(phi6);
  3241. //T g = sin(phi5)*cos(phi6);
  3242. //T h = sin(phi6);
  3243. T courrant = static_cast<T>(1);
  3244. T h = sin(phi6);
  3245. courrant *= cos(phi6);
  3246. T g = sin(phi5)*courrant;
  3247. courrant *= cos(phi5);
  3248. T f = sin(phi4)*courrant;
  3249. courrant *= cos(phi4);
  3250. T e = sin(phi3)*courrant;
  3251. courrant *= cos(phi3);
  3252. T d = sin(phi2)*courrant;
  3253. courrant *= cos(phi2);
  3254. T c = sin(phi1)*courrant;
  3255. courrant *= cos(phi1);
  3256. T b = sin(theta)*courrant;
  3257. T a = cos(theta)*courrant;
  3258. return(rho*octonion<T>(a,b,c,d,e,f,g,h));
  3259. }
  3260. template<typename T>
  3261. inline octonion<T> multipolar(T const & rho1,
  3262. T const & theta1,
  3263. T const & rho2,
  3264. T const & theta2,
  3265. T const & rho3,
  3266. T const & theta3,
  3267. T const & rho4,
  3268. T const & theta4)
  3269. {
  3270. using ::std::cos;
  3271. using ::std::sin;
  3272. T a = rho1*cos(theta1);
  3273. T b = rho1*sin(theta1);
  3274. T c = rho2*cos(theta2);
  3275. T d = rho2*sin(theta2);
  3276. T e = rho3*cos(theta3);
  3277. T f = rho3*sin(theta3);
  3278. T g = rho4*cos(theta4);
  3279. T h = rho4*sin(theta4);
  3280. return(octonion<T>(a,b,c,d,e,f,g,h));
  3281. }
  3282. template<typename T>
  3283. inline octonion<T> cylindrical(T const & r,
  3284. T const & angle,
  3285. T const & h1,
  3286. T const & h2,
  3287. T const & h3,
  3288. T const & h4,
  3289. T const & h5,
  3290. T const & h6)
  3291. {
  3292. using ::std::cos;
  3293. using ::std::sin;
  3294. T a = r*cos(angle);
  3295. T b = r*sin(angle);
  3296. return(octonion<T>(a,b,h1,h2,h3,h4,h5,h6));
  3297. }
  3298. template<typename T>
  3299. inline octonion<T> exp(octonion<T> const & o)
  3300. {
  3301. using ::std::exp;
  3302. using ::std::cos;
  3303. using ::boost::math::sinc_pi;
  3304. T u = exp(real(o));
  3305. T z = abs(unreal(o));
  3306. T w = sinc_pi(z);
  3307. return(u*octonion<T>(cos(z),
  3308. w*o.R_component_2(), w*o.R_component_3(),
  3309. w*o.R_component_4(), w*o.R_component_5(),
  3310. w*o.R_component_6(), w*o.R_component_7(),
  3311. w*o.R_component_8()));
  3312. }
  3313. template<typename T>
  3314. inline octonion<T> cos(octonion<T> const & o)
  3315. {
  3316. using ::std::sin;
  3317. using ::std::cos;
  3318. using ::std::cosh;
  3319. using ::boost::math::sinhc_pi;
  3320. T z = abs(unreal(o));
  3321. T w = -sin(o.real())*sinhc_pi(z);
  3322. return(octonion<T>(cos(o.real())*cosh(z),
  3323. w*o.R_component_2(), w*o.R_component_3(),
  3324. w*o.R_component_4(), w*o.R_component_5(),
  3325. w*o.R_component_6(), w*o.R_component_7(),
  3326. w*o.R_component_8()));
  3327. }
  3328. template<typename T>
  3329. inline octonion<T> sin(octonion<T> const & o)
  3330. {
  3331. using ::std::sin;
  3332. using ::std::cos;
  3333. using ::std::cosh;
  3334. using ::boost::math::sinhc_pi;
  3335. T z = abs(unreal(o));
  3336. T w = +cos(o.real())*sinhc_pi(z);
  3337. return(octonion<T>(sin(o.real())*cosh(z),
  3338. w*o.R_component_2(), w*o.R_component_3(),
  3339. w*o.R_component_4(), w*o.R_component_5(),
  3340. w*o.R_component_6(), w*o.R_component_7(),
  3341. w*o.R_component_8()));
  3342. }
  3343. template<typename T>
  3344. inline octonion<T> tan(octonion<T> const & o)
  3345. {
  3346. return(sin(o)/cos(o));
  3347. }
  3348. template<typename T>
  3349. inline octonion<T> cosh(octonion<T> const & o)
  3350. {
  3351. return((exp(+o)+exp(-o))/static_cast<T>(2));
  3352. }
  3353. template<typename T>
  3354. inline octonion<T> sinh(octonion<T> const & o)
  3355. {
  3356. return((exp(+o)-exp(-o))/static_cast<T>(2));
  3357. }
  3358. template<typename T>
  3359. inline octonion<T> tanh(octonion<T> const & o)
  3360. {
  3361. return(sinh(o)/cosh(o));
  3362. }
  3363. template<typename T>
  3364. octonion<T> pow(octonion<T> const & o,
  3365. int n)
  3366. {
  3367. if (n > 1)
  3368. {
  3369. int m = n>>1;
  3370. octonion<T> result = pow(o, m);
  3371. result *= result;
  3372. if (n != (m<<1))
  3373. {
  3374. result *= o; // n odd
  3375. }
  3376. return(result);
  3377. }
  3378. else if (n == 1)
  3379. {
  3380. return(o);
  3381. }
  3382. else if (n == 0)
  3383. {
  3384. return(octonion<T>(static_cast<T>(1)));
  3385. }
  3386. else /* n < 0 */
  3387. {
  3388. return(pow(octonion<T>(static_cast<T>(1))/o,-n));
  3389. }
  3390. }
  3391. // helper templates for converting copy constructors (definition)
  3392. namespace detail
  3393. {
  3394. template< typename T,
  3395. typename U
  3396. >
  3397. octonion<T> octonion_type_converter(octonion<U> const & rhs)
  3398. {
  3399. return(octonion<T>( static_cast<T>(rhs.R_component_1()),
  3400. static_cast<T>(rhs.R_component_2()),
  3401. static_cast<T>(rhs.R_component_3()),
  3402. static_cast<T>(rhs.R_component_4()),
  3403. static_cast<T>(rhs.R_component_5()),
  3404. static_cast<T>(rhs.R_component_6()),
  3405. static_cast<T>(rhs.R_component_7()),
  3406. static_cast<T>(rhs.R_component_8())));
  3407. }
  3408. }
  3409. }
  3410. }
  3411. #endif /* BOOST_OCTONION_HPP */