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 – calling Sage from a Python script


Sage has advanced numerical integrators that adapt to the rate of change in the function being integrated. Let's say that you want to use one of these integrators to integrate an analytical function. You only want to use the integrator, so you don't want to manually start Sage and load a script just to access one function. We can create a stand-alone Python script that calls Sage when necessary. Create a new plain text file, enter the following code, and save it with a .py extension.

#!/usr/bin/env sage -python
from sage.all import *

vars={}

# Define limits of integration
vars['a'] = 0
vars['b'] = 8

sage_eval('None', cmds='f(x)=e^x*cos(x)', locals=vars)
print vars['f']

sage_eval('None', cmds='value, tol = numerical_integral(f,a,b)', locals=vars)
print(vars['value'])

There are two ways to run this script. One option is to open a terminal and enter the following command. If the Sage installation is on your path, you can simply type:

$ sage...