Ask Your Question
0

can you programmatically define a [mathematical] function?

asked 2019-12-18 22:33:25 +0100

alienfetuseater gravatar image

updated 2019-12-18 22:42:25 +0100

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-12-18 22:48:16 +0100

rburing gravatar image

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
edit flag offensive delete link more

Comments

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

alienfetuseater gravatar imagealienfetuseater ( 2019-12-19 14:38:55 +0100 )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....

Iguananaut gravatar imageIguananaut ( 2019-12-23 16:55:18 +0100 )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

alienfetuseater gravatar imagealienfetuseater ( 2019-12-23 19:34:15 +0100 )edit

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

mwageringel gravatar imagemwageringel ( 2020-01-01 19:58:49 +0100 )edit
2

answered 2019-12-18 23:01:29 +0100

Emmanuel Charpentier gravatar image

updated 2019-12-18 23:23:21 +0100

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,

edit flag offensive delete link more

Comments

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

alienfetuseater gravatar imagealienfetuseater ( 2019-12-19 14:36:17 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-12-18 22:33:25 +0100

Seen: 944 times

Last updated: Dec 18 '19