author | Nishanth <nishanth@fossee.in> |
Thu, 23 Sep 2010 11:00:44 +0530 | |
changeset 191 | 08b2cb94c57c |
parent 187 | 3b912b3fdcbf |
child 205 | d95288e57cfc |
permissions | -rw-r--r-- |
148 | 1 |
Symbolics with Sage |
2 |
------------------- |
|
3 |
||
4 |
This tutorial on using Sage for symbolic calculation is brought to you |
|
5 |
by Fossee group. |
|
6 |
||
7 |
{{{ Part of Notebook with title }}} |
|
8 |
||
9 |
We would be using simple mathematical functions on the sage notebook |
|
187
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
10 |
for this tutorial. |
148 | 11 |
|
12 |
During the course of the tutorial we will learn |
|
13 |
||
14 |
{{{ Part of Notebook with outline }}} |
|
15 |
||
187
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
16 |
To define symbolic expressions in sage. Use built-in costants and |
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
17 |
function. Integration, differentiation using sage. Defining |
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
18 |
matrices. Defining Symbolic functions. Simplifying and solving |
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
19 |
symbolic expressions and functions. |
148 | 20 |
|
187
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
21 |
.. #[Nishanth]: The formatting is all messed up |
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
22 |
First fix the formatting and compile the rst |
3b912b3fdcbf
Included a comment in symbolics
Nishanth <nishanth@fossee.in>
parents:
148
diff
changeset
|
23 |
The I shall review |
148 | 24 |
|
25 |
Using sage we can perform mathematical operations on symbols . |
|
26 |
||
27 |
On the sage notebook type:: |
|
28 |
||
29 |
sin(y) |
|
30 |
||
31 |
It raises a name error saying that y is not defined . But in sage we |
|
32 |
can declare y as a symbol using var function. :: |
|
33 |
||
34 |
var('y') |
|
35 |
||
36 |
Now if you type:: |
|
37 |
||
38 |
sin(y) |
|
39 |
||
40 |
sage simply returns the expression . |
|
41 |
||
42 |
thus now sage treats sin(y) as a symbolic expression . You can use |
|
43 |
this to do a lot of symbolic maths using sage's built-in constants and |
|
44 |
expressions . |
|
45 |
||
46 |
Try out :: |
|
47 |
||
48 |
var('x,alpha,y,beta') x^2/alpha^2+y^2/beta^2 |
|
49 |
||
50 |
Similarly , we can define many algebraic and trigonometric expressions |
|
51 |
using sage . |
|
52 |
||
53 |
||
54 |
||
55 |
Sage also provides a few built-in constants which are commonly used in |
|
56 |
mathematics . |
|
57 |
||
58 |
example : pi,e,oo , Function n gives the numerical values of all these |
|
59 |
constants. |
|
60 |
||
61 |
For instance:: |
|
62 |
||
63 |
n(e) |
|
64 |
||
65 |
2.71828182845905 |
|
66 |
||
67 |
gives numerical value of e. |
|
68 |
||
69 |
If you look into the documentation of n by doing :: |
|
70 |
||
71 |
n(<Tab> |
|
72 |
||
73 |
You will see what all arguments it can take etc .. It will be very |
|
74 |
helpful if you look at the documentation of all functions introduced |
|
75 |
||
76 |
||
77 |
Also we can define the no of digits we wish to use in the numerical |
|
78 |
value . For this we have to pass an argument digits. Type:: |
|
79 |
||
80 |
n(pi, digits = 10) |
|
81 |
||
82 |
Apart from the constants sage also has a lot of builtin functions like |
|
83 |
sin,cos,sinh,cosh,log,factorial,gamma,exp,arcsin,arccos,arctan etc ... |
|
84 |
lets try some out on the sage notebook. :: |
|
85 |
||
86 |
sin(pi/2) |
|
87 |
||
88 |
arctan(oo) |
|
89 |
||
90 |
log(e,e) |
|
91 |
||
92 |
||
93 |
Given that we have defined variables like x,y etc .. , We can define |
|
94 |
an arbitrary function with desired name in the following way.:: |
|
95 |
||
96 |
var('x') function(<tab> {{{ Just to show the documentation |
|
97 |
extend this line }}} function('f',x) |
|
98 |
||
99 |
Here f is the name of the function and x is the independent variable . |
|
100 |
Now we can define f(x) to be :: |
|
101 |
||
102 |
f(x) = x/2 + sin(x) |
|
103 |
||
104 |
Evaluating this function f for the value x=pi returns pi/2.:: |
|
105 |
||
106 |
f(pi) |
|
107 |
||
108 |
We can also define function that are not continuous but defined |
|
109 |
piecewise. We will be using a function which is a parabola between 0 |
|
110 |
to 1 and a constant from 1 to 2 . type the following as given on the |
|
111 |
screen:: |
|
112 |
||
113 |
||
114 |
var('x') h(x)=x^2 g(x)=1 f=Piecewise(<Tab> {{{ Just to show the |
|
115 |
documentation extend this line }}} |
|
116 |
f=Piecewise([[(0,1),h(x)],[(1,2),g(x)]],x) f |
|
117 |
||
118 |
Checking f at 0.4, 1.4 and 3 :: f(0.4) f(1.4) f(3) |
|
119 |
||
120 |
for f(3) it raises a value not defined in domain error . |
|
121 |
||
122 |
||
123 |
Apart from operations on expressions and functions one can also use |
|
124 |
them for series . |
|
125 |
||
126 |
We first define a function f(n) in the way discussed above.:: |
|
127 |
||
128 |
var('n') function('f', n) |
|
129 |
||
130 |
||
131 |
To sum the function for a range of discrete values of n, we use the |
|
132 |
sage function sum. |
|
133 |
||
134 |
For a convergent series , f(n)=1/n^2 we can say :: |
|
135 |
||
136 |
var('n') function('f', n) |
|
137 |
||
138 |
f(n) = 1/n^2 |
|
139 |
||
140 |
sum(f(n), n, 1, oo) |
|
141 |
||
142 |
For the famous Madhava series :: var('n') function('f', n) |
|
143 |
||
144 |
f(n) = (-1)^(n-1)*1/(2*n - 1) |
|
145 |
||
146 |
This series converges to pi/4. It was used by ancient Indians to |
|
147 |
interpret pi. |
|
148 |
||
149 |
For a divergent series, sum would raise a an error 'Sum is |
|
150 |
divergent' :: |
|
151 |
||
152 |
var('n') |
|
153 |
function('f', n) |
|
154 |
f(n) = 1/n sum(f(n), n,1, oo) |
|
155 |
||
156 |
||
157 |
||
158 |
||
159 |
We can perform simple calculus operation using sage |
|
160 |
||
161 |
For example lets try an expression first :: |
|
162 |
||
163 |
diff(x**2+sin(x),x) 2x+cos(x) |
|
164 |
||
165 |
The diff function differentiates an expression or a function . Its |
|
166 |
first argument is expression or function and second argument is the |
|
167 |
independent variable . |
|
168 |
||
169 |
We have already tried an expression now lets try a function :: |
|
170 |
||
171 |
f=exp(x^2)+arcsin(x) diff(f(x),x) |
|
172 |
||
173 |
To get a higher order differentiation we need to add an extra argument |
|
174 |
for order :: |
|
175 |
||
176 |
diff(<tab> diff(f(x),x,3) |
|
177 |
||
178 |
||
179 |
in this case it is 3. |
|
180 |
||
181 |
||
182 |
Just like differentiation of expression you can also integrate them :: |
|
183 |
||
184 |
x = var('x') s = integral(1/(1 + (tan(x))**2),x) s |
|
185 |
||
186 |
||
187 |
||
188 |
To find factors of an expression use the function factor |
|
189 |
||
190 |
factor(<tab> y = (x^100 - x^70)*(cos(x)^2 + cos(x)^2*tan(x)^2) f = |
|
191 |
factor(y) |
|
192 |
||
193 |
One can also simplify complicated expression using sage :: |
|
194 |
f.simplify_full() |
|
195 |
||
196 |
This simplifies the expression fully . You can also do simplification |
|
197 |
of just the algebraic part and the trigonometric part :: |
|
198 |
||
199 |
f.simplify_exp() f.simplify_trig() |
|
200 |
||
201 |
||
202 |
One can also find roots of an equation by using find_root function:: |
|
203 |
||
204 |
phi = var('phi') find_root(cos(phi)==sin(phi),0,pi/2) |
|
205 |
||
206 |
Lets substitute this solution into the equation and see we were |
|
207 |
correct :: |
|
208 |
||
209 |
var('phi') f(phi)=cos(phi)-sin(phi) |
|
210 |
root=find_root(f(phi)==0,0,pi/2) f.substitute(phi=root) |
|
211 |
||
212 |
||
213 |
as we can see the solution is almost equal to zero . |
|
214 |
||
215 |
||
216 |
We can also define symbolic matrices :: |
|
217 |
||
218 |
||
219 |
||
220 |
var('a,b,c,d') A=matrix([[a,1,0],[0,b,0],[0,c,d]]) A |
|
221 |
||
222 |
||
223 |
Now lets do some of the matrix operations on this matrix :: |
|
224 |
||
225 |
||
226 |
A.det() A.inverse() |
|
227 |
||
228 |
You can do :: |
|
229 |
||
230 |
A.<Tab> |
|
231 |
||
232 |
To see what all operations are available |
|
233 |
||
234 |
||
235 |
{{{ Part of the notebook with summary }}} |
|
236 |
||
237 |
So in this tutorial we learnt how to |
|
238 |
||
239 |
||
240 |
We learnt about defining symbolic expression and functions . |
|
241 |
And some built-in constants and functions . |
|
242 |
Getting value of built-in constants using n function. |
|
243 |
Using Tab to see the documentation. |
|
244 |
Also we learnt how to sum a series using sum function. |
|
245 |
diff() and integrate() for calculus operations . |
|
246 |
Finding roots , factors and simplifying expression using find_root(), |
|
247 |
factor() , simplify_full, simplify_exp , simplify_trig . |
|
248 |
Substituting values in expression using substitute function. |
|
249 |
And finally creating symbolic matrices and performing operation on them . |