# Sage does not recognise two expressions as equal
bool(arctan(1+abs(x)) == pi/2 - arctan(1/(1+abs(x))))
x = var('x')
plot( arctan(1+abs(x)) + arctan(1/(1+abs(x))) , (x, -5, 5))
a, x = var('a, x')
y = cos(x+a) * (x+1); y
y.subs(a=-x)
y.subs(x=pi/2, a=pi/3)
y.subs(x=0.5, a=2.3)
y(a=-x)
y(x=pi/2, a=pi/3)
y(x=0.5, a=2.3)
x, y, z = var('x, y, z')
q = x*y + y*z + z*x
bool(q(x=y, y=z, z=x) == q), bool(q(z=y)(y=x) == 3*x^2)
y, z = var('y, z'); f = x^3 + y^2 + z
f.substitute(x^3 == y^2, z==1)
f(x)=(2*x+1)^3
f(-3)
f.expand()
y = var('y')
u = sin(x) + x*cos(y)
v = u.function(x, y)
v
w(x, y) = u
w
x, y = SR.var('x,y')
p = (x+y)*(x+1)^2
p2 = p.expand()
p2
p2.collect(x)
((x+y+sin(x))^2).expand().collect(sin(x))
(x^x/x).simplify()
f = (e^x-1) / (1+e^(x/2))
f.canonicalize_radical()
f = cos(x)^6 + sin(x)^6 + 3 * sin(x)^2 * cos(x)^2
f.simplify_trig()
f = cos(x)^6
f.reduce_trig().show()
f = sin(5 * x)
f.expand_trig().show()
n = var('n')
f = factorial(n+1)/factorial(n)
f.simplify_factorial()
f = sqrt(abs(x)^2)
f.canonicalize_radical()
f = log(x*y)
f.canonicalize_radical()
forget()
assume(x > 0)
bool(sqrt(x^2) == x)
forget(x > 0)
bool(sqrt(x^2) == x)
n = var('n')
assume(n, 'integer')
sin(n*pi)
a = var('a')
c = (a+1)^2 - (a^2+2*a+1) # c = 0
eq = c * x == 0
eq
eq2 = eq / c # divide by zero
eq2
solve(eq2, x)
solve(eq, x) # Sage avoids this mistake.
expand(c)
c = cos(a)^2 + sin(a)^2 - 1 # c = 0
eq = c*x == 0
solve(eq, x) # Sage does not avoid the pitfall.
c.simplify_trig()
c.is_zero()
z, phi = var('z, phi')
eq = z**2 - 2/cos(phi)*z + 5/cos(phi)**2 - 4 == 0
eq.show()
eq.lhs()
eq.rhs()
sols = solve(eq, z)
show(sols)
y = var('y')
sols = solve(y^7==y, y)
show(sols)
solve(x^2-1, x, solution_dict=True) # dictionary
solve([x+y == 3, 2*x+2*y == 6], x, y) # System of two equations
solve([cos(x)*sin(x) == 1/2, x+y == 0], x, y)
sols = solve(x^2+x-1 > 0, x) # inequalities
show(sols)
x, y, z = var('x, y, z')
sols = solve([x^2 * y * z == 18, x * y^3 * z == 24, x * y * z^4 == 6], x, y, z)
for sol in sols:
print(sol)
len(sols) # 17 solutions, among which 16 are approximate complex solutions
expr = sin(x) + sin(2 * x) + sin(3 * x)
solve(expr, x)
find_root(expr, 0.1, pi) # numerical solution
f = expr.simplify_trig()
f
solve(f, x)
sols = (x^3+2*x+1).roots(x) # with their multiplicity
show(sols)
(x^3+2*x+1).roots(x, ring=RR) # RR means "Real Field with 53 bits of precision"
(x^3+2*x+1).roots(x, ring=CC) # CC means "Complex Field with 53 bits of precision"
solve(x^(1/x)==(1/x)^x, x) # no explicit solution
y = function('y')(x)
y
desolve(diff(y,x,x) + x*diff(y,x) + y == 0, y, [0,0,1]).show()
k, n = var('k, n')
sum(k, k, 1, n).factor().show()
n, k, y = var('n, k, y')
sum(binomial(n,k) * x^k * y^(n-k), k, 0, n).show()
k, n = var('k, n')
(
sum(binomial(n,k), k, 0, n),
sum(k * binomial(n, k), k, 0, n),
sum((-1)^k*binomial(n,k), k, 0, n),
)
a, q, k, n = var('a, q, k, n')
sum(a*q^k, k, 0, n).show()
forget()
assume(abs(q) < 1)
sum(a*q^k, k, 0, oo).show() # oo means infinity
forget()
assume(q > 1)
# sum(a*q^k, k, 0, infinity) # error since the sum is divergent.
Let us compute the following limits:
limit((x**(1/3) - 2) / ((x + 19)**(1/3) - 3), x = 8)
f(x) = (cos(pi/4-x)-tan(x))/(1-sin(pi/4 + x))
limit(f(x), x = pi/4)
limit(f(x), x = pi/4, dir='minus')
limit(f(x), x = pi/4, dir='plus')
Example. (A sequence study) Let us consider the sequence $$u_n = \frac{n^{100}}{100^n}.$$
u(n) = n^100 / 100^n
u(1.), u(2.), u(3.), u(4.), u(5.), u(6.), u(7.), u(8.), u(9.), u(10.)
plot(u(x), x, 1, 40)
v(x) = diff(u(x), x)
sol = solve(v(x) == 0, x)
sol
floor(sol[0].rhs())
limit(u(n), n=infinity)
n0 = find_root(u(n) - 1e-8 == 0, 22, 1000)
n0
Let us determine the power series expansion of the following functions:
((1+arctan(x))^(1/x)).series(x==0, 3).show()
(ln(2*sin(x))).series(x==pi/6, 3).show()
(ln(2*sin(x))).series(x==pi/6, 3).truncate().show()
$(x^3 + x)^{\frac{1}{2}} - (x^3 - x)^{\frac{1}{3}}$ behaves around $+\infty$.
taylor((x**3+x)**(1/3) - (x**3-x)**(1/3), x, infinity, 10)
Example. (Machin’s formula) Prove the following formula: $$\frac{\pi}{4} = 4 \arctan \frac{1}{5} - \arctan \frac{1}{239}$$
tan(4*arctan(1/5)).simplify_trig()
tan(pi/4+arctan(1/239)).simplify_trig()
f = arctan(x).series(x, 10)
f.show()
(
(16*f.subs(x==1/5) - 4*f.subs(x==1/239)).n(),
pi.n(),
)
Example. (Evaluation of the Riemann zeta function)
k = var('k')
expr = (
sum(1/k^2, k, 1, infinity),
sum(1/k^4, k, 1, infinity),
sum(1/k^5, k, 1, infinity),
)
show(expr)
Example. (A formula due to Ramanujan) Using the first $12$ terms of the following series, we give an approximation of and we compare it with the value given by Sage. $$\frac{1}{\pi} = \frac{2\sqrt{2}}{9801} \sum_{k=0}^{\infty} \frac{(4k)! \cdot (1103+26390 k)}{(k!)^4 \cdot 396^{4k}}$$
s = 2*sqrt(2)/9801 * (
sum(
( factorial(4*k) * (1103+26390*k) ) / ((factorial(k))^4 * 396^(4*k))
for k in (0..11)
)
)
(1/s).n(digits=100)
(pi-1/s).n(digits=100).n()
# the partial sum of the first 12 terms already yields 95 significant digits of pi.
Example. (Convergence of a series) Let us study the convergence of the series $$\sum_{n\geq 0 } \sin\left( \pi \sqrt{4n^2 + 1} \right)$$
n = var('n')
u = sin(pi*(sqrt(4*n^2+1)-2*n))
taylor(u, n, infinity, 3).show()
We deduce $u_n \sim \frac{\pi}{4}$, so the series $\sum_{n\geq 0} u_n$ diverges.
diff(sin(x^2), x)
reset()
f(x) = function('f')(x)
g(x) = function('g')(x)
diff(f(g(x)), x).show()
diff(ln(f(x)), x)
f(x,y) = x*y + sin(x^2) + e^(-x)
derivative(f, x)
derivative(f, y) # or diff(f, y), or f.diff(y)
Let us check that the following function is harmonic: $$f(x,y) = \frac{1}{2} \ln(x^2 + y^2) \quad \text{for all $(x,y) \neq (0,0)$.}$$ Note that a function $f$ is said harmonic when its Laplacian $\Delta f = \partial_1^2 f + \partial_2^2 f$.
x, y = var('x, y')
f = ln(x**2+y**2) / 2
delta = diff(f,x,2) + diff(f,y,2)
delta.simplify_rational()
sin(x).integral(x, 0, pi/2)
integrate(1/(1+x^2), x)
integrate(1/(1+x^2), x, -infinity, infinity)
integrate(exp(-x**2), x, 0, infinity)
# integrate(exp(-x), x, -infinity, infinity) # error since integral is divergent.
Example. Let us compute, for x 2 R, the integral $$\varphi(x) = \int_0^{\infty} \frac{x \cos u }{u^2+x^2} \ du.$$
forget()
u = var('u')
f = x * cos(u) / (u^2 + x^2)
# f.integrate(u, 0, infinity) # error, Is x positive, negative or zero?
forget()
assume(x > 0)
A = f.integrate(u, 0, infinity, hold = 'True')
show(A == A.unhold())
forget()
assume(x < 0)
A = f.integrate(u, 0, infinity, hold = 'True')
show(A == A.unhold())
We have: $\quad \varphi(x) = \frac{|x|}{x} \frac{\pi}{2} e^{-|x|}$ for all $x \in \mathbb{R}^{*}.$
integral_numerical(sin(x)/x, 0, 1) # ( the answer, an error estimate )
g = integrate(exp(-x**2), x, 0, infinity)
g, g.n()
approx = integral_numerical(exp(-x**2), 0, infinity)
approx
approx[0]-g.n()
A = matrix(QQ, [[1,2],[3,4]])
A
show(A) # pretty_print(A)
B = A.inverse()
show(B)
A = matrix(QQ, [[2,4,3],[-4,-6,-3],[3,3,1]])
A.characteristic_polynomial()
A.eigenvalues()
A.minimal_polynomial().factor()
eigen = A.eigenvectors_right()
show(eigen)
J = A.jordan_form(transformation=True) # A is triangularisable.
show(J)
Example. Let us diagonalise the matrix $$A = \begin{pmatrix} 1 & −1/2 \\ −1/2 & −1 \\ \end{pmatrix} .$$
A = matrix(QQ, [[1,-1/2],[-1/2,-1]])
# A.jordan_form() # error, A is NOT triangularisable in Rational Field.
A = matrix(QQ, [[1,-1/2],[-1/2,-1]])
A.minimal_polynomial()
R = QQ[sqrt(5)]
A = A.change_ring(R)
J = A.jordan_form(transformation=True, subdivide=False)
show(J)
Example. Let us diagonalise the matrix $$A = \begin{pmatrix} 2 & \sqrt{6} & \sqrt{2} \\ \sqrt{6} & 3 & \sqrt{3} \\ \sqrt{2} & \sqrt{3} & 1 \\ \end{pmatrix} .$$
K.<sqrt2> = NumberField(x^2 - 2)
L.<sqrt3> = K.extension(x^2 - 3)
A = matrix(L,
[[2, sqrt2*sqrt3, sqrt2],
[sqrt2*sqrt3, 3, sqrt3],
[sqrt2, sqrt3, 1],
]
)
show(A)
J = A.jordan_form(transformation=True)
show(J)