- •Credits
- •About the Author
- •About the Reviewers
- •www.PacktPub.com
- •Preface
- •Getting started
- •More advanced graphics
- •Summary
- •Installing Sage
- •Starting Sage
- •Start Sage
- •Prerequisites
- •Installation
- •Summary
- •Command history
- •Working with code
- •Arithmetic operators
- •Strings
- •Functions
- •Functions with keyword arguments
- •Objects
- •Summary
- •Python 2 and Python 3
- •Running scripts
- •Strings
- •List comprehensions
- •Storing data in a dictionary
- •Summary
- •Vectors and vector spaces
- •Creating a vector space
- •Vector operators and methods
- •Decomposing matrices
- •Summary
- •Using graphics primitives
- •Summary
- •Substitutions
- •Finding roots
- •Derivatives
- •Integrals
- •Series and summations
- •Summary
- •Computing gradients
- •Constrained optimization
- •Probability
- •Summary
- •Making our tanks move
- •Unit testing
- •Summary
- •Introducing Python decorators
- •Making interactive graphics
- •Summary
- •Index
Chapter 7
What just happened?
We used the solve function to solve an equation, a system of equations, and an inequality. The first argument to solve is an equation or a list of equations. The next argument (or arguments) is the variable or variables to solve for. By default, the solutions are returned as a list of symbolic expressions. The optional keyword argument solution_dict=True
causes the solutions to be returned as a list of dictionaries. Each dictionary has a key for each variable that was solved for, and the value for a key is the solution for that variable. Solving an inequality uses the same syntax. In this case, Sage was unable to find a symbolic solution to the inequality.
Finding roots
Finding the roots of an equation (also known as the zeros of the equation) is closely related to solving an equation. Callable symbolic expressions in Sage have a special method that finds their roots.
Timeforaction–findingroots
Enter the following code into an input cell in a worksheet, and evaluate the cell:
#A problem we already know the answer to var('a, b, c, x')
f(x) = a * x^2 + b * x + c root_list = f.roots(x) for root in root_list:
print("Root with multiplicity {0}:".format(root[1])) show(root[0])
#Something more complicated
g(x) = expand((x^2 - 1)^3 * (x^2 + 1) * (x - 2)); g.show()
root_list = g.roots(x) for root in root_list:
print("Root: {0} multiplicity: {1}".format(root[0], root[1])) p1 = plot(g, (-2, 2))
p1.ymin(-10) p1.show()
[ 191 ]
Making Symbolic Mathematics Easy
The output is shown in the following screenshot:
[ 192 ]
Chapter 7
What just happened?
We started off by finding the roots for the general second-order polynomial with the roots method. The result is a list of tuples that represent the roots. The first item in each tuple is the multiplicity, and the second item is the root. We passed the variable x to roots. If this argument is not provided, roots formulates the solution in terms of the default variable. We then looked at a higher-order polynomial, which has three real roots and two complex roots. The analytical results are confirmed by a plot of the function that shows the real roots of the function. The roots method accepts several optional arguments that control its behaviour:
Keyword |
Default |
Description |
explicit_solutions |
True |
If False, also include implicit roots |
multiplicities |
True |
If False, return a list of roots without multiplicities |
ring |
None |
If a ring is specified, the expression is converted to a polynomial |
|
|
over the ring, and the roots are found over the ring |
Differentialandintegralcalculus
Many branches of advanced mathematics are built upon a foundation of basic calculus. The following examples will demonstrate how to use Sage to compute limits, derivatives, and integrals of symbolic functions.
Timeforaction–calculatinglimits
The concept of the limit is often used to define the integral and the derivative. Run the following code to see how to compute limits with Sage:
var('x')
#Something easy f(x) = 1 / x
print("Limit of 1/x as x->0+: {0}".format(limit(f, x=0, dir='plus')))
print("Limit of 1/x as x->0-: {0}".format(limit(f, x=0 dir='minus')))
p1 = plot(f, (x, -1, 1), detect_poles='show') p1.axes_range(-1, 1, -10, 10)
p1.show()
#Something more complex
g(x)=(2 * x + 8) / (x^2 + x - 12) g.show()
print("Limit of g(x) as x->-4: {0}".format(limit(g, x=-4)))
[ 193 ]
Making Symbolic Mathematics Easy
h(x) = (x^2 - 4) / (x - 2) h.show()
print("Limit of h(x) ax x->2: {0}".format(lim(h, x=2)))
The results are shown in the following screenshot:
[ 194 ]
