How use substitute method in function definitions?

2 views
Skip to first unread message

cseb...@gmail.com

unread,
Mar 26, 2009, 9:56:31 PM3/26/09
to sage-support
This will bomb because numerical_integral doesn't allow vars for the
limits of integration:
Y(t)=2500+numerical_integral(S(u)-R(u),0,t)

cseb...@gmail.com

unread,
Mar 26, 2009, 9:58:13 PM3/26/09
to sage-support
This will bomb because numerical_integral doesn't allow vars for the
limits of integration:
Y(t)=2500+numerical_integral(S(u)-R(u),0,t)[0]

I tried Y(t)=2500+numerical_integral(S(u)-R(u),0,x).substitute(x=t)[0]

and

Y(t) = ( 2500+numerical_integral(S(u)-R(u),0,x)[0]).substitute(x=t)

but no luck

cs

Robert Bradshaw

unread,
Mar 26, 2009, 10:16:07 PM3/26/09
to sage-s...@googlegroups.com

You can do

def Y(t):
return 2500+numerical_integral(S(u)-R(u),0,t)[0]

but then it won't be a symbolic object. (It will be a Python function.)

- Robert

Jason Grout

unread,
Mar 26, 2009, 10:58:40 PM3/26/09
to sage-s...@googlegroups.com


How come n(integrate(some expression that doesn't integrate, x, 0, 1))
doesn't return the result of numerical_integral(same expression, 0, 1) ?
It seems that would solve the problem, as then you would just call
integrate normally in the above expression, so Y(t) would generally
return an unevaluated integral, and then you would just do n(Y(4)) to
get the numerical approximation given by numerical_integral.

Example:

sage: var('t')
t
sage: assume(t>0)
sage: a=(integrate(sin(cos(x)), 0, t)).subs(t=4)
sage: a
integrate(sin(cos(x)), x, 0, 4)
sage: n(a) # Why doesn't this return the result of numerical_integral?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/home/jason/.sage/temp/littleone/29880/_home_jason__sage_init_sage_0.py
in <module>()

/home/jason/sage/local/lib/python2.5/site-packages/sage/misc/functional.pyc
in numerical_approx(x, prec, digits)
765 prec = int((digits+1) * 3.32192) + 1
766 try:
--> 767 return x.numerical_approx(prec)
768 except AttributeError:
769 from sage.rings.complex_double import
is_ComplexDoubleElement

/home/jason/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc
in numerical_approx(self, prec, digits)
1514 except TypeError:
1515 # try to return a complex result
-> 1516 approx = self._complex_mpfr_field_(ComplexField(prec))
1517
1518 return approx

/home/jason/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc
in _complex_mpfr_field_(self, field)
1748
1749 def _complex_mpfr_field_(self, field):
-> 1750 raise TypeError
1751
1752 def _complex_double_(self, C):

TypeError:
sage: numerical_integral(sin(cos(x)), 0, 4)
(-0.6589735634713888, 2.714926799945442e-14)


Jason

kcrisman

unread,
Mar 27, 2009, 9:04:44 AM3/27/09
to sage-support
> sage: n(a) # Why doesn't this return the result of numerical_integral?
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /home/jason/.sage/temp/littleone/29880/_home_jason__sage_init_sage_0.py
> in <module>()
>
> /home/jason/sage/local/lib/python2.5/site-packages/sage/misc/functional.pyc
> in numerical_approx(x, prec, digits)
>      765             prec = int((digits+1) * 3.32192) + 1
>      766     try:
> --> 767         return x.numerical_approx(prec)
>      768     except AttributeError:
>      769         from sage.rings.complex_double import
> is_ComplexDoubleElement
>
> /home/jason/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.p yc
> in numerical_approx(self, prec, digits)
>     1514         except TypeError:
>     1515             # try to return a complex result
> -> 1516             approx = self._complex_mpfr_field_(ComplexField(prec))

Here's the problem: the only TypeError it recognizes is to try to
evaluate a complex integral, but since sin(cos(x)) doesn't have an
antiderivative Maxima knows about, it tries this. Maybe one
(you? :) ) can implement a catch for the event that the integral does
not completely resolve symbolically (e.g. try sage: integrate(1/
(1+x^8)) versus sage: integrate(1/(1+x^7)), where the first just
returns the question but the second did the only piece of the partial
fraction it could, yet both should probably have numerical_integral
applied somehow when n() is called.)

- kcrisman

cseb...@gmail.com

unread,
Mar 27, 2009, 1:36:37 PM3/27/09
to sage-support


On Mar 26, 7:16 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
> def Y(t):
>      return 2500+numerical_integral(S(u)-R(u),0,t)[0]
>
> but then it won't be a symbolic object. (It will be a Python function.)

Wait. What is the difference between a "symbolic object" and a
"Python function" ?

Not sure whey the "def" way works but not the "Y(t) = ..." way.

cs

Robert Bradshaw

unread,
Mar 27, 2009, 3:33:05 PM3/27/09
to sage-s...@googlegroups.com
On Mar 27, 2009, at 10:36 AM, cseb...@gmail.com wrote:

> On Mar 26, 7:16 pm, Robert Bradshaw <rober...@math.washington.edu>
> wrote:
>> def Y(t):
>> return 2500+numerical_integral(S(u)-R(u),0,t)[0]
>>
>> but then it won't be a symbolic object. (It will be a Python
>> function.)
>
> Wait. What is the difference between a "symbolic object" and a
> "Python function" ?

A symbolic object can be manipulated, reasoned about, and has a bunch
of methods attached.

sage: f(x) = x^3-x
sage: f + 1
x |--> x^3 - x + 1
sage: f.integrate(x)
x |--> x^4/4 - x^2/2
...

A Python function can just be called (pretty much). It's just a chunk
of executable code (for example, could include loops, branching, look
stuff up in a file/online, etc.).

sage: def f(x): x^3-x
....:
sage: f.integrate()
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
AttributeError: 'function' object has no attribute 'integrate'

> Not sure whey the "def" way works but not the "Y(t) = ..." way.

The body of a function is not executed until it is called. In the
latter case, when you call Y(3) the "t" is specialized at 3 and the
numerical integration actually can evaluate at a given point.

- Robert

Chris Seberino

unread,
Mar 27, 2009, 8:58:12 PM3/27/09
to sage-support


On Mar 27, 6:04 am, kcrisman <kcris...@gmail.com> wrote:
> Maybe one
> (you? :) ) can implement a catch...

At first I was interested in this change but now I'm wondering if it
is best the way it is now.

f(x) = .... defines a symbolic object as was previously mentioned. A
symbolic object is for analytical results.

It doesn't seem like it would make sense to add any approximation
relationed functions like n(..) or numerical_integral(..) to a
symbolic object.

On the other hand,....some may argue that it is best to let the Sage
user have the freedom to add anything he/she wants to a "symbolic"
function.

I'm curious what others think.

Chris

Jason Grout

unread,
Mar 27, 2009, 9:20:29 PM3/27/09
to sage-s...@googlegroups.com

We already have plenty of approximation functions attached to the
symbolic functions. I think it's extremely valuable. For example, it's
very handy for plotting things.

sage: sin(1)
sin(1)
sage: sin(1).numerical_approx()
0.841470984807897


In fact, the first thing n() tries is the numerical_approx() method of
the object. I think all we have to do is define an
integral.numerical_approx() function that returns the results of
numerical_integral()

Jason

Jason Grout

unread,
Mar 27, 2009, 9:32:11 PM3/27/09
to sage-s...@googlegroups.com


Um, duh, that's where the TypeError occurs above. Okay, I guess I
meant: one should just fix the numeric_approx function for integral...

Jason

Chris Seberino

unread,
Mar 27, 2009, 11:39:03 PM3/27/09
to sage-support


On Mar 27, 6:20 pm, Jason Grout <jason-s...@creativetrax.com> wrote:
> We already have plenty of approximation functions attached to the
> symbolic functions.

> sage: sin(1).numerical_approx()

Yes. I was unclear. What I meant was it wouldn't make sense to use
approximation function in the *definition* of a symbolic function...

e.g. f(x) = numerical_integral(...)

Do people want that? Or is that easy to do?

Chris
Reply all
Reply to author
Forward
0 new messages