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, 10 Nov 2011 08:16:59 +0100How to make special functions/orthogonal polynomials as callable symbolic expression.https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/Hello,
I'd like to make some special functions/orthogonal polynomials as callable symbolic
expression. However, those functions always remind me the argument is not an integer.
var('n a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
, and
var('n x')
g(x) = spherical_bessel_J(n, x)
TypeError: unable to convert x (=n) to an integer
Even if I tried the "domain" keyword, there's still the same problem:
var('n', domain=ZZ)
var('a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
How do I reassure those functions that I will give integers to n later in each calculation?Sun, 06 Nov 2011 03:18:21 +0100https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/Answer by kcrisman for <p>Hello,</p>
<p>I'd like to make some special functions/orthogonal polynomials as callable symbolic
expression. However, those functions always remind me the argument is not an integer.</p>
<pre><code>var('n a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>, and </p>
<pre><code>var('n x')
g(x) = spherical_bessel_J(n, x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>Even if I tried the "domain" keyword, there's still the same problem:</p>
<pre><code>var('n', domain=ZZ)
var('a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>How do I reassure those functions that I will give integers to n later in each calculation?</p>
https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12865#post-id-12865Another thing you can do is to initialize a symbolic function with its derivative etc. See [Ticket #11143](http://trac.sagemath.org/sage_trac/ticket/11143) for a lot of discussion of how to do this. Of course, we'd love to have you contribute that to Sage in that case so that others can use it!Mon, 07 Nov 2011 10:49:09 +0100https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12865#post-id-12865Answer by burcin for <p>Hello,</p>
<p>I'd like to make some special functions/orthogonal polynomials as callable symbolic
expression. However, those functions always remind me the argument is not an integer.</p>
<pre><code>var('n a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>, and </p>
<pre><code>var('n x')
g(x) = spherical_bessel_J(n, x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>Even if I tried the "domain" keyword, there's still the same problem:</p>
<pre><code>var('n', domain=ZZ)
var('a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>How do I reassure those functions that I will give integers to n later in each calculation?</p>
https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12874#post-id-12874There is a patch attached to [#9706](http://trac.sagemath.org/sage_trac/ticket/9706) which fixes this. It's quite old, so it might need some manual care. It would be great if someone started working on that ticket again. I don't think Stefan has time any more.Thu, 10 Nov 2011 08:16:59 +0100https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12874#post-id-12874Answer by parzan for <p>Hello,</p>
<p>I'd like to make some special functions/orthogonal polynomials as callable symbolic
expression. However, those functions always remind me the argument is not an integer.</p>
<pre><code>var('n a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>, and </p>
<pre><code>var('n x')
g(x) = spherical_bessel_J(n, x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>Even if I tried the "domain" keyword, there's still the same problem:</p>
<pre><code>var('n', domain=ZZ)
var('a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
</code></pre>
<p>How do I reassure those functions that I will give integers to n later in each calculation?</p>
https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12861#post-id-12861`gen_laguerre` wraps maxima's function with the same name. In maxima, if you enter `gen_laguerre(5,6,x)`, for example, you get a polynomial in `x`, and this is what the sage function is intended for.
If you enter in maxima `gen_laguerre(n,a,x)` it will accept it and spit `gen_laguerre(n,a,x)` back at you, and will later know how to differentiate it with respect to `x`, for example, but this capability is currently not wrapped in sage. You can work directly with maxima objects:
sage: f = maxima('gen_laguerre(n,a,x)')
sage: f.diff(x)
(n*gen_laguerre(n,a,x)-(n+a)*gen_laguerre(n-1,a,x)*unit_step(n))/x
but if you only intend to evaluate your function later, and not use symbolic manipulations (such as `diff`), you can create a python function instead of a symbolic object:
sage: f = lambda n,a,x:gen_laguerre(n,a,x)
sage: f(3,4,5)
-10/3
BTW, this is the code of `gen_laguerre`:
sage_eval(maxima.eval('gen_laguerre(%s,%s,x)'%(ZZ(n),a)), locals={'x':x})
so you see that `n` is evaluated to an integer. If you do
maxima.eval('gen_laguerre(n,a,x)')
instead you will just get a string saying `gen_laguerre(n,a,x)` which is not very useful.
Sun, 06 Nov 2011 13:48:05 +0100https://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/?answer=12861#post-id-12861