# In the tried and true methodology of computer science,
# we always begin learning a new language in the same way:
print("Hello, world!")
# Now, not to tax our engine too much, let's try some arithmetic:
2+2
# So far so good. Well, let's step it up a bit. Can you write
# out the factorial of 20? No? How about the factorial of 1000?
factorial(1000)
# Or, from the ridiculous to the sublime,
# how about some pie. That is, some Pi ... 100 decimal places worth
numerical_approx(pi, prec=335)
# Expanding polynomials is always a challenge. Let's try (1+x)^3
expand((1+x)^3)
# OK. Suppose the exponent is 100
expand((1+x)^100)
# I like that one just for the pretty picture. Even fancier if the exponent is 300
expand((1+x)^300)
# An issue of considerable importance both in pure mathematics and
# in computer security is the difficulty in factoring natural numbers. Now,
# if we were to look at 400-digit integers,
# Sage wouldn't be of much help. But let's take a garden-variet 50-digit integer and
factor(58493045827349573470893572173482937623975243713879)
# Solving equations. For equations in one variable
# linear and quadratic are too easy, so
var('x')
solve(x^3-3*x^2+3*x+9==0,x)
# Let's write out a sum. Say, the odd numbers up to 101.
# Using range, the odd numbers beginning at 1 and ending just before 103.
M = range(1,103,2); M
# Now add them
P = sum(M); P
# By the way
sqrt(P)
# Or, a symbolic sum
Q = [x^i for i in range(1,103,2)];R = sum(Q); R
# Let's move on to differential calculus.
diff(sin(x),x)
diff(sin(x)*(1/x),x)
# Or a fifth derivative
diff(sin(x)*(1/x),x,5)
# Or take the derivative of a previous sum
diff(R,x)
# If derivatives, why not indefinite integrals?
integral(1/x,x)
# Well, we omitted the constant, but who cares?
#
# And, of course, the definite integral
integral(1/x,x,1,10)
# Note that the definite integral solution is described by the log function.
# However,we can compute log(10) with 100 bits of precision.
log(10).n(100)
# Of course, we can take limits
limit(sin(x)/x,x=0)
# Let's go back to the integers,
# and calculate an integer division, or modular arithmetic.
mod(238472368426,1241246186)
# Alternatively,
238472368426 % 1241246186
# Finding the modular inverse is also possible
inverse_mod(45,97)
mod(45*69,97)
# We next illustrate the extended Euclidean algorithm,
# the Mobius function,
# and the Chinese remainder theorem
d,u,v = xgcd(12,15)
d == u*12 + v*15
d, u, v
prime_divisors(1001)
phi = 1001*prod([1-1/p for p in prime_divisors(1001)]); phi
euler_phi(1001)
# Finally we illustrate the Chinese remainder theorem
x = crt(2,1,3,5); x
x % 3
x % 5
x % 7
# Better still to calculate a^b (mod c), in this example the inverse of 23 mod 9973
mod(23^(-1),9973)
# Check
mod(23*7805,9973)
# Series
# For the terms up to x^20 of the Taylor series for the cosine
g=cos(x)
g.taylor(x,0,20)
# Matrices
# Here are two 3 x 3 matrices over the integers
A = matrix(ZZ,3,[1,2,3,4,5,6,7,8,10]); A
B = matrix(ZZ,3,[9,6,3,8,5,2,7,4,-1]); B
# Let's multiply them
A * B
# A few other matrix computations
A.determinant()
B.determinant()
A.determinant()*B.determinant()
C=A*B
C.determinant()
A.inverse()
A.eigenvalues()
A^10
A*A*A*A*A*A*A*A*A*A
# From the matrix A above, solve AX=D where D = [10,11,12]
D = vector([10, 11, 12]); D
A.solve_right(D)
G=PermutationGroup([(1,2,3,4,5)])
G.order()
# Abstract algebra. List all the elements of S5
# the symmetric group on 5 elements,
# That is, all permutations
permutations([1,2,3,4,5])
# Displaying a graph
d = {0: [1,4,5], 1: [2,6], 2: [3,7], 3: [4,8], 4: [9], \
5: [7, 8], 6: [8,9], 7: [9]}
G = Graph(d); G
G.plot().show()
# Un hommage a Pascal
#
for j in range(1,20):
R = [binomial(j,i) for i in range (0,j+1)];R
# Signore Fibonacci
Q = [fibonacci(i) for i in range(1,101)]; Q
# Let's graph some functions
plot(cos, (-5,5))
plot(sin,(-10,10))
plot(log,(1,10))
y = var('y')
plot3d(x^2 + y^2, (x,-2,2), (y,-2,2))
# Typesetting
# Let's write Sage output into Latex form
k = 1/(sqrt(3)*I + 3/4 + sqrt(73)*5/9); k
latex(k)
# Writing programs in Sage
#
# Sage has a fairly standard syntax. It shouldn't be too
# unfamiliar to persons who write in any of many procedural languages
#
print("Hello, World!")
name = "Wayne"
print "Hello,",name,"!"
print "Count to ten:"
for i in range(1,11):
print(i)