Demo: Factoring

Factoring an integer:

{{{id=1| factor(2012) /// \newcommand{\Bold}[1]{\mathbf{#1}}2^{2} \cdot 503 }}}

Factoring a symbolic expression:

{{{id=8| var('x,y') factor(x^3 - (sin(y)*x^2)^3) /// \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} }}}

Factoring a polynomial over a nontrivial finite field:

{{{id=7| F. = GF(49) R. = F[] factor(x^4 + x^3 - 2) /// \newcommand{\Bold}[1]{\mathbf{#1}}(x + \alpha + 1) \cdot (x + 6 \alpha + 2) \cdot (x + 6)^{2} }}} {{{id=6| /// }}}

Demo: Solving Equations

Solve a quadratic equation:

{{{id=15| x = var('x'); solve(x^2 + 7*x == 5, x) /// \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] }}}

Solve a system of two linear equations with one unknown coefficient $\alpha$:

{{{id=17| var('alpha, y') solve([3*x + 7*y == 2, alpha*x + 3*y == 8], x,y) /// \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] }}} {{{id=51| A = matrix([[3,7], [alpha, 3]]); A /// \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 3 & 7 \\ \alpha & 3 \end{array}\right) }}} {{{id=52| v = vector([2,8]); v /// \newcommand{\Bold}[1]{\mathbf{#1}}\left(2,8\right) }}} {{{id=53| A.solve_right(v) /// \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) }}} {{{id=20| /// }}}

Demo: Computing Symbolic Integrals

{{{id=19| f = 1/sqrt(x^2 + 2*x - 1); f.integrate(x) /// \newcommand{\Bold}[1]{\mathbf{#1}}\log\left(2 \, x + 2 \, \sqrt{x^{2} + 2 \, x - 1} + 2\right) }}} {{{id=18| g = integrate(sin(x)*tan(x), x); g /// \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) }}} {{{id=23| h = g.diff(x); h /// \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) }}} {{{id=54| h - sin(x)*tan(x) /// \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) }}} {{{id=24| (h - sin(x)*tan(x)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}0 }}} {{{id=26| /// }}}

Demo: Plotting a 2D Function

{{{id=55| f(x) = 1/sqrt(x^2 + 2*x - 1) /// }}} {{{id=56| interact? /// \newcommand{\Bold}[1]{\mathbf{#1}}\hbox{ < function interact at 0x109a43230 > } }}} {{{id=27| @interact def h(t=(1..20), grid=True): show(plot(f, (x, .6, 2), thickness=t, color='purple', fill=True, gridlines=grid)) ///
grid 
}}} {{{id=32| /// }}}

Demo: Plotting a 3D Function

{{{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| /// }}}

Demo: Interact

{{{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)) ///
Eigenvalues (quality) 
}}} {{{id=49| /// }}}