compy
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.html

README.html

<html><body>
<h2>Classes</h2>
<ul><li><a href="#Cluster">Cluster</a></li><li><a href="#DisjointSets">DisjointSets</a></li><li><a href="#Matrix">Matrix</a></li><li><a href="#NeuralNetwork">NeuralNetwork</a></li><li><a href="#PersistentDictionary">PersistentDictionary</a></li><li><a href="#PrimVertex">PrimVertex</a></li><li><a href="#QuadratureIntegrator">QuadratureIntegrator</a></li><li><a href="#Trader">Trader</a></li><li><a href="#YStock">YStock</a></li><li><a href="#memoize">memoize</a></li><li><a href="#memoize_persistent">memoize_persistent</a></li></ul>
<h2>Functions</h2>
<ul><li><a href="#Cholesky">Cholesky</a></li><li><a href="#D">D</a></li><li><a href="#DD">DD</a></li><li><a href="#Dijkstra">Dijkstra</a></li><li><a href="#Jacobi_eigenvalues">Jacobi_eigenvalues</a></li><li><a href="#Kruskal">Kruskal</a></li><li><a href="#Markowitz">Markowitz</a></li><li><a href="#POLYNOMIAL">POLYNOMIAL</a></li><li><a href="#__add__">__add__</a></li><li><a href="#__div__">__div__</a></li><li><a href="#__getitem__">__getitem__</a></li><li><a href="#__mul__">__mul__</a></li><li><a href="#__neg__">__neg__</a></li><li><a href="#__radd__">__radd__</a></li><li><a href="#__rdiv__">__rdiv__</a></li><li><a href="#__rmul__">__rmul__</a></li><li><a href="#__rsub__">__rsub__</a></li><li><a href="#__setitem__">__setitem__</a></li><li><a href="#__str__">__str__</a></li><li><a href="#__sub__">__sub__</a></li><li><a href="#as_list">as_list</a></li><li><a href="#breadth_first_search">breadth_first_search</a></li><li><a href="#col">col</a></li><li><a href="#color2d">color2d</a></li><li><a href="#compute_correlation">compute_correlation</a></li><li><a href="#condition_number">condition_number</a></li><li><a href="#continuum_knapsack">continuum_knapsack</a></li><li><a href="#core">core</a></li><li><a href="#decode_huffman">decode_huffman</a></li><li><a href="#depth_first_search">depth_first_search</a></li><li><a href="#df">df</a></li><li><a href="#diagonal">diagonal</a></li><li><a href="#draw">draw</a></li><li><a href="#encode_huffman">encode_huffman</a></li><li><a href="#eval_fitting_function">eval_fitting_function</a></li><li><a href="#exp">exp</a></li><li><a href="#fib">fib</a></li><li><a href="#fit">fit</a></li><li><a href="#fit_least_squares">fit_least_squares</a></li><li><a href="#from_list">from_list</a></li><li><a href="#g">g</a></li><li><a href="#g">g</a></li><li><a href="#g">g</a></li><li><a href="#gradient">gradient</a></li><li><a href="#hessian">hessian</a></li><li><a href="#identity">identity</a></li><li><a href="#inorder_tree_walk">inorder_tree_walk</a></li><li><a href="#integrate">integrate</a></li><li><a href="#integrate_naive">integrate_naive</a></li><li><a href="#invert_bicgstab">invert_bicgstab</a></li><li><a href="#invert_minimum_residue">invert_minimum_residue</a></li><li><a href="#is_almost_symmetric">is_almost_symmetric</a></li><li><a href="#is_almost_zero">is_almost_zero</a></li><li><a href="#is_positive_definite">is_positive_definite</a></li><li><a href="#jacobian">jacobian</a></li><li><a href="#lcs">lcs</a></li><li><a href="#make_maze">make_maze</a></li><li><a href="#maxind">maxind</a></li><li><a href="#mycos">mycos</a></li><li><a href="#myexp">myexp</a></li><li><a href="#mysin">mysin</a></li><li><a href="#needleman_wunsch">needleman_wunsch</a></li><li><a href="#norm">norm</a></li><li><a href="#optimize_bisection">optimize_bisection</a></li><li><a href="#optimize_golden_search">optimize_golden_search</a></li><li><a href="#optimize_newton">optimize_newton</a></li><li><a href="#optimize_newton_multi">optimize_newton_multi</a></li><li><a href="#optimize_newton_multi_imporved">optimize_newton_multi_imporved</a></li><li><a href="#optimize_newton_stabilized">optimize_newton_stabilized</a></li><li><a href="#optimize_secant">optimize_secant</a></li><li><a href="#partial">partial</a></li><li><a href="#row">row</a></li><li><a href="#solve_bisection">solve_bisection</a></li><li><a href="#solve_fixed_point">solve_fixed_point</a></li><li><a href="#solve_newton">solve_newton</a></li><li><a href="#solve_newton_multi">solve_newton_multi</a></li><li><a href="#solve_newton_stabilized">solve_newton_stabilized</a></li><li><a href="#solve_secant">solve_secant</a></li><li><a href="#sqrt">sqrt</a></li><li><a href="#swap_rows">swap_rows</a></li><li><a href="#t">t</a></li></ul>
<h2>Module  import</h2><h3> urllib and  json</h3><h4>API</h4><a name="YStock"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>YStock<span style="font-weight: bold">:<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>symbol<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>current<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>historical<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>start<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>stop<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>download<span style="font-weight: bold">(</span>symbol<span style="font-weight: bold">=</span><span style="color: #FF9966">'goog'</span><span style="font-weight: bold">,</span>what<span style="font-weight: bold">=</span><span style="color: #FF9966">'adjusted_close'</span><span style="font-weight: bold">,</span>start<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">,</span>stop<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h3> sqlite database and persistence</h3><h4>API</h4><a name="PersistentDictionary"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>PersistentDictionary<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,<br/>                 </span>path<span style="font-weight: bold">=</span><span style="color: #FF9966">'persistence.sqlite'</span><span style="font-weight: bold">,<br/>                 </span>autocommit<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">True</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>uuid<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>keys<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>pattern<span style="font-weight: bold">=</span><span style="color: #FF9966">'*'</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__contains__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>key<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__iter__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__setitem__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>key<span style="font-weight: bold">,</span>value<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__getitem__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>key<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__delitem__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>pattern<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>items<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>pattern<span style="font-weight: bold">=</span><span style="color: #FF9966">'*'</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.<br/>15.<br/>16.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>SP100 <span style="font-weight: bold">= [</span><span style="color: #FF9966">'AA'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AAPL'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'ABT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AEP'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'ALL'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AMGN'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AMZN'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AVP'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'AXP'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BA'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BAC'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'BAX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BHI'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BK'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BMY'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'BRK.B'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CAT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'C'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CL'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CMCSA'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'COF'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'COP'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'COST'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'CPB'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CSCO'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CVS'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'CVX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'DD'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'DELL'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'DIS'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'DOW'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'DVN'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'EMC'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'ETR'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'EXC'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'F'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'FCX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'FDX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'GD'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'GE'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'GILD'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'GOOG'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'GS'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'HAL'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'HD'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'HNZ'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'HON'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'HPQ'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'IBM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'INTC'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'JNJ'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'JPM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'KFT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'KO'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'LMT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'LOW'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MA'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MCD'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MDT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MET'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'MMM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MO'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MON'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MRK'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MS'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'MSFT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'NKE'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'NOV'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'NSC'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'NWSA'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'NYX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'ORCL'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'OXY'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'PEP'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'PFE'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'PG'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'PM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'QCOM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'RF'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'RTN'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'S'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'SLB'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'SLE'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'SO'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'T'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'TGT'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'TWX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'TXN'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'UNH'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'UPS'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'USB'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'UTX'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'VZ'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'WAG'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'WFC'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'WMB'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'WMT'</span><span style="font-weight: bold">,<br/>... </span><span style="color: #FF9966">'WY'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'XOM'</span><span style="font-weight: bold">, </span><span style="color: #FF9966">'XRX'</span><span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>datetime <span style="color:#185369; font-weight: bold">import </span>date<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>symbol <span style="color:#185369; font-weight: bold">in </span>SP100<span style="font-weight: bold">:<br/>...     </span>key <span style="font-weight: bold">= </span>symbol<span style="font-weight: bold">+</span><span style="color: #FF9966">'/2011'<br/></span><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">if not </span>key <span style="color:#185369; font-weight: bold">in </span>storage<span style="font-weight: bold">:<br/>...         </span>storage<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span>YStock<span style="font-weight: bold">(</span>symbol<span style="font-weight: bold">).</span>historical<span style="font-weight: bold">(</span>start<span style="font-weight: bold">=</span>date<span style="font-weight: bold">(</span><span style="color: red">2011</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">),<br/>...                                                  </span>stop<span style="font-weight: bold">=</span>date<span style="font-weight: bold">(</span><span style="color: red">2011</span><span style="font-weight: bold">,</span><span style="color: red">12</span><span style="font-weight: bold">,</span><span style="color: red">31</span><span style="font-weight: bold">))</span></pre></td></tr></table><h3> matplotlib</h3><h4>API</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">...</span></pre></td></tr></table><h4>API</h4><a name="draw"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'title'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'x'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'y'</span><span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'tmp.png'</span><span style="font-weight: bold">,<br/>         </span>linesets<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>pointsets<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>histsets<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>ellisets<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">,<br/>         </span>xrange<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>yrange<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>appl <span style="font-weight: bold">= </span>storage<span style="font-weight: bold">[</span><span style="color: #FF9966">'AAPL/2011'</span><span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [(</span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">[</span><span style="color: #FF9966">'adjusted_close'</span><span style="font-weight: bold">]) </span><span style="color:#185369; font-weight: bold">for </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>appl<span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'Apple Stock (2011)'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'trading day'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'adjusted close'</span><span style="font-weight: bold">,<br/>...      </span>linesets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'AAPL'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">}],</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/aapl2011.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/aapl2011.png" /><br/><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>appl <span style="font-weight: bold">= </span>storage<span style="font-weight: bold">[</span><span style="color: #FF9966">'AAPL/2011'</span><span style="font-weight: bold">][</span><span style="color: red">1</span><span style="font-weight: bold">:] </span><span style="color: green; font-style: italic"># skip 1st day<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [</span>day<span style="font-weight: bold">[</span><span style="color: #FF9966">'arithmetic_return'</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>day <span style="color:#185369; font-weight: bold">in </span>appl<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'Apple Stock (2011)'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'arithmetic return'</span><span style="font-weight: bold">, </span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'frequency'</span><span style="font-weight: bold">,<br/>...      </span>histsets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">}],</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/aapl2011hist.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/aapl2011hist.png" /><br/><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>random <span style="color:#185369; font-weight: bold">import </span>gauss<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [(</span>gauss<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">),</span>gauss<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">),</span>gauss<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0.2</span><span style="font-weight: bold">),</span>gauss<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0.2</span><span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">30</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'example scatter plot'</span><span style="font-weight: bold">, </span>xrange<span style="font-weight: bold">=(-</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">), </span>yrange<span style="font-weight: bold">=(-</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">),<br/>...      </span>ellisets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">}],</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/scatter.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/scatter.png" /><br/><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>points <span style="font-weight: bold">= []<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>key <span style="color:#185369; font-weight: bold">in </span>storage<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">(</span><span style="color: #FF9966">'*/2011'</span><span style="font-weight: bold">):<br/>...     </span>v <span style="font-weight: bold">= [</span>day<span style="font-weight: bold">[</span><span style="color: #FF9966">'log_return'</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>day <span style="color:#185369; font-weight: bold">in </span>storage<span style="font-weight: bold">[</span>key<span style="font-weight: bold">][</span><span style="color: red">1</span><span style="font-weight: bold">:]]<br/>...     </span>ret <span style="font-weight: bold">= </span>sum<span style="font-weight: bold">(</span>v<span style="font-weight: bold">)/</span>len<span style="font-weight: bold">(</span>v<span style="font-weight: bold">)<br/>...     </span>var <span style="font-weight: bold">= </span>sum<span style="font-weight: bold">(</span>x<span style="font-weight: bold">**</span><span style="color: red">2 </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>v<span style="font-weight: bold">)/</span>len<span style="font-weight: bold">(</span>v<span style="font-weight: bold">) - </span>ret<span style="font-weight: bold">**</span><span style="color: red">2<br/></span><span style="font-weight: bold">...     </span>points<span style="font-weight: bold">.</span>append<span style="font-weight: bold">((</span>var<span style="font-weight: bold">*</span>math<span style="font-weight: bold">.</span>sqrt<span style="font-weight: bold">(</span>len<span style="font-weight: bold">(</span>v<span style="font-weight: bold">)),</span>ret<span style="font-weight: bold">*</span>len<span style="font-weight: bold">(</span>v<span style="font-weight: bold">),</span><span style="color: red">0.0002</span><span style="font-weight: bold">,</span><span style="color: red">0.02</span><span style="font-weight: bold">))<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'S&amp;P100 (2011)'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'risk'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'return'</span><span style="font-weight: bold">,<br/>...      </span>ellisets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">}],</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/sp100rr.png'</span><span style="font-weight: bold">,<br/>...      </span>xrange <span style="font-weight: bold">= (</span>min<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">),</span>max<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">)),<br/>...      </span>yrange <span style="font-weight: bold">= (</span>min<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">),</span>max<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">)))</span></pre></td></tr></table><img width="300px" src="images/sp100rr.png" /><br/><h4>API</h4><a name="color2d"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>color2d<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'title'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'x'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'y'</span><span style="font-weight: bold">,<br/>            </span>data<span style="font-weight: bold">=[[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">],[</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">5</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">5</span><span style="font-weight: bold">,</span><span style="color: red">6</span><span style="font-weight: bold">],[</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">5</span><span style="font-weight: bold">,</span><span style="color: red">6</span><span style="font-weight: bold">,</span><span style="color: red">7</span><span style="font-weight: bold">]],<br/>            </span>filename <span style="font-weight: bold">= </span><span style="color: #FF9966">'tmp.png'</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">)**</span><span style="color: red">2</span><span style="font-weight: bold">+(</span>y<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)**</span><span style="color: red">2<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [[</span>f<span style="font-weight: bold">(</span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>i<span style="font-weight: bold">-</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>j<span style="font-weight: bold">-</span><span style="color: red">3</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">61</span><span style="font-weight: bold">)] </span><span style="color:#185369; font-weight: bold">for </span>j <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">61</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>color2d<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'example 2d function'</span><span style="font-weight: bold">,<br/>...      </span>data <span style="font-weight: bold">= </span>points<span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/color2d.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/color2d.png" /><br/><h2>Types of Algorithms</h2><h3>Memoization</h3><h4>API</h4><a name="memoize"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>memoize<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__ <span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>f<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__call__ <span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, *</span>args<span style="font-weight: bold">, **</span>kwargs<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="fib"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>fib<span style="font-weight: bold">(</span>n<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>fib<span style="font-weight: bold">(</span><span style="color: red">11</span><span style="font-weight: bold">))<br/></span><span style="color: red">89</span></pre></td></tr></table><h4>API</h4><a name="memoize_persistent"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>memoize_persistent<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__ <span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>f<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__call__ <span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, *</span>args<span style="font-weight: bold">, **</span>kwargs<span style="font-weight: bold">):...</span></pre></td></tr></table><h2>Graph algorithms</h2><h3>Breadth first search</h3><h4>API</h4><a name="breadth_first_search"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>breadth_first_search<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">,</span>start<span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Depth first search</h3><h4>API</h4><a name="depth_first_search"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>depth_first_search<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">,</span>start<span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Disjoint Sets</h3><h4>API</h4><a name="DisjointSets"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>DisjointSets<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>parent<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>join<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__len__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="make_maze"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>make_maze<span style="font-weight: bold">(</span>n<span style="font-weight: bold">,</span>d<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>walls<span style="font-weight: bold">, </span>teared_down_walls <span style="font-weight: bold">= </span>make_maze<span style="font-weight: bold">(</span>n<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">,</span>d<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/maze1.png" /><br/><h3>Minimum spanning tree: Kruskal</h3><img width="300px" src="images/mst.png" /><br/><h4>API</h4><a name="Kruskal"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>Kruskal<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Minimum spenning tree: Prim</h3><h4>API</h4><a name="PrimVertex"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>PrimVertex<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>id<span style="font-weight: bold">,</span>links<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__cmp__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>other<span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>Prim<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">, </span>start<span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Single source shortest paths: Dijkstra</h3><h4>API</h4><a name="Dijkstra"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>Dijkstra<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">, </span>start<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>vertices <span style="font-weight: bold">= </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>links <span style="font-weight: bold">= [(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">,</span>abs<span style="font-weight: bold">(</span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span>i<span style="font-weight: bold">+</span>j<span style="font-weight: bold">+</span><span style="color: red">1</span><span style="font-weight: bold">))) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>vertices <span style="color:#185369; font-weight: bold">for </span>j <span style="color:#185369; font-weight: bold">in </span>vertices<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>graph <span style="font-weight: bold">= [</span>vertices<span style="font-weight: bold">,</span>links<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>links <span style="font-weight: bold">= </span>Dijkstra<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>link <span style="color:#185369; font-weight: bold">in </span>links<span style="font-weight: bold">: </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>link<span style="font-weight: bold">)<br/>(</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0.897</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0.141</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">3</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0.420</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">4</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0.798</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">5</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0.279</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">6</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0.553</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">7</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">0.685</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">8</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0.412</span><span style="font-weight: bold">...)<br/>(</span><span style="color: red">9</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0.544</span><span style="font-weight: bold">...)</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>n<span style="font-weight: bold">,</span>d <span style="font-weight: bold">= </span><span style="color: red">4</span><span style="font-weight: bold">, </span><span style="color: red">2<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>walls<span style="font-weight: bold">, </span>links <span style="font-weight: bold">= </span>make_maze<span style="font-weight: bold">(</span>n<span style="font-weight: bold">,</span>d<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>symmetrized_links <span style="font-weight: bold">= [(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span><span style="font-weight: bold">(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">in </span>links<span style="font-weight: bold">]+[(</span>j<span style="font-weight: bold">,</span>i<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span><span style="font-weight: bold">(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">in </span>links<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>graph <span style="font-weight: bold">= [</span>range<span style="font-weight: bold">(</span>n<span style="font-weight: bold">*</span>n<span style="font-weight: bold">),</span>symmetrized_links<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>links <span style="font-weight: bold">= </span>Dijkstra<span style="font-weight: bold">(</span>graph<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>paths <span style="font-weight: bold">= </span>dict<span style="font-weight: bold">((</span>i<span style="font-weight: bold">,(</span>j<span style="font-weight: bold">,</span>d<span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span><span style="font-weight: bold">(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">,</span>d<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">in </span>links<span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/maze2.png" /><br/><h2>More on Greedy Algorithms</h2><h3>Huffman encoding</h3><h4>API</h4><a name="encode_huffman"></a><a name="inorder_tree_walk"></a><a name="decode_huffman"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>encode_huffman<span style="font-weight: bold">(</span>input<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>inorder_tree_walk<span style="font-weight: bold">(</span>t<span style="font-weight: bold">, </span>key<span style="font-weight: bold">, </span>keys<span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>decode_huffman<span style="font-weight: bold">(</span>keys<span style="font-weight: bold">, </span>encoded<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>input <span style="font-weight: bold">= </span><span style="color: #FF9966">'this is a nice day'<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>keys<span style="font-weight: bold">, </span>encoded <span style="font-weight: bold">= </span>encode_huffman<span style="font-weight: bold">(</span>input<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>encoded<span style="font-weight: bold">)<br/></span><span style="color: red">10111001110010001100100011110010101100110100000011111111110<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>decoded <span style="font-weight: bold">= </span>decode_huffman<span style="font-weight: bold">(</span>keys<span style="font-weight: bold">,</span>encoded<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>decoded <span style="font-weight: bold">== </span>input<span style="font-weight: bold">)<br/></span><span style="color:#185369; font-weight: bold">True<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">1.0</span><span style="font-weight: bold">*</span>len<span style="font-weight: bold">(</span>input<span style="font-weight: bold">)/(</span>len<span style="font-weight: bold">(</span>encoded<span style="font-weight: bold">)/</span><span style="color: red">8</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.57</span><span style="font-weight: bold">...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>math <span style="color:#185369; font-weight: bold">import </span>log<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>input <span style="font-weight: bold">= </span><span style="color: #FF9966">'this is a nice day'<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>w <span style="font-weight: bold">= [</span><span style="color: red">1.0</span><span style="font-weight: bold">*</span>input<span style="font-weight: bold">.</span>count<span style="font-weight: bold">(</span>c<span style="font-weight: bold">)/</span>len<span style="font-weight: bold">(</span>input<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>c <span style="color:#185369; font-weight: bold">in </span>set<span style="font-weight: bold">(</span>input<span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>E <span style="font-weight: bold">= -</span>sum<span style="font-weight: bold">(</span>wi<span style="font-weight: bold">*</span>log<span style="font-weight: bold">(</span>wi<span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>wi <span style="color:#185369; font-weight: bold">in </span>w<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>E<span style="font-weight: bold">)<br/></span><span style="color: red">3.23</span><span style="font-weight: bold">...</span></pre></td></tr></table><h3>Longest common subsequence</h3><h4>API</h4><a name="lcs"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>lcs<span style="font-weight: bold">(</span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>dna1 <span style="font-weight: bold">= </span><span style="color: #FF9966">'ATGCTTTAGAGGATGCGTAGATAGCTAAATAGCTCGCTAGA'<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>dna2 <span style="font-weight: bold">= </span><span style="color: #FF9966">'GATAGGTACCACAATAATAAGGATAGCTCGCAAATCCTCGA'<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>lcs<span style="font-weight: bold">(</span>dna1<span style="font-weight: bold">,</span>dna2<span style="font-weight: bold">))<br/></span><span style="color: red">26</span></pre></td></tr></table><h3>Needleman-Wunsch</h3><h4>API</h4><a name="needleman_wunsch"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>needleman_wunsch<span style="font-weight: bold">(</span>a<span style="font-weight: bold">,</span>b<span style="font-weight: bold">,</span>p<span style="font-weight: bold">=</span><span style="color: red">0.97</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>bases <span style="font-weight: bold">= </span><span style="color: #FF9966">'ATGC'<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>random <span style="color:#185369; font-weight: bold">import </span>choice<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>genes <span style="font-weight: bold">= [</span><span style="color: #FF9966">''</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>choice<span style="font-weight: bold">(</span>bases<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>k <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">20</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>chromosome1 <span style="font-weight: bold">= </span><span style="color: #FF9966">''</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>choice<span style="font-weight: bold">(</span>genes<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">))<br/>&gt;&gt;&gt; </span>chromosome2 <span style="font-weight: bold">= </span><span style="color: #FF9966">''</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>choice<span style="font-weight: bold">(</span>genes<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">))<br/>&gt;&gt;&gt; </span>z <span style="font-weight: bold">= </span>needleman_wunsch<span style="font-weight: bold">(</span>chromosome1<span style="font-weight: bold">, </span>chromosome2<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>color2d<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'Needleman-Wunsch'</span><span style="font-weight: bold">, </span>data<span style="font-weight: bold">=</span>z<span style="font-weight: bold">,<br/>...         </span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/needleman.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/needleman.png" /><br/><h3>Continuum Knapsack</h3><h4>API</h4><a name="continuum_knapsack"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>continuum_knapsack<span style="font-weight: bold">(</span>a<span style="font-weight: bold">,</span>b<span style="font-weight: bold">,</span>c<span style="font-weight: bold">):...</span></pre></td></tr></table><h2>Standard Strategies</h2><h3>Replace derivatives with finite differences</h3><h4>API</h4><a name="D"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>D<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>h<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="DD"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>DD<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>h<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span>x<span style="font-weight: bold">*</span>x<span style="font-weight: bold">-</span><span style="color: red">5.0</span><span style="font-weight: bold">*</span>x<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>f<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">))<br/></span><span style="color: red">0.0<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>f1 <span style="font-weight: bold">= </span>D<span style="font-weight: bold">(</span>f<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># first derivative<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>f1<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">))<br/>-</span><span style="color: red">5.0<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>f2 <span style="font-weight: bold">= </span>DD<span style="font-weight: bold">(</span>f<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># second derivative<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>f2<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.00000</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span>f2 <span style="font-weight: bold">= </span>D<span style="font-weight: bold">(</span>f1<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># second derivative<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>f2<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.99999</span><span style="font-weight: bold">...</span></pre></td></tr></table><h3>Taylor Series</h3><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>X <span style="font-weight: bold">= [</span><span style="color: red">0.03</span><span style="font-weight: bold">*</span>i <span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>xrange<span style="font-weight: bold">(</span><span style="color: red">200</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>Y <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'sin(x)'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">]}<br/>&gt;&gt;&gt; </span>Y1 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 1st'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span>x<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">100</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>Y2 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 3rd'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span>x<span style="font-weight: bold">-</span>x<span style="font-weight: bold">**</span><span style="color: red">3</span><span style="font-weight: bold">/</span><span style="color: red">6</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">100</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>Y3 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 5th'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span>x<span style="font-weight: bold">-</span>x<span style="font-weight: bold">**</span><span style="color: red">3</span><span style="font-weight: bold">/</span><span style="color: red">6</span><span style="font-weight: bold">+</span>x<span style="font-weight: bold">**</span><span style="color: red">5</span><span style="font-weight: bold">/</span><span style="color: red">120</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">100</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'sin(x) approximations'</span><span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/sin.png'</span><span style="font-weight: bold">,</span>linesets<span style="font-weight: bold">=[</span>Y<span style="font-weight: bold">,</span>Y1<span style="font-weight: bold">,</span>Y2<span style="font-weight: bold">,</span>Y3<span style="font-weight: bold">])</span></pre></td></tr></table><img width="300px" src="images/sin.png" /><br/><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>a <span style="font-weight: bold">= </span>math<span style="font-weight: bold">.</span>pi<span style="font-weight: bold">/</span><span style="color: red">2<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>X <span style="font-weight: bold">= [</span><span style="color: red">0.03</span><span style="font-weight: bold">*</span>i <span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>xrange<span style="font-weight: bold">(</span><span style="color: red">200</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>Y <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'sin(x)'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">]}<br/>&gt;&gt;&gt; </span>Y1 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 2nd'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">-(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">2</span><span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">150</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>Y2 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 4th'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">-(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">2</span><span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">+(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">4</span><span style="font-weight: bold">/</span><span style="color: red">24</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">150</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>Y3 <span style="font-weight: bold">= {</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'Taylor 6th'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>x<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">-(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">2</span><span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">+(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">4</span><span style="font-weight: bold">/</span><span style="color: red">24</span><span style="font-weight: bold">-(</span>x<span style="font-weight: bold">-</span>a<span style="font-weight: bold">)**</span><span style="color: red">6</span><span style="font-weight: bold">/</span><span style="color: red">720</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>x <span style="color:#185369; font-weight: bold">in </span>X<span style="font-weight: bold">[:</span><span style="color: red">150</span><span style="font-weight: bold">]]}<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'sin(x) approximations'</span><span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/sin2.png'</span><span style="font-weight: bold">,</span>linesets<span style="font-weight: bold">=[</span>Y<span style="font-weight: bold">,</span>Y1<span style="font-weight: bold">,</span>Y2<span style="font-weight: bold">,</span>Y2<span style="font-weight: bold">])</span></pre></td></tr></table><img width="300px" src="images/sin2.png" /><br/><h4>API</h4><a name="myexp"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>myexp<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>precision<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">,</span>max_steps<span style="font-weight: bold">=</span><span style="color: red">40</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">):<br/>...     </span>x<span style="font-weight: bold">= </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>i<br/><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">assert </span>abs<span style="font-weight: bold">(</span>myexp<span style="font-weight: bold">(</span>x<span style="font-weight: bold">) - </span>math<span style="font-weight: bold">.</span>exp<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)) &lt; </span><span style="color: red">1e-4</span></pre></td></tr></table><h4>API</h4><a name="mysin"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>mysin<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>precision<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">,</span>max_steps<span style="font-weight: bold">=</span><span style="color: red">40</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">):<br/>...     </span>x<span style="font-weight: bold">= </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>i<br/><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">assert </span>abs<span style="font-weight: bold">(</span>mysin<span style="font-weight: bold">(</span>x<span style="font-weight: bold">) - </span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)) &lt; </span><span style="color: red">1e-4</span></pre></td></tr></table><h4>API</h4><a name="mycos"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>mycos<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>precision<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">,</span>max_steps<span style="font-weight: bold">=</span><span style="color: red">40</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">):<br/>...     </span>x <span style="font-weight: bold">= </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>i<br/><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">assert </span>abs<span style="font-weight: bold">(</span>mycos<span style="font-weight: bold">(</span>x<span style="font-weight: bold">) - </span>math<span style="font-weight: bold">.</span>cos<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)) &lt; </span><span style="color: red">1e-4</span></pre></td></tr></table><h2>Linear Algebra</h2><h3>Linear Systems</h3><h4>API</h4><a name="Matrix"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>Matrix<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>rows<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">,</span>cols<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">,</span>fill<span style="font-weight: bold">=</span><span style="color: red">0.0</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="__getitem__"></a><a name="__setitem__"></a><a name="row"></a><a name="col"></a><a name="as_list"></a><a name="__str__"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>__getitem__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">)):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__setitem__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,(</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">),</span>value<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>row<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>col<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>as_list<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__str__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="identity"></a><a name="diagonal"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>identity<span style="font-weight: bold">(</span>rows<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">,</span>one<span style="font-weight: bold">=</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span>fill<span style="font-weight: bold">=</span><span style="color: red">0.0</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>diagonal<span style="font-weight: bold">(</span>d<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="from_list"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>from_list<span style="font-weight: bold">(</span>v<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="__add__"></a><a name="__sub__"></a><a name="__radd__"></a><a name="__rsub__"></a><a name="__neg__"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>__add__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__sub__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__radd__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__rsub__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__neg__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span><span style="color: red">2.0</span><span style="font-weight: bold">],[</span><span style="color: red">3.0</span><span style="font-weight: bold">,</span><span style="color: red">4.0</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">+ </span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)      </span><span style="color: green; font-style: italic"># calls A.__add__(A)<br/></span><span style="font-weight: bold">[[</span><span style="color: red">2.0</span><span style="font-weight: bold">, </span><span style="color: red">4.0</span><span style="font-weight: bold">], [</span><span style="color: red">6.0</span><span style="font-weight: bold">, </span><span style="color: red">8.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">+ </span><span style="color: red">2</span><span style="font-weight: bold">)      </span><span style="color: green; font-style: italic"># calls A.__add__(2)<br/></span><span style="font-weight: bold">[[</span><span style="color: red">3.0</span><span style="font-weight: bold">, </span><span style="color: red">2.0</span><span style="font-weight: bold">], [</span><span style="color: red">3.0</span><span style="font-weight: bold">, </span><span style="color: red">6.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">)      </span><span style="color: green; font-style: italic"># calls A.__add__(1)<br/></span><span style="font-weight: bold">[[</span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">2.0</span><span style="font-weight: bold">], [</span><span style="color: red">3.0</span><span style="font-weight: bold">, </span><span style="color: red">3.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(-</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)         </span><span style="color: green; font-style: italic"># calls A.__neg__()<br/></span><span style="font-weight: bold">[[-</span><span style="color: red">1.0</span><span style="font-weight: bold">, -</span><span style="color: red">2.0</span><span style="font-weight: bold">], [-</span><span style="color: red">3.0</span><span style="font-weight: bold">, -</span><span style="color: red">4.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">5 </span><span style="font-weight: bold">- </span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)      </span><span style="color: green; font-style: italic"># calls A.__rsub__(5)<br/></span><span style="font-weight: bold">[[</span><span style="color: red">4.0</span><span style="font-weight: bold">, -</span><span style="color: red">2.0</span><span style="font-weight: bold">], [-</span><span style="color: red">3.0</span><span style="font-weight: bold">, </span><span style="color: red">1.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span>b <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1.0</span><span style="font-weight: bold">],[</span><span style="color: red">2.0</span><span style="font-weight: bold">],[</span><span style="color: red">3.0</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>b <span style="font-weight: bold">+ </span><span style="color: red">2</span><span style="font-weight: bold">)      </span><span style="color: green; font-style: italic"># calls b.__add__(2)<br/></span><span style="font-weight: bold">[[</span><span style="color: red">3.0</span><span style="font-weight: bold">], [</span><span style="color: red">4.0</span><span style="font-weight: bold">], [</span><span style="color: red">5.0</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">+ </span><span style="color: red">1</span>j<span style="font-weight: bold">)<br/>[[(</span><span style="color: red">1</span><span style="font-weight: bold">+</span><span style="color: red">1</span>j<span style="font-weight: bold">), </span><span style="color: red">2.0</span><span style="font-weight: bold">], [</span><span style="color: red">3.0</span><span style="font-weight: bold">, (</span><span style="color: red">4</span><span style="font-weight: bold">+</span><span style="color: red">1</span>j<span style="font-weight: bold">)]]</span></pre></td></tr></table><h4>API</h4><a name="__rmul__"></a><a name="__mul__"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>__rmul__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>x<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__mul__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span><span style="color: red">2.0</span><span style="font-weight: bold">],[</span><span style="color: red">3.0</span><span style="font-weight: bold">,</span><span style="color: red">4.0</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">*</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)       </span><span style="color: green; font-style: italic"># scalar * matrix<br/></span><span style="font-weight: bold">[[</span><span style="color: red">2.0</span><span style="font-weight: bold">, </span><span style="color: red">4.0</span><span style="font-weight: bold">], [</span><span style="color: red">6.0</span><span style="font-weight: bold">, </span><span style="color: red">8.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">*</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)       </span><span style="color: green; font-style: italic"># matrix * matrix<br/></span><span style="font-weight: bold">[[</span><span style="color: red">7.0</span><span style="font-weight: bold">, </span><span style="color: red">10.0</span><span style="font-weight: bold">], [</span><span style="color: red">15.0</span><span style="font-weight: bold">, </span><span style="color: red">22.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span>b <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">],[</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>b<span style="font-weight: bold">*</span>b<span style="font-weight: bold">)       </span><span style="color: green; font-style: italic"># scalar product<br/></span><span style="color: red">14</span></pre></td></tr></table><h3>Examples of linear transformations</h3><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.<br/>15.<br/>16.<br/>17.<br/>18.<br/>19.<br/>20.<br/>21.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [(</span>math<span style="font-weight: bold">.</span>cos<span style="font-weight: bold">(</span><span style="color: red">0.0628</span><span style="font-weight: bold">*</span>t<span style="font-weight: bold">),</span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span><span style="color: red">0.0628</span><span style="font-weight: bold">*</span>t<span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">for </span>t <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">200</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>points <span style="font-weight: bold">+= [(</span><span style="color: red">0.02</span><span style="font-weight: bold">*</span>t<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>t <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">50</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>points <span style="font-weight: bold">+= [(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0.02</span><span style="font-weight: bold">*</span>t<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>t <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">50</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'Linear Transformation'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'x'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'y'</span><span style="font-weight: bold">,</span>filename <span style="font-weight: bold">= </span><span style="color: #FF9966">'la1.png'</span><span style="font-weight: bold">,<br/>...      </span>ellisets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">}], </span>xrange<span style="font-weight: bold">=(-</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">), </span>yrange<span style="font-weight: bold">=(-</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">))<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>points<span style="font-weight: bold">,</span>filename<span style="font-weight: bold">):<br/>...      </span>data <span style="font-weight: bold">= [(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]*</span>x<span style="font-weight: bold">+</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">]*</span>y<span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]*</span>x<span style="font-weight: bold">+</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">]*</span>y<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">]<br/>...      </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'Linear Transformation'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'x'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'y'</span><span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span>filename<span style="font-weight: bold">,<br/>...           </span>ellisets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">},{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>data<span style="font-weight: bold">}])<br/>&gt;&gt;&gt; </span>A1 <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">0.2</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">],[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>f<span style="font-weight: bold">(</span>A1<span style="font-weight: bold">, </span>points<span style="font-weight: bold">, </span><span style="color: #FF9966">'la2.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>A2 <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">],[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0.2</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>f<span style="font-weight: bold">(</span>A2<span style="font-weight: bold">, </span>points<span style="font-weight: bold">, </span><span style="color: #FF9966">'la3.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>S <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">0.3</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">],[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0.3</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>f<span style="font-weight: bold">(</span>S<span style="font-weight: bold">, </span>points<span style="font-weight: bold">, </span><span style="color: #FF9966">'la4.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>s<span style="font-weight: bold">, </span>c <span style="font-weight: bold">= </span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span><span style="color: red">0.5</span><span style="font-weight: bold">), </span>math<span style="font-weight: bold">.</span>cos<span style="font-weight: bold">(</span><span style="color: red">0.5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>R <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span>c<span style="font-weight: bold">,-</span>s<span style="font-weight: bold">],[</span>s<span style="font-weight: bold">,</span>c<span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>B1 <span style="font-weight: bold">= </span>R<span style="font-weight: bold">*</span>A1<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>f<span style="font-weight: bold">(</span>B1<span style="font-weight: bold">, </span>points<span style="font-weight: bold">, </span><span style="color: #FF9966">'la5.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>B2 <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">0.2</span><span style="font-weight: bold">,</span><span style="color: red">0.4</span><span style="font-weight: bold">],[</span><span style="color: red">0.5</span><span style="font-weight: bold">,</span><span style="color: red">0.3</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>f<span style="font-weight: bold">(</span>B2<span style="font-weight: bold">, </span>points<span style="font-weight: bold">, </span><span style="color: #FF9966">'la6.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/la1.png" /><br/><img width="300px" src="images/la2.png" /><br/><img width="300px" src="images/la3.png" /><br/><img width="300px" src="images/la4.png" /><br/><img width="300px" src="images/la5.png" /><br/><img width="300px" src="images/la6.png" /><br/><h3>Matrix inversion and Guass-Jordan algorithm</h3><h4>API</h4><a name="__rdiv__"></a><a name="__div__"></a><a name="swap_rows"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>__rdiv__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>x<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__div__<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">B</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>swap_rows<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">,</span>j<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">9</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">/</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)<br/>[[</span><span style="color: red">9.0</span><span style="font-weight: bold">, -</span><span style="color: red">2.0</span><span style="font-weight: bold">], [-</span><span style="color: red">4.0</span><span style="font-weight: bold">, </span><span style="color: red">1.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">/</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)<br/>[[</span><span style="color: red">1.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">], [</span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">1.0</span><span style="font-weight: bold">]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">)<br/>[[</span><span style="color: red">0.5</span><span style="font-weight: bold">, </span><span style="color: red">1.0</span><span style="font-weight: bold">], [</span><span style="color: red">2.0</span><span style="font-weight: bold">, </span><span style="color: red">4.5</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h3>Transposing a matrix</h3><h4>API</h4><a name="t"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>t<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">.</span>t<span style="font-weight: bold">)<br/>[[</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">3</span><span style="font-weight: bold">], [</span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">4</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h4>API</h4><a name="is_almost_symmetric"></a><a name="is_almost_zero"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>is_almost_symmetric<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>is_almost_zero<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Solving Systems of Linear Equations</h3><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">6</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>b <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">3</span><span style="font-weight: bold">],[</span><span style="color: red">6</span><span style="font-weight: bold">],[</span><span style="color: red">10</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>x <span style="font-weight: bold">= (</span><span style="color: red">1</span><span style="font-weight: bold">/</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)*</span>b<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">)<br/>[[-</span><span style="color: red">1.0</span><span style="font-weight: bold">], [</span><span style="color: red">3.0</span><span style="font-weight: bold">], [-</span><span style="color: red">1.0</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h3>Norm and condition number again</h3><h4>API</h4><a name="norm"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>norm<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>p<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="condition_number"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>condition_number<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>x<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">,</span>h<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span>x<span style="font-weight: bold">*</span>x<span style="font-weight: bold">-</span><span style="color: red">5.0</span><span style="font-weight: bold">*</span>x<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>condition_number<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">))<br/></span><span style="color: red">0.74999</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>condition_number<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">))<br/></span><span style="color: red">21.0</span></pre></td></tr></table><h4>API</h4><a name="exp"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>exp<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">,</span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">,</span>ns<span style="font-weight: bold">=</span><span style="color: red">40</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">],[</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>exp<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">))<br/>[[</span><span style="color: red">51.96</span><span style="font-weight: bold">..., </span><span style="color: red">74.73</span><span style="font-weight: bold">...], [</span><span style="color: red">112.10</span><span style="font-weight: bold">..., </span><span style="color: red">164.07</span><span style="font-weight: bold">...]]</span></pre></td></tr></table><h3>Cholesky factorization</h3><h4>API</h4><a name="Cholesky"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>Cholesky<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">.</span>from_list<span style="font-weight: bold">([[</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">],[</span><span style="color: red">2</span><span style="font-weight: bold">,</span><span style="color: red">9</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">],[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span><span style="color: red">16</span><span style="font-weight: bold">]])<br/>&gt;&gt;&gt; </span>L <span style="font-weight: bold">= </span>Cholesky<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>is_almost_zero<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">- </span>L<span style="font-weight: bold">*</span>L<span style="font-weight: bold">.</span>t<span style="font-weight: bold">))<br/></span><span style="color:#185369; font-weight: bold">True</span></pre></td></tr></table><h4>API</h4><a name="is_positive_definite"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>is_positive_definite<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Modern Portfolio Theory</h3><h4>API</h4><a name="Markowitz"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>Markowitz<span style="font-weight: bold">(</span>mu<span style="font-weight: bold">, </span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">, </span>r_free<span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Linear Least Squares and $^2$</h3><h4>API</h4><a name="fit_least_squares"></a><a name="eval_fitting_function"></a><a name="POLYNOMIAL"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>fit_least_squares<span style="font-weight: bold">(</span>points<span style="font-weight: bold">, </span>f<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>eval_fitting_function<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>c<span style="font-weight: bold">,</span>x<span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>POLYNOMIAL<span style="font-weight: bold">(</span>n<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.<br/>15.<br/>16.<br/>17.<br/>18.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [(</span>k<span style="font-weight: bold">,</span><span style="color: red">5</span><span style="font-weight: bold">+</span><span style="color: red">0.8</span><span style="font-weight: bold">*</span>k<span style="font-weight: bold">+</span><span style="color: red">0.3</span><span style="font-weight: bold">*</span>k<span style="font-weight: bold">*</span>k<span style="font-weight: bold">+</span>math<span style="font-weight: bold">.</span>sin<span style="font-weight: bold">(</span>k<span style="font-weight: bold">),</span><span style="color: red">2</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>k <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">100</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>a<span style="font-weight: bold">,</span>chi2<span style="font-weight: bold">,</span>fitting_f <span style="font-weight: bold">= </span>fit_least_squares<span style="font-weight: bold">(</span>points<span style="font-weight: bold">,</span>QUADRATIC<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">[-</span><span style="color: red">10</span><span style="font-weight: bold">:]:<br/>...     </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>round<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">],</span><span style="color: red">2</span><span style="font-weight: bold">), </span>round<span style="font-weight: bold">(</span>fitting_f<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]),</span><span style="color: red">2</span><span style="font-weight: bold">))<br/></span><span style="color: red">90 2507.89 2506.98<br/>91 2562.21 2562.08<br/>92 2617.02 2617.78<br/>93 2673.15 2674.08<br/>94 2730.75 2730.98<br/>95 2789.18 2788.48<br/>96 2847.58 2846.58<br/>97 2905.68 2905.28<br/>98 2964.03 2964.58<br/>99 3023.5 3024.48<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'polynomial fit'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'t'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'e(t),o(t)'</span><span style="font-weight: bold">,<br/>...      </span>pointsets<span style="font-weight: bold">=[{</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'o(t)'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>points<span style="font-weight: bold">[:</span><span style="color: red">10</span><span style="font-weight: bold">]}],<br/>...      </span>linesets<span style="font-weight: bold">=[{</span><span style="color: #FF9966">'label'</span><span style="font-weight: bold">:</span><span style="color: #FF9966">'e(t)'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">],</span>fitting_f<span style="font-weight: bold">(</span>p<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">])) </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">[:</span><span style="color: red">10</span><span style="font-weight: bold">]]}],<br/>...      </span>filename <span style="font-weight: bold">= </span><span style="color: #FF9966">'images/polynomialfit.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/polynomialfit.png" /><br/><h3>Trading and technical analysis</h3><h4>API</h4><a name="Trader"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>Trader<span style="font-weight: bold">:<br/>    </span><span style="color:#185369; font-weight: bold">def </span>model<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>window<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>strategy<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>window<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>simulate<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>data<span style="font-weight: bold">,</span>cash<span style="font-weight: bold">=</span><span style="color: red">1000.0</span><span style="font-weight: bold">,</span>shares<span style="font-weight: bold">=</span><span style="color: red">0.0</span><span style="font-weight: bold">,</span>days<span style="font-weight: bold">=</span><span style="color: red">7</span><span style="font-weight: bold">,</span>daily_rate<span style="font-weight: bold">=</span><span style="color: red">0.03</span><span style="font-weight: bold">/</span><span style="color: red">360</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>datetime <span style="color:#185369; font-weight: bold">import </span>date<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>data <span style="font-weight: bold">= </span>YStock<span style="font-weight: bold">.</span>download<span style="font-weight: bold">(</span><span style="color: #FF9966">'aapl'</span><span style="font-weight: bold">,</span><span style="color: #FF9966">'adjusted_close'</span><span style="font-weight: bold">,<br/>...        </span>start<span style="font-weight: bold">=</span>date<span style="font-weight: bold">(</span><span style="color: red">2011</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">),</span>stop<span style="font-weight: bold">=</span>date<span style="font-weight: bold">(</span><span style="color: red">2011</span><span style="font-weight: bold">,</span><span style="color: red">12</span><span style="font-weight: bold">,</span><span style="color: red">31</span><span style="font-weight: bold">))<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>Trader<span style="font-weight: bold">().</span>simulate<span style="font-weight: bold">(</span>data<span style="font-weight: bold">,</span>cash<span style="font-weight: bold">=</span><span style="color: red">1000.0</span><span style="font-weight: bold">))<br/></span><span style="color: red">1133.2463</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">1000.0</span><span style="font-weight: bold">*</span>math<span style="font-weight: bold">.</span>exp<span style="font-weight: bold">(</span><span style="color: red">0.03</span><span style="font-weight: bold">))<br/></span><span style="color: red">1030.4545</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">1000.0</span><span style="font-weight: bold">*</span>data<span style="font-weight: bold">[-</span><span style="color: red">1</span><span style="font-weight: bold">]/</span>data<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">])<br/></span><span style="color: red">1228.8739</span><span style="font-weight: bold">...</span></pre></td></tr></table><h3>Eigenvalues and Jacobi algorithm</h3><h4>API</h4><a name="sqrt"></a><a name="Jacobi_eigenvalues"></a><a name="maxind"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>sqrt<span style="font-weight: bold">(</span>x<span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>Jacobi_eigenvalues<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">,</span>checkpoint<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">False</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>maxind<span style="font-weight: bold">(</span>M<span style="font-weight: bold">,</span>k<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">import </span>random<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="text-decoration:None;color:#FF5C1F;">A</span> <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">(</span><span style="color: red">4</span><span style="font-weight: bold">,</span><span style="color: red">4</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">for </span>r <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">.</span>rows<span style="font-weight: bold">):<br/>...     </span><span style="color:#185369; font-weight: bold">for </span>c <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>r<span style="font-weight: bold">,</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">.</span>cols<span style="font-weight: bold">):<br/>...         </span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span>r<span style="font-weight: bold">,</span>c<span style="font-weight: bold">] = </span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">[</span>c<span style="font-weight: bold">,</span>r<span style="font-weight: bold">] = </span>random<span style="font-weight: bold">.</span>gauss<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">,</span><span style="color: red">10</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>U<span style="font-weight: bold">,</span>e <span style="font-weight: bold">= </span>Jacobi_eigenvalues<span style="font-weight: bold">(</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>is_almost_zero<span style="font-weight: bold">(</span>U<span style="font-weight: bold">*</span>Matrix<span style="font-weight: bold">.</span>diagonal<span style="font-weight: bold">(</span>e<span style="font-weight: bold">)*</span>U<span style="font-weight: bold">.</span>t<span style="font-weight: bold">-</span><span style="text-decoration:None;color:#FF5C1F;">A</span><span style="font-weight: bold">))<br/></span><span style="color:#185369; font-weight: bold">True</span></pre></td></tr></table><h3>Principal Component Analysis</h3><h4>API</h4><a name="compute_correlation"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>compute_correlation<span style="font-weight: bold">(</span>stocks<span style="font-weight: bold">, </span>key<span style="font-weight: bold">=</span><span style="color: #FF9966">'arithmetic_return'</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>symbols <span style="font-weight: bold">= </span>storage<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">(</span><span style="color: #FF9966">'*/2011'</span><span style="font-weight: bold">)[:</span><span style="color: red">20</span><span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>stocks <span style="font-weight: bold">= [</span>storage<span style="font-weight: bold">[</span>symbol<span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>symbol <span style="color:#185369; font-weight: bold">in </span>symbols<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>corr <span style="font-weight: bold">= </span>compute_correlation<span style="font-weight: bold">(</span>stocks<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>U<span style="font-weight: bold">,</span>e <span style="font-weight: bold">= </span>Jacobi_eigenvalues<span style="font-weight: bold">(</span>corr<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'SP100 eigenvalues'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'i'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'e[i]'</span><span style="font-weight: bold">,</span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/sp100eigen.png'</span><span style="font-weight: bold">,<br/>...      </span>linesets<span style="font-weight: bold">=[{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[(</span>i<span style="font-weight: bold">,</span>ei<span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>i<span style="font-weight: bold">,</span>ei<span style="font-weight: bold">, </span><span style="color:#185369; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>e<span style="font-weight: bold">)]}])</span></pre></td></tr></table><img width="300px" src="images/sp100eigen.png" /><br/><h2>Sparse matrix inversion</h2><h3>Minimum residue</h3><h4>API</h4><a name="invert_minimum_residue"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>invert_minimum_residue<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>x<span style="font-weight: bold">,</span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">,</span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">,</span>ns<span style="font-weight: bold">=</span><span style="color: red">200</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h3>Stabilized bi-conjugate gradient</h3><h4>API</h4><a name="invert_bicgstab"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>invert_bicgstab<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>x<span style="font-weight: bold">,</span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">,</span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">,</span>ns<span style="font-weight: bold">=</span><span style="color: red">200</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.<br/>12.<br/>13.<br/>14.<br/>15.<br/>16.<br/>17.<br/>18.<br/>19.<br/>20.<br/>21.<br/>22.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>m <span style="font-weight: bold">= </span><span style="color: red">30<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>x <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">(</span>m<span style="font-weight: bold">*</span>m<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span>fill<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">lambda </span>r<span style="font-weight: bold">,</span>c<span style="font-weight: bold">:(</span>r<span style="font-weight: bold">//</span>m <span style="color:#185369; font-weight: bold">in</span><span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">,</span><span style="color: red">20</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">or </span>r<span style="font-weight: bold">%</span>m <span style="color:#185369; font-weight: bold">in</span><span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">,</span><span style="color: red">20</span><span style="font-weight: bold">)) </span><span style="color:#185369; font-weight: bold">and </span><span style="color: red">1</span><span style="font-weight: bold">. </span><span style="color:#185369; font-weight: bold">or </span><span style="color: red">0</span><span style="font-weight: bold">.)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>smear<span style="font-weight: bold">(</span>x<span style="font-weight: bold">):<br/>...     </span>alpha<span style="font-weight: bold">, </span>beta <span style="font-weight: bold">= </span><span style="color: red">0.4</span><span style="font-weight: bold">, </span><span style="color: red">8<br/></span><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">for </span>k <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>beta<span style="font-weight: bold">):<br/>...        </span>y <span style="font-weight: bold">= </span>Matrix<span style="font-weight: bold">(</span>x<span style="font-weight: bold">.</span>rows<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)<br/>...        </span><span style="color:#185369; font-weight: bold">for </span>r <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>m<span style="font-weight: bold">):<br/>...            </span><span style="color:#185369; font-weight: bold">for </span>c <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>m<span style="font-weight: bold">):<br/>...                </span>y<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">] = (</span><span style="color: red">1.0</span><span style="font-weight: bold">-</span>alpha<span style="font-weight: bold">/</span><span style="color: red">4</span><span style="font-weight: bold">)*</span>x<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]<br/>...                </span><span style="color:#185369; font-weight: bold">if </span>c<span style="font-weight: bold">&lt;</span>m<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">: </span>y<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">] += </span>alpha <span style="font-weight: bold">* </span>x<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">+</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]<br/>...                </span><span style="color:#185369; font-weight: bold">if </span>c<span style="font-weight: bold">&gt;</span><span style="color: red">0</span><span style="font-weight: bold">:   </span>y<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">] += </span>alpha <span style="font-weight: bold">* </span>x<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]<br/>...                </span><span style="color:#185369; font-weight: bold">if </span>r<span style="font-weight: bold">&lt;</span>m<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">: </span>y<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">] += </span>alpha <span style="font-weight: bold">* </span>x<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">+</span>m<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]<br/>...                </span><span style="color:#185369; font-weight: bold">if </span>c<span style="font-weight: bold">&gt;</span><span style="color: red">0</span><span style="font-weight: bold">:   </span>y<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">] += </span>alpha <span style="font-weight: bold">* </span>x<span style="font-weight: bold">[</span>r<span style="font-weight: bold">*</span>m<span style="font-weight: bold">+</span>c<span style="font-weight: bold">-</span>m<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">]<br/>...        </span>x <span style="font-weight: bold">= </span>y<br/><span style="font-weight: bold">...     </span><span style="color:#185369; font-weight: bold">return </span>y<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>y <span style="font-weight: bold">= </span>smear<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>z <span style="font-weight: bold">= </span>invert_minimum_residue<span style="font-weight: bold">(</span>smear<span style="font-weight: bold">,</span>y<span style="font-weight: bold">,</span>ns<span style="font-weight: bold">=</span><span style="color: red">1000</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>y<span style="font-weight: bold">.</span>cols <span style="font-weight: bold">= </span>y<span style="font-weight: bold">.</span>rows <span style="font-weight: bold">= </span>z<span style="font-weight: bold">.</span>rows <span style="font-weight: bold">= </span>z<span style="font-weight: bold">.</span>cols <span style="font-weight: bold">= </span>m<br/><span style="font-weight: bold">&gt;&gt;&gt; </span>color2d<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">"Defocused image"</span><span style="font-weight: bold">, </span>data <span style="font-weight: bold">= </span>y<span style="font-weight: bold">.</span>as_list<span style="font-weight: bold">(),<br/>...         </span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/defocused.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>color2d<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">"refocus image"</span><span style="font-weight: bold">, </span>data <span style="font-weight: bold">= </span>z<span style="font-weight: bold">.</span>as_list<span style="font-weight: bold">(),<br/>...         </span>filename<span style="font-weight: bold">=</span><span style="color: #FF9966">'images/refocused.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/defocused.png" /><br/><img width="300px" src="images/refocused.png" /><br/><h2>Solvers for non-linear equations</h2><h3>Fixed-point method</h3><h4>API</h4><a name="solve_fixed_point"></a><a name="g"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_fixed_point<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">100</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>g<span style="font-weight: bold">(</span>x<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)/</span><span style="color: red">10<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>solve_fixed_point<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span>rp<span style="font-weight: bold">=</span><span style="color: red">0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0</span></pre></td></tr></table><h3>Bisection method</h3><h4>API</h4><a name="solve_bisection"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_bisection<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">100</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>solve_bisection<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span><span style="color: red">3.0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0</span></pre></td></tr></table><h3>Newton method</h3><h4>API</h4><a name="solve_newton"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_newton<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>solve_newton<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1.0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0</span></pre></td></tr></table><h3>Secant method</h3><h4>API</h4><a name="solve_secant"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_secant<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>solve_secant<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1.0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0</span></pre></td></tr></table><h3>Newton Stabilized</h3><h4>API</h4><a name="solve_newton_stabilized"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_newton_stabilized<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>solve_newton_stabilized<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1.0</span><span style="font-weight: bold">,</span><span style="color: red">3.0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0</span></pre></td></tr></table><h2>Optimization in one dimension</h2><h3>Bisection method</h3><h4>API</h4><a name="optimize_bisection"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_bisection<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">100</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>optimize_bisection<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">2.0</span><span style="font-weight: bold">,</span><span style="color: red">5.0</span><span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">3.5</span></pre></td></tr></table><h3>Newton method</h3><h4>API</h4><a name="optimize_newton"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_newton<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>optimize_newton<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">3.0</span><span style="font-weight: bold">),</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">3.5</span></pre></td></tr></table><h3>Secant method</h3><h4>API</h4><a name="optimize_secant"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_secant<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">100</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>optimize_secant<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">3.0</span><span style="font-weight: bold">),</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">3.5</span></pre></td></tr></table><h3>Newton stabilized</h3><h4>API</h4><a name="optimize_newton_stabilized"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_newton_stabilized<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>optimize_newton_stabilized<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">2.0</span><span style="font-weight: bold">,</span><span style="color: red">5.0</span><span style="font-weight: bold">),</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">3.5</span></pre></td></tr></table><h3>Golden-section search</h3><h4>API</h4><a name="optimize_golden_search"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_golden_search<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">100</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)*(</span>x<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>optimize_golden_search<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">2.0</span><span style="font-weight: bold">,</span><span style="color: red">5.0</span><span style="font-weight: bold">),</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">3.5</span></pre></td></tr></table><h2>Functions of many variables</h2><h4>API</h4><a name="partial"></a><a name="df"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>partial<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>i<span style="font-weight: bold">,</span>h<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>df<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,</span>f<span style="font-weight: bold">=</span>f<span style="font-weight: bold">,</span>i<span style="font-weight: bold">=</span>i<span style="font-weight: bold">,</span>h<span style="font-weight: bold">=</span>h<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="color: red">2.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span><span style="color: red">3.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]+</span><span style="color: red">5.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]*</span>x<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>df0 <span style="font-weight: bold">= </span>partial<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>df1 <span style="font-weight: bold">= </span>partial<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>df2 <span style="font-weight: bold">= </span>partial<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span><span style="color: red">2</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>x <span style="font-weight: bold">= (</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>round<span style="font-weight: bold">(</span>df0<span style="font-weight: bold">(</span>x<span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">), </span>round<span style="font-weight: bold">(</span>df1<span style="font-weight: bold">(</span>x<span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">), </span>round<span style="font-weight: bold">(</span>df2<span style="font-weight: bold">(</span>x<span style="font-weight: bold">),</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">2.0 8.0 5.0</span></pre></td></tr></table><h3>Jacobian, Gradient and Hessian</h3><h4>API</h4><a name="gradient"></a><a name="hessian"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>gradient<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>h<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>hessian<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>h<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="color: red">2.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span><span style="color: red">3.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]+</span><span style="color: red">5.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]*</span>x<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>gradient<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">=(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)))<br/>[[</span><span style="color: red">1.999999</span><span style="font-weight: bold">...], [</span><span style="color: red">7.999999</span><span style="font-weight: bold">...], [</span><span style="color: red">4.999999</span><span style="font-weight: bold">...]]<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>hessian<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">=(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)))<br/>[[</span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">], [</span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">5.000000</span><span style="font-weight: bold">...], [</span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">5.000000</span><span style="font-weight: bold">..., </span><span style="color: red">0.0</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h4>API</h4><a name="jacobian"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>jacobian<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>h<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span><span style="color: red">2.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span><span style="color: red">3.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]+</span><span style="color: red">5.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]*</span>x<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">], </span><span style="color: red">2.0</span><span style="font-weight: bold">*</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>jacobian<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">=(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)))<br/>[[</span><span style="color: red">1.9999999</span><span style="font-weight: bold">..., </span><span style="color: red">7.999999</span><span style="font-weight: bold">..., </span><span style="color: red">4.9999999</span><span style="font-weight: bold">...], [</span><span style="color: red">1.9999999</span><span style="font-weight: bold">..., </span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">]]</span></pre></td></tr></table><h3>Newton method (solver)</h3><h4>API</h4><a name="solve_newton_multi"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>solve_newton_multi<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">], </span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]+</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]**</span><span style="color: red">2</span><span style="font-weight: bold">-</span><span style="color: red">2</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>solve_newton_multi<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">=(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)))<br/>[</span><span style="color: red">1.0</span><span style="font-weight: bold">..., -</span><span style="color: red">1.0</span><span style="font-weight: bold">...]</span></pre></td></tr></table><h3>Newton method (optimize)</h3><h4>API</h4><a name="optimize_newton_multi"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_newton_multi<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>f<span style="font-weight: bold">(</span>x<span style="font-weight: bold">): </span><span style="color:#185369; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]-</span><span style="color: red">2</span><span style="font-weight: bold">)**</span><span style="color: red">2</span><span style="font-weight: bold">+(</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]-</span><span style="color: red">3</span><span style="font-weight: bold">)**</span><span style="color: red">2<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>optimize_newton_multi<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">=(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)))<br/>[</span><span style="color: red">2.0</span><span style="font-weight: bold">, </span><span style="color: red">3.0</span><span style="font-weight: bold">]</span></pre></td></tr></table><h3>Improved Newton method (optimize)</h3><h4>API</h4><a name="optimize_newton_multi_imporved"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>optimize_newton_multi_imporved<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">, </span>h<span style="font-weight: bold">=</span><span style="color: red">10.0</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h2>Non-linear fitting</h2><h4>API</h4><a name="fit"></a><a name="g"></a><a name="core"></a><a name="g"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>fit<span style="font-weight: bold">(</span>data<span style="font-weight: bold">, </span>fs<span style="font-weight: bold">, </span>b<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-6</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">200</span><span style="font-weight: bold">, </span>constraint<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">):...<br/>        </span><span style="color:#185369; font-weight: bold">def </span>g<span style="font-weight: bold">(</span>b<span style="font-weight: bold">, </span>data<span style="font-weight: bold">=</span>data<span style="font-weight: bold">, </span>f<span style="font-weight: bold">=</span>fs<span style="font-weight: bold">, </span>constraint<span style="font-weight: bold">=</span>constraint<span style="font-weight: bold">):...<br/>        </span><span style="color:#185369; font-weight: bold">def </span>core<span style="font-weight: bold">(</span>b<span style="font-weight: bold">,</span>data<span style="font-weight: bold">=</span>data<span style="font-weight: bold">,</span>fs<span style="font-weight: bold">=</span>fs<span style="font-weight: bold">):...<br/>        </span><span style="color:#185369; font-weight: bold">def </span>g<span style="font-weight: bold">(</span>b<span style="font-weight: bold">,</span>data<span style="font-weight: bold">=</span>data<span style="font-weight: bold">,</span>fs<span style="font-weight: bold">=</span>fs<span style="font-weight: bold">,</span>constraint<span style="font-weight: bold">=</span>constraint<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>data <span style="font-weight: bold">= [(</span>i<span style="font-weight: bold">, </span>i<span style="font-weight: bold">+</span><span style="color: red">2.0</span><span style="font-weight: bold">*</span>i<span style="font-weight: bold">**</span><span style="color: red">2</span><span style="font-weight: bold">+</span><span style="color: red">300.0</span><span style="font-weight: bold">/(</span>i<span style="font-weight: bold">+</span><span style="color: red">10</span><span style="font-weight: bold">), </span><span style="color: red">2.0</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">10</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>fs <span style="font-weight: bold">= [(</span><span style="color:#185369; font-weight: bold">lambda </span>b<span style="font-weight: bold">,</span>x<span style="font-weight: bold">: </span>x<span style="font-weight: bold">), (</span><span style="color:#185369; font-weight: bold">lambda </span>b<span style="font-weight: bold">,</span>x<span style="font-weight: bold">: </span>x<span style="font-weight: bold">*</span>x<span style="font-weight: bold">), (</span><span style="color:#185369; font-weight: bold">lambda </span>b<span style="font-weight: bold">,</span>x<span style="font-weight: bold">: </span><span style="color: red">1.0</span><span style="font-weight: bold">/(</span>x<span style="font-weight: bold">+</span>b<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]))]<br/>&gt;&gt;&gt; </span>ab<span style="font-weight: bold">, </span>chi2 <span style="font-weight: bold">= </span>fit<span style="font-weight: bold">(</span>data<span style="font-weight: bold">,</span>fs<span style="font-weight: bold">,[</span><span style="color: red">5</span><span style="font-weight: bold">])<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>ab<span style="font-weight: bold">, </span>chi2<span style="font-weight: bold">)<br/>[</span><span style="color: red">0.999</span><span style="font-weight: bold">..., </span><span style="color: red">2.000</span><span style="font-weight: bold">..., </span><span style="color: red">300.000</span><span style="font-weight: bold">..., </span><span style="color: red">10.000</span><span style="font-weight: bold">...] ...</span></pre></td></tr></table><h2>Integration</h2><img width="300px" src="images/integral1.png" /><br/><img width="300px" src="images/integral2.png" /><br/><h4>API</h4><a name="integrate_naive"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>integrate_naive<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>n<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>API</h4><a name="integrate"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">def </span>integrate<span style="font-weight: bold">(</span>f<span style="font-weight: bold">, </span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">, </span>ap<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>rp<span style="font-weight: bold">=</span><span style="color: red">1e-4</span><span style="font-weight: bold">, </span>ns<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.<br/>11.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>math <span style="color:#185369; font-weight: bold">import </span>sin<span style="font-weight: bold">, </span>cos<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.6020</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.8958</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">8</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.9666</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.9899</span><span style="font-weight: bold">...<br/>&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">1.0</span><span style="font-weight: bold">-</span>cos<span style="font-weight: bold">(</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.9899</span><span style="font-weight: bold">...</span></pre></td></tr></table><h3>Quadrature</h3><h4>API</h4><a name="QuadratureIntegrator"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>QuadratureIntegrator<span style="font-weight: bold">:<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>delta<span style="font-weight: bold">,</span>order<span style="font-weight: bold">=</span><span style="color: red">4</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>integrate<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>f<span style="font-weight: bold">,</span>a<span style="font-weight: bold">):...<br/></span><span style="color:#185369; font-weight: bold">def </span>integrate_quadrature_naive<span style="font-weight: bold">(</span>f<span style="font-weight: bold">,</span>a<span style="font-weight: bold">,</span>b<span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">20</span><span style="font-weight: bold">,</span>order<span style="font-weight: bold">=</span><span style="color: red">4</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">from </span>math <span style="color:#185369; font-weight: bold">import </span>sin<br/><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_quadrature_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">,</span>order<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.60208248595<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_quadrature_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">,</span>order<span style="font-weight: bold">=</span><span style="color: red">3</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.99373945223<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">print</span><span style="font-weight: bold">(</span>integrate_quadrature_naive<span style="font-weight: bold">(</span>sin<span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">3</span><span style="font-weight: bold">,</span>n<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">,</span>order<span style="font-weight: bold">=</span><span style="color: red">4</span><span style="font-weight: bold">))<br/></span><span style="color: red">1.99164529955</span></pre></td></tr></table><h2>Artificial Intelligence and Machine Learning</h2><h3>Clustering Algorithms</h3><img width="300px" src="images/dendrogram.png" /><br/><h4>API</h4><a name="Cluster"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>Cluster<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>points<span style="font-weight: bold">,</span>metric<span style="font-weight: bold">,</span>weights<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">None</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>parent<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>i<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>step<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>find<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">,</span>k<span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span><span style="color:#185369; font-weight: bold">def </span>metric<span style="font-weight: bold">(</span>a<span style="font-weight: bold">,</span>b<span style="font-weight: bold">):<br/>...     </span><span style="color:#185369; font-weight: bold">return </span>math<span style="font-weight: bold">.</span>sqrt<span style="font-weight: bold">(</span>sum<span style="font-weight: bold">((</span>x<span style="font-weight: bold">-</span>b<span style="font-weight: bold">[</span>i<span style="font-weight: bold">])**</span><span style="color: red">2 </span><span style="color:#185369; font-weight: bold">for </span>i<span style="font-weight: bold">,</span>x <span style="color:#185369; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>a<span style="font-weight: bold">)))<br/>&gt;&gt;&gt; </span>points <span style="font-weight: bold">= [[</span>random<span style="font-weight: bold">.</span>gauss<span style="font-weight: bold">(</span>i <span style="font-weight: bold">% </span><span style="color: red">5</span><span style="font-weight: bold">,</span><span style="color: red">0.3</span><span style="font-weight: bold">) </span><span style="color:#185369; font-weight: bold">for </span>j <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">10</span><span style="font-weight: bold">)] </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">200</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>c <span style="font-weight: bold">= </span>Cluster<span style="font-weight: bold">(</span>points<span style="font-weight: bold">,</span>metric<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>r<span style="font-weight: bold">, </span>clusters <span style="font-weight: bold">= </span>c<span style="font-weight: bold">.</span>find<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># cluster all points until one cluster only<br/></span><span style="font-weight: bold">&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'clustering example'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'distance'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'number of clusters'</span><span style="font-weight: bold">,<br/>...      </span>linesets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:</span>c<span style="font-weight: bold">.</span>dd<span style="font-weight: bold">[</span><span style="color: red">150</span><span style="font-weight: bold">:]}], </span>filename <span style="font-weight: bold">= </span><span style="color: #FF9966">'clustering1.png'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>draw<span style="font-weight: bold">(</span>title<span style="font-weight: bold">=</span><span style="color: #FF9966">'clustering example (2d projection)'</span><span style="font-weight: bold">,</span>xlab<span style="font-weight: bold">=</span><span style="color: #FF9966">'p[0]'</span><span style="font-weight: bold">,</span>ylab<span style="font-weight: bold">=</span><span style="color: #FF9966">'p[1]'</span><span style="font-weight: bold">,<br/>...      </span>ellisets <span style="font-weight: bold">= [{</span><span style="color: #FF9966">'data'</span><span style="font-weight: bold">:[</span>p<span style="font-weight: bold">[:</span><span style="color: red">2</span><span style="font-weight: bold">] </span><span style="color:#185369; font-weight: bold">for </span>p <span style="color:#185369; font-weight: bold">in </span>points<span style="font-weight: bold">]}],</span>filename <span style="font-weight: bold">= </span><span style="color: #FF9966">'clustering2.png'</span><span style="font-weight: bold">)</span></pre></td></tr></table><img width="300px" src="images/clustering1.png" /><br/><img width="300px" src="images/clustering2.png" /><br/><h3>Neural Network</h3><img width="300px" src="images/neural.png" /><br/><h4>API</h4><a name="NeuralNetwork"></a><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.<br/>9.<br/>10.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="color:#185369; font-weight: bold">class </span>NeuralNetwork<span style="font-weight: bold">:<br/>    </span><span style="color:#185369; font-weight: bold">def </span>rand<span style="font-weight: bold">(</span>a<span style="font-weight: bold">, </span>b<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>sigmoid<span style="font-weight: bold">(</span>x<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>dsigmoid<span style="font-weight: bold">(</span>y<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>ni<span style="font-weight: bold">, </span>nh<span style="font-weight: bold">, </span>no<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>update<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>inputs<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>back_propagate<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>targets<span style="font-weight: bold">, </span>N<span style="font-weight: bold">, </span>M<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>test<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>patterns<span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>weights<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">):...<br/>    </span><span style="color:#185369; font-weight: bold">def </span>train<span style="font-weight: bold">(</span><span style="color:#185369; font-weight: bold">self</span><span style="font-weight: bold">, </span>patterns<span style="font-weight: bold">, </span>iterations<span style="font-weight: bold">=</span><span style="color: red">1000</span><span style="font-weight: bold">, </span>N<span style="font-weight: bold">=</span><span style="color: red">0.5</span><span style="font-weight: bold">, </span>M<span style="font-weight: bold">=</span><span style="color: red">0.1</span><span style="font-weight: bold">, </span>check<span style="font-weight: bold">=</span><span style="color:#185369; font-weight: bold">False</span><span style="font-weight: bold">):...</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>pat <span style="font-weight: bold">= [[[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">], [</span><span style="color: red">0</span><span style="font-weight: bold">]], [[</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">], [</span><span style="color: red">1</span><span style="font-weight: bold">]], [[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">], [</span><span style="color: red">1</span><span style="font-weight: bold">]], [[</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">], [</span><span style="color: red">0</span><span style="font-weight: bold">]]]<br/>&gt;&gt;&gt; </span>n <span style="font-weight: bold">= </span>NeuralNetwork<span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>n<span style="font-weight: bold">.</span>train<span style="font-weight: bold">(</span>pat<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>n<span style="font-weight: bold">.</span>test<span style="font-weight: bold">(</span>pat<span style="font-weight: bold">)<br/>[</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">] -&gt; [</span><span style="color: red">0.00</span><span style="font-weight: bold">...]<br/>[</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">] -&gt; [</span><span style="color: red">0.98</span><span style="font-weight: bold">...]<br/>[</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">] -&gt; [</span><span style="color: red">0.98</span><span style="font-weight: bold">...]<br/>[</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">] -&gt; [-</span><span style="color: red">0.00</span><span style="font-weight: bold">...]</span></pre></td></tr></table><h4>Usage Example</h4><table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;
">1.<br/>2.<br/>3.<br/>4.<br/>5.<br/>6.<br/>7.<br/>8.</pre></td><td><pre style="
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;
"><span style="font-weight: bold">&gt;&gt;&gt; </span>storage <span style="font-weight: bold">= </span>PersistentDictionary<span style="font-weight: bold">(</span><span style="color: #FF9966">'sp100.sqlite'</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>v <span style="font-weight: bold">= [</span>day<span style="font-weight: bold">[</span><span style="color: #FF9966">'arithmetic_return'</span><span style="font-weight: bold">]*</span><span style="color: red">300 </span><span style="color:#185369; font-weight: bold">for </span>day <span style="color:#185369; font-weight: bold">in </span>storage<span style="font-weight: bold">[</span><span style="color: #FF9966">'AAPL/2011'</span><span style="font-weight: bold">][</span><span style="color: red">1</span><span style="font-weight: bold">:]]<br/>&gt;&gt;&gt; </span>pat <span style="font-weight: bold">= [[</span>v<span style="font-weight: bold">[</span>i<span style="font-weight: bold">:</span>i<span style="font-weight: bold">+</span><span style="color: red">5</span><span style="font-weight: bold">],[</span>v<span style="font-weight: bold">[</span>i<span style="font-weight: bold">+</span><span style="color: red">5</span><span style="font-weight: bold">]]] </span><span style="color:#185369; font-weight: bold">for </span>i <span style="color:#185369; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>len<span style="font-weight: bold">(</span>v<span style="font-weight: bold">)-</span><span style="color: red">5</span><span style="font-weight: bold">)]<br/>&gt;&gt;&gt; </span>n <span style="font-weight: bold">= </span>NeuralNetwork<span style="font-weight: bold">(</span><span style="color: red">5</span><span style="font-weight: bold">, </span><span style="color: red">5</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>n<span style="font-weight: bold">.</span>train<span style="font-weight: bold">(</span>pat<span style="font-weight: bold">)<br/>&gt;&gt;&gt; </span>predictions <span style="font-weight: bold">= [</span>n<span style="font-weight: bold">.</span>update<span style="font-weight: bold">(</span>item<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]) </span><span style="color:#185369; font-weight: bold">for </span>item <span style="color:#185369; font-weight: bold">in </span>pat<span style="font-weight: bold">]<br/>&gt;&gt;&gt; </span>success_rate <span style="font-weight: bold">= </span>sum<span style="font-weight: bold">(</span><span style="color: red">1.0 </span><span style="color:#185369; font-weight: bold">for </span>i<span style="font-weight: bold">,</span>e <span style="color:#185369; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>predictions<span style="font-weight: bold">)<br/>...                    </span><span style="color:#185369; font-weight: bold">if </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]*</span>v<span style="font-weight: bold">[</span>i<span style="font-weight: bold">+</span><span style="color: red">5</span><span style="font-weight: bold">]&gt;</span><span style="color: red">0</span><span style="font-weight: bold">)/</span>len<span style="font-weight: bold">(</span>pat<span style="font-weight: bold">)</span></pre></td></tr></table>
</body></html>