Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Sage Beginner's Guide
  • Table Of Contents Toc
Sage Beginner's Guide

Sage Beginner's Guide

By : Craig Finch
3.9 (11)
close
close
Sage Beginner's Guide

Sage Beginner's Guide

3.9 (11)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
close
close
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1
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...
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Sage Beginner's Guide
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon