0% found this document useful (0 votes)
34 views21 pages

SageMath Note 1

The document provides a comprehensive overview of mathematical functions and operations using Sage Math, including logarithmic, trigonometric, and exponential functions, as well as complex numbers and vectors. It covers syntax for various mathematical computations, such as factorization, simplification, and numerical approximations. Additionally, it discusses prime numbers, their properties, and how to manipulate variables and functions in Sage Math.

Uploaded by

abel.aryanghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views21 pages

SageMath Note 1

The document provides a comprehensive overview of mathematical functions and operations using Sage Math, including logarithmic, trigonometric, and exponential functions, as well as complex numbers and vectors. It covers syntax for various mathematical computations, such as factorization, simplification, and numerical approximations. Additionally, it discusses prime numbers, their properties, and how to manipulate variables and functions in Sage Math.

Uploaded by

abel.aryanghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

In [ ]:

To look √(x) we can use the ′ show′ command

In [1]:
show(sqrt(5))

√5

In [2]:

n(log2)

Out[2]:
0.693147180559945

In [ ]:

In [3]:
ln(2)

Out[3]:
log(2)

In [ ]:

In [4]:
2*log(2.)
Out[4]:

1.38629436111989

In [ ]:

In [5]:
log(4.)
Out[5]:
1.38629436111989

In [ ]:

In [ ]:
# For Logarithm in base 10 : log(x, 10)

In [ ]:
In [6]:
log(12.0,10)

Out[6]:
1.07918124604762

In [ ]:

In [7]:
log(3).n()

Out[7]:
1.09861228866811

In [ ]:

Some spcial values


imaginary unit i I or i

Infinity ∞ Infinity or ∞

Archimedes’ constant π pi

In [ ]:

In [8]:
sqrt(-1)

Out[8]:
I

In [9]:
pi
Out[9]:
pi

In [10]:
n(pi)
Out[10]:
3.14159265358979

In [11]:
n(pi,digits=160)
Out[11]:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034
825342117067982148086513282306647093844609550582231725359408128481117450

In [ ]:

To compute eπi one can easily use the following expression:

In [6]:

e^(pi*i)
Out[6]:
-1

In [ ]:

In [12]:
exp(2 * I * pi/3)
Out[12]:
1/2*I*sqrt(3) - 1/2

In [ ]:

To get numerical approximations :

numerical_approx(result, digits=n)

n(result)

In [13]:
n(exp(2 * I * pi/3))
Out[13]:
-0.500000000000000 + 0.866025403784439*I

In [ ]:

If needed, it is possible to explicitly call a simplification function:

To simplify cos−1
(sin(π/3))

In [14]:

simplify(acos(sin(pi/3)))
Out[14]:
1/6*pi

In [ ]:

Note: Although Sage Math is case-sensitive


Note: Although Sage Math is case-sensitive
−−

Use I or i to find √−1

In [15]:
i^2
Out[15]:
-1

In [16]:
I^2
Out[16]:
-1

In [ ]:

Built-in functions :
iv) Trigonometric Function : sin, cos, tan

v) Inverse Trigonometric Function : asin , acos , atan

In [ ]:

In [17]:
sin(pi)
Out[17]:
0

In [18]:
tan(pi/3)
Out[18]:
sqrt(3)

In [ ]:

Of course, it is also possible to compute numerical approximations of the results, with an accuracy as large as
desired:

In [ ]:

In [19]:
numerical_approx(tan(pi/3), digits=10)
Out[19]:
1.732050808
1.732050808

In [ ]:

In [20]:
asin(1)

Out[20]:
1/2*pi

In [ ]:

In [21]:
asin(1.0)
Out[21]:
1.57079632679490

In [ ]:

In [22]:
tan(pi)
Out[22]:
0

In [23]:
tan(pi/2)
Out[23]:
Infinity

In [ ]:

In [ ]:

iii) Exponential and logarithm :


exp or e , log (natural logarithm)

In [24]:
e^2
Out[24]:
e^2

In [ ]:

In [25]:
e^(2.0)
Out[25]:
7.38905609893065

In [ ]:

In [8]:
numerical_approx(e^2, digits=60)
Out[8]:
7.38905609893065022723042746057500781318031557055184732408713

In [ ]:

In [26]:
log(12,2)
Out[26]:
log(12)/log(2)

In [ ]:

In [27]:
n(log(12,2))
Out[27]:
3.58496250072116

In [ ]:

In [ ]:
v) Hyperbolic functions : sinh, cosh, tanh

vi) Inverse hyperbolic functions : arcsinh, arccosh, arctanh

viii) Integer part , etc : floor, ceil, trunc, round

In [ ]:

In [28]:
ceil(5/3)
Out[28]:
2

In [ ]:

In [29]:

(5/3).trunc()
Out[29]:
1
1

In [ ]:

In [30]:
round(pi, 3)
Out[30]:
3.142

In [ ]:

In [31]:
abs(-5.2)
Out[31]:
5.20000000000000

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [33]:
acos(sin(pi/3))
Out[33]:
arccos(1/2*sqrt(3))

In [ ]:

In [34]:
n(acos(sin(pi/3)))
Out[34]:
0.523598775598299

In [ ]:

In [35]:
show(exp(I*pi/7))

( 7 i π)
1
e
In [ ]:

In [36]:
exp(I*pi/7)
Out[36]:
e^(1/7*I*pi)

In [ ]:

In [37]:

n(exp(I*pi/7))
Out[37]:
0.900968867902419 + 0.433883739117558*I

In [ ]:

Factors and Primes


In [ ]:
Syntax : factor(n)

In [ ]:

In [1]:
factor(1050)
Out[1]:
2 * 3 * 5^2 * 7

In [ ]:

In [ ]:
# checking primes and checking next primes

In [ ]:
is_prime(n)

next_prime(n)

prevous_prime(n)

In [ ]:

In [2]:
is_prime(2^50-1)
is_prime(2^50-1)
Out[2]:
False

In [ ]:

In [3]:
next_prime(23)
Out[3]:
29

In [ ]:

In [4]:
previous_prime(23)
Out[4]:
19

In [ ]:

To find numbers of primes. INPUT: x - a real number prime_bound Syntax : prime_pi

will make sure to use all the primes up to prime_bound

In [ ]:

In [5]:
prime_pi(2)
Out[5]:
1

In [6]:
prime_pi(10.5)
Out[6]:
4

In [7]:
prime_pi(140)
Out[7]:
34

In [ ]:

To find numbers of primes. INPUT: x - a real number prime_bound - (default 0) a real number < 2^32,
In [8]:
prime_pi(2^32)
Out[8]:
203280221

In [ ]:

In [ ]:
To find prime divisors :

INPUT: x - an integer

Syntax : prime_divisors

Gives all the prime divisors.

In [ ]:

In [9]:
prime_divisors(1000)
Out[9]:
[2, 5]

In [ ]:

In [10]:
prime_factors(1000)
Out[10]:
[2, 5]

In [ ]:

In [ ]:
To find divisors :

INPUT: x - an integer

Syntax : divisors

Gives all the divisors.

In [11]:
divisors(340)

Out[11]:
[1, 2, 4, 5, 10, 17, 20, 34, 68, 85, 170, 340]

In [ ]:
In [ ]:

## finding all primes in an interval [a , b]

Syntax : prime_range(a,b)

In [ ]:

In [12]:

prime_range(20,40)
Out[12]:

[23, 29, 31, 37]

In [ ]:

In [ ]:
# To factor an integer expression

(Number).factor()

In [ ]:

In [13]:
320.factor()

Out[13]:
2^6 * 5

In [ ]:

In [1]:

#to factor an integer expression

(2^50-1).factor()

Out[1]:
3 * 11 * 31 * 251 * 601 * 1801 * 4051

In [ ]:

In [14]:

#compute the greatest common divisor


# of two integers
gcd(90,12)

Out[14]:
6

In [ ]:
In [15]:

#compute the least common multiple


# of two integers
lcm(6,4)

Out[15]:
12

Breaking Long Lines of Code


Similarly to Python, whenever you have a line of code that is too long and you want to break it into multiple lines,
you can use “\”

(((2-4)*(4-3)+4+260-100)^10)/(2^20+3^20)
In [ ]:

In [2]:

(((2-4)*(4-3)+4+ \
260-100)^10)/(2^20+3^20).n()

Out[2]:
3.56939380760786e12

In [ ]:

In [ ]:

Defining variables on rings


For engineering and scientific computation, you will generally use real or complex number
s and ignore the other types.

However, when working with symbolic mathematics or doingtheoretical work, it may be very
important to specify the correct ring for a variable.

Ring Constructor in Sage

Integers : ZZ
Rational numbers : QQ
Real numbers : RR
Complex numbers : CC

In [ ]:

VECTORS and LIST


First we define a vector over the field of real numbers:

v
= vector
(RR,n,
[a0 , a1 .
[a0 , a1 .
.... an ]
)

In [17]:

v = vector(RR, 4, [1, 1/3, 1/4, 1/5])

In [18]:
v

Out[18]:
(1.00000000000000, 0.333333333333333, 0.250000000000000, 0.200000000000000)

In [19]:
v[1]

Out[19]:
0.333333333333333

In [ ]:

In [20]:

v = vector(QQ, 4, [1, 1/3, 1/4, 1/5])

In [21]:
v

Out[21]:
(1, 1/3, 1/4, 1/5)

In [ ]:

In [22]:

v=[1, 1/3, 1/4, 1/5]

In [23]:
v[0]

Out[23]:
1

In [24]:
v[2]

Out[24]:
1/4

In [ ]:

In [25]:

u=['Hello', 1, sin(x^2), 1/5, 0.5]


In [ ]:

In [26]:

u[0]
Out[26]:

'Hello'

In [27]:

u[2]
Out[27]:

sin(x^2)

In [ ]:

In [28]:
len(u)

Out[28]:
5

In [ ]:

In [29]:

del u[1]

In [30]:
len(u)

Out[30]:
4

In [ ]:

In [ ]:

Basic Algebra (Factorisation and Expansion)


In [ ]:

Function function_name(argument_1, argument_2, … , argument_n):

f(x1 , x2 ,......, xn )

In [ ]:
Example : f(x) Find f(-3)
= (2x
+ 1)3
+ ex

In [31]:

f(x)=(2*x+1)^3+e^x;
f(-3) # not numerical values
Out[31]:
e^(-3) - 125

In [32]:
n(f(-3)) # numerical values

Out[32]:
-124.950212931632

In [ ]:

In [ ]:

f(x) be a polnomial

factor(f) # For factorisation


OR
f.factor()

In [10]:

f(x)=x^3-3*x^2+2*x
factor(f)

Out[10]:
x |--> (x - 1)*(x - 2)*x

In [ ]:

In [11]:

f.factor()
Out[11]:

x |--> (x - 1)*(x - 2)*x

In [ ]:

In [33]:
g(x)=x^2-3
g.factor()

Out[33]:
x |--> x^2 - 3

In [34]:
In [34]:

realpoly.<x>=PolynomialRing(RR) # declare real polynomial


factor(x^2-3)
Out[34]:

(x - 1.73205080756888) * (x + 1.73205080756888)

In [ ]:

In [ ]:
To expand the function :

Use : f.expand()

In [ ]:

Function f(x)
= (2x
+ 1)7

In [35]:

f(x)=(2*x+1)^7
f.expand()

Out[35]:
x |--> 128*x^7 + 448*x^6 + 672*x^5 + 560*x^4 + 280*x^3 + 84*x^2 + 14*x + 1

In [ ]:

Factors of two variable Function f(x,y)


= x2
− 3xy
− 2y2

In [36]:
sage: f(x,y)=x^2-3*x*y+2*y^2
sage: f.factor()

Out[36]:
(x, y) |--> (x - y)*(x - 2*y)

In [ ]:

Define a function f(x,y,


z)
= 4x2
(x2
+ y2
+ z2
+ z)
+ y2
(y2
+ z2
− 1)

In [37]:

var('x,y,z')
f(x, y, z) = 4*x^2 * (x^2 + y^2 + z^2 + z) + y^2 * (y^2 + z^2 - 1)
f(1,2,3)

Out[37]:
116

Complex Variable
In [ ]:

In [39]:

a=1
b=2
z=a+i*b
z

Out[39]:
2*I + 1

In [ ]:

In [40]:

z.n(30)
Out[40]:

1.0000000 + 2.0000000*I

In [ ]:

In [18]:
factor(x^2+1)

Out[18]:
(x - I) * (x + I)

In [ ]:

In [10]:
complexpoly.<x>=PolynomialRing(CC)
factor(x^2+2)

Out[10]:
(x - 1.41421356237310*I) * (x + 1.41421356237310*I)
In [ ]:

In [ ]:

Several variables can be assigned simultaneously,

which differs from sequential

assignments x = y ; y = x; z=x

In [ ]:

In [20]:
x=2;y=x+3;z=x+y
z

Out[20]:
7

In [ ]:

In [21]:

a, b = 50, 100 # (a, b) = (50, 100) and [50, 100] are also possible
a,b
Out[21]:

(50, 100)

In [ ]:

In [ ]:

The assignment a, b = b, a is equivalent

to swapping the values of a and b

with out using an auxiliary variable:

In [ ]:

In [41]:

x,y=50, 60
x, y = y, x
x, y

Out[41]:
(60, 50)

In [ ]:

In Sage : x=y=z=0 , assigns the same value


In [ ]:

x += k is equivalent to x = x+k

and

and y = k is equivalent to y = y k

In [ ]:

In [23]:

x=10;x=x+10;x
Out[23]:

20

In [24]:
x=10;x+=10;x

Out[24]:
20

In [ ]:

creates a simple array of values


One-dimensional ( n + 1) elements
variable_name = [ a[0],a[1],...... ]
.,a[n]

In [ ]:

In [ ]:

# For array :

x = SR.var('x', n)

where n is a positive integer (notice that indexing starts at 0)

Simply : x=('x', n)

For example :

In [ ]:

In [42]:

x =('x', 10)
x = [1,2,3,4, 5,10,12]
x[0],x[2],x[5]
Out[42]:

(1, 3, 10)
(1, 3, 10)

In [ ]:

In [43]:

(x[0]+x[2])*x[5]
Out[43]:

40

In [ ]:

In [39]:
# We could have assigned z to another variable than y:

var('z')
2*z+3
y = var('z')
2*y + 3
Out[39]:

2*z + 3

In [ ]:

Creates Two-dimensioal array of values


mn elements
variable_name = [(a[0][0],a[0][1]),
,.......,
(a[m − 1][n − 1]
]

In [ ]:

Matrices
Writing a matrix order mxn
A
=
⎛ a11 a12 ⋯ a1n ⎞

⎜ 21
a a22 ⋯ a2n ⎟


⎜ ⎟

⎜ ⋮ ⋮ ⋱ ⋮ ⎟
⎝a am2 ⋯ amn ⎠
m1

In [ ]:
Example :
A
=
⎛ 10 −2 5
⎜4

20 −10
⎜6 −7 15
⎝1 2 3




In [ ]:

In [44]:
A=matrix(ZZ,[[10,-2,5],[4,20,-10],[6,-7,15],[1,2,3]]) # Ring of Integer
A
Out[44]:
[ 10 -2 5]
[ 4 20 -10]
[ 6 -7 15]
[ 1 2 3]

In [45]:

show(A)

⎛ 10 −2 5 ⎞


4 20 −10 ⎟

⎜6 −7 15 ⎟
⎝1 2 3 ⎠

You might also like