Ask Your Question
1

Variable 't' not found

asked 2018-04-19 23:06:04 +0100

Romuald_314 gravatar image

Hi there,

I was trying to simply plot some fonctions defined as integrals like this one:

var('t')
plot( integrate(cos(x*t),t,0,sin(a)), x, 0, 10)
# a is an angle such that 0 < a < pi/2 defined beforehand

to do some brief visual checks on what I'm working on. That one works perfectly.

Then, just adding a "/sqrt(1-t^2)" :

var('t')
plot( integrate(cos(x*t)/sqrt(1-t^2),t,0,sin(a)), x, 0, 10)

ruins everything: gives me the error message : "... ValueError: Variable 't' not found"

And in fact I also tried with "/(1-t^2)" instead or even "/(1-t)", same result.

I'm very surprised, and I can't understand that. Thanks for any help.


Here is the full report:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_91.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCd0JykKcGxvdCggaW50ZWdyYXRlKGNvcyh4KnQpL3QsdCwwLHNpbihhKSksIHgsIDAsIDEwKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpW3H_J9/___code___.py", line 4, in <module>
    exec compile(u'plot( integrate(cos(x*t)/t,t,_sage_const_0 ,sin(a)), x, _sage_const_0 , _sage_const_10 )
  File "", line 1, in <module>

  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 554, in wrapper
    return func(*args, **options)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1931, in plot
    G = funcs.plot(*args, **original_opts)
  File "sage/symbolic/expression.pyx", line 11383, in sage.symbolic.expression.Expression.plot (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:63333)
  File "sage/symbolic/expression.pyx", line 11424, in sage.symbolic.expression.Expression._plot_fast_callable (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:63626)
  File "sage/ext/fast_callable.pyx", line 456, in sage.ext.fast_callable.fast_callable (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:4485)
  File "sage/symbolic/expression.pyx", line 11261, in sage.symbolic.expression.Expression._fast_callable_ (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:62248)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1579, in fast_callable
    return FastCallableConverter(ex, etb)()
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 226, in __call__
    return self.composition(ex, operator)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1545, in composition
    return self.etb.call(function, *ex.operands())
  File "sage/ext/fast_callable.pyx", line 734, in sage.ext.fast_callable.ExpressionTreeBuilder.call (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:6980)
  File "sage/ext/fast_callable.pyx", line 609, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:5984)
  File "sage/symbolic/expression.pyx", line 11261, in sage.symbolic.expression.Expression._fast_callable_ (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:62248)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1579, in fast_callable
    return FastCallableConverter(ex, etb)()
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 217, in __call__
    return self.arithmetic(div, div.operator())
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1507, in arithmetic
    return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1507, in <lambda>
    return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
  File "sage/ext/fast_callable.pyx", line 734, in sage.ext.fast_callable.ExpressionTreeBuilder.call (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:6980)
  File "sage/ext/fast_callable.pyx", line 609, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:5984)
  File "sage/symbolic/expression.pyx", line 11261, in sage.symbolic.expression.Expression._fast_callable_ (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:62248)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1579, in fast_callable
    return FastCallableConverter(ex, etb)()
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 226, in __call__
    return self.composition(ex, operator)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1545, in composition
    return self.etb.call(function, *ex.operands())
  File "sage/ext/fast_callable.pyx", line 734, in sage.ext.fast_callable.ExpressionTreeBuilder.call (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:6980)
  File "sage/ext/fast_callable.pyx", line 609, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:5984)
  File "sage/symbolic/expression.pyx", line 11261, in sage.symbolic.expression.Expression._fast_callable_ (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:62248)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1579, in fast_callable
    return FastCallableConverter(ex, etb)()
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 217, in __call__
    return self.arithmetic(div, div.operator())
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1507, in arithmetic
    return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1507, in <lambda>
    return reduce(lambda x,y: self.etb.call(operator, x,y), operands)
  File "sage/ext/fast_callable.pyx", line 734, in sage.ext.fast_callable.ExpressionTreeBuilder.call (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:6980)
  File "sage/ext/fast_callable.pyx", line 609, in sage.ext.fast_callable.ExpressionTreeBuilder.__call__ (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:5984)
  File "sage/symbolic/expression.pyx", line 11261, in sage.symbolic.expression.Expression._fast_callable_ (/usr/lib/sagemath//src/build/cythonized/sage/symbolic/expression.cpp:62248)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1579, in fast_callable
    return FastCallableConverter(ex, etb)()
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 212, in __call__
    return self.symbol(ex)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.py", line 1528, in symbol
    return self.etb.var(SR(ex))
  File "sage/ext/fast_callable.pyx", line 681, in sage.ext.fast_callable.ExpressionTreeBuilder.var (/usr/lib/sagemath//src/build/cythonized/sage/ext/fast_callable.c:6561)
ValueError: Variable 't' not found
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-04-20 07:50:32 +0100

slelievre gravatar image

The range of x values where you want the plot should look like (x, 0, 10) instead of x, 0, 10.

The following works for me:

sage: a = 0.5
sage: x, t = SR.var('x t')
sage: plot(integrate(cos(x*t), t, 0, sin(a)), (x, 0, 10))

Or you can use a Python function rather than a Sage symbolic function, and describe the range as (0, 10).

sage: plot(lambda x: integrate(cos(x*t), t, 0, sin(a)), (0, 10))

The syntax lambda x: expression involving x is the Python equivalent of the mathematical notation $x \mapsto \text{expression involving } x$.

edit flag offensive delete link more

Comments

It seems to work as for the variable... but for some reason, this is extremely slow. For instance I could not get any result after 5 minutes of ongoing computing (I deliberately stopped it) for

x, t = SR.var('x t')
a = 0.5
plot(lambda x: integrate(cos(x*t)/sqrt(1-t), t, 0, sin(a)), (0, 10))

Anyway, I guess you solved my problem, this is probably another matter. Unless you have suggestions about it?

Romuald_314 gravatar imageRomuald_314 ( 2018-04-20 08:36:23 +0100 )edit

For me it takes a couple of seconds on a 2014 computer with Sage 8.2.rc2 compiled from source.

sage: version()
'SageMath version 8.2.rc2, Release Date: 2018-04-10'
sage: a = 0.5
sage: x, t = SR.var('x t')
sage: %time plot(integrate(cos(x*t), t, 0, sin(a)), (x, 0, 10))
CPU times: user 1.99 s, sys: 236 ms, total: 2.22 s
Wall time: 2.19 s
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: %time plot(lambda x: integrate(cos(x*t), t, 0, sin(a)), (0, 10))
CPU times: user 2.71 s, sys: 52.5 ms, total: 2.76 s
Wall time: 2.35 s
Launched png viewer for Graphics object consisting of 1 graphics primitive

What version of Sage are you using? What operating system? How did you install Sage? Is it a very old computer?

slelievre gravatar imageslelievre ( 2018-04-20 11:16:08 +0100 )edit
1

Sorry, the timing I gave was with the function in your original question, where a primitive is easy to compute.

The function in your follow-up question is a bit more complicated, and looking for an exact form of the integral is what is taking time.

In such a case, using numerical integration is probably a better idea. (It might make sense for the original function too, if speed matters).

sage: f = lambda x: numerical_integral(cos(x*t)/sqrt(1-t), 0, sin(a))[0]
sage: %time plot(f, (0, 10))
CPU times: user 207 ms, sys: 20.2 ms, total: 228 ms
Wall time: 213 ms
Launched png viewer for Graphics object consisting of 1 graphics primitive
slelievre gravatar imageslelievre ( 2018-04-20 11:25:41 +0100 )edit

Yes... Oh great! I had never thought of using this numerical integration function - I didn't know about it and I tried to define mine. You've just let me know about this latter.

This integral is given to be impossible to express in terms of standard mathematical functions by Wolfram Alpha.

I was still trying to integrate with the classic sage integrate function because I thought sage automatically switched to numerical methods; however, I've already had some surprises when evaluating integrals of absolute values functions - thus, positive - and it gave me some negative results!

By the way, I'm running sage 7.5.1 on a 2014-computer or so (acer V3 771G) on Xubuntu.

And you definitely gave a solution to my problem - as I said, my point is only to get fast verification on what I'm proving.

Romuald_314 gravatar imageRomuald_314 ( 2018-04-20 12:24:56 +0100 )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: 2018-04-19 23:06:04 +0100

Seen: 1,161 times

Last updated: Apr 20 '18