From: Antonio Jimenez Pastor Date: Mon, 8 Oct 2018 11:02:55 +0000 (+0200) Subject: Improved the simplifier of functions. X-Git-Url: http://git.risc.jku.at/gitweb/?a=commitdiff_plain;h=2c1ec50812f0f0575f560e348e6d0ff1178fc6a9;p=ajpastor%2Fdiff_defined_functions.git Improved the simplifier of functions. Now from D(K[x]) we can simplify to K[x]. Fixed a couple of errors on the examples --- diff --git a/ajpastor/dd_functions/ddExamples.py b/ajpastor/dd_functions/ddExamples.py index 9fe0262..a9e4c87 100644 --- a/ajpastor/dd_functions/ddExamples.py +++ b/ajpastor/dd_functions/ddExamples.py @@ -104,8 +104,8 @@ def Sin(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Sin(x)(input); + if(is_DDFunction(input)): + return Sin(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -137,8 +137,8 @@ def Cos(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Cos(x)(input); + if(is_DDFunction(input)): + return Cos(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -170,8 +170,8 @@ def Tan(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Tan(x)(input); + if(is_DDFunction(input)): + return Tan(x)(input); if(input == x): return DDFinite.element([-_sage_const_2,0,Cos(x)**2],[0,1]); g, dR = __decide_parent(input, ddR,_sage_const_2 ); @@ -220,8 +220,8 @@ def Sinh(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Sinh(x)(input); + if(is_DDFunction(input)): + return Sinh(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -253,8 +253,8 @@ def Cosh(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Cosh(x)(input); + if(is_DDFunction(input)): + return Cosh(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -293,8 +293,8 @@ def Log(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Log(x+_sage_const_1 )(input-1); + if(is_DDFunction(input)): + return Log(x+_sage_const_1 )(input-1); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -326,8 +326,8 @@ def Log1(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Log1(x)(input); + if(is_DDFunction(input)): + return Log1(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); @@ -361,8 +361,8 @@ def Exp(input, ddR = None): This function can be converted into symbolic expressions. ''' - #if(is_DDFunction(input)): - # return Exp(x)(input); + if(is_DDFunction(input)): + return Exp(x)(input); f,dR = __decide_parent(input, ddR); evaluate = lambda p : dR.getSequenceElement(p,_sage_const_0 ); diff --git a/ajpastor/dd_functions/ddFunction.py b/ajpastor/dd_functions/ddFunction.py index 75054c7..82468ab 100644 --- a/ajpastor/dd_functions/ddFunction.py +++ b/ajpastor/dd_functions/ddFunction.py @@ -1864,13 +1864,24 @@ class DDFunction (IntegralDomainElement): if(is_DDRing(R)): return R(self).to_simpler(); elif(is_PolynomialRing(R)): - if(self.getOrder() == 1): - degree = abs(self[0].lc()); - if(self.derivative(times=degree+1).is_null): - x = self.parent().variables()[0]; - return sum(self.getSequenceElement(i)*x**i for i in range(degree+1)); - except: - pass; + degs = [self[i].degree() - i for i in range(self.getOrder()+1)]; + m = max(degs); + maxs = [i for i in range(len(degs)) if degs[i] == m]; + + if(len(maxs) <= 1): + raise ValueError("1:Function %s is not a polynomial" %repr(self)); + + x = R.gens()[0]; + pol = sum(falling_factorial(x,i)*self[i].lc() for i in maxs); + + root = max(root[0] for root in pol.roots() if (root[0] in ZZ and root[0] > 0)); + pol = sum(x**i*self.getSequenceElement(i) for i in range(root+1)); + + if(pol == self): + return pol; + raise ValueError("2:Function %s is not a polynomial" %repr(self)); + except Exception as e: + print e; return self; def quick_equals(self,other): ### TO REVIEW diff --git a/ajpastor/lazy/conversion.py b/ajpastor/lazy/conversion.py index 80c3ef8..4cc1ada 100644 --- a/ajpastor/lazy/conversion.py +++ b/ajpastor/lazy/conversion.py @@ -166,6 +166,9 @@ class ConversionSystem(object): - Matrices or vectors with polynomials rocignized by the Conversion system - Elements in `self.base()` ''' + from sage.structure.element import is_Matrix; + from sage.structure.element import is_Vector; + if(element in self.poly_ring()): try: return self.poly_ring()(element).reduce(self._relations()); @@ -182,12 +185,12 @@ class ConversionSystem(object): return set([self.simplify(el) for el in element]); elif(isinstance(element, tuple)): return tuple([self.simplify(el) for el in element]); - elif(isinstance(element, sage.matrix.matrix.Matrix)): + elif(is_Matrix(element)): R = self.poly_ring(); if(element.parent().base().is_field()): R = self.poly_field(); return Matrix(R, [[self.simplify(el) for el in row] for row in element]); - elif(isinstance(element, sage.modules.free_module_element.FreeModuleElement)): + elif(is_Vector(element)): R = self.poly_ring(); if(element.parent().base().is_field()): R = self.poly_field(); diff --git a/releases/diff_defined_functions__0.6.zip b/releases/diff_defined_functions__0.6.zip index ab45241..355a2d0 100644 Binary files a/releases/diff_defined_functions__0.6.zip and b/releases/diff_defined_functions__0.6.zip differ diff --git a/releases/old/diff_defined_functions__0.6__18.10.08_13:02:55.zip b/releases/old/diff_defined_functions__0.6__18.10.08_13:02:55.zip new file mode 100644 index 0000000..355a2d0 Binary files /dev/null and b/releases/old/diff_defined_functions__0.6__18.10.08_13:02:55.zip differ