321
|
1 |
Symbolics with Sage
|
|
2 |
-------------------
|
|
3 |
|
|
4 |
Hello friends and welcome to the tutorial on symbolics with sage.
|
|
5 |
|
342
|
6 |
{{{ Show welcome slide }}}
|
321
|
7 |
|
|
8 |
|
|
9 |
.. #[Madhu: What is this line doing here. I don't see much use of it]
|
|
10 |
|
|
11 |
During the course of the tutorial we will learn
|
|
12 |
|
342
|
13 |
{{{ Show outline slide }}}
|
321
|
14 |
|
342
|
15 |
* Defining symbolic expressions in sage.
|
|
16 |
* Using built-in costants and functions.
|
|
17 |
* Performing Integration, differentiation using sage.
|
|
18 |
* Defining matrices.
|
|
19 |
* Defining Symbolic functions.
|
|
20 |
* Simplifying and solving symbolic expressions and functions.
|
321
|
21 |
|
342
|
22 |
We can use Sage for symbolic maths.
|
321
|
23 |
|
|
24 |
On the sage notebook type::
|
|
25 |
|
|
26 |
sin(y)
|
|
27 |
|
|
28 |
It raises a name error saying that y is not defined. But in sage we
|
|
29 |
can declare y as a symbol using var function.
|
|
30 |
|
342
|
31 |
|
321
|
32 |
::
|
|
33 |
var('y')
|
|
34 |
|
|
35 |
Now if you type::
|
|
36 |
|
|
37 |
sin(y)
|
|
38 |
|
342
|
39 |
sage simply returns the expression.
|
|
40 |
|
321
|
41 |
|
342
|
42 |
Thus sage treats sin(y) as a symbolic expression . We can use
|
|
43 |
this to do symbolic maths using sage's built-in constants and
|
|
44 |
expressions..
|
321
|
45 |
|
|
46 |
|
342
|
47 |
So let us try ::
|
|
48 |
|
|
49 |
var('x,alpha,y,beta')
|
|
50 |
x^2/alpha^2+y^2/beta^2
|
|
51 |
|
|
52 |
taking another example
|
|
53 |
|
|
54 |
var('theta')
|
|
55 |
sin^2(theta)+cos^2(theta)
|
321
|
56 |
|
342
|
57 |
|
|
58 |
Similarly, we can define many algebraic and trigonometric expressions
|
321
|
59 |
using sage .
|
|
60 |
|
|
61 |
|
|
62 |
Sage also provides a few built-in constants which are commonly used in
|
|
63 |
mathematics .
|
|
64 |
|
342
|
65 |
example : pi,e,infinity , Function n gives the numerical values of all these
|
321
|
66 |
constants.
|
|
67 |
|
342
|
68 |
{{{ Type n(pi)
|
|
69 |
n(e)
|
|
70 |
n(oo)
|
|
71 |
On the sage notebook }}}
|
321
|
72 |
|
342
|
73 |
|
321
|
74 |
|
342
|
75 |
If you look into the documentation of function "n" by doing
|
321
|
76 |
|
|
77 |
.. #[Madhu: "documentation of the function "n"?]
|
|
78 |
|
|
79 |
::
|
|
80 |
n(<Tab>
|
|
81 |
|
342
|
82 |
You will see what all arguments it takes and what it returns. It will be very
|
|
83 |
helpful if you look at the documentation of all functions introduced through
|
|
84 |
this script.
|
321
|
85 |
|
342
|
86 |
|
321
|
87 |
|
342
|
88 |
Also we can define the no. of digits we wish to use in the numerical
|
321
|
89 |
value . For this we have to pass an argument digits. Type
|
|
90 |
|
|
91 |
.. #[Madhu: "no of digits"? Also "We wish to obtain" than "we wish to
|
|
92 |
use"?]
|
|
93 |
::
|
|
94 |
|
|
95 |
n(pi, digits = 10)
|
|
96 |
|
|
97 |
Apart from the constants sage also has a lot of builtin functions like
|
342
|
98 |
sin,cos,log,factorial,gamma,exp,arcsin etc ...
|
|
99 |
lets try some of them out on the sage notebook.
|
321
|
100 |
|
342
|
101 |
|
321
|
102 |
::
|
|
103 |
|
|
104 |
sin(pi/2)
|
|
105 |
|
|
106 |
arctan(oo)
|
|
107 |
|
|
108 |
log(e,e)
|
|
109 |
|
|
110 |
|
|
111 |
Given that we have defined variables like x,y etc .. , We can define
|
|
112 |
an arbitrary function with desired name in the following way.::
|
|
113 |
|
342
|
114 |
var('x')
|
|
115 |
function('f',x)
|
321
|
116 |
|
|
117 |
|
|
118 |
Here f is the name of the function and x is the independent variable .
|
|
119 |
Now we can define f(x) to be ::
|
|
120 |
|
|
121 |
f(x) = x/2 + sin(x)
|
|
122 |
|
|
123 |
Evaluating this function f for the value x=pi returns pi/2.::
|
|
124 |
|
|
125 |
f(pi)
|
|
126 |
|
|
127 |
We can also define functions that are not continuous but defined
|
342
|
128 |
piecewise. Let us define a function which is a parabola between 0
|
|
129 |
to 1 and a constant from 1 to 2 . Type the following as given on the
|
321
|
130 |
screen
|
|
131 |
|
|
132 |
::
|
|
133 |
|
|
134 |
|
342
|
135 |
var('x')
|
|
136 |
h(x)=x^2 g(x)=1
|
|
137 |
f=Piecewise(<Tab>
|
|
138 |
|
|
139 |
{{{ Show the documentation of Piecewise }}}
|
|
140 |
|
|
141 |
::
|
321
|
142 |
f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
342
|
147 |
We can also define functions which are series
|
321
|
148 |
|
|
149 |
|
|
150 |
We first define a function f(n) in the way discussed above.::
|
|
151 |
|
342
|
152 |
var('n')
|
|
153 |
function('f', n)
|
321
|
154 |
|
|
155 |
|
|
156 |
To sum the function for a range of discrete values of n, we use the
|
|
157 |
sage function sum.
|
|
158 |
|
|
159 |
For a convergent series , f(n)=1/n^2 we can say ::
|
|
160 |
|
342
|
161 |
var('n')
|
|
162 |
function('f', n)
|
321
|
163 |
|
|
164 |
f(n) = 1/n^2
|
|
165 |
|
|
166 |
sum(f(n), n, 1, oo)
|
|
167 |
|
342
|
168 |
|
|
169 |
Lets us now try another series ::
|
321
|
170 |
|
|
171 |
|
|
172 |
f(n) = (-1)^(n-1)*1/(2*n - 1)
|
342
|
173 |
sum(f(n), n, 1, oo)
|
321
|
174 |
|
|
175 |
|
342
|
176 |
This series converges to pi/4.
|
321
|
177 |
|
|
178 |
|
342
|
179 |
Moving on let us see how to perform simple calculus operations using Sage
|
321
|
180 |
|
|
181 |
For example lets try an expression first ::
|
|
182 |
|
342
|
183 |
diff(x**2+sin(x),x)
|
|
184 |
2x+cos(x)
|
321
|
185 |
|
342
|
186 |
The diff function differentiates an expression or a function. Its
|
321
|
187 |
first argument is expression or function and second argument is the
|
342
|
188 |
independent variable.
|
321
|
189 |
|
|
190 |
We have already tried an expression now lets try a function ::
|
|
191 |
|
342
|
192 |
f=exp(x^2)+arcsin(x)
|
|
193 |
diff(f(x),x)
|
321
|
194 |
|
342
|
195 |
To get a higher order differential we need to add an extra third argument
|
321
|
196 |
for order ::
|
|
197 |
|
|
198 |
diff(<tab> diff(f(x),x,3)
|
|
199 |
|
|
200 |
in this case it is 3.
|
|
201 |
|
|
202 |
|
|
203 |
Just like differentiation of expression you can also integrate them ::
|
|
204 |
|
342
|
205 |
x = var('x')
|
|
206 |
s = integral(1/(1 + (tan(x))**2),x)
|
|
207 |
s
|
321
|
208 |
|
342
|
209 |
|
321
|
210 |
|
342
|
211 |
Many a times we need to find factors of an expression ,we can use the "factor" function
|
321
|
212 |
|
|
213 |
::
|
342
|
214 |
factor(<tab>
|
|
215 |
y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2)
|
|
216 |
f = factor(y)
|
321
|
217 |
|
342
|
218 |
One can simplify complicated expression ::
|
|
219 |
|
321
|
220 |
f.simplify_full()
|
|
221 |
|
342
|
222 |
This simplifies the expression fully . We can also do simplification
|
321
|
223 |
of just the algebraic part and the trigonometric part ::
|
|
224 |
|
342
|
225 |
f.simplify_exp()
|
|
226 |
f.simplify_trig()
|
321
|
227 |
|
342
|
228 |
|
321
|
229 |
|
|
230 |
One can also find roots of an equation by using find_root function::
|
|
231 |
|
342
|
232 |
phi = var('phi')
|
|
233 |
find_root(cos(phi)==sin(phi),0,pi/2)
|
321
|
234 |
|
|
235 |
Lets substitute this solution into the equation and see we were
|
|
236 |
correct ::
|
|
237 |
|
342
|
238 |
var('phi')
|
|
239 |
f(phi)=cos(phi)-sin(phi)
|
|
240 |
root=find_root(f(phi)==0,0,pi/2)
|
|
241 |
f.substitute(phi=root)
|
321
|
242 |
|
342
|
243 |
as we can see when we substitute the value the answer is almost = 0 showing
|
|
244 |
the solution we got was correct.
|
321
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
342
|
249 |
Lets us now try some matrix algebra symbolically ::
|
|
250 |
|
|
251 |
|
321
|
252 |
|
342
|
253 |
var('a,b,c,d')
|
|
254 |
A=matrix([[a,1,0],[0,b,0],[0,c,d]])
|
|
255 |
A
|
321
|
256 |
|
|
257 |
Now lets do some of the matrix operations on this matrix
|
|
258 |
|
|
259 |
|
342
|
260 |
::
|
|
261 |
A.det()
|
|
262 |
A.inverse()
|
321
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
{{{ Part of the notebook with summary }}}
|
|
267 |
|
|
268 |
So in this tutorial we learnt how to
|
|
269 |
|
|
270 |
|
342
|
271 |
* We learnt about defining symbolic expression and functions.
|
|
272 |
* Using built-in constants and functions.
|
|
273 |
* Using <Tab> to see the documentation of a function.
|
|
274 |
* Simple calculus operations .
|
|
275 |
* Substituting values in expression using substitute function.
|
|
276 |
* Creating symbolic matrices and performing operation on them .
|
321
|
277 |
|