PREP Quickstart, NumAnalysis
system:sage


<h1>Sage Quickstart for Numerical Analysis</h1>
<p><span id="cell_outer_0">This&nbsp;<a href="http://www.sagemath.org/" target="_blank">Sage</a>&nbsp;worksheet was developed for the MAA PREP  Workshop "Sage: Using Open-Source Mathematics Software with  Undergraduates" (funding provided by NSF DUE 0817071).</span></p>
<p>Sage includes many tools for numerical analysis investigations.</p>
<p>RealField using arbitrary precision (implemented with MPFR).&nbsp; The default real numbers (RR) is RealField(53) (i.e., 53 bits of precision).</p>

{{{id=1|
ring=RealField(3)
///
}}}

<p>To print the actual number (without rounding off the last few imprecise digits to only display correct digits), call the .str() method with the option "truncate=False".</p>
<p>(There is a patch on its way through the review process that makes it easy to set this option once instead of needing to provide it every time).</p>

{{{id=3|
ring('1').nextabove().str(truncate=False)
///
'1.2'
}}}

{{{id=4|
ring=RealField(20)
ring('1').nextabove().str(truncate=False)
///
'1.0000019'
}}}

<p>You can also specify the rounding mode.</p>

{{{id=11|
ringup=RealField(3,rnd='RNDD')
ringdown=RealField(3,rnd='RNDU')
///
}}}

{{{id=13|
ringup(1/9).str(truncate=False)
///
'0.10'
}}}

{{{id=14|
ringdown(1/9).str(truncate=False)
///
'0.13'
}}}

<p>Sage also lets you compute using intervals to keep track of error bounds.&nbsp; These basically use the round up and round down features shown above.</p>

{{{id=19|
ring=RealIntervalField(10)
a=ring(1/9)
a
///
0.112?
}}}

<p>The question mark doesn't quite mean what you think it does!</p>

{{{id=8|
1/a
///
9.0?
}}}

{{{id=16|
print (1/a).str(style='brackets')
///
[8.9843 .. 9.0157]
}}}

{{{id=17|

///
}}}

<p>Scipy (included in Sage) has a lot of numerical algorithms.&nbsp; See <a href="http://docs.scipy.org/doc/scipy/reference/" target="_blank">http://docs.scipy.org/doc/scipy/reference/</a></p>
<p>Mpmath is also included in Sage, and contains a huge amount of numerical stuff.&nbsp; See <a href="http://mpmath.googlecode.com/svn/tags/0.14/doc/build/index.html" target="_blank">http://mpmath.googlecode.com/svn/tags/0.14/doc/build/index.html</a></p>
<p>The <a href="http://docs.python.org/library/decimal.html" target="_blank">Decimal python module</a> has also been useful for textbook exercises which involved rounding in base 10.</p>

{{{id=5|

///
}}}