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 – expanding and factoring polynomials


A number of methods are especially useful when working with polynomials. Let's see how they work:

var('x')
exp1 = (x + 3)^3 == (x - 1)^2
show(exp1)

print("Expanded expression:")
exp2 = exp1.expand()
show(exp2)

print("Expand left-hand side only:")
show(exp1.expand('left'))
lhs = exp1.expand('left').lhs()

print("Factor LHS:")
show(lhs.factor())

print("Information about the expanded LHS:")
print("  Highest degree of x on LHS: {0}".format(lhs.degree(x)))
print("  Coefficients: {0}".format(lhs.coeffs(x)))
print("  Coefficient of x^2: {0}".format(lhs.coeff(x,2)))
print("  Trailing coefficient of LHS: {0}".format
    (lhs.trailing_coefficient(x)))

The output is shown in the following screenshot:

What just happened?

We defined a relational symbolic expression that equates two polynomials. We used the expand method (with no arguments) to multiply out the polynomials on each side of the equality. We also used the expand method with the optional...