Ask Your Question

LaurentClaessens's profile - activity

2023-05-19 14:35:34 +0200 received badge  Notable Question (source)
2019-10-20 05:42:51 +0200 commented answer Taylor expansion crashing (python3)

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.

2019-10-19 17:23:05 +0200 received badge  Editor (source)
2019-10-19 16:22:27 +0200 asked a question Taylor expansion crashing (python3)

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

2019-09-29 19:44:33 +0200 received badge  Popular Question (source)
2017-06-16 16:59:03 +0200 commented answer Undeterministic numerical approximation

Thanks for the answer. Since I am only interested in the firsts decimals (these are coordinates of of points on a A4 paper, in centimeters), I didn't though to increase the precision.

I'l have to tests that.

2017-06-15 08:16:06 +0200 received badge  Student (source)
2017-06-15 04:32:32 +0200 asked a question Undeterministic numerical approximation

Let the following expression of x :

f=1/152444172305856930250752000000*x^28 + 1/10888869450418352160768000000*x^27 + 1/15511210043330985984000000*x^25 + 1/310224200866619719680000*x^24 + 1/25852016738884976640000*x^23 + 1/51090942171709440000*x^21 + 1/1216451004088320000*x^20 + 1/121645100408832000*x^19 + 1/355687428096000*x^17 + 1/10461394944000*x^16 + 1/1307674368000*x^15 + 1/6227020800*x^13 + 1/239500800*x^12 + 1/39916800*x^11 + 1/362880*x^9 + 1/20160*x^8 + 1/5040*x^7 + 1/120*x^5 + 1/12*x^4 + 1/6*x^3 + x - cos(x) + 2 -exp(x)

I noticed that evaluating that for the value x=numerical_approximation(10) leads to non-deterministic results (from the 12th decimal).

I guess that this is a "cancellation error" : the last term (-exp(x)) and the whole stuff before are both of the order of magnitude 22026.

I'm only interested in the first 3 or 4 decimals.

  • QUESTION 1 How can I "force" sage to provide deterministic results ? Doing numerical_approx(...,prec=30) makes the job, but the "30" is quite heuristic; I'm not sure that this is a good approach.

  • QUESTION 2 (not really sage-related) How should I unit-test a function that has undeterministic bahaviour like that ?