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 relational expressions


Let's express some simple inequalities as relational expressions to see how they work:

exp1 = SR(-5) < SR(-3)    # use the symbolic ring
print("Expression {0} is {1}".format(exp1, bool(exp1)))

exp2 = exp1^2
print("Expression {0} is {1}".format(exp2, bool(exp2)))

forget()
exp3 = x^2 + 2 >= -3 * x^2
print("Expression {0} is {1}".format(exp3, bool(exp3)))

p1 = plot(exp3.lhs(), (x, -5, 5), legend_label='lhs')
# also lhs() or left_hand_side()

p2 = plot(exp3.rhs(), (x, -5, 5), color='red', legend_label='rhs')
# also rhs() or right_hand_side()

show(p1 + p2)

The output is shown in the following screenshot:

What just happened?

We defined a simple relational expression that expresses an inequality between two integers. We used the SR function to create symbolic objects, rather than integer objects, that represent the values -5 and -3. This is important because we need to give Sage a symbolic expression to work with. To evaluate the truth of...