This session will provide a hands-on introduction to the basic capabilities of Sage.
This will cover the bulk of the Maple and Mathematica capabilities that are used in Calculus and other undergraduate mathematics courses.
|
|
|
|
|
|
Try a random 200 by 200 matrix with big integer entries. Then calculate its determinant. It could be a big number.
200 x 200 dense matrix over Integer Ring 200 x 200 dense matrix over Integer Ring |
200 x 200 dense matrix over Integer Ring (type 'print a.str()' to see all of the entries) 142936791710378076404599507342377629505 38511081392475790399488276741634170247 200 x 200 dense matrix over Integer Ring (type 'print a.str()' to see all of the entries) 142936791710378076404599507342377629505 38511081392475790399488276741634170247 |
|
|
|
Sage does Calculus:
15*(x^5 - 1)^2*x^4 15*(x^5 - 1)^2*x^4 |
1/16*x^16 - 3/11*x^11 + 1/2*x^6 - x 1/16*x^16 - 3/11*x^11 + 1/2*x^6 - x |
|
|
|
|
![]() |
|
![]() |
|
Let's start with a result by Archimedes, namely that
$\pi < \frac{22}{7}$.
Archimedes used a very geometric approach involving inscribed and circumscribed polygons.
But today we might simply ask Sage, or any other modern Computer Algebra System (CAS), to evaluate the integral
$\int_0^1 \frac{(1-x)^4 x^4}{1+x^2} \,dx$
|
Since the integrand is positive on the interval (0,1), the inequality immediately follows. However, this may not be completely satisfactory until we look at the antiderivative.
$\int \frac{(1-x)^4 x^4}{1+x^2} \,dx$
|
Of course this approach not only depends on modern Computer Algebra Systems. It also depends on the mathematical edifice of Trigonometry and Calculus that led to the wonderful relation
$\int \frac{1}{1+x^2} \,dx = arctan(x)$
We can push further by taking the geometric series
$\frac{1}{1-x} = 1 + x + x^2 + x^3 + \cdots$
which only converges for $|x| < 1$, and replace $x$ by $-x^2$ to get the alternating series
$\frac{1}{1+x^2} = 1 - x^2 + x^4 - x^6 + \cdots$
Then, following James Gregory's lead (1668), integrate term by term to get the series
$\arctan(x) = x - \frac{x^3}{3} + \frac{x^5}{5} - \frac{x^7}{7} + \cdots$
Finally, we can conclude, since we have an alternating series with decreasing terms, that
$\frac{\pi}{4} = \arctan(1) = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots = \sum\limits_{k=0}^\infty {\frac{(-1)^k}{2k+1}}$
Gregory's series can be used to calculate $\pi$, but it has notoriously slow convergence, but studying it with the aid of today's computers has led to some surprising connections.
<type 'sage.rings.real_mpfr.RealNumber'> 3.1415926535897932384626433833 3.1415926535897932384626433832795028841971693993751058209749 3.1415926535897932384626433832795028841971693993751 6.2831853071795864769252867666 6.2831853071795864769252867666 0.00000000000000000000000000000 <type 'sage.rings.real_mpfr.RealNumber'> 3.1415926535897932384626433833 3.1415926535897932384626433832795028841971693993751058209749 3.1415926535897932384626433832795028841971693993751 6.2831853071795864769252867666 6.2831853071795864769252867666 0.00000000000000000000000000000 |
As noted above, Gregory's series for $\pi$, is over 300 years old and has notoriously slow convergence.
$\pi = 4 \sum\limits_{k=0}^\infty {\frac{{( - 1)^k }}{{2k + 1}}}$
Nonetheless, let's take an experimental approach and compute some partial sums with a little more accuracy than pencil and paper or even 12-digit calculators.
3.0418396189294022111359572659882257405477219718706 3.1315929035585527643074142382769205164030543844066 3.1405926538397929259635965028693959704513893307797 3.1414926535900432384595183833748153787870136427442 3.1415906535897932404626433832695028841972913993751 3.1415926535897932384626433832795028841971693993751 3.0418396189294022111359572659882257405477219718706 3.1315929035585527643074142382769205164030543844066 3.1405926538397929259635965028693959704513893307797 3.1414926535900432384595183833748153787870136427442 3.1415906535897932404626433832695028841972913993751 3.1415926535897932384626433832795028841971693993751 |
With 10 terms the second digit is incorrect, but third and fourth happen to be correct -- probably just a curious coincidence.
With 100 terms the second digit is now correct, but the third digit is wrong. On the other hand the next 4 digits, 1592, happen to be OK.
With 1000 terms the third digit is now correct, but the fourth digit is wrong. On the other hand the next 6 digits, 592653, happen to be OK.
With 10,000 terms the fourth digit is now correct, but the fifth digit is wrong. On the other hand the next 6 digits, 926535, happen to be OK.
With a half a million terms we get the fifth and sixth digits correct, but the seventh digit is wrong. On the other hand, the next 10 digits, 6535897932, are correct. The next 2 digits are wrong but the next 10 digits, 4626422832, are correct. After that, we have one wrong digit followed by another 10 correct digits, 9502884198. Then 3 wrong digits, and, wrapping up, the 7 last digits are correct.
Something beyond coincidence is certainly going on here!
Although it took the sophisticated expertise of three mathematicians to explain the phenomenon; the experimental observation that got the ball rolling was by Joseph Roy North of Colorado Springs - a person of commendable curiosity.
Jonathan Borwein, Peter Borwein, and Karl Dilcher, "Pi, Euler Numbers, and Asymptotic Expansions", American Math Monthly, 1989.
1.5607973262955052392565914871547462164608848226857 1.5697963277948916192923203066902697395329262915799 1.5706963267958966191813216977397500570990899094173 1.5707863267948976192313211916397520520985833146876 1.5707953267948966202313216916347514420986456996876 1.5707963267948966192313216916397514420985846996876 1.5607973262955052392565914871547462164608848226857 1.5697963277948916192923203066902697395329262915799 1.5706963267958966191813216977397500570990899094173 1.5707863267948976192313211916397520520985833146876 1.5707953267948966202313216916347514420986456996876 1.5707963267948966192313216916397514420985846996876 |
1.560797326295505239256591487154746216460884822685658023243553968744347 1.569796327794891619292320306690269739532926291579917284073598715948943 1.570696326795896619181321697739750057099089909417276609848259381272946 1.570786326794897619231321191639752052098583314687557962587445268504108 1.570795326794896620231321691634751442098645699687551525487472346674908 1.570796326794896619231321691639751442098584699687552910487472296153908 1.560797326295505239256591487154746216460884822685658023243553968744347 1.569796327794891619292320306690269739532926291579917284073598715948943 1.570696326795896619181321697739750057099089909417276609848259381272946 1.570786326794897619231321191639752052098583314687557962587445268504108 1.570795326794896620231321691634751442098645699687551525487472346674908 1.570796326794896619231321691639751442098584699687552910487472296153908 |
|
The OEIC confirms that these first 6 corrections are indeed the first 6 Euler numbers with alternating signs.
The OEIC also gives us a number of connections and references -- including the paper that first examined this phenomenon.
The bottom line is that we have experimentally established the fact that it is highly likely that the error in the Gregory series has an asymptotic expansion whose coefficients involve the Euler numbers.
In fact, armed with this insight, which they obtained in a very similar fashion, Dilcher and the brothers Borwein showed that the first 6 terms of the asymptotic expansion for even $n$ are:
$\frac{E_0}{2n}$, $\frac{E_2}{(2n)^3}$, $\frac{E_4}{(2n)^5}$, $\frac{E_6}{(2n)^7}$, $\frac{E_8}{(2n)^9}$, and $\frac{E_{10}}{(2n)^{11}}$
where $E_0 = 1$, $E_2 = -1$, $E_4 = 5$, $E_6 = -61$, $E_8 = 1385$, and $E_{10} = -50521$.
|
(x, y, u, v) (x, y, u, v) |
|
|
|
|
Sage can draw 3d plots:
|
|
|
|
Sage can plot Yoda:
|
|
t t |
|
Traceback (click to the left of this block for traceback) ... NameError: name 't' is not defined Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_17.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cGFyYW1ldHJpY19wbG90M2QoW2Nvcyh0KSwgc2luKHQpLCB0XSwgKHQsIDAsIDYgKiBwaSkp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpFv4dnR/___code___.py", line 3, in <module> exec compile(u'parametric_plot3d([cos(t), sin(t), t], (t, _sage_const_0 , _sage_const_6 * pi)) File "", line 1, in <module> NameError: name 't' is not defined |
|
|
Traceback (click to the left of this block for traceback) ... NameError: name 't' is not defined Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_18.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cGFyYW1ldHJpY19wbG90M2QoW3ReMiwgNyAqIHQgLSAxMiwgdF4yXSwgKHQsIDAsIDUpKSBcCiAgICArIHBhcmFtZXRyaWNfcGxvdDNkKFs0ICogdCAtIDMsIHReMiwgNSAqIHQgLSA2XSwgKHQsIDAsIDUpLCBjb2xvciA9ICdyZWQnKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpfOExt6/___code___.py", line 3, in <module> exec compile(u"parametric_plot3d([t**_sage_const_2 , _sage_const_7 * t - _sage_const_12 , t**_sage_const_2 ], (t, _sage_const_0 , _sage_const_5 )) \\\n + parametric_plot3d([_sage_const_4 * t - _sage_const_3 , t**_sage_const_2 , _sage_const_5 * t - _sage_const_6 ], (t, _sage_const_0 , _sage_const_5 ), color = 'red')" + '\n', '', 'single') File "", line 1, in <module> NameError: name 't' is not defined |
|
|
Traceback (click to the left of this block for traceback) ... NameError: name 't' is not defined Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_19.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCdhbHBoYSBiZXRhJykKREUxID0gZGlmZih5LHQpID09IGFscGhhKnkgLSBiZXRhCnNob3coREUxKQp1MSA9IGRlc29sdmUoREUxLCB5LCBpdmFyPXQpCnNob3codTEp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmp_nFvld/___code___.py", line 3, in <module> DE1 = diff(y,t) == alpha*y - beta NameError: name 't' is not defined |
|
c + beta/alpha c*e^alpha + beta/alpha c*e + beta/alpha c*e^(2*alpha) + beta/alpha c + beta/alpha c*e^alpha + beta/alpha c*e + beta/alpha c*e^(2*alpha) + beta/alpha |
t |--> 20*e^(1/2*t) + 80 t |--> 20*e^(1/2*t) + 80 |
|
(k1*sin(1/4*sqrt(31)*t) + k2*cos(1/4*sqrt(31)*t))*e^(-1/4*t) (k1*sin(1/4*sqrt(31)*t) + k2*cos(1/4*sqrt(31)*t))*e^(-1/4*t) |
t |--> 1/2*(sin(1/4*sqrt(31)*t) + 2*cos(1/4*sqrt(31)*t))*e^(-1/4*t) t |--> 1/2*(sin(1/4*sqrt(31)*t) + 2*cos(1/4*sqrt(31)*t))*e^(-1/4*t) |
|
-(log(-P + r(t)) - log(r(t)))/(P*alpha) == c + t -(log(-P + r(t)) - log(r(t)))/(P*alpha) == c + t |
-(P - r(t))/r(t) == K*e^(-P*alpha*t) -(P - r(t))/r(t) == K*e^(-P*alpha*t) |
[r(t) == -P*e^(P*alpha*t)/(K - e^(P*alpha*t))] [r(t) == -P*e^(P*alpha*t)/(K - e^(P*alpha*t))] |
|
|
|
|
Sage can do graph theory:
Flower Snark: Graph on 20 vertices Flower Snark: Graph on 20 vertices |
Traceback (click to the left of this block for traceback) ... SyntaxError: invalid syntax Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_24.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZyA9IEdyYXBoKH0pOyBnLnNldF9wb3MofSk7ICBncmFwaF9lZGl0b3IoZyk7"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpgG7qK1/___code___.py", line 2 g = Graph(}); g.set_pos(}); graph_editor(g); ^ SyntaxError: invalid syntax |
Sage contains many unique and deep algorithms:
|