Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download

Tutorial Release 10.4 The Sage Development Team https://doc.sagemath.org/pdf/en/tutorial/sage_tutorial.pdf

Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.

This tutorial is the best way to become familiar with Sage in only a few hours. You can read it in HTML or PDF versions, or from the Sage notebook (click Help, then click Tutorial to interactively work through the tutorial from within Sage).

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

https://creativecommons.org/licenses/by-sa/3.0/

386 views
ubuntu2204
Kernel: SageMath 10.4

Numerical Approximations

As the last example shows, some mathematical expressions return "exact" values, rather than numerical approximations. To get a numerical approximation, use either the function N or the method n (both of these have a longer name, numerical_approx, and the function N is the same as n)). These take optional arguments prec, which is the requested number of bits of precision, and digits, which is the requested number of decimal digits of precision; the default is 53 bits of precision.

# Import SageMath functions from sage.all import *
# Exact and numerical approximation examples exp(2)
e^2
n(exp(2))
7.38905609893065
sqrt(pi).numerical_approx()
1.77245385090552
sin(10).n(digits=5)
-0.54402
N(sin(10), digits=10)
-0.5440211109
numerical_approx(pi, prec=200)
3.1415926535897932384626433832795028841971693993751058209749
# Using explicit Integer conversion exp(Integer(2))
e^2
n(exp(Integer(2)))
7.38905609893065
sqrt(pi).numerical_approx()
1.77245385090552
sin(Integer(10)).n(digits=Integer(5))
-0.54402
N(sin(Integer(10)), digits=Integer(10))
-0.5440211109
numerical_approx(pi, prec=Integer(200))
3.1415926535897932384626433832795028841971693993751058209749

Dynamic Typing in Python

Python is dynamically typed, so the value referred to by each variable has a type associated with it, but a given variable may hold values of any Python type within a given scope:

# Import SageMath functions from sage.all import *
# Assign integer to variable a a = Integer(5) # a is an integer type(a)
<class 'sage.rings.integer.Integer'>
# Assign rational number to variable a a = Integer(5) / Integer(3) # now a is a rational number type(a)
<class 'sage.rings.rational.Rational'>
# Assign string to variable a a = 'hello' # now a is a string type(a)
<class 'str'>

Note

The C programming language, which is statically typed, is much different; a variable declared to hold an int can only hold an int in its scope.