[sympycore] r1210 committed - Implemented variable_integrate and variable_subs methods for Polynomia...

1 view
Skip to first unread message

codesite...@google.com

unread,
Sep 18, 2011, 5:35:45 PM9/18/11
to sympycor...@googlegroups.com
Revision: 1210
Author: pearu.peterson
Date: Sun Sep 18 14:34:28 2011
Log: Implemented variable_integrate and variable_subs methods for
PolynomialRing.
http://code.google.com/p/sympycore/source/detail?r=1210

Modified:
/trunk/sympycore/arithmetic/number_theory.py
/trunk/sympycore/heads/polynomial.py
/trunk/sympycore/polynomials/algebra.py

=======================================
--- /trunk/sympycore/arithmetic/number_theory.py Sun Apr 3 12:36:26 2011
+++ /trunk/sympycore/arithmetic/number_theory.py Sun Sep 18 14:34:28 2011
@@ -172,7 +172,8 @@

where ``a(n,0) = p_0^n``.
"""
-
+ if m==0:
+ return {}
if m==2:
return binomial_coefficients(n)
symbols = [(0,)*i + (1,) + (0,)*(m-i-1) for i in range(m)]
=======================================
--- /trunk/sympycore/heads/polynomial.py Sun Mar 15 12:15:52 2009
+++ /trunk/sympycore/heads/polynomial.py Sun Sep 18 14:34:28 2011
@@ -5,6 +5,7 @@

from ..core import init_module, Pair
init_module.import_heads()
+init_module.import_numbers()

class SparsepolyHead(Head):
"""
@@ -51,6 +52,39 @@
def term_coeff(self, cls, expr):
return expr, 1

+ def integrate_indefinite_index(self, cls, data, expr, index):
+ """
+ Return indefinite integral of expr with respect to index-th
variable.
+ data is expr.data, index is integer.
+ """
+ new_data = {}
+ for exp, coeff in data.iteritems():
+ new_exp = type(exp)(*exp.pair)
+ new_exp[index] += 1
+ new_coeff = number_div(cls.ring, coeff, new_exp[index])
+ new_data[new_exp] = new_coeff
+ return cls(new_data)
+
+ def diff_index(self, cls, data, expr, index, order):
+ """
+ Return the order-th derivative of expr with respect to index-th
variable.
+ data is expr.data, index is integer.
+ """
+ if order==0:
+ return expr
+ new_data = {}
+ for exp, coeff in data.iteritems():
+ if order > exp[index]:
+ continue
+ new_exp = type(exp)(*exp.pair)
+ new_exp[index] -= order
+ n = exp[index]-1
+ new_coeff = coeff * (exp[index]-1)
+ for o in range(order-1):
+ n *= exp[index]-1
+ new_data[new_exp] = coeff * n
+ return cls(new_data)
+
class DensepolyHead(Head):
"""
DensepolyHead is a head for dense polynomials represented
=======================================
--- /trunk/sympycore/polynomials/algebra.py Fri Aug 21 04:01:35 2009
+++ /trunk/sympycore/polynomials/algebra.py Sun Sep 18 14:34:28 2011
@@ -454,7 +454,7 @@
return self.zero
raise NotImplementedError(`self,nvars`)

- def diff(self, index=0):
+ def diff(self, index=0): # TODO: don't use index as the order of
variables is automatically sorted
if self.nvars==0:
return self.zero
head, data = self.pair
@@ -472,6 +472,41 @@
d[exps.add(index,-1)] = coeff * e
return type(self)(head, d)

+ def variable_integrate(self, variable, *bounds):
+ try:
+ index = list(self.variables).index(variable)
+ except ValueError:
+ index = None
+ if index is not None:
+ indef_integral =
self.head.integrate_indefinite_index(type(self), self.data, self, index)
+ if bounds:
+ low, high = bounds
+ return indef_integral.variable_subs(variable, high) -
indef_integral.variable_subs(variable, low)
+ return indef_integral
+ raise NotImplementedError(`self.variables, variable, index`)
+
+ def variable_subs(self, variable, newexpr):
+ cls = type(self)
+ newexpr = cls(newexpr)
+ try:
+ index = list(self.variables).index(variable)
+ except ValueError:
+ index = None
+ if index is not None:
+ head, data = self.pair
+ result = cls.Number(0)
+ variables = cls.variables
+ for exps, coeff in data.iteritems():
+ term = cls.Number(1)
+ for i,exp in enumerate(exps):
+ if exp:
+ if i==index:
+ term *= newexpr**exp
+ else:
+ term *= cls.Symbol(variables[i])**exp
+ result += term * cls.Number(coeff)
+ return result
+ raise NotImplementedError(`self.variables, variable, index`)

def divmod_POLY1_POLY1_SPARSE(lhs, rhs, cls):
d2 = rhs.degree

Reply all
Reply to author
Forward
0 new messages