Sage/Printable version
Appearance
< Sage
This is the print version of Sage You won't see this message or any elements not part of the book's content when you print or preview this page. |
Sage
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Sage
Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.
First Steps
Installation
[edit | edit source]Run
[edit | edit source]Commandline Interface
[edit | edit source]In Ubuntu terminal:
> ./sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 8.1, Release Date: 2017-12-07 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
> sage:
Notebook Interface
[edit | edit source]Inside Sage type "notebook()" for the browser-based notebook interface.
> sage: notebook()
Introduction
[edit | edit source]Sage is based on Python. With some exceptions, all commands are regular Python syntax.
Very simple calculation
[edit | edit source]- Numbers
> sage: 1+1
> 2
- Symbolic expression
> sage: sqrt(2)
> sqrt(2)
- Evaluate a symbolic expression
> sage: sqrt(2).n(digits=100)
> 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573
Arrays
[edit | edit source] > sage: a = [1,2,3]
> sage: sum(a)
> 6
Crash Course
Understanding Objects
[edit | edit source]To work with Sage you have to know a little bit of Python, because it is the underlying language Sage is written in and it is the language Sage understands!
- Objects: An object is a data structure with methods that operate on the data inside an object. In Python - and Sage - everything is an object, even the number "1".
- You can store objects in variables. That is a string of characters where only a-z, A-Z, digits (digits must not be at the first position) and the sign "_" is allowed (e.g. "abs_1"). To accomplish this, enter "x=1" and indicate with "Shift-Return/Enter" that you have ended your input.
- To access the data (the object's value) behind the variable, you have to enter it and probably write "print" in front of it.
- The methods of an object can be accessed via the "." sign and then comes the name and parenthesis "(...)", where they can be empty or have arguments.
- The data stored inside an object is accessed similarly just by the "." and the name but with no parenthesis.
- Functions are objects that do not need an object.
Example:
# storing the object "11" in "x"
sage: x = 11
#show me "x"
sage: print x
11
# factorial() is a method of x
sage: x.factorial()
39916800
# sqrt is a function
sage: sqrt(x)
sqrt(11)
- Objects can be combined with others:
- using an operator sign like "+", "*", ...
- inserting them into the argument list of a method or function
Example:
# operator "+" adds the values of "x" and "x"
sage: x+x
22
# function "n" evaluates the numerical value of "sqrt(11)"
sage: n(sqrt(11), digits=50)
3.3166247903553998491149327366706866839270885455894