This solution applies the tips from the other answers and from here. The D
operator can be defined as follows:
- considered only when multiplied from the left, so that
D(t)*2*t**3 = 6*t**2
but 2*t**3*D(t)
does nothing
- all the expressions and symbols used with
D
must have is_commutative = False
- is evaluated in the context of a given expression using
evaluateExpr()
- which goes from the right to the left along the expression finding the
D
opperators and applying mydiff()
* to the corresponding right portion
*:mydiff
is used instead of diff
to allow a higher order D
to be created, like mydiff(D(t), t) = D(t,t)
The diff
inside __mul__()
in D
was kept for reference only, since in the current solution the evaluateExpr()
actually does the differentiation job. A python mudule was created and saved as d.py
.
import sympy
from sympy.core.decorators import call_highest_priority
from sympy import Expr, Matrix, Mul, Add, diff
from sympy.core.numbers import Zero
class D(Expr):
_op_priority = 11.
is_commutative = False
def __init__(self, *variables, **assumptions):
super(D, self).__init__()
self.evaluate = False
self.variables = variables
def __repr__(self):
return 'D%s' % str(self.variables)
def __str__(self):
return self.__repr__()
@call_highest_priority('__mul__')
def __rmul__(self, other):
return Mul(other, self)
@call_highest_priority('__rmul__')
def __mul__(self, other):
if isinstance(other, D):
variables = self.variables + other.variables
return D(*variables)
if isinstance(other, Matrix):
other_copy = other.copy()
for i, elem in enumerate(other):
other_copy[i] = self * elem
return other_copy
if self.evaluate:
return diff(other, *self.variables)
else:
return Mul(self, other)
def __pow__(self, other):
variables = self.variables
for i in range(other-1):
variables += self.variables
return D(*variables)
def mydiff(expr, *variables):
if isinstance(expr, D):
expr.variables += variables
return D(*expr.variables)
if isinstance(expr, Matrix):
expr_copy = expr.copy()
for i, elem in enumerate(expr):
expr_copy[i] = diff(elem, *variables)
return expr_copy
return diff(expr, *variables)
def evaluateMul(expr):
end = 0
if expr.args:
if isinstance(expr.args[-1], D):
if len(expr.args[:-1])==1:
cte = expr.args[0]
return Zero()
end = -1
for i in range(len(expr.args)-1+end, -1, -1):
arg = expr.args[i]
if isinstance(arg, Add):
arg = evaluateAdd(arg)
if isinstance(arg, Mul):
arg = evaluateMul(arg)
if isinstance(arg, D):
left = Mul(*expr.args[:i])
right = Mul(*expr.args[i+1:])
right = mydiff(right, *arg.variables)
ans = left * right
return evaluateMul(ans)
return expr
def evaluateAdd(expr):
newargs = []
for arg in expr.args:
if isinstance(arg, Mul):
arg = evaluateMul(arg)
if isinstance(arg, Add):
arg = evaluateAdd(arg)
if isinstance(arg, D):
arg = Zero()
newargs.append(arg)
return Add(*newargs)
#courtesy: https://mcmap.net/q/49500/-make-all-symbols-commutative-in-a-sympy-expression
def disableNonCommutivity(expr):
replacements = {s: sympy.Dummy(s.name) for s in expr.free_symbols}
return expr.xreplace(replacements)
def evaluateExpr(expr):
if isinstance(expr, Matrix):
for i, elem in enumerate(expr):
elem = elem.expand()
expr[i] = evaluateExpr(elem)
return disableNonCommutivity(expr)
expr = expr.expand()
if isinstance(expr, Mul):
expr = evaluateMul(expr)
elif isinstance(expr, Add):
expr = evaluateAdd(expr)
elif isinstance(expr, D):
expr = Zero()
return disableNonCommutivity(expr)
Example 1: curl of a vector field. Note that it is important to define the variables with commutative=False
since their order in Mul().args
will affect the results, see this other question.
from d import D, evaluateExpr
from sympy import Matrix
sympy.var('x', commutative=False)
sympy.var('y', commutative=False)
sympy.var('z', commutative=False)
curl = Matrix( [[ D(x), D(y), D(z) ]] )
field = Matrix( [[ x**2*y, x*y*z, -x**2*y**2 ]] )
evaluateExpr( curl.cross( field ) )
# [-x*y - 2*x**2*y, 2*x*y**2, -x**2 + y*z]
Example 2: Typical Ritz approximation used in structural analysis.
from d import D, evaluateExpr
from sympy import sin, cos, Matrix
sin.is_commutative = False
cos.is_commutative = False
g1 = []
g2 = []
g3 = []
sympy.var('x', commutative=False)
sympy.var('t', commutative=False)
sympy.var('r', commutative=False)
sympy.var('A', commutative=False)
m=5
n=5
for j in xrange(1,n+1):
for i in xrange(1,m+1):
g1 += [sin(i*x)*sin(j*t), 0, 0]
g2 += [ 0, cos(i*x)*sin(j*t), 0]
g3 += [ 0, 0, sin(i*x)*cos(j*t)]
g = Matrix( [g1, g2, g3] )
B = Matrix(\
[[ D(x), 0, 0],
[ 1/r*A, 0, 0],
[ 1/r*D(t), 0, 0],
[ 0, D(x), 0],
[ 0, 1/r*A, 1/r*D(t)],
[ 0, 1/r*D(t), D(x)-1/x],
[ 0, 0, 1],
[ 0, 1, 0]])
ans = evaluateExpr(B*g)
A print_to_file()
function has been created to quickly check big expressions.
import sympy
import subprocess
def print_to_file( guy, append=False ):
flag = 'w'
if append: flag = 'a'
outfile = open(r'print.txt', flag)
outfile.write('\n')
outfile.write( sympy.pretty(guy, wrap_line=False) )
outfile.write('\n')
outfile.close()
subprocess.Popen( [r'notepad.exe', r'print.txt'] )
print_to_file( B*g )
print_to_file( ans, append=True )
__rmul__
from the right object first than__mul__
from the left object. See question: #5181820 – SouseEvalfMixin
? – Toxic