avery_tutorial_sage_2 #Create some symbolic variables to be used here #Note that x is the only predefined symbolic variable. #Turn on formatted output by checking the Typeset box above var("a b c d y z") #Create a function using basic notation. #Notice the difference between print output and typeset output f(x) = x^2 * sin(x)^2 print 1/x, f(x) 1/x, f(x) 1/x x^2*sin(x)^2 #Remember how to evaluate exact vs floating point expressions f(2); f(2.) #You can differentiate and integrate functions just like ordinary expressions #Note that diff can handle multiple derivatives in sequence D1 = diff(f(x), x, 2) I1 = integrate(f(x), x) I2 = integrate(f(x), x, 0, 2*pi).factor() E1 = (x^3 * y^4).diff(x, 1, y, 2) E2 = (x^3 * y^4).diff(y, 2, x, 1) D1; I1; I2; E1; E2 #x first, then y #y first, then x #Power series using the taylor() function. Let's do a simple one first. #Expand exp(x) around 0 to 5th order taylor(exp(x), x, 0, 5) #Here's a more complicated one expanded to 10th order! taylor(exp(tan(x)), x, 0, 10) #Expand our previously defined function f(x) = x^2 * sin(x)^2 taylor(f(x), x, 0, 12).factor() #Let's expand the potential function phi(x,t) = 1 / sqrt(1 - 2*t*x + x^2) #The coefficients of x^n are n^th order Legendre polynomials in t. #That's why phi(x,t) is called a "generating function" for Legendre polynomials. var("t") phi(x,t) = 1 / sqrt(1 - 2*t*x + x^2) taylor(phi(x,t), x, 0, 5) #Indefinite or definite integral of an expression or function integrate(sin(x)^2, x); integrate(sin(x)^2, x, 0, pi) #Sage represents +infinity as oo. oo; -oo; tan(pi/2); atan(oo) #Integrate exp(-x^2) from -infinity to +infinity integrate(exp(-x^2), x, -oo, oo) #Now do some sums over powers of integers var("n N") sum(n, n, 1, N).factor(); sum(n^2, n, 1, N).factor(); sum(n^3, n, 1, N).factor() #How about some sums over large powers of integers sum(n^8, n, 1, N).factor(); sum(n^10, n, 1, N).factor() #Do some infinite sums over negative powers of integers. #This defines the Riemann zeta function: zeta(k) = sum(1/n^k, n, 1, oo) #These sums occur when integrating photon energy and number distributions sum(1/n^2, n, 1, oo); zeta(2); sum(1/n^3, n, 1, oo); zeta(3); sum(1/n^4, n, 1, oo); zeta(4); #Sum over powers of odd integers and powers of alternating sign integers sum(1/(2*n-1)^2, n, 1, oo); sum( (-1)^(n-1)/n^2, n, 1, oo ) #Summing an infinite series assume(abs(x) < 1); sum(x^n, n, 0, oo); sum(x^(2*n+1), n, 0, oo); sum( x^n/factorial(n), n, 0, oo) #Try evaluating sin(k*pi), cos(k*pi) without knowing that k is an integer var("k") sin(k*pi), cos(k*pi) #Now tell Sage that k is a positive integer. Unfortunately, it doesn't give more info. assume(k, "integer"); assume(k > 0) sin(k*pi), cos(k*pi) #Try integrating sin(m*x) without knowing that m is an integer var("m") integrate(sin(m*x), x, 0, 2*pi).factor() #Now tell Sage that m is an integer and integrate again assume(m, "integer") integrate(sin(m*x), x, 0, 2*pi) #We can find all assumptions using the assumptions() command assumptions() #Or just find out about a particular variable assumptions(k) #You can delete particular assumptions using the forget() function (k > 0).forget(); assumptions(k) #You can also forget all assumptions. forget(); assumptions() #Create variables a and n and tell sage they are positive var("a n"); assume(a > 0); assume(n > 0); assume(n, "integer") #Now we can integrate with them. Note that we forced n to be integer, but the gamma #function does not actually require that. This is a shortcoming of sage. f1 = exp(-a*x) f2 = 1 / (x^2 + a^2) f3 = x^n * exp(-a*x) integrate(f1, x, 0, oo); integrate(f2, x, -oo, oo); integrate(f3, x, 0, oo) #You can list all variables associated with function or expression with the variables() function h(x) = x^2 * cos(x) g(a,b) = a^2 + b^2 h().variables(); g.variables() #Sage supports a large number of special functions, including orthogonal polynomials. #Look at standard Legendre polynomials order 0 - 5. #The factor() suffix puts the polynomials in recognizable form P0 = legendre_P(0,x) P1 = legendre_P(1,x) P2 = legendre_P(2,x).factor() P3 = legendre_P(3,x).factor() P4 = legendre_P(4,x).factor() P5 = legendre_P(5,x).factor() P0; P1; P2; P3; P4; P5 #Hermite polynomials are used in the solution of Schrodinger's equation for the harmonic oscillator H0 = hermite(0, H1 = hermite(1, H2 = hermite(2, H3 = hermite(3, H4 = hermite(4, H5 = hermite(5, H0; H1; H2; H3; x) x) x) x) x) x) H4; H5 #Laguerre polynomials are used in the solution of Schrodinger's radial equation for hydrogen L0 = laguerre(0,x) L1 = laguerre(1,x) L2 = laguerre(2,x) L3 = laguerre(3,x) L4 = laguerre(4,x) L5 = laguerre(5,x) L0; L1; L2; L3; L4; L5