Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – defining callable symbolic expressions


Let's say we want to define this mathematical function and perform some calculations with it:

Evaluate the following code to define the function:

var('a, x')
f(x) = a * x^3

print(type(f))
print(f)
show(f)

print(f(2, a=5))
print type(f(2, a=5))

The result should look like this:

Now, let's define another function, which is the derivative of f(x):

Evaluate the following code to define g(x):

g(x) = derivative(f, x)
show(g)
g(x=2, a=3)

The result should look like this:

What just happened?

We started out using the var function to define some symbolic variables. Technically, we didn't need to explicitly define x as a symbolic variable, because Sage assumes that x is symbolic by default. We then used the notation f(x) = a * x^3 to define a callable symbolic expression called f, and we confirmed that f was symbolic by using the type function. We used the print function to display f, and then introduced a new function called show to display a typeset...