-
Notifications
You must be signed in to change notification settings - Fork 0
/
multivectors.py
1238 lines (1012 loc) · 36.7 KB
/
multivectors.py
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Compute multivectors in arbitrary-dimensional space.
Installation
------------
pip install multivectors
Usage
-----
>>> import math
>>> from multivectors import x, y, z
>>> v = 2*x + 3*y + 4*z
>>> print(v.rotate(math.pi/2, x * y))
(-3.00x + 2.00y + 4.00z)
For more see `the docs <https://multivectors.rtfd.io>`_
"""
from __future__ import annotations
import math
from numbers import Real
from typing import Dict, Iterable, List, Optional, Tuple, Union
__all__ = [
'MultiVector',
'_',
'x',
'y',
'z',
'w'
]
__version__ = '1.2.1'
NAMES = 'xyzw'
def merge(arr: List[int], left: List[int], right: List[int]) -> int:
"""Perform a merge of sorted lists and count the swaps.
Args:
arr: The list to write into.
left: The left sorted list.
right: The right sorted list.
Returns:
The number of swaps made when merging.
"""
i = j = count = 0
ll = len(left)
lr = len(right)
while i < ll or j < lr:
if i == ll:
arr[i+j] = right[j]
j += 1
elif j == lr:
arr[i+j] = left[i]
i += 1
elif left[i] <= right[j]:
arr[i+j] = left[i]
i += 1
else:
arr[i+j] = right[j]
count += ll - i
j += 1
return count
def count_swaps(arr: List[int], copy: bool = True) -> int:
"""Count the number of swaps needed to sort a list.
Args:
arr: The list to sort.
copy: If :obj:`True`, (the default) don't modify the original list.
Returns:
The number of swaps made when sorting the list.
Examples:
>>> count_swaps([1, 3, 2, 5, 4])
2
>>> count_swaps([3, 2, 1])
3
"""
if copy:
arr = list(arr)
if len(arr) < 2:
return 0
mid = (len(arr) + 1) // 2
left = arr[:mid]
right = arr[mid:]
return (count_swaps(left, False)
+ count_swaps(right, False)
+ merge(arr, left, right))
def names_to_idxs(name: str,
raise_on_invalid_chars: bool = False) -> List[int]:
"""Convert swizzled basis vector names into generalized basis indices.
Args:
name: The names to convert.
raise_on_invalid_chars: If :obj:`True` (default :obj:`False`),
raise :exc:`AttributeError` if any characters appear in the
name that are invalid in a basis name.
Returns:
A list of basis indexes that the name represents.
Raises:
AttributeError: If characters invalid for a basis name appear,
and ``raise_on_invalid_chars`` is :obj:`True`.
Examples:
>>> names_to_idxs('xyw')
[0, 1, 3]
>>> names_to_idxs('e_1e2_z')
[0, 1, 2]
>>> names_to_idxs('_')
[]
"""
if name.startswith('__'):
# fail on magic attributes immediately
raise AttributeError
idxs: List[int] = []
index = 0
name_len = len(name)
while index < name_len:
char = name[index]
if char in NAMES:
idxs.append(NAMES.index(char))
index += 1
elif char == 'e':
index += 1
end = index
subname = []
while (
end < name_len
and name[end] not in (NAMES + 'e')
and not subname
):
if '0' <= name[end] <= '9':
subname.append(name[end])
elif raise_on_invalid_chars:
raise AttributeError
end += 1
try:
idxs.append(int(''.join(subname)) - 1)
except ValueError:
raise AttributeError(f'empty eN notation: {name!r}') from None
index = end
elif char == '_':
index += 1
elif raise_on_invalid_chars:
raise AttributeError
else:
index += 1
return idxs
def idxs_to_idxs(idxs: Index) -> List[int]:
"""Convert multiple possible ways to specify multiple indices.
This is intended to be given the argument to :class:`MultiVector` indexing:
``V[0]`` would call ``idxs_to_idxs(0)``, ``V[0, 1]`` would call
``idxs_to_idxs((0, 1))``, and ``V[0:1]`` would call
``idxs_to_idxs(slice(0, 1, None))``
Args:
idxs: The indexes to convert. This can be an integer, for just that
index; an iterable of integers, for those indexes directly; or a
slice, for the indexes the slice represents.
Returns:
A list of basis indexes, converted from the argument.
Examples:
>>> idxs_to_idxs(slice(None, 5, None))
[0, 1, 2, 3, 4]
>>> idxs_to_idxs((1, 3, 4))
[1, 3, 4]
>>> idxs_to_idxs(1)
[1]
"""
if isinstance(idxs, int):
return [idxs]
if isinstance(idxs, slice):
if idxs.stop is None:
raise TypeError('cannot have infinite bases')
return list(range(*idxs.indices(idxs.stop)))
return list(idxs)
def idxs_to_names(idxs: Index, sep: str = '') -> str:
"""Convert indices to a swizzled name combination.
Args:
idxs: The basis index(es), as accepted by :func:`idxs_to_idxs`.
sep: The separator to use between the basis vector names.
Returns:
The swizzled names.
Examples:
>>> idxs_to_names(slice(None, 5, None))
'e1e2e3e4e5'
>>> idxs_to_names((0, 1, 3))
'xyw'
>>> idxs_to_names(2)
'z'
>>> idxs_to_names((0, 2, 3), sep='*')
'x*z*w'
"""
idxs = idxs_to_idxs(idxs)
if max(idxs) > 3:
return sep.join(f'e{idx + 1}' for idx in idxs)
return sep.join(NAMES[idx] for idx in idxs)
def condense_bases(bases: Tuple[int, ...], scalar: float = 1.0) \
-> Tuple[Tuple[int, ...], float]:
"""Normalize a sequence of bases, modifying the scalar as necessary.
Args:
bases: The tuple of basis indices.
scalar: Real number that will scale the resulting bases.
Returns:
A 2-tuple of normalized bases and the modified scalar.
Examples:
>>> condense_bases((1, 1, 2, 1, 2), 2.0)
((1,), -2.0)
>>> condense_bases((1, 2, 1, 2), 1.5)
((), -1.5)
>>> condense_bases((2, 1, 3, 2, 3, 3), 1.0)
((1, 3), 1.0)
"""
_bases = list(bases)
if count_swaps(_bases) % 2 != 0:
scalar *= -1.0
_bases.sort()
_bases = [basis for basis in set(_bases)
if _bases.count(basis) % 2 != 0]
return tuple(_bases), scalar
class MultiVector:
"""A linear combination of geometric products of basis vectors.
The bare constructor is not meant for regular use.
Use module swizzling or the factory
:meth:`~MultiVector.from_terms()` instead.
Basis vector names can be swizzled on instances:
>>> from multivectors import x, y, z
>>> (x + y).x
1.0
>>> (x*y + z).xy
1.0
>>> (x + y).e3
0.0
And indices can be combined:
>>> (x + y) % 0
1.0
>>> (x*y + z) % (0, 1)
1.0
>>> (x + y) % 2
0.0
"""
__slots__ = ('termdict',)
termdict: TermDict
@property
def grade(self) -> Union[int, None]:
"""The grade of this blade.
Returns:
The number of different bases this blade consists of,
or :obj:`None` if this multivector is not a blade (one term).
Examples:
>>> from multivectors import x, y, z
>>> (x + y).grade # not a blade
>>> (z * 2 + z).grade
1
>>> (x*y*z).grade
3
"""
if len(self.termdict) != 1:
return None
(bases,) = self.termdict.keys()
return len(bases)
@property
def terms(self) -> Tuple[MultiVector, ...]:
"""Get a sequence of blades comprising this multivector.
Examples:
>>> from multivectors import x, y, z, w
>>> (x + y).terms
((1.0 * x), (1.0 * y))
>>> ((x + y) * (z + w)).terms
((1.0 * x*z), (1.0 * x*w), (1.0 * y*z), (1.0 * y*w))
"""
items = sorted(self.termdict.items(),
key=lambda item: (len(item[0]), item[0]))
return tuple(MultiVector({key: value}) for key, value in items)
def __init__(self, termdict: TermDict):
""":meta private:"""
self.termdict = {bases: float(scalar)
for bases, scalar in termdict.items()
if scalar != 0.0} or {(): 0.0}
@classmethod
def from_terms(cls, *terms: SOV) -> MultiVector:
"""Create a multivector by summing a sequence of terms.
Args:
*terms: The terms. If you have an iterable of terms, use
(e.g.) ``from_terms(*terms)``
Returns:
A multivector.
Examples:
>>> from multivectors import x, y, z
>>> MultiVector.from_terms(x, y)
(1.0 * x + 1.0 * y)
>>> MultiVector.from_terms()
(0.0)
>>> MultiVector.from_terms(z)
(1.0 * z)
>>> MultiVector.from_terms(2 * x, x)
(3.0 * x)
"""
termseq = [
t for term in terms for t in
(term if isinstance(term, MultiVector)
else MultiVector({(): term})).termdict.items()
]
termsdict: Dict[Tuple[int, ...], List[float]] = {}
for bases, scalar in termseq:
termsdict.setdefault(bases, []).append(scalar)
termdict = {bases: math.fsum(scalars)
for bases, scalars in termsdict.items()}
# discard zeros
termdict = {bases: scalar for bases, scalar in termdict.items()
if scalar != 0.0}
return cls(termdict)
@classmethod
def scalar(cls, num) -> MultiVector:
"""Create a MultiVector representing a scalar.
Args:
num: Any object that can be :func:`float()`-d.
Returns:
A multivector with only a scalar part of ``num``.
Examples:
>>> from multivectors import MultiVector
>>> MultiVector.scalar('0')
(0.0)
>>> MultiVector.scalar('1.2')
(1.2)
"""
return cls({(): float(num)})
def __getattr__(self, name: str) -> float:
"""Support basis name swizzling."""
return self.termdict.get(tuple(names_to_idxs(name)), 0.0)
def __getitem__(self, grades: Index) -> MultiVector:
"""The choose operator - returns the sum of all blades of grade k.
Args:
grades: The grade(s) to choose.
Examples:
>>> from multivectors import x, y, z
>>> (1 + 2*x + 3*y + 4*x*y)[1]
(2.0 * x + 3.0 * y)
>>> (1 + 2 + 3*x + 4*x*y + 5*y*z)[2]
(4.0 * x*y + 5.0 * y*z)
>>> (1 + 2 + 3*x + 4*x*y + 5*y*z)[0]
(3.0)
>>> (1 + 2 + 3*x + 4*x*y + 5*y*z).choose(0)
(3.0)
>>> (1 + 2*x + 3*x*y)[:2]
(1.0 + 2.0 * x)
>>> (1 + 2*x + 3*x*y)[1:]
(2.0 * x + 3.0 * x*y)
"""
if isinstance(grades, slice):
stop = (max(map(len, self.termdict.keys())) + 1) \
if grades.stop is None else grades.stop
grades = slice(grades.start, stop, grades.step)
grades = set(idxs_to_idxs(grades))
return MultiVector({
bases: scalar for bases, scalar in self.termdict.items()
if len(bases) in grades})
choose = __getitem__
def __repr__(self) -> str:
"""Return a representation of this multivector.
Depending on the global namespace, this may be :func:`eval()`-able.
Examples:
>>> from multivectors import x, y, z, w
>>> repr(x)
'(1.0 * x)'
>>> repr(x + y)
'(1.0 * x + 1.0 * y)'
>>> repr(y*z - x*w)
'(-1.0 * x*w + 1.0 * y*z)'
"""
if self.grade is not None:
((bases, scalar),) = self.termdict.items()
if bases == ():
return f'({scalar!r})'
names = idxs_to_names(bases, '*')
return f'({scalar!r} * {names!s})'
return '(' + ' + '.join(repr(term).strip('()')
for term in self.terms) + ')'
def __str__(self) -> str:
"""Return a representation of this multivector suited for showing.
Examples:
>>> from multivectors import x, y, z, w
>>> str(x)
'1.00x'
>>> str(x + y)
'(1.00x + 1.00y)'
>>> str(y*z - x*w)
'(-1.00xw + 1.00yz)'
>>> print(1 + x + x*y)
(1.00 + 1.00x + 1.00xy)
"""
return f'{self:.2f}'
def __format__(self, spec: str) -> str:
"""Return a representation of this multivector suited for formatting.
Args:
spec: The format spec (forwarded to the underlying :class:`float`s)
Examples:
>>> from multivectors import x, y, z, w
>>> f'{x:.3f}'
'1.000x'
>>> V = x + 2 * y + z
>>> '{:.3f}'.format(V)
'(1.000x + 2.000y + 1.000z)'
>>> V = y*z - x*w
>>> f'{V:.1f}'
'(-1.0xw + 1.0yz)'
"""
if self.grade is not None:
((bases, scalar),) = self.termdict.items()
if bases == ():
return format(scalar, spec)
names = idxs_to_names(bases)
return format(scalar, spec) + str(names)
return '(' + ' + '.join(format(term, spec) for term in self.terms) + ')'
# Relational operators
def __eq__(self, other: SOV) -> bool:
"""Compare equality of two objects.
Returns:
:obj:`True` if all terms of this multivector are equal to the
``other``; :obj:`True` if this multivector is scalar and equals
the ``other``; or :obj:`False` for all other cases or types.
Examples:
>>> from multivectors import x, y
>>> x + y == y + x
True
>>> x + 2*y == 2*x + y
False
"""
if not isinstance(other, MultiVector):
if self.grade != 0:
return False # this MV is not a scalar
return self._ == other
return self.termdict == other.termdict
def __ne__(self, other: SOV) -> bool:
"""Compare inequality of two objects.
Returns:
:obj:`False` if all terms of this multivector are equal to the
``other``; :obj:`False` if this multivector is scalar and equals
the ``other``; or :obj:`True` for all other cases or types.
Examples:
>>> from multivectors import x, y
>>> x + y != y + x
False
>>> x + 2*y != 2*x + y
True
"""
return not (self == other)
def __lt__(self, other: float) -> bool:
"""Compare this blade less than an object.
Returns:
:obj:`True` if this is a scalar blade less than the scalar;
:obj:`False` if this is a scalar blade not less than the scalar;
or :obj:`NotImplemented` for all other types.
Examples:
>>> from multivectors import _, x
>>> _ * 1 < 2
True
>>> _ * 2 < 1
False
>>> x * 1 < 2
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of \
'MultiVector' and 'int'
"""
if not isinstance(other, Real) or self.grade != 0:
return NotImplemented
return self._ < other
def __gt__(self, other: float) -> bool:
"""Compare this blade greater than an object.
Returns:
:obj:`True` if this is a scalar blade greater than the scalar;
:obj:`False` if this is a scalar blade not greater than the scalar;
or :obj:`NotImplemented` for all other types.
Examples:
>>> from multivectors import _, x
>>> _ * 1 > 2
False
>>> _ * 2 > 1
True
>>> x * 1 > 2
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of \
'MultiVector' and 'int'
"""
if not isinstance(other, Real) or self.grade != 0:
return NotImplemented
return self._ > other
def __le__(self, other: float) -> bool:
"""Compare this blade less than or equal to an object.
Returns:
:obj:`True` if this is a scalar blade less than or equal to the
scalar; :obj:`False` if this is a scalar blade greater than the
scalar; or :obj:`NotImplemented` for all other types.
Examples:
>>> from multivectors import _, x
>>> _ * 1 <= 2
True
>>> _ * 2 <= 2
True
>>> _ * 2 <= 1
False
>>> x * 1 <= 2
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of \
'MultiVector' and 'int'
"""
if not isinstance(other, Real) or self.grade != 0:
return NotImplemented
return self._ <= other
def __ge__(self, other: float) -> bool:
"""Compare this blade greater than or equal to an object.
Returns:
:obj:`True` if this is a scalar blade greater than or equal to the
scalar; :obj:`False` if this is a scalar blade less than the
scalar; or :obj:`NotImplemented` for all other types.
Examples:
>>> from multivectors import _, x
>>> _ * 1 >= 2
False
>>> _ * 2 >= 2
True
>>> _ * 2 >= 1
True
>>> x * 1 >= 2
Traceback (most recent call last):
...
TypeError: '>=' not supported between instances of \
'MultiVector' and 'int'
"""
if not isinstance(other, Real) or self.grade != 0:
return NotImplemented
return self._ >= other
# Binary operators
def __add__(self, other: SOV) -> MultiVector:
"""Add a multivector and another object.
Examples:
>>> from multivectors import x, y, z, w
>>> (x + z) + (y + w)
(1.0 * x + 1.0 * y + 1.0 * z + 1.0 * w)
>>> (x + z) + y
(1.0 * x + 1.0 * y + 1.0 * z)
>>> (x + y) + 1
(1.0 + 1.0 * x + 1.0 * y)
"""
if isinstance(other, MultiVector):
return self.from_terms(*self.terms, *other.terms)
if isinstance(other, Real):
return self.from_terms(*self.terms, other)
return NotImplemented
def __radd__(self, other: float) -> MultiVector:
"""Support adding multivectors on the right side of objects.
Examples:
>>> from multivectors import x, y, z
>>> 1 + (x + z)
(1.0 + 1.0 * x + 1.0 * z)
>>> x + (y + z)
(1.0 * x + 1.0 * y + 1.0 * z)
"""
if isinstance(other, MultiVector):
return self.from_terms(*other.terms, *self.terms)
if isinstance(other, Real):
return self.from_terms(other, *self.terms)
return NotImplemented
def __sub__(self, other: SOV) -> MultiVector:
"""Subtracting is adding the negation.
Examples:
>>> from multivectors import x, y, z, w
>>> (x + z) - (y + w)
(1.0 * x + -1.0 * y + 1.0 * z + -1.0 * w)
>>> (x + z) - y
(1.0 * x + -1.0 * y + 1.0 * z)
"""
return self + (-other)
def __rsub__(self, other: float) -> MultiVector:
"""Support subtracting multivectors from objects.
Examples:
>>> from multivectors import x, y, z, w
>>> 1 - (x + z)
(1.0 + -1.0 * x + -1.0 * z)
>>> x - (y + z)
(1.0 * x + -1.0 * y + -1.0 * z)
"""
return other + (-self)
def __mul__(self, other: SOV) -> MultiVector:
"""Multiply a multivector and another object.
Returns:
``(a + b) * (c + d) = a*c + a*d + b*c + b*d``
for multivectors ``(a + b)`` and ``(c + d)``.
Returns:
``(a + b) * v = a*v + b*v``
for multivector ``(a + b)`` and scalar ``v``.
Examples:
>>> from multivectors import x, y, z, w
>>> (x + y) * (z + w)
(1.0 * x*z + 1.0 * x*w + 1.0 * y*z + 1.0 * y*w)
>>> (x + y) * 3
(3.0 * x + 3.0 * y)
>>> (x + y) * x
(1.0 + -1.0 * x*y)
"""
if isinstance(other, MultiVector):
if self.grade is not None and other.grade is not None:
((bases1, scalar1),) = self.termdict.items()
((bases2, scalar2),) = other.termdict.items()
(bases, scalar) = condense_bases(
(*bases1, *bases2), scalar1 * scalar2)
return MultiVector({bases: scalar})
return self.from_terms(*(a * b for b in other.terms
for a in self.terms))
if isinstance(other, Real):
return self.from_terms(*(term * self.from_terms(other)
for term in self.terms))
return NotImplemented
def __rmul__(self, other: float) -> MultiVector:
"""Support multiplying multivectors on the right side of scalars.
Examples:
>>> from multivectors import x, y
>>> 3 * (x + y)
(3.0 * x + 3.0 * y)
>>> y * (x + y)
(1.0 + -1.0 * x*y)
"""
if isinstance(other, Real):
return self.from_terms(*(self.from_terms(other) * term
for term in self.terms))
return NotImplemented
def __matmul__(self, other: SOV) -> MultiVector:
"""Get the inner (dot) product of two objects.
Returns:
``u @ v = (u * v)[abs(u.grade - v.grade)]``
when :attr:`~MultiVector.grade` is defined.
Returns:
``(a + b) @ (c + d) = a@c + a@d + b@c + b@d``
for multivectors ``(a + b)`` and ``(c + d)``
Returns:
``(a + b) @ v = a@v + b@v``
for multivector ``(a + b)`` and scalar ``v``
Examples:
>>> from multivectors import x, y, z
>>> (2*x + 3*y) @ (4*x + 5*y)
(23.0)
>>> (2*x*y).dot(3*y*z)
(0.0)
>>> (x + y).inner(3)
(3.0 * x + 3.0 * y)
>>> (x + y) @ x
(1.0)
"""
if isinstance(other, MultiVector):
if self.grade is not None and other.grade is not None:
return (self * other)[abs(self.grade - other.grade)]
return self.from_terms(*(
a @ b for b in other.terms for a in self.terms))
if isinstance(other, Real):
return self.from_terms(*(term @ self.from_terms(other)
for term in self.terms))
return NotImplemented
dot = inner = __matmul__
def __rmatmul__(self, other: float) -> MultiVector:
"""Support dotting multivectors on the right hand side.
Returns:
``v @ (a + b) = v@a + v@b``
for multivector ``(a + b)`` and scalar ``v``
Examples:
>>> from multivectors import x, y
>>> 3 @ (x + y)
(3.0 * x + 3.0 * y)
>>> x @ (x + y)
(1.0)
"""
if isinstance(other, Real):
return self.from_terms(*(self.from_terms(other) @ term
for term in self.terms))
return NotImplemented
def __truediv__(self, other: SOV) -> MultiVector:
"""Divide two objects.
Returns:
``(a + b) / v = a/v + b/v``
Examples:
>>> from multivectors import x, y
>>> (6*x + 9*y) / 3
(2.0 * x + 3.0 * y)
>>> (6*x + 9*y) / (3*x)
(2.0 + -3.0 * x*y)
"""
if not isinstance(other, Real):
if not isinstance(other, MultiVector):
return NotImplemented
if other.grade is None:
return NotImplemented
other = 1 / other
return MultiVector.from_terms(*(term * other for term in self.terms))
def __rtruediv__(self, other: float) -> MultiVector:
"""Divide a scalar by a multivector. Only defined for blades.
Examples:
>>> from multivectors import x, y
>>> 1 / x
(1.0 * x)
>>> 2 / (4 * x*y)
(-0.5 * x*y)
"""
if self.grade is None:
raise TypeError('cannot take inverse of multivector '
'with more than one term')
return other * self / (self * self)._
def __mod__(self, idxs: Index) -> float:
"""Support index swizzling.
Examples:
>>> from multivectors import x, y
>>> (x + y) % 0
1.0
>>> v = 1 + 2*y + 3*x*y
>>> v % ()
1.0
>>> v % 1
2.0
>>> v % (0, 1)
3.0
>>> v % 2
0.0
"""
return self.termdict.get(tuple(idxs_to_idxs(idxs)), 0.0)
def __pow__(self, other: int) -> MultiVector:
"""A multivector raised to an integer power.
``V ** n = V * V * V * ... * V``, ``n`` times.
``V ** -n = 1 / (V ** n)``
Examples:
>>> from multivectors import x, y
>>> (x + y) ** 3
(2.0 * x + 2.0 * y)
>>> (2 * x*y) ** -5
(-0.03125 * x*y)
"""
if not isinstance(other, int):
return NotImplemented
result = self.scalar(1)
for _ in range(abs(other)):
result *= self
if other < 0:
return 1.0 / result
return result
def __rpow__(self, other: float) -> MultiVector:
"""A real number raised to a multivector power.
``x ** V = e ** ln(x ** V) = e ** (V ln x)``
Examples:
>>> from multivectors import x, y
>>> round(2 ** (x + y), 2)
(1.52 + 0.81 * x + 0.81 * y)
"""
if not isinstance(other, Real):
return NotImplemented
return (self * math.log(other)).exp()
def exp(self) -> MultiVector:
r"""e raised to this multivector power.
.. math::
e^V = \exp(V) = \sum_{n=0}^{\infty} \frac{V^n}{n!}
Examples:
>>> from math import pi, sqrt
>>> from multivectors import x, y
>>> # 45-degree rotation through xy-plane
>>> # results in (1+xy)/sqrt(2)
>>> (pi/4 * x*y).exp() * sqrt(2)
(1.0 + 1.0 * x*y)
>>> round((pi * x*y).exp(), 14)
(-1.0)
"""
# since we have to sequentially sum terms anyway, we might
# as well repeatedly multiply for the integer exponent
# and sequentially multiply for the factorial
i = current_factorial = 1
result = self.scalar(1)
current_exponent = self.scalar(1)
last_result = self.scalar(0)
while last_result != result:
last_result = result
current_exponent *= self
current_factorial *= i
i += 1
result += current_exponent / current_factorial
return last_result
def __xor__(self, other: SOV) -> MultiVector:
"""Get the outer (wedge) product of two objects.
.. warning::
Operator precedence puts ``^`` after ``+``!
Make sure to put outer products in parentheses, like this:
``u * v == u @ v + (u ^ v)``
Returns:
``u ^ v = (u * v)[u.grade + v.grade]``
when :attr:`~MultiVector.grade` is defined
Returns:
``(a + b) ^ (c + d) = (a^c) + (a^d) + (b^c) + (b^d)``
for multivector ``(a + b)`` and ``(c + d)``
Returns:
``(a + b) ^ v = (a^v) + (b^v)``
for multivector ``(a + b)`` and scalar ``v``
Examples:
>>> from multivectors import x, y, z
>>> (2*x + 3*y) ^ (4*x + 5*y)
(-2.0 * x*y)
>>> (2*x*y).wedge(3*y*z)
(0.0)
>>> (x + y).outer(3)
(3.0 * x + 3.0 * y)
>>> (x + y) ^ x
(-1.0 * x*y)
"""
if isinstance(other, MultiVector):
if self.grade is not None and other.grade is not None:
return (self * other)[self.grade + other.grade]
return self.from_terms(*(
a ^ b for b in other.terms for a in self.terms))
if isinstance(other, Real):
return self.from_terms(*(term ^ self.from_terms(other)
for term in self.terms))
return NotImplemented
wedge = outer = __xor__
def __rxor__(self, other: float) -> MultiVector:
"""Support wedging multivectors on the right hand side.
Returns:
``v ^ (a + b) = (v^a) + (v^b)``
for multivector ``(a + b)`` and simple ``v``
Examples:
>>> from multivectors import x, y
>>> 3 ^ (x + y)
(3.0 * x + 3.0 * y)
>>> x ^ (x + y)
(1.0 * x*y)
"""
if isinstance(other, Real):
return self.from_terms(*(self.from_terms(other) ^ term
for term in self.terms))
return NotImplemented
# Unary operators
def __neg__(self) -> MultiVector:
"""The negation of a multivector is the negation of all its terms.