Ask Your Question
1

Taylor expansion crashing (python3)

asked 2019-10-19 09:22:27 -0600

updated 2019-10-19 10:26:26 -0600

tmonteil gravatar image

I type the following in the sage terminal :

f = (x+1)**(0.1)+exp(x)
f.taylor(x,0,3)

That causes me quite a crash I do not understand.

I tried with some variations, like

f = x**(0.1)+exp(x)
f.taylor(x,0,3)

or

f = (x+1)**(0.1)+exp(x)
f.taylor(x,0,2)

Both are working well.

EDIT :

This one

g = 3*(x+1)**(1/10)+exp(x)
g.taylor(x,0,3)
  • works well in the sage terminal
  • crashes when it is in a file "attach"ed to the sage terminal.

I guess it is due to the Sage's preparser.

Any idea ? Why does the Taylor expansion of that particular function crashes from order 3 ?

If it is important, I'm using a home-compiled version of Sage with python3 :

┌────────────────────────────────────────────────────────────────────┐ │ SageMath version 8.9, Release Date: 2019-09-29 │ │ Using Python 3.7.3. Type "help()" for help. │ └────────────────────────────────────────────────────────────────────┘

Here is the trace :

┌─────────────────────────────────┐ │ SageMath version 8.9, Release Date: 2019-09-29 │ │ Using Python 3.7.3. Type "help()" for help. │ └─────────────────────────────────┘ sage: f = (x+1)**(0.1)+exp(x)

sage: f.taylor(x,0,3)

RuntimeError Traceback (most recent call last) /home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in __init__(self, parent, value, is_name, name) 707 try: --> 708 self._name = parent._create(value, name=name) 709 except (TypeError, RuntimeError, ValueError) as x:

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/maxima_lib.py in _create(self, value, name) 605 else: --> 606 self.set(name, value) 607 except RuntimeError as error:

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/maxima_lib.py in set(self, var, value) 514 cmd = '%s : %s$'%(var, value.rstrip(';')) --> 515 self.eval(cmd) 516

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/maxima_lib.py in _eval_line(self, line, locals, reformat, **kwds) 460 if statement: --> 461 maxima_eval("#$%s$" % statement) 462 if not reformat:

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/libs/ecl.pyx in sage.libs.ecl.EclObject.__call__ (build/cythonized/sage/libs/ecl.c:7793)() 805 lispargs = EclObject(list(args)) --> 806 return ecl_wrap(ecl_safe_apply(self.obj,(<eclobject>lispargs).obj)) 807

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/libs/ecl.pyx in sage.libs.ecl.ecl_safe_apply (build/cythonized/sage/libs/ecl.c:5455)() 377 s = si_coerce_to_base_string(ecl_values(1)) --> 378 raise RuntimeError("ECL says: {}".format( 379 char_to_str(ecl_base_string_pointer_safe(s))))

RuntimeError: ECL says: In function GCD, the value of the first argument is 0.91 which is not of the expected type INTEGER

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last) <ipython-input-2-ed48eb8006d1> in <module>() ----> 1 f.taylor(x,Integer(0),Integer(3))

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.taylor (build/cythonized/sage/symbolic/expression.cpp:27942)() 4581 except Exception: 4582 raise NotImplementedError("Wrong arguments passed to taylor. See taylor? for more details.") -> 4583 l = self._maxima_().taylor(B) 4584 return self.parent()(l) 4585

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in __call__(self, args, *kwds) 668 669 def __call__(self, args, *kwds): --> 670 return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds) 671 672 def help(self):

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in function_call(self, function, args, kwds) 589 [s.name() for s in args], 590 ['%s=%s'%(key,value.name()) for key, value in kwds.items()]) --> 591 return self.new(s) 592 593 def _function_call_string(self, function, args, kwds):

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in new(self, code) 358 359 def new(self, code): --> 360 return self(code) 361 362 ###################################################################

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in __call__(self, x, name) 286 287 if isinstance(x, string_types): --> 288 return cls(self, x, name=name) 289 try: 290 # Special methods do not and should not have an option to

/home/moky/.Sage/local/lib/python3.7/site-packages/sage/interfaces/interface.py in __init__(self, parent, value, is_name, name) 708 self._name = parent._create(value, name=name) 709 except (TypeError, RuntimeError, ValueError) as x: --> 710 raise TypeError(x) 711 712 def _latex_(self):

TypeError: ECL says: In function GCD, the value of the first argument is 0.91 which is not of the expected type INTEGER

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2019-10-19 10:13:10 -0600

tmonteil gravatar image

updated 2019-10-19 11:12:36 -0600

It is indeed a bug, thanks for reporting,

That said, you should notice that the symbolic ring does not handle numerical values very well, see for example https://trac.sagemath.org/ticket/14821

So here is a workaround:

sage: f = (x+1)^(1/10)+exp(x)
....: f.taylor(x,0,3)
1171/6000*x^3 + 91/200*x^2 + 11/10*x + 2

Note that it is not Python3 specific, i can reproduce it on a Python2 build.

edit flag offensive delete link more

Comments

Ok in the terminal; I begin to understand. In fact my need is to make the Taylor expansion of an user-defined symbolic expression quite deep inside a script I'm writing.

Something like that I guess:

def my_function(g):
   g = SR(  preparse(str(g)) )
   return g.taylor(x,0,3)

The problem is that str(g) does not return a "correct" string to be re-preparsed.

LaurentClaessens gravatar imageLaurentClaessens ( 2019-10-19 22:42:51 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-10-19 09:22:27 -0600

Seen: 35 times

Last updated: Oct 19