Drawn by Using SageMath
About SageMath
SageMath is a free, open-source mathematical software system under the GPL. It integrates many open source Python packages and is written in Python, but also supports other languages. Its goal is to create a mutable open source software alternative to Matlab, Magma, Maple, and Mathematica.
In order to make it easy for everyone to understand, learn and use SageMath, our SmartNotebook (abbreviation: SNB) makes full use of the characteristics of Notebook by introducing the SageMath engine and meticulously optimizing and improving compatibility, which not only reduces a large number of complex installations and configurations, but also allows users to use it immediately.
Without further ado, let's use the examples provided by SmartNotebook combined with SDSU Sage Tutorial to quickly understand some of SageMath's syntax, functions and other features.
Explore SageMath with SNB
First, we use SmartNotebook to quickly create a notebook for the Sagemath kernel. As shown below, enter the title of the notebook, select the Sagemath kernel, and click submit.
Next, we enter the notebook and directly create the Code Cell cell, and we can write and execute the Sage code.
For example, let's factor the integer 20221208 prime, create a new Code Cell cell, enter the following code and click Execute:
factor(20221208)
2^3 * 7 * 361093
SageMath's help system
Help: Use the "?"
When we need to know exactly what a function or command does and how to use it, we can use "?" to invoke the built-in help system. For example:
lcm?
View the source code: Use the "??"
Some students who like to study the source code want to see the source code of a command in SageMath, you just need to type the command you are interested in, and then type "??".
For example, look at the source code of factor() (the screenshot section takes part of the source code):
factor??
SageMath as a Calculator
Here we introduce how to use some arithmetic and function commands of SageMath like a graphing calculator.
Basic arithmetic
The basic arithmetic operators are addition +, subtraction -, multiply by *, divide / , exponential ^
The symbol in front of the number - indicates that it is a negative number.
print('1+1=',1+1)
print('103-101=',103-101)
print('7*9=',7*9)
print('7337/11=',7337/11)
print('11/4=',11/4)
print('2^5=',2^5)
print('-11+9=',-11+9)
-6
Output:
1+1= 2
103-101= 2
7*9= 63
7337/11= 667
11/4= 11/4
2^5= 32
-11+9= -2
-6
SageMath follows the standard sequence of operations, namely PEMDAS (parenthesis, exponents, multiplication, division, addition, subtraction): parentheses, exponents, multiplication, division, addition, subtraction.
print(2*4^2+1)
print((2*4)^2+1)
print(2*4^(2+1))
print(-3^2)
print((-3)^2)
Output:
33
65
128
-9
9
When two integers are divided, there is a subtlety: SageMath will return a fraction or its decimal approximation. Unlike most graphing calculators, SageMath will try to be as accurate as possible and will return fractions unless otherwise noted.
print(11/4)
print(11/4.0)
print(11/4.)
print(11.0/4)
print(11/4*1.)
Output:
11/4
2.75000000000000
2.75000000000000
2.75000000000000
2.75000000000000
Integer division and factorization
Sometimes when we use division, the division operator does not give us all the information we want. For example, we want to know not only what the reduced fraction is, but even its decimal approximation, or what the unique quotient and remainder are. We can use the operator // to calculate the quotient and the operator % for the remainder. Use the divmod() function to calculate both the quotient and the remainder.
print( 14 // 4)
print( 14 % 4)
print( divmod(14,4))
Output:
3
2
(3, 2)
We know that when two integers are divisible, we can use that the A/B remainder is 0, which means that it can be divisible, but SageMath integers have a built-in method specifically to check whether one integer is divisible by another integer.
print(3.divides(15))
print(5.divides(17))
Output:
True
False
Related to the divides method is also a method divisors(), which can return a list of all positive divisors of the specified integer.
print(12.divisors())
101.divisors()
Output:
[1, 2, 3, 4, 6, 12]
[1, 101]
We know that when the divisor of an integer is just 1 and itself, the number is prime. To check if a number is prime in SageMath, we can use method is_prime()
print(153.is_prime())
(2^19-1).is_prime()
Output:
False
True
factor() calculates the prime factorization of integers
print(62.factor())
63.factor()
Output:
2 * 31
3^2 * 7
Interested in simply knowing which prime numbers are divided by integers, we can use the prime_divisors() or prime_factors() method.
print(24.prime_divisors())
print(24.prime_factors())
print(63.prime_factors())
print(63.prime_divisors())
Output:
[2, 3]
[2, 3]
[3, 7]
[3, 7]
Find the greatest common divisor and least common multiple.
The greatest common divisor (GCD) is the largest of all these common divisors.
The least common multiple (LCM) is the smallest integer that divides two integers.
print( gcd(14,63))
print( gcd(15,19))
print(lcm(4,5))
lcm(14,21)
Output:
7
1
20
42
Standard functions and constants
SageMath contains almost all the standard functions that people encounter when learning math. Next, we'll cover some of the most commonly used functions: maximum, minimum, floor, ceiling, trigonometric, exponential, logarithm functions. We will also see many standard mathematical constants such as Euler's constant (e), π, and the golden ratio (φ).
# max 最大值
print(max(1,5,8))
# min 最小值
print(min(1/2,1/3))
# abs 绝对值
print(abs(-10))
# floor 向下取整
print(floor(2.1))
# ceil 向上取整
print(ceil(2.1))
Output:
8
1/3
10
2
3
When using floor, CEIL, be especially careful and pay attention to the calculation accuracy
print(floor(1/(2.1-2)))
print(1/(2.1-2))
Output:
9
9.99999999999999
Computers store real numbers in binary form, and we are used to using decimal representations. Decimal notation is very simple and short, but when converted to binary, it is:
Since computers cannot store an infinite number of numbers, this is rounded somewhere, causing the precision error we see.
However, in SageMath, rational numbers (fractions) are exact, so we will never see this rounding error. Therefore, it is generally a good idea to use rational numbers (fractions) instead of decimals whenever possible, especially if high precision is required.
floor(1/(21/10-2))
Output:
10
sqrt() This command/function calculates the square root of a real number. As we have seen earlier in fractions, if we want a decimal approximation, we can get it by giving a decimal number as input.
print(sqrt(3))
print( sqrt(3.0))
Output:
sqrt(3)
1.73205080756888
To calculate other roots, we use rational indices. SageMath can compute any rational power. If the exponent or cardinality is decimal, the output will be decimal.
print( 3^(1/2) )
print( (3.0)^(1/2) )
print( 8^(1/2) )
print( 8^(1/3) )
Output:
sqrt(3)
1.73205080756888
2*sqrt(2)
2
SageMath also supports all standard trigonometric functions such as sine sin() and cosine cos()
print( sin(1) )
print( sin(1.0) )
print( cos(3/2) )
print( cos(3/2.0) )
Output:
sin(1)
0.841470984807897
body(3/2)
0.0707372016677029
Once again, we see the same behavior as SageMath, which will give us an exact answer. You might be thinking, if there's no way to simplify, why bother? Well, some expressions that involve sinusoids can indeed be simplified. For example, an important identity in geometry is :
SageMath has a built-in symbolic π to use this identity:
print(pi)
print(sin(pi/3))
Output:
pi
1/2*sqrt(3)
Within SageMath, we're dealing entirely with π, not some numerical approximation. However, we can call numeric approximation using the following method: pi.n()
print(pi.n())
print(sin(pi))
print(sin(pi.n()))
Output:
3.14159265358979
1.22464679914735e-16
We see that when symbols are used, SageMath returns exact results. However, when we use an approximation, we get an approximation. is abbreviated for 10^{-16}, the number should be zero, but the approximation introduces an error. Here are a few examples of using symbols, exact π approximations to numeric values:
print( sin(pi/6))
print( sin(pi.n()/6))
print( sin(pi/4))
print(sin(pi.n()/4))
Output:
1/2
0.500000000000000
1/2*sqrt(2)
0.707106781186547
Continuing our topic, there are lesser-known special angles that subtly simplify the value of sine or cosine.
print( sin(pi/10))
print( cos(pi/5) )
print( sin(5*pi/12) )
Output:
1/4*sqrt(5) - 1/4
1/4*sqrt(5) + 1/4
1/4*sqrt(6) + 1/4*sqrt(2)
Other trigonometric, inverse trigonometric, and hyperbolic functions are also available
print( arctan(1.0) )
print( sinh(9.0))
Output:
0.785398163397448
4051.54190208279
Similar to π within SageMath, it has a built-in number sign constant e, which is the base of the natural logarithm.
print(e)
print(e.n())
Output:
and
2.71828182845905
While some people may be familiar with using natural logarithms and representing logarithmic base 10, in SageMath both represent logarithmic base e. We can specify a different base as the second argument to the command: to be calculated in SageMath:
We use commands: ln(x), log(x), log(x, b), and the power base e can be done using functions and by raising the symbolic constant to a specified power.
print( ln(e) )
print( log(e) )
print( log(e^2) )
print( log(10) )
print( log(10.0) )
print( log(100,10) )
print( exp(2) )
print( exp(2.0) )
print( exp(log(pi)) )
print( e^(log(2)) )
Output:
1
1
2
log(10)
2.30258509299405
2
e^2
7.38905609893065
pi
2
Okay, this time our exploration of SageMath is introduced here, in the following articles we will explore and introduce the following, I hope interested friends can continue to pay attention:
- Use SageMath to solve equations and inequalities
- Introduction to SageMath 2D and 3D visualization
- SageMath's data structure and programming
"New Language Data Story Collection, Number Speaks New Language" Popular science data science, telling data stories, and deeply mining the value of data. Welcome friends to contribute!
WeChat | SnbDataStory
"New Language Data Story Collection, Numbers Speak New Language"