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.Wed, 01 Jan 2020 19:58:49 +0100can you programmatically define a [mathematical] function?https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/I want to take an array of coefficients and turn that into a function, a math function not a python function. for example take
[2, 0, 2, 7]
and turn this into
$$f(x) = 2x^3 + 2x + 7$$
something like
def createSym(coefficients, degree, x):
symbolicEqn = ''
for i in poly:
symbolicEqn += ' + ', (x**deg)*i
deg -= 1
return symbolicEqn
pass
then call my definition in the script like
x = var('x')
coeffArray = [2, 0, 2, 7]
degree = 3
polynomialEqn = createSym(coeffArray, degree, x)
But symbolicEqn is just a string and not an expression. Is there a sage/python way to do this?Wed, 18 Dec 2019 22:33:25 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/Answer by Emmanuel Charpentier for <p>I want to take an array of coefficients and turn that into a function, a math function not a python function. for example take</p>
<pre><code>[2, 0, 2, 7]
</code></pre>
<p>and turn this into </p>
<p>$$f(x) = 2x^3 + 2x + 7$$</p>
<p>something like</p>
<pre><code>def createSym(coefficients, degree, x):
symbolicEqn = ''
for i in poly:
symbolicEqn += ' + ', (x**deg)*i
deg -= 1
return symbolicEqn
pass
</code></pre>
<p>then call my definition in the script like</p>
<pre><code>x = var('x')
coeffArray = [2, 0, 2, 7]
degree = 3
polynomialEqn = createSym(coeffArray, degree, x)
</code></pre>
<p>But symbolicEqn is just a string and not an expression. Is there a sage/python way to do this?</p>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?answer=49074#post-id-49074There are more than one way to do it.
The most basic one is to create a "callable symbolic expression", a. k. a symbolic function. In your example, you may write:
sage: f(x)=2*x^3+2*x+7
This entity is, mathematically, a function:
sage: f
x |--> 2*x^3 + 2*x + 7
It can be differentiated:
sage: f.diff(x)
x |--> 6*x^2 + 2
integrated:
sage: f.integrate(x)
x |--> 1/2*x^4 + x^2 + 7*x
numerically evaluated, plotted, and so on...
This is well explained in the [tutorial](https://doc.sagemath.org/html/en/tutorial/tour_functions.html) (a bit later than the chapter devoted to [(Python) functions...](https://doc.sagemath.org/html/en/tutorial/tour_help.html#functions-indentation-and-counting)).
To do this programactically, you need to be a bit more cautious, but roughly:
def createPoly(coefs, var):
pows=[u for u in range(len(coefs))]
pows.reverse()
poly=sum(map(lambda a,b:a*var**b, coefs, pows))
return poly.function(var)
sage: g=createPoly([1, 2, 3],x)
sage: g
x |--> x^2 + 2*x + 3
There are other ways, allowing to define "smarter" symbolic functions (such as special functions), allowing to special-case differentiation, integration numerical evaluation and others... Their use is a bit more sophisticatred.
I suggest to read *all* the tuttorial, and to complement it by this [excellent free book](http://sagebook.gforge.inria.fr/english.html).
HTH,Wed, 18 Dec 2019 23:01:29 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?answer=49074#post-id-49074Comment by alienfetuseater for <p>There are more than one way to do it.</p>
<p>The most basic one is to create a "callable symbolic expression", a. k. a symbolic function. In your example, you may write:</p>
<pre><code>sage: f(x)=2*x^3+2*x+7
</code></pre>
<p>This entity is, mathematically, a function:</p>
<pre><code>sage: f
x |--> 2*x^3 + 2*x + 7
</code></pre>
<p>It can be differentiated:</p>
<pre><code>sage: f.diff(x)
x |--> 6*x^2 + 2
</code></pre>
<p>integrated:</p>
<pre><code>sage: f.integrate(x)
x |--> 1/2*x^4 + x^2 + 7*x
</code></pre>
<p>numerically evaluated, plotted, and so on...</p>
<p>This is well explained in the <a href="https://doc.sagemath.org/html/en/tutorial/tour_functions.html">tutorial</a> (a bit later than the chapter devoted to <a href="https://doc.sagemath.org/html/en/tutorial/tour_help.html#functions-indentation-and-counting">(Python) functions...</a>).</p>
<p>To do this programactically, you need to be a bit more cautious, but roughly:</p>
<pre><code>def createPoly(coefs, var):
pows=[u for u in range(len(coefs))]
pows.reverse()
poly=sum(map(lambda a,b:a*var**b, coefs, pows))
return poly.function(var)
sage: g=createPoly([1, 2, 3],x)
sage: g
x |--> x^2 + 2*x + 3
</code></pre>
<p>There are other ways, allowing to define "smarter" symbolic functions (such as special functions), allowing to special-case differentiation, integration numerical evaluation and others... Their use is a bit more sophisticatred.</p>
<p>I suggest to read <em>all</em> the tuttorial, and to complement it by this <a href="http://sagebook.gforge.inria.fr/english.html">excellent free book</a>.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49084#post-id-49084thank you for your additional answer, like the one above i did not know you could use the summation function without indices.Thu, 19 Dec 2019 14:36:17 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49084#post-id-49084Answer by rburing for <p>I want to take an array of coefficients and turn that into a function, a math function not a python function. for example take</p>
<pre><code>[2, 0, 2, 7]
</code></pre>
<p>and turn this into </p>
<p>$$f(x) = 2x^3 + 2x + 7$$</p>
<p>something like</p>
<pre><code>def createSym(coefficients, degree, x):
symbolicEqn = ''
for i in poly:
symbolicEqn += ' + ', (x**deg)*i
deg -= 1
return symbolicEqn
pass
</code></pre>
<p>then call my definition in the script like</p>
<pre><code>x = var('x')
coeffArray = [2, 0, 2, 7]
degree = 3
polynomialEqn = createSym(coeffArray, degree, x)
</code></pre>
<p>But symbolicEqn is just a string and not an expression. Is there a sage/python way to do this?</p>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?answer=49073#post-id-49073Sure, you can do something like this:
def poly_from_coeffs(coeffs, x):
return sum(c*x^k for (k,c) in enumerate(reversed(coeffs)))
Example:
sage: poly_from_coeffs([2, 0, 2, 7], x)
2*x^3 + 2*x + 7Wed, 18 Dec 2019 22:48:16 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?answer=49073#post-id-49073Comment by alienfetuseater for <p>Sure, you can do something like this:</p>
<pre><code>def poly_from_coeffs(coeffs, x):
return sum(c*x^k for (k,c) in enumerate(reversed(coeffs)))
</code></pre>
<p>Example:</p>
<pre><code>sage: poly_from_coeffs([2, 0, 2, 7], x)
2*x^3 + 2*x + 7
</code></pre>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49085#post-id-49085when used in a script it looks like:
x = sage.var('x')
polynomial = poly_from_coeffs(solution, x)
print polynomial, '\n'
print polynomial.diff(2), '\n'
i did not realize the summation function could be used without indices. thank you for your answer works very wellThu, 19 Dec 2019 14:38:55 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49085#post-id-49085Comment by Iguananaut for <p>Sure, you can do something like this:</p>
<pre><code>def poly_from_coeffs(coeffs, x):
return sum(c*x^k for (k,c) in enumerate(reversed(coeffs)))
</code></pre>
<p>Example:</p>
<pre><code>sage: poly_from_coeffs([2, 0, 2, 7], x)
2*x^3 + 2*x + 7
</code></pre>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49140#post-id-49140I feel like there ought to be an easy to to create a polynomial from a list of coefficients (and possibly a specified order). Built-in, I mean. But if it exists I can't find it....Mon, 23 Dec 2019 16:55:18 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49140#post-id-49140Comment by alienfetuseater for <p>Sure, you can do something like this:</p>
<pre><code>def poly_from_coeffs(coeffs, x):
return sum(c*x^k for (k,c) in enumerate(reversed(coeffs)))
</code></pre>
<p>Example:</p>
<pre><code>sage: poly_from_coeffs([2, 0, 2, 7], x)
2*x^3 + 2*x + 7
</code></pre>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49148#post-id-49148i agree, theres polyfit from numpy i believe, which does interpolation to find equation of best fit but thats as close as ive foundMon, 23 Dec 2019 19:34:15 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49148#post-id-49148Comment by mwageringel for <p>Sure, you can do something like this:</p>
<pre><code>def poly_from_coeffs(coeffs, x):
return sum(c*x^k for (k,c) in enumerate(reversed(coeffs)))
</code></pre>
<p>Example:</p>
<pre><code>sage: poly_from_coeffs([2, 0, 2, 7], x)
2*x^3 + 2*x + 7
</code></pre>
https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49313#post-id-49313Univariate polynomials can be constructed by `QQ['x']([7,2,0,2])`, but I could not find any documentation for this.Wed, 01 Jan 2020 19:58:49 +0100https://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/?comment=49313#post-id-49313