Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

can you programmatically define a [mathematical] function?

asked 5 years ago

alienfetuseater gravatar image

updated 5 years ago

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)=2x3+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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 5 years ago

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
Preview: (hide)
link

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 ( 5 years ago )
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 ( 5 years ago )

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 ( 5 years ago )

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

mwageringel gravatar imagemwageringel ( 5 years ago )
2

answered 5 years ago

Emmanuel Charpentier gravatar image

updated 5 years ago

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,

Preview: (hide)
link

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 ( 5 years ago )

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: 5 years ago

Seen: 1,063 times

Last updated: Dec 18 '19