{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "
There are a couple of ways of defining a matrix in Sage. You can define a matrix directly...
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A=matrix([[0,1,2,9],[5,10,-15,1],[3,6,-8,0]])\n", "print A" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "...or you can define some vectors and combine them to make a matrix.
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "th1=vector([2,3,0,0,4]); th2=vector([0,0,1,5,-2]); th3=matrix([th1,th2])\n", "th3" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Remember, a matrix can represent a system of linear equations. Instead of doing all the algebra of solving these by hand (you know, adding together equations, or subtracting them, or multiplying them by constants, all that stuff) you can ask the computer to do it for you:
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "th3.rref()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "However, there is a caution with this---Python isn't so great with numerical approximations, and thus neither is Sage, and that can really wreck this command. Check out the following:
" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ ".1+.2-.3" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "As you can see, what is exactly zero to a human is not so to a computer. Well, unless we tell it to operate that way... look at the subtle differences in instruction and effect of the next two commands.
" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix([[-.5,.16,.25,.25],[.2,-.33,.1,.2],[.15,.09,-.5,.2],[.15,.08,.15,-.65]]).rref()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix(QQ,[[-.5,.16,.25,.25],[.2,-.33,.1,.2],[.15,.09,-.5,.2],[.15,.08,.15,-.65]]).rref()" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "In both cases, the matrix has rational entries, but in the first case Sage does the computation using numerical approximations (in base 2, so not actually exact because the numbers aren't easy sums of powers of 2) and in the second case Sage is instructed to do exact computations over the rationals. So that second answer is the correct one.
\n", "\n", "