def __init__(self, e, context=None): r'''differential term >>> x,y,z = var("x y z") >>> f = function("f")(x,y,z) >>> g = function("g")(x,y,z) >>> h = function("h")(x,y,z) >>> ctx = Context ((f,g),(x,y,z)) >>> d = (x**2) * diff(f, x, y) >>> dterm = _Dterm(d,ctx) >>> print (dterm) (x^2) * diff(f(x, y, z), x, y) ''' self._coeff, self._d = 1, 1 self._context = context if is_derivative(e) or is_function(e): self._d = e else: r = [] for o in e.operands(): if is_derivative(o) or is_function(o): self._d = o else: r.append(o) self._coeff = functools.reduce(mul, r, 1) self._expression = self._coeff * self._d
def _init(self, e): if is_derivative(e) or is_function(e): self._p.append(_Dterm(e, self._context)) elif e.operator().__name__ == 'mul_vararg': self._p.append(_Dterm(e, self._context)) else: for s in e.operands(): coeff, d = [], [] if is_derivative(s) or is_function(s): d.append(s) else: for item in s.operands(): (d if (is_derivative(item) or self.ctxfunc(item)) else coeff).append(item) coeff = functools.reduce(mul, coeff, 1) found = False if d: for _p in self._p: if eq(_p._d, d[0]): _p._coeff += coeff found = True break if not found: if d: self._p.append(_Dterm(coeff * d[0], self._context)) else: self._p.append(_Dterm(coeff, self._context)) self._p.sort(key=functools.cmp_to_key( lambda item1, item2: sorter(item1._d, item2._d, self._context)), reverse=True) self.normalize()
def analyze_dterm(dd): if is_derivative(dd): f = dd.operator().function() elif is_function(dd): f = dd.operator() else: f = [ _ for _ in dd.operands() if is_function(_) or is_derivative(_) ][0] if is_derivative(f): f = f.operator().function() return f
def __init__(self, dependent, independent, weight=Mgrevlex): """ sorting : (in)dependent [i] > dependent [i+i] """ # XXX maybe we can create the matrices here? self._independent = tuple(independent) self._dependent = tuple( (_.operator() if is_function(_) else _ for _ in dependent)) self._weight = weight(self._dependent, self._independent) self._basefield = PolynomialRing(QQ, independent)
def EulerD(density, depend, independ): r''' >>> t = var("t") >>> u= function('u') >>> v= function('v') >>> L=u(t)*v(t) + diff(u(t), t)**2 + diff(v(t), t)**2 - u(t)**2 - v(t)**2 >>> EulerD(L, (u,v), t) [-2*u(t) + v(t) - 2*diff(u(t), t, t), u(t) - 2*v(t) - 2*diff(v(t), t, t)] >>> L=u(t)*v(t) + diff(u(t), t)**2 + diff(v(t), t)**2 + 2*diff(u(t), t) * diff(v(t), t) >>> EulerD(L, (u,v), t) [v(t) - 2*diff(u(t), t, t) - 2*diff(v(t), t, t), u(t) - 2*diff(u(t), t, t) - 2*diff(v(t), t, t)] ''' wtable = [function("w_%s" % i) for i in range(len(depend))] y = function('y') w = function('w') e = var('e') result = [] for j in range(len(depend)): loc_result = 0 def f0(*args): return y(independ) + e * w(independ) def dep(*args): return depend[j](independ) fh = density.substitute_function(depend[j], f0) fh = fh.substitute_function(y, dep) fh = fh.substitute_function(w, wtable[j]) fh = fh.diff(e) fh = fh.subs({e: 0}).expand() if fh.operator().__name__ == 'mul_vararg': operands = [fh] else: operands = fh.operands() for operand in operands: d = None coeff = [] for _ops in operand.operands(): if is_op_du(_ops, wtable[j](independ)): d = _ops.operands().count(independ) elif is_function(_ops) and _ops.operator() == wtable[j]: pass else: coeff.append(_ops) coeff = functools.reduce(mul, coeff, 1) if d is not None: coeff = ((-1)**d) * diff(coeff, independ, d) loc_result += coeff result.append(loc_result) return result
def Lfunc(self): if is_function(self._p[0]._d): return self._p[0]._d.operator() else: return self._p[0]._d.operator().function()