1 + 1
Sage can caluclate with rational numbers. It switches to floating point numbers with the n() method.
13/5
n(13/5)
sin(1), sqrt(2)
sqrt(2).n(), sqrt(2).n(2000) # n() can be used as a method
sin(1.0)
sin(pi/3)
show(sin(pi/3))
_.n(1000) # _ the previous result
111^111
13//5, 13%5
64^(1/6) # root
factor(24) # text completeion with TAB, try it with writing only fact and pressing TAB
exp(2), e^2
e^2.n()
log(e), log(100), log(100.), log(100,10), log(1024,2)
arcsin(1/2)
apple = 10^10 # creating a variable
apple%3
A question symbol ? after a command shows detailed help.
((3 + 3*I)^4, (sqrt(3) + i)^6) # a series with 2 elements, normal parenthesis can omitted
expand(_[0]) # this expands the second expression
l = [1, 3, 2, 4]
l[1:3]
l = range(7); l # with ; we can write multiple commands in a line
list[c:d] from element c to d, d is not included
list[c:d:e] from element c to d with e steps
l[1], l[-1], l[:2], l[4:], l[3:1], l[0:-1], l[0:-1:2]
Similar to how we define sets in mathematics. Example: $\{ f(x) \mid x\in H\}$.
(In sage lists and sets are different, in a set the order of its elements aren't given.)
L = [factor(n) for n in range(10000, 10025)]
print L
[expression for variable in something if condition]
Mathematically: $\{ f(x) \mid x\in H \text{ such that } g(x)\}$.
P = [n for n in range(10000,10100) if is_prime(n)]
print P
3 == 4
3 <> 4, 3 != 4
3 < 4
var('x')
f(x) = x^3
f(3)
var('c')
f(c)
a = f(c^2-3)
expand(a)
a
a = 3
a = a+1
a = a^2
a
a = "this is a text"
a
show(x^2*exp(x))
show(diff(x^2*exp(x), x))
diff(x^2*exp(x), x, 10)
integral(9*x^2*exp(3*x^3), x)
diff(y^2*exp(y), y)
var('y')
diff(y^2*exp(y), y, 10)
solve([x+y==3], x)
var('x y z p q')
eq1 = p+q==5
eq2 = p*x+q*y==-5
eq3 = p*x^2+q*y^2==10
solve([eq1,eq2,eq3], q, x, y)
solve( [2*x + 3*y + 2*z == -1, x + y + z == 0], x, y )
solve( [2*x + 3*y + 2*z == -1, x + y + z == 0], x, y, z )
solve(cos(x)==sin(x), x)
find_root(cos(x)==sin(x), 0, 10)
limit(arctan(-x) + exp(-x)*x^1000, x=oo)
a = 13
if a%2 == 0:
print "even"
else:
print "odd"
f(x,y) = x^3*exp(x^2)*y^3
f.diff(y)
f.integral(x)
def gcd(a,b):
if b==0:
return abs(a)
else:
return gcd(b,a%b)
gcd(1111,1111111111)
Colors = ["Spades", "Diamond", "Hearts", "Clubs"]
Values = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
Cards = Set(Colors).cartesian_product(Set(Values)) #.map(tuple)
Cards.random_element()
Cards.cardinality()
Hands = Subsets(Cards, 5)
Hands.random_element()
Hands.cardinality()
binomial(52,5)
def is_it_a_pair(hand):
return len(set(value for (color, value) in hand)) == 4
hand = Hands.random_element(); hand
is_it_a_pair(hand)
n = -1.1
n.is_unit()
type(n)
polinomQ.<q> = PolynomialRing(QQ)
polinomR.<x> = PolynomialRing(RR)
polinomC.<z> = PolynomialRing(CC)
factor(q^4 + 2*q^2 + 2)
factor(x^4 + 2*x^2 + 2)
factor(z^4 + 2*z^2 + 2)
factor(x^2 - 2)
(q^4 - 3*q^3 + 2*q^2 - q + 1) // (q^2 - 1)
(q^4 - 3*q^3 + 2*q^2 - q + 1) % (q^2 - 1)
(q^2 - 1)*(q^2 - 3*q + 3) - 4*q + 4
p = q^4 - 3*q^3 + 2*q^2 - q + 1
p.is_irreducible()
p.xgcd(q^2-1)
-1/4*p + (1/4*q^2 - 3/4*q + 3/4) * (q^2 - 1)
A = Matrix([[1,2,3],[3,2,1],[1,1,1]]); A
A.LU()
P, L, U = A.LU()
P*L*U
A.rank()
A.nullity()
A.left_kernel() # left zerospace: xA=0
A.right_kernel() # right zerospace: Ax=0
A.row_space()
A.echelon_form()
A.column_space()
B = A.transpose()
B.echelon_form()
B.rref()
v = vector([1,1,-4])
v
A, v*A, A*v
w = vector([3,5,2])
A.solve_right(w)
A \ w
A
A.solve_left(w)
B = A.change_ring(GF(2))
B.echelon_form()
B.base_ring()
plot(cos, (-5,5))
plot([cos,sin], (-5,5), aspect_ratio=0.5)
parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color='red')
var('x')
p = plot(2*x^4-2*x^3+3*x^2-3*x+4, (x,-1,1), color = 'cyan', thickness=3)
p += plot(2*x^4, (x,-1,1), color = 'magenta', thickness=3)
p.show()