ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 30 Jan 2015 17:49:40 +0100TypeError in creating ODE variablehttps://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/I've just started using Sage for my ODE class in which my professor is using Maple. I don't have any experience using either system, so I'm just trying to figure it out using online tutorials. I don't understand what the problem here is:
![Errors](http://i.imgur.com/pfJTo7A.png)
Can anyone tell me how to fix this?Fri, 30 Jan 2015 08:07:49 +0100https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/Comment by tmonteil for <p>I've just started using Sage for my ODE class in which my professor is using Maple. I don't have any experience using either system, so I'm just trying to figure it out using online tutorials. I don't understand what the problem here is:</p>
<p><img alt="Errors" src="http://i.imgur.com/pfJTo7A.png"/> </p>
<p>Can anyone tell me how to fix this?</p>
https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25643#post-id-25643You should better copy the content of the worksheet in the question, so that it is easier to copy/paste, and so that the information will not disappear when the image will be removed from remote website.Fri, 30 Jan 2015 09:15:36 +0100https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25643#post-id-25643Answer by tmonteil for <p>I've just started using Sage for my ODE class in which my professor is using Maple. I don't have any experience using either system, so I'm just trying to figure it out using online tutorials. I don't understand what the problem here is:</p>
<p><img alt="Errors" src="http://i.imgur.com/pfJTo7A.png"/> </p>
<p>Can anyone tell me how to fix this?</p>
https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?answer=25648#post-id-25648 It seems indeed that you hit a bug:
sage: var('x')
x
sage: function('y', x)
y(x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
TypeError:
When you write `var('x')`, two actions are done : it returns the symbolic variable (or "symbol") `x` and it gives to the Python variable (or 'name') `x` this value. This process of both returning a value and creating a Python variable with the same name is called *variable injection*, and it is a big source of confusion.
When you write `function('y', x)`, it is supposed to be the same : it should return the symbolic function `y(x)` and it give to the Python variable (or 'name') `y` this value.
The problem comes from the fact that `function()` does not inject its result into the Python variable `y` correctly:
sage: z = function('y', x)
sage: y
y
sage: z
y(x)
sage: y == z
False
sage: type(y)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
sage: type(z)
<type 'sage.symbolic.expression.Expression'>
As you can see, `function('y', x)` returned a *symbolic expression* `y(x)`, while the Python variable `y` got some different stuff, more precisely a *function* (with no variable name specifies, somehow similar to `cos` or `sin`).
So, a first workaround is to overwrite the variable injection:
sage: x = var('x')
sage: y = function('y', x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
Another workaround is to consider `y` as a function (whose variable name is not specified), and use the symbolic expression `y(x)` in your equation:
sage: var('x')
sage: function('y')
y
sage: de = x*e^y(x)*diff(y(x),x) == 2*(e^y(x)+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
Thanks for reporting, actually a similar bug was reported as [trac ticket 15025](http://trac.sagemath.org/ticket/15025).
Fri, 30 Jan 2015 11:50:56 +0100https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?answer=25648#post-id-25648Comment by tmonteil for <p>It seems indeed that you hit a bug:</p>
<pre><code>sage: var('x')
x
sage: function('y', x)
y(x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
TypeError:
</code></pre>
<p>When you write <code>var('x')</code>, two actions are done : it returns the symbolic variable (or "symbol") <code>x</code> and it gives to the Python variable (or 'name') <code>x</code> this value. This process of both returning a value and creating a Python variable with the same name is called <em>variable injection</em>, and it is a big source of confusion.</p>
<p>When you write <code>function('y', x)</code>, it is supposed to be the same : it should return the symbolic function <code>y(x)</code> and it give to the Python variable (or 'name') <code>y</code> this value.</p>
<p>The problem comes from the fact that <code>function()</code> does not inject its result into the Python variable <code>y</code> correctly:</p>
<pre><code>sage: z = function('y', x)
sage: y
y
sage: z
y(x)
sage: y == z
False
sage: type(y)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
sage: type(z)
<type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>As you can see, <code>function('y', x)</code> returned a <em>symbolic expression</em> <code>y(x)</code>, while the Python variable <code>y</code> got some different stuff, more precisely a <em>function</em> (with no variable name specifies, somehow similar to <code>cos</code> or <code>sin</code>).</p>
<p>So, a first workaround is to overwrite the variable injection:</p>
<pre><code>sage: x = var('x')
sage: y = function('y', x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
</code></pre>
<p>Another workaround is to consider <code>y</code> as a function (whose variable name is not specified), and use the symbolic expression <code>y(x)</code> in your equation:</p>
<pre><code>sage: var('x')
sage: function('y')
y
sage: de = x*e^y(x)*diff(y(x),x) == 2*(e^y(x)+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
</code></pre>
<p>Thanks for reporting, actually a similar bug was reported as <a href="http://trac.sagemath.org/ticket/15025">trac ticket 15025</a>.</p>
https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25656#post-id-25656It was not a stupid question, you really hit an issue of Sage. It is even possible that `function('y', x)` will be deprecated at some point (so you will have to use the second workaround).Fri, 30 Jan 2015 17:49:40 +0100https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25656#post-id-25656Comment by r.jay.hutchinson for <p>It seems indeed that you hit a bug:</p>
<pre><code>sage: var('x')
x
sage: function('y', x)
y(x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
TypeError:
</code></pre>
<p>When you write <code>var('x')</code>, two actions are done : it returns the symbolic variable (or "symbol") <code>x</code> and it gives to the Python variable (or 'name') <code>x</code> this value. This process of both returning a value and creating a Python variable with the same name is called <em>variable injection</em>, and it is a big source of confusion.</p>
<p>When you write <code>function('y', x)</code>, it is supposed to be the same : it should return the symbolic function <code>y(x)</code> and it give to the Python variable (or 'name') <code>y</code> this value.</p>
<p>The problem comes from the fact that <code>function()</code> does not inject its result into the Python variable <code>y</code> correctly:</p>
<pre><code>sage: z = function('y', x)
sage: y
y
sage: z
y(x)
sage: y == z
False
sage: type(y)
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
sage: type(z)
<type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>As you can see, <code>function('y', x)</code> returned a <em>symbolic expression</em> <code>y(x)</code>, while the Python variable <code>y</code> got some different stuff, more precisely a <em>function</em> (with no variable name specifies, somehow similar to <code>cos</code> or <code>sin</code>).</p>
<p>So, a first workaround is to overwrite the variable injection:</p>
<pre><code>sage: x = var('x')
sage: y = function('y', x)
sage: de = x*e^y*diff(y,x) == 2*(e^y+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
</code></pre>
<p>Another workaround is to consider <code>y</code> as a function (whose variable name is not specified), and use the symbolic expression <code>y(x)</code> in your equation:</p>
<pre><code>sage: var('x')
sage: function('y')
y
sage: de = x*e^y(x)*diff(y(x),x) == 2*(e^y(x)+x^3*e^(2*x))
sage: de
x*e^y(x)*D[0](y)(x) == 2*x^3*e^(2*x) + 2*e^y(x)
</code></pre>
<p>Thanks for reporting, actually a similar bug was reported as <a href="http://trac.sagemath.org/ticket/15025">trac ticket 15025</a>.</p>
https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25654#post-id-25654That makes sense. A lot of this Sage/Python stuff is not particularly intuitive to those of us with a limited CS background. Thanks for responding quickly to what probably seems like a stupid question!Fri, 30 Jan 2015 16:24:45 +0100https://ask.sagemath.org/question/25641/typeerror-in-creating-ode-variable/?comment=25654#post-id-25654