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, 23 Mar 2012 18:53:03 +0100Is there any way to define an as-yet-unknown function?https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/I'd like to know if there's a way of declaring functions in sage that are as-yet unknown. For example, let's say I have a function
p = R*T/v - a(T)/v/(v+b)
And I would like to be able to take a derivative like this
deriv(p,T)
and be given something back a partial derivative something like
R/v - diff(a(T),T)/v/(v+b)
However at present I can't seem to put an abstract function `a(T)` into my expression or find anything in the documentation that says how this is done.
As I recall there was a way to do this with wxMaxima, so maybe I just haven't found the trick in Sage yet.
Fri, 23 Mar 2012 14:11:38 +0100https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/Answer by Shashank for <p>I'd like to know if there's a way of declaring functions in sage that are as-yet unknown. For example, let's say I have a function</p>
<pre><code>p = R*T/v - a(T)/v/(v+b)
</code></pre>
<p>And I would like to be able to take a derivative like this</p>
<pre><code>deriv(p,T)
</code></pre>
<p>and be given something back a partial derivative something like</p>
<pre><code>R/v - diff(a(T),T)/v/(v+b)
</code></pre>
<p>However at present I can't seem to put an abstract function <code>a(T)</code> into my expression or find anything in the documentation that says how this is done.</p>
<p>As I recall there was a way to do this with wxMaxima, so maybe I just haven't found the trick in Sage yet.</p>
https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/?answer=12078#post-id-12078Answer from 2012. See further down for 2020 update.
You can define a function using the keyword `function`.
The script below gives the answer you expect.
T = var('T')
a = function('a', T)
R = var('R')
v = var('v')
b = var('b')
p = R*T/v - a(T)/v/(v+b)
diff(p, T)
----
[2020 update] Nowadays, `function` no longer
takes the variable as an argument.
Use `a = function('a')` instead of `a = function('a', T)`.
Example:
sage: version()
'SageMath version 9.0, Release Date: 2020-01-01'
sage: b, v, R, T = SR.var('b v R T')
sage: a = function('a')
sage: p = R*T/v - a(T)/v/(v+b)
sage: diff(p, T)
R/v - diff(a(T), T)/((b + v)*v)Fri, 23 Mar 2012 14:17:30 +0100https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/?answer=12078#post-id-12078Comment by jdpipe for <p>Answer from 2012. See further down for 2020 update.</p>
<p>You can define a function using the keyword <code>function</code>.
The script below gives the answer you expect.</p>
<pre><code>T = var('T')
a = function('a', T)
R = var('R')
v = var('v')
b = var('b')
p = R*T/v - a(T)/v/(v+b)
diff(p, T)
</code></pre>
<hr>
<p>[2020 update] Nowadays, <code>function</code> no longer
takes the variable as an argument.</p>
<p>Use <code>a = function('a')</code> instead of <code>a = function('a', T)</code>.</p>
<p>Example:</p>
<pre><code>sage: version()
'SageMath version 9.0, Release Date: 2020-01-01'
sage: b, v, R, T = SR.var('b v R T')
sage: a = function('a')
sage: p = R*T/v - a(T)/v/(v+b)
sage: diff(p, T)
R/v - diff(a(T), T)/((b + v)*v)
</code></pre>
https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/?comment=20064#post-id-20064Great. seems to work fine. The output is a bit weird though, in typeset sage, the diff(a(T),T) is written as D [ 0 ] ( a ) ( T ), not particular intuitive. But it works! Thank you.Fri, 23 Mar 2012 18:53:03 +0100https://ask.sagemath.org/question/8822/is-there-any-way-to-define-an-as-yet-unknown-function/?comment=20064#post-id-20064