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.Thu, 21 Nov 2013 06:03:38 +0100define symbolic constanthttps://ask.sagemath.org/question/10757/define-symbolic-constant/I need to define some symbolic constants in some expressions, so that the symbolic constant survives differentiation. How can they be defined?Wed, 20 Nov 2013 01:40:12 +0100https://ask.sagemath.org/question/10757/define-symbolic-constant/Answer by tmonteil for <p>I need to define some symbolic constants in some expressions, so that the symbolic constant survives differentiation. How can they be defined?</p>
https://ask.sagemath.org/question/10757/define-symbolic-constant/?answer=15711#post-id-15711I may not understand your question. As long as you declare the variable whith respect to which you differentiate, there should not be any problem, for example:
sage: f(x,y) = x*y
sage: f
(x, y) |--> x*y
sage: f.differentiate(x)
(x, y) |--> y
Here, you differentiate whith respect to `x`, so `y` is considered as a constant. Also:
sage: f(x) = pi*x
sage: f
x |--> pi*x
sage: f.differentiate(x)
x |--> pi
But Sage also protect you from differentiating whith respect to a constant:
sage: f.differentiate(pi)
TypeError: argument symb must be a symbol
Wed, 20 Nov 2013 04:48:59 +0100https://ask.sagemath.org/question/10757/define-symbolic-constant/?answer=15711#post-id-15711Answer by OrbitalMechanic for <p>I need to define some symbolic constants in some expressions, so that the symbolic constant survives differentiation. How can they be defined?</p>
https://ask.sagemath.org/question/10757/define-symbolic-constant/?answer=15714#post-id-15714Permit me to ask this another way. I want to compute the symbolic gradient of the following:
-mu * x / r^3, where mu is a constant and r = sqrt( x^2 + y^2 + z^2 )
In my sage worksheet I have:
variables = var( 'x, y, z, r, f' )
constants = var( 'm' )
r = sqrt( x^2 + y^2 + z^2 )
f = -m * x / r^3
show(f.gradient([x,y,z]))
Sage does not give me the correct answer. My question is how do I get Sage to treat or declare m as a constant in the calculus sense?
Wed, 20 Nov 2013 21:16:31 +0100https://ask.sagemath.org/question/10757/define-symbolic-constant/?answer=15714#post-id-15714Comment by tmonteil for <p>Permit me to ask this another way. I want to compute the symbolic gradient of the following:</p>
<p>-mu * x / r^3, where mu is a constant and r = sqrt( x^2 + y^2 + z^2 )</p>
<p>In my sage worksheet I have:</p>
<p>variables = var( 'x, y, z, r, f' )</p>
<p>constants = var( 'm' )</p>
<p>r = sqrt( x^2 + y^2 + z^2 )</p>
<p>f = -m * x / r^3</p>
<p>show(f.gradient([x,y,z]))</p>
<p>Sage does not give me the correct answer. My question is how do I get Sage to treat or declare m as a constant in the calculus sense?</p>
https://ask.sagemath.org/question/10757/define-symbolic-constant/?comment=16667#post-id-16667When i do this, i get $\left(\frac{3 \, m x^{2}}{{\left(x^{2} + y^{2} + z^{2}\right)}^{\frac{5}{2}}} - \frac{m}{{\left(x^{2} + y^{2} + z^{2}\right)}^{\frac{3}{2}}},\,\frac{3 \, m x y}{{\left(x^{2} + y^{2} + z^{2}\right)}^{\frac{5}{2}}},\,\frac{3 \, m x z}{{\left(x^{2} + y^{2} + z^{2}\right)}^{\frac{5}{2}}}\right)$
I did the computation by hand and it seems to be the correct result. If you replace `m` by `pi`, you will get the same answer. Which expression did you expect ?Thu, 21 Nov 2013 06:03:38 +0100https://ask.sagemath.org/question/10757/define-symbolic-constant/?comment=16667#post-id-16667