# 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?

edit retag close merge delete

Sort by » oldest newest most voted

Sure, 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 + 7

more

when 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 well

( 2019-12-19 14:38:55 +0200 )edit
2

I 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....

( 2019-12-23 16:55:18 +0200 )edit

i agree, theres polyfit from numpy i believe, which does interpolation to find equation of best fit but thats as close as ive found

( 2019-12-23 19:34:15 +0200 )edit

Univariate polynomials can be constructed by QQ['x']([7,2,0,2]), but I could not find any documentation for this.

( 2020-01-01 19:58:49 +0200 )edit

There 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 (a bit later than the chapter devoted to (Python) functions...).

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.

HTH,

more

thank you for your additional answer, like the one above i did not know you could use the summation function without indices.

( 2019-12-19 14:36:17 +0200 )edit