-
Notifications
You must be signed in to change notification settings - Fork 0
/
nion.hpp
874 lines (759 loc) · 34.2 KB
/
nion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*
Copyright 2023 Marcus Dante Liebenthal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef NION_HPP
#define NION_HPP
#include <cmath>
#include <limits>
#include <type_traits>
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <concepts>
#include <vector>
#include <array>
#include "niolloc.hpp"
// macro to determine if heap should be used for memory (zero enables heap)
#define NION_USE_HEAP 0
/**
* @file nion.hpp
* @brief Templated class that implements Cayley-Dickson division algebra.
* @author Marcus Dante Liebenthal
* @version 1.0
* @date 2022-10-08
*/
template<arith_ops T, // type of the coefficients (required to have arithmetic operations)
std::size_t N = NION_USE_HEAP> // type of the size
// (default is zero, where heap is used for memory instead of stack)
struct nion; // forward declaration
// create a concept that checks if a type is the same as this nion type
template<typename S, typename T, std::size_t N>
concept not_nion = !std::is_same_v<T, nion<T, N>>;
template<arith_ops T, std::size_t N>
struct nion {
// determine the minimum number of bits required to represent N at compile time
static constexpr bool on_heap = N == NION_USE_HEAP; // determine if heap should be used for memory
niolloc<T,N> elem_{}; // declare the memory allocation object
using D = niolloc<T,N>::D; // declare the type of the memory allocation object
D size_ = elem_.size_; // declare the size of the memory allocation object (Should be synced with the allocator)
constexpr inline niolloc<T,N> &elem() { return elem_; } // return a reference to the memory allocation object
constexpr inline const niolloc<T,N> &elem() const { return elem_; } // return a const reference to the memory allocation object
constexpr inline niolloc<T,N>::D size() const { return elem_.size_; } // return the size of the memory allocation object
/***************************
* NION CONSTRUCTORS
***************************/
/**
* @brief Default constructor.
* @details Constructs a nion object of size 1 with all coefficients set to 0.
*/
constexpr inline nion<T,N>() {
elem_ = niolloc<T,N>(); // default allocate memory
size_ = size(); // sync size with allocator
}
/**
* @brief Destroy the nion object
*/
constexpr inline ~nion() = default; // niolloc destructor is called automatically
/**
* @brief cast nion if both arith_ops types are different and max size is different
*/
template<arith_ops S, std::size_t M>
constexpr inline explicit operator nion<S,M>(){
nion<S,M> out;
out.elem_ = elem_; // copy memory (automatic type conversion from niolloc)
out.size_ = out.size(); // sync size with allocator
return out;
}
/**
* @brief Construct a new nion object from vector
* @param components The components of the nion.
* @param size The size of the nion.
* @details Constructs a nion object from a vector of components.
* @note The size of the nion is the number of components.
* @note The size of the nion must be greater than zero.
*/
template <arith_ops S> requires (std::is_convertible_v<S, T>)
constexpr inline nion<T,N>(const S *vals, std::size_t size){ elem_ = niolloc<T,N>(vals, size); size_ = this->size(); }
/**
* @brief Construct a new nion object from an initializer list.
* @param components The components of the nion as an initializer list.
*/
constexpr inline nion<T,N>(const std::initializer_list<T> &vals) { elem_ = niolloc<T,N>(vals); size_ = size(); }
/**
* @brief Construct a new nion object from a vector
* @param components The components of the nion as a vector.
*/
constexpr inline explicit nion<T,N>(const std::vector<T> &vals) { elem_ = niolloc<T,N>(vals); size_ = size(); }
/**
* @brief Construct a new nion object from a scalar
* @param scalar The value to fill the nion with.
*/
template<not_nion<T,N> S> requires (std::is_convertible_v<S,T> && !std::is_integral_v<S>)
constexpr inline explicit nion<T,N>(const S &scalar) { elem_ = scalar; size_ = size(); } // return the nion
/**
* @brief Construct an empty nion object
* @param size The size of the nion.
*/
template<integral_type Z>
constexpr inline explicit nion<T,N>(Z size) { elem_ = niolloc<T,N>(size); size_ = this->size(); }
/**
* @brief fill nion with a value
* @param val The value to fill the nion with.
*/
template<arith_ops S> requires (std::is_convertible_v<S, T>)
constexpr inline void fill(const S &val){ elem_.fill(val); }
/**
* @brief zero nion
*/
constexpr inline void zero(){
fill(T()); // set all coefficients to zero (default value)
};
constexpr inline nion<T,N>(const nion<T,N> &other) = default; // copy constructor
constexpr inline nion<T,N> &operator=(const nion<T,N> &other) = default; // copy assignment operator
constexpr inline nion<T,N>(nion<T,N> &&other) noexcept = default; // move constructor
constexpr inline nion<T,N> &operator=(nion<T,N> &&other) noexcept = default; // move assignment operator
/// constructors and assignments for different nion types
template<arith_ops S, std::size_t M> requires ( std::is_convertible_v<S,T> && (!std::is_same_v<T,S> || N != M) )
constexpr inline explicit nion<T,N>(const nion<S,M> &other) { elem_ = other.elem_; size_ = size(); } //copy constructor
template<arith_ops S, std::size_t M> requires ( std::is_convertible_v<S,T> && (!std::is_same_v<T,S> || N != M) )
constexpr inline nion<T,N> &operator=(const nion<S,M> &other) { elem_ = other.elem_; size_ = size(); } //copy assignment operator
template<arith_ops S, std::size_t M> requires ( std::is_convertible_v<S,T> && (!std::is_same_v<T,S> || N != M) )
constexpr inline explicit nion<T,N>(nion<S,M> &&other) noexcept { elem_ = std::move(other.elem_); size_ = size(); } //move constructor
template<arith_ops S, std::size_t M> requires ( std::is_convertible_v<S,T> && (!std::is_same_v<T,S> || N != M) )
constexpr inline nion<T,N> &operator=(nion<S,M> &&other) noexcept { elem_ = std::move(other.elem_); size_ = size(); } //move assignment operator
/**
* @brief Construct a new nion object from a scalar with no imaginary components.
* @param size The size of the nion.
* @param scalar The scalar value of the nion.
* @return nion<T,N> The nion object.
* @note This is a convenience function for creating a nion from a scalar.
*/
template<not_nion<T,N> S = T, integral_type Z> requires (std::is_convertible_v<S,T> && !std::is_pointer_v<S>)
constexpr inline nion<T,N>(const S &realVal, Z size) {
elem_ = niolloc<T,N>(size); // allocate memory
zero(); // set all coefficients to zero (default value)
elem_[0] = realVal; // set "real" component
size_ = this->size(); // sync size with allocator
}
/**
* @brief Construct nion from half size nions. q = (a,b)
* @param a The first half size nion in pair.
* @param b The second half size nion in pair.
* @return The nion constructed from the half size nions.
* @note This is a convenience function for constructing a nion from pairing two half size nions.
*/
template< arith_ops S, arith_ops U, std::size_t M, std::size_t P> requires (std::is_convertible_v<S,T> && std::is_convertible_v<U,T>)
static constexpr inline nion<T,N> make_pair(const nion<S,M> &a, const nion<U,P> &b);
/***************************
* NION OPERATORS *
***************************/
/**
* @brief resizes the nion to the given size.
* @param size The new size of the nion.
*/
template<integral_type Z>
constexpr inline void resize(Z size);
/**
* @brief convert scalar to nion
* @param scalar The scalar to convert to a nion.
* @return The nion constructed from the scalar.
* @note This is a convenience function for constructing a nion from a scalar.
*/
template<not_nion<T,N> S> requires (std::is_convertible_v<S,T> && !std::is_integral_v<S>)
constexpr inline nion<T,N> &operator=(const S &scalar) { elem_ = scalar; size_ = size(); return *this; }
/**
* @brief overload the - operator for a nion.
* @return The negation of the nion.
*/
constexpr inline nion<T,N> operator-() const;
/**
* @brief overload the [] operator for a nion.
* @param index The index of the component to get.
* @return The component at the index passed by value.
*/
constexpr inline T &operator[](D index);
constexpr inline const T &operator[](D index) const;
/**
* @brief Get the conjugate of the nion.
* @return The conjugate of the nion.
* @detail (a,b)* = (a*,-b)
*/
constexpr inline nion<T,N> conj() const;
/**
* @brief make this nion the conjugate of itself.
* @return reference to this nion.
* @detail (a,b)* = (a*,-b)
*/
constexpr inline void conj_inplace();
/**
* @brief overload the += operator for nions.
* @param other The nion to add to this nion.
* @return The sum of this nion and the other nion inplace.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline void operator+=(const nion<T,M> &other);
/**
* @breif overload the -= operator for nions.
* @param other The nion to substract from this nion.
* @return The subtraction of this nion and the other nion inplace.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline void operator-=(const nion<T,M> &other);
/**
* @breif overload the *= operator for nions.
* @param other The nion to multiply this nion by.
* @return The product of this nion and the other nion inplace.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline void operator*=(const nion<T,M> &other);
/**
* @breif overload the /= operator for nions.
* @param other The nion to divide this nion by.
* @return The division of this nion and the other nion inplace.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline void operator/=(const nion<T,M> &other);
/**
* @brief overload the + operator for nions.
* @param other The nion to add to this nion.
* @return The sum of this nion and the other nion.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline nion<T,N> operator+(const nion<T,M> &other) const;
/**
* @brief overload the - operator for nions.
* @param other The nion to substract this nion by.
* @return The subtraction of this nion and the other nion.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline nion<T,N> operator-(const nion<T,M> &other) const;
/**
* @brief overload the * operator for nions.
* @param other The nion to multiply this nion by.
* @return The product of this nion and the other nion.
* @details The product of two nions is defined as (a,b)(c,d) = (a c - d* b, d a + b c*) = (z,w), where * is the conjugate.
* and a, b, c, d are the nions with half the size of the original nions.
* @note product has the same size as the larger size of the two nions.
* @note This is recursive function and will call itself until the size is 1.
*/
template<arith_ops S, std::size_t M> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> operator*(const nion<S,M> &other) const;
/**
* @brief compute the inverse of the nion.
* @return The inverse of the nion.
*/
constexpr inline nion<T,N> inv() const;
/**
* @brief overload the / operator for nions.
* @param other The nion to divide this nion by.
* @return The division of this nion and the other nion.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline nion<T,N> operator/(const nion<T,M> &other) const;
/**
* @brief absolute value of the nion.
* @return The absolute value of the nion.
* @details The absolute value of the nion is the sum of the squares of its components.
*/
constexpr inline T abs() const;
/**
* @brief norm of the nion.
* @return The norm of the nion.
* @details The norm of a nion is the sqrt of the sum of the squares of its components.
*/
constexpr inline T norm() const;
/**
* @brief shortest rotation of the nion to the real axis.
* @tparam T type of the nion.
* @param nion The nion to rotate
* @return The shortest rotation of the nion to the real axis (preserves the norm and sign of the real component).
* @details computes the length of the nion and does the shortest rotation to the real line.
*/
constexpr inline T rotate_real() const;
/**
* @brief return real part of the nion.
* @return The real part of the nion.
*/
constexpr inline T real() const;
/**
* @brief Calculate the imaginary components of a nion.
* @return The imaginary components of the nion.
*/
constexpr inline nion<T,N> imag() const;
/**
* @brief Compute the argument of the nion.
* @return The argument of the nion.
* @details The argument of a nion is the angle between the real axis and the nion.
*/
constexpr inline T arg() const;
/**
* @brief Compute the unit nion.
* @return The square magnitude of the nion, the square magnitude of the angle, and the unit nion as a tuple.
*/
constexpr std::tuple<T, T, nion<T,N>> polar() const;
/**
* @brief overload the == operator for nions.
* @param other The nion to compare this nion to.
* @return True if the nions are equal, false otherwise.
* @details Two nions are equal if they have the same size and the same components.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator==(const nion<T,M> &other) const;
/**
* @brief overload the != operator for nions.
* @param other The nion to compare this nion to.
* @return True if the nions are not equal, false otherwise.
* @details Two nions are equal if they have the same size and the same components.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator!=(const nion<T,M> &other) const;
/**
* @brief overload the > operator for nions.
* @param other The nion to compare this nion to.
* @return True if this nion is greater than the other nion, false otherwise.
* @details sorting is undefined for nions with sizes greater than 1. However, we can still compare
* nions with sizes greater than 1 by comparing the projections of the nions onto the real line.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator>(const nion<T,M> &other) const;
/**
* @brief overload the < operator for nions.
* @param other The nion to compare this nion to.
* @return True if this nion is less than the other nion, false otherwise.
* @details sorting is undefined for nions with sizes greater than 1. However, we can still compare
* nions with sizes greater than 1 by comparing the rotations of the nions onto the real line.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator<(const nion<T,M> &other) const;
/**
* @brief overload the >= operator for nions.
* @param other The nion to compare this nion to.
* @return True if this nion is greater than or equal to the other nion, false otherwise.
* @details sorting is undefined for nions with sizes greater than 1. However, we can still compare
* nions with sizes greater than 1 by comparing the rotations of the nions onto the real line.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator>=(const nion<T,M> &other) const;
/**
* @brief overload the <= operator for nions.
* @param other The nion to compare this nion to.
* @return True if this nion is less than or equal to the other nion, false otherwise.
* @details sorting is undefined for nions with sizes greater than 1. However, we can still compare
* nions with sizes greater than 1 by comparing the rotations of the nions onto the real line.
*/
template<std::size_t M> // M is the size of the other nion.
constexpr inline bool operator<=(const nion<T,M> &other) const;
/**
* @brief overload the + operator for nions with scalars.
* @tparam S The type of the scalar.
* @param scalar The scalar to add this nion by.
* @return The sum of this nion and the scalar.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> operator+(S scalar) const;
/**
* @brief overload the ++ operator for nions.
* @return The nion incremented by 1.
*/
constexpr inline nion<T,N> &operator++();
/**
* @brief overload the - operator for nions with scalars.
* @tparam S The type of the scalar.
* @param scalar The scalar to subtract this nion by.
* @return The subtraction of this nion and the scalar.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> operator-(S scalar) const;
/**
* @brief overload the -- operator for nions.
* @return The real component of the nion decremented by 1.
*/
constexpr inline nion<T,N> &operator--();
/**
* @brief overload the * operator for nions with scalars.
* @tparam S The type of the scalar.
* @param scalar The scalar to multiply this nion by.
* @return The product of this nion and the scalar.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> operator*(S scalar) const;
/**
* @brief overload the / operator for nions with scalars.
* @tparam S The type of the scalar.
* @param scalar The scalar to divide this nion by.
* @return The division of this nion and the scalar.
*/
template<not_nion<T,N> S> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> operator/(S scalar) const;
/**
* @brief overload the += operator for nions with scalars.
* @tparam S type of the scalar.
* @param other The scalar to add to this nion.
* @return The sum of this nion and the scalar inplace.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline void operator+=(S scalar);
/**
* @brief overload the -= operator for nions with scalars.
* @tparam S type of the scalar.
* @param other The scalar to substract from this nion.
* @return The subtraction of this nion and the scalar inplace.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline void operator-=(S scalar);
/**
* @breif overload the *= operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to multiply this nion by.
* @return The product of this nion and the scalar inplace.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline void operator*=(S scalar);
/**
* @breif overload the /= operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to divide this nion by.
* @return The division of this nion and the scalar inplace.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline void operator/=(S scalar);
/**
* @brief overload the == operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return True if the nion is equal to the scalar, false otherwise.
* @details A nion is equal to a scalar if the scalar is equal to the first component of the nion
* and all other components are equal to zero.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator==(S scalar) const;
/**
* @brief overload the != operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return False if the nion is equal to the scalar, true otherwise.
* @details A nion is equal to a scalar if the scalar is equal to the first component of the nion
* and all other components are equal to zero.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator!=(S scalar) const;
/**
* @brief overload the > operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return True if the nion is greater than the scalar, false otherwise.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator>(S scalar) const;
/**
* @brief overload the < operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return True if the nion is less than the scalar, false otherwise.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator<(S scalar) const;
/**
* @brief overload the >= operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return True if the nion is greater than or equal to the scalar, false otherwise.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator>=(S scalar) const;
/**
* @brief overload the <= operator for nions with scalars.
* @tparam S type of the scalar.
* @param scalar The scalar to compare this nion to.
* @return True if the nion is less than or equal to the scalar, false otherwise.
*/
template<not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
constexpr inline bool operator<=(S scalar) const;
/**
* @brief comparison if nion is a real number.
* @return True if the nion is a real number, false otherwise.
* @details A nion is a real number if all components except the first are equal to zero.
*/
constexpr inline bool is_real() const;
/**
* @brief Converts a nion to a string.
* @return The string representation of the nion.
*/
inline std::string to_string() const;
};
// struct nion
/***************************
* NION OPERATOR OVERLOADS
***************************/
/**
* @brief overload the * operator for lhs scalars and rhs nions.
* @tparam T type of the nion.
* @tparam S type of the scalar.
* @param scalar type of the scalar.
* @param z The nion to multiply the scalar by.
* @return
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline nion<T,N> operator*(S scalar, const nion<T,N> &z);
/**
* @brief overload the / operator for lhs scalars and rhs nions.
* @tparam T type of the nion.
* @tparam S type of the scalar.
* @param scalar type of the scalar.
* @param z The nion to divide the scalar by.
* @return
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline nion<T,N> operator/(S scalar, const nion<T,N> &z);
/**
* @brief overload the + operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to add the scalar by.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline nion<T,N> operator+(S scalar, const nion<T,N> &z);
/**
* @brief overload the - operator for lhs scalars and rhs nions.
* @tparam S The type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to subtract the scalar by.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline nion<T,N> operator-(S scalar, const nion<T,N> &z);
/**
* @brief overload the == operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return True if the nion is equal to the scalar, false otherwise.
* @details A nion is equal to a scalar if the scalar is equal to the first component of the nion
* and all other components are equal to zero.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator==(S scalar, const nion<T,N> &z);
/**
* @brief overload the != operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return False if the nion is equal to the scalar, true otherwise.
* @details A nion is equal to a scalar if the scalar is equal to the first component of the nion
* and all other components are equal to zero.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator!=(S scalar, const nion<T,N> &z);
/**
* @brief overload the > operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return True if the scalar is greater than the nion, false otherwise.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator>(S scalar, const nion<T,N> &z);
/**
* @brief overload the < operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return True if the scalar is less than the nion, false otherwise.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator<(S scalar, const nion<T,N> &z);
/**
* @brief overload the >= operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return True if the scalar is greater than or equal to the nion, false otherwise.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator>=(S scalar, const nion<T,N> &z);
/**
* @brief overload the <= operator for lhs scalars and rhs nions.
* @tparam S type of the scalar.
* @tparam T type of the nion.
* @param scalar type of the scalar.
* @param z The nion to compare the scalar to.
* @return True if the scalar is less than or equal to the nion, false otherwise.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T> requires (std::is_convertible_v<S,T>)
extern constexpr inline bool operator<=(S scalar, const nion<T,N> &z);
/**
* @brief overload the << operator for nions.
* @param os The output stream.
* @param z The nion to print.
* @return The output stream.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
extern std::ostream &operator<<(std::ostream &os, const nion<T,N> &z);
/**
* @brief overload the >> operator for nions.
* @param is The input stream.
* @param z The nion to read into.
* @return The input stream.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
extern std::istream &operator>>(std::istream &is, nion<T,N> &z);
/***************************
* NION FUNCTION IMPLEMENTATIONS *
***************************/
/**
* @brief Calculate the real part of a nion.
* @tparam T type of the nion.
* @param z The nion to calculate the real part of.
* @return The real part of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline T real(const nion<T,N> &z);
/**
* @brief Calculate the imaginary part of a nion.
* @tparam T type of the nion.
* @param z The nion to calculate the imaginary part of.
* @return The imaginary part of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> imag(const nion<T,N> &z);
/**
* @brief compute the conjugate of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the conjugate of.
* @return The conjugate of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> conj(const nion<T,N> &z);
/**
* @brief compute the absolute value of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the absolute value of.
* @return The absolute value of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline T abs(const nion<T,N> &z);
/**
* @brief compute the norm of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the norm of.
* @return The norm of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline T norm(const nion<T,N> &z);
/**
* @brief compute the inverse of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the inverse of.
* @return The inverse of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> inv(const nion<T,N> &z);
/**
* @brief compute the dot product of two nions.
* @tparam T type of the nions.
* @param lhs The left hand side nion.
* @param rhs The right hand side nion.
* @return The dot product of the nions.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, std::size_t M = N>
constexpr inline T dot(const nion<T,N> &lhs, const nion<T,M> &rhs);
/***************************
* NION ALGEBRAIC FUNCTIONS *
***************************/
/**
* @brief compute exponential of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the exponential of.
* @return The exponential of the nion.
* @details The exponential of a nion is defined as e^z = e^r * (cos|v| + v/|v| * sin|v|).
* where a is the real component and v is the imaginary components.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> exp(const nion<T,N> &z);
/**
* @brief compute the principle logarithm of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the natural logarithm of.
* @return The natural logarithm of the nion.
* @details The natural logarithm of a nion is defined as ln(z) = ln(|z|) + v/|v| * atan(|v|/r).
* where a is the real component and v is the imaginary components.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> log(const nion<T,N> &z);
/**
* @brief compute the power of a nion with scalar.
* @tparam T type of the nion.
* @tparam S type of the power.
* @param base The nion to compute the power of.
* @param power The power to raise the nion to.
* @return The power of the nion.
* @details The power of a nion is defined as z^p = e^(p * ln(z)).
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S> requires (std::is_convertible_v<S,T>)
constexpr inline nion<T,N> pow(const nion<T,N> &base, S power);
/**
* @brief compute the power of a nion with nion.
* @tparam T type of the nion.
* @param base The nion to compute the power of.
* @param power The power to raise the nion to.
* @return The power of the nion.
* @details The power of a nion is defined as z^p = e^(p * ln(z)).
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, std::size_t M = N>
constexpr inline nion<T,N> pow(const nion<T,N> &base, const nion<T,M> &power);
/**
* @brief compute the power of a scalar with a nion.
* @tparam T type of the nion.
* @tparam S type of the power.
* @param power The nion to use as a power.
* @param base The scalar to raise by the nion
* @return The power of the nion.
* @details The power of with a nion is defined as x^z = e^(z * ln(x)).
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP, not_nion<T,N> S = T>
constexpr inline nion<T,N> pow(S base, const nion<T,N> &power);
/**
* @brief compute the square of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the square of.
* @return The square of the nion.
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> sqr(const nion<T,N> &base);
/**
* @brief compute the square root of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the square root of.
* @return The square root of the nion.
* @details The square root of a nion is defined as sqrt(z) = z^(1/2).
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> sqrt(const nion<T,N> &z);
/**
* @brief compute the cube root of a nion.
* @tparam T type of the nion.
* @param z The nion to compute the cube root of.
* @return The cube root of the nion.
* @details The cube root of a nion is defined as cbrt(z) = z^(1/3).
*/
template<arith_ops T, std::size_t N = NION_USE_HEAP>
constexpr inline nion<T,N> cbrt(const nion<T,N> &z);
#include "nion.cpp"
using nionF = nion<float, NION_USE_HEAP>; // single precision
using nionD = nion<double, NION_USE_HEAP>; // double precision
#endif //NION_HPP