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.Tue, 27 Mar 2018 11:06:28 -0500Lazy evaluation of derivatives of an unknown functionhttps://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/Hi,
I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?
Here is what I tried so far:
var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.
Many thanks!Mon, 10 Jun 2013 05:27:40 -0500https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/Comment by rickhg12hs for <p>Hi,</p>
<p>I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?</p>
<p>Here is what I tried so far:</p>
<pre><code>var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
</code></pre>
<p>I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?comment=17503#post-id-17503Could you provide a specific example? Why not just plug in the putative solutions to original DE to check them?Thu, 13 Jun 2013 20:21:31 -0500https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?comment=17503#post-id-17503Comment by nosneros for <p>Hi,</p>
<p>I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?</p>
<p>Here is what I tried so far:</p>
<pre><code>var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
</code></pre>
<p>I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?comment=17498#post-id-17498That is what I'm doing for now, but I was thinking it would be convenient to be able to construct the system of pdes with an arbitrary unknown function and then substitute in the trial solutions to check them. I will play with it a bit more and see if I can come up with a better example use case. Thanks!Fri, 14 Jun 2013 10:45:42 -0500https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?comment=17498#post-id-17498Answer by nbruin for <p>Hi,</p>
<p>I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?</p>
<p>Here is what I tried so far:</p>
<pre><code>var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
</code></pre>
<p>I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?answer=41794#post-id-41794The original question tried to replicate the syntax in which sage prints derivatives. This comes up more often. Isn't it time to include (perhaps not in the global namespace, but at least somewhere from which it is easily imported) an object that allows users to do so? It's valid python syntax so it's fairly easy to do. It does require boilerplate that's a bit beyond a one-liner, though:
from sage.symbolic.operators import FDerivativeOperator
class Dclass(object):
def __init__(self,L=None):
if L is None:
self.L=[]
else:
self.L=L
def __getitem__(self,index):
if isinstance(index,tuple):
index=list(index)
elif not(isinstance(index,list)):
index=[index]
return Dclass(self.L+index)
def __call__(self,arg):
return FDerivativeOperator(arg,self.L)
def __repr__(self):
return "D"+str(self.L)
D=Dclass()
With this definition you can just write:
sage: function('f'); var('x','y','z')
sage: D[0,1,2](f)(x,y,z)
diff(f(x, y, z), x, y, z)
This is now https://trac.sagemath.org/ticket/25054Tue, 27 Mar 2018 11:06:28 -0500https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?answer=41794#post-id-41794Answer by slelievre for <p>Hi,</p>
<p>I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?</p>
<p>Here is what I tried so far:</p>
<pre><code>var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
</code></pre>
<p>I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.</p>
<p>Many thanks!</p>
https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?answer=41789#post-id-41789The method `substitute_function` allows to substitute a function with another.
This allows to check if a particular function solves a particular partial differential equation.
For example:
sage: x, y = SR.var('x y')
sage: f = function('f')
sage: eq = diff(f(x, y), x, x) + diff(f(x, y), y, y) == 0
sage: eq
diff(f(x, y), x, x) + diff(f(x, y), y, y) == 0
sage: g(x, y) = x*y - 3*x - 2*y + 7
sage: g
(x, y) |--> x*y - 3*x - 2*y + 7
sage: eq.substitute_function(f, g)
0 == 0
sage: bool(eq.substitute_function(f, g))
True
Tue, 27 Mar 2018 04:30:44 -0500https://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/?answer=41789#post-id-41789