lomath
Version:
Lomath is a tensorial math library extended from lodash.
1,150 lines (1,095 loc) • 124 kB
HTML
<!DOCTYPE html>
<html lang="en" id="docs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>lomath.js</title>
<link rel="stylesheet" href="styles.css" media="all">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/default.min.css">
</head>
<body>
<div class="toc-container">
<h2><code><a href="http://kengz.github.io/lomath/">lomath.js</a></code></h2>
<ul>
<li><a href="#">Introduction</a></li>
<li><a href="https://github.com/kengz/lomath">Github Repository</a></li>
<li><a href="tests.html">Test suite</a></li>
<li><a href="annotated/index.html">Annotated Source</a></li>
</ul>
<div>
<h2 id="-basics-"><code>basics</code></h2>
<ul>
<li><a href="#add"><code>add</code></a></li>
<li><a href="#c"><code>c</code></a></li>
<li><a href="#divide"><code>divide</code></a></li>
<li><a href="#fsum"><code>fsum</code></a></li>
<li><a href="#log"><code>log</code></a></li>
<li><a href="#logistic"><code>logistic</code></a></li>
<li><a href="#multiply"><code>multiply</code></a></li>
<li><a href="#prod"><code>prod</code></a></li>
<li><a href="#root"><code>root</code></a></li>
<li><a href="#square"><code>square</code></a></li>
<li><a href="#subtract"><code>subtract</code></a></li>
<li><a href="#sum"><code>sum</code></a></li>
</ul>
</div>
<div>
<h2 id="-combinatorics-"><code>combinatorics</code></h2>
<ul>
<li><a href="#combList"><code>combList</code></a></li>
<li><a href="#combination"><code>combination</code></a></li>
<li><a href="#factorial"><code>factorial</code></a></li>
<li><a href="#genAry"><code>genAry</code></a></li>
<li><a href="#pSubset"><code>pSubset</code></a></li>
<li><a href="#permList"><code>permList</code></a></li>
<li><a href="#permutation"><code>permutation</code></a></li>
<li><a href="#permute"><code>permute</code></a></li>
<li><a href="#subset"><code>subset</code></a></li>
<li><a href="#toNumArr"><code>toNumArr</code></a></li>
</ul>
</div>
<div>
<h2 id="-composition-"><code>composition</code></h2>
<ul>
<li><a href="#asso"><code>asso</code></a></li>
<li><a href="#assodist"><code>assodist</code></a></li>
<li><a href="#distribute"><code>distribute</code></a></li>
<li><a href="#distributeBoth"><code>distributeBoth</code></a></li>
<li><a href="#distributeLeft"><code>distributeLeft</code></a></li>
<li><a href="#distributeRight"><code>distributeRight</code></a></li>
<li><a href="#distributeSingle"><code>distributeSingle</code></a></li>
<li><a href="#op"><code>op</code></a></li>
</ul>
</div>
<div>
<h2 id="-initialization-"><code>initialization</code></h2>
<ul>
<li><a href="#numeric"><code>numeric</code></a></li>
<li><a href="#seq"><code>seq</code></a></li>
</ul>
</div>
<div>
<h2 id="-matrices-"><code>matrices</code></h2>
<ul>
<li><a href="#adj"><code>adj</code></a></li>
<li><a href="#coMatrix"><code>coMatrix</code></a></li>
<li><a href="#coSubMatrix"><code>coSubMatrix</code></a></li>
<li><a href="#det"><code>det</code></a></li>
<li><a href="#detSum"><code>detSum</code></a></li>
<li><a href="#inv"><code>inv</code></a></li>
<li><a href="#matMultiply"><code>matMultiply</code></a></li>
<li><a href="#trace"><code>trace</code></a></li>
<li><a href="#transpose"><code>transpose</code></a></li>
</ul>
</div>
<div>
<h2 id="-native-math-"><code>native-Math</code></h2>
<ul>
<li><a href="#abs"><code>abs</code></a></li>
<li><a href="#acos"><code>acos</code></a></li>
<li><a href="#acosh"><code>acosh</code></a></li>
<li><a href="#asin"><code>asin</code></a></li>
<li><a href="#asinh"><code>asinh</code></a></li>
<li><a href="#atan"><code>atan</code></a></li>
<li><a href="#atanh"><code>atanh</code></a></li>
<li><a href="#ceil"><code>ceil</code></a></li>
<li><a href="#cos"><code>cos</code></a></li>
<li><a href="#cosh"><code>cosh</code></a></li>
<li><a href="#exp"><code>exp</code></a></li>
<li><a href="#floor"><code>floor</code></a></li>
<li><a href="#log10"><code>log10</code></a></li>
<li><a href="#log1p"><code>log1p</code></a></li>
<li><a href="#log2"><code>log2</code></a></li>
<li><a href="#pow"><code>pow</code></a></li>
<li><a href="#round"><code>round</code></a></li>
<li><a href="#sign"><code>sign</code></a></li>
<li><a href="#sin"><code>sin</code></a></li>
<li><a href="#sinh"><code>sinh</code></a></li>
<li><a href="#sqrt"><code>sqrt</code></a></li>
<li><a href="#tan"><code>tan</code></a></li>
<li><a href="#tanh"><code>tanh</code></a></li>
<li><a href="#trunc"><code>trunc</code></a></li>
</ul>
</div>
<div>
<h2 id="-plotting-"><code>plotting</code></h2>
<ul>
<li><a href="#advPlot"><code>advPlot</code></a></li>
<li><a href="#hc"><code>hc</code></a></li>
<li><a href="#plot"><code>plot</code></a></li>
<li><a href="#render"><code>render</code></a></li>
</ul>
</div>
<div>
<h2 id="-properties-"><code>properties</code></h2>
<ul>
<li><a href="#depth"><code>depth</code></a></li>
<li><a href="#dim"><code>dim</code></a></li>
<li><a href="#isFlat"><code>isFlat</code></a></li>
<li><a href="#maxDeepestLength"><code>maxDeepestLength</code></a></li>
<li><a href="#volume"><code>volume</code></a></li>
</ul>
</div>
<div>
<h2 id="-regexp-"><code>regexp</code></h2>
<ul>
<li><a href="#reAnd"><code>reAnd</code></a></li>
<li><a href="#reAndMatch"><code>reAndMatch</code></a></li>
<li><a href="#reGet"><code>reGet</code></a></li>
<li><a href="#reMatch"><code>reMatch</code></a></li>
<li><a href="#reNotMatch"><code>reNotMatch</code></a></li>
<li><a href="#reOr"><code>reOr</code></a></li>
<li><a href="#reOrMatch"><code>reOrMatch</code></a></li>
<li><a href="#reString"><code>reString</code></a></li>
<li><a href="#reWrap"><code>reWrap</code></a></li>
</ul>
</div>
<div>
<h2 id="-signature-"><code>signature</code></h2>
<ul>
<li><a href="#deepEqual"><code>deepEqual</code></a></li>
<li><a href="#inRange"><code>inRange</code></a></li>
<li><a href="#isDouble"><code>isDouble</code></a></li>
<li><a href="#isInteger"><code>isInteger</code></a></li>
<li><a href="#isNegative"><code>isNegative</code></a></li>
<li><a href="#isPositive"><code>isPositive</code></a></li>
<li><a href="#isZero"><code>isZero</code></a></li>
<li><a href="#nonNegative"><code>nonNegative</code></a></li>
<li><a href="#nonPositive"><code>nonPositive</code></a></li>
<li><a href="#nonZero"><code>nonZero</code></a></li>
<li><a href="#parity"><code>parity</code></a></li>
<li><a href="#sameSig"><code>sameSig</code></a></li>
</ul>
</div>
<div>
<h2 id="-statistics-"><code>statistics</code></h2>
<ul>
<li><a href="#expGRate"><code>expGRate</code></a></li>
<li><a href="#expVal"><code>expVal</code></a></li>
<li><a href="#mean"><code>mean</code></a></li>
<li><a href="#stdev"><code>stdev</code></a></li>
<li><a href="#trailExpGRate"><code>trailExpGRate</code></a></li>
<li><a href="#variance"><code>variance</code></a></li>
</ul>
</div>
<div>
<h2 id="-timing-"><code>timing</code></h2>
<ul>
<li><a href="#p"><code>p</code></a></li>
<li><a href="#tick"><code>tick</code></a></li>
<li><a href="#tock"><code>tock</code></a></li>
</ul>
</div>
<div>
<h2 id="-transformation-"><code>transformation</code></h2>
<ul>
<li><a href="#batchIndexOf"><code>batchIndexOf</code></a></li>
<li><a href="#cbind"><code>cbind</code></a></li>
<li><a href="#cbindByField"><code>cbindByField</code></a></li>
<li><a href="#extend"><code>extend</code></a></li>
<li><a href="#flattenJSON"><code>flattenJSON</code></a></li>
<li><a href="#rbind"><code>rbind</code></a></li>
<li><a href="#rectangularize"><code>rectangularize</code></a></li>
<li><a href="#reshape"><code>reshape</code></a></li>
<li><a href="#reverse"><code>reverse</code></a></li>
<li><a href="#swap"><code>swap</code></a></li>
<li><a href="#unflattenJSON"><code>unflattenJSON</code></a></li>
<li><a href="#validInds"><code>validInds</code></a></li>
</ul>
</div>
<div>
<h2 id="-trend-"><code>trend</code></h2>
<ul>
<li><a href="#decreasing"><code>decreasing</code></a></li>
<li><a href="#increasing"><code>increasing</code></a></li>
<li><a href="#nonDecreasing"><code>nonDecreasing</code></a></li>
<li><a href="#nonIncreasing"><code>nonIncreasing</code></a></li>
<li><a href="#stairs"><code>stairs</code></a></li>
<li><a href="#stairsTrend"><code>stairsTrend</code></a></li>
</ul>
</div>
<div>
<h2 id="-vector-"><code>vector</code></h2>
<ul>
<li><a href="#dot"><code>dot</code></a></li>
<li><a href="#norm"><code>norm</code></a></li>
<li><a href="#normalize"><code>normalize</code></a></li>
<li><a href="#powSum"><code>powSum</code></a></li>
<li><a href="#rescale"><code>rescale</code></a></li>
</ul>
</div>
<div>
<h2 id="-methods-"><code>Methods</code></h2>
<ul>
<li><a href="#histogram"><code>histogram</code></a></li>
</ul>
</div>
<div>
<h2 id="-properties-"><code>Properties</code></h2>
</div>
</div>
<div class="doc-container">
<div>
<p><a href="http://badge.fury.io/js/lomath"><img src="https://badge.fury.io/js/lomath.svg" alt="npm version"></a> <a href="https://travis-ci.org/kengz/lomath"><img src="https://travis-ci.org/kengz/lomath.svg?branch=master" alt="Build Status"></a> <a href="https://coveralls.io/r/kengz/lomath?branch=master"><img src="https://coveralls.io/repos/kengz/lomath/badge.svg?branch=master" alt="Coverage Status"></a> <a href="https://gemnasium.com/kengz/lomath"><img src="https://gemnasium.com/kengz/lomath.svg" alt="Dependency Status"></a></p>
<p><a href="https://github.com/kengz/lomath"><strong>Lomath</strong></a> is a tensorial math library extended from <a href="https://lodash.com"><code>lodash</code></a>, with performant math functions applicable to tensors(multi-arrays). It also has a standalone plotting module that using <code>HighCharts</code> and <code>BrowserSync</code>.</p>
<p>See the <a href="http://kengz.github.io/lomath/">API documentation</a>. For the included functions, see the <a href="https://lodash.com/docs">lodash API documentation</a>.</p>
<h2 id="community">Community</h2>
<p><a href="https://gitter.im/kengz/lomath?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://badges.gitter.im/Join%20Chat.svg" alt="Join the chat at https://gitter.im/kengz/lomath"></a></p>
<h2 id="installation">Installation</h2>
<p><strong>Installation</strong>:</p>
<pre><code>npm install lomath
</code></pre><p>To use the plotting module, do:</p>
<pre><code>// in the terminal at your project's root, do:
cd node_modules/lomath
npm install
</code></pre><h2 id="usage">Usage</h2>
<p>See the <a href="http://kengz.github.io/lomath/">API documentation</a> and <a href="https://lodash.com/docs">lodash API documentation</a> for example usage.</p>
<p><strong>Lomath</strong> comes with the latest version of <a href="https://lodash.com"><code>lodash</code></a>, and more. Use it as you would normally use <code>lodash</code> or <code>underscore</code>, and it has many math functions familiar to users of the <code>R</code> language.</p>
<p>For clearer terminology, we call <code>tensors</code> the generic data structures:</p>
<table>
<thead>
<tr>
<th style="text-align:left">data structure</th>
<th style="text-align:left">terminology</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>0</code></td>
<td style="text-align:left">scalar = rank-0 tensor</td>
</tr>
<tr>
<td style="text-align:left"><code>[1, 2, 3]</code></td>
<td style="text-align:left">vector = rank-1 tensor</td>
</tr>
<tr>
<td style="text-align:left"><code>[[1, 2], [3, 4]]</code></td>
<td style="text-align:left">matrix = rank-2 tensor</td>
</tr>
<tr>
<td style="text-align:left">...and so on</td>
<td style="text-align:left">rank-n tensor</td>
</tr>
</tbody>
</table>
<p>You can also extend <code>lomath</code> and define your own function that applies to tensors, using the function composition module such as <code>_.distribute, _.asso</code>, etc.</p>
<h2 id="plotting-module">Plotting Module</h2>
<p><img src="./demo.gif" alt="Sample plot" width="100%" style="display:inline-block"></p>
<p><code>lomath</code> comes with a standalone plotting module that using <code>HighCharts</code> and <code>BrowserSync</code>. Just run your JS file normally when you plot (example below), and it will automatically pull up a browser showing you the charts; you can save them!</p>
<p><strong>Demo</strong>: see <code>demo/demo.js</code> for magic.</p>
<pre><code>var _ = require('lomath');
// use lodash as usual
var v = _.range(10);
// lomath: generalized math functions applicable to multi-array
var vv = _.square(v);
console.log(v);
console.log(vv);
// prints all the functions in lomath
// console.log(_.functions(_));
// data visualization: highcharts plotter
// call contructor of highcharts plotter. Note the ()
var hc = _.hc();
// first, list all you wish to plot.
hc.plot(
[{
name: "linear",
data: v
}, {
name: "square",
data: vv
}],
"Title 1"
)
hc.plot(
[{
name: "log",
data: _.log(v)
}],
"Title 2"
)
//optionally you can use the original HighCharts plot options object by:
hc.advPlot(HighChartsOptions);
// Magic here! Finally, the command to render all the plots above.
// Pulls up a browser (default to chrome for better support) with the charts.
// calling hc.render(true) will autosave all plots to your downloads folder.
hc.render();
</code></pre><h2 id="roadmap">Roadmap</h2>
<ul>
<li>add timer tick tock</li>
<li>Multivar methods for ML: special dot, matrix mult, sum of expression using index, trace, det, inverse, logistic fn.</li>
<li>add aliases</li>
<li>add Randomjs engine</li>
<li>higher dimensional math</li>
<li>JSON object methods</li>
<li>more stats methods: sample, multidim probs, covarience, distributions</li>
<li>performance benchmark</li>
</ul>
<h2>API documentation</h2>
<div>
<h2 id="-basics-methods-"><code>“basics” Methods</code></h2>
<div>
<h3 id="-a-id-add-a-add-x-"><a id="add"></a><code>add([...X])</code></h3>
<p><a href="#add">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L355" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Adds tensors using <code>_.assodist</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.add(1, 2, 3)
// → 6
_.add(1, [1, 2, 3])
// → [2, 3, 4]
_.add(1, [[1, 2], [3, 4]])
// → [[2, 3], [4, 5]]
_.add([10, 20], [[1, 2], [3, 4]])
// → [[11, 12], [23, 24]]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-c-a-c-x-"><a id="c"></a><code>c([...X])</code></h3>
<p><a href="#c">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L245" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Concatenates all arguments into single vector by <code>_.flattenDeep</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(vector)</em>: A vector with the scalars from all tensors.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.c('a', 'b', 'c')
// → ['a', 'b', 'c']
_.c(1, ['a', 'b', 'c'], 2)
// → [1, 'a', 'b', 'c', 2]
_.c([[1, 2], [3, 4])
// → [1, 2, 3, 4]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-divide-a-divide-x-"><a id="divide"></a><code>divide([...X])</code></h3>
<p><a href="#divide">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L436" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Divides tensors using <code>_.assodist</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.divide(3, 2, 1)
// → 1.5
_.divide([1, 2, 3], 2)
// → [0.5, 1, 1.5]
_.divide([[1, 2], [3, 4]], 2)
// → [[0.5, 1], [1.5, 2]]
_.divide([[1, 2], [3, 4]], [1, 2])
// → [[1, 2], [1.5, 2]]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-fsum-a-fsum-t-fn-"><a id="fsum"></a><code>fsum(T, fn)</code></h3>
<p><a href="#fsum">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L293" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Functional sum, Basically Sigma_i fn(T[i]) with fn(T, i), where T is a tensor, i is first level index.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>T</code> <em>(tensor)</em>: A tensor.</li>
<li><code>fn</code> <em>(function)</em>: A function fn(T, i) applied to the i-th term of T for the sum. Note the function can access the whole T for any term i for greater generality.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(scalar)</em>: A scalar summed from all the terms from the mapped T fn(T, i).</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">// sum of the elements multiplied by indices in a sequence, i.e. Sigma_i i*(x_i)
_.fsum([1,1,1], function(T, i){
return T[i] * i;
})
// → 0+1+2
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-log-a-log-t-n-e-"><a id="log"></a><code>log(T, [n=e])</code></h3>
<p><a href="#log">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L455" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Takes the log of tensor T to base n (defaulted to e) element-wise using <code>_.distribute</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>T</code> <em>(tensor)</em>: A tensor.</li>
<li><code>[n=e]</code> <em>(number)</em>: The optional base; defaulted to e.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.log([1, Math.E])
// → [0, 1]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-logistic-a-logistic-t-"><a id="logistic"></a><code>logistic(T)</code></h3>
<p><a href="#logistic">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L516" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Applies the logistic (sigmoid) function to tensor T element-wise.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>T</code> <em>(tensor)</em>: A tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.logistic([-10, 0, 10])
// → [ 0.00004539786870243441, 0.5, 0.9999546021312976 ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-multiply-a-multiply-x-"><a id="multiply"></a><code>multiply([...X])</code></h3>
<p><a href="#multiply">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L409" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Multiplies tensors using <code>_.assodist</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.multiply(1, 2, 3)
// → 6
_.multiply(1, [1, 2, 3])
// → [1, 2, 3]
_.multiply(1, [[1, 2], [3, 4]])
// → [[1, 2], [3, 4]]
_.multiply([10, 20], [[1, 2], [3, 4]])
// → [[10, 20], [60, 80]]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-prod-a-prod-x-"><a id="prod"></a><code>prod([...X])</code></h3>
<p><a href="#prod">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L324" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Multiplies together all scalars in all argument tensors.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(scalar)</em>: A product scalar from all scalars in the tensors.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.prod(1, 2, 3)
// → 6
_.prod([1, 2, 3])
// → 6
_.prod(1, [1, 2, 3], [[1, 2], [3, 4]])
// → 144
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-root-a-root-t-n-2-"><a id="root"></a><code>root(T, [n=2])</code></h3>
<p><a href="#root">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L498" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Takes the n-th root (defaulted to 2) of tensor T element-wise using <code>_.distribute</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>T</code> <em>(tensor)</em>: A tensor.</li>
<li><code>[n=2]</code> <em>(number)</em>: The optional base; defaulted to <code>2</code> for squareroot.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.root([1, 4])
// → [1, 2]
_.root([-1, -8], 3)
// → [-1, -2]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-square-a-square-t-"><a id="square"></a><code>square(T)</code></h3>
<p><a href="#square">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L473" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Squares a tensor element-wise using <code>_.distributeSingle</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>T</code> <em>(tensor)</em>: A tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.square([1, 2])
// → [1, 4]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-subtract-a-subtract-x-"><a id="subtract"></a><code>subtract([...X])</code></h3>
<p><a href="#subtract">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L382" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Subtracts tensors using <code>_.assodist</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.subtract(1, 2, 3)
// → -5
_.subtract(1, [1, 2, 3])
// → [0, -1, -2]
_.subtract(1, [[1, 2], [3, 4]])
// → [[0, -1], [-2, -3]]
_.subtract([10, 20], [[1, 2], [3, 4]])
// → [[9, 8], [17, 16]]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-sum-a-sum-x-"><a id="sum"></a><code>sum([...X])</code></h3>
<p><a href="#sum">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L270" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Sums all scalars in all argument tensors.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(scalar)</em>: A scalar summed from all scalars in the tensors.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.sum('a', 'b', 'c')
// → 'abc'
_.sum(0, [1, 2, 3], [[1, 2], [3, 4])
// → 16
</code></pre>
<hr>
</div>
</div>
<div>
<h2 id="-combinatorics-methods-"><code>“combinatorics” Methods</code></h2>
<div>
<h3 id="-a-id-comblist-a-comblist-n-r-"><a id="combList"></a><code>combList(n, r)</code></h3>
<p><a href="#combList">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2042" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates the indices of n-choose-r. Calls <code>_.subset</code> internally, chooses the array with string length r, and converts to numbers.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The number of items to choose.</li>
<li><code>r</code> <em>(number)</em>: The number of items chosen.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of index arrays specifying the subset indices.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.combList(3, 2)
// → [ [ 0, 1 ], [ 0, 2 ], [ 1, 2 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-combination-a-combination-n-r-"><a id="combination"></a><code>combination(n, r)</code></h3>
<p><a href="#combination">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2146" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns n-choose-r.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The integer.</li>
<li><code>r</code> <em>(number)</em>: The integer.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: nCr</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.combination(1000, 1)
// → 1000
_.combination(1000, 1000) // No integer overflow; uses symmetry.
// → 1
_.combination(1000, 500) // Inevitable overflow.
// → NaN
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-factorial-a-factorial-n-"><a id="factorial"></a><code>factorial(n)</code></h3>
<p><a href="#factorial">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2092" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns n!.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The integer.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: n!</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.factorial(5)
// → 120
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-genary-a-genary-length-n-"><a id="genAry"></a><code>genAry(length, N)</code></h3>
<p><a href="#genAry">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1915" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates all the strings of N-nary numbers up to length.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>length</code> <em>(number)</em>: The length of the N-nary numbers.</li>
<li><code>N</code> <em>(number)</em>: The number base.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of strings of the N-nary numbers.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.genAry(3, 2) // binary, length 3
// → ['000', '001', '010', '011', '100', '101', '110', '111']
_.genAry(2, 3) // ternary, length 2
// → ['00', '01', '02', '10', '11', '12', '20', '21', '22']
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-psubset-a-psubset-n-"><a id="pSubset"></a><code>pSubset(n)</code></h3>
<p><a href="#pSubset">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1962" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates all the permutation subset indices of n items.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The number of items to permute.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of strings of length n, specifying the permutation indices.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.pSubset(3)
// → [
// ['0', '1', '2'],
// ['01', '02', '10', '12', '20', '21'],
// ['012', '021', '102', '120', '201', '210']
// ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-permlist-a-permlist-n-r-"><a id="permList"></a><code>permList(n, r)</code></h3>
<p><a href="#permList">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2026" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates the indices of n-permute-r. Calls <code>_.pSubset</code> internally, chooses the array with string length r, and converts to numbers.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The number of items to permute.</li>
<li><code>r</code> <em>(number)</em>: The number of items chosen.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of index arrays specifying the permutation indices.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.permList(3, 2)
// → [ [ 0, 1 ], [ 0, 2 ], [ 1, 0 ], [ 1, 2 ], [ 2, 0 ], [ 2, 1 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-permutation-a-permutation-n-r-"><a id="permutation"></a><code>permutation(n, r)</code></h3>
<p><a href="#permutation">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2117" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns n-permute-r.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The integer.</li>
<li><code>r</code> <em>(number)</em>: The integer.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: nPr</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.permutation(5, 5)
// → 120
_.permutation(1000, 1)
// → 1000
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-permute-a-permute-n-"><a id="permute"></a><code>permute(n)</code></h3>
<p><a href="#permute">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L2064" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates the permutation indices of n items in lexicographical order.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The number of items to permute.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of index arrays specifying the permutation indices.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.permute(3)
// → [
// [ 0, 1, 2 ],
// [ 0, 2, 1 ],
// [ 1, 0, 2 ],
// [ 1, 2, 0 ],
// [ 2, 0, 1 ],
// [ 2, 1, 0 ]
// ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-subset-a-subset-n-"><a id="subset"></a><code>subset(n)</code></h3>
<p><a href="#subset">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1996" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generates all the (combination) subset indices of n items.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>n</code> <em>(number)</em>: The number of items to choose.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of strings of length n, specifying the subset indices.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.subset(3)
// → [
// ['0', '1', '2'],
// ['01', '02', '12'],
// ['012']
// ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-tonumarr-a-tonumarr-strings-"><a id="toNumArr"></a><code>toNumArr(strings)</code></h3>
<p><a href="#toNumArr">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1939" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Converts an array of strings to array of array of numbers.
Used with <code>_.genAry</code> and related number/subset-generating functions.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>strings</code> <em>(Array)</em>: The strings of numbers to convert into arrays.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: T The array of array of numbers from the strings.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.toNumArr(['00', '01', '10', '11']) // binary, length 2
// → [[0, 0], [0, 1], [1, 0], [1, 1]]
</code></pre>
<hr>
</div>
</div>
<div>
<h2 id="-composition-methods-"><code>“composition” Methods</code></h2>
<div>
<h3 id="-a-id-asso-a-asso-fn-x-"><a id="asso"></a><code>asso(fn, [...x])</code></h3>
<p><a href="#asso">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L185" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generic association: take the arguments object or array and apply atomic function (with scalar arguments) from left to right.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: An atomic binary function <em>(both arguments must be scalars)</em>.</li>
<li><code>[...x]</code> <em>(...number)</em>: Scalars; can be grouped in a single array.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: A scalar from the function applied to all arguments in order.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.asso(_.op, 'a', 'b', 'c')
// where _.op is used to show the order of composition
// → 'a*b*c'
_.asso(_.op, ['a', 'b', 'c'])
// → 'a*b*c'
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-assodist-a-assodist-fn-x-"><a id="assodist"></a><code>assodist(fn, [...X])</code></h3>
<p><a href="#assodist">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L218" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generic association with distributivity: Similar to <code>_.asso</code> but is for tensor functions; apply atomic fn distributively in order using <code>_.distribute</code>.
Usage: for applying fn on tensors element-wise if they have compatible dimensions.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: An atomic binary function <em>(both arguments must be scalars)</em>.</li>
<li><code>[...X]</code> <em>(...tensors)</em>: tensors.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied to all arguments in order.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.assodist(_.op, 'a', 'b', 'c')
// where _.op is used to show the order of composition
// → 'a*b*c'
_.assodist(_.op, 'a', [1, 2, 3], 'b')
// → ['a*1*b', 'a*2*b', 'a*3*b']
_.assodist(_.op, 'a', [[1, 2], [3, 4]])
// → [['a*1', 'a*2'], ['a*3', 'a*4']]
_.assodist(_.op, ['a', 'b'], [[1, 2], [3, 4]])
// → [['a*1', 'a*2'], ['b*3', 'b*4']]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-distribute-a-distribute-fn-x-y-"><a id="distribute"></a><code>distribute(fn, X, Y)</code></h3>
<p><a href="#distribute">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L160" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Generic Distribution: Distribute fn between left tensor X and right tensor Y, while preserving the argument-ordering (vital for non-commutative functions).
Pairs up the tensors term-wise while descending down the depths recursively using <code>_.distributeBoth</code>, until finding a scalar to <code>_.distributeLeft/Right</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: A binary function.</li>
<li><code>X</code> <em>(tensor)</em>: A tensor.</li>
<li><code>Y</code> <em>(tensor)</em>: A tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied element-wise between X and Y.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.distribute(_.op, 'a', [1, 2, 3])
// where _.op is used to show the order of composition
// → ['a*1', 'a*2', 'a*3']
_.distribute(_.op, 'a', [[1, 2], [3, 4])
// → [ [ 'a*1', 'a*2' ], [ 'a*3', 'a*4' ] ]
_.distribute(_.op, ['a', 'b', 'c'], [1, 2, 3])
// → [ 'a*1', 'b*2', 'c*3' ]
_.distribute(_.op, ['a', 'b', 'c'], [1, 2, 3, 4, 5, 6])
// → [ 'a*1', 'b*2', 'c*3' , 'a*4', 'b*5', 'c*6']
_.distribute(_.op, ['a', 'b', 'c'], [[1, 2], [3, 4], [5, 6]])
// → [ [ 'a*1', 'a*2' ], [ 'b*3', 'b*4' ], [ 'c*5', 'c*6' ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-distributeboth-a-distributeboth-fn-x-y-"><a id="distributeBoth"></a><code>distributeBoth(fn, X, Y)</code></h3>
<p><a href="#distributeBoth">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L117" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Distributes a binary function between non-scalar tensors X, Y: pair them up term-wise and calling <code>_.distribute</code> recursively. Perserves the order of arguments.
If at any depth X and Y have different lengths, recycle if the mod of lengths is 0.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: A binary function.</li>
<li><code>X</code> <em>(tensor)</em>: A non-scalar tensor.</li>
<li><code>Y</code> <em>(tensor)</em>: A non-scalar tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied element-wise between X and Y.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.distributeBoth(_.op, ['a', 'b', 'c'], [1, 2, 3])
// where _.op is used to show the order of composition
// → [ 'a*1', 'b*2', 'c*3' ]
_.distributeBoth(_.op, ['a', 'b', 'c'], [1, 2, 3, 4, 5, 6])
// → [ 'a*1', 'b*2', 'c*3' , 'a*4', 'b*5', 'c*6']
_.distributeBoth(_.op, ['a', 'b', 'c'], [[1, 2], [3, 4], [5, 6]])
// → [ [ 'a*1', 'a*2' ], [ 'b*3', 'b*4' ], [ 'c*5', 'c*6' ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-distributeleft-a-distributeleft-fn-x-y-"><a id="distributeLeft"></a><code>distributeLeft(fn, X, y)</code></h3>
<p><a href="#distributeLeft">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L63" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Distributes a binary function with left tensor X over right scalar y. Preserves the order of arguments.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: A binary function.</li>
<li><code>X</code> <em>(tensor)</em>: A non-scalar tensor.</li>
<li><code>y</code> <em>(number)</em>: A scalar.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied element-wise between X and y.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.distributeLeft(_.op([1, 2, 3, 4], 5))
// where _.op is used to show the order of composition
// → [ '1*5', '2*5', '3*5', '4*5' ]
_.distributeLeft(_.op, [[1, 2], [3, 4]], 5)
// → [ [ '1*5', '2*5' ], [ '3*5', '4*5' ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-distributeright-a-distributeright-fn-x-y-"><a id="distributeRight"></a><code>distributeRight(fn, x, Y)</code></h3>
<p><a href="#distributeRight">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L88" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Distributes a binary function with left scalar x over right tensor Y. Preserves the order of arguments.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: A binary function.</li>
<li><code>x</code> <em>(number)</em>: A scalar.</li>
<li><code>Y</code> <em>(tensor)</em>: A non-scalar tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied element-wise between x and Y.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.distributeRight(_.op, 5, [1, 2, 3, 4])
// where _.op is used to show the order of composition
// → [ '5*1', '5*2', '5*3', '5*4' ]
_.distributeRight(_.op, 5, [[1, 2], [3, 4]])
// → [ [ '5*1', '5*2' ], [ '5*3', '5*4' ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-distributesingle-a-distributesingle-fn-y-"><a id="distributeSingle"></a><code>distributeSingle(fn, Y)</code></h3>
<p><a href="#distributeSingle">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L37" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Distributes a unary function over every scalar in tensor Y.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>fn</code> <em>(Function)</em>: A unary function.</li>
<li><code>Y</code> <em>(tensor)</em>: A non-scalar tensor.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A tensor from the function applied element-wise to Y.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.distributeSingle(_.square, [1, 2, 3, 4])
// → [ 1, 4, 9, 16 ]
_.distributeSingle(_.square, [[1, 2], [3, 4]])
// → [ [ 1, 4 ], [ 9, 16 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-op-a-op-x-y-"><a id="op"></a><code>op(x, y)</code></h3>
<p><a href="#op">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L18" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Sample operation to demonstrate function composition.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>x</code> <em>(*)</em>: An argument.</li>
<li><code>y</code> <em>(*)</em>: An argument.</li>
</ol>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.op('a', 'b')
// → 'a*b'
</code></pre>
<hr>
</div>
</div>
<div>
<h2 id="-initialization-methods-"><code>“initialization” Methods</code></h2>
<div>
<h3 id="-a-id-numeric-a-numeric-n-val-0-"><a id="numeric"></a><code>numeric(N, [val=0])</code></h3>
<p><a href="#numeric">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1136" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns an initialized array of length N filled with the value (defaulted to 0). Reminiscent of <code>numeric()</code> of <code>R</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>N</code> <em>(number)</em>: The length of array.</li>
<li><code>[val=0]</code> <em>(*)</em>: The value to fill array with.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: filled An array initialized to the value.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.numeric(3)
// → [0, 0, 0]
_.numeric(3, 'a')
// → ['a', 'a', 'a']
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-seq-a-seq-start-0-end-step-1-"><a id="seq"></a><code>seq([start=0], end, [step=1])</code></h3>
<p><a href="#seq">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1105" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns a sequence of numbers from start to end, with interval. Similar to lodash's <code>_.range</code> but the default starts from 1; this is for <code>R</code> users who are familiar with <code>seq()</code>.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>[start=0]</code> <em>(number)</em>: The start value.</li>
<li><code>end</code> <em>(number)</em>: The end value.</li>
<li><code>[step=1]</code> <em>(number)</em>: The interval step.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(Array)</em>: seq An array initialized to the sequence.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.seq(3)
// → [1, 2, 3]
_.seq(2, 4)
// → [2, 3, 4]
_.seq(1, 9, 2)
[ 1, 3, 5, 7, 9 ]
</code></pre>
<hr>
</div>
</div>
<div>
<h2 id="-matrices-methods-"><code>“matrices” Methods</code></h2>
<div>
<h3 id="-a-id-adj-a-adj-m-"><a id="adj"></a><code>adj(M)</code></h3>
<p><a href="#adj">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1817" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns the adjugate matrix, i.e. the transpose of the comatrix.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The original matrix.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: A The adj matrix.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.adj([[1,2,3],[4,5,6],[11,13,17]])
// → [ [ 7, 5, -3 ], [ -2, -16, 6 ], [ -3, 9, -3 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-comatrix-a-comatrix-m-"><a id="coMatrix"></a><code>coMatrix(M)</code></h3>
<p><a href="#coMatrix">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1793" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns the comatrix, i.e. the minor matrix or matrix of cofactors, of M.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The original matrix.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: C The comatrix.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.coMatrix([[1,2,3],[4,5,6],[11,13,17]])
// → [ [ 7, -2, -3 ], [ 5, -16, 9 ], [ -3, 6, -3 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-cosubmatrix-a-cosubmatrix-m-r-0-c-0-"><a id="coSubMatrix"></a><code>coSubMatrix(M, [r=0], [c=0])</code></h3>
<p><a href="#coSubMatrix">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1765" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns the submatrix for calculating cofactor, i.e. by taking out row r, col c from M.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The original matrix.</li>
<li><code>[r=0]</code> <em>(number)</em>: The cofactor row.</li>
<li><code>[c=0]</code> <em>(number)</em>: The cofactor col.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: F The submatrix.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.coSubMatrix([[1,2,3],[4,5,6],[7,8,9]])
// → [ [ 5, 6 ], [ 8, 9 ] ]
_.coSubMatrix([[1,2,3],[4,5,6],[7,8,9]], 0, 1)
// → [ [ 4, 6 ], [ 7, 9 ] ]
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-det-a-det-m-"><a id="det"></a><code>det(M)</code></h3>
<p><a href="#det">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1860" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns the determinant of a matrix.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The matrix.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: det The determinant.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.det([[1,2,3],[4,5,6],[11,13,17]])
// → -6
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-detsum-a-detsum-m-i-"><a id="detSum"></a><code>detSum(M, i)</code></h3>
<p><a href="#detSum">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1842" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Helper function for determinant, used with _.fsum. Sums all the cofactors multiplied with the comatrices.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The original matrix.</li>
<li><code>i</code> <em>(number)</em>: The index of cofactor, needed for parity.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(number)</em>: recursive-sub-determinant Parity * cofactor * det(coSubMatrix)</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.detSum([[1,2,3],[4,5,6],[11,13,17]], 0)
// → 7
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-inv-a-inv-m-"><a id="inv"></a><code>inv(M)</code></h3>
<p><a href="#inv">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1892" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Returns the inverse of a matrix, or null if non-invertible.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>M</code> <em>(tensor)</em>: The matrix.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: Inv The inverse matrix, or null.</p>
<h4 id="example">Example</h4>
<pre><code class="lang-js">_.inv([[1,4,7],[3,0,5],[-1,9,11]])
// → [ [ 5.625, -2.375, -2.5 ],
// [ 4.75, -2.25, -2 ],
// [ -3.375, 1.625, 1.5 ] ]
_.inv([[1,1],[1,1]])
// → null
</code></pre>
<hr>
</div>
<div>
<h3 id="-a-id-matmultiply-a-matmultiply-a-b-"><a id="matMultiply"></a><code>matMultiply(A, B)</code></h3>
<p><a href="#matMultiply">#</a> <a href="https://github.com/kengz/lomath/blob/master/index.js#L1742" title="View in source">Ⓢ</a> <a href="#basics" title="Jump back to the TOC.">Ⓣ</a></p>
<p>Multiply two matrices.</p>
<h4 id="arguments">Arguments</h4>
<ol>
<li><code>A</code> <em>(tensor)</em>: The first matrix.</li>
<li><code>B</code> <em>(tensor)</em>: The second matrix.</li>
</ol>
<h4 id="returns">Returns</h4>
<p><em>(tensor)</em>: AB The two matrices multiplied together.</p>
<h4 id="ex