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.Tue, 31 Aug 2021 19:37:28 +0200how to define a constant in sage?https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/Hello, I just started using sage. I am having some difficulties in declaring constants. I am trying to calculate derivatives of functions but I don't want to put values for the constant but rather define constants in the function. Somehow I am not able to find any literature on that.
I would be really grateful if someone can help me with this issue.
Cheers,
SanyogMon, 19 Mar 2012 17:51:49 +0100https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/Answer by ny2292000 for <p>Hello, I just started using sage. I am having some difficulties in declaring constants. I am trying to calculate derivatives of functions but I don't want to put values for the constant but rather define constants in the function. Somehow I am not able to find any literature on that.</p>
<p>I would be really grateful if someone can help me with this issue.</p>
<p>Cheers,</p>
<p>Sanyog</p>
https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?answer=58769#post-id-58769This is a follow up. This is my code:
var("theta ee")
u=cos(theta)+ee
diff(u(theta),theta)
this is the output:
-sin(theta) + 1
I don't understand what is going on. Nowhere was ee equated to theta.
What is happening here?Tue, 31 Aug 2021 16:41:17 +0200https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?answer=58769#post-id-58769Comment by John Palmieri for <p>This is a follow up. This is my code:</p>
<pre><code>var("theta ee")
u=cos(theta)+ee
diff(u(theta),theta)
</code></pre>
<p>this is the output:</p>
<pre><code>-sin(theta) + 1
</code></pre>
<p>I don't understand what is going on. Nowhere was ee equated to theta.</p>
<p>What is happening here?</p>
https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=58774#post-id-58774With my version of Sage, `u(theta)` also produces a warning about variable substitution. If you instead do `u(theta=theta)`, you will get what you expect, and the derivative will be what you expect.Tue, 31 Aug 2021 19:37:28 +0200https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=58774#post-id-58774Comment by John Palmieri for <p>This is a follow up. This is my code:</p>
<pre><code>var("theta ee")
u=cos(theta)+ee
diff(u(theta),theta)
</code></pre>
<p>this is the output:</p>
<pre><code>-sin(theta) + 1
</code></pre>
<p>I don't understand what is going on. Nowhere was ee equated to theta.</p>
<p>What is happening here?</p>
https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=58773#post-id-58773You should really ask a separate question rather than posting a follow-up to a 9-year old question. If you evaluate `u(theta)`, you will see that it is substituting `theta` in for `ee`: `u` is an expression involving two variables, and it substitutes the first one for `ee`, the second for `theta`. So `u(1, 2)` will give `cos(2) + 1)`. Anyway, `u(theta)` evaluates to `cos(theta) + theta`, and then you're differentiating that.Tue, 31 Aug 2021 19:36:29 +0200https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=58773#post-id-58773Answer by John Palmieri for <p>Hello, I just started using sage. I am having some difficulties in declaring constants. I am trying to calculate derivatives of functions but I don't want to put values for the constant but rather define constants in the function. Somehow I am not able to find any literature on that.</p>
<p>I would be really grateful if someone can help me with this issue.</p>
<p>Cheers,</p>
<p>Sanyog</p>
https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?answer=13378#post-id-13378The code
sage: var('x y z')
(x, y, z)
sage: f = 3*x*y + 2*z + 12
sage: f.diff(x)
3*y
sage: g(x,y,z) = 3*x*y + 2*z + 12
sage: g.diff(x)
(x, y, z) |--> 3*y
provides two slightly different ways of defining a function involving x, y, and z, then differentiating with respect to x, thus treating y and z as constants. Is this what you want?
Mon, 19 Mar 2012 21:35:55 +0100https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?answer=13378#post-id-13378Comment by kcrisman for <p>The code</p>
<pre><code>sage: var('x y z')
(x, y, z)
sage: f = 3*x*y + 2*z + 12
sage: f.diff(x)
3*y
sage: g(x,y,z) = 3*x*y + 2*z + 12
sage: g.diff(x)
(x, y, z) |--> 3*y
</code></pre>
<p>provides two slightly different ways of defining a function involving x, y, and z, then differentiating with respect to x, thus treating y and z as constants. Is this what you want?</p>
https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=20104#post-id-20104To follow up, I think that John means just doing `var('a')` will make `a` a "symbolic constant" in the sense you desire.Mon, 19 Mar 2012 23:27:41 +0100https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/?comment=20104#post-id-20104