Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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,

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,