ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 06 Sep 2015 17:31:53 -0500Problems in solving eigenvalue equations with differential operatorshttps://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/Let's say we have a linear differential operator $\hat A = \sum_k a_k \frac {d^k} {dx^k}$ and we want to solve the eigenvalue equation $\hat A f(x) = a f(x)$ which is an ODE that we put in sage and outputs a solution $g(x)$. We now want to substitute $f(x)$ with $g(x)$ and simplify our expression in order to extract the eigenvalues of $\hat A$. The problem here is that I am not able to substitute the derivatives with my solution neither via `eqn.substitute_expression(f(x) == g(x))` nor via `eqn.substitute_function(f(x),g(x))` because `D[0]f(x)`, ... `D[0,0,...,0]f(x)` remain unchanged.
Thu, 03 Sep 2015 16:50:54 -0500https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/Answer by nbruin for <p>Let's say we have a linear differential operator $\hat A = \sum_k a_k \frac {d^k} {dx^k}$ and we want to solve the eigenvalue equation $\hat A f(x) = a f(x)$ which is an ODE that we put in sage and outputs a solution $g(x)$. We now want to substitute $f(x)$ with $g(x)$ and simplify our expression in order to extract the eigenvalues of $\hat A$. The problem here is that I am not able to substitute the derivatives with my solution neither via <code>eqn.substitute_expression(f(x) == g(x))</code> nor via <code>eqn.substitute_function(f(x),g(x))</code> because <code>D[0]f(x)</code>, ... <code>D[0,0,...,0]f(x)</code> remain unchanged.</p>
https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?answer=29400#post-id-29400It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:
sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D[0](g)(x) + c*D[0, 0](g)(x)
f gets beautifully replaced by g. It seems from your question that you might be trying
sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions.
**edit:** Thank you for clarifying (editing your question would have been better than posting the additional information as an answer)
You're a victim of erroneous documentation. The command `f=function('f',x)` doesn't give you a function, it gives you an expression "function f evaluated at x". This is misused in a lot of places, particularly the DE code. It means that the argument that you give to `substitute_function` isn't a function. Try and print it:
sage: f=function('f',x) # This doesn't do what you might think it does. Don't use it.
sage: f
f(x)
sage: f(x)
DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
f(x)
The DeprecationWarning is already an indication that typing in `f(x)` isn't what you'd expect it to be.
If instead you do the following, things works as you'd expect.
sage: var('x,a,b')
(x, a, b)
sage: function('f')(x)
f(x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f(x),x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f(x), ivar=x)
sage: eqn.substitute_function(f,g)
b*cos(sqrt(-a*b)*x/a)*f(0) - sqrt(-a*b)*sin(sqrt(-a*b)*x/a)*D[0](f)(0) == (a^2*sin(sqrt(-a*b)*x/a)*D[0](f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a
You just did what is in the documentation of `desolve_laplace`, but as you found out, the code there doesn't quite do what it claims, so that `substitute_function` doesn't work as expected. You're not the first one to fall in this trap. There are some tickets about it, see http://trac.sagemath.org/ticket/17447 for instance.Fri, 04 Sep 2015 02:55:17 -0500https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?answer=29400#post-id-29400Comment by papachristoumarios for <p>It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:</p>
<pre><code>sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D[0](g)(x) + c*D[0, 0](g)(x)
</code></pre>
<p>f gets beautifully replaced by g. It seems from your question that you might be trying</p>
<pre><code>sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
</code></pre>
<p>which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions. </p>
<p><strong>edit:</strong> Thank you for clarifying (editing your question would have been better than posting the additional information as an answer)</p>
<p>You're a victim of erroneous documentation. The command <code>f=function('f',x)</code> doesn't give you a function, it gives you an expression "function f evaluated at x". This is misused in a lot of places, particularly the DE code. It means that the argument that you give to <code>substitute_function</code> isn't a function. Try and print it:</p>
<pre><code>sage: f=function('f',x) # This doesn't do what you might think it does. Don't use it.
sage: f
f(x)
sage: f(x)
DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
f(x)
</code></pre>
<p>The DeprecationWarning is already an indication that typing in <code>f(x)</code> isn't what you'd expect it to be.</p>
<p>If instead you do the following, things works as you'd expect.</p>
<pre><code>sage: var('x,a,b')
(x, a, b)
sage: function('f')(x)
f(x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f(x),x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f(x), ivar=x)
sage: eqn.substitute_function(f,g)
b*cos(sqrt(-a*b)*x/a)*f(0) - sqrt(-a*b)*sin(sqrt(-a*b)*x/a)*D[0](f)(0) == (a^2*sin(sqrt(-a*b)*x/a)*D[0](f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a
</code></pre>
<p>You just did what is in the documentation of <code>desolve_laplace</code>, but as you found out, the code there doesn't quite do what it claims, so that <code>substitute_function</code>doesn't work as expected. You're not the first one to fall in this trap. There are some tickets about it, see <a href="http://trac.sagemath.org/ticket/17447">http://trac.sagemath.org/ticket/17447</a> for instance.</p>
https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?comment=29411#post-id-29411@nbruin Thank you very much!Sun, 06 Sep 2015 17:31:53 -0500https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?comment=29411#post-id-29411Comment by papachristoumarios for <p>It would help if you'd post the code you are having problems with. I don't experience the problem you seem to run into:</p>
<pre><code>sage: var('a,b,c')
(a, b, c)
sage: function('f')
f
sage: function('g')
g
sage: E=a*f(x)+b*diff(f(x),x)+c*diff(f(x),x,x)
sage: E
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
sage: E.substitute_function(f,g)
a*g(x) + b*D[0](g)(x) + c*D[0, 0](g)(x)
</code></pre>
<p>f gets beautifully replaced by g. It seems from your question that you might be trying</p>
<pre><code>sage: E.substitute_function(f(x),g(x))
a*f(x) + b*D[0](f)(x) + c*D[0, 0](f)(x)
</code></pre>
<p>which is not according to the documentation of substitute_function and indeed doesn't work: the arguments should be functions, not expressions. </p>
<p><strong>edit:</strong> Thank you for clarifying (editing your question would have been better than posting the additional information as an answer)</p>
<p>You're a victim of erroneous documentation. The command <code>f=function('f',x)</code> doesn't give you a function, it gives you an expression "function f evaluated at x". This is misused in a lot of places, particularly the DE code. It means that the argument that you give to <code>substitute_function</code> isn't a function. Try and print it:</p>
<pre><code>sage: f=function('f',x) # This doesn't do what you might think it does. Don't use it.
sage: f
f(x)
sage: f(x)
DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
f(x)
</code></pre>
<p>The DeprecationWarning is already an indication that typing in <code>f(x)</code> isn't what you'd expect it to be.</p>
<p>If instead you do the following, things works as you'd expect.</p>
<pre><code>sage: var('x,a,b')
(x, a, b)
sage: function('f')(x)
f(x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f(x),x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f(x), ivar=x)
sage: eqn.substitute_function(f,g)
b*cos(sqrt(-a*b)*x/a)*f(0) - sqrt(-a*b)*sin(sqrt(-a*b)*x/a)*D[0](f)(0) == (a^2*sin(sqrt(-a*b)*x/a)*D[0](f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a
</code></pre>
<p>You just did what is in the documentation of <code>desolve_laplace</code>, but as you found out, the code there doesn't quite do what it claims, so that <code>substitute_function</code>doesn't work as expected. You're not the first one to fall in this trap. There are some tickets about it, see <a href="http://trac.sagemath.org/ticket/17447">http://trac.sagemath.org/ticket/17447</a> for instance.</p>
https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?comment=29410#post-id-29410@nbruin. See my simplified example (I have posted it as another answer)Sun, 06 Sep 2015 03:13:10 -0500https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?comment=29410#post-id-29410Answer by papachristoumarios for <p>Let's say we have a linear differential operator $\hat A = \sum_k a_k \frac {d^k} {dx^k}$ and we want to solve the eigenvalue equation $\hat A f(x) = a f(x)$ which is an ODE that we put in sage and outputs a solution $g(x)$. We now want to substitute $f(x)$ with $g(x)$ and simplify our expression in order to extract the eigenvalues of $\hat A$. The problem here is that I am not able to substitute the derivatives with my solution neither via <code>eqn.substitute_expression(f(x) == g(x))</code> nor via <code>eqn.substitute_function(f(x),g(x))</code> because <code>D[0]f(x)</code>, ... <code>D[0,0,...,0]f(x)</code> remain unchanged.</p>
https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?answer=29409#post-id-29409Take for example that $\hat A = a \frac{d^2}{dx^2}, a>0$ which leads to the eigenvalue equation $$\hat A f(x) = b f(x) \iff af''(x) = bf(x)$$ (I assume for simplicity that $b<0$). Using `desolve_laplace` I define `g(x)` as the solution to the equation. Then I am not able to substitute the solution to my original equation
Code:
sage: x,a,b = var('x,a,b')
sage: f = function('f',x)
sage: g = function('g',x)
sage: assume(a>0); assume(b<0)
sage: eqn = a*diff(f,x,2) == b*f(x)
sage: g(x) = desolve_laplace(eqn, f, ivar=x)
sage: g
x |--> (a^2*sin(sqrt(-a*b)*x/a)*D[0](f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))/a
sage: eqn.substitute_function(f,g)
a*D[0, 0](f)(x) == b*f(x)
sage: eqn.substitute_expression(f(x) == g(x))
a*D[0, 0](f)(x) == (a^2*sin(sqrt(-a*b)*x/a)*D[0](f)(0)/sqrt(-a*b) + a*cos(sqrt(-a*b)*x/a)*f(0))*b/a
Am I clear?
Sun, 06 Sep 2015 03:12:07 -0500https://ask.sagemath.org/question/29398/problems-in-solving-eigenvalue-equations-with-differential-operators/?answer=29409#post-id-29409