A Short Introduction to Sage: Demo

William Stein, May 12, 2011

{{{id=57| /// }}}

Demo: Factoring

Factoring an integer:

{{{id=1| factor(2012) /// 2^2 * 503 }}}

Factoring a symbolic expression:

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

Demo: Solving Equations

Solve a quadratic equation:

{{{id=15| x = var('x'); solve(x^2 + 7*x == 5, x) /// [x == -1/2*sqrt(69) - 7/2, x == 1/2*sqrt(69) - 7/2] }}}

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

Symbolically:

{{{id=17| var('alpha, y') solve([3*x + 7*y == 2, alpha*x + 3*y == 8], x,y) /// [[x == 50/(7*alpha - 9), y == 2*(alpha - 12)/(7*alpha - 9)]] }}}

Algebraically:

{{{id=64| R. = PolynomialRing(QQ) R /// Univariate Polynomial Ring in alpha over Rational Field }}} {{{id=51| A = matrix([[3,7], [alpha, 3]]); A /// [ 3 7] [alpha 3] }}} {{{id=65| A.parent() /// Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in alpha over Rational Field }}} {{{id=52| v = vector([2,8]); v /// (2, 8) }}} {{{id=53| A.solve_right(v) /// (-50/3/(-7/3*alpha + 3), (-2/3*alpha + 8)/(-7/3*alpha + 3)) }}}

By the way, matrix algebra in Sage is powerful:

{{{id=69| A = random_matrix(ZZ, 500) time A.det() /// -81782235799002533979486536101001964792833596855592506501460539706145549655819243799955870618927837100007705666384972495829025942876484722038131227054034375229674527893485439481958325648853548144544201620891106271904595861129761946374314059202732456301083092021511731881094100811353972938898254353725535947427682392782310522602499146837872634224703126777328936944266114174449754175568224157397528353121297057322686197160799771845337808266659925444768083397084881415481869320882863436742937101243163539326349234047382129830556362021453729763403942217787584636267379615793372187256321736256143874076541876140498519213547377612481368514051319090509029337506634288747197985938915605518324502358742224083669080104649446235706929527119495050020735292690629919997902919129921553081033814723644148138255199389624801947025192852586015376536047556968876829722388970677914347046656252466518035935464508939117714309627394536951574846374201143854757431465352256569016290053671658770111125591549738315897966243079871198489763519941623053378988783164367939966845057576806633400659069767663282046530446407048202056337514065477036141517278147330757348905912866032720351254771873302556017873614823011289253557759882699245957500262911734088972193029237200296872559051483620916835350452387722399558424675752840218304634679273300149133931676265456056407887278072297719484395844317559745336 Time: CPU 2.92 s, Wall: 2.71 s }}} {{{id=66| /// }}} {{{id=20| /// }}}

Demo: Computing Symbolic Integrals

{{{id=19| f = 1/sqrt(x^2 + 2*x - 1) show(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); show(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); show(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| show(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() /// 0 }}} {{{id=26| /// }}}

Demo: Plotting a 2D Function

{{{id=70| plot(1/sqrt(x^2 + 2*x - 1), .6, 2) /// }}} {{{id=27| var('x') @interact def _(f = 1/sqrt(x^2 + 2*x - 1), t=(1..20), color=Color('purple'), grid=True): plot(f, (x, .6, 2), thickness=t, color=color, fill=True, gridlines=grid).show() ///
color 
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 + 'handplant.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)",(10,(1..A_image.shape[0]//2)))): 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=(10,5)) html("Compressed to %.1f%% of size using %s eigenvalues."%( 100*(2.0*i*n+i)/(n*n), i)) ///
Eigenvalues (quality) 
}}} {{{id=72| /// }}}

Plotting a Standard 3d Model

{{{id=49| # Yoda! (over 50,000 triangles) from scipy import io yoda = io.loadmat(DATA + 'yodapose.mat') from sage.plot.plot3d.index_face_set import IndexFaceSet V = yoda['V']; F3=yoda['F3']-1; F4=yoda['F4']-1 Y = IndexFaceSet(F3,V,color=Color('#444444')) + IndexFaceSet(F4,V,color=Color('#007700')) Y = Y.rotateX(-1) Y.show(aspect_ratio=1, frame=False, zoom=1.2) /// }}} {{{id=74| /// }}} {{{id=75| /// }}}