sage intro demo
system:sage


<h2>Using the Maxima Interface</h2>

{{{id=50|
maxima('2 + 2')
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}4</span></html>
}}}

{{{id=34|
f = maxima('1/sqrt(x^2 + 2*x - 1)'); f
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}{{1}\over{\sqrt{x^2+2\,x-1}}}</span></html>
}}}

{{{id=42|
type(f)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{ < class 'sage.interfaces.maxima.MaximaElement' > }</span></html>
}}}

{{{id=36|
f.integrate(x)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\log \left(2\,\sqrt{x^2+2\,x-1}+2\,x+2\right)</span></html>
}}}

{{{id=37|
maxima.plot2d('cos(2*x) + 2*exp(-x)','[x,0,1]',
                    '[plot_format,openmath]')
///
}}}

{{{id=38|

///
}}}

<h2>Moretti Symbolic Calculus</h2>

{{{id=41|
x = var('x')
f = 1/sqrt(x^2 + 2*x - 1); f
///
}}}

{{{id=40|
f.integrate(x)
///
}}}

{{{id=44|

///
}}}

<h2>Pynac based symbolics</h2>

{{{id=43|
x = var('x'); f = 1/sqrt(x^2 + 2*x - 1); type(f)
///
}}}

{{{id=46|
timeit('1/(f*f)')
///
}}}

{{{id=47|
g = maxima(f)
timeit('1/(g*g)')
///
}}}

{{{id=33|

///
}}}

<h2>Demo: Factoring</h2>

<p>Factoring an integer:</p>

{{{id=1|
factor(2012)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}2^{2} \cdot 503</span></html>
}}}

<p>Factoring a symbolic expression:</p>

{{{id=8|
x,y=var('x,y')
factor(x^3 - (sin(y)*x^2)^3)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}-{\left(x \sin\left(y\right) - 1\right)} {\left(x^{2} \sin\left(y\right)^{2} + x \sin\left(y\right) + 1\right)} x^{3}</span></html>
}}}

<p>Factoring a polynomial over a nontrivial finite field:</p>

{{{id=7|
F.<alpha> = GF(49)
x = polygen(F)
factor(x^4 + x^3 - 2)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}(x + \alpha + 1) \cdot (x + 6 \alpha + 2) \cdot (x + 6)^{2}</span></html>
}}}

{{{id=6|

///
}}}

<h2>Demo: Solving Equations</h2>
<p>Solve a quadratic equation:</p>

{{{id=15|
x = var('x'); solve(x^2 + 7*x == 5, x)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left[x = -\frac{1}{2} \, \sqrt{69} - \frac{7}{2}, x = \frac{1}{2} \, \sqrt{69} - \frac{7}{2}\right]</span></html>
}}}

<p>Solve a system of two linear equations with one unknown coefficient $\alpha$:</p>

{{{id=17|
var('alpha, y')
solve([3*x + 7*y == 2, alpha*x + 3*y == 8], x,y)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \frac{50}{7 \, \alpha - 9}, y = \frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right]\right]</span></html>
}}}

{{{id=51|
A = matrix([[3,7], [alpha, 3]]); A
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
3 & 7 \\
\alpha & 3
\end{array}\right)</span></html>
}}}

{{{id=52|
v = vector([2,8]); v
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left(2,8\right)</span></html>
}}}

{{{id=53|
A.solve_right(v)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left(\frac{-14 \, {\left(\alpha - 12\right)}}{3 \, {\left(7 \, \alpha - 9\right)}} + \frac{2}{3},\frac{2 \, {\left(\alpha - 12\right)}}{7 \, \alpha - 9}\right)</span></html>
}}}

{{{id=20|

///
}}}

<h2>Demo: Computing Symbolic Integrals</h2>

{{{id=19|
f = 1/sqrt(x^2 + 2*x - 1); f.integrate(x)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(2 \, x + 2 \, \sqrt{x^{2} + 2 \, x - 1} + 2\right)</span></html>
}}}

{{{id=18|
g = integrate(sin(x)*tan(x), x); g
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} \, \log\left(\sin\left(x\right) - 1\right) + \frac{1}{2} \, \log\left(\sin\left(x\right) + 1\right) - \sin\left(x\right)</span></html>
}}}

{{{id=23|
h = g.diff(x); h
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{-\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)</span></html>
}}}

{{{id=54|
h - sin(x)*tan(x)
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}-\sin\left(x\right) \tan\left(x\right) + \frac{-\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) - 1\right)}} + \frac{\cos\left(x\right)}{2 \, {\left(\sin\left(x\right) + 1\right)}} - \cos\left(x\right)</span></html>
}}}

{{{id=24|
(h - sin(x)*tan(x)).simplify_full()
///
<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}0</span></html>
}}}

{{{id=26|

///
}}}

<h2>Demo: Plotting a 2D Function</h2>

{{{id=55|
f = 1/sqrt(x^2 + 2*x - 1)
///
}}}

{{{id=56|
interact?
///
}}}

{{{id=27|
@interact
def h(t=(1..20), grid=True):
    show(plot(f, (x, .6, 2), 
                  thickness=t, color='purple', fill=True, gridlines=grid))
///
<html><!--notruncate--><div padding=6 id="div-interact-27"> <table width=800px height=20px bgcolor="#c5c5c5"
                 cellpadding=15><tr><td bgcolor="#f9f9f9" valign=top align=left><table>
<tr><td colspan=3><table><tr><td align=right><font color="black">t&nbsp;</font></td><td><table><tr><td>
        <div id="slider-t-27" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-t-27-lbl"></font></td></tr></table><script>(function(){ var values = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]; setTimeout(function() {
    $('#slider-t-27').slider({
        step: 1, min: 0, max: 19, value: 0,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-t-27-lbl').text(values[position]); interact(27, '_interact_.update(\'27\', \'t\', 10, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'27\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-t-27-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-t-27-lbl').text(values[$('#slider-t-27').slider('value')]);
    }, 1); })();</script></td>
</tr><tr><td align=right><font color="black">grid&nbsp;</font></td><td><input type="checkbox" checked width=200px onchange="interact(27, '_interact_.update(\'27\', \'grid\', 11, _interact_.standard_b64decode(\''+encode64(this.checked)+'\'), globals()); _interact_.recompute(\'27\');')"></input></td>
</tr></table></td></tr>
<tr><td></td><td style='width: 100%;'><div id="cell-interact-27"><?__SAGE__START>
        <table border=0 bgcolor="white" width=100%>
        <tr><td bgcolor="white" align=left valign=top><pre><?__SAGE__TEXT></pre></td></tr>
        <tr><td  align=left valign=top><?__SAGE__HTML></td></tr>
        </table><?__SAGE__END></div></td><td></td></tr>
<tr><td colspan=3></td></tr>
</table></td>
                 </tr></table></div>
                 </html>
}}}

{{{id=32|

///
}}}

<h2>Demo: Plotting a 3D Function</h2>

{{{id=31|
var('x,y')
plot3d(sin(x-y)*y*cos(x), (x,-3,3), (y,-3,3), opacity=.8, color='red') + icosahedron(color='blue')
///
}}}

{{{id=30|

///
}}}

<h2>Demo: Interact</h2>

{{{id=29|
import pylab; import numpy
A_image = numpy.mean(pylab.imread(DATA + 'mumbai.png'), 2)
u,s,v = numpy.linalg.svd(A_image)
S = numpy.zeros( A_image.shape )
S[:len(s),:len(s)] = numpy.diag(s)
n = A_image.shape[0]
@interact
def svd_image(i = ("Eigenvalues (quality)",(20,(1..A_image.shape[0])))):
    A_approx = numpy.dot(numpy.dot(u[:,:i], S[:i,:i]), v[:i,:])
    g = graphics_array([matrix_plot(A_approx), matrix_plot(A_image)])
    show(g, axes=False, figsize=(8,3))
    html("Compressed to %.1f%% of size using %s eigenvalues."%(
                  100*(2.0*i*n+i)/(n*n), i))
///
<html><!--notruncate--><div padding=6 id="div-interact-29"> <table width=800px height=20px bgcolor="#c5c5c5"
                 cellpadding=15><tr><td bgcolor="#f9f9f9" valign=top align=left><table>
<tr><td colspan=3><table><tr><td align=right><font color="black">Eigenvalues (quality)&nbsp;</font></td><td><table><tr><td>
        <div id="slider-i-29" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-i-29-lbl"></font></td></tr></table><script>(function(){ var values = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150","151","152","153","154","155","156","157","158","159","160","161","162","163","164","165","166","167","168","169","170","171","172","173","174","175","176","177","178","179","180","181","182","183","184","185","186","187","188","189","190","191","192","193","194","195","196","197","198","199","200","201","202","203","204","205","206","207","208","209","210","211","212","213","214","215","216","217","218","219","220","221","222","223","224","225","226","227","228","229","230","231","232","233","234","235","236","237","238","239","240","241","242","243","244","245","246","247","248","249","250","251","252","253","254","255","256","257","258","259","260","261","262","263","264","265","266","267","268","269","270","271","272","273","274","275","276","277","278","279","280","281","282","283","284","285","286","287","288","289","290","291","292","293","294","295","296","297","298","299","300","301","302","303","304","305","306","307","308","309","310","311","312","313","314","315","316","317","318","319","320"]; setTimeout(function() {
    $('#slider-i-29').slider({
        step: 1, min: 0, max: 319, value: 19,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-i-29-lbl').text(values[position]); interact(29, '_interact_.update(\'29\', \'i\', 12, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'29\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-i-29-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-i-29-lbl').text(values[$('#slider-i-29').slider('value')]);
    }, 1); })();</script></td>
</tr></table></td></tr>
<tr><td></td><td style='width: 100%;'><div id="cell-interact-29"><?__SAGE__START>
        <table border=0 bgcolor="white" width=100%>
        <tr><td bgcolor="white" align=left valign=top><pre><?__SAGE__TEXT></pre></td></tr>
        <tr><td  align=left valign=top><?__SAGE__HTML></td></tr>
        </table><?__SAGE__END></div></td><td></td></tr>
<tr><td colspan=3></td></tr>
</table></td>
                 </tr></table></div>
                 </html>
}}}

{{{id=49|

///
}}}