AN INTRODUCTION TO SAGE PROGRAMMING
AN INTRODUCTION TO SAGE PROGRAMMING With Applications to SAGE Interacts for Numerical Methods RAZVAN A. MEZEI Lenoir-Rhyne University Donald & Helen Schort School of Mathematics and Computing Sciences Hickory, NC USA
Copyright 2016 by John Wiley & Sons, Inc. All rights reserved Published by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. For general information on our other products and services or for technical support, please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic formats. For more information about Wiley products, visit our web site at www.wiley.com. Library of Congress Cataloging-in-Publication Data applied for. ISBN:9781119122784 Typeset in 10/12pt TimesLTStd by SPi Global, Chennai, India. Printed in the United States of America 10987654321 1 2016
I would like to dedicate this work to all my instructors who passionately directed my interest into the great fields of Mathematics, Computer Science, and Statistics. To name just a few of them, in chronological order: Aurel Netea, Petru Dragos, Barnabas Bede, Alina Alb Lupas, Dan Noje, Mircea Balaj, Sorin Gal, Mircea Dragan, Alexandru Bica, Ioan Fechete, Ioan Dzitac, George Anastassiou, Maria Botelho, James Campbell, E. Olusegun George, Nikos Frantzikinakis, and not the least Seok Wong. There are many more, but the named ones modeled my thinking and gave me a direction to where I am today and influenced me the most. Thank you from all my heart for your dedication, support, and friendship. Razvan A. Mezei
CONTENTS Preface xi 1 Introduction 1 1.1 What is Sage Math?, 1 1.2 Various Flavors of Sage Math, 2 1.2.1 Sage Math on Your Machine, 2 1.2.2 Sage Cell, 2 1.2.3 Sage Cloud, 2 2 Using Sage Math as a Calculator 5 2.1 First Sage Math Examples, 5 2.2 Computations, 6 2.2.1 Basic Arithmetic Operators, 6 2.2.2 Decimals Versus Exact Values, 10 2.2.3 Constants, 11 2.2.4 Breaking Long Lines of Code, 12 2.2.5 Comments, 13 2.2.6 Library Functions, 14 2.2.7 Working with Strings, 17 2.2.8 Solving Equations and Inequalities, 19 2.2.9 Calculus Functions, 21 2.2.10 Exercises, 25
viii CONTENTS 2.3 Graphs, 28 2.3.1 2D Graphs, 28 2.3.2 3D Graphs, 50 2.3.3 Exercises, 52 3 Introduction to Programming in Sage 55 3.1 Variables, 56 3.1.1 Exercises, 59 3.2 More on Operators, 59 3.2.1 Exercises, 61 3.3 Making Decisions, 61 3.3.1 Boolean Expressions, 61 3.3.2 If Statements, 64 3.3.3 Exercises, 71 3.4 Loops, 73 3.4.1 For Loops, 73 3.4.2 Strings, 80 3.4.3 While Loops, 82 3.4.4 Nested Loops, 86 3.4.5 Lists, 88 3.4.6 Exercises, 93 3.5 Functions, 96 3.5.1 Using Library Functions: Random, SciPy, NumPy, 101 3.5.2 Exercises, 101 3.6 Interacts, 103 3.6.1 Exercises, 118 3.7 Application to Data Security: Caesar s Cipher. Interacts, Strings, and Encryption, 120 3.7.1 Exercises, 122 3.8 Application to Business: Present Value of an Annuity. Amortization, 122 3.8.1 Exercises, 127 3.9 Application to Elementary Statistics: Mean, Median, Histograms, and Bar Charts, 128 3.9.1 Exercises, 135 4 Sage Interacts for Numerical Methods 137 4.1 Equations of Lines, 137 4.1.1 Exercises, 139 4.2 Tangent Lines and Plots, 139 4.2.1 Exercises, 142 4.3 Taylor Polynomials, 143 4.3.1 Exercises, 147
CONTENTS ix 4.4 Riemann Sum and Definite Integrals, 148 4.4.1 Exercises, 153 4.5 Trapezoidal Rule for Numerical Integration, 153 4.5.1 Exercises, 159 4.6 Bisection Algorithm for Solving Equations, 160 4.6.1 Exercises, 167 4.7 Newton Raphson Algorithm for Solving Equations, 167 4.7.1 Exercises, 178 4.8 Polynomial Interpolation, 179 4.8.1 Exercises, 184 4.9 Linear Spline Interpolation, 184 4.9.1 Exercises, 188 4.10 Cubic Spline Interpolation, 188 4.10.1 Exercises, 196 4.11 Sage for Solving Differential Equations, 196 4.12 Numerical Methods for Ordinary Differential Equations, 199 4.12.1 Exercises, 205 4.13 Numerical Methods for Partial Differential Equations, 205 4.13.1 Exercises, 210 4.14 Scatter Plots Line of Best Fit and More, 211 4.14.1 Exercises, 218 4.15 Matrices, Eigenvalues, and Eigenvectors, 218 4.15.1 Exercises, 224 4.16 Solving Matrix Equations, 224 4.16.1 Exercises, 226 References 227 Index 229
PREFACE This work is intended to be a gentle introduction to programming in Sage Math and Sage Interacts. It assumes no programming background from the reader, and it is specifically tailored for Mathematics, Mathematics Education, and Engineering students and instructors. The book starts with a description on how one can use Sage Math as a calculator. It also explains how one can use it for computations and for plotting. Then, it covers a brief and gentle introduction to programming in Sage Math. You will learn how to create your own methods in Sage Math and how to create Sage Interacts. The book ends with a chapter that gives several examples on how one can use Sage Interacts for various Numerical Methods. If you have no programming background yet, your programming skills need some improvements, you want to learn how to use Sage Math to program some numerical methods, or you want to create neat interactive representations of some mathematical concepts, then this book is for you. The book, however, does not address in detail the mathematical topics covered in the given Sage Interacts examples. In particular, no mathematical proofs are given in here. If you want to study the Mathematical side of Numerical Analysis, we recommend pairing this book with such a textbook. See, for example, [2; 7; 8]. If you are still wondering whether to use a programming language such as Python, C, C++, Java, or some computational software such as Maple, Mathematica, Octave, or R, look no further. We strongly recommend you Sage Math. It is FREE, open-source, and it uses a Python-like syntax. This last phrase contains some of the strongest arguments why you may want to choose Sage Math. To elaborate on the last statement, free open-source software allows you to use the software for free and also allows you to access the source code, which can be
xii PREFACE a great source of inspiration and information. It also allows you to obtain the entire Sage Math s source code and change it to better address your own needs. The Sage Math syntax is similar to that of Python. Python has become very popular for being one of the easiest introductory programming languages. It became so trendy that many (if not most, by the time you read this book) universities in the United States are using Python to teach Introductory to Programming courses. It is that easy! In fact, most of the Sage Math source code was written in Python. An interesting article one may want to read in this direction is Python bumps off Java as top learning language, by Joab Jackson (http://www.javaworld.com/article/2452940/learn-java /python-bumps-off-java-as-top-learning-language.html). It says that Python has surpassed Java as the top language used to introduce U.S. students to programming and computer science. We hope we got your interest in learning Sage Math. You can use it as a Computer Algebra System, as a Programming Tool, or to create nice interactive mathematical demonstrations (using Sage Interacts). You can download and install Sage Math on your own machine, or you can use it over the Internet: for this, you may either choose Sage Cloud, or you may prefer Sage Cell. Sage is accessible from your desktop computer or from your smartphone. We recommend this book to all undergraduate Mathematics, Mathematics Education, Computer Science and Information Technology, pre-engineering, and Science students and instructors. Sage Math can be used in most Mathematics courses, in Introduction to Programming courses, as well as other computational courses. Razvan A. Mezei USA