<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <link rel="icon" type="image/gif" href="favicon.gif"/> <link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" /> <link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" /> <title>Numerical Analysis & Statistics: MATLAB, R, NumPy, Julia - Hyperpolyglot</title> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script> <style type="text/css" id="internal-style"> @import url(hyperpolyglot.css); </style> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <meta http-equiv="content-language" content="en"/> </head> <body> <div id="container-wrap-wrap"> <div id="container-wrap"> <div id="container"> <div id="header"> <h1><a href="index.html"><span>Hyperpolyglot</span></a></h1> </div> <div id="content-wrap"> <div id="main-content"> <div id="page-title"> Numerical Analysis & Statistics: MATLAB, R, NumPy, Julia </div> <div id="page-content"> <p><a name="top" shape="rect" id="top"></a><em>a side-by-side reference sheet</em></p> <p><strong>sheet one:</strong> <a href="numerical-analysis#grammar-invocation" shape="rect">grammar and invocation</a> | <a href="numerical-analysis#var-expr" shape="rect">variables and expressions</a> | <a href="numerical-analysis#arithmetic-logic" shape="rect">arithmetic and logic</a> | <a href="numerical-analysis#strings" shape="rect">strings</a> | <a href="numerical-analysis#regexes" shape="rect">regexes</a> | <a href="numerical-analysis#dates-time" shape="rect">dates and time</a> | <a href="numerical-analysis#tuples" shape="rect">tuples</a> | <a href="numerical-analysis#arrays" shape="rect">arrays</a> | <a href="numerical-analysis#arithmetic-sequences" shape="rect">arithmetic sequences</a> | <a href="numerical-analysis#two-d-arrays" shape="rect">2d arrays</a> | <a href="numerical-analysis#three-d-arrays" shape="rect">3d arrays</a> | <a href="numerical-analysis#dictionaries" shape="rect">dictionaries</a> | <a href="numerical-analysis#functions" shape="rect">functions</a> | <a href="numerical-analysis#execution-control" shape="rect">execution control</a> | <a href="numerical-analysis#file-handle" shape="rect">file handles</a> | <a href="numerical-analysis#directories" shape="rect">directories</a> | <a href="numerical-analysis#processes-environment" shape="rect">processes and environment</a> | <a href="numerical-analysis#libraries-namespaces" shape="rect">libraries and namespaces</a> | <a href="numerical-analysis#reflection" shape="rect">reflection</a> | <a href="numerical-analysis#debugging" shape="rect">debugging</a></p> <p><strong><a href="numerical-analysis2" shape="rect">sheet two</a>:</strong> <a href="numerical-analysis2#tables" shape="rect">tables</a> | <a href="numerical-analysis2#import-export" shape="rect">import and export</a> | <a href="numerical-analysis2#relational-algebra" shape="rect">relational algebra</a> | <a href="numerical-analysis2#aggregation" shape="rect">aggregation</a></p> <p><a href="numerical-analysis2#vectors" shape="rect">vectors</a> | <a href="numerical-analysis2#matrices" shape="rect">matrices</a> | <a href="numerical-analysis2#sparse-matrices" shape="rect">sparse matrices</a> | <a href="numerical-analysis2#optimization" shape="rect">optimization</a> | <a href="numerical-analysis2#polynomials" shape="rect">polynomials</a> | <a href="numerical-analysis2#descriptive-statistics" shape="rect">descriptive statistics</a> | <a href="numerical-analysis2#distributions" shape="rect">distributions</a> | <a href="numerical-analysis2#linear-regression" shape="rect">linear regression</a> | <a href="numerical-analysis2#statistical-tests" shape="rect">statistical tests</a> | <a href="numerical-analysis2#time-series" shape="rect">time series</a> | <a href="numerical-analysis2#fast-fourier-transform" shape="rect">fast fourier transform</a> | <a href="numerical-analysis2#clustering" shape="rect">clustering</a> | <a href="numerical-analysis2#images" shape="rect">images</a> | <a href="numerical-analysis2#sound" shape="rect">sound</a></p> <p><a href="numerical-analysis2#bar-charts" shape="rect">bar charts</a> | <a href="numerical-analysis2#scatter-plots" shape="rect">scatter plots</a> | <a href="numerical-analysis2#line-charts" shape="rect">line charts</a> | <a href="numerical-analysis2#surface-charts" shape="rect">surface charts</a> | <a href="numerical-analysis2#chart-options" shape="rect">chart options</a></p> <table class="wiki-content-table"><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="version-used" shape="rect" id="version-used"></a><a href="numerical-analysis#version-used-note" shape="rect">version used</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>MATLAB 8.3</em></span><br /> <br /> <span style="color: gray"><em>Octave 3.8</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>3.1</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>Python 2.7</em></span><br /> <span style="color: gray"><em>NumPy 1.7</em></span><br /> <span style="color: gray"><em>SciPy 0.13</em></span><br /> <span style="color: gray"><em>Pandas 0.12</em></span><br /> <span style="color: gray"><em>Matplotlib 1.3</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>0.4</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="show-version" shape="rect" id="show-version"></a><a href="numerical-analysis#show-version-note" shape="rect">show version</a></td><td rowspan="1" colspan="1">$ matlab -nojvm -nodisplay -r 'exit'<br /> <br /> $ octave <span style="white-space: pre-wrap;">--version</span></td><td rowspan="1" colspan="1">$ R <span style="white-space: pre-wrap;">--version</span></td><td rowspan="1" colspan="1">sys.version<br /> np.<span style="white-space: pre-wrap;">__version__</span><br /> sp.<span style="white-space: pre-wrap;">__version__</span><br /> mpl.<span style="white-space: pre-wrap;">__version__</span></td><td rowspan="1" colspan="1">$ julia <span style="white-space: pre-wrap;">--</span>version</td></tr><tr><td rowspan="1" colspan="1"><a name="implicit-prologue" shape="rect" id="implicit-prologue"></a><a href="numerical-analysis#implicit-prologue-note" shape="rect">implicit prologue</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">install.packages('ggplot2')<br /> library('ggplot2')</td><td rowspan="1" colspan="1">import sys, os, re, math<br /> import numpy as np<br /> import scipy as sp<br /> import scipy.stats as stats<br /> import pandas as pd<br /> import matplotlib as mpl<br /> import matplotlib.pyplot as plt</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="grammar-invocation" shape="rect" id="grammar-invocation"></a><a href="numerical-analysis#grammar-invocation-note" shape="rect">grammar and invocation</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="interpreter" shape="rect" id="interpreter"></a><a href="numerical-analysis#interpreter-note" shape="rect">interpreter</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">$ cat <span style="white-space: pre-wrap;">>></span>foo.m<br /> 1 + 1<br /> exit<br /> <br /> $ matlab -nojvm -nodisplay -r "run('foo.m')"<br /> <br /> $ octave foo.m</td><td rowspan="1" colspan="1">$ cat <span style="white-space: pre-wrap;">>></span>foo.r<br /> 1 + 1<br /> <br /> $ Rscript foo.r<br /> <br /> $ R -f foo.r</td><td rowspan="1" colspan="1">$ cat <span style="white-space: pre-wrap;">>></span>foo.py<br /> print(1 + 1)<br /> <br /> $ python foo.py</td><td rowspan="1" colspan="1">$ cat <span style="white-space: pre-wrap;">>></span>foo.jl<br /> println(1 + 1)<br /> <br /> $ julia foo.jl</td></tr><tr><td rowspan="1" colspan="1"><a name="repl" shape="rect" id="repl"></a><a href="numerical-analysis#repl-note" shape="rect">repl</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">$ matlab -nojvm -nodisplay<br /> <br /> $ octave</td><td rowspan="1" colspan="1">$ R</td><td rowspan="1" colspan="1">$ python</td><td rowspan="1" colspan="1">$ julia</td></tr><tr><td rowspan="1" colspan="1"><a name="cmd-line-program" shape="rect" id="cmd-line-program"></a><a href="numerical-analysis#cmd-line-program-note" shape="rect">command line program</a></td><td rowspan="1" colspan="1">$ matlab -nojvm -nodisplay -r 'disp(1 + 1); exit'<br /> <br /> $ octave <span style="white-space: pre-wrap;">--</span>silent <span style="white-space: pre-wrap;">--</span>eval '1 + 1'</td><td rowspan="1" colspan="1">$ Rscript -e 'print("hi")'</td><td rowspan="1" colspan="1">python -c 'print("hi")'</td><td rowspan="1" colspan="1">$ julia -e 'println("hi")'</td></tr><tr><td rowspan="1" colspan="1"><a name="block-delimiters" shape="rect" id="block-delimiters"></a><a href="numerical-analysis#block-delimiters-note" shape="rect">block delimiters</a></td><td rowspan="1" colspan="1">function end<br /> if elseif else end<br /> while end<br /> for end</td><td rowspan="1" colspan="1">{ }</td><td rowspan="1" colspan="1"><span style="color: gray"><em>offside rule</em></span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="stmt-separator" shape="rect" id="stmt-separator"></a><a href="numerical-analysis#stmt-separator-note" shape="rect">statement separator</a></td><td rowspan="1" colspan="1">; <span style="color: gray"><em>or newline</em></span><br /> <br /> <span style="color: gray"><em>Newlines not separators after three dots:</em> <span style="white-space: pre-wrap;">...</span></span><br /> <br /> <span style="color: gray"><em>Output is suppressed when lines end with a semicolon.</em></span></td><td rowspan="1" colspan="1">; <span style="color: gray"><em>or sometimes newline</em></span><br /> <br /> <span style="color: gray"><em>Newlines not separators inside (), [], {}, '', "", or after binary operator.</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>newline or ;<br /> <br /> Newlines not separators inside (), [], {}, triple quote literals, or after backslash: \</em></span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="eol-comment" shape="rect" id="eol-comment"></a><a href="numerical-analysis#eol-comment-note" shape="rect">end-of-line comment</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">1 + 1 <span style="color: gray">% addition</span></td><td rowspan="1" colspan="1">1 + 1 <span style="color: gray"># addition</span></td><td rowspan="1" colspan="1">1 + 1 <span style="color: gray"># addition</span></td><td rowspan="1" colspan="1">1 + 1 <span style="color: gray"># addition</span></td></tr><tr><th colspan="5" rowspan="1"><a name="var-expr" shape="rect" id="var-expr"></a><a href="numerical-analysis#var-expr-note" shape="rect">variables and expressions</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="assignment" shape="rect" id="assignment"></a><a href="numerical-analysis#assignment-note" shape="rect">assignment</a></td><td rowspan="1" colspan="1">i = 3</td><td rowspan="1" colspan="1">i = 3<br /> i <- 3<br /> 3 -> i<br /> assign("i", 3)</td><td rowspan="1" colspan="1">i = 3</td><td rowspan="1" colspan="1">i = 3</td></tr><tr><td rowspan="1" colspan="1"><a name="parallel-assignment" shape="rect" id="parallel-assignment"></a><a href="numerical-analysis#parallel-assignment-note" shape="rect">parallel assignment</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="swap" shape="rect" id="swap"></a><a href="numerical-analysis#swap-note" shape="rect">swap</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="compound-assignment" shape="rect" id="compound-assignment"></a><a href="numerical-analysis#compound-assignment-note" shape="rect">compound assignment</a><br /> <span style="color: gray"><em>arithmetic, string, logical</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"># do not return values:</span><br /> += -= *= /= <span style="white-space: pre-wrap;">//</span>= %= <span style="white-space: pre-wrap;">**</span>=<br /> += *=<br /> &= |= ^=</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="null" shape="rect" id="null"></a><a href="numerical-analysis#null-note" shape="rect">null</a></td><td rowspan="1" colspan="1"><span style="color: gray">% Only used in place of numeric values:</span><br /> NaN</td><td rowspan="1" colspan="1">NA NULL</td><td rowspan="1" colspan="1">None np.nan<br /> <br /> <span style="color: gray"># None cannot be stored in a numpy array;<br /> # np.nan can if dtype is float64.</span></td><td rowspan="1" colspan="1"><span style="color: gray"># Only used in place of float values:</span><br /> NaN</td></tr><tr><td rowspan="1" colspan="1"><a name="null-test" shape="rect" id="null-test"></a><a href="numerical-analysis#null-test-note" shape="rect">null test</a></td><td rowspan="1" colspan="1">isnan(v)<br /> <br /> <span style="color: gray">% true for '', []:</span><br /> isempty(v)</td><td rowspan="1" colspan="1">is.na(v)<br /> is.null(v)</td><td rowspan="1" colspan="1">v == None<br /> v is None<br /> <br /> np.isnan(np.nan)<br /> <span style="color: gray"># np.nan == np.nan is False</span></td><td rowspan="1" colspan="1">isnan(v)</td></tr><tr><td rowspan="1" colspan="1"><a name="cond-expr" shape="rect" id="cond-expr"></a><a href="numerical-analysis#cond-expr-note" shape="rect">conditional expression</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">(if (x > 0) x else -x)<br /> ifelse(x > 0, x, -x)</td><td rowspan="1" colspan="1">x if x > 0 else -x</td><td rowspan="1" colspan="1">x > 0 ? x : -x</td></tr><tr><th colspan="5" rowspan="1"><a name="arithmetic-logic" shape="rect" id="arithmetic-logic"></a><a href="numerical-analysis#arithmetic-logic-note" shape="rect">arithmetic and logic</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="boolean-type" shape="rect" id="boolean-type"></a><a href="numerical-analysis#boolean-type-note" shape="rect">boolean type</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"># Python:</span><br /> bool<br /> <br /> <span style="color: gray"># NumPy:</span><br /> bool_</td><td rowspan="1" colspan="1">Bool</td></tr><tr><td rowspan="1" colspan="1"><a name="true-false" shape="rect" id="true-false"></a><a href="numerical-analysis#true-false-note" shape="rect">true and false</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">1 0 true false</td><td rowspan="1" colspan="1">TRUE FALSE T F</td><td rowspan="1" colspan="1">True False</td><td rowspan="1" colspan="1">true false</td></tr><tr><td rowspan="1" colspan="1"><a name="falsehoods" shape="rect" id="falsehoods"></a><a href="numerical-analysis#falsehoods-note" shape="rect">falsehoods</a></td><td rowspan="1" colspan="1">false 0 0.0<br /> <span style="color: gray"><em>matrices evaluate to false unless nonempty and all entries evaluate to true</em></span></td><td rowspan="1" colspan="1">FALSE F 0 0.0<br /> <span style="color: gray"><em>matrices evaluate to value of first entry; string in boolean context causes error</em></span></td><td rowspan="1" colspan="1">False None 0 0.0 '' [] {}</td><td rowspan="1" colspan="1">false</td></tr><tr><td rowspan="1" colspan="1"><a name="logical-op" shape="rect" id="logical-op"></a><a href="numerical-analysis#logical-op-note" shape="rect">logical operators</a></td><td rowspan="1" colspan="1">~true | (true & false)<br /> <br /> <span style="color: gray">% short-circuit operators:</span><br /> && <span style="white-space: pre-wrap;">||</span></td><td rowspan="1" colspan="1">!TRUE | (TRUE & FALSE)<br /> <span style="color: gray"><em>short-circuit operators:</em></span><br /> && <span style="white-space: pre-wrap;">||</span><br /> <br /> <span style="color: gray">& <em>and</em> | <em>can operate on and return vectors, but</em> <span style="white-space: pre-wrap;">&&</span> <em>and</em> <span style="white-space: pre-wrap;">||</span> <em>return scalars</em></span></td><td rowspan="1" colspan="1">and or not</td><td rowspan="1" colspan="1"><span style="white-space: pre-wrap;">&& || !</span></td></tr><tr><td rowspan="1" colspan="1"><a name="relational-op" shape="rect" id="relational-op"></a><a href="numerical-analysis#relational-op-note" shape="rect">relational operators</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">== ~= > < >= <=</td><td rowspan="1" colspan="1">== != > < >= <=</td><td rowspan="1" colspan="1">== != > < >= <=</td><td rowspan="1" colspan="1">== != > < >= <=</td></tr><tr><td rowspan="1" colspan="1"><a name="int-type" shape="rect" id="int-type"></a><a href="numerical-analysis#int-type-note" shape="rect">integer type</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"># Python:</span><br /> int<br /> <br /> <span style="color: gray"># NumPy:</span><br /> int8 int16 int32 int64</td><td rowspan="1" colspan="1">Int8 Int16 Int32 Int64 Int128<br /> <br /> Int <span style="color: gray"><em>is either 32 or 64 bits, depending on</em></span> WORD_SIZE</td></tr><tr><td rowspan="1" colspan="1"><a name="unsigned-type" shape="rect" id="unsigned-type"></a><a href="numerical-analysis#unsigned-type-note" shape="rect">unsigned type</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">uint8 uint16 uint32 uint64</td><td rowspan="1" colspan="1">UInt8 UInt16 UInt32 UInt64 UInt128<br /> <br /> UInt <span style="color: gray"><em>is either 32 or 64 bits, depending on</em></span> WORD_SIZE</td></tr><tr><td rowspan="1" colspan="1"><a name="float-type" shape="rect" id="float-type"></a><a href="numerical-analysis#float-type-note" shape="rect">float type</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"># Python:</span><br /> float<br /> <br /> <span style="color: gray"># NumPy:</span><br /> float16 float32 float64</td><td rowspan="1" colspan="1">Float16 Float32 Float64</td></tr><tr><td rowspan="1" colspan="1"><a name="arith-op" shape="rect" id="arith-op"></a><a href="numerical-analysis#arith-op-note" shape="rect">arithmetic operators</a><br /> <span style="color: gray"><em>add, sub, mult, div, quot, rem</em></span></td><td rowspan="1" colspan="1">+ - * / <span style="color: gray"><em>none</em></span> mod(<span style="color: gray"><em>n</em></span>, <span style="color: gray"><em>divisor</em></span>)</td><td rowspan="1" colspan="1">+ - * / %/% %%</td><td rowspan="1" colspan="1">+ - * / // %</td><td rowspan="1" colspan="1">+ - * / div(<span style="color: gray"><em>n</em></span>, <span style="color: gray"><em>divisor</em></span>) rem(<span style="color: gray"><em>n</em></span>, <span style="color: gray"><em>divisor</em></span>)<br /> <br /> <span style="color: gray"># always non-negative:</span><br /> mod(<span style="color: gray"><em>n</em></span>, <span style="color: gray"><em>divisor</em></span>)</td></tr><tr><td rowspan="1" colspan="1"><a name="int-div" shape="rect" id="int-div"></a><a href="numerical-analysis#int-div-note" shape="rect">integer division</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">fix(13 / 5)</td><td rowspan="1" colspan="1">13 %/% 5<br /> as.integer(13 / 5)</td><td rowspan="1" colspan="1">13 // 5</td><td rowspan="1" colspan="1">div(13, 5)</td></tr><tr><td rowspan="1" colspan="1"><a name="int-div-zero" shape="rect" id="int-div-zero"></a><a href="numerical-analysis#int-div-zero-note" shape="rect">integer division by zero</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">Inf NaN <span style="color: gray"><em>or</em></span> -Inf</td><td rowspan="1" colspan="1"><span style="color: gray"><em>result of converting Inf or NaN to an integer with as.integer:</em></span><br /> NA</td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> ZeroDivisionError</span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> DivideError</span></td></tr><tr><td rowspan="1" colspan="1"><a name="float-div" shape="rect" id="float-div"></a><a href="numerical-analysis#float-div-note" shape="rect">float division</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">13 / 5</td><td rowspan="1" colspan="1">13 / 5</td><td rowspan="1" colspan="1">float(13) / 5</td><td rowspan="1" colspan="1">13 / 5<br /> 5 \ 13</td></tr><tr><td rowspan="1" colspan="1"><a name="float-div-zero" shape="rect" id="float-div-zero"></a><a href="numerical-analysis#float-div-zero-note" shape="rect">float division by zero</a><br /> <span style="color: gray"><em>dividend is positive, zero, negative</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>these values are literals:</em></span><br /> Inf<br /> NaN<br /> -Inf</td><td rowspan="1" colspan="1"><span style="color: gray"><em>these values are literals:</em></span><br /> Inf<br /> NaN<br /> -Inf</td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> ZeroDivisionError</span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>these values are literals:</em></span><br /> Inf<br /> NaN<br /> -Inf</td></tr><tr><td rowspan="1" colspan="1"><a name="power" shape="rect" id="power"></a><a href="numerical-analysis#power-note" shape="rect">power</a></td><td rowspan="1" colspan="1">2 ^ 16</td><td rowspan="1" colspan="1">2 ^ 16<br /> 2 ** 16</td><td rowspan="1" colspan="1">2 <span style="white-space: pre-wrap;">**</span> 16</td><td rowspan="1" colspan="1">2 ^ 16</td></tr><tr><td rowspan="1" colspan="1"><a name="sqrt" shape="rect" id="sqrt"></a><a href="numerical-analysis#sqrt-note" shape="rect">sqrt</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">sqrt(2)</td><td rowspan="1" colspan="1">sqrt(2)</td><td rowspan="1" colspan="1">math.sqrt(2)</td><td rowspan="1" colspan="1">sqrt(2)</td></tr><tr><td rowspan="1" colspan="1"><a name="sqrt-negative-one" shape="rect" id="sqrt-negative-one"></a><a href="numerical-analysis#sqrt-negative-one-note" shape="rect">sqrt -1</a></td><td rowspan="1" colspan="1"><span style="color: gray">% returns 0 + 1i:</span><br /> sqrt(-1)</td><td rowspan="1" colspan="1"><span style="color: gray"># returns NaN:</span><br /> sqrt(-1)<br /> <br /> <span style="color: gray"># returns 0+1i:</span><br /> sqrt(-1+0i)</td><td rowspan="1" colspan="1"><span style="color: gray"># raises ValueError:</span><br /> math.sqrt(-1)<br /> <br /> <span style="color: gray"># returns 1.41421j:</span><br /> import cmath<br /> cmath.sqrt(-1)</td><td rowspan="1" colspan="1"><span style="color: gray"># raises DomainError:</span><br /> sqrt(-1)<br /> <br /> <span style="color: gray"># returns 0.0 + 1.0im:</span><br /> sqrt(-1 + 0im)</td></tr><tr><td rowspan="1" colspan="1"><a name="transcendental-func" shape="rect" id="transcendental-func"></a><a href="numerical-analysis#transcendental-func-note" shape="rect">transcendental functions</a></td><td rowspan="1" colspan="1">exp log sin cos tan asin acos atan atan2</td><td rowspan="1" colspan="1">exp log sin cos tan asin acos atan atan2</td><td rowspan="1" colspan="1">math.exp math.log math.sin math.cos math.tan math.asin math.acos math.atan math.atan2</td><td rowspan="1" colspan="1">exp log sin cos tan asin acos atan atan2</td></tr><tr><td rowspan="1" colspan="1"><a name="transcendental-const" shape="rect" id="transcendental-const"></a><a href="numerical-analysis#transcendental-const-note" shape="rect">transcendental constants</a></td><td rowspan="1" colspan="1">pi e</td><td rowspan="1" colspan="1">pi exp(1)</td><td rowspan="1" colspan="1">math.pi math.e</td><td rowspan="1" colspan="1">pi e</td></tr><tr><td rowspan="1" colspan="1"><a name="float-truncation" shape="rect" id="float-truncation"></a><a href="numerical-analysis#float-truncation-note" shape="rect">float truncation</a><br /> <span style="color: gray"><em>round towards zero, to nearest integer, down, up</em></span></td><td rowspan="1" colspan="1">fix(x)<br /> round(x)<br /> floor(x)<br /> ceil(x)</td><td rowspan="1" colspan="1">as.integer(x)<br /> round(x)<br /> floor(x)<br /> ceiling(x)</td><td rowspan="1" colspan="1">int(x)<br /> int(round(x))<br /> math.floor(x)<br /> math.ceil(x)</td><td rowspan="1" colspan="1">Int(trunc(x))<br /> Int(round(x))<br /> Int(floor(x))<br /> Int(ceil(x))<br /> <br /> <span style="color: gray"># trunc() and other functions return floats.<br /> # Int() raises InexactError if float argument has<br /> # nonzero fractional portion.</span></td></tr><tr><td rowspan="1" colspan="1"><a name="absolute-val" shape="rect" id="absolute-val"></a><a href="numerical-analysis#absolute-val-note" shape="rect">absolute value</a><br /> <span style="color: gray"><em>and signum</em></span></td><td rowspan="1" colspan="1">abs sign</td><td rowspan="1" colspan="1">abs sign</td><td rowspan="1" colspan="1">abs(-3.7)<br /> math.copysign(1, -3.7)</td><td rowspan="1" colspan="1">abs(-3.7)<br /> sign(-3.7)</td></tr><tr><td rowspan="1" colspan="1"><a name="int-overflow" shape="rect" id="int-overflow"></a><a href="numerical-analysis#int-overflow-note" shape="rect">integer overflow</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>becomes float; largest representable integer in the variable</em> intmax</span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>becomes float; largest representable integer in the variable</em> .Machine$integer.max</span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>becomes arbitrary length integer of type</em> long</span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="float-overflow" shape="rect" id="float-overflow"></a><a href="numerical-analysis#float-overflow-note" shape="rect">float overflow</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">Inf</td><td rowspan="1" colspan="1">Inf</td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> OverflowError</span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="float-limits" shape="rect" id="float-limits"></a><a href="numerical-analysis#float-limits-note" shape="rect">float limits</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">eps<br /> realmax<br /> realmin</td><td rowspan="1" colspan="1">.Machine$double.eps<br /> .Machine$double.xmax<br /> .Machine$double.xmin</td><td rowspan="1" colspan="1">np.finfo(np.float64).eps<br /> np.finfo(np.float64).max<br /> np.finfo(np.float64).min</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="rational-construction" shape="rect" id="rational-construction"></a><a href="numerical-analysis#rational-construction-note" shape="rect">rational construction</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">22 <span style="white-space: pre-wrap;">//</span> 7</td></tr><tr><td rowspan="1" colspan="1"><a name="rational-decomposition" shape="rect" id="rational-decomposition"></a><a href="numerical-analysis#rational-decomposition-note" shape="rect">rational decomposition</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">num(22 <span style="white-space: pre-wrap;">//</span> 7)<br /> den(22 <span style="white-space: pre-wrap;">//</span> 7)</td></tr><tr><td rowspan="1" colspan="1"><a name="complex-types" shape="rect" id="complex-types"></a><a href="numerical-analysis#complex-types-note" shape="rect">complex types</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">complex64 complex128</td><td rowspan="1" colspan="1">Complex32 Complex64 Complex128</td></tr><tr><td rowspan="1" colspan="1"><a name="complex-construction" shape="rect" id="complex-construction"></a><a href="numerical-analysis#complex-construction-note" shape="rect">complex construction</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">1 + 3i</td><td rowspan="1" colspan="1">1 + 3i</td><td rowspan="1" colspan="1">1 + 3j</td><td rowspan="1" colspan="1">1 + 3im<br /> complex(1, 3)</td></tr><tr><td rowspan="1" colspan="1"><a name="complex-decomposition" shape="rect" id="complex-decomposition"></a><a href="numerical-analysis#complex-decomposition-note" shape="rect">complex decomposition</a></td><td rowspan="1" colspan="1">real imag<br /> abs arg<br /> conj</td><td rowspan="1" colspan="1">Re Im<br /> abs Arg<br /> Conj</td><td rowspan="1" colspan="1">import cmath<br /> <br /> z.real<br /> z.imag<br /> cmath.polar(z)[1]</td><td rowspan="1" colspan="1">real(1 + 3im)<br /> imag(1 + 3im)<br /> abs(1 + 3im)<br /> angle(1 + 3im)<br /> conj(1 + 3im)</td></tr><tr><td rowspan="1" colspan="1"><a name="random-num" shape="rect" id="random-num"></a><a href="numerical-analysis#random-num-note" shape="rect">random number</a><br /> <span style="color: gray"><em>uniform integer, uniform float</em></span></td><td rowspan="1" colspan="1">floor(100 * rand)<br /> rand</td><td rowspan="1" colspan="1">floor(100 * runif(1))<br /> runif(1)</td><td rowspan="1" colspan="1">np.random.randint(0, 100)<br /> np.random.rand()</td><td rowspan="1" colspan="1">rand(1:100)<br /> rand()</td></tr><tr><td rowspan="1" colspan="1"><a name="random-seed" shape="rect" id="random-seed"></a><a href="numerical-analysis#random-seed-note" shape="rect">random seed</a><br /> <span style="color: gray"><em>set, get, and restore</em></span></td><td rowspan="1" colspan="1">rand('state', 17)<br /> sd = rand('state')<br /> rand('state', sd)</td><td rowspan="1" colspan="1">set.seed(17)<br /> sd = .Random.seed<br /> <span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">np.random.seed(17)<br /> sd = np.random.get_state()<br /> np.random.set_state(sd)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="bit-op" shape="rect" id="bit-op"></a><a href="numerical-analysis#bit-op-note" shape="rect">bit operators</a></td><td rowspan="1" colspan="1">bitshift(100, 3)<br /> bitshift(100, -3)<br /> bitand(1, 2)<br /> bitor(1, 2)<br /> bitxor(1, 2)<br /> <span style="color: gray">% MATLAB:</span><br /> bitcmp(1, 'uint16')<br /> <span style="color: gray">% Octave:</span><br /> bitcmp(1, 16)</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">100 <span style="white-space: pre-wrap;"><<</span> 3<br /> 100 <span style="white-space: pre-wrap;">>></span> 3<br /> 1 & 2<br /> 1 | 2<br /> 1 ^ 2<br /> ~1</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="binary-octal-hex-literals" shape="rect" id="binary-octal-hex-literals"></a><a href="numerical-analysis#binary-octal-hex-literals-note" shape="rect">binary, octal, and hex literals</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">0b101010<br /> 0o52<br /> 0x2a</td></tr><tr><td rowspan="1" colspan="1"><a name="radix" shape="rect" id="radix"></a><a href="numerical-analysis#radix-note" shape="rect">radix</a><br /> <span style="color: gray"><em>convert integer to and from string with radix</em></span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">base(7, 42)<br /> parse(Int, "60", 7)</td></tr><tr><th colspan="5" rowspan="1"><a name="strings" shape="rect" id="strings"></a><a href="numerical-analysis#strings-note" shape="rect">strings</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="str-literal" shape="rect" id="str-literal"></a><a href="numerical-analysis#str-literal-note" shape="rect">literal</a></td><td rowspan="1" colspan="1">'don''t say "no"'<br /> <br /> <span style="color: gray">% Octave only:</span><br /> "don't say \"no\""</td><td rowspan="1" colspan="1">"don't say \"no\""<br /> 'don\'t say "no"'</td><td rowspan="1" colspan="1">'don\'t say "no"'<br /> "don't say \"no\""<br /> r"don't " r'say "no"'</td><td rowspan="1" colspan="1">"don't say \"no\""</td></tr><tr><td rowspan="1" colspan="1"><a name="str-newline" shape="rect" id="str-newline"></a><a href="numerical-analysis#str-newline-note" shape="rect">newline in literal</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>no</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>yes</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>no</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>yes</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="str-esc" shape="rect" id="str-esc"></a><a href="numerical-analysis#str-esc-note" shape="rect">literal escapes</a></td><td rowspan="1" colspan="1"><span style="color: gray">% Octave double quote only:</span><br /> \\ \" \' \0 \a \b \f \n \r \t \v</td><td rowspan="1" colspan="1">\\ \" \' \a \b \f \n \r \t \v \<span style="color: gray"><em>ooo</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"># single and double quoted:</span><br /> \<span style="color: gray"><em>newline</em></span> \\ \' \" \a \b \f \n \r \t \v \<span style="color: gray"><em>ooo</em></span> \x<span style="color: gray"><em>hh</em></span></td><td rowspan="1" colspan="1">\\ \" \' \a \b \f \n \t \r \v<br /> \<span style="color: gray"><em>ooo</em></span> \x<span style="color: gray"><em>hh</em></span> \u<span style="color: gray"><em>hhhh</em></span> \U<span style="color: gray"><em>hhhhhhhh</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="var-interpolation" shape="rect" id="var-interpolation"></a><a href="numerical-analysis#var-interpolation-note" shape="rect">variable interpolation</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">count = 3<br /> item = "ball"<br /> println("$count $(item)s")</td></tr><tr><td rowspan="1" colspan="1"><a name="expr-interpolation" shape="rect" id="expr-interpolation"></a><a href="numerical-analysis#expr-interpolation-note" shape="rect">expression interpolation</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">"1 + 1 = $(1 + 1)"</td></tr><tr><td rowspan="1" colspan="1"><a name="str-concat" shape="rect" id="str-concat"></a><a href="numerical-analysis#str-concat-note" shape="rect">concatenate</a></td><td rowspan="1" colspan="1">strcat('one ', 'two ', 'three')</td><td rowspan="1" colspan="1">paste("one", "two", "three", sep=" ")</td><td rowspan="1" colspan="1">'one ' + 'two ' + 'three'<br /> <span style="color: gray"><em>literals, but not variables, can be concatenated with juxtaposition:</em></span><br /> 'one ' "two " 'three'</td><td rowspan="1" colspan="1">"one " * "two " * "three"<br /> <br /> string("one ", "two ", "three")</td></tr><tr><td rowspan="1" colspan="1"><a name="str-replicate" shape="rect" id="str-replicate"></a><a href="numerical-analysis#str-replicate-note" shape="rect">replicate</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">hbar = repmat('-', 1, 80)</td><td rowspan="1" colspan="1">hbar = paste(rep('-', 80), collapse='')</td><td rowspan="1" colspan="1">hbar = '-' * 80</td><td rowspan="1" colspan="1">hbar = "-" ^ 80<br /> <br /> hbar = repeat("-", 80)</td></tr><tr><td rowspan="1" colspan="1"><a name="index-substr" shape="rect" id="index-substr"></a><a href="numerical-analysis#index-substr-note" shape="rect">index of substring</a></td><td rowspan="1" colspan="1"><span style="color: gray">% returns array of one-indexed<br /> % locations</span><br /> strfind('hello', 'el')</td><td rowspan="1" colspan="1"><span style="color: gray"><em>counts from one, returns<br /> -1 if not found</em></span><br /> regexpr("el", "hello")</td><td rowspan="1" colspan="1"><span style="color: gray"># Counts from zero; raises ValueError if not found:</span><br /> 'hello'.index('el')</td><td rowspan="1" colspan="1"><span style="color: gray"># returns UnitRange:</span><br /> search("hello", "el")</td></tr><tr><td rowspan="1" colspan="1"><a name="extract-substr" shape="rect" id="extract-substr"></a><a href="numerical-analysis#extract-substr-note" shape="rect">extract substring</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">s = 'hello'<br /> <span style="color: gray">% syntax error: 'hello'(1:4)</span><br /> s(1:4)</td><td rowspan="1" colspan="1">substr("hello", 1, 4)</td><td rowspan="1" colspan="1">'hello'[0:4]</td><td rowspan="1" colspan="1">"hello"[1:4]</td></tr><tr><td rowspan="1" colspan="1"><a name="split" shape="rect" id="split"></a><a href="numerical-analysis#split-note" shape="rect">split</a></td><td rowspan="1" colspan="1"><span style="color: gray">% returns cell array:</span><br /> strsplit('foo,bar,baz', ',')</td><td rowspan="1" colspan="1">strsplit('foo,bar,baz', ',')</td><td rowspan="1" colspan="1">'foo,bar,baz'.split(',')</td><td rowspan="1" colspan="1">split("foo,bar,baz", ",")</td></tr><tr><td rowspan="1" colspan="1"><a name="join" shape="rect" id="join"></a><a href="numerical-analysis#join-note" shape="rect">join</a></td><td rowspan="1" colspan="1"><span style="color: gray">% takes cell array as arg:</span><br /> strjoin({'foo', 'bar', 'baz'}, ',')</td><td rowspan="1" colspan="1">paste("foo", "bar", "baz", sep=",")<br /> paste(c('foo', 'bar', 'baz'),<br /> <span style="white-space: pre-wrap;"> </span>collapse=',')</td><td rowspan="1" colspan="1">','.join(['foo', 'bar', 'baz'])</td><td rowspan="1" colspan="1">join(["foo", "bar", "baz"], ",")</td></tr><tr><td rowspan="1" colspan="1"><a name="trim" shape="rect" id="trim"></a><a href="numerical-analysis#trim-note" shape="rect">trim</a><br /> <span style="color: gray"><em>both sides, left, right</em></span></td><td rowspan="1" colspan="1">strtrim(' foo ')<br /> <span style="color: gray"><em>none</em></span><br /> deblank('foo ')</td><td rowspan="1" colspan="1">gsub("(^[\n\t ]+|[\n\t ]+$)",<br /> <span style="white-space: pre-wrap;"> </span>"",<br /> <span style="white-space: pre-wrap;"> </span>" foo ")<br /> sub("^[\n\t ]+", "", " foo")<br /> sub("[\n\t ]+$", "", "foo ")</td><td rowspan="1" colspan="1">' foo '.strip()<br /> ' foo'.lstrip()<br /> 'foo '.rstrip()</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="pad" shape="rect" id="pad"></a><a href="numerical-analysis#pad-note" shape="rect">pad</a><br /> <span style="color: gray"><em>on right, on left, centered</em></span></td><td rowspan="1" colspan="1">s = repmat(' ', 1, 10)<br /> s(1:5) = 'lorem'<br /> strjust(s, 'left')<br /> strjust(s, 'right')<br /> strjust(s, 'center')</td><td rowspan="1" colspan="1">sprintf("%-10s", "lorem")<br /> sprintf("%10s", "lorem")<br /> <span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">'lorem'.ljust(10)<br /> 'lorem'.rjust(10)<br /> 'lorem'.center(10)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="num-to-str" shape="rect" id="num-to-str"></a><a href="numerical-analysis#num-to-str-note" shape="rect">number to string</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">strcat('value: ', num2str(8))</td><td rowspan="1" colspan="1">paste("value: ", toString("8"))</td><td rowspan="1" colspan="1">'value: ' + str(8)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="str-to-num" shape="rect" id="str-to-num"></a><a href="numerical-analysis#str-to-num-note" shape="rect">string to number</a></td><td rowspan="1" colspan="1">7 + str2num('12')<br /> 73.9 + str2num('.037')</td><td rowspan="1" colspan="1">7 + as.integer("12")<br /> 73.9 + as.double(".037")</td><td rowspan="1" colspan="1">7 + int('12')<br /> 73.9 + float('.037')</td><td rowspan="1" colspan="1">7 + parse(Int, "12")<br /> 73.9 + parse(Float64, ".037")</td></tr><tr><td rowspan="1" colspan="1"><a name="case-manipulation" shape="rect" id="case-manipulation"></a><a href="numerical-analysis#case-manipulation-note" shape="rect">translate case</a></td><td rowspan="1" colspan="1">lower('FOO')<br /> upper('foo')</td><td rowspan="1" colspan="1">tolower("FOO")<br /> toupper("foo")</td><td rowspan="1" colspan="1">'foo'.upper()<br /> 'FOO'.lower()</td><td rowspan="1" colspan="1">uppercase("foo")<br /> lowercase("FOO")</td></tr><tr><td rowspan="1" colspan="1"><a name="sprintf" shape="rect" id="sprintf"></a><a href="numerical-analysis#sprintf-note" shape="rect">sprintf</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">sprintf('%s: %.3f %d', 'foo', 2.2, 7)</td><td rowspan="1" colspan="1">sprintf("%s: %.3f %d", "foo", 2.2, 7)</td><td rowspan="1" colspan="1">'%s: %.3f %d' % ('foo', 2.2, 7)</td><td rowspan="1" colspan="1">@sprintf("%s: %.2f %d", "foo", 2.2, 7)</td></tr><tr><td rowspan="1" colspan="1"><a name="str-len" shape="rect" id="str-len"></a><a href="numerical-analysis#str-len-note" shape="rect">length</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">length('hello')</td><td rowspan="1" colspan="1">nchar("hello")</td><td rowspan="1" colspan="1">len('hello')</td><td rowspan="1" colspan="1">length("hello")<br /> <br /> <span style="color: gray"># index of first byte of last char:</span><br /> endof("hello")</td></tr><tr><td rowspan="1" colspan="1"><a name="char-access" shape="rect" id="char-access"></a><a href="numerical-analysis#char-access-note" shape="rect">character access</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">s = 'hello'<br /> <span style="color: gray">% syntax error: 'hello'(1)</span><br /> s(1)</td><td rowspan="1" colspan="1">substr("hello", 1, 1)</td><td rowspan="1" colspan="1">'hello'[0]</td><td rowspan="1" colspan="1">"hello"[1]<br /> <br /> <span style="color: gray"># index must be byte-index of the first byte of a<br /> # character. Raises BoundsErrror if no such byte,<br /> # and UnicodeError if byte not first in char.</span></td></tr><tr><td rowspan="1" colspan="1"><a name="chr-ord" shape="rect" id="chr-ord"></a><a href="numerical-analysis#chr-ord-note" shape="rect">chr and ord</a></td><td rowspan="1" colspan="1">char(65)<br /> double('A')</td><td rowspan="1" colspan="1">intToUtf8(65)<br /> utf8ToInt("A")</td><td rowspan="1" colspan="1">chr(65)<br /> ord('A')</td><td rowspan="1" colspan="1">Char(65)<br /> Int('A')</td></tr><tr><th colspan="5" rowspan="1"><a name="regexes" shape="rect" id="regexes"></a><a href="numerical-analysis#regexes-note" shape="rect">regular expressions</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="char-class-abbrevs" shape="rect" id="char-class-abbrevs"></a><a href="numerical-analysis#char-class-abbrevs-note" shape="rect">character class abbreviations</a></td><td rowspan="1" colspan="1">. \d \D \s \S \w \W<br /> <br /> <span style="color: gray">% also C-string style backslash escapes:</span><br /> \a \b \f \n \r \t \v</td><td rowspan="1" colspan="1"><span style="color: gray"># escape backslash in strings by doubling:</span><br /> . \d \D \s \S \w \W</td><td rowspan="1" colspan="1">. \d \D \s \S \w \W</td><td rowspan="1" colspan="1">. \d \D \h \H \s \S \v \V \w \W</td></tr><tr><td rowspan="1" colspan="1"><a name="regex-anchors" shape="rect" id="regex-anchors"></a><a href="numerical-analysis#regex-anchors-note" shape="rect">anchors</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">^ $ \< \></td><td rowspan="1" colspan="1"><span style="color: gray"># escape backslash in strings by doubling:</span><br /> ^ $ \< \> \b \B</td><td rowspan="1" colspan="1">^ $ \A \b \B \Z</td><td rowspan="1" colspan="1">^ $ \A \b \B \z \Z</td></tr><tr><td rowspan="1" colspan="1"><a name="regex-match" shape="rect" id="regex-match"></a><a href="numerical-analysis#regex-match-note" shape="rect">match test</a></td><td rowspan="1" colspan="1">regexp('hello', '^[a-z]+$')<br /> regexp('hello', '^\S+$')</td><td rowspan="1" colspan="1">regexpr("^[a-z]+$", "hello") > 0<br /> regexpr('^\\S+$', "hello") > 0</td><td rowspan="1" colspan="1">re.search(r'^[a-z]+$', 'hello')<br /> re.search(r'^\S+$', 'hello')</td><td rowspan="1" colspan="1">ismatch(r"^[a-z]+$", "hello")</td></tr><tr><td rowspan="1" colspan="1"><a name="case-insensitive-regex-match" shape="rect" id="case-insensitive-regex-match"></a><a href="numerical-analysis#case-insensitive-regex-match-note" shape="rect">case insensitive match test</a></td><td rowspan="1" colspan="1">regexpi('Lorem Ipsum', 'lorem')</td><td rowspan="1" colspan="1">regexpr('(?i)lorem', "Lorem Ipsum") > 0</td><td rowspan="1" colspan="1">re.search(r'lorem', 'Lorem Ipsum', re.I)</td><td rowspan="1" colspan="1">ismatch(r"lorem"i, "Lorem Ipsum")</td></tr><tr><td rowspan="1" colspan="1"><a name="regex-modifiers" shape="rect" id="regex-modifiers"></a><a href="numerical-analysis#regex-modifiers-note" shape="rect">modifiers</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">(?i) (?m) (?s) (?x)</td><td rowspan="1" colspan="1">re.I re.M re.S re.X</td><td rowspan="1" colspan="1">i m s x</td></tr><tr><td rowspan="1" colspan="1"><a name="regex-subst" shape="rect" id="regex-subst"></a><a href="numerical-analysis#regex-subst-note" shape="rect">substitution</a><br /> <span style="color: gray"><em>first match, all matches</em></span></td><td rowspan="1" colspan="1">s = 'do re mi mi mi'<br /> regexprep(s, 'ma', 'once')<br /> regexprep(s, 'mi', 'ma')</td><td rowspan="1" colspan="1">sub('mi', 'ma', 'do re mi mi mi')<br /> gsub('mi', 'ma', 'do re mi mi mi')</td><td rowspan="1" colspan="1">rx = re.compile(r'mi')<br /> s = rx.sub('ma', 'do re mi mi mi', 1)<br /> s2 = rx.sub('ma', 'do re mi mi mi')</td><td rowspan="1" colspan="1">replace("do re mi mi mi", r"mi", s"ma", 1)<br /> replace("do re mi mi mi", r"mi", s"ma")</td></tr><tr><td rowspan="1" colspan="1"><a name="regex-backreference" shape="rect" id="regex-backreference"></a><a href="numerical-analysis#regex-backreference-note" shape="rect">backreference in match and substitution</a></td><td rowspan="1" colspan="1">regexp('do do', '(\w+) \1')<br /> regexprep('do re', '(\w+) (\w+)', '$2 $1')</td><td rowspan="1" colspan="1">regexpr('(\\w+) \\1', 'do do') > 0<br /> sub('(\\w+) (\\w+)', '\\2 \\1', 'do re')</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span><br /> <br /> rx = re.compile(r'(\w+) (\w+)')<br /> rx.sub(r'\2 \1', 'do re')</td><td rowspan="1" colspan="1">ismatch(r"(\w+) \1", "do do")</td></tr><tr><td rowspan="1" colspan="1"><a name="group-capture" shape="rect" id="group-capture"></a><a href="numerical-analysis#group-capture-note" shape="rect">group capture</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">rx = '(\d{4})-(\d{2})-(\d{2})'<br /> m = re.search(rx, '2010-06-03')<br /> yr, mo, dy = m.groups()</td><td rowspan="1" colspan="1">rx = r"(\d{4})-(\d{2})-(\d{2})"<br /> m = match(rx, "2010-06-03")<br /> yr, mn, dy = m.captures</td></tr><tr><th colspan="5" rowspan="1"><a name="dates-time" shape="rect" id="dates-time"></a><a href="numerical-analysis#dates-time-note" shape="rect">dates and time</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="current-date-time" shape="rect" id="current-date-time"></a><a href="numerical-analysis#current-date-time-note" shape="rect">current date/time</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">t = now</td><td rowspan="1" colspan="1">t = as.POSIXlt(Sys.time())</td><td rowspan="1" colspan="1">import datetime<br /> <br /> t = datetime.datetime.now()</td><td rowspan="1" colspan="1">t = now()</td></tr><tr><td rowspan="1" colspan="1"><a name="date-time-type" shape="rect" id="date-time-type"></a><a href="numerical-analysis#date-time-type-note" shape="rect">date/time type</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>floating point number representing days since year 0 in the Gregorian calendar</em></span></td><td rowspan="1" colspan="1">POSIXlt</td><td rowspan="1" colspan="1">datettime</td><td rowspan="1" colspan="1">DateTime</td></tr><tr><td rowspan="1" colspan="1"><a name="date-time-diff-type" shape="rect" id="date-time-diff-type"></a><a href="numerical-analysis#date-time-diff-type-note" shape="rect">date/time difference type</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>floating point number representing days</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>a</em> difftime <em>object which behaves like a floating point number representing seconds</em></span></td><td rowspan="1" colspan="1"><span style="color: gray">timedelta, <em>which can be converted to float value in seconds via</em> total_seconds() <em>method</em></span></td><td rowspan="1" colspan="1">Base.Dates.Millisecond</td></tr><tr><td rowspan="1" colspan="1"><a name="date-parts" shape="rect" id="date-parts"></a><a href="numerical-analysis#date-parts-note" shape="rect">get date parts</a></td><td rowspan="1" colspan="1">dv = datevec(t)<br /> dv(1)<br /> dv(2)<br /> dv(3)<br /> <span style="color: gray">% syntax error: datevec(t)(1)</span></td><td rowspan="1" colspan="1">t$year + 1900<br /> t$mon + 1<br /> t$mday</td><td rowspan="1" colspan="1">t.year<br /> t.month<br /> t.day</td><td rowspan="1" colspan="1">Dates.year(t)<br /> Dates.month(t)<br /> Dates.day(t)</td></tr><tr><td rowspan="1" colspan="1"><a name="time-parts" shape="rect" id="time-parts"></a><a href="numerical-analysis#time-parts-note" shape="rect">get time parts</a></td><td rowspan="1" colspan="1">dv = datevec(t)<br /> dv(4)<br /> dv(5)<br /> dv(6)</td><td rowspan="1" colspan="1">t$hour<br /> t$min<br /> t$sec</td><td rowspan="1" colspan="1">t.hour<br /> t.minute<br /> t.second</td><td rowspan="1" colspan="1">Dates.hour(t)<br /> Dates.minute(t)<br /> Dates.second(t)</td></tr><tr><td rowspan="1" colspan="1"><a name="build-date-time" shape="rect" id="build-date-time"></a><a href="numerical-analysis#build-date-time-note" shape="rect">build date/time from parts</a></td><td rowspan="1" colspan="1">t = datenum([2011 9 20 23 1 2])</td><td rowspan="1" colspan="1">t = as.POSIXlt(Sys.time())<br /> t$year = 2011 - 1900<br /> t$mon = 9 - 1<br /> t$mday = 20<br /> t$hour = 23<br /> t$min = 1<br /> t$sec = 2</td><td rowspan="1" colspan="1">import datetime<br /> <br /> t = datetime.datetime(2011, 9, 20, 23, 1, 2)</td><td rowspan="1" colspan="1">t = DateTime(2011, 9, 20, 23, 1, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="date-to-str" shape="rect" id="date-to-str"></a><a href="numerical-analysis#date-to-str-note" shape="rect">convert to string</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">datestr(t)</td><td rowspan="1" colspan="1">print(t)</td><td rowspan="1" colspan="1">str(t)</td><td rowspan="1" colspan="1">"$t"</td></tr><tr><td rowspan="1" colspan="1"><a name="strptime" shape="rect" id="strptime"></a><a href="numerical-analysis#strptime-note" shape="rect">parse datetime</a></td><td rowspan="1" colspan="1">s = '2011-09-20 23:01:02'<br /> fmt = 'yyyy-mm-dd HH:MM:SS'<br /> t = datenum(s, fmt)</td><td rowspan="1" colspan="1">t = strptime('2011-09-20 23:01:02',<br /> <span style="white-space: pre-wrap;"> </span>'%Y-%m-%d %H:%M:%S')</td><td rowspan="1" colspan="1">import datetime<br /> <br /> s = '2011-05-03 10:00:00'<br /> fmt = '%Y-%m-%d %H:%M:%S'<br /> t = datetime.datetime.strptime(s, fmt)</td><td rowspan="1" colspan="1">fmt = "yyyy-mm-dd HH:MM:SS"<br /> t = DateTime("2011-05-03 10:00:00", fmt)<br /> <br /> <span style="color: gray"># fmt string can be compiled:</span><br /> df = Dates.DateFormat(fmt)<br /> t2 = DateTime("2011-05-03 10:00:00", df)</td></tr><tr><td rowspan="1" colspan="1"><a name="strftime" shape="rect" id="strftime"></a><a href="numerical-analysis#strftime-note" shape="rect">format datetime</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">datestr(t, 'yyyy-mm-dd HH:MM:SS')</td><td rowspan="1" colspan="1">format(t, format='%Y-%m-%d %H:%M:%S')</td><td rowspan="1" colspan="1">t.strftime('%Y-%m-%d %H:%M:%S')</td><td rowspan="1" colspan="1">Dates.format(t, "yyyy-mm-dd HH:MM:SS")</td></tr><tr><td rowspan="1" colspan="1"><a name="sleep" shape="rect" id="sleep"></a><a href="numerical-analysis#sleep-note" shape="rect">sleep</a></td><td rowspan="1" colspan="1">pause(0.5)</td><td rowspan="1" colspan="1">Sys.sleep(0.5)</td><td rowspan="1" colspan="1">import time<br /> <br /> time.sleep(0.5)</td><td rowspan="1" colspan="1">sleep(0.5)</td></tr><tr><th colspan="5" rowspan="1"><a name="tuples" shape="rect" id="tuples"></a><a href="numerical-analysis#tuples-note" shape="rect">tuples</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="tuple-type" shape="rect" id="tuple-type"></a><a href="numerical-analysis#tuple-type-note" shape="rect">type</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">cell</td><td rowspan="1" colspan="1">list</td><td rowspan="1" colspan="1">tuple</td><td rowspan="1" colspan="1">Tuple{<span style="color: gray">T[, <span style="white-space: pre-wrap;">...</span>]</span>}</td></tr><tr><td rowspan="1" colspan="1"><a name="tuple-literal" shape="rect" id="tuple-literal"></a><a href="numerical-analysis#tuple-literal-note" shape="rect">literal</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">tup = {1.7, 'hello', [1 2 3]}</td><td rowspan="1" colspan="1">tup = list(1.7, "hello", c(1, 2, 3))</td><td rowspan="1" colspan="1">tup = (1.7, "hello", [1,2,3])</td><td rowspan="1" colspan="1">tup = (1.7, "foo", [1, 2, 3])</td></tr><tr><td rowspan="1" colspan="1"><a name="tuple-access" shape="rect" id="tuple-access"></a><a href="numerical-analysis#tuple-access-note" shape="rect">lookup element</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray">% indices start at one:</span><br /> tup{1}</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at one:</span><br /> tup[[1]]</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at zero:</span><br /> tup[0]</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at one:</span><br /> tup[1]</td></tr><tr><td rowspan="1" colspan="1"><a name="tuple-update" shape="rect" id="tuple-update"></a><a href="numerical-analysis#tuple-update-note" shape="rect">update element</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">tup{1} = 2.7</td><td rowspan="1" colspan="1">tup[[1]] = 2.7</td><td rowspan="1" colspan="1"><span style="color: gray"><em>tuples are immutable</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>tuples are immutable</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="tuple-len" shape="rect" id="tuple-len"></a><a href="numerical-analysis#tuple-len-note" shape="rect">length</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">length(tup)</td><td rowspan="1" colspan="1">length(tup)</td><td rowspan="1" colspan="1">len(tup)</td><td rowspan="1" colspan="1">length(tup)</td></tr><tr><th colspan="5" rowspan="1"><a name="arrays" shape="rect" id="arrays"></a><a href="numerical-analysis#arrays-note" shape="rect">arrays</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="array-elem-type" shape="rect" id="array-elem-type"></a><a href="numerical-analysis#array-elem-type-note" shape="rect">element type</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>always numeric</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"># "numeric":</span><br /> class(c(1, 2, 3))<br /> <br /> <span style="color: gray"># arrays can also have "boolean" or "string" elements</span></td><td rowspan="1" colspan="1"><span style="color: gray"># values can have different types:</span><br /> [type(x) for x in a]</td><td rowspan="1" colspan="1">a = [1, 2, 3]<br /> <br /> <span style="color: gray"># Array{Int64, 2}:</span><br /> typeof(a)<br /> <span style="color: gray"># Int64:</span><br /> typeof(a[1])</td></tr><tr><td rowspan="1" colspan="1"><a name="array-literal" shape="rect" id="array-literal"></a><a href="numerical-analysis#array-literal-note" shape="rect">literal</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">a = [1, 2, 3, 4]<br /> <br /> <span style="color: gray">% commas are optional:</span><br /> a = [1 2 3 4]</td><td rowspan="1" colspan="1"><span style="color: gray"># use c() constructor:</span><br /> a = c(1, 2, 3, 4)</td><td rowspan="1" colspan="1">a = [1, 2, 3, 4]</td><td rowspan="1" colspan="1">a = [1, 2, 3, 4]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-size" shape="rect" id="array-size"></a><a href="numerical-analysis#array-size-note" shape="rect">size</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">length(a)</td><td rowspan="1" colspan="1">length(a)</td><td rowspan="1" colspan="1">len(a)</td><td rowspan="1" colspan="1">length(a)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-empty-test" shape="rect" id="array-empty-test"></a><a href="numerical-analysis#array-empty-test-note" shape="rect">empty test</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">length(a) == 0<br /> <br /> <span style="color: gray">% An array used in a conditional test is<br /> % false unless nonempty and all entries evaluate<br /> % as true.</span></td><td rowspan="1" colspan="1">length(a) == 0</td><td rowspan="1" colspan="1">not a</td><td rowspan="1" colspan="1">isempty(a)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-lookup" shape="rect" id="array-lookup"></a><a href="numerical-analysis#array-lookup-note" shape="rect">lookup</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray">% Indices start at one:</span><br /> a(1)</td><td rowspan="1" colspan="1"><span style="color: gray"># Indices start at one:</span><br /> a[1]</td><td rowspan="1" colspan="1"><span style="color: gray"># Indices start at zero:</span><br /> a[0]</td><td rowspan="1" colspan="1"><span style="color: gray"># Indices start at one:</span><br /> a[1]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-update" shape="rect" id="array-update"></a><a href="numerical-analysis#array-update-note" shape="rect">update</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">a(1) = -1</td><td rowspan="1" colspan="1">a[1] = -1</td><td rowspan="1" colspan="1">a[0] = -1</td><td rowspan="1" colspan="1">a[1] = -1</td></tr><tr><td rowspan="1" colspan="1"><a name="array-out-of-bounds" shape="rect" id="array-out-of-bounds"></a><a href="numerical-analysis#array-out-of-bounds-note" shape="rect">out-of-bounds behavior</a></td><td rowspan="1" colspan="1">a = []<br /> <br /> <span style="color: gray">% error:</span><br /> a(1)<br /> <br /> <span style="color: gray">% increases array size to 10;<br /> % zero-fills slots 1 through 9:</span><br /> a(10) = 10</td><td rowspan="1" colspan="1">a = c()<br /> <span style="color: gray"># evaluates as NA:</span><br /> a[10]<br /> <span style="color: gray"># increases array size to 10:</span><br /> a[10] = "lorem"</td><td rowspan="1" colspan="1">a = []<br /> <span style="color: gray"># raises IndexError:</span><br /> a[10]<br /> <span style="color: gray"># raises IndexError:</span><br /> a[10] = 'lorem'</td><td rowspan="1" colspan="1">a = []<br /> <br /> <span style="color: gray"># raises BoundsError:</span><br /> a[10]<br /> <span style="color: gray"># raises BoundsError:</span><br /> a[10] = "lorem"</td></tr><tr><td rowspan="1" colspan="1"><a name="array-element-index" shape="rect" id="array-element-index"></a><a href="numerical-analysis#array-element-index-note" shape="rect">index of element</a></td><td rowspan="1" colspan="1">a = [7 8 9 10 8]<br /> <br /> <span style="color: gray">% returns [2 5]:</span><br /> find(a == 8)<br /> <br /> <span style="color: gray">% returns 2:</span><br /> find(a == 8, 1, 'first')</td><td rowspan="1" colspan="1">a = c('x', 'y', 'z', 'w', 'y')<br /> <br /> <span style="color: gray"># c(2, 5):</span><br /> which(a == 'y')</td><td rowspan="1" colspan="1">a = ['x', 'y', 'z', 'w', 'y']<br /> <br /> a.index('y')<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 1</span><br /> a.rindex('y')<span style="white-space: pre-wrap;"> </span><span style="color: gray"># 4</span></td><td rowspan="1" colspan="1">a = ["x", "y", "z", "w", "y"]<br /> <br /> <span style="color: gray"># 2:</span><br /> findfirst(a, "y")<br /> <span style="color: gray"># 5:</span><br /> julia> findlast(a, "y")</td></tr><tr><td rowspan="1" colspan="1"><a name="array-slice" shape="rect" id="array-slice"></a><a href="numerical-analysis#array-slice-note" shape="rect">slice</a><br /> <span style="color: gray"><em>by endpoints</em></span></td><td rowspan="1" colspan="1">a = ['a' 'b' 'c' 'd' 'e']<br /> <br /> <span style="color: gray">% ['c' 'd']:</span><br /> a(3:4)</td><td rowspan="1" colspan="1">a = c("a", "b", "c", "d", "e")<br /> <br /> <span style="color: gray"># c("c", "d"):</span><br /> a[seq(3, 4)]</td><td rowspan="1" colspan="1">a = ['a', 'b', 'c', 'd', 'e']<br /> <br /> <span style="color: gray"># ['c', 'd']:</span><br /> a[2:4]</td><td rowspan="1" colspan="1">a = ["a", "b", "c", "d", "e"]<br /> <br /> <span style="color: gray"># ["c", "d"]:</span><br /> a[3:4]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-slice-to-end" shape="rect" id="array-slice-to-end"></a><a href="numerical-analysis#array-slice-to-end-note" shape="rect">slice to end</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">a = ['a' 'b' 'c' 'd' 'e']<br /> <br /> <span style="color: gray">% ['c' 'd' 'e']:</span><br /> a(3:end)</td><td rowspan="1" colspan="1">a = c("a", "b", "c", "d", "e")<br /> <br /> <span style="color: gray"># both return c("c", "d", "e"):</span><br /> tail(a, n=length(a) - 2)<br /> a[-1:-2]</td><td rowspan="1" colspan="1">a = ['a', 'b', 'c', 'd', 'e']<br /> <br /> <span style="color: gray"># ['c', 'd', 'e']:</span><br /> a[2:]</td><td rowspan="1" colspan="1">a = ["a", "b", "c", "d", "e"]<br /> <br /> <span style="color: gray"># ["c", "d", "e"]:</span><br /> a[3:end]</td></tr><tr><td rowspan="1" colspan="1"><a name="int-array-as-index" shape="rect" id="int-array-as-index"></a><a href="numerical-analysis#int-array-as-index" shape="rect">integer array as index</a></td><td rowspan="1" colspan="1">[7 8 9]([1 3 3])</td><td rowspan="1" colspan="1">c(7, 8, 9)[c(1, 3, 3)]</td><td rowspan="1" colspan="1">np.array([7, 8, 9])[[0, 2, 2]]</td><td rowspan="1" colspan="1"><span style="color: gray"># [7, 9, 9]:</span><br /> [7, 8, 9][[1, 3, 3]]</td></tr><tr><td rowspan="1" colspan="1"><a name="logical-array-as-index" shape="rect" id="logical-array-as-index"></a><a href="numerical-analysis#logical-array-as-index" shape="rect">logical array as index</a></td><td rowspan="1" colspan="1">[7 8 9]([true false true])</td><td rowspan="1" colspan="1">c(7, 8, 9)[c(T, F, T)]</td><td rowspan="1" colspan="1">np.array([7, 8, 9])[[True, False, True]]</td><td rowspan="1" colspan="1"><span style="color: gray"># [7, 9]:</span><br /> [7, 8, 9][[true, false, true]]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-concatenation" shape="rect" id="array-concatenation"></a><a href="numerical-analysis#array-concatenation-note" shape="rect">concatenate</a></td><td rowspan="1" colspan="1">a = [1 2 3]<br /> a2 = [a [4 5 6]]<br /> a = [a [4 5 6]]<br /> <span style="color: gray">% or:</span><br /> a = horzcat(a, a2)</td><td rowspan="1" colspan="1">a = c(1, 2, 3)<br /> a2 = append(a, c(4, 5, 6))<br /> a = append(a, c(4, 5, 6))</td><td rowspan="1" colspan="1">a = [1, 2, 3]<br /> a2 = a + [4, 5, 6]<br /> a.extend([4, 5, 6])</td><td rowspan="1" colspan="1">a = [1, 2, 3]<br /> a2 = vcat(a, [4, 5, 6])<br /> a = vcat(a, [4, 5, 6])</td></tr><tr><td rowspan="1" colspan="1"><a name="array-replication" shape="rect" id="array-replication"></a><a href="numerical-analysis#array-replication-note" shape="rect">replicate</a></td><td rowspan="1" colspan="1">a = repmat(NA, 1, 10)</td><td rowspan="1" colspan="1">a = rep(NA, 10)<br /> <br /> <span style="color: gray"># 30 a's, 50 b's, and 90 c's:</span><br /> rep(c("a", "b", "c"), c(30, 50, 90))</td><td rowspan="1" colspan="1">a = [None] * 10<br /> a = [None for i in range(0, 10)]</td><td rowspan="1" colspan="1">fill(NaN, 10)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-copy" shape="rect" id="array-copy"></a><a href="numerical-analysis#array-copy-note" shape="rect">copy</a><br /> <span style="color: gray"><em>address copy, shallow copy, deep copy</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>There is no address copy. Because arrays cannot be nested, there is no distinction between shallow copy and deep copy. Assignment and passing an array to a function can be regarded as performing a shallow or deep copy, though MATLAB does not allocate memory for a 2nd array until one of the arrays is modified.</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>Arrays in R behave like arrays in MATLAB.</em></span></td><td rowspan="1" colspan="1">import copy<br /> <br /> a = [1, 2, [3, 4]]<br /> <br /> a2 = a<br /> a3 = list(a)<br /> a4 = copy.deepcopy(a)</td><td rowspan="1" colspan="1">a = Any[1, 2, [3, 4]]<br /> <br /> a2 = a<br /> a3 = copy(a)<br /> a4 = deepcopy(a)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-iteration" shape="rect" id="array-iteration"></a><a href="numerical-analysis#array-iteration-note" shape="rect">iteration</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">a = [9 7 3]<br /> for i = 1:numel(a)<br /> <span style="white-space: pre-wrap;"> </span>x = a(i)<br /> <span style="white-space: pre-wrap;"> </span>disp(x)<br /> end</td><td rowspan="1" colspan="1">for (x in c(9, 7, 3)) {<br /> <span style="white-space: pre-wrap;"> </span>print(x)<br /> }</td><td rowspan="1" colspan="1">for i in [9, 7, 3]:<br /> <span style="white-space: pre-wrap;"> </span>print(i)</td><td rowspan="1" colspan="1">for i = [9, 7, 3]<br /> <span style="white-space: pre-wrap;"> </span>println(i)<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="indexed-array-iteration" shape="rect" id="indexed-array-iteration"></a><a href="numerical-analysis#indexed-array-iteration-note" shape="rect">indexed iteration</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">for (i in seq_along(a)) {<br /> <span style="white-space: pre-wrap;"> </span>cat(sprintf("%s at index %d\n", i, a[i]))<br /> }</td><td rowspan="1" colspan="1">a = ['do', 're', 'mi', 'fa']<br /> for i, s in enumerate(a):<br /> <span style="white-space: pre-wrap;"> </span>print('%s at index %d' % (s, i))</td><td rowspan="1" colspan="1">a = ["do", "re", "mi", "fa"]<br /> for (i, s) in enumerate(a)<br /> <span style="white-space: pre-wrap;"> </span>println(i, " ", s)<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="array-reverse" shape="rect" id="array-reverse"></a><a href="numerical-analysis#array-reverse-note" shape="rect">reverse</a></td><td rowspan="1" colspan="1">a = [1 2 3]<br /> a2 = fliplr(a)<br /> a = fliplr(a)</td><td rowspan="1" colspan="1">a = c(1, 2, 3)<br /> a2 = rev(a)<br /> a = rev(a)</td><td rowspan="1" colspan="1">a = [1, 2, 3]<br /> a2 = a[::-1]<br /> a.reverse()</td><td rowspan="1" colspan="1">a = [1, 2, 3]<br /> a2 = reverse(a)<br /> reverse!(a)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-sort" shape="rect" id="array-sort"></a><a href="numerical-analysis#array-sort-note" shape="rect">sort</a></td><td rowspan="1" colspan="1">a = [3 1 4 2]<br /> a = sort(a)</td><td rowspan="1" colspan="1">a = c('b', 'A', 'a', 'B')<br /> a2 = sort(a)<br /> a = sort(a)</td><td rowspan="1" colspan="1">a = ['b', 'A', 'a', 'B']<br /> sorted(a)<br /> a.sort()<br /> a.sort(key=str.lower)</td><td rowspan="1" colspan="1">a = [3, 1, 4, 2]<br /> a2 = sort(a)<br /> sort!(a)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-dedupe" shape="rect" id="array-dedupe"></a><a href="numerical-analysis#array-dedupe-note" shape="rect">dedupe</a></td><td rowspan="1" colspan="1">a = [1 2 2 3]<br /> a2 = unique(a)</td><td rowspan="1" colspan="1">a = c(1, 2, 2, 3)<br /> a2 = unique(a)</td><td rowspan="1" colspan="1">a = [1, 2, 2, 3]<br /> a2 = list(set(a)))</td><td rowspan="1" colspan="1">a = unique([1, 2, 2, 3])</td></tr><tr><td rowspan="1" colspan="1"><a name="membership" shape="rect" id="membership"></a><a href="numerical-analysis#membership-note" shape="rect">membership</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">ismember(7, a)</td><td rowspan="1" colspan="1">7 %in% a<br /> is.element(7, a)</td><td rowspan="1" colspan="1">7 in a</td><td rowspan="1" colspan="1">7 in a<br /> 7 ∈ a<br /> a ∋ 7</td></tr><tr><td rowspan="1" colspan="1"><a name="intersection" shape="rect" id="intersection"></a><a href="numerical-analysis#intersection-note" shape="rect">intersection</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">intersect([1 2], [2 3 4])</td><td rowspan="1" colspan="1">intersect(c(1, 2), c(2, 3, 4))</td><td rowspan="1" colspan="1">{1, 2} & {2, 3, 4}</td><td rowspan="1" colspan="1">intersection([1, 2], [2, 3, 4])<br /> ∩([1, 2], [2, 3, 4])</td></tr><tr><td rowspan="1" colspan="1"><a name="union" shape="rect" id="union"></a><a href="numerical-analysis#union-note" shape="rect">union</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">union([1 2], [2 3 4])</td><td rowspan="1" colspan="1">union(c(1, 2), c(2, 3, 4))</td><td rowspan="1" colspan="1">{1, 2} | {2, 3, 4}</td><td rowspan="1" colspan="1">union([1, 2], [2, 3, 4])<br /> ∪([1, 2], [2, 3, 4])</td></tr><tr><td rowspan="1" colspan="1"><a name="set-diff" shape="rect" id="set-diff"></a><a href="numerical-analysis#set-diff-note" shape="rect">relative complement, symmetric difference</a></td><td rowspan="1" colspan="1">setdiff([1 2 3], [2])<br /> <br /> a1 = [1 2]<br /> a2 = [2 3 4]<br /> union(setdiff(a1, a2), setdiff(a2, a1))</td><td rowspan="1" colspan="1">setdiff(c(1, 2, 3), c(2))<br /> <br /> union(setdiff(c(1, 2), c(2, 3, 4)),<br /> <span style="white-space: pre-wrap;"> </span>setdiff(c(2, 3, 4), c(1, 2)))</td><td rowspan="1" colspan="1">{1, 2, 3} - {2}<br /> <br /> {1, 2} ^ {2, 3, 4}</td><td rowspan="1" colspan="1">setdiff([1, 2, 3], [2])<br /> symdiff([1, 2], [2, 3, 4])</td></tr><tr><td rowspan="1" colspan="1"><a name="map" shape="rect" id="map"></a><a href="numerical-analysis#map-note" shape="rect">map</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">arrayfun( @(x) x*x, [1 2 3])</td><td rowspan="1" colspan="1">sapply(c(1,2,3), function (x) { x * x})</td><td rowspan="1" colspan="1">map(lambda x: x * x, [1, 2, 3])<br /> <span style="color: gray"># or use list comprehension:</span><br /> [x * x for x in [1, 2, 3]]</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="filter" shape="rect" id="filter"></a><a href="numerical-analysis#filter-note" shape="rect">filter</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">a = [1 2 3]<br /> a(a > 2)</td><td rowspan="1" colspan="1">a = c(1, 2, 3)<br /> a[a > 2]<br /> <br /> Filter(function(x) { x > 2}, a)</td><td rowspan="1" colspan="1">filter(lambda x: x > 1, [1, 2, 3])<br /> <span style="color: gray"># or use list comprehension:</span><br /> [x for x in [1, 2, 3] if x > 1]</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="reduce" shape="rect" id="reduce"></a><a href="numerical-analysis#reduce-note" shape="rect">reduce</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">Reduce(function(x, y) { x + y }, c(1, 2, 3), 0)</td><td rowspan="1" colspan="1">reduce(lambda x, y: x + y, [1 ,2, 3], 0)</td><td rowspan="1" colspan="1">reduce(+, [1, 2, 3])<br /> foldl(-, 0, [1, 2, 3])<br /> foldr(-, 0, [1, 2, 3])</td></tr><tr><td rowspan="1" colspan="1"><a name="universal-existential-test" shape="rect" id="universal-existential-test"></a><a href="numerical-analysis#universal-existential-test-note" shape="rect">universal and existential tests</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">all(mod([1 2 3 4], 2) == 0)<br /> any(mod([1 2 3 4]) == 0)</td><td rowspan="1" colspan="1">all(c(1, 2, 3, 4) %% 2 == 0)<br /> any(c(1, 2, 3, 4) %% 2 == 0)</td><td rowspan="1" colspan="1">all(i % 2 == 0 for i in [1, 2, 3, 4])<br /> any(i % 2 == 0 for i in [1, 2, 3, 4])</td><td rowspan="1" colspan="1">all([x % 2 == 0 for x in [1, 2, 3, 4]])<br /> any([x % 2 == 0 for x in [1, 2, 3, 4]])</td></tr><tr><td rowspan="1" colspan="1"><a name="shuffle-sample" shape="rect" id="shuffle-sample"></a><a href="numerical-analysis#shuffle-sample-note" shape="rect">shuffle and sample</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">a = c(1, 1, 2, 3, 9, 28)<br /> sample(a, 3)<br /> a[sample.int(length(a))]</td><td rowspan="1" colspan="1">from random import shuffle, sample<br /> <br /> a = [1, 2, 3, 4]<br /> shuffle(a)<br /> sample(a, 2)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="zip" shape="rect" id="zip"></a><a href="numerical-analysis#zip-note" shape="rect">zip</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none; MATLAB arrays can't be nested</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"># R arrays can't be nested.<br /> # One approximation of zip is a 2d array:</span><br /> a = rbind(c(1, 2, 3), c('a', 'b', 'c'))<br /> <br /> <span style="color: gray"># To prevent data type coercion, use a data frame:</span><br /> df = data.frame(numbers=c(1, 2, 3),<br /> <span style="white-space: pre-wrap;"> </span>letters=c('a', 'b', 'c'))</td><td rowspan="1" colspan="1"><span style="color: gray"># array of 3 pairs:</span><br /> a = zip([1, 2, 3], ['a', 'b', 'c'])</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="arithmetic-sequences" shape="rect" id="arithmetic-sequences"></a><a href="numerical-analysis#arithmetic-sequences-note" shape="rect">arithmetic sequences</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="arith-seq-diff-one" shape="rect" id="arith-seq-diff-one"></a><a href="numerical-analysis#arith-seq-diff-one-note" shape="rect">unit difference</a></td><td rowspan="1" colspan="1">1:100</td><td rowspan="1" colspan="1"><span style="color: gray"># type integer:</span><br /> 1:100<br /> seq(1, 100)<br /> <br /> <span style="color: gray"># type double:</span><br /> seq(1, 100, 1)</td><td rowspan="1" colspan="1">range(1, 101)</td><td rowspan="1" colspan="1">1:100</td></tr><tr><td rowspan="1" colspan="1"><a name="arith-seq-diff-ten" shape="rect" id="arith-seq-diff-ten"></a><a href="numerical-analysis#arith-seq-diff-ten-note" shape="rect">difference of 10</a></td><td rowspan="1" colspan="1">0:10:100</td><td rowspan="1" colspan="1"><span style="color: gray"># type double:</span><br /> seq(0, 100, 10)</td><td rowspan="1" colspan="1">range(0, 101, 10)</td><td rowspan="1" colspan="1">0:10:100</td></tr><tr><td rowspan="1" colspan="1"><a name="arith-seq-diff-tenth" shape="rect" id="arith-seq-diff-tenth"></a><a href="numerical-analysis#arith-seq-diff-tenth-note" shape="rect">difference of 0.1</a></td><td rowspan="1" colspan="1">0:0.1:10</td><td rowspan="1" colspan="1">seq(0, 10, 0.1)</td><td rowspan="1" colspan="1">[0.1 * x for x in range(0, 101)]<br /> <br /> <span style="color: gray"># 3rd arg is length of sequence, not step size:</span><br /> sp.linspace(0, 10, 100)</td><td rowspan="1" colspan="1">0:0.1:10</td></tr><tr><td rowspan="1" colspan="1"><a name="arith-seq-computed-diff" shape="rect" id="arith-seq-computed-diff"></a><a href="numerical-analysis#arith-seq-computed-diff-note" shape="rect">computed difference</a></td><td rowspan="1" colspan="1"><span style="color: gray">% 100 evenly spaced values:</span><br /> linspace(3.7, 19.4, 100)<br /> <br /> <span style="color: gray">% 100 is default num. of elements:</span><br /> linspace(3.7, 19.4)</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">numpy.linspace(3.7, 19.4, 100)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="iter-over-arith-seq" shape="rect" id="iter-over-arith-seq"></a><a href="numerical-analysis#iter-over-arith-seq-note" shape="rect">iterate</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"># range replaces xrange in Python 3:</span><br /> n = 0;<br /> for i in xrange(1, 1000001):<br /> <span style="white-space: pre-wrap;"> </span>n += i</td><td rowspan="1" colspan="1">n = 0<br /> for i in 1:1000000<br /> <span style="white-space: pre-wrap;"> </span>n += i<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="arith-seq-to-array" shape="rect" id="arith-seq-to-array"></a><a href="numerical-analysis#arith-seq-to-array-note" shape="rect">to array</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">a = range(1, 11)<br /> <span style="color: gray"># Python 3:</span><br /> a = list(range(1, 11))</td><td rowspan="1" colspan="1">a = Array(1:10)</td></tr><tr><th colspan="5" rowspan="1"><a name="two-d-arrays" shape="rect" id="two-d-arrays"></a><a href="numerical-analysis#two-d-arrays-note" shape="rect">two dimensional arrays</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-elem-type" shape="rect" id="array-2d-elem-type"></a><a href="numerical-analysis#array-2d-elem-type-note" shape="rect">element type</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>always numeric</em></span></td><td rowspan="1" colspan="1">A = array(c(1, 2, 3, 4), dim=c(2, 2))<br /> <br /> <span style="color: gray"># "array":</span><br /> class(A)<br /> <br /> <span style="color: gray"># "boolean", "numeric", or "string":</span><br /> class(c(A))</td><td rowspan="1" colspan="1">np.array([[1, 2], [3, 4]]).dtype<br /> <br /> <span style="color: gray"># possible values: np.bool, np.int64,<br /> # np.float64, np.complex128, <span style="white-space: pre-wrap;">...</span></span></td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> eltype(A)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-literal" shape="rect" id="array-2d-literal"></a><a href="numerical-analysis#array-2d-literal-note" shape="rect">literal</a></td><td rowspan="1" colspan="1">[1, 2; 3, 4]<br /> <br /> <span style="color: gray">% commas optional; newlines can replace semicolons::</span><br /> [1 2<br /> <span style="white-space: pre-wrap;"> </span>3 4]</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">[1 2; 3 4]<br /> <br /> <span style="color: gray"># A 1-d array created with commas is a<br /> # n×1 array. If commas are used in a literal,<br /> # then semicolons and spaces as delimiters.</span><br /> <br /> [1 2<br /> <span style="white-space: pre-wrap;"> </span>3 4]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-construct-seq" shape="rect" id="array-2d-construct-seq"></a><a href="numerical-analysis#array-2d-construct-seq-note" shape="rect">construct from sequence</a></td><td rowspan="1" colspan="1">reshape([1 2 3 4], 2, 2)</td><td rowspan="1" colspan="1">array(c(1, 2, 3, 4), dim=c(2, 2))</td><td rowspan="1" colspan="1">A = np.array([1, 2, 3, 4]).reshape(2, 2)<br /> <br /> <span style="color: gray"># convert to nested Python lists:</span><br /> A.tolist()</td><td rowspan="1" colspan="1">reshape([1, 2, 3, 4], 2, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-construct-rows" shape="rect" id="array-2d-construct-rows"></a><a href="numerical-analysis#array-2d-construct-rows-note" shape="rect">construct from rows</a></td><td rowspan="1" colspan="1">row1 = [1 2 3]<br /> row2 = [4 5 6]<br /> <br /> A = [row1; row2]</td><td rowspan="1" colspan="1">rbind(c(1, 2, 3), c(4, 5, 6))</td><td rowspan="1" colspan="1">row1 = np.array([1, 2, 3])<br /> row2 = np.array([4, 5, 6])<br /> <br /> np.vstack((row1, row2))<br /> <br /> np.array([[1, 2], [3, 4]])</td><td rowspan="1" colspan="1">vcat([1 2 3], [4 5 6])<br /> <br /> row1 = [1 2 3]<br /> row2 = [4 5 6]<br /> [row1; row2]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-construct-columns" shape="rect" id="array-2d-construct-columns"></a><a href="numerical-analysis#array-2d-construct-columns-note" shape="rect">construct from columns</a></td><td rowspan="1" colspan="1">col1 = [1; 4]<br /> col2 = [2; 5]<br /> col3 = [3; 6]<br /> <br /> <span style="color: gray">% commas are optional:</span><br /> A = [col1, col2, col3]</td><td rowspan="1" colspan="1">cbind(c(1, 4), c(2, 5), c(3, 6))</td><td rowspan="1" colspan="1">cols = (<br /> <span style="white-space: pre-wrap;"> </span>np.array([1, 4]),<br /> <span style="white-space: pre-wrap;"> </span>np.array([2, 5]),<br /> <span style="white-space: pre-wrap;"> </span>np.array([3, 6])<br /> )<br /> np.vstack(cols).transpose()</td><td rowspan="1" colspan="1">hcat([1, 4], [2, 5], [3, 6])<br /> <br /> col1 = [1, 4]<br /> col2 = [2, 5]<br /> col3 = [3, 6]<br /> [col1 col2 col3]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-construct-subarrays" shape="rect" id="array-2d-construct-subarrays"></a><a href="numerical-analysis#array-2d-construct-subarrays-note" shape="rect">construct from subarrays</a></td><td rowspan="1" colspan="1">A = [1 3; 2 4]<br /> <br /> A4_by_2 = [A; A]<br /> A2_by_4 = [A A]</td><td rowspan="1" colspan="1">A = matrix(c(1, 2, 3, 4), nrow=2)<br /> A4_by_2 = rbind(A, A)<br /> A2_by_4 = cbind(A, A)</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> A2_by_4 = np.hstack([A, A])<br /> A4_by_2 = np.vstack([A, A])</td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> A4_by_2 = [A; A]<br /> A2_by_4 = [A A ]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-cast" shape="rect" id="array-2d-cast"></a><a href="numerical-analysis#array-2d-cast-note" shape="rect">cast element type</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-size" shape="rect" id="array-2d-size"></a><a href="numerical-analysis#array-2d-size-note" shape="rect">size</a><br /> <span style="color: gray"><em>number of elements, number of dimensions, dimension lengths</em></span></td><td rowspan="1" colspan="1">numel(A)<br /> ndims(A)<br /> size(A)<br /> <br /> <span style="color: gray">% length of 1st dimension (i.e. # of rows):</span><br /> size(A, 1)<br /> <br /> <span style="color: gray">% length of longest dimension:</span><br /> length(A)</td><td rowspan="1" colspan="1">length(A)<br /> length(dim(A))<br /> dim(A)</td><td rowspan="1" colspan="1">A.size<br /> A.ndim<br /> A.shape<br /> <br /> <span style="color: gray"># number of rows:</span><br /> len(A)</td><td rowspan="1" colspan="1">length(A)<br /> ndims(A)<br /> size(A)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-lookup" shape="rect" id="array-2d-lookup"></a><a href="numerical-analysis#array-2d-lookup-note" shape="rect">lookup</a></td><td rowspan="1" colspan="1"><span style="color: gray">% indices start at one:</span><br /> [1 2; 3 4](1, 1)</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at one:</span><br /> A = array(c(1, 2, 3, 4), dim=c(2, 2)<br /> <br /> A[1, 1]</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at zero:</span><br /> A = np.array([[1, 2], [3, 4]])<br /> <br /> A[0][0] <span style="color: gray"><em>or</em></span><br /> A[0, 0]</td><td rowspan="1" colspan="1"><span style="color: gray"># indices start at one:</span><br /> A[1, 1]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-1d-lookup" shape="rect" id="array-2d-1d-lookup"></a><a href="numerical-analysis#array-2d-1d-lookup-note" shape="rect">1d lookup</a></td><td rowspan="1" colspan="1">A = [2 4; 6 8]<br /> <span style="color: gray">% returns 8:</span><br /> A(4)<br /> <br /> <span style="color: gray">% convert to column vector of length 4:</span><br /> A2 = A(:)</td><td rowspan="1" colspan="1">A = array(c(2, 4, 6, 8), dim=c(2, 2))<br /> <br /> <span style="color: gray"># returns 8:</span><br /> A[4]</td><td rowspan="1" colspan="1">A = np.array([[2, 4], [6, 8]])<br /> <br /> <span style="color: gray"># returns np.array([6, 8]):</span><br /> A[1]<br /> <br /> <span style="color: gray"># returns 8:</span><br /> A.flat[3]</td><td rowspan="1" colspan="1">A = [2 4; 6 8]<br /> <br /> <span style="color: gray"># returns 8:</span><br /> A[4]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-slice" shape="rect" id="array-2d-slice"></a><a href="numerical-analysis#array-2d-slice-note" shape="rect">lookup row or column</a></td><td rowspan="1" colspan="1">A = [1 2 3; 4 5 6; 7 8 9]<br /> <br /> <span style="color: gray">% 2nd row:</span><br /> A(2, :)<br /> <br /> <span style="color: gray">% 2nd column:</span><br /> A(:, 2)</td><td rowspan="1" colspan="1">A = t(array(1:9, dim=c(3, 3)))<br /> <br /> <span style="color: gray"># 2nd row:</span><br /> A[2, ]<br /> <br /> <span style="color: gray"># 2nd column:</span><br /> A[, 2]</td><td rowspan="1" colspan="1">A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<br /> <br /> <span style="color: gray"># 2nd row:</span><br /> A[1, :]<br /> <br /> <span style="color: gray"># 2nd column:</span><br /> A[:, 1]</td><td rowspan="1" colspan="1">A = [1 2 3; 4 5 6; 7 8 9]<br /> <br /> <span style="color: gray"># 2nd row:</span><br /> A[2, :]<br /> <br /> <span style="color: gray"># 2nd column:</span><br /> A[:, 2]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-update" shape="rect" id="array-2d-update"></a><a href="numerical-analysis#array-2d-update-note" shape="rect">update</a></td><td rowspan="1" colspan="1">A = [2 4; 6 8]<br /> A(1, 1) = 3</td><td rowspan="1" colspan="1">A = array(c(2, 4, 6, 8), dim=c(2, 2))<br /> A[1, 1] = 3</td><td rowspan="1" colspan="1">A = np.array([[2, 4], [6, 8]])<br /> A[0, 0] = 3</td><td rowspan="1" colspan="1">A = [2 4; 6 8]<br /> A[1, 1] = 3</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-update-row-col" shape="rect" id="array-2d-update-row-col"></a><a href="numerical-analysis#array-2d-update-row-col-note" shape="rect">update row or column</a></td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> <span style="color: gray">% [2 1; 3 4]:</span><br /> A(1, :) = [2 1]<br /> <br /> <span style="color: gray">% [3 1; 2 4]:</span><br /> A(:, 1) = [3 2]</td><td rowspan="1" colspan="1">A = t(array(1:4, dim=c(2, 2)))<br /> <br /> A[1, ] = c(2, 1)<br /> A[, 1] = c(3, 2)</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> <br /> A[0, :] = [2, 1]<br /> <span style="color: gray"># or</span><br /> A[0] = [2, 1]<br /> <br /> A[:, 0] = [3, 2]</td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> A[1, :] = [2 1]<br /> A[:, 1] = [3; 2]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-update-subarray" shape="rect" id="array-2d-update-subarray"></a><a href="numerical-analysis#array-2d-update-subarray-note" shape="rect">update subarray</a></td><td rowspan="1" colspan="1">A = ones(3, 3)<br /> A(1:2, 1:2) = 2 * ones(2, 2)<br /> <span style="color: gray">% or just:</span><br /> A(1:2, 1:2) = 2</td><td rowspan="1" colspan="1">A = array(1, dim=c(3, 3))<br /> A[1:2, 1:2] = array(2, dim=c(2, 2))<br /> <span style="color: gray"># or just:</span><br /> A[1:2, 1:2] = 2</td><td rowspan="1" colspan="1">A = np.ones([3, 3])<br /> A[0:2, 0:2] = 2 * np.ones([2, 2])</td><td rowspan="1" colspan="1">A = ones(3, 3)<br /> A[1:2, 1:2] = 2 * ones(2, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-out-of-bounds" shape="rect" id="array-2d-out-of-bounds"></a><a href="numerical-analysis#array-2d-out-of-bounds-note" shape="rect">out-of-bounds behavior</a></td><td rowspan="1" colspan="1">A = [2 4; 6 8]<br /> <br /> <span style="color: gray">% error:</span><br /> x = A(3, 1)<br /> <br /> <span style="color: gray">% becomes 3x2 array with zero at (3, 2):</span><br /> A(3, 1) = 9</td><td rowspan="1" colspan="1"><span style="color: gray"><em>Lookups and updates both cause subscript out of bounds error.</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>Lookups and updates both raise an</em> IndexError <em>exception.</em></span></td><td rowspan="1" colspan="1"><span style="color: gray">BoundsError</span></td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-slice-subarray" shape="rect" id="array-2d-slice-subarray"></a><a href="numerical-analysis#array-2d-slice-subarray-note" shape="rect">slice subarray</a></td><td rowspan="1" colspan="1">A = reshape(1:16, 4, 4)'<br /> <br /> <span style="color: gray">% top left 2x2 subarray:</span><br /> A(1:2, 1:2)<br /> <br /> <span style="color: gray">% bottom right 2x2 subarray:</span><br /> A(end-1:end, end-1:end)<br /> <br /> <span style="color: gray">% 2x2 array containing corners:</span><br /> A([1 4], [1 4])<br /> A([1 end], [1 end])</td><td rowspan="1" colspan="1">A = t(array(1:16, dim=c(4, 4)))<br /> <br /> <span style="color: gray"># top left 2x2 subarray:</span><br /> A[1:2, 1:2]<br /> <br /> <span style="color: gray"># bottom right 2x2 subarray:</span><br /> A[-1:-2, -1:-2]<br /> <br /> <span style="color: gray"># 2x2 array containing corners:</span><br /> A[c(1, 4), c(1, 4)]</td><td rowspan="1" colspan="1">A = np.array(range(1, 17)).reshape(4, 4)<br /> <br /> <span style="color: gray"># top left 2x2 subarray:</span><br /> A[0:2, 0:2]<br /> <br /> <span style="color: gray"># bottom right 2x2 subarray:</span><br /> A[2:, 2:]</td><td rowspan="1" colspan="1">A = reshape(1:16, 4, 4)<br /> <br /> A[1:2, 1:2]<br /> <br /> A[3:4, 3:4]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-transpose" shape="rect" id="array-2d-transpose"></a><a href="numerical-analysis#array-2d-transpose-note" shape="rect">transpose</a></td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> transpose(A)</td><td rowspan="1" colspan="1">A = array(c(1, 2, 3, 4), dim=c(2, 2))<br /> t(A)</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> A.transpose()<br /> A.T</td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> transpose(A)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-flip" shape="rect" id="array-2d-flip"></a><a href="numerical-analysis#array-2d-flip-note" shape="rect">flip</a></td><td rowspan="1" colspan="1"><span style="color: gray">% [ 2 1; 4 3]:</span><br /> fliplr([1 2; 3 4])<br /> <br /> <span style="color: gray">% [3 4; 1 2]:</span><br /> flipud([1 2; 3 4])</td><td rowspan="1" colspan="1"><span style="color: gray"># install.packages('pracma'):</span><br /> require(pracma)<br /> <br /> A = t(array(1:4, dim=c(2, 2)))<br /> <br /> fliplr(A)<br /> flipud(A)</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> <br /> np.fliplr(A)<br /> np.flipud(A)</td><td rowspan="1" colspan="1"><span style="color: gray"># [2 1; 4 3]:</span><br /> flipdim([1 2; 3 4], 2)<br /> <br /> <span style="color: gray"># [3 4; 1 2]:</span><br /> flipdim([1 2; 3 4], 1)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-circular-shift" shape="rect" id="array-2d-circular-shift"></a><a href="numerical-analysis#array-2d-circular-shift-note" shape="rect">circular shift</a><br /> <br /> <span style="color: gray"><em>along columns, along rows</em></span></td><td rowspan="1" colspan="1">A = [1 2; 3, 4]<br /> <br /> <span style="color: gray">% [3 4; 1 2]:</span><br /> circshift(A, 1)<br /> <br /> <span style="color: gray">% [2 1; 4 3]:</span><br /> circshift(A, 1, 2)<br /> <br /> <span style="color: gray">% The 2nd argument can be any integer; negative values shift<br /> % in the opposite direction.</span></td><td rowspan="1" colspan="1"><span style="color: gray"># install.packages('pracma'):</span><br /> require(pracma)<br /> <br /> A = t(array(1:4, dim=c(2, 2)))<br /> <br /> circshift(A, c(1, 0))<br /> circshift(A, c(0, 1))</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> <br /> np.roll(A, 1, axis=0)<br /> np.roll(A, 1, axis=1)</td><td rowspan="1" colspan="1">circshift([1 2; 3 4], [1, 0])<br /> circshift([1 2; 3 4], [0, 1])</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-rotate" shape="rect" id="array-2d-rotate"></a><a href="numerical-analysis#array-2d-rotate-note" shape="rect">rotate</a><br /> <span style="color: gray"><em>clockwise, counter-clockwise</em></span></td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> <span style="color: gray">% [3 1; 4 2]:</span><br /> rot90(A, -1)<br /> <br /> <span style="color: gray">% [2 4; 1 3]:</span><br /> rot90(A)<br /> <br /> <span style="color: gray">% set 2nd arg to 2 for 180 degree rotation</span></td><td rowspan="1" colspan="1"><span style="color: gray"># install.packages('pracma'):</span><br /> require(pracma)<br /> <br /> A = t(array(1:4, dim=c(2, 2)))<br /> <br /> rot90(A)<br /> rot90(A, -1)<br /> rot90(A, 2)</td><td rowspan="1" colspan="1">A = np.array([[1, 2], [3, 4]])<br /> <br /> np.rot90(A)<br /> np.rot90(A, -1)<br /> np.rot90(A, 2)</td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> rotr90(A)<br /> rotl90(A)<br /> rotr90(A, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-2d-reduce" shape="rect" id="array-2d-reduce"></a><a href="numerical-analysis#array-2d-reduce-note" shape="rect">reduce</a><br /> <span style="color: gray"><em>rows, columns</em></span></td><td rowspan="1" colspan="1">M = [1 2; 3 4]<br /> <br /> <span style="color: gray">% sum each row:</span><br /> cellfun(@sum, num2cell(M, 2))<br /> <br /> <span style="color: gray">% sum each column:</span><br /> cellfun(@sum, num2cell(M, 1))<br /> <br /> <span style="color: gray">% sum(M, 2) and sum(M, 1) also sum rows and columns</span></td><td rowspan="1" colspan="1">M = matrix(c(1, 2, 3, 4), nrow=2)<br /> <br /> <span style="color: gray"># sum each row:</span><br /> apply(M, 1, sum)<br /> <br /> <span style="color: gray"># sum each column:</span><br /> apply(M, 2, sum)</td><td rowspan="1" colspan="1">M = np.array([[1, 2], [3, 4]])<br /> <br /> np.add.reduce(A, 1)<br /> <br /> np.add.reduce(A, 0)<br /> <br /> <span style="color: gray"># np.add is a built-in universal function. All universal functions have a reduce method.</span><br /> <br /> <span style="color: gray"># np.sum(A, 1,) and np.sum(A, 0) also sum rows and columns</span></td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> <br /> <span style="color: gray">[3; 7]:</span><br /> reducedim(+, A, [2], 0)<br /> <br /> <span style="color: gray">[4 6]:</span><br /> reducedim(+, A, [1], 0)</td></tr><tr><th colspan="5" rowspan="1"><a name="three-d-arrays" shape="rect" id="three-d-arrays"></a><a href="numerical-analysis#three-d-arrays-note" shape="rect">three dimensional arrays</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-construct-seq" shape="rect" id="array-3d-construct-seq"></a><a href="numerical-analysis#array-3d-construct-seq-note" shape="rect">construct from sequence</a></td><td rowspan="1" colspan="1">reshape([1 2 3 4 5 6 7 8], 2, 2, 2)</td><td rowspan="1" colspan="1">array(seq(1, 8), dim=c(2, 2, 2))</td><td rowspan="1" colspan="1">np.array(range(1, 9)).reshape(2, 2, 2)</td><td rowspan="1" colspan="1">reshape(1:8, 2, 2, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-construct-nested-seq" shape="rect" id="array-3d-construct-nested-seq"></a><a href="numerical-analysis#array-3d-construct-nested-seq-note" shape="rect">construct from nested sequences</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-construct-2d-arrays" shape="rect" id="array-3d-construct-2d-arrays"></a><a href="numerical-analysis#array-3d-construct-2d-arrays-note" shape="rect">construct 3d array from 2d arrays</a></td><td rowspan="1" colspan="1">A = [1, 2; 3, 4]<br /> A(:,:,2) = [5, 6; 7, 8]</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">A = Array{Float64}(2, 2, 2)<br /> A[:, :, 1] = [1 2; 3 4]<br /> A[:, :, 2] = [5 6; 7 8]</td></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-permute-axes" shape="rect" id="array-3d-permute-axes"></a><a href="numerical-analysis#array-3d-permute-axes-note" shape="rect">permute axes</a></td><td rowspan="1" colspan="1">A = reshape([1 2 3 4 5 6 7 8], 2, 2, 2)<br /> <br /> <span style="color: gray">% swap 2nd and 3rd axes:</span><br /> permute(A, [1 3 2])</td><td rowspan="1" colspan="1">A = array(1:8, dim=c(2, 2, 2))<br /> <br /> <span style="color: gray"># swap 2nd and 3rd axes:</span><br /> aperm(A, perm=c(1, 3, 2))</td><td rowspan="1" colspan="1">A = np.array(range(1, 9)).reshape(2, 2, 2)<br /> <br /> <span style="color: gray"># swap 2nd and 3rd axes:</span><br /> A.transpose((0, 2, 1))</td><td rowspan="1" colspan="1">A = reshape(1:8, 2, 2, 2)<br /> <br /> <span style="color: gray"># swap 2nd and 3rd axes:</span><br /> reshape(A, [1, 3, 2])</td></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-flip" shape="rect" id="array-3d-flip"></a><a href="numerical-analysis#array-3d-flip-note" shape="rect">flip</a></td><td rowspan="1" colspan="1">A = reshape([1 2 3 4 5 6 7 8], 2, 2, 2)<br /> <br /> flipdim(A, 3)</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">A = reshape(1:8, 2, 2, 2)<br /> <br /> flipdim(A, 3)</td></tr><tr><td rowspan="1" colspan="1"><a name="array-3d-circular-shift" shape="rect" id="array-3d-circular-shift"></a><a href="numerical-analysis#array-3d-circular-shift-note" shape="rect">circular shift</a></td><td rowspan="1" colspan="1">A = reshape([1 2 3 4 5 6 7 8], 2, 2, 2)<br /> <br /> <span style="color: gray">% 3rd arg specifies axis:</span><br /> circshift(A, 1, 3)</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">A = np.array(range(1, 9)).reshape(2, 2, 2)<br /> <br /> np.roll(A, 1, axis=2)</td><td rowspan="1" colspan="1">A = reshape(1:8, 2, 2, 2)<br /> <br /> circshift(A, [0, 0, 1])</td></tr><tr><th colspan="5" rowspan="1"><a name="dictionaries" shape="rect" id="dictionaries"></a><a href="numerical-analysis#dictionaries-note" shape="rect">dictionaries</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="dict-literal" shape="rect" id="dict-literal"></a><a href="numerical-analysis#dict-literal-note" shape="rect">literal</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray">% no literal; use constructor:</span><br /> d = struct('n', 10, 'avg', 3.7, 'sd', 0.4)<br /> <br /> <span style="color: gray">% or build from two cell arrays:</span><br /> d = cell2struct({10 3.7 0.4}, {'n' 'avg' 'sd'}, 2)</td><td rowspan="1" colspan="1"><span style="color: gray"># keys are 'n', 'avg', and 'sd':</span><br /> d = list(n=10, avg=3.7, sd=0.4)<br /> <br /> <span style="color: gray"># keys are 1, 2, and 3:</span><br /> d2 = list('do', 're', 'mi')</td><td rowspan="1" colspan="1">d = {'n': 10, 'avg': 3.7, 'sd': 0.4}</td><td rowspan="1" colspan="1">d = Dict("n"=>10.0, "avg"=>3.7, "sd"=>0.4)</td></tr><tr><td rowspan="1" colspan="1"><a name="dict-size" shape="rect" id="dict-size"></a><a href="numerical-analysis#dict-size-note" shape="rect">size</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">length(fieldnames(d))</td><td rowspan="1" colspan="1">length(d)</td><td rowspan="1" colspan="1">len(d)</td><td rowspan="1" colspan="1">length(d)</td></tr><tr><td rowspan="1" colspan="1"><a name="dict-lookup" shape="rect" id="dict-lookup"></a><a href="numerical-analysis#dict-lookup-note" shape="rect">lookup</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">d.n<br /> getfield(d, 'n')</td><td rowspan="1" colspan="1">d[['n']]<br /> <br /> <span style="color: gray"># if 'n' is a key:</span><br /> d$n</td><td rowspan="1" colspan="1">d['n']</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-update" shape="rect" id="dict-update"></a><a href="numerical-analysis#dict-update-note" shape="rect">update</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">d.var = d.sd<span style="white-space: pre-wrap;">**2</span></td><td rowspan="1" colspan="1">d$var = d$sd<span style="white-space: pre-wrap;">**</span>2</td><td rowspan="1" colspan="1">d['var'] = d['sd']<span style="white-space: pre-wrap;">**</span>2</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-missing-key" shape="rect" id="dict-missing-key"></a><a href="numerical-analysis#dict-missing-key-note" shape="rect">missing key behavior</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>error</em></span></td><td rowspan="1" colspan="1">NULL</td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> KeyError</span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-is-key-present" shape="rect" id="dict-is-key-present"></a><a href="numerical-analysis#dict-is-key-present-note" shape="rect">is key present</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">isfield(d, 'var')</td><td rowspan="1" colspan="1">is.null(d$var)</td><td rowspan="1" colspan="1">'var' in d</td><td rowspan="1" colspan="1">haskey(d, "var")</td></tr><tr><td rowspan="1" colspan="1"><a name="dict-delete" shape="rect" id="dict-delete"></a><a href="numerical-analysis#dict-delete-note" shape="rect">delete</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">d = rmfield(d, 'sd')</td><td rowspan="1" colspan="1">d$sd = NULL</td><td rowspan="1" colspan="1">del(d['sd'])</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-iterate" shape="rect" id="dict-iterate"></a><a href="numerical-analysis#dict-iterate-note" shape="rect">iterate</a></td><td rowspan="1" colspan="1">for i = 1:numel(fieldnames(d))<br /> <span style="white-space: pre-wrap;"> </span>k = fieldnames(d){i}<br /> <span style="white-space: pre-wrap;"> </span>v = d.(k)<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>code</em></span><br /> end</td><td rowspan="1" colspan="1">for (k in names(d)) {<br /> <span style="white-space: pre-wrap;"> </span>v = d[[k]]<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>code</em></span><br /> }</td><td rowspan="1" colspan="1">for k, v in d.iteritems():<br /> <span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>code</em></span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-keys-values-arrays" shape="rect" id="dict-keys-values-arrays"></a><a href="numerical-analysis#dict-keys-values-arrays-note" shape="rect">keys and values as arrays</a></td><td rowspan="1" colspan="1"><span style="color: gray">% these return cell arrays:</span><br /> fieldnames(d)<br /> struct2cell(d)</td><td rowspan="1" colspan="1">names(d)<br /> unlist(d, use.names=F)</td><td rowspan="1" colspan="1">d.keys()<br /> d.values()</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dict-merge" shape="rect" id="dict-merge"></a><a href="numerical-analysis#dict-merge-note" shape="rect">merge</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">d1 = list(a=1, b=2)<br /> d2 = list(b=3, c=4)<br /> <span style="color: gray"># values of first dictionary take precedence:</span><br /> d3 = c(d1, d2)</td><td rowspan="1" colspan="1">d1 = {'a':1, 'b':2}<br /> d2 = {'b':3, 'c':4}<br /> d1.update(d2)</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="functions" shape="rect" id="functions"></a><a href="numerical-analysis#functions-note" shape="rect">functions</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="def-func" shape="rect" id="def-func"></a><a href="numerical-analysis#def-func-note" shape="rect">define function</a></td><td rowspan="1" colspan="1">function add(x, y)<br /> <span style="white-space: pre-wrap;"> </span>x + y<br /> end</td><td rowspan="1" colspan="1">add = function(x, y) {x + y}</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">function add(x,y)<br /> <span style="white-space: pre-wrap;"> </span>x + y<br /> end<br /> <br /> <span style="color: gray"># optional syntax when body is an expression:</span><br /> add(x, y) = x + y</td></tr><tr><td rowspan="1" colspan="1"><a name="invoke-func" shape="rect" id="invoke-func"></a><a href="numerical-analysis#invoke-func-note" shape="rect">invoke function</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">add(3, 7)</td><td rowspan="1" colspan="1">add(3, 7)</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">add(3, 7)</td></tr><tr><td rowspan="1" colspan="1"><a name="nested-func" shape="rect" id="nested-func"></a><a href="numerical-analysis#nested-func-note" shape="rect">nested function</a></td><td rowspan="1" colspan="1">function ret1 = add3(x, y, z)<br /> <span style="white-space: pre-wrap;"> </span>function ret2 = add2(x, y)<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>ret2 = x + y;<br /> <span style="white-space: pre-wrap;"> </span>end<br /> <br /> <span style="white-space: pre-wrap;"> </span>ret1 = add2(x, y) + z;<br /> end</td><td rowspan="1" colspan="1">add3 = function(x, y, z) {<br /> <span style="white-space: pre-wrap;"> </span>add2 = function(x, y) { x + y }<br /> <span style="white-space: pre-wrap;"> </span>add2(x, y) + z<br /> }</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">function add3(x, y, z)<br /> <span style="white-space: pre-wrap;"> </span>function add2(x2, y2)<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>x2 + y2<br /> <span style="white-space: pre-wrap;"> </span>end<br /> <span style="white-space: pre-wrap;"> </span>add2(x, y) + z<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="missing-arg" shape="rect" id="missing-arg"></a><a href="numerical-analysis#missing-arg-note" shape="rect">missing argument behavior</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises error if code with the parameter that is missing an argument is executed</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises error</em></span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> MethodError</span></td></tr><tr><td rowspan="1" colspan="1"><a name="extra-arg" shape="rect" id="extra-arg"></a><a href="numerical-analysis#extra-arg-note" shape="rect">extra argument behavior</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>ignored</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises error</em></span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raises</em> MethodError</span></td></tr><tr><td rowspan="1" colspan="1"><a name="default-arg" shape="rect" id="default-arg"></a><a href="numerical-analysis#default-arg-note" shape="rect">default argument</a></td><td rowspan="1" colspan="1">function mylog(x, base=10)<br /> <span style="white-space: pre-wrap;"> </span>log(x) / log(base)<br /> end</td><td rowspan="1" colspan="1">mylog = function(x,base=10) {<br /> <span style="white-space: pre-wrap;"> </span>log(x) / log(base)<br /> }</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="variadic-func" shape="rect" id="variadic-func"></a><a href="numerical-analysis#variadic-func-note" shape="rect">variadic function</a></td><td rowspan="1" colspan="1">function s = add(varargin)<br /> <span style="white-space: pre-wrap;"> </span>if nargin == 0<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s = 0<br /> <span style="white-space: pre-wrap;"> </span>else<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>r = add(varargin{2:nargin})<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s = varargin{1} + r<br /> <span style="white-space: pre-wrap;"> </span>end<br /> end</td><td rowspan="1" colspan="1">add = function (<span style="white-space: pre-wrap;">...</span>) {<br /> <span style="white-space: pre-wrap;"> </span>a = list(<span style="white-space: pre-wrap;">...</span>)<br /> <span style="white-space: pre-wrap;"> </span>if (length(a) == 0)<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>return(0)<br /> <span style="white-space: pre-wrap;"> </span>s = 0<br /> <span style="white-space: pre-wrap;"> </span>for(i in 1:length(a)) {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>s = s + a[[i]]<br /> <span style="white-space: pre-wrap;"> </span>}<br /> <span style="white-space: pre-wrap;"> </span>return(s)<br /> }</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="retval" shape="rect" id="retval"></a><a href="numerical-analysis#retval-note" shape="rect">return value</a></td><td rowspan="1" colspan="1">function ret = add(x, y)<br /> <span style="white-space: pre-wrap;"> </span>ret = x + y;<br /> end<br /> <br /> <span style="color: gray">% If a return variable is declared, the<br /> % value assigned to it is returned. Otherwise<br /> % the value of the last statement will be<br /> % used if it does not end with a semicolon.</span></td><td rowspan="1" colspan="1"><span style="color: gray">return <em>argument or last expression evaluated.</em> NULL <em>if</em> return <em>called without an argument.</em></span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray">return <em>argument or last expression evaluated.</em> Void <em>if</em> return <em>called without an argument.</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="multiple-retval" shape="rect" id="multiple-retval"></a><a href="numerical-analysis#multiple-retval-note" shape="rect">multiple return values</a></td><td rowspan="1" colspan="1">function [x, y] = first_two(a)<br /> <span style="white-space: pre-wrap;"> </span>x = a(1);<br /> <span style="white-space: pre-wrap;"> </span>y = a(2);<br /> end<br /> <br /> <span style="color: gray">% sets first to 7; second to 8:</span><br /> [first, second] = first_two([7 8 9])</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">function first_two(a)<br /> <span style="white-space: pre-wrap;"> </span>a[1], a[2]<br /> end<br /> <br /> x, y = first_two([1, 2, 3])</td></tr><tr><td rowspan="1" colspan="1"><a name="anonymous-func-literal" shape="rect" id="anonymous-func-literal"></a><a href="numerical-analysis#anonymous-func-literal-note" shape="rect">anonymous function literal</a></td><td rowspan="1" colspan="1"><span style="color: gray">% body must be an expression:</span><br /> @(x, y) x + y</td><td rowspan="1" colspan="1">function(x, y) {x + y}</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">add = (x, y) -> x + y<br /> <br /> add = function(x, y)<br /> <span style="white-space: pre-wrap;"> </span>x + y<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="invoke-anonymous-func" shape="rect" id="invoke-anonymous-func"></a><a href="numerical-analysis#invoke-anonymous-func-note" shape="rect">invoke anonymous function</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">add(1, 2)</td></tr><tr><td rowspan="1" colspan="1"><a name="closure" shape="rect" id="closure"></a><a href="numerical-analysis#closure-note" shape="rect">closure</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">make_counter = function() {<br /> <span style="white-space: pre-wrap;"> </span>i = 0<br /> <span style="white-space: pre-wrap;"> </span>function() {<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>i <span style="white-space: pre-wrap;"><<</span>- i + 1<br /> <span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>i<br /> <span style="white-space: pre-wrap;"> </span>}<br /> }</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="func-as-val" shape="rect" id="func-as-val"></a><a href="numerical-analysis#func-as-val-note" shape="rect">function as value</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">@add</td><td rowspan="1" colspan="1">add</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">add</td></tr><tr><td rowspan="1" colspan="1"><a name="overload-operator" shape="rect" id="overload-operator"></a><a href="numerical-analysis#overload-operator-note" shape="rect">overload operator</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="call-op-like-func" shape="rect" id="call-op-like-func"></a><a href="numerical-analysis#call-op-like-func-note" shape="rect">call operator like function</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="white-space: pre-wrap;">`+`</span>(3, 7)</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">+(3, 7)</td></tr><tr><th colspan="5" rowspan="1"><a name="execution-control" shape="rect" id="execution-control"></a><a href="numerical-analysis#execution-control-note" shape="rect">execution control</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="if" shape="rect" id="if"></a><a href="numerical-analysis#if-note" shape="rect">if</a></td><td rowspan="1" colspan="1">if (x > 0)<br /> <span style="white-space: pre-wrap;"> </span>disp('positive')<br /> elseif (x < 0)<br /> <span style="white-space: pre-wrap;"> </span>disp('negative')<br /> else<br /> <span style="white-space: pre-wrap;"> </span>disp('zero')<br /> end</td><td rowspan="1" colspan="1">if (x > 0) {<br /> <span style="white-space: pre-wrap;"> </span>print('positive')<br /> } else if (x < 0) {<br /> <span style="white-space: pre-wrap;"> </span>print('negative')<br /> } else {<br /> <span style="white-space: pre-wrap;"> </span>print('zero')<br /> }</td><td rowspan="1" colspan="1">if x > 0:<br /> <span style="white-space: pre-wrap;"> </span>print('positive')<br /> elif x < 0:<br /> <span style="white-space: pre-wrap;"> </span>print('negative')<br /> else:<br /> <span style="white-space: pre-wrap;"> </span>print('zero')</td><td rowspan="1" colspan="1">if x > 0<br /> <span style="white-space: pre-wrap;"> </span>println("positive")<br /> elseif x < 0<br /> <span style="white-space: pre-wrap;"> </span>println("negative")<br /> else<br /> <span style="white-space: pre-wrap;"> </span>println("zero")<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="while" shape="rect" id="while"></a><a href="numerical-analysis#while-note" shape="rect">while</a></td><td rowspan="1" colspan="1">i = 0<br /> while (i < 10)<br /> <span style="white-space: pre-wrap;"> </span>i = i + 1<br /> <span style="white-space: pre-wrap;"> </span>disp(i)<br /> end</td><td rowspan="1" colspan="1">while (i < 10) {<br /> <span style="white-space: pre-wrap;"> </span>i = i + 1<br /> <span style="white-space: pre-wrap;"> </span>print(i)<br /> }</td><td rowspan="1" colspan="1">while i < 10:<br /> <span style="white-space: pre-wrap;"> </span>i += 1<br /> <span style="white-space: pre-wrap;"> </span>print(i)</td><td rowspan="1" colspan="1">i = 0<br /> while i < 10<br /> <span style="white-space: pre-wrap;"> </span>i += 1<br /> <span style="white-space: pre-wrap;"> </span>println(i)<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="for" shape="rect" id="for"></a><a href="numerical-analysis#for-note" shape="rect">for</a></td><td rowspan="1" colspan="1">for i = 1:10<br /> <span style="white-space: pre-wrap;"> </span>disp(i)<br /> end</td><td rowspan="1" colspan="1">for (i in 1:10) {<br /> <span style="white-space: pre-wrap;"> </span>print(i)<br /> }</td><td rowspan="1" colspan="1">for i in range(1,11):<br /> <span style="white-space: pre-wrap;"> </span>print(i)</td><td rowspan="1" colspan="1">for i = 1:10<br /> <span style="white-space: pre-wrap;"> </span>println(i)<br /> end</td></tr><tr><td rowspan="1" colspan="1"><a name="break-continue" shape="rect" id="break-continue"></a><a href="numerical-analysis#break-continue-note" shape="rect">break/continue</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">break continue</td><td rowspan="1" colspan="1">break next</td><td rowspan="1" colspan="1">break continue</td><td rowspan="1" colspan="1">break continue</td></tr><tr><td rowspan="1" colspan="1"><a name="raise-exc" shape="rect" id="raise-exc"></a><a href="numerical-analysis#raise-exc-note" shape="rect">raise exception</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">error('%s', 'failed')</td><td rowspan="1" colspan="1">stop('failed')</td><td rowspan="1" colspan="1">raise Exception('failed')</td><td rowspan="1" colspan="1">throw("failed")</td></tr><tr><td rowspan="1" colspan="1"><a name="handle-exc" shape="rect" id="handle-exc"></a><a href="numerical-analysis#handle-exc-note" shape="rect">handle exception</a></td><td rowspan="1" colspan="1">try<br /> <span style="white-space: pre-wrap;"> </span>error('failed')<br /> catch err<br /> <span style="white-space: pre-wrap;"> </span>disp(err)<br /> end</td><td rowspan="1" colspan="1">tryCatch(<br /> <span style="white-space: pre-wrap;"> </span>stop('failed'),<br /> <span style="white-space: pre-wrap;"> </span>error=function(e) print(message(e)))</td><td rowspan="1" colspan="1">try:<br /> <span style="white-space: pre-wrap;"> </span>raise Exception('failed')<br /> except Exception as e:<br /> <span style="white-space: pre-wrap;"> </span>print(e)</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="file-handle" shape="rect" id="file-handle"></a><a href="numerical-analysis#file-handle-note" shape="rect">file handles</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="std-file-handles" shape="rect" id="std-file-handles"></a><a href="numerical-analysis#std-file-handles-note" shape="rect">standard file handles</a></td><td rowspan="1" colspan="1">0 1 2<br /> <br /> <span style="color: gray">% Octave has predefined variables<br /> % containing the above descriptors:</span><br /> stdin stdout stderr</td><td rowspan="1" colspan="1">stdin() stdout() stderr()</td><td rowspan="1" colspan="1">sys.stdin sys.stdout sys.stderr</td><td rowspan="1" colspan="1">STDIN STDOUT STDERR</td></tr><tr><td rowspan="1" colspan="1"><a name="read-line-stdin" shape="rect" id="read-line-stdin"></a><a href="numerical-analysis#read-line-stdin-note" shape="rect">read line from stdin</a></td><td rowspan="1" colspan="1">line = input('', 's')</td><td rowspan="1" colspan="1">line = readLines(n=1)</td><td rowspan="1" colspan="1">line = sys.stdin.readline()</td><td rowspan="1" colspan="1">line = readline()</td></tr><tr><td rowspan="1" colspan="1"><a name="write-line-stdout" shape="rect" id="write-line-stdout"></a><a href="numerical-analysis#write-line-stdout-note" shape="rect">write line to stdout</a></td><td rowspan="1" colspan="1">fprintf(1, 'hello\n')</td><td rowspan="1" colspan="1">cat("hello\n")<br /> <br /> writeLines("hello")</td><td rowspan="1" colspan="1">print('hello')</td><td rowspan="1" colspan="1">println("hello")</td></tr><tr><td rowspan="1" colspan="1"><a name="printf" shape="rect" id="printf"></a><a href="numerical-analysis#printf-note" shape="rect">write formatted string to stdout</a></td><td rowspan="1" colspan="1">fprintf(1, '%.2f\n', pi)</td><td rowspan="1" colspan="1">cat(sprintf("%.2f\n", pi))</td><td rowspan="1" colspan="1">import math<br /> <br /> print('%.2f' % math.pi)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="open-file" shape="rect" id="open-file"></a><a href="numerical-analysis#open-file-note" shape="rect">open file for reading</a></td><td rowspan="1" colspan="1">f = fopen('/etc/hosts')<br /> if (f == -1)<br /> <span style="white-space: pre-wrap;"> </span>error('failed to open file')<br /> end</td><td rowspan="1" colspan="1">f = file("/etc/hosts", "r")</td><td rowspan="1" colspan="1">f = open('/etc/hosts')</td><td rowspan="1" colspan="1">f = open("/etc/hosts")</td></tr><tr><td rowspan="1" colspan="1"><a name="open-file-write" shape="rect" id="open-file-write"></a><a href="numerical-analysis#open-file-write-note" shape="rect">open file for writing</a></td><td rowspan="1" colspan="1">if ((f = fopen('/tmp/test', 'w') == -1)<br /> <span style="white-space: pre-wrap;"> </span>error('failed to open file')<br /> endif</td><td rowspan="1" colspan="1">f = file("/tmp/test", "w")</td><td rowspan="1" colspan="1">f = open('/tmp/test', 'w')</td><td rowspan="1" colspan="1">f = open("/etc/hosts", "w")</td></tr><tr><td rowspan="1" colspan="1"><a name="open-file-append" shape="rect" id="open-file-append"></a><a href="numerical-analysis#open-file-append-note" shape="rect">open file for appending</a></td><td rowspan="1" colspan="1">if ((f = fopen('/tmp/err.log', 'a') == -1)<br /> <span style="white-space: pre-wrap;"> </span>error('failed to open file')<br /> endif</td><td rowspan="1" colspan="1">f = file("/tmp/err.log", "a")</td><td rowspan="1" colspan="1">f = open('/tmp/err.log', 'a')</td><td rowspan="1" colspan="1">f = open("/tmp/err.log", "a")</td></tr><tr><td rowspan="1" colspan="1"><a name="close-file" shape="rect" id="close-file"></a><a href="numerical-analysis#close-file-note" shape="rect">close file</a></td><td rowspan="1" colspan="1">fclose(f)</td><td rowspan="1" colspan="1">close(f)</td><td rowspan="1" colspan="1">f.close()</td><td rowspan="1" colspan="1">close(f)</td></tr><tr><td rowspan="1" colspan="1"><a name="io-err" shape="rect" id="io-err"></a><a href="numerical-analysis#io-err-note" shape="rect">i/o errors</a></td><td rowspan="1" colspan="1"><span style="color: gray">fopen <em>returns -1;</em> fclose <em>throws an error</em></span></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"><em>raise</em> IOError <em>exception</em></span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="read-line" shape="rect" id="read-line"></a><a href="numerical-analysis#read-line-note" shape="rect">read line</a></td><td rowspan="1" colspan="1">line = fgets(f)</td><td rowspan="1" colspan="1">line = readLines(f, n=1)</td><td rowspan="1" colspan="1">line = f.readline()</td><td rowspan="1" colspan="1">line = readline(f)</td></tr><tr><td rowspan="1" colspan="1"><a name="file-iterate" shape="rect" id="file-iterate"></a><a href="numerical-analysis#file-iterate-note" shape="rect">iterate over file by line</a></td><td rowspan="1" colspan="1">while(!feof(f))<br /> <span style="white-space: pre-wrap;"> </span>line = fgets(f)<br /> <span style="white-space: pre-wrap;"> </span>puts(line)<br /> endwhile</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">for line in f:<br /> <span style="white-space: pre-wrap;"> </span>print(line)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="read-file-array" shape="rect" id="read-file-array"></a><a href="numerical-analysis#read-file-array-note" shape="rect">read file into array of strings</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">lines = readLines(f)</td><td rowspan="1" colspan="1">lines = f.readlines()</td><td rowspan="1" colspan="1">lines = readlines(f)</td></tr><tr><td rowspan="1" colspan="1"><a name="write-str" shape="rect" id="write-str"></a><a href="numerical-analysis#write-str-note" shape="rect">write string</a></td><td rowspan="1" colspan="1">fputs(f, 'lorem ipsum')</td><td rowspan="1" colspan="1">cat("lorem ipsum", file=f)</td><td rowspan="1" colspan="1">f.write('lorem ipsum')</td><td rowspan="1" colspan="1">write(f, "lorem ipsum")</td></tr><tr><td rowspan="1" colspan="1"><a name="write-line" shape="rect" id="write-line"></a><a href="numerical-analysis#write-line-note" shape="rect">write line</a></td><td rowspan="1" colspan="1">fputs(f, 'lorem ipsum\n')</td><td rowspan="1" colspan="1">writeLines("lorem ipsum", con=f)</td><td rowspan="1" colspan="1">f.write('lorem ipsum\n')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="flush" shape="rect" id="flush"></a><a href="numerical-analysis#flush-note" shape="rect">flush file handle</a></td><td rowspan="1" colspan="1">fflush(f)</td><td rowspan="1" colspan="1">flush(f)</td><td rowspan="1" colspan="1">f.flush()</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="seek" shape="rect" id="seek"></a><a href="numerical-analysis#seek-note" shape="rect">file handle position</a><br /> <span style="color: gray"><em>get, set</em></span></td><td rowspan="1" colspan="1">ftell(f)<br /> <br /> <span style="color: gray">% 3rd arg can be SEEK_CUR or SEEK_END</span><br /> fseek(f, 0, SEEK_SET)</td><td rowspan="1" colspan="1">seek(f)<br /> <br /> <span style="color: gray"># sets seek point to 12 bytes after start;<br /> # origin can also be "current" or "end"</span><br /> seek(f, where=0, origin="start")</td><td rowspan="1" colspan="1">f.tell()<br /> <br /> f.seek(0)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="redirect-stdout-to-file" shape="rect" id="redirect-stdout-to-file"></a><a href="numerical-analysis#redirect-stdout-to-file-note" shape="rect">redirect stdout to file</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">sink("foo.txt")</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1">write variables to file</td><td rowspan="1" colspan="1">A = [1 2; 3 4]<br /> B = [4 3; 2 1]<br /> <br /> save('data.mdata', 'A', 'B')</td><td rowspan="1" colspan="1">A = matrix(c(1, 3, 2, 4), nrow=2)<br /> B = matrix(c(4, 2, 3, 1), nrow=2)<br /> <br /> save(A, B, file='data.rdata')</td><td rowspan="1" colspan="1">A = np.matrix([[1, 2], [3, 4]])<br /> B = np.matrix([[4, 3], [2, 1]])<br /> <br /> <span style="color: gray"># Data must be of type np.array;</span><br /> <span style="color: gray"># file will have .npz suffix:</span><br /> np.savez('data', A=A, B=B)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1">read variables from file</td><td rowspan="1" colspan="1"><span style="color: gray">% puts A and B in scope:</span><br /> load('data.mdata')<br /> <br /> <span style="color: gray">% puts just A in scope:</span><br /> load('data.mdata', 'A')</td><td rowspan="1" colspan="1"><span style="color: gray"># puts A and B in scope:</span><br /> load('data.rdata')</td><td rowspan="1" colspan="1">data = np.load('data.npz')<br /> A = data['A']<br /> B = data['B']</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1">write all variables in scope to file</td><td rowspan="1" colspan="1">save('data.txt')</td><td rowspan="1" colspan="1">save.image('data.txt')</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="directories" shape="rect" id="directories"></a><a href="numerical-analysis#directories-note" shape="rect">directories</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="working-dir" shape="rect" id="working-dir"></a><a href="numerical-analysis#working-dir-note" shape="rect">working directory</a><br /> <span style="color: gray"><em>get, set</em></span></td><td rowspan="1" colspan="1">pwd<br /> <br /> cd('/tmp')</td><td rowspan="1" colspan="1">getwd()<br /> <br /> setwd("/tmp")</td><td rowspan="1" colspan="1">os.path.abspath('.')<br /> <br /> os.chdir('/tmp')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="build-pathname" shape="rect" id="build-pathname"></a><a href="numerical-analysis#build-pathname-note" shape="rect">build pathname</a></td><td rowspan="1" colspan="1">fullfile('/etc', 'hosts')</td><td rowspan="1" colspan="1">file.path("/etc", "hosts")</td><td rowspan="1" colspan="1">os.path.join('/etc', 'hosts')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="dirname-basename" shape="rect" id="dirname-basename"></a><a href="numerical-analysis#dirname-basename-note" shape="rect">dirname and basename</a></td><td rowspan="1" colspan="1">[dir, base] = fileparts('/etc/hosts')</td><td rowspan="1" colspan="1">dirname("/etc/hosts")<br /> basename("/etc/hosts")</td><td rowspan="1" colspan="1">os.path.dirname('/etc/hosts')<br /> os.path.basename('/etc/hosts')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="absolute-pathname" shape="rect" id="absolute-pathname"></a><a href="numerical-analysis#absolute-pathname-note" shape="rect">absolute pathname</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">normalizePath("..")</td><td rowspan="1" colspan="1">os.path.abspath('..')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="iterate-dir" shape="rect" id="iterate-dir"></a><a href="numerical-analysis#iterate-dir-note" shape="rect">iterate over directory by file</a></td><td rowspan="1" colspan="1"><span style="color: gray">% lists /etc:</span><br /> ls('/etc')<br /> <br /> <span style="color: gray">% lists working directory:</span><br /> ls()</td><td rowspan="1" colspan="1"><span style="color: gray"># list.files() defaults to working directory</span><br /> for (path in list.files('/etc')) {<br /> <span style="white-space: pre-wrap;"> </span>print(path)<br /> }</td><td rowspan="1" colspan="1">for filename in os.listdir('/etc'):<br /> <span style="white-space: pre-wrap;"> </span>print(filename)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="glob-paths" shape="rect" id="glob-paths"></a><a href="numerical-analysis#glob-paths-note" shape="rect">glob paths</a></td><td rowspan="1" colspan="1">glob('/etc/*')</td><td rowspan="1" colspan="1">Sys.glob('/etc/*')</td><td rowspan="1" colspan="1">import glob<br /> <br /> glob.glob('/etc/*')</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="processes-environment" shape="rect" id="processes-environment"></a><a href="numerical-analysis#processes-environment-note" shape="rect">processes and environment</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="cmd-line-arg" shape="rect" id="cmd-line-arg"></a><a href="numerical-analysis#cmd-line-arg-note" shape="rect">command line arguments</a></td><td rowspan="1" colspan="1"><span style="color: gray">% does not include interpreter name:</span><br /> argv()</td><td rowspan="1" colspan="1"><span style="color: gray"># first arg is name of interpreter:</span><br /> commandArgs()<br /> <br /> <span style="color: gray"># arguments after <span style="white-space: pre-wrap;">--</span>args only:</span><br /> commandArgs(TRUE)</td><td rowspan="1" colspan="1">sys.argv</td><td rowspan="1" colspan="1">ARGS</td></tr><tr><td rowspan="1" colspan="1"><a name="env-var" shape="rect" id="env-var"></a><a href="numerical-analysis#env-var-note" shape="rect">environment variable</a><br /> <span style="color: gray"><em>get, set</em></span></td><td rowspan="1" colspan="1">getenv('HOME')<br /> <br /> setenv('PATH', '/bin')</td><td rowspan="1" colspan="1">Sys.getenv("HOME")<br /> <br /> Sys.setenv(PATH="/bin")</td><td rowspan="1" colspan="1">os.getenv('HOME')<br /> <br /> os.environ['PATH'] = '/bin'</td><td rowspan="1" colspan="1">ENV["HOME"]<br /> <br /> ENV["PATH"] = "/bin"</td></tr><tr><td rowspan="1" colspan="1"><a name="exit" shape="rect" id="exit"></a><a href="numerical-analysis#exit-note" shape="rect">exit</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">exit(0)</td><td rowspan="1" colspan="1">quit(save="no", status=0)</td><td rowspan="1" colspan="1">sys.exit(0)</td><td rowspan="1" colspan="1">exit(0)</td></tr><tr><td rowspan="1" colspan="1"><a name="external-cmd" shape="rect" id="external-cmd"></a><a href="numerical-analysis#external-cmd-note" shape="rect">external command</a></td><td rowspan="1" colspan="1">if (shell_cmd('ls -l /tmp'))<br /> <span style="white-space: pre-wrap;"> </span>error('ls failed')<br /> endif</td><td rowspan="1" colspan="1">if (system("ls -l /tmp")) {<br /> <span style="white-space: pre-wrap;"> </span>stop("ls failed")<br /> }</td><td rowspan="1" colspan="1">if os.system('ls -l /tmp'):<br /> <span style="white-space: pre-wrap;"> </span>raise Exception('ls failed')</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="cmd-subst" shape="rect" id="cmd-subst"></a><a href="numerical-analysis#cmd-subst-note" shape="rect">command substitution</a></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">s = readall(‘ls`)</td></tr><tr><th colspan="5" rowspan="1"><a name="libraries-namespaces" shape="rect" id="libraries-namespaces"></a><a href="numerical-analysis#libraries-namespaces-note" shape="rect">libraries and namespaces</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="load-lib" shape="rect" id="load-lib"></a><a href="numerical-analysis#load-lib-note" shape="rect">load library</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>When a function is invoked, MATLAB searches the library path for a file with the same name and a</em> .m <em>suffix. Other functions defined in the file are not visible outside the file.</em></span></td><td rowspan="1" colspan="1"><span style="color: gray"># quoting the name of the package is optional:</span><br /> require("foo")<br /> <span style="color: gray"># or:</span><br /> library("foo")<br /> <br /> <span style="color: gray"># if the package does not exist, require returns false, and library raises an error.</span></td><td rowspan="1" colspan="1">import foo</td><td rowspan="1" colspan="1">include("foo.jl")</td></tr><tr><td rowspan="1" colspan="1"><a name="list-lib" shape="rect" id="list-lib"></a><a href="numerical-analysis#list-lib-note" shape="rect">list loaded libraries</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">search()</td><td rowspan="1" colspan="1">dir()</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="lib-path" shape="rect" id="lib-path"></a><a href="numerical-analysis#lib-path-note" shape="rect">library search path</a></td><td rowspan="1" colspan="1">path()<br /> addath(’~/foo')<br /> rmpath('~/foo')</td><td rowspan="1" colspan="1">.libPaths()</td><td rowspan="1" colspan="1">sys.path</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="source-file" shape="rect" id="source-file"></a><a href="numerical-analysis#source-file-note" shape="rect">source file</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">run('foo.m')</td><td rowspan="1" colspan="1">source("foo.r")</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="install-pkg" shape="rect" id="install-pkg"></a><a href="numerical-analysis#install-pkg-note" shape="rect">install package</a></td><td rowspan="1" colspan="1"><span style="color: gray">% Octave: how to install package<br /> % downloaded from Octave-Forge:</span><br /> pkg install foo-1.0.0.tar.gz</td><td rowspan="1" colspan="1">install.packages("ggplot2")</td><td rowspan="1" colspan="1">$ pip install scipy</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="load-pkg" shape="rect" id="load-pkg"></a><a href="numerical-analysis#load-pkg-note" shape="rect">load package library</a></td><td rowspan="1" colspan="1"><span style="color: gray">% Octave:</span><br /> pkg load foo</td><td rowspan="1" colspan="1">require("foo")<br /> <span style="color: gray"># or:</span><br /> library("foo")</td><td rowspan="1" colspan="1">import foo</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a name="list-pkg" shape="rect" id="list-pkg"></a><a href="numerical-analysis#list-pkg-note" shape="rect">list installed packages</a></td><td rowspan="1" colspan="1"><span style="color: gray">% Octave:</span><br /> pkg list</td><td rowspan="1" colspan="1">library()<br /> installed.packages()</td><td rowspan="1" colspan="1">$ pip freeze</td><td rowspan="1" colspan="1"></td></tr><tr><th colspan="5" rowspan="1"><a name="reflection" shape="rect" id="reflection"></a><a href="numerical-analysis#reflection-note" shape="rect">reflection</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="data-type" shape="rect" id="data-type"></a><a href="numerical-analysis#data-type-note" shape="rect">data type</a></td><td rowspan="1" colspan="1">class(x)</td><td rowspan="1" colspan="1">class(x)<br /> <span style="color: gray"># often more informative:</span><br /> str(x)</td><td rowspan="1" colspan="1">type(x)</td><td rowspan="1" colspan="1">typeof(x)</td></tr><tr><td rowspan="1" colspan="1"><a name="attr" shape="rect" id="attr"></a><a href="numerical-analysis#attr-note" shape="rect">attributes</a></td><td rowspan="1" colspan="1"><span style="color: gray">% if x holds an object:</span><br /> x</td><td rowspan="1" colspan="1">attributes(x)</td><td rowspan="1" colspan="1">[m for m in dir(x)<br /> <span style="white-space: pre-wrap;"> </span>if not callable(getattr(o,m))]</td><td rowspan="1" colspan="1">fieldnames(x)</td></tr><tr><td rowspan="1" colspan="1"><a name="methods" shape="rect" id="methods"></a><a href="numerical-analysis#method-note" shape="rect">methods</a></td><td rowspan="1" colspan="1"><span style="color: gray">% if x holds an object:</span><br /> methods(x)</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none; objects are implemented by functions which dispatch based on type of first arg</em></span></td><td rowspan="1" colspan="1">[m for m in dir(x)<br /> <span style="white-space: pre-wrap;"> </span>if callable(getattr(o,m))]</td><td rowspan="1" colspan="1">methods(x)</td></tr><tr><td rowspan="1" colspan="1"><a name="var-in-scope" shape="rect" id="var-in-scope"></a><a href="numerical-analysis#var-in-scope-note" shape="rect">variables in scope</a></td><td rowspan="1" colspan="1">who()<br /> <br /> <span style="color: gray">% with size and type:</span><br /> whos()</td><td rowspan="1" colspan="1">objects()<br /> ls()<br /> <br /> <span style="color: gray"># with type and description:</span><br /> ls.str()</td><td rowspan="1" colspan="1">dir()</td><td rowspan="1" colspan="1">whos()</td></tr><tr><td rowspan="1" colspan="1"><a name="undef-var" shape="rect" id="undef-var"></a><a href="numerical-analysis#undef-var-note" shape="rect">undefine variable</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">clear('x')</td><td rowspan="1" colspan="1">rm(v)</td><td rowspan="1" colspan="1">del(x)</td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="undef-all-var" shape="rect" id="undef-all-var"></a><a href="numerical-analysis#undef-all-var-note" shape="rect">undefine all variables</a></td><td rowspan="1" colspan="1">clear -a</td><td rowspan="1" colspan="1">rm(list=objects())</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td></tr><tr><td rowspan="1" colspan="1"><a name="eval" shape="rect" id="eval"></a><a href="numerical-analysis#eval-note" shape="rect">eval</a><br /> <span style="white-space: pre-wrap;"> </span></td><td rowspan="1" colspan="1">eval('1 + 1')</td><td rowspan="1" colspan="1">eval(parse(text='1 + 1'))</td><td rowspan="1" colspan="1">eval('1 + 1')</td><td rowspan="1" colspan="1">eval(parse("1 + 1"))</td></tr><tr><td rowspan="1" colspan="1"><a name="func-doc" shape="rect" id="func-doc"></a><a href="numerical-analysis#func-doc-note" shape="rect">function documentation</a></td><td rowspan="1" colspan="1">help tan</td><td rowspan="1" colspan="1">help(tan)<br /> ?tan</td><td rowspan="1" colspan="1">math.tan.<span style="white-space: pre-wrap;">__doc__</span></td><td rowspan="1" colspan="1">?tan</td></tr><tr><td rowspan="1" colspan="1"><a name="ls-lib" shape="rect" id="ls-lib"></a><a href="numerical-analysis#ls-lib-note" shape="rect">list library functions</a></td><td rowspan="1" colspan="1"><span style="color: gray"><em>none</em></span></td><td rowspan="1" colspan="1">ls("package:moments")</td><td rowspan="1" colspan="1">dir(stats)</td><td rowspan="1" colspan="1">whos(Base)</td></tr><tr><td rowspan="1" colspan="1"><a name="grep-doc" shape="rect" id="grep-doc"></a><a href="numerical-analysis#grep-doc-note" shape="rect">search documentation</a></td><td rowspan="1" colspan="1">docsearch tan</td><td rowspan="1" colspan="1">??tan</td><td rowspan="1" colspan="1">$ pydoc -k tan</td><td rowspan="1" colspan="1">apropos("tan")</td></tr><tr><th colspan="5" rowspan="1"><a name="debugging" shape="rect" id="debugging"></a><a href="numerical-analysis#debugging-note" shape="rect">debugging</a></th></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><a href="numerical-analysis#matlab" shape="rect">matlab</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#r" shape="rect">r</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#numpy" shape="rect">numpy</a></th><th rowspan="1" colspan="1"><a href="numerical-analysis#julia" shape="rect">julia</a></th></tr><tr><td rowspan="1" colspan="1"><a name="benchmark-code" shape="rect" id="benchmark-code"></a><a href="numerical-analysis#benchmark-code-note" shape="rect">benchmark code</a></td><td rowspan="1" colspan="1">tic<br /> n = 0<br /> for i = 1:1000*1000<br /> <span style="white-space: pre-wrap;"> </span>n = n + 1;<br /> end<br /> toc</td><td rowspan="1" colspan="1"></td><td rowspan="1" colspan="1">import timeit<br /> <br /> timeit.timeit('i += 1',<br /> <span style="white-space: pre-wrap;"> </span>'i = 0',<br /> <span style="white-space: pre-wrap;"> </span>number=1000000)</td><td rowspan="1" colspan="1"></td></tr><tr><th rowspan="1" colspan="1"></th><th rowspan="1" colspan="1"><span style="color: #efefef"><span style="white-space: pre-wrap;">__________________________________________________</span></span></th><th rowspan="1" colspan="1"><span style="color: #efefef"><span style="white-space: pre-wrap;">__________________________________________________</span></span></th><th rowspan="1" colspan="1"><span style="color: #efefef"><span style="white-space: pre-wrap;">__________________________________________________</span></span></th><th rowspan="1" colspan="1"><span style="color: #efefef"><span style="white-space: pre-wrap;">__________________________________________________</span></span></th></tr></table> <p><a href="numerical-analysis2#tables" shape="rect">tables</a> | <a href="numerical-analysis2#import-export" shape="rect">import and export</a> | <a href="numerical-analysis2#relational-algebra" shape="rect">relational algebra</a> | <a href="numerical-analysis2#aggregation" shape="rect">aggregation</a></p> <p><a href="numerical-analysis2#vectors" shape="rect">vectors</a> | <a href="numerical-analysis2#matrices" shape="rect">matrices</a> | <a href="numerical-analysis2#sparse-matrices" shape="rect">sparse matrices</a> | <a href="numerical-analysis2#optimization" shape="rect">optimization</a> | <a href="numerical-analysis2#polynomials" shape="rect">polynomials</a> | <a href="numerical-analysis2#descriptive-statistics" shape="rect">descriptive statistics</a> | <a href="numerical-analysis2#distributions" shape="rect">distributions</a> | <a href="numerical-analysis2#linear-regression" shape="rect">linear regression</a> | <a href="numerical-analysis2#statistical-tests" shape="rect">statistical tests</a> | <a href="numerical-analysis2#time-series" shape="rect">time series</a> | <a href="numerical-analysis2#fast-fourier-transform" shape="rect">fast fourier transform</a> | <a href="numerical-analysis2#clustering" shape="rect">clustering</a> | <a href="numerical-analysis2#images" shape="rect">images</a> | <a href="numerical-analysis2#sound" shape="rect">sound</a></p> <p><a href="numerical-analysis2#univariate-charts" shape="rect">univariate charts</a> | <a href="numerical-analysis2#bivariate-charts" shape="rect">bivariate charts</a> | <a href="numerical-analysis2#multivariate-charts" shape="rect">multivariate charts</a></p> <p><a name="general-note" shape="rect" id="general-note"></a></p> <h1 id="toc0"><span><a href="numerical-analysis#top" shape="rect">General</a></span></h1> <p><a name="version-used-note" shape="rect" id="version-used-note"></a></p> <h2 id="toc1"><span><a href="numerical-analysis#version-used" shape="rect">version used</a></span></h2> <p>The version of software used to check the examples in the reference sheet.</p> <p><a name="show-version-note" shape="rect" id="show-version-note"></a></p> <h2 id="toc2"><span><a href="numerical-analysis#show-version" shape="rect">show version</a></span></h2> <p>How to determine the version of an installation.</p> <p><a name="implicit-prologue-note" shape="rect" id="implicit-prologue-note"></a></p> <h2 id="toc3"><span><a href="numerical-analysis#implicit-prologue" shape="rect">implicit prologue</a></span></h2> <p>Code which examples in the sheet assume to have already been executed.</p> <p><strong>r:</strong></p> <p>The <tt>ggplot2</tt> library must be installed and loaded to use the plotting functions <tt>qplot</tt> and <tt>ggplot</tt>.</p> <p><a name="grammar-invocation-note" shape="rect" id="grammar-invocation-note"></a></p> <h1 id="toc4"><span><a href="numerical-analysis#grammar-invocation" shape="rect">Grammar and Invocation</a></span></h1> <p><a name="interpreter-note" shape="rect" id="interpreter-note"></a></p> <h2 id="toc5"><span><a href="numerical-analysis#interpreter" shape="rect">interpreter</a></span></h2> <p>How to invoke the interpreter on a script.</p> <p><a name="repl-note" shape="rect" id="repl-note"></a></p> <h2 id="toc6"><span><a href="numerical-analysis#repl" shape="rect">repl</a></span></h2> <p>How to launch a command line read-eval-print loop for the language.</p> <p><strong>r:</strong></p> <p>R installations come with a GUI REPL.</p> <p>The shell <tt>zsh</tt> has a built-in command <tt>r</tt> which re-runs the last command. Shell built-ins take precedence over external commands, but one can invoke the R REPL with:</p> <div class="code"> <pre xml:space="preserve"> <code>$ command r</code> </pre></div> <p><a name="cmd-line-program-note" shape="rect" id="cmd-line-program-note"></a></p> <h2 id="toc7"><span><a href="numerical-analysis#cmd-line-program" shape="rect">command line program</a></span></h2> <p>How to pass the code to be executed to the interpreter as a command line argument.</p> <p><a name="env-var-note" shape="rect" id="env-var-note"></a></p> <h2 id="toc8"><span><a href="numerical-analysis#env-var" shape="rect">environment variables</a></span></h2> <p>How to get and set an environment variable.</p> <p><a name="block-delimiters-note" shape="rect" id="block-delimiters-note"></a></p> <h2 id="toc9"><span><a href="numerical-analysis#block-delimiters" shape="rect">block delimiters</a></span></h2> <p>Punctuation or keywords which define blocks.</p> <p><strong>matlab:</strong></p> <p>The list of keywords which define blocks is not exhaustive. Blocks are also defined by</p> <ul><li><em>switch</em>, <em>case</em>, <em>otherwise</em>, <em>endswitch</em></li><li><em>unwind_protect</em>, <em>unwind_protect_cleanup</em>, <em>end_unwind_protect</em></li><li><em>try</em>, <em>catch</em>, <em>end_try_catch</em></li></ul> <p><a name="stmt-separator-note" shape="rect" id="stmt-separator-note"></a></p> <h2 id="toc10"><span><a href="numerical-analysis#stmt-separator" shape="rect">statement separator</a></span></h2> <p>How statements are separated.</p> <p><strong>matlab:</strong></p> <p>Semicolons are used at the end of lines to suppress output. Output echoes the assignment performed by a statement; if the statement is not an assignment the value of the statement is assigned to the special variable <tt>ans</tt>.</p> <p>In Octave, but not MATLAB, newlines are not separators when preceded by a backslash.</p> <p><a name="eol-comment-note" shape="rect" id="eol-comment-note"></a></p> <h2 id="toc11"><span><a href="numerical-analysis#eol-comment" shape="rect">end-of-line comment</a></span></h2> <p>Character used to start a comment that goes to the end of the line.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, also supports shell-style comments which start with <tt>#</tt>.</p> <p><a name="var-expr-note" shape="rect" id="var-expr-note"></a></p> <h1 id="toc12"><span><a href="numerical-analysis#var-expr" shape="rect">Variables and Expressions</a></span></h1> <p><a name="assignment-note" shape="rect" id="assignment-note"></a></p> <h2 id="toc13"><span><a href="numerical-analysis#assignment" shape="rect">assignment</a></span></h2> <p><strong>r:</strong></p> <p>Traditionally <- was used in R for assignment. Using an = for assignment was introduced in version 1.4.0 sometime before 2002. -> can also be used for assignment:</p> <div class="code"> <pre xml:space="preserve"> <code>3 -> x</code> </pre></div> <p><a name="compound-assignment-note" shape="rect" id="compound-assignment-note"></a></p> <h2 id="toc14"><span><a href="numerical-analysis#compound-assignment" shape="rect">compound assignment</a></span></h2> <p>The compound assignment operators.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, has compound assignment operators for arithmetic and bit operations:</p> <div class="code"> <pre xml:space="preserve"> <code>+= -= *= /= **= ^= &= |=</code> </pre></div> <p>Octave, but not MATLAB, also has the C-stye increment and decrement operators <tt>++</tt> and <tt><span style="white-space: pre-wrap;">--</span></tt>, which can be used in prefix and postfix position.</p> <p><a name="incr-decr-note" shape="rect" id="incr-decr-note"></a></p> <h2 id="toc15"><span><a href="numerical-analysis#incr-decr" shape="rect">increment and decrement operator</a></span></h2> <p>The operator for incrementing the value in a variable; the operator for decrementing the value in a variable.</p> <p><a name="null-note" shape="rect" id="null-note"></a></p> <h2 id="toc16"><span><a href="numerical-analysis#null" shape="rect">null</a></span></h2> <p><strong>matlab:</strong></p> <p><tt>NaN</tt> can be used for missing numerical values. Using a comparison operator on it always returns false, including <tt>NaN == NaN</tt>. Using a logical operator on <tt>NaN</tt> raises an error.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, provides <tt>NA</tt> which is a synonym of <tt>NaN</tt>.</p> <p><strong>r:</strong></p> <p>Relational operators return <tt>NA</tt> when one of the arguments is <tt>NA</tt>. In particular <tt>NA == NA</tt> is <tt>NA</tt>. When acting on values that might be <tt>NA</tt>, the logical operators observe the rules of <a href="http://en.wikipedia.org/wiki/Ternary_logic" shape="rect">ternary logic</a>, treating <tt>NA</tt> is the unknown value.</p> <p><a name="null-test-note" shape="rect" id="null-test-note"></a></p> <h2 id="toc17"><span><a href="numerical-analysis#null-test" shape="rect">null test</a></span></h2> <p>How to test if a value is null.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, has <tt>isna</tt> and <tt>isnull</tt>, which are synonyms of <tt>isnan</tt> and <tt>isempty</tt>.</p> <p><a name="cond-expr-note" shape="rect" id="cond-expr-note"></a></p> <h2 id="toc18"><span><a href="numerical-analysis#cond-expr" shape="rect">conditional expression</a></span></h2> <p>A conditional expression.</p> <p><a name="arithmetic-logic-note" shape="rect" id="arithmetic-logic-note"></a></p> <h1 id="toc19"><span><a href="numerical-analysis#arithmetic-logic" shape="rect">Arithmetic and Logic</a></span></h1> <p><a name="true-false-note" shape="rect" id="true-false-note"></a></p> <h2 id="toc20"><span><a href="numerical-analysis#true-false" shape="rect">true and false</a></span></h2> <p>The boolean literals.</p> <p><strong>matlab:</strong></p> <p><em>true</em> and <em>false</em> are functions which return matrices of ones and zeros of type <em>logical</em>. If no arguments are specified they return single entry matrices. If one argument is provided, a square matrix is returned. If two arguments are provided, they are the row and column dimensions.</p> <p><a name="falsehoods-note" shape="rect" id="falsehoods-note"></a></p> <h2 id="toc21"><span><a href="numerical-analysis#falsehoods" shape="rect">falsehoods</a></span></h2> <p>Values which evaluate to false in a conditional test.</p> <p><strong>matlab:</strong></p> <p>When used in a conditional, matrices evaluate to false unless they are nonempty and all their entries evaluate to true. Because strings are matrices of characters, an empty string ('' or "") will evaluate to false. Most other strings will evaluate to true, but it is possible to create a nonempty string which evaluates to false by inserting a null character; e.g. "false\000".</p> <p><strong>r:</strong></p> <p>When used in a conditional, a vector evaluates to the boolean value of its first entry. Using a vector with more than one entry in a conditional results in a warning message. Using an empty vector in a conditional, <em>c()</em> or <em>NULL</em>, raises an error.</p> <p><a name="logical-op-note" shape="rect" id="logical-op-note"></a></p> <h2 id="toc22"><span><a href="numerical-analysis#logical-op" shape="rect">logical operators</a></span></h2> <p>The boolean operators.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, also uses the exclamation point '!' for negation.</p> <p><a name="relational-op-note" shape="rect" id="relational-op-note"></a></p> <h2 id="toc23"><span><a href="numerical-analysis#relational-op" shape="rect">relational operators</a></span></h2> <p>The relational operators.</p> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, also uses <tt>!=</tt> for an inequality test.</p> <p><a name="arith-op-note" shape="rect" id="arith-op-note"></a></p> <h2 id="toc24"><span><a href="numerical-analysis#arith-op" shape="rect">arithmetic operators</a></span></h2> <p>The arithmetic operators: addition, subtraction, multiplication, division, quotient, remainder.</p> <p><strong>matlab:</strong></p> <p><em>mod</em> is a function and not an infix operator. <em>mod</em> returns a positive value if the first argument is positive, whereas <em>rem</em> returns a negative value.</p> <p><a name="int-div-note" shape="rect" id="int-div-note"></a></p> <h2 id="toc25"><span><a href="numerical-analysis#int-div" shape="rect">integer division</a></span></h2> <p>How to compute the quotient of two integers.</p> <p><a name="int-div-zero-note" shape="rect" id="int-div-zero-note"></a></p> <h2 id="toc26"><span><a href="numerical-analysis#int-div-zero" shape="rect">integer division by zero</a></span></h2> <p>What happens when an integer is divided by zero.</p> <p><a name="float-div-note" shape="rect" id="float-div-note"></a></p> <h2 id="toc27"><span><a href="numerical-analysis#float-div" shape="rect">float division</a></span></h2> <p>How to perform float division, even if the arguments are integers.</p> <p><a name="float-div-zero-note" shape="rect" id="float-div-zero-note"></a></p> <h2 id="toc28"><span><a href="numerical-analysis#float-div-zero" shape="rect">float division by zero</a></span></h2> <p>What happens when a float is divided by zero.</p> <p><a name="power-note" shape="rect" id="power-note"></a></p> <h2 id="toc29"><span><a href="numerical-analysis#power" shape="rect">power</a></span></h2> <p><strong>octave:</strong></p> <p>Octave, but not MATLAB, supports <tt><span style="white-space: pre-wrap;">**</span></tt> as a synonym of <tt>^</tt>.</p> <p><a name="sqrt-note" shape="rect" id="sqrt-note"></a></p> <h2 id="toc30"><span><a href="numerical-analysis#sqrt" shape="rect">sqrt</a></span></h2> <p>The square root function.</p> <p><a name="sqrt-negative-one-note" shape="rect" id="sqrt-negative-one-note"></a></p> <h2 id="toc31"><span><a href="numerical-analysis#sqrt-negative-one" shape="rect">sqrt(-1)</a></span></h2> <p>The result of taking the square root of a negative number.</p> <p><a name="transcendental-func-note" shape="rect" id="transcendental-func-note"></a></p> <h2 id="toc32"><span><a href="numerical-analysis#transcendental-func" shape="rect">transcendental functions</a></span></h2> <p>The standard transcendental functions.</p> <p><a name="transcendental-const-note" shape="rect" id="transcendental-const-note"></a></p> <h2 id="toc33"><span><a href="numerical-analysis#transcendental-const" shape="rect">transcendental constants</a></span></h2> <p>Constants for <em>pi</em> and <em>e</em>.</p> <p><a name="float-truncation-note" shape="rect" id="float-truncation-note"></a></p> <h2 id="toc34"><span><a href="numerical-analysis#float-truncation" shape="rect">float truncation</a></span></h2> <p>Ways of converting a float to a nearby integer.</p> <p><a name="absolute-val-note" shape="rect" id="absolute-val-note"></a></p> <h2 id="toc35"><span><a href="numerical-analysis#absolute-val" shape="rect">absolute value</a></span></h2> <p>The absolute value and signum of a number.</p> <p><a name="int-overflow-note" shape="rect" id="int-overflow-note"></a></p> <h2 id="toc36"><span><a href="numerical-analysis#int-overflow" shape="rect">integer overflow</a></span></h2> <p>What happens when an expression evaluates to an integer which is too big to be represented.</p> <p><a name="float-overflow-note" shape="rect" id="float-overflow-note"></a></p> <h2 id="toc37"><span><a href="numerical-analysis#float-overflow" shape="rect">float overflow</a></span></h2> <p>What happens when an expression evaluates to a float which is too big to be represented.</p> <p><a name="float-limits-note" shape="rect" id="float-limits-note"></a></p> <h2 id="toc38"><span><a href="numerical-analysis#float-limits" shape="rect">float limits</a></span></h2> <p>The <a href="http://en.wikipedia.org/wiki/Machine_epsilon" shape="rect">machine epsilon</a>; the largest representable float and the smallest (i.e. closest to negative infinity) representable float.</p> <p><a name="complex-construction-note" shape="rect" id="complex-construction-note"></a></p> <h2 id="toc39"><span><a href="numerical-analysis#complex-construction" shape="rect">complex construction</a></span></h2> <p>Literals for complex numbers.</p> <p><a name="complex-decomposition-note" shape="rect" id="complex-decomposition-note"></a></p> <h2 id="toc40"><span><a href="numerical-analysis#complex-decomposition" shape="rect">complex decomposition</a></span></h2> <p>How to decompose a complex number into its real and imaginary parts; how to decompose a complex number into its absolute value and argument; how to get the complex conjugate.</p> <p><a name="random-num-note" shape="rect" id="random-num-note"></a></p> <h2 id="toc41"><span><a href="numerical-analysis#random-num" shape="rect">random number</a></span></h2> <p>How to generate a random integer from a uniform distribution; how to generate a random float from a uniform distribution.</p> <p><a name="random-seed-note" shape="rect" id="random-seed-note"></a></p> <h2 id="toc42"><span><a href="numerical-analysis#random-seed" shape="rect">random seed</a></span></h2> <p>How to set, get, and restore the seed used by the random number generator.</p> <p><strong>matlab:</strong></p> <p>At startup the random number generator is seeded using operating system entropy.</p> <p><strong>r:</strong></p> <p>At startup the random number generator is seeded using the current time.</p> <p><strong>numpy:</strong></p> <p>On Unix the random number generator is seeded at startup from /dev/random.</p> <p><a name="bit-op-note" shape="rect" id="bit-op-note"></a></p> <h2 id="toc43"><span><a href="numerical-analysis#bit-op" shape="rect">bit operators</a></span></h2> <p>The bit operators left shift, right shift, and, or , xor, and negation.</p> <p><strong>matlab/octave:</strong></p> <p><tt>bitshift</tt> takes a second argument which is positive for left shift and negative for right shift.</p> <p><tt>bitcmp</tt> takes a second argument which is the size in bits of the integer being operated on. Octave is not compatible with MATLAB in how the integer size is indicated.</p> <p><strong>r:</strong></p> <p>There is a library on CRAN called <tt>bitops</tt> which provides bit operators.</p> <p><a name="strings-note" shape="rect" id="strings-note"></a></p> <h1 id="toc44"><span><a href="numerical-analysis#strings" shape="rect">Strings</a></span></h1> <p><a name="str-literal-note" shape="rect" id="str-literal-note"></a></p> <h2 id="toc45"><span><a href="numerical-analysis#str-literal" shape="rect">literal</a></span></h2> <p>The syntax for a string literal.</p> <p><a name="str-newline-note" shape="rect" id="str-newline-note"></a></p> <h2 id="toc46"><span><a href="numerical-analysis#str-newline" shape="rect">newline in literal</a></span></h2> <p>Can a newline be included in a string literal? Equivalently, can a string literal span more than one line of source code?</p> <p><strong>octave:</strong></p> <p>Double quote strings are Octave specific.</p> <p>A newline can be inserted into a double quote string using the backslash escape <tt>\n</tt>.</p> <p>A double quote string can be continued on the next line by ending the line with a backslash. No newline is inserted into the string.</p> <p><a name="str-esc-note" shape="rect" id="str-esc-note"></a></p> <h2 id="toc47"><span><a href="numerical-analysis#str-esc" shape="rect">literal escapes</a></span></h2> <p>Escape sequences for including special characters in string literals.</p> <p><strong>matlab:</strong></p> <p>C-style backslash escapes are not recognized by string literals, but they are recognized by the IO system; the string 'foo\n' contains 5 characters, but emits 4 characters when written to standard output.</p> <p><a name="str-concat-note" shape="rect" id="str-concat-note"></a></p> <h2 id="toc48"><span><a href="numerical-analysis#str-concat" shape="rect">concatenate</a></span></h2> <p>How to concatenate strings.</p> <p><a name="str-replicate-note" shape="rect" id="str-replicate-note"></a></p> <h2 id="toc49"><span><a href="numerical-analysis#str-replicate" shape="rect">replicate</a></span></h2> <p>How to create a string which consists of a character of substring repeated a fixed number of times.</p> <p><a name="index-substr-note" shape="rect" id="index-substr-note"></a></p> <h2 id="toc50"><span><a href="numerical-analysis#index-substr" shape="rect">index of substring</a></span></h2> <p>How to get the index of first occurrence of a substring.</p> <p><a name="extract-substr-note" shape="rect" id="extract-substr-note"></a></p> <h2 id="toc51"><span><a href="numerical-analysis#extract-substr" shape="rect">extract substring</a></span></h2> <p>How to get the substring at a given index.</p> <p><strong>octave:</strong></p> <p>Octave supports indexing string literals directly: <tt>'hello'(1:4)</tt>.</p> <p><a name="split-note" shape="rect" id="split-note"></a></p> <h2 id="toc52"><span><a href="numerical-analysis#split" shape="rect">split</a></span></h2> <p>How to split a string into an array of substrings. In the original string the substrings must be separated by a character, string, or regex pattern which will not appear in the array of substrings.</p> <p>The split operation can be used to extract the fields from a field delimited record of data.</p> <p><strong>matlab:</strong></p> <p>Cell arrays, which are essentially <a href="numerical-analysis#tuples" shape="rect">tuples</a>, are used to store variable-length strings.</p> <p>A two dimensional array of characters can be used to store strings of the same length, one per row. Regular arrays cannot otherwise be used to store strings.</p> <p><a name="join-note" shape="rect" id="join-note"></a></p> <h2 id="toc53"><span><a href="numerical-analysis#join" shape="rect">join</a></span></h2> <p>How to join an array of substrings into single string. The substrings can be separated by a specified character or string.</p> <p>Joining is the inverse of splitting.</p> <p><a name="trim-note" shape="rect" id="trim-note"></a></p> <h2 id="toc54"><span><a href="numerical-analysis#trim" shape="rect">trim</a></span></h2> <p>How to remove whitespace from the beginning and the end of a string.</p> <p>Trimming is often performed on user provided input.</p> <p><a name="pad-note" shape="rect" id="pad-note"></a></p> <h2 id="toc55"><span><a href="numerical-analysis#pad" shape="rect">pad</a></span></h2> <p>How to pad the edge of a string with spaces so that it is a prescribed length.</p> <p><a name="num-to-str-note" shape="rect" id="num-to-str-note"></a></p> <h2 id="toc56"><span><a href="numerical-analysis#num-to-str" shape="rect">number to string</a></span></h2> <p>How to convert a number to a string.</p> <p><a name="str-to-num-note" shape="rect" id="str-to-num-note"></a></p> <h2 id="toc57"><span><a href="numerical-analysis#str-to-num" shape="rect">string to number</a></span></h2> <p>How to convert a string to number.</p> <p><a name="case-manipulation-note" shape="rect" id="case-manipulation-note"></a></p> <h2 id="toc58"><span><a href="numerical-analysis#case-manipulation" shape="rect">translate case</a></span></h2> <p>How to put a string into all caps. How to put a string into all lower case letters.</p> <p><a name="sprintf-note" shape="rect" id="sprintf-note"></a></p> <h2 id="toc59"><span><a href="numerical-analysis#sprintf" shape="rect">sprintf</a></span></h2> <p>How to create a string using a printf style format.</p> <p><a name="str-len-note" shape="rect" id="str-len-note"></a></p> <h2 id="toc60"><span><a href="numerical-analysis#str-len" shape="rect">length</a></span></h2> <p>How to get the number of characters in a string.</p> <p><a name="char-access-note" shape="rect" id="char-access-note"></a></p> <h2 id="toc61"><span><a href="numerical-analysis#char-access" shape="rect">character access</a></span></h2> <p>How to get the character in a string at a given index.</p> <p><strong>octave:</strong></p> <p>Octave supports indexing string literals directly: <tt>'hello'(1)</tt>.</p> <p><a name="chr-ord-note" shape="rect" id="chr-ord-note"></a></p> <h2 id="toc62"><span><a href="numerical-analysis#chr-ord" shape="rect">chr and ord</a></span></h2> <p>How to convert an ASCII code to a character; how to convert a character to its ASCII code.</p> <p><a name="regexes-note" shape="rect" id="regexes-note"></a></p> <h1 id="toc63"><span><a href="numerical-analysis#regexes" shape="rect">Regular Expressions</a></span></h1> <p><a name="char-class-abbrevs-note" shape="rect" id="char-class-abbrevs-note"></a></p> <h2 id="toc64"><span><a href="numerical-analysis#char-class-abbrevs" shape="rect">character class abbreviations</a></span></h2> <p>The supported character class abbreviations.</p> <p>A character class is a set of one or more characters. In regular expressions, an arbitrary character class can be specified by listing the characters inside square brackets. If the first character is a circumflex <tt>^</tt>, the character class is all characters not in the list. A hyphen <tt>-</tt> can be used to list a range of characters.</p> <p><strong>matlab:</strong></p> <p>The C-style backslash escapes, which can be regarded as character classes which match a single character, are a feature of the regular expression engine and not string literals like in other languages.</p> <p><a name="regex-anchors-note" shape="rect" id="regex-anchors-note"></a></p> <h2 id="toc65"><span><a href="numerical-analysis#regex-anchors" shape="rect">anchors</a></span></h2> <p>The supported anchors.</p> <p>The <tt>\<</tt> and <tt>\></tt> anchors match the start and end of a word respectively.</p> <p><a name="regex-match-note" shape="rect" id="regex-match-note"></a></p> <h2 id="toc66"><span><a href="numerical-analysis#regex-match" shape="rect">match test</a></span></h2> <p>How to test whether a string matches a regular expression.</p> <p><a name="case-insensitive-regex-match-note" shape="rect" id="case-insensitive-regex-match-note"></a></p> <h2 id="toc67"><span><a href="numerical-analysis#case-insensitive-regex-match" shape="rect">case insensitive match test</a></span></h2> <p>How to perform a case insensitive match test.</p> <p><a name="regex-subst-note" shape="rect" id="regex-subst-note"></a></p> <h2 id="toc68"><span><a href="numerical-analysis#regex-subst" shape="rect">substitution</a></span></h2> <p>How to replace all substring which match a pattern with a specified string; how to replace the first substring which matches a pattern with a specified string.</p> <p><a name="regex-backreference-note" shape="rect" id="regex-backreference-note"></a></p> <h2 id="toc69"><span><a href="numerical-analysis#regex-backreference" shape="rect">backreference in match and substitution</a></span></h2> <p>How to use backreferences in a regex; how to use backreferences in the replacement string of substitution.</p> <p><a name="dates-time-note" shape="rect" id="dates-time-note"></a></p> <h1 id="toc70"><span><a href="numerical-analysis#dates-time" shape="rect">Date and Time</a></span></h1> <p><a name="current-date-time-note" shape="rect" id="current-date-time-note"></a></p> <h2 id="toc71"><span><a href="numerical-analysis#current-date-time" shape="rect">current date/time</a></span></h2> <p>How to get the current date and time.</p> <p><strong>r:</strong></p> <p><tt>Sys.time()</tt> returns a value of type <tt>POSIXct</tt>.</p> <p><a name="date-time-type-note" shape="rect" id="date-time-type-note"></a></p> <h2 id="toc72"><span><a href="numerical-analysis#date-time-type" shape="rect">date/time type</a></span></h2> <p>The data type used to hold a combined date and time value.</p> <p><strong>matlab:</strong></p> <p>The Gregorian calendar was introduced in 1582. The Proleptic Gregorian Calendar is sometimes used for earlier dates, but in the Proleptic Gregorian Calendar the year 1 CE is preceded by the year 1 BCE. The MATLAB epoch thus starts at the beginning of the year 1 BCE, but uses a zero to refer to this year.</p> <p><a name="date-time-diff-type-note" shape="rect" id="date-time-diff-type-note"></a></p> <h2 id="toc73"><span><a href="numerical-analysis#date-time-diff-type" shape="rect">date/time difference type</a></span></h2> <p>The data type used to hold the difference between two date/time types.</p> <p><a name="date-parts-note" shape="rect" id="date-parts-note"></a></p> <h2 id="toc74"><span><a href="numerical-analysis#date-parts" shape="rect">get date parts</a></span></h2> <p>How to get the year, the month as an integer from 1 through 12, and the day of the month from a date/time value.</p> <p><strong>octave:</strong></p> <p>In Octave, but not MATLAB, one can use index notation on the return value of a function:</p> <div class="code"> <pre xml:space="preserve"> <code>t = now datevec(t)(1)</code> </pre></div> <p><a name="time-parts-note" shape="rect" id="time-parts-note"></a></p> <h2 id="toc75"><span><a href="numerical-analysis#time-parts" shape="rect">get time parts</a></span></h2> <p>How to get the hour as an integer from 0 through 23, the minute, and the second from a date/time value.</p> <p><a name="build-date-time-note" shape="rect" id="build-date-time-note"></a></p> <h2 id="toc76"><span><a href="numerical-analysis#build-date-time" shape="rect">build date/time from parts</a></span></h2> <p>How to build a date/time value from the year, month, day, hour, minute, and second as integers.</p> <p><a name="date-to-str-note" shape="rect" id="date-to-str-note"></a></p> <h2 id="toc77"><span><a href="numerical-analysis#date-to-str" shape="rect">convert to string</a></span></h2> <p>How to convert a date value to a string using the default format for the locale.</p> <p><a name="strptime-note" shape="rect" id="strptime-note"></a></p> <h2 id="toc78"><span><a href="numerical-analysis#strptime" shape="rect">parse datetime</a></span></h2> <p>How to parse a date/time value from a string in the manner of strptime from the C standard library.</p> <p><a name="strftime-note" shape="rect" id="strftime-note"></a></p> <h2 id="toc79"><span><a href="numerical-analysis#strftime" shape="rect">format datetime</a></span></h2> <p>How to write a date/time value to a string in the manner of strftime from the C standard library.</p> <p><a name="tuples-note" shape="rect" id="tuples-note"></a></p> <h1 id="toc80"><span><a href="numerical-analysis#tuples" shape="rect">Tuples</a></span></h1> <p><a name="tuple-type-note" shape="rect" id="tuple-type-note"></a></p> <h2 id="toc81"><span><a href="numerical-analysis#tuple-type" shape="rect">type</a></span></h2> <p>The name of the data type which implements tuples.</p> <p><a name="tuple-literal-note" shape="rect" id="tuple-literal-note"></a></p> <h2 id="toc82"><span><a href="numerical-analysis#tuple-literal" shape="rect">literal</a></span></h2> <p>How to create a tuple, which we define as a fixed length, inhomogeneous list.</p> <p><a name="tuple-access-note" shape="rect" id="tuple-access-note"></a></p> <h2 id="toc83"><span><a href="numerical-analysis#tuple-access" shape="rect">lookup element</a></span></h2> <p>How to access an element of a tuple.</p> <p><a name="tuple-update-note" shape="rect" id="tuple-update-note"></a></p> <h2 id="toc84"><span><a href="numerical-analysis#tuple-update" shape="rect">update element</a></span></h2> <p>How to change one of a tuple's elements.</p> <p><a name="tuple-len-note" shape="rect" id="tuple-len-note"></a></p> <h2 id="toc85"><span><a href="numerical-analysis#tuple-len" shape="rect">length</a></span></h2> <p>How to get the number of elements in a tuple.</p> <p><a name="arrays-note" shape="rect" id="arrays-note"></a></p> <h1 id="toc86"><span><a href="numerical-analysis#arrays" shape="rect">Arrays</a></span></h1> <p>This section covers one-dimensional arrays which map integers to values.</p> <p><a href="numerical-analysis#multidimensional-arrays" shape="rect">Multidimensional arrays</a> are a generalization which map tuples of integers to values.</p> <p><a href="numerical-analysis#vectors" shape="rect">Vectors</a> and <a href="numerical-analysis#matrices" shape="rect">matrices</a> are one-dimensional and two-dimensional arrays respectively containing numeric values. They support additional operations including the dot product, matrix multiplication, and norms.</p> <p>Here are the data types covered in each section:</p> <table class="wiki-content-table"><tr><th rowspan="1" colspan="1">section</th><th rowspan="1" colspan="1">matlab</th><th rowspan="1" colspan="1">r</th><th rowspan="1" colspan="1">numpy</th><th rowspan="1" colspan="1">julia</th></tr><tr><td rowspan="1" colspan="1"><a href="numerical-analysis#arrays" shape="rect">arrays</a></td><td rowspan="1" colspan="1">matrix (ndims = 1)</td><td rowspan="1" colspan="1">vector</td><td rowspan="1" colspan="1">list</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a href="numerical-analysis#multidimensional-arrays" shape="rect">multidimensional arrays</a></td><td rowspan="1" colspan="1">matrix</td><td rowspan="1" colspan="1">array</td><td rowspan="1" colspan="1">np.array</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a href="numerical-analysis#vectors" shape="rect">vectors</a></td><td rowspan="1" colspan="1">matrix (ndims = 1)</td><td rowspan="1" colspan="1">vector</td><td rowspan="1" colspan="1">np.array (ndim = 1)</td><td rowspan="1" colspan="1"></td></tr><tr><td rowspan="1" colspan="1"><a href="numerical-analysis#matrices" shape="rect">matrices</a></td><td rowspan="1" colspan="1">matrix (ndims = 2)</td><td rowspan="1" colspan="1">matrix</td><td rowspan="1" colspan="1">np.matrix</td><td rowspan="1" colspan="1"></td></tr></table> <p><a name="array-elem-type-note" shape="rect" id="array-elem-type-note"></a></p> <h2 id="toc87"><span><a href="numerical-analysis#array-elem-type" shape="rect">element type</a></span></h2> <p>How to get the type of the elements of an array.</p> <p><a name="permitted-array-elem-types-note" shape="rect" id="permitted-array-elem-types-note"></a></p> <h2 id="toc88"><span><a href="numerical-analysis#permitted-array-elem-types" shape="rect">permitted element types</a></span></h2> <p>Permitted data types for array elements.</p> <p><strong>matlab:</strong></p> <p>Arrays in Octave can only contain numeric elements.</p> <p>Array literals can have a nested structure, but Octave will flatten them. The following literals create the same array:</p> <div class="code"> <pre xml:space="preserve"> <code>[ 1 2 3 [ 4 5 6] ] [ 1 2 3 4 5 6 ]</code> </pre></div> <p>Logical values can be put into an array because <em>true</em> and <em>false</em> are synonyms for 1 and 0. Thus the following literals create the same arrays:</p> <div class="code"> <pre xml:space="preserve"> <code>[ true false false ] [ 1 0 0 ]</code> </pre></div> <p>If a string is encountered in an array literal, the string is treated as an array of ASCII values and it is concatenated with other ASCII values to produce as string. The following literals all create the same string:</p> <div class="code"> <pre xml:space="preserve"> <code>[ 'foo', 98, 97, 114] [ 'foo', 'bar' ] 'foobar'</code> </pre></div> <p>If the other numeric values in an array literal that includes a string are not integer values that fit into a ASCII byte, then they are converted to byte sized values.</p> <p><strong>r:</strong></p> <p>Array literals can have a nested structure, but R will flatten them. The following literals produce the same array of 6 elements:</p> <div class="code"> <pre xml:space="preserve"> <code>c(1,2,3,c(4,5,6)) c(1,2,3,4,5,6)</code> </pre></div> <p>If an array literal contains a mixture of booleans and numbers, then the boolean literals will be converted to 1 (for TRUE and T) and 0 (for FALSE and F).</p> <p>If an array literal contains strings and either booleans or numbers, then the booleans and numbers will be converted to their string representations. For the booleans the string representations are "TRUE'" and "FALSE".</p> <p><a name="array-literal-note" shape="rect" id="array-literal-note"></a></p> <h2 id="toc89"><span><a href="numerical-analysis#array-literal" shape="rect">literal</a></span></h2> <p>The syntax, if any, for an array literal.</p> <p><strong>matlab:</strong></p> <p>The array literal</p> <div class="code"> <pre xml:space="preserve"> <code>[1,'foo',3]</code> </pre></div> <br /> will create an array with 5 elements of class <em>char</em>. <p><strong>r:</strong></p> <p>The array literal</p> <div class="code"> <pre xml:space="preserve"> <code>c(1,'foo',3)</code> </pre></div> <p>will create an array of 3 elements of class <em>character</em>, which is the R string type.</p> <p><a name="array-size-note" shape="rect" id="array-size-note"></a></p> <h2 id="toc90"><span><a href="numerical-analysis#array-size" shape="rect">size</a></span></h2> <p>How to get the number of values in an array.</p> <p><a name="array-empty-test-note" shape="rect" id="array-empty-test-note"></a></p> <h2 id="toc91"><span><a href="numerical-analysis#array-empty-test" shape="rect">empty test</a></span></h2> <p><a name="array-lookup-note" shape="rect" id="array-lookup-note"></a></p> <h2 id="toc92"><span><a href="numerical-analysis#array-lookup" shape="rect">lookup</a></span></h2> <p><a name="array-update-note" shape="rect" id="array-update-note"></a></p> <h2 id="toc93"><span><a href="numerical-analysis#array-update" shape="rect">update</a></span></h2> <p><a name="array-out-of-bounds-note" shape="rect" id="array-out-of-bounds-note"></a></p> <h2 id="toc94"><span><a href="numerical-analysis#array-out-of-bounds" shape="rect">out-of-bounds behavior</a></span></h2> <p><a name="array-element-index-note" shape="rect" id="array-element-index-note"></a></p> <h2 id="toc95"><span><a href="numerical-analysis#array-element-index" shape="rect">index of element</a></span></h2> <p><a name="array-slice-note" shape="rect" id="array-slice-note"></a></p> <h2 id="toc96"><span><a href="numerical-analysis#array-slice" shape="rect">slice</a></span></h2> <p><a name="array-slice-to-end-note" shape="rect" id="array-slice-to-end-note"></a></p> <h2 id="toc97"><span><a href="numerical-analysis#array-slice-to-end" shape="rect">slice to end</a></span></h2> <p><a name="array-concatenation-note" shape="rect" id="array-concatenation-note"></a></p> <h2 id="toc98"><span><a href="numerical-analysis#array-concatenation" shape="rect">concatenate</a></span></h2> <p><a name="array-replication-note" shape="rect" id="array-replication-note"></a></p> <h2 id="toc99"><span><a href="numerical-analysis#array-replication" shape="rect">replicate</a></span></h2> <p><a name="array-copy-note" shape="rect" id="array-copy-note"></a></p> <h2 id="toc100"><span><a href="numerical-analysis#array-copy" shape="rect">copy</a></span></h2> <p>How to make an address copy, a shallow copy, and a deep copy of an array.</p> <p>After an address copy is made, modifications to the copy also modify the original array.</p> <p>After a shallow copy is made, the addition, removal, or replacement of elements in the copy does not modify of the original array. However, if elements in the copy are modified, those elements are also modified in the original array.</p> <p>A deep copy is a recursive copy. The original array is copied and a deep copy is performed on all elements of the array. No change to the contents of the copy will modify the contents of the original array.</p> <p><a name="arithmetic-sequences-note" shape="rect" id="arithmetic-sequences-note"></a></p> <h1 id="toc101"><span><a href="numerical-analysis#arithmetic-sequences" shape="rect">Arithmetic Sequences</a></span></h1> <p>An arithmetic sequence is a sequence of numeric values in which consecutive terms have a constant difference.</p> <p><a name="arith-seq-diff-one-note" shape="rect" id="arith-seq-diff-one-note"></a></p> <h2 id="toc102"><span><a href="numerical-analysis#arith-seq-diff-one" shape="rect">unit difference</a></span></h2> <p>An arithmetic sequence with a difference of 1.</p> <p><a name="arith-seq-diff-ten-note" shape="rect" id="arith-seq-diff-ten-note"></a></p> <h2 id="toc103"><span><a href="numerical-analysis#arith-seq-diff-ten" shape="rect">difference of 10</a></span></h2> <p>An arithmetic sequence with a difference of 10.</p> <p><a name="arith-seq-diff-tenth-note" shape="rect" id="arith-seq-diff-tenth-note"></a></p> <h2 id="toc104"><span><a href="numerical-analysis#arith-seq-diff-tenth" shape="rect">difference of 0.1</a></span></h2> <p>An arithmetic sequence with a difference of 0.1.</p> <p><a name="arith-seq-computed-diff-note" shape="rect" id="arith-seq-computed-diff-note"></a></p> <h2 id="toc105"><span><a href="numerical-analysis#arith-seq-computed-diff" shape="rect">computed difference</a></span></h2> <p>An arithmetic sequence where the difference is computed using the start and end values and the number of elements.</p> <p><a name="iter-over-arith-seq-note" shape="rect" id="iter-over-arith-seq-note"></a></p> <h2 id="toc106"><span><a href="numerical-analysis#iter-over-arith-seq" shape="rect">iterate</a></span></h2> <p>How to iterate over an arithmetic sequence.</p> <p><a name="arith-seq-to-array-note" shape="rect" id="arith-seq-to-array-note"></a></p> <h2 id="toc107"><span><a href="numerical-analysis#arith-seq-to-array" shape="rect">to array</a></span></h2> <p>How to convert an arithmetic sequence to an array.</p> <p><a name="two-d-arrays-note" shape="rect" id="two-d-arrays-note"></a><br /> <a name="three-d-arrays-note" shape="rect" id="three-d-arrays-note"></a></p> <h1 id="toc108"><span><a href="numerical-analysis#two-d-arrays" shape="rect">Multidimensional Arrays</a></span></h1> <p>Multidimensional arrays are a generalization of arrays which map tuples of integers to values. All tuples in the domain of a multidimensional array have the same length; this length is the dimension of the array.</p> <p>The multidimensional arrays described in this sheet are homogeneous, meaning that the values are all of the same type. This restriction allows the implementation to store the values of the multidimensional array in a contiguous region of memory without the use of references or points.</p> <p>Multidimensional arrays should be contrasted with nested arrays. When arrays are nested, the innermost nested arrays contain the values and the other arrays contain references to arrays. The syntax for looking up a value is usually different:</p> <div class="code"> <pre xml:space="preserve"> <code># nested: a[1][2] # multidimensional: a[1, 2]</code> </pre></div> <p><a name="multidimensional-array-elem-type-note" shape="rect" id="multidimensional-array-elem-type-note"></a></p> <h2 id="toc109"><span><a href="numerical-analysis#multidimensional-array-elem-type" shape="rect">element type</a></span></h2> <p>How to get the type of the values stored in a multidimensional array.</p> <p><strong>r:</strong></p> <p><a name="1d-array-literal-note" shape="rect" id="1d-array-literal-note"></a></p> <h2 id="toc110"><span><a href="numerical-analysis#1d-array-literal" shape="rect">literal—1d</a></span></h2> <p><a name="2d-array-literal-note" shape="rect" id="2d-array-literal-note"></a></p> <h2 id="toc111"><span><a href="numerical-analysis#2d-array-literal" shape="rect">literal—2d</a></span></h2> <p><a name="2d-construct-seq-note" shape="rect" id="2d-construct-seq-note"></a></p> <h2 id="toc112"><span><a href="numerical-analysis#2d-construct-seq" shape="rect">construct from sequence—2d</a></span></h2> <p><a name="3d-construct-seq-note" shape="rect" id="3d-construct-seq-note"></a></p> <h2 id="toc113"><span><a href="numerical-analysis#3d-construct-seq" shape="rect">construct from sequence—3d</a></span></h2> <p><a name="2d-construct-nested-seq-note" shape="rect" id="2d-construct-nested-seq-note"></a></p> <h2 id="toc114"><span><a href="numerical-analysis#2d-construct-nested-seq" shape="rect">construct from nested sequences—2d</a></span></h2> <p><a name="3d-construct-nested-seq-note" shape="rect" id="3d-construct-nested-seq-note"></a></p> <h2 id="toc115"><span><a href="numerical-analysis#3d-construct-nested-seq" shape="rect">construct from nested sequences—3d</a></span></h2> <p><a name="2d-construct-rows-note" shape="rect" id="2d-construct-rows-note"></a></p> <h2 id="toc116"><span><a href="numerical-analysis#2d-construct-rows" shape="rect">construct from rows—2d</a></span></h2> <p><a name="2d-construct-columns-note" shape="rect" id="2d-construct-columns-note"></a></p> <h2 id="toc117"><span><a href="numerical-analysis#2d-construct-columns" shape="rect">construct from columns—2d</a></span></h2> <p><a name="2d-construct-subarrays-note" shape="rect" id="2d-construct-subarrays-note"></a></p> <h2 id="toc118"><span><a href="numerical-analysis#2d-construct-subarrays" shape="rect">construct from subarrays—2d</a></span></h2> <p><a name="3d-construct-2d-arrays-note" shape="rect" id="3d-construct-2d-arrays-note"></a></p> <h2 id="toc119"><span><a href="numerical-analysis#3d-construct-2d-arrays" shape="rect">construct 3d array from 2d arrays</a></span></h2> <p><a name="multidimensional-array-size-note" shape="rect" id="multidimensional-array-size-note"></a></p> <h2 id="toc120"><span><a href="numerical-analysis#multidimensional-array-size" shape="rect">size</a></span></h2> <p><a name="1d-lookup-note" shape="rect" id="1d-lookup-note"></a></p> <h2 id="toc121"><span><a href="numerical-analysis#1d-lookup" shape="rect">lookup—1d</a></span></h2> <p><a name="2d-lookup-note" shape="rect" id="2d-lookup-note"></a></p> <h2 id="toc122"><span><a href="numerical-analysis#2d-lookup" shape="rect">lookup—2d</a></span></h2> <p><a name="1d-lookup-2d-array-note" shape="rect" id="1d-lookup-2d-array-note"></a></p> <h2 id="toc123"><span><a href="numerical-analysis#1d-lookup-2d-array" shape="rect">1d lookup on 2d array</a></span></h2> <p><a name="2d-update-note" shape="rect" id="2d-update-note"></a></p> <h2 id="toc124"><span><a href="numerical-analysis#2d-update" shape="rect">update—2d</a></span></h2> <p><a name="multidimensional-array-out-of-bounds-note" shape="rect" id="multidimensional-array-out-of-bounds-note"></a></p> <h2 id="toc125"><span><a href="numerical-analysis#multidimensional-array-out-of-bounds" shape="rect">out-of-bounds behavior</a></span></h2> <p><a name="multidimensional-array-slice-note" shape="rect" id="multidimensional-array-slice-note"></a></p> <h2 id="toc126"><span><a href="numerical-analysis#multidimensional-array-slice" shape="rect">slice</a></span></h2> <p><a name="multidimensional-array-slice-to-end-note" shape="rect" id="multidimensional-array-slice-to-end-note"></a></p> <h2 id="toc127"><span><a href="numerical-analysis#multidimensional-array-slice-to-end" shape="rect">slice to end</a></span></h2> <p><a name="multidimensional-array-slice-subarray-note" shape="rect" id="multidimensional-array-slice-subarray-note"></a></p> <h2 id="toc128"><span><a href="numerical-analysis#multidimensional-array-slice-subarray" shape="rect">slice subarray</a></span></h2> <p><a name="multidimensional-array-transpose-note" shape="rect" id="multidimensional-array-transpose-note"></a></p> <h2 id="toc129"><span><a href="numerical-analysis#multidimensional-array-transpose" shape="rect">transpose</a></span></h2> <p><a name="multidimensional-array-permute-axes-note" shape="rect" id="multidimensional-array-permute-axes-note"></a></p> <h2 id="toc130"><span><a href="numerical-analysis#multidimensional-array-permute-axes" shape="rect">permute axes</a></span></h2> <p><a name="2d-flip-note" shape="rect" id="2d-flip-note"></a></p> <h2 id="toc131"><span><a href="numerical-analysis#2d-flip" shape="rect">flip—2d</a></span></h2> <p><a name="3d-flip-note" shape="rect" id="3d-flip-note"></a></p> <h2 id="toc132"><span><a href="numerical-analysis#3d-flip" shape="rect">flip—3d</a></span></h2> <h2 id="toc133"><span>circular shift—2d</span></h2> <h2 id="toc134"><span>rotate—2d</span></h2> <h2 id="toc135"><span>apply function element-wise</span></h2> <h2 id="toc136"><span>apply function to linear subarrays</span></h2> <p><a name="dictionaries-note" shape="rect" id="dictionaries-note"></a></p> <h1 id="toc137"><span><a href="numerical-analysis#dictionaries" shape="rect">Dictionaries</a></span></h1> <p><a name="dict-literal-note" shape="rect" id="dict-literal-note"></a></p> <h2 id="toc138"><span><a href="numerical-analysis#dict-literal" shape="rect">literal</a></span></h2> <p>The syntax for a dictionary literal.</p> <p><a name="dict-size-note" shape="rect" id="dict-size-note"></a></p> <h2 id="toc139"><span><a href="numerical-analysis#dict-size" shape="rect">size</a></span></h2> <p>How to get the number of keys in a dictionary.</p> <p><a name="dict-lookup-note" shape="rect" id="dict-lookup-note"></a></p> <h2 id="toc140"><span><a href="numerical-analysis#dict-lookup" shape="rect">lookup</a></span></h2> <p>How to use a key to lookup a value in a dictionary.</p> <p><a name="dict-update-note" shape="rect" id="dict-update-note"></a></p> <h2 id="toc141"><span><a href="numerical-analysis#dict-update" shape="rect">update</a></span></h2> <p>How to add or key-value pair or change the value for an existing key.</p> <p><a name="dict-missing-key-note" shape="rect" id="dict-missing-key-note"></a></p> <h2 id="toc142"><span><a href="numerical-analysis#dict-missing-key" shape="rect">missing key behavior</a></span></h2> <p>What happens when looking up a key that isn't in the dictionary.</p> <p><a name="dict-delete-note" shape="rect" id="dict-delete-note"></a></p> <h2 id="toc143"><span><a href="numerical-analysis#dict-delete" shape="rect">delete</a></span></h2> <p>How to delete a key-value pair from a dictionary.</p> <p><a name="dict-iterate-note" shape="rect" id="dict-iterate-note"></a></p> <h2 id="toc144"><span><a href="numerical-analysis#dict-iterate" shape="rect">iterate</a></span></h2> <p>How to iterate over the key-value pairs.</p> <p><a name="dict-keys-values-arrays-note" shape="rect" id="dict-keys-values-arrays-note"></a></p> <h2 id="toc145"><span><a href="numerical-analysis#dict-keys-values-arrays" shape="rect">keys and values as arrays</a></span></h2> <p>How to get an array containing the keys; how to get an array containing the values.</p> <p><a name="dict-merge-note" shape="rect" id="dict-merge-note"></a></p> <h2 id="toc146"><span><a href="numerical-analysis#dict-merge" shape="rect">merge</a></span></h2> <p>How to merge two dictionaries.</p> <p><a name="functions-note" shape="rect" id="functions-note"></a></p> <h1 id="toc147"><span><a href="numerical-analysis#functions" shape="rect">Functions</a></span></h1> <p><a name="def-func-note" shape="rect" id="def-func-note"></a></p> <h2 id="toc148"><span><a href="numerical-analysis#def-func" shape="rect">define function</a></span></h2> <p>How to define a function.</p> <p><a name="invoke-func-note" shape="rect" id="invoke-func-note"></a></p> <h2 id="toc149"><span><a href="numerical-analysis#invoke-func" shape="rect">invoke function</a></span></h2> <p>How to invoke a function.</p> <p><a name="nested-func-note" shape="rect" id="nested-func-note"></a></p> <h2 id="toc150"><span><a href="numerical-analysis#nested-func" shape="rect">nested function</a></span></h2> <p><a name="missing-arg-note" shape="rect" id="missing-arg-note"></a></p> <h2 id="toc151"><span><a href="numerical-analysis#missing-arg" shape="rect">missing argument behavior</a></span></h2> <p>What happens when a function is invoked with too few arguments.</p> <p><a name="extra-arg-note" shape="rect" id="extra-arg-note"></a></p> <h2 id="toc152"><span><a href="numerical-analysis#extra-arg" shape="rect">extra argument behavior</a></span></h2> <p>What happens when a function is invoked with too many arguments.</p> <p><a name="default-arg-note" shape="rect" id="default-arg-note"></a></p> <h2 id="toc153"><span><a href="numerical-analysis#default-arg" shape="rect">default argument</a></span></h2> <p>How to assign a default argument to a parameter.</p> <p><a name="variadic-func-note" shape="rect" id="variadic-func-note"></a></p> <h2 id="toc154"><span><a href="numerical-analysis#variadic-func" shape="rect">variadic function</a></span></h2> <p>How to define a function which accepts a variable number of arguments.</p> <p><a name="retval-note" shape="rect" id="retval-note"></a></p> <h2 id="toc155"><span><a href="numerical-analysis#retval" shape="rect">return value</a></span></h2> <p>How the return value of a function is determined.</p> <p><a name="multiple-retval-note" shape="rect" id="multiple-retval-note"></a></p> <h2 id="toc156"><span><a href="numerical-analysis#multiple-retval" shape="rect">multiple return values</a></span></h2> <p>How to return multiple values from a function.</p> <p><a name="anonymous-func-literal-note" shape="rect" id="anonymous-func-literal-note"></a></p> <h2 id="toc157"><span><a href="numerical-analysis#anonymous-func-literal" shape="rect">anonymous function literal</a></span></h2> <p>The syntax for an anonymous function.</p> <p><a name="invoke-anonymous-func-note" shape="rect" id="invoke-anonymous-func-note"></a></p> <h2 id="toc158"><span><a href="numerical-analysis#invoke-anonymous-func" shape="rect">invoke anonymous function</a></span></h2> <p><a name="closure-note" shape="rect" id="closure-note"></a></p> <h2 id="toc159"><span><a href="numerical-analysis#closure" shape="rect">closure</a></span></h2> <p><a name="func-as-value-note" shape="rect" id="func-as-value-note"></a></p> <h2 id="toc160"><span><a href="numerical-analysis#func-as-value" shape="rect">function as value</a></span></h2> <p>How to store a function in a variable.</p> <p><a name="execution-control-note" shape="rect" id="execution-control-note"></a></p> <h1 id="toc161"><span><a href="numerical-analysis#execution-control" shape="rect">Execution Control</a></span></h1> <p><a name="if-note" shape="rect" id="if-note"></a></p> <h2 id="toc162"><span><a href="numerical-analysis#if" shape="rect">if</a></span></h2> <p>How to write a branch statement.</p> <p><a name="while-note" shape="rect" id="while-note"></a></p> <h2 id="toc163"><span><a href="numerical-analysis#while" shape="rect">while</a></span></h2> <p>How to write a conditional loop.</p> <p><a name="for-note" shape="rect" id="for-note"></a></p> <h2 id="toc164"><span><a href="numerical-analysis#for" shape="rect">for</a></span></h2> <p>How to write a C-style for statement.</p> <p><a name="break-continue-note" shape="rect" id="break-continue-note"></a></p> <h2 id="toc165"><span><a href="numerical-analysis#break-continue" shape="rect">break/continue</a></span></h2> <p>How to break out of a loop. How to jump to the next iteration of a loop.</p> <p><a name="raise-exc-note" shape="rect" id="raise-exc-note"></a></p> <h2 id="toc166"><span><a href="numerical-analysis#raise-exc" shape="rect">raise exception</a></span></h2> <p>How to raise an exception.</p> <p><a name="handle-exc-note" shape="rect" id="handle-exc-note"></a></p> <h2 id="toc167"><span><a href="numerical-analysis#handle-exc" shape="rect">handle exception</a></span></h2> <p>How to handle an exception.</p> <p><a name="file-handle-note" shape="rect" id="file-handle-note"></a></p> <h1 id="toc168"><span><a href="numerical-analysis#file-handle" shape="rect">File Handles</a></span></h1> <p><a name="std-file-handles-note" shape="rect" id="std-file-handles-note"></a></p> <h2 id="toc169"><span><a href="numerical-analysis#std-file-handles" shape="rect">standard file handles</a></span></h2> <p>Standard input, standard output, and standard error.</p> <p><a name="read-line-stdin-note" shape="rect" id="read-line-stdin-note"></a></p> <h2 id="toc170"><span><a href="numerical-analysis#read-line-stdin" shape="rect">read line from stdin</a></span></h2> <p><a name="write-line-stdout-note" shape="rect" id="write-line-stdout-note"></a></p> <h2 id="toc171"><span><a href="numerical-analysis#write-line-stdout" shape="rect">write line to stdout</a></span></h2> <p>How to write a line to stdout.</p> <p><strong>matlab:</strong></p> <p>The backslash escape sequence <tt>\n</tt> is stored as two characters in the string and interpreted as a newline by the IO system.</p> <p><a name="printf-note" shape="rect" id="printf-note"></a></p> <h2 id="toc172"><span><a href="numerical-analysis#printf" shape="rect">write formatted string to stdout</a></span></h2> <p><a name="open-file-note" shape="rect" id="open-file-note"></a></p> <h2 id="toc173"><span><a href="numerical-analysis#open-file" shape="rect">open file for reading</a></span></h2> <p><a name="open-file-write-note" shape="rect" id="open-file-write-note"></a></p> <h2 id="toc174"><span><a href="numerical-analysis#open-file-write" shape="rect">open file for writing</a></span></h2> <p><a name="open-file-append-note" shape="rect" id="open-file-append-note"></a></p> <h2 id="toc175"><span><a href="numerical-analysis#open-file-append" shape="rect">open file for appending</a></span></h2> <p><a name="close-file-note" shape="rect" id="close-file-note"></a></p> <h2 id="toc176"><span><a href="numerical-analysis#close-file" shape="rect">close file</a></span></h2> <p><a name="io-err-note" shape="rect" id="io-err-note"></a></p> <h2 id="toc177"><span><a href="numerical-analysis#io-err" shape="rect">i/o errors</a></span></h2> <p><a name="read-line-note" shape="rect" id="read-line-note"></a></p> <h2 id="toc178"><span><a href="numerical-analysis#read-line" shape="rect">read line</a></span></h2> <p><a name="file-iterate-note" shape="rect" id="file-iterate-note"></a></p> <h2 id="toc179"><span><a href="numerical-analysis#file-iterate" shape="rect">iterate over file by line</a></span></h2> <p><a name="read-file-array-note" shape="rect" id="read-file-array-note"></a></p> <h2 id="toc180"><span><a href="numerical-analysis#read-file-array" shape="rect">read file into array of strings</a></span></h2> <p><a name="write-str-note" shape="rect" id="write-str-note"></a></p> <h2 id="toc181"><span><a href="numerical-analysis#write-str" shape="rect">write string</a></span></h2> <p><a name="write-line-note" shape="rect" id="write-line-note"></a></p> <h2 id="toc182"><span><a href="numerical-analysis#write-line" shape="rect">write line</a></span></h2> <p><a name="flush-note" shape="rect" id="flush-note"></a></p> <h2 id="toc183"><span><a href="numerical-analysis#flush" shape="rect">flush file handle</a></span></h2> <p><a name="seek-note" shape="rect" id="seek-note"></a></p> <h2 id="toc184"><span><a href="numerical-analysis#seek" shape="rect">file handle position</a></span></h2> <p><a name="redirect-stdout-to-file-note" shape="rect" id="redirect-stdout-to-file-note"></a></p> <h2 id="toc185"><span><a href="numerical-analysis#redirect-stdout-to-file" shape="rect">redirect stdout to file</a></span></h2> <p><a name="directories-note" shape="rect" id="directories-note"></a></p> <h1 id="toc186"><span><a href="numerical-analysis#directories" shape="rect">Directories</a></span></h1> <p><a name="working-dir-note" shape="rect" id="working-dir-note"></a></p> <h2 id="toc187"><span><a href="numerical-analysis#working-dir" shape="rect">working directory</a></span></h2> <p>How to get and set the working directory.</p> <p><a name="processes-environment-note" shape="rect" id="processes-environment-note"></a></p> <h1 id="toc188"><span><a href="numerical-analysis#processes-environment" shape="rect">Processes and Environment</a></span></h1> <p><a name="cmd-line-arg-note" shape="rect" id="cmd-line-arg-note"></a></p> <h2 id="toc189"><span><a href="numerical-analysis#cmd-line-arg" shape="rect">command line arguments</a></span></h2> <p>How to get the command line arguments.</p> <p><a name="env-var-note" shape="rect" id="env-var-note"></a></p> <h2 id="toc190"><span><a href="numerical-analysis#env-var" shape="rect">environment variables</a></span></h2> <p>How to get and set and environment variable.</p> <p><a name="libraries-namespaces-note" shape="rect" id="libraries-namespaces-note"></a></p> <h1 id="toc191"><span><a href="numerical-analysis#libraries-namespaces" shape="rect">Libraries and Namespaces</a></span></h1> <p><a name="load-lib-note" shape="rect" id="load-lib-note"></a></p> <h2 id="toc192"><span><a href="numerical-analysis#load-lib" shape="rect">load library</a></span></h2> <p>How to load a library.</p> <p><a name="list-lib-note" shape="rect" id="list-lib-note"></a></p> <h2 id="toc193"><span><a href="numerical-analysis#list-lib" shape="rect">list loaded libraries</a></span></h2> <p>Show the list of libraries which have been loaded.</p> <p><a name="lib-path-note" shape="rect" id="lib-path-note"></a></p> <h2 id="toc194"><span><a href="numerical-analysis#lib-path" shape="rect">library search path</a></span></h2> <p>The list of directories the interpreter will search looking for a library to load.</p> <p><a name="source-file-note" shape="rect" id="source-file-note"></a></p> <h2 id="toc195"><span><a href="numerical-analysis#source-file" shape="rect">source file</a></span></h2> <p>How to source a file.</p> <p><strong>r:</strong></p> <p>When sourcing a file, the suffix if any must be specified, unlike when loading library. Also, a library may contain a shared object, but a sourced file must consist of just R source code.</p> <p><a name="install-pkg-note" shape="rect" id="install-pkg-note"></a></p> <h2 id="toc196"><span><a href="numerical-analysis#install-pkg" shape="rect">install package</a></span></h2> <p>How to install a package.</p> <p><a name="list-pkg-note" shape="rect" id="list-pkg-note"></a></p> <h2 id="toc197"><span><a href="numerical-analysis#list-pkg" shape="rect">list installed packages</a></span></h2> <p>How to list the packages which have been installed.</p> <p><a name="reflection-note" shape="rect" id="reflection-note"></a></p> <h1 id="toc198"><span><a href="numerical-analysis#reflection" shape="rect">Reflection</a></span></h1> <p><a name="data-type-note" shape="rect" id="data-type-note"></a></p> <h2 id="toc199"><span><a href="numerical-analysis#data-type" shape="rect">data type</a></span></h2> <p>How to get the data type of a value.</p> <p><strong>r:</strong></p> <p>For vectors <tt>class</tt> returns the <em>mode</em> of the vector which is the type of data contained in it. The possible modes are</p> <ul><li>numeric</li><li>complex</li><li>logical</li><li>character</li><li>raw</li></ul> <p>Some of the more common class types for non-vector entities are:</p> <ul><li>matrix</li><li>array</li><li>list</li><li>factor</li><li>data.frame</li></ul> <p><a name="attr-note" shape="rect" id="attr-note"></a></p> <h2 id="toc200"><span><a href="numerical-analysis#attr" shape="rect">attributes</a></span></h2> <p>How to get the attributes for an object.</p> <p><strong>r:</strong></p> <p>Arrays and vectors do not have attributes.</p> <p><a name="method-note" shape="rect" id="method-note"></a></p> <h2 id="toc201"><span><a href="numerical-analysis#methods" shape="rect">methods</a></span></h2> <p>How to get the methods for an object.</p> <p><a name="var-in-scope-note" shape="rect" id="var-in-scope-note"></a></p> <h2 id="toc202"><span><a href="numerical-analysis#var-in-scope" shape="rect">variables in scope</a></span></h2> <p>How to list the variables in scope.</p> <p><a name="undef-var-note" shape="rect" id="undef-var-note"></a></p> <h2 id="toc203"><span><a href="numerical-analysis#undef-var" shape="rect">undefine variable</a></span></h2> <p>How to undefine a variable.</p> <p><a name="undef-all-var-note" shape="rect" id="undef-all-var-note"></a></p> <h2 id="toc204"><span><a href="numerical-analysis#undef-all-var" shape="rect">undefine all variables</a></span></h2> <p>How to undefine all variables.</p> <p><a name="eval-note" shape="rect" id="eval-note"></a></p> <h2 id="toc205"><span><a href="numerical-analysis#eval" shape="rect">eval</a></span></h2> <p>How to interpret a string as source code and execute it.</p> <p><a name="func-doc-note" shape="rect" id="func-doc-note"></a></p> <h2 id="toc206"><span><a href="numerical-analysis#func-doc" shape="rect">function documentation</a></span></h2> <p>How to get the documentation for a function.</p> <p><a name="ls-lib-note" shape="rect" id="ls-lib-note"></a></p> <h2 id="toc207"><span><a href="numerical-analysis#ls-lib" shape="rect">list library functions</a></span></h2> <p>How to list the functions and other definitions in a library.</p> <p><a name="grep-doc-note" shape="rect" id="grep-doc-note"></a></p> <h2 id="toc208"><span><a href="numerical-analysis#grep-doc" shape="rect">search documentation</a></span></h2> <p>How to search the documentation by keyword.</p> <p><a name="debugging-note" shape="rect" id="debugging-note"></a></p> <h1 id="toc209"><span><a href="numerical-analysis#debugging" shape="rect">Debugging</a></span></h1> <p><a name="benchmark-code-note" shape="rect" id="benchmark-code-note"></a></p> <h2 id="toc210"><span><a href="numerical-analysis#benchmark-code" shape="rect">benchmark code</a></span></h2> <p>How to benchmark code.</p> <p><a name="matlab" shape="rect" id="matlab"></a></p> <h1 id="toc211"><span><a href="numerical-analysis#top" shape="rect">MATLAB</a></span></h1> <p><a href="http://www.gnu.org/software/octave/doc/interpreter/" shape="rect">Octave Manual</a><br /> <a href="http://www.mathworks.com/help/techdoc/" shape="rect">MATLAB Documentation</a><br /> <a href="http://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB" shape="rect">Differences between Octave and MATLAB</a><br /> <a href="http://octave.sourceforge.net/packages.php" shape="rect">Octave-Forge Packages</a></p> <p>The basic data type of MATLAB is a matrix of floats. There is no distinction between a scalar and a 1x1 matrix, and functions that work on scalars typically work on matrices as well by performing the scalar function on each entry in the matrix and returning the results in a matrix with the same dimensions. Operators such as the logical operators ('&' '|' '!'), relational operators ('==', '!=', '<', '>'), and arithmetic operators ('+', '-') all work this way. However the multiplication '*' and division '/' operators perform matrix multiplication and matrix division, respectively. The <tt>.*</tt> and <tt>./</tt> operators are available if entry-wise multiplication or division is desired.</p> <p>Floats are by default double precision; single precision can be specified with the <em>single</em> constructor. MATLAB has convenient matrix literal notation: commas or spaces can be used to separate row entries, and semicolons or newlines can be used to separate rows.</p> <p>Arrays and vectors are implemented as single-row (<tt>1xn</tt>) matrices. As a result an <em>n</em>-element vector must be transposed before it can be multiplied on the right of a <tt>mxn</tt> matrix.</p> <p>Numeric literals that lack a decimal point such as <em>17</em> and <em>-34</em> create floats, in contrast to most other programming languages. To create an integer, an integer constructor which specifies the size such as <em>int8</em> and <em>uint16</em> must be used. Matrices of integers are supported, but the entries in a given matrix must all have the same numeric type.</p> <p>Strings are implemented as single-row (<tt>1xn</tt>) matrices of characters. Matrices cannot contain strings. If a string is put in matrix literal, each character in the string becomes an entry in the resulting matrix. This is consistent with how matrices are treated if they are nested inside another matrix. The following literals all yield the same string or <tt>1xn</tt> matrix of characters:</p> <div class="code"> <pre xml:space="preserve"> <code>'foo' [ 'f' 'o' 'o' ] [ 'foo' ] [ [ 'f' 'o' 'o' ] ]</code> </pre></div> <p><em>true</em> and <em>false</em> are functions which return matrices of ones and zeros. The ones and zeros have type <em>logical</em> instead of <em>double</em>, which is created by the literals 1 and 0. Other than having a different class, the 0 and 1 of type <em>logical</em> behave the same as the 0 and 1 of type <em>double</em>.</p> <p>MATLAB has a tuple type (in MATLAB terminology, a cell array) which can be used to hold multiple strings. It can also hold values with different types.</p> <p><a name="r" shape="rect" id="r"></a></p> <h1 id="toc212"><span><a href="numerical-analysis#top" shape="rect">R</a></span></h1> <p><a href="http://cran.r-project.org/doc/manuals/R-intro.html" shape="rect">An Introduction to R</a><br /> <a href="http://adv-r.had.co.nz/" shape="rect">Advanced R Programming</a><br /> <a href="http://cran.r-project.org/" shape="rect">The Comprehensive R Archive Network</a></p> <p>The primitive data types of R are vectors of floats, vectors of strings, and vectors of booleans. There is no distinction between a scalar and a vector with one entry in it. Functions and operators which accept a scalar argument will typically accept a vector argument, returning a vector of the same size with the scalar operation performed on each the entries of the original vector.</p> <p>The scalars in a vector must all be of the same type, but R also provides a <em>list</em> data type which can be used as a tuple (entries accessed by index), record (entries accessed by name), or even as a dictionary.</p> <p>In addition R provides a <em>data frame</em> type which is a list (in R terminology) of vectors all of the same length. Data frames are equivalent to the data sets of other statistical analysis packages.</p> <p><a name="numpy" shape="rect" id="numpy"></a></p> <h1 id="toc213"><span><a href="numerical-analysis#top" shape="rect">NumPy</a></span></h1> <p><a href="http://docs.scipy.org/doc/" shape="rect">NumPy and SciPy Documentation</a><br /> <a href="http://matplotlib.sourceforge.net/" shape="rect">matplotlib intro</a><br /> <a href="http://www.scipy.org/NumPy_for_Matlab_Users" shape="rect">NumPy for Matlab Users</a><br /> <a href="http://pandas.pydata.org/pandas-docs/stable/" shape="rect">Pandas Documentation</a><br /> <a href="http://pandas.pydata.org/pandas-docs/dev/genindex.html" shape="rect">Pandas Method/Attribute Index</a></p> <p>NumPy is a Python library which provides a data type called <tt>array</tt>. It differs from the Python <tt>list</tt> data type in the following ways:</p> <ul><li>N-dimensional. Although the <tt>list</tt> type can be nested to hold higher dimension data, the <tt>array</tt> can hold higher dimension data in a space efficient manner without using indirection.</li><li>homogeneous. The elements of an <tt>array</tt> are restricted to be of a specified type. The NumPy library introduces new primitive types not available in vanilla Python. However, the element type of an array can be <tt>object</tt> which permits storing anything in the array.</li></ul> <p>In the reference sheet the <a href="numerical-analysis#array" shape="rect">array section</a> covers the vanilla Python <tt>list</tt> and the <a href="numerical-analysis#multidimensional-array" shape="rect">multidimensional array section</a> covers the NumPy <tt>array</tt>.</p> <p><em>List the NumPy primitive types</em></p> <p>SciPy, Matplotlib, and Pandas are libraries which depend on Numpy.</p> <p><a name="julia" shape="rect" id="julia"></a></p> <h1 id="toc214"><span><a href="numerical-analysis#top" shape="rect">Julia</a></span></h1> <p><a href="http://julialang.org/" shape="rect">http://julialang.org/</a></p> </div> </div> </div> <div id="license-area" class="license-area"> <a href="https://github.com/clarkgrubb/hyperpolyglot/issues">issue tracker</a> | content of this page licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"> creative commons attribution-sharealike 3.0</a> <br> </div> </div> </div> </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-17129977-2']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html>