-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector_expr.py
1274 lines (1069 loc) · 41.9 KB
/
vector_expr.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
# import sympy as sp
from sympy import (
sympify, S, Basic, Expr, Derivative, Abs, Mul, Add, Pow,
Symbol, Number, log, Wild, fraction, postorder_traversal
)
from sympy.core.compatibility import (
default_sort_key, with_metaclass, reduce
)
from sympy.core.decorators import call_highest_priority, _sympifyit
from sympy.core.evalf import EvalfMixin
from sympy.core.function import UndefinedFunction
from sympy.core.operations import AssocOp
from sympy.core.singleton import Singleton
from sympy.strategies import flatten, typed
from sympy.utilities.iterables import ordered
from sympy.vector import (
Vector,
divergence, curl, gradient
)
# TODO:
class VectorExpr(Expr):
""" Superclass for vector expressions.
Example:
a = VectorSymbol("a")
b = VectorSymbol("a")
c = VectorSymbol("a")
expr = (a ^ (b ^ c)) + (a & b) * c + a.mag * b
"""
is_Vector = False
is_VectorExpr = True
is_number = False
is_symbol = False
is_scalar = False
is_commutative = True
_op_priority = 11
@_sympifyit('other', NotImplemented)
@call_highest_priority('__radd__')
def __add__(self, other):
return VecAdd(self, other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__add__')
def __radd__(self, other):
return VecAdd(other, self)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__rdiv__')
def __div__(self, other):
return self * other**S.NegativeOne
@_sympifyit('other', NotImplemented)
@call_highest_priority('__div__')
def __rdiv__(self, other):
raise NotImplementedError()
__truediv__ = __div__
__rtruediv__ = __rdiv__
def __neg__(self):
return VecMul(S.NegativeOne, self)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__rsub__')
def __sub__(self, other):
return VecAdd(self, -other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__sub__')
def __rsub__(self, other):
return VecAdd(other, -self)
@_sympifyit('other', NotImplemented)
def __and__(self, other):
return VecDot(self, other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__rmul__')
def __mul__(self, other):
return VecMul(self, other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__mul__')
def __rmul__(self, other):
return VecMul(self, other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__rpow__')
def __pow__(self, other):
return VecPow(self, other)
@_sympifyit('other', NotImplemented)
@call_highest_priority('__pow__')
def __rpow__(self, other):
return VecPow(other, self)
@_sympifyit('other', NotImplemented)
def __xor__(self, other):
return VecCross(self, other)
def dot(self, other):
return VecDot(self, other)
def cross(self, other):
return VecCross(self, other)
def diff(self, *symbols, **assumptions):
assumptions.setdefault("evaluate", True)
return D(Derivative(self, *symbols, **assumptions))
def normalize(self, **kwargs):
# TODO: what if I do a check for evaluate=True in Normalize
# constructor?
evaluate = kwargs.get('evaluate', False)
n = Normalize(self)
if evaluate:
n.doit(**kwargs)
return n
@property
def norm(self):
return self.normalize()
def magnitude(self):
return Magnitude(self)
@property
def mag(self):
return self.magnitude()
def gradient(self):
return Grad(self)
@property
def grad(self):
return self.gradient()
def divergence(self):
return VecDot(Nabla(), self)
@property
def div(self):
return self.divergence()
def curl(self):
return VecCross(Nabla(), self)
@property
def cu(self):
return self.curl()
def laplace(self):
return Laplace(self)
@property
def lap(self):
return self.laplace()
def expand(self, deep=True, modulus=None, power_base=True, power_exp=True,
mul=True, log=True, multinomial=True, basic=True, **hints):
if isinstance(self, VectorSymbol):
return self
_spaces.append(_token)
debug("VectorExpr expand", self)
# first expand dot and cross products
A, B = [WVS(t) for t in ["A", "B"]]
def func(expr, pattern, prev=None):
# debug("\n", hints)
old = expr
found = list(ordered(list(expr.find(pattern))))
# print("func", expr)
# debug("found", found)
# print("found", found)
for f in found:
fexp = f.expand(**hints)
# debug("\t fexp", fexp)
if f != fexp:
expr = expr.xreplace({f: fexp})
# debug("expr", expr)
if old != expr:
expr = func(expr, pattern)
return expr
expr = func(self, A ^ B)
# print("expr", expr)
expr = func(expr, A & B)
# print("expr", expr)
expr = func(expr, Grad)
expr = func(expr, Laplace)
# print("expr", expr)
del _spaces[-1]
return Expr.expand(expr, deep=deep, modulus=modulus, power_base=power_base,
power_exp=power_exp, mul=mul, log=log, multinomial=multinomial,
basic=basic, **hints)
def get_postprocessor(cls):
def _postprocessor(expr):
vec_class = {Mul: VecMul, Add: VecAdd, Pow: VecPow}[cls]
nonvectors = []
vectors = []
print("POST-PROCESSOR", cls)
for term in expr.args:
print("\t", term.func, term)
# if isinstance(term, (VectorExpr, Vector)):
if term.has(VectorExpr) or term.has(Vector):
vectors.append(term)
else:
nonvectors.append(term)
if not vectors:
print("YEAH")
return cls._from_args(nonvectors)
print("\tvectors", vectors)
print("\tnonvectors", nonvectors)
if vec_class == VecAdd or vec_class == VecMul:
return vec_class(*nonvectors, *vectors, evaluate=True)
else:
return vec_class(*expr.args)
return _postprocessor
Basic._constructor_postprocessor_mapping[VectorExpr] = {
"Mul": [get_postprocessor(Mul)],
"Add": [get_postprocessor(Add)],
"Pow": [get_postprocessor(Pow)],
}
def wtf():
from sympy import collect_const
a = VectorSymbol("a")
b = VectorSymbol("b")
expr = 3 * a + 3 * b
print(collect_const(expr))
class VectorSymbol(VectorExpr):
""" Symbolic representation of a Vector object.
"""
is_symbol = True
is_Vector = True
_vec_symbol = ""
_unit_vec_symbol = ""
_bold = False
_italic = False
def __new__(cls, name, **kwargs):
if not isinstance(name, (str, Symbol, VectorSymbol)):
raise TypeError("'name' must be a string or a Symbol or a VectorSymbol")
if isinstance(name, VectorSymbol):
return name
if isinstance(name, str):
name = Symbol(name)
obj = Basic.__new__(cls, name)
# TODO:
# Say we created the following two symbols:
# v1a = VectorSymbol("v1", _vec_symbol="\hat{%s}")
# v1b = VectorSymbol("v1")
# v1a == v1b -> True
# Do I want to include the attributes in the hash to make them
# different?
obj._vec_symbol = kwargs.get('_vec_symbol', "")
obj._unit_vec_symbol = kwargs.get('_unit_vec_symbol', "")
obj._bold = kwargs.get('_bold', False)
obj._italic = kwargs.get('_italic', False)
return obj
def doit(self, **kwargs):
return self
@property
def free_symbols(self):
return set((self,))
@property
def name(self):
return self.args[0].name
class VectorZero(VectorSymbol):
is_ZeroVector = True
def __new__(cls, name="0", **kwargs):
return super().__new__(cls, name, **kwargs)
def magnitude(self):
return S.Zero
def normalize(self):
return self
def _eval_derivative(self, s):
return self
class VectorOne(VectorSymbol):
def __new__(cls, name="1", **kwargs):
return super().__new__(cls, name, **kwargs)
def _eval_derivative(self, s):
return VectorZero()
VectorSymbol.zero = VectorZero()
VectorSymbol.one = VectorOne()
class Nabla(VectorSymbol):
def __new__(cls, **kwargs):
return super().__new__(cls, r"\nabla", **kwargs)
def magnitude(self):
raise TypeError("nabla operator doesn't have magnitude.")
def normalize(self):
raise TypeError("nabla operator cannot be normalized.")
@_sympifyit('other', NotImplemented)
def gradient(self, other):
return Grad(self, other)
@_sympifyit('other', NotImplemented)
def laplace(self, other):
return Laplace(self, other)
grad = gradient
lap = laplace
def _eval_derivative(self, s):
raise NotImplementedError("Differentiation of nabla operator not implemented.")
class WildVectorSymbol(Wild, VectorSymbol):
def __new__(cls, name, exclude=(), properties=(), **assumptions):
obj = Wild.__new__(cls, name, exclude=(), properties=(), **assumptions)
return obj
WVS = WildVectorSymbol
class Normalize(VectorExpr):
""" Symbolic representation of a normalized symbolic vector. Given a vector
v, the normalized form is: v / v.magnitude
"""
is_Normalized = True
is_Vector = True
def __new__(cls, v):
v = sympify(v)
if isinstance(v, Normalize):
return v
if not isinstance(v, (VectorExpr, Vector)):
raise TypeError("Can only normalize instances of VectorExpr or Vector.")
if not v.is_Vector:
raise TypeError("VectorExpr must be a vector, not a scalar.")
if isinstance(v, (Nabla, VectorZero)):
return v.norm
return Basic.__new__(cls, v)
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
if issubclass(args[0].func, Vector):
# here args[0] could be an instance of VectorAdd or VecMul or Vector
return args[0].normalize()
return VecMul(args[0], VecPow(Magnitude(args[0]), -1))
class Magnitude(VectorExpr):
""" Symbolic representation of the magnitude of a symbolic vector.
"""
is_Vector = False
is_positive = True
def __new__(cls, v):
v = sympify(v)
if isinstance(v, Magnitude):
return v
if not isinstance(v, (VectorExpr, Vector)):
# for example, v is a number, or symbol
return Abs(v)
if not v.is_Vector:
# for example, dot-product of two VectorSymbol
return Abs(v)
if isinstance(v, Nabla):
return v.mag
return Basic.__new__(cls, v)
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in self.args]
if isinstance(args[0], Vector):
return args[0].magnitude()
return self.func(*args)
class D(VectorExpr):
""" Represent an unevaluated derivative.
This class is necessary because even if it's possible to set the attribute
is_Vector=True to an unevaluated Derivative (as of Sympy version 1.5.1,
which may be a bug), it is not possible to set this attribute to objects of
type Add (read-only property). If v1 and v2 are two vector expression
with is_Vector=True:
d = Derivative(v1 + v2, x).doit()
will return Add(Derivative(v1, x), Derivative(v2, x)), with
is_Vector=False, which is wrong, preventing from further vector expression
operations. For example, VecCross(v1, d) will fail because
d.is_Vector=False.
Solution! Wrap unevaluated derivatives with this class.
"""
is_Vector = False
def __new__(cls, d):
d = sympify(d)
if not isinstance(d, Derivative):
return d
obj = Basic.__new__(cls, d)
obj.is_Vector = d.expr.is_Vector
return obj
def _eval_derivative(self, s):
d = self.args[0]
variable_count = list(d.variable_count) + [(s, 1)]
return D(Derivative(d.expr, *variable_count))
class DotCross(VectorExpr):
""" Abstract base class for VecCross and VecDot.
Not to be instantiated directly.
"""
@property
def reverse(self):
raise NotImplementedError
def diff(self, *symbols, **assumptions):
if isinstance(self.args[0], Nabla):
return D(Derivative(self, *symbols))
return super().diff(*symbols, **assumptions)
def _eval_derivative(self, s):
expr0 = self.args[0].diff(s)
expr1 = self.args[1].diff(s)
t1 = self.func(expr0, self.args[1])
t2 = self.func(self.args[0], expr1)
return t1 + t2
def expand(self, **hints):
# dot = hints.get('dot', True)
# cross = hints.get('cross', True)
prod = hints.get('prod', False)
quotient = hints.get('quotient', False)
_spaces.append(_token)
debug("DotCross expand", self)
# left = self.args[0]
# right = self.args[1]
left = self.args[0].expand(**hints)
right = self.args[1].expand(**hints)
get_args = lambda expr: expr.args if isinstance(expr, VecAdd) else [expr]
# deal with divergence/curl
if isinstance(left, Nabla):
num, den = fraction(right)
if den != S.One and quotient:
# quotient rule
return (den * self.func(left, num) - self.func(Grad(den), num)) / den**2
if prod and den == 1 and isinstance(right, VecMul):
# product rule
if len(right.args) > 1:
vector = None
scalars = []
for a in right.args:
if a.is_Vector:
vector = a
else:
scalars.append(a)
new_args = [VecMul(*scalars, self.func(left, vector))]
for i, s in enumerate(scalars):
new_args.append(
VecMul(*scalars[:i], *scalars[i+1:], self.func(Grad(s), vector))
)
return VecAdd(*new_args)
return self
right = get_args(right)
return VecAdd(*[self.func(left, r) for r in right])
debug("\t", left, right)
if not (isinstance(left, VecAdd) or isinstance(right, VecAdd)):
debug("\t ASD0 not VecAdd", self.func(left, right))
del _spaces[-1]
return self.func(left, right)
left = get_args(left)
right = get_args(right)
debug("\t ASD1 left", left)
debug("\t ASD2 right", right)
def _get_vector(expr):
if isinstance(expr, (VectorSymbol, VecCross, VecDot, Grad, Laplace, Advection)):
return expr
return [t for t in expr.args if t.is_Vector][0]
def _get_coeff(expr):
if isinstance(expr, (VectorSymbol, Grad, Advection, Laplace)):
return 1
return VecMul(*[t for t in expr.args if not t.is_Vector])
# get_vector = lambda expr: [t for t in expr.args if t.is_Vector][0]
# get_coeff = lambda expr: VecMul(*[t for t in expr.args if not t.is_Vector])
terms = []
for l in left:
cl = _get_coeff(l)
vl = _get_vector(l)
for r in right:
cr = _get_coeff(r)
vr = _get_vector(r)
# print("vl", vl)
# print("vr", vr)
c = cl * cr
terms.append(c * self.func(vl, vr))
debug("\t ASD3 terms", terms)
del _spaces[-1]
return VecAdd(*terms)
class VecDot(DotCross):
""" Symbolic representation of the dot product between two symbolic vectors.
"""
is_Dot = True
def __new__(cls, expr1, expr2, **kwargs):
expr1 = sympify(expr1)
expr2 = sympify(expr2)
check = lambda x: isinstance(x, (VectorExpr, Vector))
if not (check(expr1) and check(expr2) and \
expr1.is_Vector and expr2.is_Vector):
raise TypeError("Both side of the dot-operator must be vectors:\n" +
"\t Left: " + str(expr1.func) + ", " + str(expr1.is_Vector) + ", {}\n".format(expr1) +
"\t Right: " + str(expr2.func) + ", " + str(expr2.is_Vector) + ", {}\n".format(expr2)
)
if expr1 == VectorZero() or expr2 == VectorZero():
return S.Zero
if expr1 == expr2 and isinstance(expr1, Nabla):
raise TypeError("Dot product of two nabla operators not supported.\n" +
"To create the Laplacian operator, use the class Laplace.")
if isinstance(expr2, Nabla):
raise TypeError("To compute the divergence, nabla operator must be the first argument.\n" +
"To write the advection operator, use the class Advection."
)
obj = Expr.__new__(cls, expr1, expr2)
return obj
@property
def reverse(self):
# take into account the fact that arg[0] and arg[1] could be mixed
# instances of Vector and VectorExpr
return self.func(self.args[1], self.args[0])
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
if isinstance(args[0], Vector) and \
isinstance(args[1], Vector):
return args[0].dot(args[1])
if isinstance(args[0], Vector) and \
isinstance(args[1], Nabla):
return divergence(args[0])
if isinstance(args[1], Vector) and \
isinstance(args[0], Nabla):
return divergence(args[1])
if args[0] == args[1]:
return VecPow(args[0].mag, 2)
return self.func(*args)
def expand(self, **hints):
dot = hints.get('dot', True)
if dot:
return super().expand(**hints)
left = self.args[0].expand(**hints)
right = self.args[1].expand(**hints)
return self.func(left, right)
class VecCross(DotCross):
""" Symbolic representation of the cross product between two symbolic
vectors.
"""
is_Cross = True
is_Vector = True
is_commutative = False
def __new__(cls, expr1, expr2):
expr1 = sympify(expr1)
expr2 = sympify(expr2)
check = lambda x: isinstance(x, (VectorExpr, Vector))
if not (check(expr1) and check(expr2) and \
expr1.is_Vector and expr2.is_Vector):
raise TypeError("Both side of the cross-operator must be vectors\n" +
"\t Left: " + str(expr1.func) + ", " + str(expr1.is_Vector) + ", {}\n".format(expr1) +
"\t Right: " + str(expr2.func) + ", " + str(expr2.is_Vector) + ", {}\n".format(expr2)
)
if expr1 == VectorZero() or expr2 == VectorZero():
return VectorZero()
if (isinstance(expr1, Vector) and isinstance(expr2, Vector) and
expr1 == expr2):
# TODO: At this point I'm dealing with unevaluated cross product.
# is it better to return VectorZero()?
return expr1.zero
if expr1 == expr2 and isinstance(expr1, Nabla):
raise TypeError("Cross product of two nabla operators not supported.")
if isinstance(expr2, Nabla):
raise TypeError("To compute the curl, nabla operator must be the first argument.\n" +
"To write the advection operator, use the class Advection.\n" +
"\t Left: " + str(expr1.func) + "{}\n".format(expr1) +
"\t Right: " + str(expr2.func) + "{}\n".format(expr2)
)
if expr1 == expr2:
return VectorZero()
obj = Expr.__new__(cls, expr1, expr2)
return obj
@property
def reverse(self):
# take into account the fact that arg[0] and arg[1] could be mixed
# instances of Vector and VectorExpr
return -self.func(self.args[1], self.args[0])
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
if isinstance(args[0], Vector) and \
isinstance(args[1], Vector):
return args[0].cross(args[1])
if isinstance(args[0], Vector) and \
isinstance(args[1], Nabla):
return -curl(args[0])
if isinstance(args[1], Vector) and \
isinstance(args[0], Nabla):
return curl(args[1])
return self.func(*args)
def expand(self, **hints):
cross = hints.get('cross', True)
if cross:
return super().expand(**hints)
left = self.args[0].expand(**hints)
right = self.args[1].expand(**hints)
return self.func(left, right)
class Advection(VectorExpr):
""" Symbolic representation of the following operator/expression:
(v & nabla) * f
where:
v : vector
f : vector or scalar field
Note that this is different than (nabla & v) * f, because (nabla & v) is the
divergence of v.
"""
is_Vector = True
is_commutative = False
def __new__(cls, v, f, n = Nabla()):
v = sympify(v)
f = sympify(f)
if not isinstance(n, Nabla):
raise TypeError("n must be an instance of the class Nabla.")
if (not v.is_Vector) or isinstance(v, Nabla):
raise TypeError("v must be a vector or an expression with is_Vector=True. It must not be nabla.")
if isinstance(f, Nabla):
raise TypeError("f (the field) cannot be nabla.")
obj = Expr.__new__(cls, v, f, n)
obj.is_Vector = v.is_Vector
return obj
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
v, f, n = args
if not f.is_Vector and isinstance(v, Vector):
return v & Grad(f).doit(**kwargs)
if isinstance(f, Vector) and isinstance(v, Vector):
s = Vector.zero
for e, comp in f.components.items():
s += e * Advection(v, comp).doit(**kwargs)
return s
return self.func(*args)
# used for debugging the expressions trees: append a _token to _spaces each time
# an object is created. Remember to remove the token once the object has been
# created
_spaces = []
_token = " "
_DEBUG = False
def debug(*args):
if _DEBUG:
print("".join(_spaces), *args)
# _print_Gradient is already used in pretty_print, hence the name Gradient
class Grad(VectorExpr):
""" Symbolic representation of the gradient of a scalar field.
"""
is_Vector = True
def __new__(cls, arg1, arg2=None, **kwargs):
# Ideally, only the scalar field is required. However, it can
# accept also the nabla operator (in case of some rendering
# customization)
if not arg2:
n = Nabla()
f = sympify(arg1)
else:
n = sympify(arg1)
f = sympify(arg2)
if not isinstance(n, Nabla):
raise TypeError("The first argument of the gradient operator must be Nabla.")
if isinstance(f, Nabla):
raise NotImplementedError("Differentiation of nabla operator not implemented.")
if f.is_Vector:
raise TypeError("Gradient of vector fields not implemented.")
return Basic.__new__(cls, n, f)
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
# TODO: is the following condition ok?
if not isinstance(args[1], VectorExpr):
return gradient(args[1])
return self.func(*args)
def expand(self, **hints):
gradient = hints.get('gradient', False)
prod = hints.get('prod', False)
quotient = hints.get('quotient', False)
if gradient:
if isinstance(self.args[1], Add):
return VecAdd(*[self.func(a).expand(**hints) for a in self.args[1].args])
# if isinstance(self.args[1], Mul):
num, den = fraction(self.args[1])
if den != S.One and quotient:
# quotient rule
return (den * Grad(num) - num * Grad(den)) / den**2
if prod and den == 1:
# product rule
args = self.args[1].args
if len(args) > 1:
new_args = []
for i, a in enumerate(args):
new_args.append(
VecMul(*args[:i], *args[i+1:], self.func(a))
)
return VecAdd(*new_args)
return self
class Laplace(VectorExpr):
""" Symbolic representation of the Laplacian operator.
"""
is_Vector = False
def __new__(cls, arg1, arg2=None, **kwargs):
# Ideally, only the scalar/vector field is required. However, it can
# accept also the nabla operator (in case of some rendering
# customization)
if not arg2:
n = Nabla()
f = sympify(arg1)
else:
n = sympify(arg1)
f = sympify(arg2)
if not isinstance(n, Nabla):
raise TypeError("The first argument of the laplace operator must be Nabla.")
if isinstance(f, Nabla):
raise NotImplementedError("Differentiation of nabla operator not implemented.")
# if f.is_Vector:
# raise TypeError("Gradient of vector fields not implemented.")
obj = Basic.__new__(cls, n, f)
if f.is_Vector:
obj.is_Vector = True
return obj
def doit(self, **kwargs):
deep = kwargs.get('deep', True)
args = self.args
if deep:
args = [arg.doit(**kwargs) for arg in args]
if not isinstance(args[1], (Vector, VectorExpr)):
# scalar field: we use the identity nabla^2 = nabla.div(f.grad())
return VecDot(args[0], Grad(args[0], args[1])).doit(deep=deep)
elif isinstance(args[1], Vector):
components = args[1].components
laplace = lambda c: VecDot(args[0], Grad(args[0], c)).doit()
v = Vector.zero
for base, comp in components.items():
v += base * laplace(comp)
return v
return self.func(*args)
def expand(self, **hints):
laplacian = hints.get('laplacian', False)
prod = hints.get('prod', False)
if laplacian:
if isinstance(self.args[1], Add):
return VecAdd(*[self.func(a).expand(**hints) for a in self.args[1].args])
if prod:
# product rule:
n, f = self.args
args = f.args
if (len(args) == 2) and all([not a.is_Vector for a in args]):
return args[0] * Laplace(n, args[1]) + args[1] * Laplace(n, args[0]) + 2 * (Grad(n, args[0]) & Grad(n, args[1]))
return self
class VecAdd(VectorExpr, Add):
""" A sum of Vector expressions.
VecAdd inherits from and operates like SymPy Add.
"""
is_VecAdd = True
is_commutative = True
def __new__(cls, *args, **kwargs):
evaluate = kwargs.get('evaluate', True)
if not args:
return VectorZero()
args = list(map(sympify, args))
# If for any reasons we create VecAdd(0), I want 0 to be returned.
# Skipping this check, VectorZero() will be returned instead
if len(args) == 1 and args[0] == S.Zero:
return S.Zero
if evaluate:
# remove instances of S.Zero and VectorZero
args = [a for a in args
if not (isinstance(a, VectorZero) or (a == S.Zero) or (a == Vector.zero))]
if len(args) == 0:
return VectorZero()
elif len(args) == 1:
# doesn't make any sense to have 1 argument in VecAdd if
# evaluate=True
return args[0]
obj = AssocOp.__new__(cls, *args, evaluate=evaluate)
obj = _sanitize_args(obj)
# print("VecAdd OBJ", obj.func, obj)
if not isinstance(obj, cls):
return obj
# are there any scalars or vectors?
any_vectors = any([a.is_Vector for a in obj.args])
all_vectors = all([a.is_Vector for a in obj.args])
# addition of mixed scalars and vectors is not supported.
# If there are scalars in the addition, either all arguments
# are scalars (hence not a vector expression) or there are
# mixed arguments, hence throw an error
obj.is_Vector = all_vectors
if (any_vectors and (not all_vectors)):
asd = obj.args[1]
print("####", asd.is_Vector, asd.args)
print("####", [a.is_Vector for a in asd.args])
for a in postorder_traversal(asd):
print(a.func, a.is_Vector, a)
raise TypeError("VecAdd: Mix of Vector and Scalar symbols:\n\t" +
"\n\t".join(str(a.func) + ", " + str(a.is_Vector) + ", " + str(a) for a in obj.args)
)
return obj
def doit(self, **kwargs):
# Need to override this method in order to apply the rules defined
# below. Adapted from MatAdd.
deep = kwargs.get('deep', True)
if deep:
args = [arg.doit(**kwargs) for arg in self.args]
else:
args = self.args
return canonicalize(VecAdd(*args))
from sympy.strategies import (sort, condition, exhaust, do_one)
from sympy.utilities.iterables import sift
from operator import add, mul
def merge_explicit(vecadd):
""" Merge Vector arguments
Example
========
>>> from sympy.vector import CoordSys3D
>>> v1 = VectorSymbol("v1")
>>> v2 = VectorSymbol("v2")
>>> C = CoordSys3D("C")
>>> vn1 = 2 * C.i + 3 * C.j + 4 * C.k
>>> vn2 = x * C.i + y * C.j + x * y * C.k
>>> expr = v1 + v2 + vn1 + vn2
>>> pprint(expr)
(2*C.i + 3*C.j + 4*C.k) + (x*C.i + y*C.j + x*y*C.k) + v1 + v2
>>> pprint(merge_explicit(expr))
((2 + x)*C.i + (3 + y)*C.j + (4 + x*y)*C.k) + v1 + v2
"""
# there has to be some bug deep inside the core, I absolutely need this
# function to correctly perform addition
def recreate_args(args):
return [a.func(*a.args) for a in args]
# Adapted from sympy.matrices.expressions.matadd.py
groups = sift(vecadd.args, lambda arg: isinstance(arg, (Vector)))
if len(groups[True]) > 1:
return VecAdd(*(recreate_args(groups[False]) + [reduce(add, recreate_args(groups[True]))]))
# return VecAdd(*(groups[False] + [reduce(add, groups[True])]))
else:
return vecadd
rules = (
merge_explicit,
sort(default_sort_key)
)
canonicalize = exhaust(condition(lambda x: isinstance(x, VecAdd),
do_one(*rules)))
class VecMul(VectorExpr, Mul):
""" A product of Vector expressions.
VecMul inherits from and operates like SymPy Mul.
"""
is_VecMul = True
is_commutative = True
def __new__(cls, *args, **kwargs):
evaluate = kwargs.get('evaluate', True)
print("VecMul __new__", args)
if not args:
# it makes sense to return VectorOne, after all we are talking
# about VecMul. However, this would play against us when using
# expand(), because we would have multiplications of multiple
# vectors (one of which, VectorOne).
# Hence, better to return S.One.
return S.One
args = list(map(sympify, args))
if len(args) == 1:
# doesn't make any sense to have 1 argument in VecAdd if
# evaluate=True
return args[0]
# # TODO: look through args (which are unprocessed as of now) for
# # (a & nabla) * x (where x can be a scalar field or a vector field)
# dot, field = None, None
# skip_idx = []
# dot_field = []
# for i in range(len(args) - 1):
# if isinstance(args[i], VecDot) and isinstance(args[i].args[1], Nabla):
# dot = args[i]
# field = args[i + 1]