├── .babelrc ├── .gitignore ├── .npmignore ├── README.md ├── app.js ├── dist ├── mathlab.js ├── mathlab.min.js └── mathlab.min.js.map ├── docs ├── README.md ├── ast │ └── source │ │ ├── Complex.js.json │ │ ├── OldT.js.json │ │ ├── Sparse.js.json │ │ ├── abs.js.json │ │ ├── acos.js.json │ │ ├── add.js.json │ │ ├── and.js.json │ │ ├── asin.js.json │ │ ├── atan.js.json │ │ ├── atan2.js.json │ │ ├── band.js.json │ │ ├── bnot.js.json │ │ ├── bor.js.json │ │ ├── bxor.js.json │ │ ├── ceil.js.json │ │ ├── clone.js.json │ │ ├── cos.js.json │ │ ├── det.js.json │ │ ├── diag.js.json │ │ ├── dim.js.json │ │ ├── div.js.json │ │ ├── dot.js.json │ │ ├── eig.js.json │ │ ├── epsilon.js.json │ │ ├── eq.js.json │ │ ├── exp.js.json │ │ ├── fft.js.json │ │ ├── floor.js.json │ │ ├── geq.js.json │ │ ├── getBlock.js.json │ │ ├── getDiag.js.json │ │ ├── gt.js.json │ │ ├── identity.js.json │ │ ├── inv.js.json │ │ ├── leq.js.json │ │ ├── linspace.js.json │ │ ├── log.js.json │ │ ├── lshift.js.json │ │ ├── lt.js.json │ │ ├── mod.js.json │ │ ├── mul.js.json │ │ ├── neg.js.json │ │ ├── negtranspose.js.json │ │ ├── neq.js.json │ │ ├── norm2.js.json │ │ ├── norm2Squared.js.json │ │ ├── not.js.json │ │ ├── or.js.json │ │ ├── pointwise.js.json │ │ ├── pointwise2.js.json │ │ ├── pow.js.json │ │ ├── random.js.json │ │ ├── reciprocal.js.json │ │ ├── rep.js.json │ │ ├── round.js.json │ │ ├── rrshift.js.json │ │ ├── rshift.js.json │ │ ├── same.js.json │ │ ├── setBlock.js.json │ │ ├── sin.js.json │ │ ├── spPointwise.js.json │ │ ├── spPointwise2.js.json │ │ ├── sqrt.js.json │ │ ├── sub.js.json │ │ ├── sup.js.json │ │ ├── tan.js.json │ │ ├── tensor.js.json │ │ ├── transpose.js.json │ │ └── utils │ │ └── clone.js.json ├── badge.svg ├── class │ └── src │ │ ├── Complex.js~Complex.html │ │ └── Sparse.js~Sparse.html ├── coverage.json ├── css │ ├── prettify-tomorrow.css │ └── style.css ├── dump.json ├── file │ └── src │ │ ├── Complex.js.html │ │ ├── OldT.js.html │ │ ├── Sparse.js.html │ │ ├── abs.js.html │ │ ├── acos.js.html │ │ ├── add.js.html │ │ ├── and.js.html │ │ ├── asin.js.html │ │ ├── atan.js.html │ │ ├── atan2.js.html │ │ ├── band.js.html │ │ ├── bnot.js.html │ │ ├── bor.js.html │ │ ├── bxor.js.html │ │ ├── ceil.js.html │ │ ├── clone.js.html │ │ ├── cos.js.html │ │ ├── det.js.html │ │ ├── diag.js.html │ │ ├── dim.js.html │ │ ├── div.js.html │ │ ├── dot.js.html │ │ ├── eig.js.html │ │ ├── epsilon.js.html │ │ ├── eq.js.html │ │ ├── exp.js.html │ │ ├── fft.js.html │ │ ├── floor.js.html │ │ ├── geq.js.html │ │ ├── getBlock.js.html │ │ ├── getDiag.js.html │ │ ├── gt.js.html │ │ ├── identity.js.html │ │ ├── inv.js.html │ │ ├── leq.js.html │ │ ├── linspace.js.html │ │ ├── log.js.html │ │ ├── lshift.js.html │ │ ├── lt.js.html │ │ ├── mod.js.html │ │ ├── mul.js.html │ │ ├── neg.js.html │ │ ├── negtranspose.js.html │ │ ├── neq.js.html │ │ ├── norm2.js.html │ │ ├── norm2Squared.js.html │ │ ├── not.js.html │ │ ├── or.js.html │ │ ├── pointwise.js.html │ │ ├── pointwise2.js.html │ │ ├── pow.js.html │ │ ├── random.js.html │ │ ├── reciprocal.js.html │ │ ├── rep.js.html │ │ ├── round.js.html │ │ ├── rrshift.js.html │ │ ├── rshift.js.html │ │ ├── same.js.html │ │ ├── setBlock.js.html │ │ ├── sin.js.html │ │ ├── spPointwise.js.html │ │ ├── spPointwise2.js.html │ │ ├── sqrt.js.html │ │ ├── sub.js.html │ │ ├── sup.js.html │ │ ├── tan.js.html │ │ ├── tensor.js.html │ │ ├── transpose.js.html │ │ └── utils │ │ └── clone.js.html ├── function │ └── index.html ├── identifiers.html ├── image │ ├── badge.svg │ ├── github.png │ └── search.png ├── index.html ├── package.json ├── script │ ├── inherited-summary.js │ ├── inner-link.js │ ├── manual.js │ ├── patch-for-local.js │ ├── prettify │ │ ├── Apache-License-2.0.txt │ │ └── prettify.js │ ├── pretty-print.js │ ├── search.js │ ├── search_index.js │ └── test-summary.js └── source.html ├── esdoc.json ├── example ├── index.js └── package.json ├── lib ├── Complex.js ├── OldT.js ├── Sparse.js ├── T.js ├── abs.js ├── acos.js ├── add.js ├── and.js ├── asin.js ├── atan.js ├── atan2.js ├── band.js ├── bnot.js ├── bor.js ├── bxor.js ├── ceil.js ├── clone.js ├── cos.js ├── det.js ├── diag.js ├── dim.js ├── div.js ├── dot.js ├── eig.js ├── epsilon.js ├── eq.js ├── exp.js ├── fft.js ├── floor.js ├── geq.js ├── getBlock.js ├── getDiag.js ├── gt.js ├── identity.js ├── index.js ├── inv.js ├── leq.js ├── linspace.js ├── log.js ├── lshift.js ├── lt.js ├── mod.js ├── mul.js ├── neg.js ├── negtranspose.js ├── neq.js ├── norm2.js ├── norm2Squared.js ├── not.js ├── or.js ├── pointwise.js ├── pointwise2.js ├── pow.js ├── random.js ├── reciprocal.js ├── rep.js ├── round.js ├── rrshift.js ├── rshift.js ├── same.js ├── setBlock.js ├── sin.js ├── spPointwise.js ├── spPointwise2.js ├── sqrt.js ├── sub.js ├── sup.js ├── tan.js ├── tensor.js └── transpose.js ├── package.json ├── scripts ├── docGen.js ├── generateIndexInSrc.js ├── generatePointwiseFunctions.js ├── generatePointwiseFunctions2.js ├── generatePointwiseOps1.js ├── generatePointwiseOps2.js ├── generateTestTemplates.js ├── numeric.js ├── src │ ├── dotMMbig.js │ ├── dotMMsmall.js │ ├── dotMV.js │ ├── dotVM.js │ └── dotVV.js └── test │ ├── dotMMbig.js │ ├── dotMMsmall.js │ ├── dotMV.js │ ├── dotVM.js │ └── dotVV.js ├── src ├── Complex.js ├── Sparse.js ├── abs.js ├── acos.js ├── add.js ├── and.js ├── asin.js ├── atan.js ├── atan2.js ├── band.js ├── bnot.js ├── bor.js ├── bxor.js ├── ceil.js ├── clone.js ├── cos.js ├── det.js ├── diag.js ├── dim.js ├── div.js ├── dot.js ├── eig.js ├── epsilon.js ├── eq.js ├── exp.js ├── fft.js ├── floor.js ├── geq.js ├── getBlock.js ├── getDiag.js ├── gt.js ├── identity.js ├── index.js ├── inv.js ├── leq.js ├── linspace.js ├── log.js ├── lshift.js ├── lt.js ├── mod.js ├── mul.js ├── neg.js ├── negtranspose.js ├── neq.js ├── norm2.js ├── norm2Squared.js ├── not.js ├── or.js ├── pointwise.js ├── pointwise2.js ├── pow.js ├── random.js ├── reciprocal.js ├── rep.js ├── round.js ├── rrshift.js ├── rshift.js ├── same.js ├── setBlock.js ├── sin.js ├── spPointwise.js ├── spPointwise2.js ├── sqrt.js ├── sub.js ├── sup.js ├── tan.js ├── tensor.js └── transpose.js └── test ├── Complex.js ├── Sparse.js ├── abs.js ├── acos.js ├── add.js ├── and.js ├── asin.js ├── atan.js ├── atan2.js ├── band.js ├── bnot.js ├── bor.js ├── bxor.js ├── ceil.js ├── clone.js ├── cos.js ├── det.js ├── diag.js ├── dim.js ├── div.js ├── dot.js ├── eig.js ├── epsilon.js ├── eq.js ├── exp.js ├── fft.js ├── floor.js ├── geq.js ├── getBlock.js ├── getDiag.js ├── gt.js ├── identity.js ├── inv.js ├── leq.js ├── linspace.js ├── log.js ├── lshift.js ├── lt.js ├── mod.js ├── mul.js ├── neg.js ├── neq.js ├── norm2.js ├── not.js ├── or.js ├── pointwise.js ├── pow.js ├── random.js ├── rep.js ├── round.js ├── rrshift.js ├── rshift.js ├── same.js ├── setBlock.js ├── sin.js ├── sqrt.js ├── sub.js ├── tan.js ├── tensor.js └── transpose.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015","stage-0"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.log* -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.log* 4 | example 5 | .babelrc 6 | docs 7 | scripts -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // used to build bundles in dist/ for browser 2 | var mathlab = require('./lib/index') 3 | 4 | if(window !== "undefined") { 5 | window.mathlab = mathlab; 6 | } -------------------------------------------------------------------------------- /docs/badge.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | document 13 | document 14 | 74% 15 | 74% 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/image/badge.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | document 13 | document 14 | @ratio@ 15 | @ratio@ 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/image/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timqian/mathlab/4acc4383545388d5d7b85983933b24d617be45c9/docs/image/github.png -------------------------------------------------------------------------------- /docs/image/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timqian/mathlab/4acc4383545388d5d7b85983933b24d617be45c9/docs/image/search.png -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathlab", 3 | "version": "0.0.14", 4 | "description": "lodash like math lab", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "compile": "babel -d lib/ src/", 8 | "pretest": "npm run compile", 9 | "test": "mocha --recursive --require babel-register", 10 | "genDoc": "./node_modules/esdoc/out/src/ESDocCLI.js -c esdoc.json", 11 | "build:bundle": "browserify app.js -o dist/mathlab.js", 12 | "build:uglify": "uglifyjs dist/mathlab.js -mc --source-map dist/mathlab.min.js.map -o dist/mathlab.min.js", 13 | "build":"npm run build:bundle && npm run build:uglify", 14 | "prepublish": "npm run test && npm run genDoc && npm run compile && npm run build" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/timqian/mathlab.git" 19 | }, 20 | "keywords": [ 21 | "math", 22 | "javascript" 23 | ], 24 | "author": "timqian", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/timqian/mathlab/issues" 28 | }, 29 | "homepage": "https://github.com/timqian/mathlab#readme", 30 | "devDependencies": { 31 | "babel-preset-es2015": "^6.3.13", 32 | "babel-preset-stage-0": "^6.3.13", 33 | "browserify": "^13.1.0", 34 | "debug": "^2.2.0", 35 | "esdoc": "^0.4.7", 36 | "mocha": "^2.3.4", 37 | "should": "^9.0.2", 38 | "uglify-js": "^2.7.3" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /docs/script/inherited-summary.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | function toggle(ev) { 3 | var button = ev.target; 4 | var parent = ev.target.parentElement; 5 | while(parent) { 6 | if (parent.tagName === 'TABLE' && parent.classList.contains('summary')) break; 7 | parent = parent.parentElement; 8 | } 9 | 10 | if (!parent) return; 11 | 12 | var tbody = parent.querySelector('tbody'); 13 | if (button.classList.contains('opened')) { 14 | button.classList.remove('opened'); 15 | button.classList.add('closed'); 16 | tbody.style.display = 'none'; 17 | } else { 18 | button.classList.remove('closed'); 19 | button.classList.add('opened'); 20 | tbody.style.display = 'block'; 21 | } 22 | } 23 | 24 | var buttons = document.querySelectorAll('.inherited-summary thead .toggle'); 25 | for (var i = 0; i < buttons.length; i++) { 26 | buttons[i].addEventListener('click', toggle); 27 | } 28 | })(); 29 | -------------------------------------------------------------------------------- /docs/script/inner-link.js: -------------------------------------------------------------------------------- 1 | // inner link(#foo) can not correctly scroll, because page has fixed header, 2 | // so, I manually scroll. 3 | (function(){ 4 | var matched = location.hash.match(/errorLines=([\d,]+)/); 5 | if (matched) return; 6 | 7 | function adjust() { 8 | window.scrollBy(0, -55); 9 | var el = document.querySelector('.inner-link-active'); 10 | if (el) el.classList.remove('inner-link-active'); 11 | 12 | // ``[ ] . ' " @`` are not valid in DOM id. so must escape these. 13 | var id = location.hash.replace(/([\[\].'"@$])/g, '\\$1'); 14 | var el = document.querySelector(id); 15 | if (el) el.classList.add('inner-link-active'); 16 | } 17 | 18 | window.addEventListener('hashchange', adjust); 19 | 20 | if (location.hash) { 21 | setTimeout(adjust, 0); 22 | } 23 | })(); 24 | 25 | (function(){ 26 | var els = document.querySelectorAll('[href^="#"]'); 27 | for (var i = 0; i < els.length; i++) { 28 | var el = els[i]; 29 | el.href = location.href + el.getAttribute('href'); // because el.href is absolute path 30 | } 31 | })(); 32 | -------------------------------------------------------------------------------- /docs/script/manual.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | var matched = location.pathname.match(/([^/]*)\.html$/); 3 | if (!matched) return; 4 | 5 | var currentName = matched[1]; 6 | var cssClass = '.navigation [data-toc-name="' + currentName + '"]'; 7 | var styleText = cssClass + ' .manual-toc { display: block; }\n'; 8 | styleText += cssClass + ' .manual-toc-title { background-color: #039BE5; }\n'; 9 | styleText += cssClass + ' .manual-toc-title a { color: white; }\n'; 10 | var style = document.createElement('style'); 11 | style.textContent = styleText; 12 | document.querySelector('head').appendChild(style); 13 | })(); 14 | -------------------------------------------------------------------------------- /docs/script/patch-for-local.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | if (location.protocol === 'file:') { 3 | var elms = document.querySelectorAll('a[href="./"]'); 4 | for (var i = 0; i < elms.length; i++) { 5 | elms[i].href = './index.html'; 6 | } 7 | } 8 | })(); 9 | -------------------------------------------------------------------------------- /docs/script/pretty-print.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | prettyPrint(); 3 | var lines = document.querySelectorAll('.prettyprint.linenums li[class^="L"]'); 4 | for (var i = 0; i < lines.length; i++) { 5 | lines[i].id = 'lineNumber' + (i + 1); 6 | } 7 | 8 | var matched = location.hash.match(/errorLines=([\d,]+)/); 9 | if (matched) { 10 | var lines = matched[1].split(','); 11 | for (var i = 0; i < lines.length; i++) { 12 | var id = '#lineNumber' + lines[i]; 13 | var el = document.querySelector(id); 14 | el.classList.add('error-line'); 15 | } 16 | return; 17 | } 18 | 19 | if (location.hash) { 20 | // ``[ ] . ' " @`` are not valid in DOM id. so must escape these. 21 | var id = location.hash.replace(/([\[\].'"@$])/g, '\\$1'); 22 | var line = document.querySelector(id); 23 | if (line) line.classList.add('active'); 24 | } 25 | })(); 26 | -------------------------------------------------------------------------------- /docs/script/test-summary.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | function toggle(ev) { 3 | var button = ev.target; 4 | var parent = ev.target.parentElement; 5 | while(parent) { 6 | if (parent.tagName === 'TR' && parent.classList.contains('test-describe')) break; 7 | parent = parent.parentElement; 8 | } 9 | 10 | if (!parent) return; 11 | 12 | var direction; 13 | if (button.classList.contains('opened')) { 14 | button.classList.remove('opened'); 15 | button.classList.add('closed'); 16 | direction = 'closed'; 17 | } else { 18 | button.classList.remove('closed'); 19 | button.classList.add('opened'); 20 | direction = 'opened'; 21 | } 22 | 23 | var targetDepth = parseInt(parent.dataset.testDepth, 10) + 1; 24 | var nextElement = parent.nextElementSibling; 25 | while (nextElement) { 26 | var depth = parseInt(nextElement.dataset.testDepth, 10); 27 | if (depth >= targetDepth) { 28 | if (direction === 'opened') { 29 | if (depth === targetDepth) nextElement.style.display = ''; 30 | } else if (direction === 'closed') { 31 | nextElement.style.display = 'none'; 32 | var innerButton = nextElement.querySelector('.toggle'); 33 | if (innerButton && innerButton.classList.contains('opened')) { 34 | innerButton.classList.remove('opened'); 35 | innerButton.classList.add('closed'); 36 | } 37 | } 38 | } else { 39 | break; 40 | } 41 | nextElement = nextElement.nextElementSibling; 42 | } 43 | } 44 | 45 | var buttons = document.querySelectorAll('.test-summary tr.test-describe .toggle'); 46 | for (var i = 0; i < buttons.length; i++) { 47 | buttons[i].addEventListener('click', toggle); 48 | } 49 | 50 | var topDescribes = document.querySelectorAll('.test-summary tr[data-test-depth="0"]'); 51 | for (var i = 0; i < topDescribes.length; i++) { 52 | topDescribes[i].style.display = ''; 53 | } 54 | })(); 55 | -------------------------------------------------------------------------------- /esdoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "source": "./src", 3 | "destination": "./docs" 4 | } -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | var diag = require('mathlab').diag 2 | 3 | // es6: 4 | // import { diag } from 'mathlab' 5 | // or 6 | // import diag from 'mathlab/lib/diag' 7 | 8 | console.log(diag([1,2])) -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "description": "test the npm module", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "timqian", 10 | "license": "MIT", 11 | "dependencies": { 12 | "mathlab": "0.0.6" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lib/abs.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cabs(m); 11 | case 'Sparse': 12 | return sabs(m); 13 | default: 14 | return abs(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _Sparse = require('./Sparse'); 31 | 32 | var _Sparse2 = _interopRequireDefault(_Sparse); 33 | 34 | var _mul = require('./mul'); 35 | 36 | var _mul2 = _interopRequireDefault(_mul); 37 | 38 | var _sqrt = require('./sqrt'); 39 | 40 | var _sqrt2 = _interopRequireDefault(_sqrt); 41 | 42 | var _add = require('./add'); 43 | 44 | var _add2 = _interopRequireDefault(_add); 45 | 46 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 47 | 48 | var abs = (0, _pointwise2.default)(Math.abs); 49 | var sabs = (0, _spPointwise2.default)(Math.abs); 50 | 51 | function cabs(x) { 52 | if (x.im) { 53 | return new _Complex2.default((0, _sqrt2.default)((0, _add2.default)((0, _mul2.default)(x.re, x.re), (0, _mul2.default)(x.im, x.im)))); 54 | } 55 | return new _Complex2.default(abs(x.re)); 56 | } 57 | 58 | /** 59 | * Pointwise Math.abs(x) 60 | * 61 | * @export 62 | * @param {Number|Array|Complex|Sparse} m 63 | * @returns {Number|Array|Complex|Sparse} 64 | * @example 65 | * 66 | * abs(-1) // 1 67 | * abs([1, -2]) // [1, 2] 68 | * abs([[-1,2],[1,-3]]) // [[1,2],[1,3]] 69 | */ -------------------------------------------------------------------------------- /lib/acos.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cacos(m); 11 | case 'Sparse': 12 | return sacos(m); 13 | default: 14 | return acos(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var acos = (0, _pointwise2.default)(Math.acos); 33 | var sacos = (0, _spPointwise2.default)(Math.acos); 34 | 35 | function cacos(x) { 36 | throw new Error('mathlab.acos: no acos for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.acos(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Sparse} m 44 | * @returns {Number|Array|Sparse} 45 | * @example 46 | * 47 | * acos(1) // Math.acos(1) 48 | * acos([1, 2]) // [Math.acos(1), Math.acos(2)] 49 | * acos([[1,2],[1,3]]) // [ [Math.acos(1), Math.acos(2)], [Math.acos(1), Math.acos(3)] ] 50 | */ -------------------------------------------------------------------------------- /lib/add.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cadd(m1, m2); 11 | case 'Sparse': 12 | return sadd(m1, m2); 13 | default: 14 | return add(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var add = (0, _pointwise2.default)(function (x, y) { 33 | return x + y; 34 | }); 35 | var sadd = (0, _spPointwise2.default)(function (x, y) { 36 | return x + y; 37 | }); 38 | 39 | function cadd(x, y) { 40 | if (!(y instanceof _Complex2.default)) { 41 | y = new _Complex2.default(y); 42 | } 43 | if (x.im) { 44 | if (y.im) { 45 | return new _Complex2.default(add(x.re, y.re), add(x.im, y.im)); 46 | } 47 | return new _Complex2.default(add(x.re, y.re), x.im); 48 | } 49 | if (y.im) { 50 | return new _Complex2.default(add(x.re, y.re), y.im); 51 | } 52 | return new _Complex2.default(add(x.re, y.re)); 53 | } 54 | 55 | /** 56 | * Pointwise add 57 | * 58 | * @export 59 | * @param {Number|Array|Complex|Sparse} m1 60 | * @param {Number|Array|Complex|Sparse} m2 61 | * @returns {Number|Array|Complex|Sparse} 62 | * @example 63 | * 64 | * add(1, 2) // 3 65 | * add([1, 2], [2, 2]) // [3, 4] 66 | * add([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 + 2, 1 + 2], [1 + 2, 2 + 2] ] 67 | */ -------------------------------------------------------------------------------- /lib/and.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cand(m1, m2); 11 | case 'Sparse': 12 | return sand(m1, m2); 13 | default: 14 | return and(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var and = (0, _pointwise2.default)(function (x, y) { 29 | return x && y; 30 | }); 31 | var sand = (0, _spPointwise2.default)(function (x, y) { 32 | return x && y; 33 | }); 34 | 35 | function cand(x, y) { 36 | throw new Error('mathlab.and: no and for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise and 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * and(1, 0) // 0 49 | * and([1, 1], [1, 0]) // [1, 0] 50 | * and([[1,1], [1,1]], [[1,0], [0,0]])) // [[1,0], [0,0]] 51 | */ -------------------------------------------------------------------------------- /lib/asin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return casin(m); 11 | case 'Sparse': 12 | return sasin(m); 13 | default: 14 | return asin(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var asin = (0, _pointwise2.default)(Math.asin); 33 | var sasin = (0, _spPointwise2.default)(Math.asin); 34 | 35 | function casin(x) { 36 | throw new Error('mathlab.asin: no asin for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.asin(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Sparse} m 44 | * @returns {Number|Array|Sparse} 45 | * @example 46 | * 47 | * asin(1) // Math.asin(1) 48 | * asin([1, 2]) // [Math.asin(1), Math.asin(2)] 49 | * asin([[1,2],[1,3]]) // [ [Math.asin(1), Math.asin(2)], [Math.asin(1), Math.asin(3)] ] 50 | */ -------------------------------------------------------------------------------- /lib/atan.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return catan(m); 11 | case 'Sparse': 12 | return satan(m); 13 | default: 14 | return atan(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var atan = (0, _pointwise2.default)(Math.atan); 33 | var satan = (0, _spPointwise2.default)(Math.atan); 34 | 35 | function catan(x) { 36 | throw new Error('mathlab.atan: no atan for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.atan(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Sparse} m 44 | * @returns {Number|Array|Sparse} 45 | * @example 46 | * 47 | * atan(1) // Math.atan(1) 48 | * atan([1, 2]) // [Math.atan(1), Math.atan(2)] 49 | * atan([[1,2],[1,3]]) // [ [Math.atan(1), Math.atan(2)], [Math.atan(1), Math.atan(3)] ] 50 | */ -------------------------------------------------------------------------------- /lib/atan2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | // TODO 9 | return (0, _pointwise2.default)(Math.atan2)(m1, m2); 10 | }; 11 | 12 | var _pointwise = require('./pointwise2'); 13 | 14 | var _pointwise2 = _interopRequireDefault(_pointwise); 15 | 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -------------------------------------------------------------------------------- /lib/band.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cband(m1, m2); 11 | case 'Sparse': 12 | return sband(m1, m2); 13 | default: 14 | return band(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var band = (0, _pointwise2.default)(function (x, y) { 29 | return x & y; 30 | }); 31 | var sband = (0, _spPointwise2.default)(function (x, y) { 32 | return x & y; 33 | }); 34 | 35 | function cband(x, y) { 36 | throw new Error('mathlab.band: no band for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise band 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * band(1, 2) // 1 & 2 49 | * band([1, 2], [2, 2]) // [1 & 2, 2 & 2] 50 | * band([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 & 2, 1 & 2], [1 & 2, 2 & 2] ] 51 | */ -------------------------------------------------------------------------------- /lib/bnot.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cbnot(m); 11 | case 'Sparse': 12 | return sbnot(m); 13 | default: 14 | return bnot(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var bnot = (0, _pointwise2.default)(function (x) { 33 | return ~x; 34 | }); 35 | var sbnot = (0, _spPointwise2.default)(function (x) { 36 | return ~x; 37 | }); 38 | 39 | function cbnot(x) { 40 | throw new Error('mathlab.bnot: no bnot for complex number'); 41 | } 42 | 43 | /** 44 | * Pointwise Math.bnot(x) 45 | * 46 | * @export 47 | * @param {Number|Array|Sparse} m 48 | * @returns {Number|Array|Sparse} 49 | * @example 50 | * 51 | * bnot(1) // ~1 52 | * bnot([1, 2]) // [~1, ~2] 53 | * bnot([[1,2],[1,3]]) // [[~1,~2],[~1,~3]] 54 | */ -------------------------------------------------------------------------------- /lib/bor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cbor(m1, m2); 11 | case 'Sparse': 12 | return sbor(m1, m2); 13 | default: 14 | return bor(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var bor = (0, _pointwise2.default)(function (x, y) { 29 | return x | y; 30 | }); 31 | var sbor = (0, _spPointwise2.default)(function (x, y) { 32 | return x | y; 33 | }); 34 | 35 | function cbor(x, y) { 36 | throw new Error('mathlab.bor: no bor for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise bor 41 | * 42 | * @export 43 | * @param {Number|Array|Sparse} m1 44 | * @param {Number|Array|Sparse} m2 45 | * @returns {Number|Array|Sparse} 46 | * @example 47 | * 48 | * bor(1, 2) // 1 | 2 49 | * bor([1, 2], [2, 2]) // [1 | 2, 2 | 2] 50 | * bor([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 | 2, 1 | 2], [1 | 2, 2 | 2] ] 51 | */ -------------------------------------------------------------------------------- /lib/bxor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cbxor(m1, m2); 11 | case 'Sparse': 12 | return sbxor(m1, m2); 13 | default: 14 | return bxor(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var bxor = (0, _pointwise2.default)(function (x, y) { 29 | return x ^ y; 30 | }); 31 | var sbxor = (0, _spPointwise2.default)(function (x, y) { 32 | return x ^ y; 33 | }); 34 | 35 | function cbxor(x, y) { 36 | throw new Error('mathlab.bxor: no bxor for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise bxor 41 | * 42 | * @export 43 | * @param {Number|Array|Sparse} m1 44 | * @param {Number|Array|Sparse} m2 45 | * @returns {Number|Array|Sparse} 46 | * @example 47 | * 48 | * bxor(1, 2) // 1 ^ 2 49 | * bxor([1, 2], [2, 2]) // [1 ^ 2, 2 ^ 2] 50 | * bxor([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 ^ 2, 1 ^ 2], [1 ^ 2, 2 ^ 2] ] 51 | */ -------------------------------------------------------------------------------- /lib/ceil.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cceil(m); 11 | case 'Sparse': 12 | return sceil(m); 13 | default: 14 | return ceil(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var ceil = (0, _pointwise2.default)(Math.ceil); 33 | var sceil = (0, _spPointwise2.default)(Math.ceil); 34 | 35 | function cceil(x) { 36 | throw new Error('mathlab.ceil: no ceil for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.ceil(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m 44 | * @returns {Number|Array|Complex|Sparse} 45 | * @example 46 | * 47 | * ceil(1) // Math.ceil(1) 48 | * ceil([1, 2]) // [Math.ceil(1), Math.ceil(2)] 49 | * ceil([[1,2],[1,3]]) // [ [Math.ceil(1), Math.ceil(2)], [Math.ceil(1), Math.ceil(3)] ] 50 | */ -------------------------------------------------------------------------------- /lib/clone.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cclone(m); 11 | case 'Sparse': 12 | return sclone(m); 13 | default: 14 | return clone(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var clone = (0, _pointwise2.default)(function (x) { 33 | return x; 34 | }); 35 | var sclone = (0, _spPointwise2.default)(function (x) { 36 | return x; 37 | }); 38 | 39 | function cclone(x) { 40 | throw new Error('mathlab.clone: no clone for complex number'); 41 | } 42 | 43 | /** 44 | * Pointwise clone(x) 45 | * 46 | * @export 47 | * @param {Number|Array|Complex|Sparse} m 48 | * @returns {Number|Array|Complex|Sparse} 49 | * @example 50 | * 51 | * clone([[1,2],[1,3]]) // [[1,2],[1,3]] 52 | */ -------------------------------------------------------------------------------- /lib/cos.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return ccos(m); 11 | case 'Sparse': 12 | return scos(m); 13 | default: 14 | return cos(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _Sparse = require('./Sparse'); 31 | 32 | var _Sparse2 = _interopRequireDefault(_Sparse); 33 | 34 | var _neg = require('./neg'); 35 | 36 | var _neg2 = _interopRequireDefault(_neg); 37 | 38 | var _exp = require('./exp'); 39 | 40 | var _exp2 = _interopRequireDefault(_exp); 41 | 42 | var _div = require('./div'); 43 | 44 | var _div2 = _interopRequireDefault(_div); 45 | 46 | var _add = require('./add'); 47 | 48 | var _add2 = _interopRequireDefault(_add); 49 | 50 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 51 | 52 | var cos = (0, _pointwise2.default)(Math.cos); 53 | var scos = (0, _spPointwise2.default)(Math.cos); 54 | 55 | function ccos(x) { 56 | if (x.im) { 57 | return (0, _div2.default)((0, _add2.default)((0, _exp2.default)(x), (0, _exp2.default)((0, _neg2.default)(x))), 2); 58 | } 59 | return new _Complex2.default(cos(x.re)); 60 | } 61 | 62 | /** 63 | * Pointwise Math.cos(x) 64 | * 65 | * @export 66 | * @param {Number|Array|Complex|Sparse} m 67 | * @returns {Number|Array|Complex|Sparse} 68 | * @example 69 | * 70 | * cos(1) 71 | * // returns Math.cos(1) 72 | * cos([1, 2]) 73 | * // returns [Math.cos(1), Math.cos(2)] 74 | * cos([[1,2],[1,3]]) 75 | * // returns [ [Math.cos(1), Math.cos(2)], [Math.cos(1), Math.cos(3)] ] 76 | */ -------------------------------------------------------------------------------- /lib/det.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = det; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | var _clone = require('./clone'); 13 | 14 | var _clone2 = _interopRequireDefault(_clone); 15 | 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 17 | 18 | /** 19 | * Determinant of matix 20 | * 21 | * @export 22 | * @param {Array} x 23 | * @returns {Number} 24 | * @example 25 | * 26 | * det([[1, 2], [1, 2]]) 27 | * // 0 28 | */ 29 | // Determinant 30 | function det(x) { 31 | // TODO 32 | var s = (0, _dim2.default)(x); 33 | if (s.length !== 2 || s[0] !== s[1]) { 34 | throw new Error('det() only works on square matrices'); 35 | } 36 | var n = s[0], 37 | ret = 1, 38 | i, 39 | j, 40 | k, 41 | A = (0, _clone2.default)(x), 42 | Aj, 43 | Ai, 44 | alpha, 45 | temp, 46 | k1, 47 | k2, 48 | k3; 49 | for (j = 0; j < n - 1; j++) { 50 | k = j; 51 | for (i = j + 1; i < n; i++) { 52 | if (Math.abs(A[i][j]) > Math.abs(A[k][j])) { 53 | k = i; 54 | } 55 | } 56 | if (k !== j) { 57 | temp = A[k];A[k] = A[j];A[j] = temp; 58 | ret *= -1; 59 | } 60 | Aj = A[j]; 61 | for (i = j + 1; i < n; i++) { 62 | Ai = A[i]; 63 | alpha = Ai[j] / Aj[j]; 64 | for (k = j + 1; k < n - 1; k += 2) { 65 | k1 = k + 1; 66 | Ai[k] -= Aj[k] * alpha; 67 | Ai[k1] -= Aj[k1] * alpha; 68 | } 69 | if (k !== n) { 70 | Ai[k] -= Aj[k] * alpha; 71 | } 72 | } 73 | if (Aj[j] === 0) { 74 | return 0; 75 | } 76 | ret *= Aj[j]; 77 | } 78 | return ret * A[j][j]; 79 | } -------------------------------------------------------------------------------- /lib/diag.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = diag; 7 | /** 8 | * Create diagonal matrix from vector 9 | * 10 | * @param {Array} d 11 | * @returns {Array} 12 | * @example 13 | * 14 | * diag([1, 2]) 15 | * // [[1, 0], 16 | * // [0, 2]] 17 | */ 18 | function diag(d) { 19 | var i, 20 | i1, 21 | j, 22 | n = d.length, 23 | A = Array(n), 24 | Ai; 25 | for (i = n - 1; i >= 0; i--) { 26 | Ai = Array(n); 27 | i1 = i + 2; 28 | for (j = n - 1; j >= i1; j -= 2) { 29 | Ai[j] = 0; 30 | Ai[j - 1] = 0; 31 | } 32 | if (j > i) { 33 | Ai[j] = 0; 34 | } 35 | Ai[i] = d[i]; 36 | for (j = i - 1; j >= 1; j -= 2) { 37 | Ai[j] = 0; 38 | Ai[j - 1] = 0; 39 | } 40 | if (j === 0) { 41 | Ai[0] = 0; 42 | } 43 | A[i] = Ai; 44 | } 45 | return A; 46 | } -------------------------------------------------------------------------------- /lib/div.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cdiv(m1, m2); 11 | case 'Sparse': 12 | return sdiv(m1, m2); 13 | default: 14 | return div(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _mul = require('./mul'); 31 | 32 | var _mul2 = _interopRequireDefault(_mul); 33 | 34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 35 | 36 | var div = (0, _pointwise2.default)(function (x, y) { 37 | return x / y; 38 | }); 39 | var sdiv = (0, _spPointwise2.default)(function (x, y) { 40 | return x / y; 41 | }); 42 | 43 | function cdiv(x, y) { 44 | if (!(y instanceof _Complex2.default)) y = new _Complex2.default(y); 45 | if (y.im) { 46 | return (0, _mul2.default)(x, y.reciprocal()); 47 | } 48 | if (x.im) { 49 | return new _Complex2.default(div(x.re, y.re), div(x.im, y.re)); 50 | } 51 | return new _Complex2.default(div(x.re, y.re)); 52 | } 53 | 54 | /** 55 | * Pointwise div 56 | * 57 | * @export 58 | * @param {Number|Array|Complex|Sparse} m1 59 | * @param {Number|Array|Complex|Sparse} m2 60 | * @returns {Number|Array|Complex|Sparse} 61 | * @example 62 | * 63 | * div(1, 2) 64 | * // returns 1 / 2 65 | * div([1, 2], [2, 2]) 66 | * // returns [1 / 2, 2 / 2] 67 | * div([[2,1], [1,2]], [[2, 2], [2, 2]])) 68 | * // returns [ [2 / 2, 1 / 2], [1 / 2, 2 / 2] ] 69 | */ -------------------------------------------------------------------------------- /lib/epsilon.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | /** 7 | * 2.220446049250313e-16 8 | * 9 | * @export 10 | */ 11 | exports.default = 2.220446049250313e-16; -------------------------------------------------------------------------------- /lib/eq.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return ceq(m1, m2); 11 | case 'Sparse': 12 | return seq(m1, m2); 13 | default: 14 | return eq(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var eq = (0, _pointwise2.default)(function (x, y) { 29 | return x === y; 30 | }); 31 | var seq = (0, _spPointwise2.default)(function (x, y) { 32 | return x === y; 33 | }); 34 | 35 | function ceq(x, y) { 36 | throw new Error('mathlab.eq: no eq for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise eq 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * eq(1, 2) 49 | * // returns 1 === 2 50 | * eq([1, 2], [2, 2]) 51 | * // returns [1 === 2, 2 === 2] 52 | * eq([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 === 2, 1 === 2], [1 === 2, 2 === 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/exp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cexp(m); 11 | case 'Sparse': 12 | return sexp(m); 13 | default: 14 | return exp(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _cos = require('./cos'); 27 | 28 | var _cos2 = _interopRequireDefault(_cos); 29 | 30 | var _sin = require('./sin'); 31 | 32 | var _sin2 = _interopRequireDefault(_sin); 33 | 34 | var _mul = require('./mul'); 35 | 36 | var _mul2 = _interopRequireDefault(_mul); 37 | 38 | var _Complex = require('./Complex'); 39 | 40 | var _Complex2 = _interopRequireDefault(_Complex); 41 | 42 | var _Sparse = require('./Sparse'); 43 | 44 | var _Sparse2 = _interopRequireDefault(_Sparse); 45 | 46 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 47 | 48 | var exp = (0, _pointwise2.default)(Math.exp); 49 | var sexp = (0, _spPointwise2.default)(Math.exp); 50 | 51 | function cexp(x) { 52 | var ex = exp(x.re); 53 | if (x.im) { 54 | return new _Complex2.default((0, _mul2.default)((0, _cos2.default)(x.im), ex), (0, _mul2.default)((0, _sin2.default)(x.im), ex)); 55 | } 56 | return new _Complex2.default(ex); 57 | } 58 | 59 | /** 60 | * Pointwise Math.exp(x) 61 | * 62 | * @export 63 | * @param {Number|Array|Complex|Sparse} m 64 | * @returns {Number|Array|Complex|Sparse} 65 | * @example 66 | * 67 | * exp(1) 68 | * // returns Math.exp(1) 69 | * exp([1, 2]) 70 | * // returns [Math.exp(1), Math.exp(2)] 71 | * exp([[1,2],[1,3]]) 72 | * // returns [ [Math.exp(1), Math.exp(2)], [Math.exp(1), Math.exp(3)] ] 73 | */ -------------------------------------------------------------------------------- /lib/floor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cfloor(m); 11 | case 'Sparse': 12 | return sfloor(m); 13 | default: 14 | return floor(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var floor = (0, _pointwise2.default)(Math.floor); 33 | var sfloor = (0, _spPointwise2.default)(Math.floor); 34 | 35 | function cfloor(x) { 36 | throw new Error('mathlab.floor: no floor for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.floor(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m 44 | * @returns {Number|Array|Complex|Sparse} 45 | * @example 46 | * 47 | * floor(1) 48 | * // returns Math.floor(1) 49 | * floor([1, 2]) 50 | * // returns [Math.floor(1), Math.floor(2)] 51 | * floor([[1,2],[1,3]]) 52 | * // returns [ [Math.floor(1), Math.floor(2)], [Math.floor(1), Math.floor(3)] ] 53 | */ -------------------------------------------------------------------------------- /lib/geq.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cgeq(m1, m2); 11 | case 'Sparse': 12 | return sgeq(m1, m2); 13 | default: 14 | return geq(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var geq = (0, _pointwise2.default)(function (x, y) { 29 | return x >= y; 30 | }); 31 | var sgeq = (0, _spPointwise2.default)(function (x, y) { 32 | return x >= y; 33 | }); 34 | 35 | function cgeq(x, y) { 36 | throw new Error('mathlab.geq: no geq for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise geq 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * geq(1, 2) 49 | * // returns 1 >= 2 50 | * geq([1, 2], [2, 2]) 51 | * // returns [1 >= 2, 2 >= 2] 52 | * geq([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 >= 2, 1 >= 2], [1 >= 2, 2 >= 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/getBlock.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = getBlock; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 13 | 14 | /** 15 | * Extract a block from a matrix 16 | * 17 | * @export 18 | * @param {Array} x 19 | * @param {Array} from from position 20 | * @param {Array} to to position 21 | * @returns {Array} 22 | * @example 23 | * 24 | * getBlock([[1,2,3], 25 | * [3,4,5]], [0,0], [1,1]) 26 | * // [[1, 2], 27 | * [3, 4]] 28 | */ 29 | function getBlock(x, from, to) { 30 | var s = (0, _dim2.default)(x); 31 | 32 | function foo(x, k) { 33 | var i, 34 | a = from[k], 35 | n = to[k] - a, 36 | ret = Array(n); 37 | if (k === s.length - 1) { 38 | for (i = n; i >= 0; i--) { 39 | ret[i] = x[i + a]; 40 | } 41 | return ret; 42 | } 43 | for (i = n; i >= 0; i--) { 44 | ret[i] = foo(x[i + a], k + 1); 45 | } 46 | return ret; 47 | } 48 | return foo(x, 0); 49 | } -------------------------------------------------------------------------------- /lib/getDiag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (A) { 8 | switch (A.constructor.name) { 9 | case 'Complex': 10 | return cgetDiag(A); 11 | case 'Sparse': 12 | return sgetDiag(A); 13 | default: 14 | return getDiag(A); 15 | } 16 | }; 17 | 18 | var _Complex = require('./Complex'); 19 | 20 | var _Complex2 = _interopRequireDefault(_Complex); 21 | 22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 23 | 24 | function cgetDiag(A) { 25 | // TODO: Check if is matrix 26 | if (A.im) { 27 | return new _Complex2.default(getDiag(A.re), getDiag(A.im)); 28 | } 29 | return new _Complex2.default(getDiag(A.re)); 30 | } 31 | 32 | /** 33 | * Get the diagonal of a matrix 34 | * 35 | * @export 36 | * @param {Array} A 37 | * @returns {Array} 38 | * @example 39 | * 40 | * getDiag([[1, 3], [0, 2]]) 41 | * // [1, 2] 42 | */ 43 | 44 | 45 | function sgetDiag(A) { 46 | throw new Error('mathlab.getDiag: getDiag for sparse matrix not exist'); 47 | } 48 | 49 | function getDiag(A) { 50 | var n = Math.min(A.length, A[0].length), 51 | i, 52 | ret = Array(n); 53 | for (i = n - 1; i >= 1; --i) { 54 | ret[i] = A[i][i]; 55 | --i; 56 | ret[i] = A[i][i]; 57 | } 58 | if (i === 0) { 59 | ret[0] = A[0][0]; 60 | } 61 | return ret; 62 | } -------------------------------------------------------------------------------- /lib/gt.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cgt(m1, m2); 11 | case 'Sparse': 12 | return sgt(m1, m2); 13 | default: 14 | return gt(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var gt = (0, _pointwise2.default)(function (x, y) { 29 | return x > y; 30 | }); 31 | var sgt = (0, _spPointwise2.default)(function (x, y) { 32 | return x > y; 33 | }); 34 | 35 | function cgt(x, y) { 36 | throw new Error('mathlab.gt: no gt for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise gt 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * gt(1, 2) 49 | * // returns 1 > 2 50 | * gt([1, 2], [2, 2]) 51 | * // returns [1 > 2, 2 > 2] 52 | * gt([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 > 2, 1 > 2], [1 > 2, 2 > 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/identity.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = identity; 7 | 8 | var _diag = require('./diag'); 9 | 10 | var _diag2 = _interopRequireDefault(_diag); 11 | 12 | var _rep = require('./rep'); 13 | 14 | var _rep2 = _interopRequireDefault(_rep); 15 | 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 17 | 18 | /** 19 | * Generate identity matrix of given size 20 | * 21 | * @param {Number} n 22 | * @returns {Array} 23 | * @example 24 | * 25 | * identity(2) 26 | * // [[1, 0], [0, 1]] 27 | */ 28 | function identity(n) { 29 | return (0, _diag2.default)((0, _rep2.default)([n], 1)); 30 | } -------------------------------------------------------------------------------- /lib/inv.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = inv; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | var _identity = require('./identity'); 13 | 14 | var _identity2 = _interopRequireDefault(_identity); 15 | 16 | var _clone = require('./clone'); 17 | 18 | var _clone2 = _interopRequireDefault(_clone); 19 | 20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 21 | 22 | /** 23 | * Inverse of an matrix 24 | * 25 | * @export 26 | * @param {Array} x 27 | * @returns {Array} 28 | * @example 29 | * 30 | * inv([[1,2],[3,4]]) 31 | * // [[ -2, 1], 32 | * // [ 1.5, -0.5]] 33 | */ 34 | function inv(x) { 35 | var s = (0, _dim2.default)(x), 36 | abs = Math.abs, 37 | m = s[0], 38 | n = s[1]; 39 | var A = (0, _clone2.default)(x), 40 | Ai, 41 | Aj; 42 | var I = (0, _identity2.default)(m), 43 | Ii, 44 | Ij; 45 | var i, j, k, x; 46 | for (j = 0; j < n; ++j) { 47 | var i0 = -1; 48 | var v0 = -1; 49 | for (i = j; i !== m; ++i) { 50 | k = abs(A[i][j]);if (k > v0) { 51 | i0 = i;v0 = k; 52 | } 53 | } 54 | Aj = A[i0];A[i0] = A[j];A[j] = Aj; 55 | Ij = I[i0];I[i0] = I[j];I[j] = Ij; 56 | x = Aj[j]; 57 | for (k = j; k !== n; ++k) { 58 | Aj[k] /= x; 59 | }for (k = n - 1; k !== -1; --k) { 60 | Ij[k] /= x; 61 | }for (i = m - 1; i !== -1; --i) { 62 | if (i !== j) { 63 | Ai = A[i]; 64 | Ii = I[i]; 65 | x = Ai[j]; 66 | for (k = j + 1; k !== n; ++k) { 67 | Ai[k] -= Aj[k] * x; 68 | }for (k = n - 1; k > 0; --k) { 69 | Ii[k] -= Ij[k] * x;--k;Ii[k] -= Ij[k] * x; 70 | } 71 | if (k === 0) Ii[0] -= Ij[0] * x; 72 | } 73 | } 74 | } 75 | return I; 76 | } -------------------------------------------------------------------------------- /lib/leq.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cleq(m1, m2); 11 | case 'Sparse': 12 | return sleq(m1, m2); 13 | default: 14 | return leq(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var leq = (0, _pointwise2.default)(function (x, y) { 29 | return x <= y; 30 | }); 31 | var sleq = (0, _spPointwise2.default)(function (x, y) { 32 | return x <= y; 33 | }); 34 | 35 | function cleq(x, y) { 36 | throw new Error('mathlab.leq: no leq for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise leq 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * leq(1, 2) 49 | * // returns 1 <= 2 50 | * leq([1, 2], [2, 2]) 51 | * // returns [1 <= 2, 2 <= 2] 52 | * leq([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 <= 2, 1 <= 2], [1 <= 2, 2 <= 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/linspace.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = linspace; 7 | /** 8 | * Generate evenly spaced values 9 | * 10 | * @export 11 | * @param {Number} a 12 | * @param {Number} b 13 | * @param {Number} n 14 | * @returns {Array} 15 | * @example 16 | * 17 | * linspace(1, 2, 3) 18 | * // [1, 1.5, 2] 19 | */ 20 | function linspace(a, b, n) { 21 | if (typeof n === 'undefined') n = Math.max(Math.round(b - a) + 1, 1); 22 | if (n < 2) { 23 | return n === 1 ? [a] : []; 24 | } 25 | var i, 26 | ret = Array(n); 27 | n--; 28 | for (i = n; i >= 0; i--) { 29 | ret[i] = (i * b + (n - i) * a) / n; 30 | } 31 | return ret; 32 | } -------------------------------------------------------------------------------- /lib/log.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return clog(m); 11 | case 'Sparse': 12 | return slog(m); 13 | default: 14 | return log(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | var _Complex = require('./Complex'); 31 | 32 | var _Complex2 = _interopRequireDefault(_Complex); 33 | 34 | var _atan = require('./atan2'); 35 | 36 | var _atan2 = _interopRequireDefault(_atan); 37 | 38 | var _abs = require('./abs'); 39 | 40 | var _abs2 = _interopRequireDefault(_abs); 41 | 42 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 43 | 44 | var log = (0, _pointwise2.default)(Math.log); 45 | var slog = (0, _spPointwise2.default)(Math.log); 46 | 47 | function clog(x) { 48 | if (x.im) { 49 | var theta = new _Complex2.default((0, _atan2.default)(x.im, x.re)), 50 | r = (0, _abs2.default)(x); 51 | return new _Complex2.default(log(r.re), theta.re); 52 | } 53 | return new _Complex2.default(log(x.re)); 54 | } 55 | 56 | /** 57 | * Pointwise Math.log(x) 58 | * 59 | * @export 60 | * @param {Number|Array|Complex|Sparse} m 61 | * @returns {Number|Array|Complex|Sparse} 62 | * @example 63 | * 64 | * log(1) 65 | * // returns Math.log(1) 66 | * log([1, 2]) 67 | * // returns [Math.log(1), Math.log(2)] 68 | * log([[1,2],[1,3]]) 69 | * // returns [ [Math.log(1), Math.log(2)], [Math.log(1), Math.log(3)] ] 70 | */ -------------------------------------------------------------------------------- /lib/lshift.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return clshift(m1, m2); 11 | case 'Sparse': 12 | return slshift(m1, m2); 13 | default: 14 | return lshift(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var lshift = (0, _pointwise2.default)(function (x, y) { 29 | return x << y; 30 | }); 31 | var slshift = (0, _spPointwise2.default)(function (x, y) { 32 | return x << y; 33 | }); 34 | 35 | function clshift(x, y) { 36 | throw new Error('mathlab.lshift: no lshift for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise lshift 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * lshift(1, 2) 49 | * // returns 1 << 2 50 | * lshift([1, 2], [2, 2]) 51 | * // returns [1 << 2, 2 << 2] 52 | * lshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 << 2, 1 << 2], [1 << 2, 2 << 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/lt.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return clt(m1, m2); 11 | case 'Sparse': 12 | return slt(m1, m2); 13 | default: 14 | return lt(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var lt = (0, _pointwise2.default)(function (x, y) { 29 | return x < y; 30 | }); 31 | var slt = (0, _spPointwise2.default)(function (x, y) { 32 | return x < y; 33 | }); 34 | 35 | function clt(x, y) { 36 | throw new Error('mathlab.lt: no lt for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise lt 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * lt(1, 2) 49 | * // returns 1 < 2 50 | * lt([1, 2], [2, 2]) 51 | * // returns [1 < 2, 2 < 2] 52 | * lt([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 < 2, 1 < 2], [1 < 2, 2 < 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/mod.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cmod(m1, m2); 11 | case 'Sparse': 12 | return smod(m1, m2); 13 | default: 14 | return mod(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var mod = (0, _pointwise2.default)(function (x, y) { 29 | return x % y; 30 | }); 31 | var smod = (0, _spPointwise2.default)(function (x, y) { 32 | return x % y; 33 | }); 34 | 35 | function cmod(x, y) { 36 | throw new Error('mathlab.mod: no mod for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise mod 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * mod(1, 2) 49 | * // returns 1 % 2 50 | * mod([1, 2], [2, 2]) 51 | * // returns [1 % 2, 2 % 2] 52 | * mod([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 % 2, 1 % 2], [1 % 2, 2 % 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/neg.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cneg(m); 11 | case 'Sparse': 12 | return sneg(m); 13 | default: 14 | return neg(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _Sparse = require('./Sparse'); 31 | 32 | var _Sparse2 = _interopRequireDefault(_Sparse); 33 | 34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 35 | 36 | var neg = (0, _pointwise2.default)(function (x) { 37 | return -x; 38 | }); 39 | var sneg = (0, _spPointwise2.default)(function (x) { 40 | return -x; 41 | }); 42 | 43 | function cneg(x) { 44 | if (x.im) { 45 | return new _Complex2.default(neg(x.re), neg(x.im)); 46 | } 47 | return new _Complex2.default(neg(x.re)); 48 | } 49 | 50 | /** 51 | * Pointwise Math.neg(x) 52 | * 53 | * @export 54 | * @param {Number|Array|Complex|Sparse} m 55 | * @returns {Number|Array|Complex|Sparse} 56 | * @example 57 | * 58 | * neg(1) 59 | * // returns Math.neg(1) 60 | * neg([1, 2]) 61 | * // returns [Math.neg(1), Math.neg(2)] 62 | * neg([[1,2],[1,3]]) 63 | * // returns [ [Math.neg(1), Math.neg(2)], [Math.neg(1), Math.neg(3)] ] 64 | */ -------------------------------------------------------------------------------- /lib/negtranspose.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = negtranspose; 7 | /** 8 | * neg transpose 9 | * 10 | * @export 11 | * @param {any} x 12 | * @returns 13 | */ 14 | function negtranspose(x) { 15 | var i, 16 | j, 17 | m = x.length, 18 | n = x[0].length, 19 | ret = Array(n), 20 | A0, 21 | A1, 22 | Bj; 23 | for (j = 0; j < n; j++) { 24 | ret[j] = Array(m); 25 | }for (i = m - 1; i >= 1; i -= 2) { 26 | A1 = x[i]; 27 | A0 = x[i - 1]; 28 | for (j = n - 1; j >= 1; --j) { 29 | Bj = ret[j];Bj[i] = -A1[j];Bj[i - 1] = -A0[j]; 30 | --j; 31 | Bj = ret[j];Bj[i] = -A1[j];Bj[i - 1] = -A0[j]; 32 | } 33 | if (j === 0) { 34 | Bj = ret[0];Bj[i] = -A1[0];Bj[i - 1] = -A0[0]; 35 | } 36 | } 37 | if (i === 0) { 38 | A0 = x[0]; 39 | for (j = n - 1; j >= 1; --j) { 40 | ret[j][0] = -A0[j]; 41 | --j; 42 | ret[j][0] = -A0[j]; 43 | } 44 | if (j === 0) { 45 | ret[0][0] = -A0[0]; 46 | } 47 | } 48 | return ret; 49 | } -------------------------------------------------------------------------------- /lib/neq.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cneq(m1, m2); 11 | case 'Sparse': 12 | return sneq(m1, m2); 13 | default: 14 | return neq(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var neq = (0, _pointwise2.default)(function (x, y) { 29 | return x !== y; 30 | }); 31 | var sneq = (0, _spPointwise2.default)(function (x, y) { 32 | return x !== y; 33 | }); 34 | 35 | function cneq(x, y) { 36 | throw new Error('mathlab.neq: no neq for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise neq 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * neq(1, 2) 49 | * // returns 1 !== 2 50 | * neq([1, 2], [2, 2]) 51 | * // returns [1 !== 2, 2 !== 2] 52 | * neq([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 !== 2, 1 !== 2], [1 !== 2, 2 !== 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/norm2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (x, y) { 8 | switch (x.constructor.name) { 9 | case 'Complex': 10 | return cnorm2(x, y); 11 | case 'Sparse': 12 | return snorm2(x, y); 13 | default: 14 | return norm2(x, y); 15 | } 16 | }; 17 | 18 | var _norm2Squared = require('./norm2Squared'); 19 | 20 | var _norm2Squared2 = _interopRequireDefault(_norm2Squared); 21 | 22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 23 | 24 | function cnorm2(x, y) { 25 | if (x.im) { 26 | var f = _norm2Squared2.default; 27 | return Math.sqrt(f(x.re) + f(x.im)); 28 | } 29 | return norm2(x.re); 30 | } 31 | 32 | /** 33 | * Square root of the sum of the squares of the entries of x 34 | * 35 | * @export 36 | * @param {Array|Number} x 37 | * @returns {Number} 38 | * @example 39 | * 40 | * norm2(2) 41 | * // 2 42 | * norm2([2,2]) 43 | * // 2.828 44 | */ 45 | 46 | 47 | function snorm2(x, y) { 48 | throw new Error('mathlab.norm2: norm2 for sparse matrix not exist'); 49 | } 50 | 51 | function norm2(x) { 52 | return Math.sqrt((0, _norm2Squared2.default)(x)); 53 | } -------------------------------------------------------------------------------- /lib/norm2Squared.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; 8 | 9 | exports.default = nrom2Squared; 10 | 11 | var _dim = require("./dim"); 12 | 13 | var _dim2 = _interopRequireDefault(_dim); 14 | 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 16 | 17 | /** 18 | * Sum of the squares of the entries of x 19 | * 20 | * @export 21 | * @param {Array|Number} x 22 | * @returns {Number} 23 | * @example 24 | * 25 | * norm2Squared(2) 26 | * // 4 27 | * norm2Squared([2,2]) 28 | * // 8 29 | */ 30 | function nrom2Squared(x, s, k) { 31 | var accum = 0; 32 | if ((typeof x === "undefined" ? "undefined" : _typeof(x)) !== "object") { 33 | xi = x; 34 | accum += xi * xi;; 35 | return accum; 36 | } 37 | if (typeof s === "undefined") s = (0, _dim2.default)(x); 38 | if (typeof k === "undefined") k = 0; 39 | if (k === s.length - 1) return norm2SquaredV(x); 40 | var xi; 41 | var n = x.length, 42 | i; 43 | for (i = n - 1; i !== -1; --i) { 44 | xi = nrom2Squared(x[i]); 45 | accum += xi * xi;; 46 | } 47 | return accum; 48 | } 49 | 50 | function norm2SquaredV(x) { 51 | var n = x.length; 52 | var i, xi; 53 | var accum = 0;; 54 | for (i = n - 1; i !== -1; --i) { 55 | xi = x[i]; 56 | accum += xi * xi; 57 | } 58 | return accum; 59 | } -------------------------------------------------------------------------------- /lib/not.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cnot(m); 11 | case 'Sparse': 12 | return snot(m); 13 | default: 14 | return not(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var not = (0, _pointwise2.default)(function (x) { 33 | return !x; 34 | }); 35 | var snot = (0, _spPointwise2.default)(function (x) { 36 | return !x; 37 | }); 38 | 39 | function cnot(x) { 40 | throw new Error('mathlab.not: no not for complex number'); 41 | } 42 | 43 | /** 44 | * Pointwise Math.not(x) 45 | * 46 | * @export 47 | * @param {Number|Array|Complex|Sparse} m 48 | * @returns {Number|Array|Complex|Sparse} 49 | * @example 50 | * 51 | * not(1) 52 | * // returns Math.not(1) 53 | * not([1, 2]) 54 | * // returns [Math.not(1), Math.not(2)] 55 | * not([[1,2],[1,3]]) 56 | * // returns [ [Math.not(1), Math.not(2)], [Math.not(1), Math.not(3)] ] 57 | */ -------------------------------------------------------------------------------- /lib/or.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return cor(m1, m2); 11 | case 'Sparse': 12 | return sor(m1, m2); 13 | default: 14 | return or(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var or = (0, _pointwise2.default)(function (x, y) { 29 | return x || y; 30 | }); 31 | var sor = (0, _spPointwise2.default)(function (x, y) { 32 | return x || y; 33 | }); 34 | 35 | function cor(x, y) { 36 | throw new Error('mathlab.or: no or for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise or 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * or(1, 2) 49 | * // returns 1 || 2 50 | * or([1, 2], [2, 2]) 51 | * // returns [1 || 2, 2 || 2] 52 | * or([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 || 2, 1 || 2], [1 || 2, 2 || 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/pointwise.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = pointwise; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 13 | 14 | /** 15 | * Create a pointwise function 16 | * 17 | * @export 18 | * @param {Function} fun 19 | * @returns {Function} 20 | * @example 21 | * 22 | * function _inc (x) { 23 | * return x + 1 24 | * } 25 | * const inc = pointwise(inc) 26 | * inc(1) // 2 27 | * inc([1, 2]) // [2, 3] 28 | * inc([[1, 2], [1, 3]]) // [[2, 3], [2, 4]] 29 | */ 30 | function pointwise(fun) { 31 | return function (m) { 32 | switch ((0, _dim2.default)(m).length) { 33 | case 0: 34 | return fun(m); 35 | case 1: 36 | return m.map(fun); 37 | case 2: 38 | return m.map(function (a) { 39 | return a.map(fun); 40 | }); 41 | default: 42 | throw new Error('mathlab.pointwise: dimension of matrix should <= 2'); 43 | } 44 | }; 45 | } -------------------------------------------------------------------------------- /lib/pointwise2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = pointwise2; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 13 | 14 | /** 15 | * Create a pointwise function of two arguments 16 | * 17 | * @export 18 | * @param {Function} fun 19 | * @returns {Function} 20 | * @example 21 | * 22 | * function _add (x, y) { 23 | * return x + y 24 | * } 25 | * const add = pointwise(_add) 26 | * add(1, 2) // 3 27 | * add([1, 2], [1, 2]) // [2, 4] 28 | * add([[1, 2], [1, 2]], [[1, 2], [1, 2]]) //[[2, 4], [2, 4]] 29 | */ 30 | function pointwise2(fun) { 31 | return function (m1, m2) { 32 | switch ((0, _dim2.default)(m1).length) { 33 | case 0: 34 | return fun(m1, m2); 35 | case 1: 36 | { 37 | if (typeof m2 === 'number') { 38 | return m1.map(function (x) { 39 | return fun(x, m2); 40 | }); 41 | } 42 | return m1.map(function (x, i) { 43 | return fun(x, m2[i]); 44 | }); 45 | } 46 | case 2: 47 | { 48 | if (typeof m2 === 'number') { 49 | return m1.map(function (mm1) { 50 | return mm1.map(function (x) { 51 | return fun(x, m2); 52 | }); 53 | }); 54 | } 55 | return m1.map(function (mm1, i) { 56 | return mm1.map(function (x, j) { 57 | return fun(x, m2[i][j]); 58 | }); 59 | }); 60 | } 61 | 62 | default: 63 | throw new Error('mathlab.pointwise: dimension of given array should <= 2'); 64 | } 65 | }; 66 | } -------------------------------------------------------------------------------- /lib/pow.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | return (0, _pointwise2.default)(Math.pow)(m1, m2); 9 | }; 10 | 11 | var _pointwise = require('./pointwise2'); 12 | 13 | var _pointwise2 = _interopRequireDefault(_pointwise); 14 | 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -------------------------------------------------------------------------------- /lib/random.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = random; 7 | 8 | /** 9 | * Create an Array of random numbers 10 | * 11 | * @export 12 | * @param {Array} size of the random matrix 13 | * @returns {Array} array of random numbers 14 | * @example 15 | * 16 | * random([2, 3]) 17 | * // [[0.05303,0.1537,0.7280], 18 | * [0.3839,0.08818,0.6316]] 19 | */ 20 | function random(s) { 21 | return _random(s, 0); 22 | } 23 | 24 | function _random(s, k) { 25 | var i, 26 | n = s[k], 27 | ret = Array(n), 28 | rnd; 29 | if (k === s.length - 1) { 30 | rnd = Math.random; 31 | for (i = n - 1; i >= 1; i -= 2) { 32 | ret[i] = rnd(); 33 | ret[i - 1] = rnd(); 34 | } 35 | if (i === 0) { 36 | ret[0] = rnd(); 37 | } 38 | return ret; 39 | } 40 | for (i = n - 1; i >= 0; i--) { 41 | ret[i] = _random(s, k + 1); 42 | }return ret; 43 | } -------------------------------------------------------------------------------- /lib/reciprocal.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.reciprocal = reciprocal; 7 | function reciprocal() { 8 | var mul = numeric.mul, 9 | div = numeric.div; 10 | if (this.im) { 11 | var d = numeric.add(mul(this.re, this.re), mul(this.im, this.im)); 12 | return new numeric.Complex(div(this.re, d), div(numeric.neg(this.im), d)); 13 | } 14 | return new Complex(div(1, this.re)); 15 | } -------------------------------------------------------------------------------- /lib/rep.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = rep; 7 | /** 8 | * Create an Array by duplicating values 9 | * 10 | * @param {Array} s size of the Matrix 11 | * @param {Array} v value 12 | * @returns {Array} 13 | * @example 14 | * 15 | * rep([2,3], 0) 16 | * // [[0,0,0], 17 | * [0,0,0]] 18 | * 19 | * rep([3], 5) 20 | * // [5,5,5] 21 | */ 22 | function rep(s, v, k) { 23 | if (typeof k === "undefined") { 24 | k = 0; 25 | } 26 | var n = s[k], 27 | ret = Array(n), 28 | i; 29 | if (k === s.length - 1) { 30 | for (i = n - 2; i >= 0; i -= 2) { 31 | ret[i + 1] = v;ret[i] = v; 32 | } 33 | if (i === -1) { 34 | ret[0] = v; 35 | } 36 | return ret; 37 | } 38 | for (i = n - 1; i >= 0; i--) { 39 | ret[i] = rep(s, v, k + 1); 40 | } 41 | return ret; 42 | } -------------------------------------------------------------------------------- /lib/round.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return cround(m); 11 | case 'Sparse': 12 | return sround(m); 13 | default: 14 | return round(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 23 | 24 | var round = (0, _pointwise2.default)(Math.round); 25 | 26 | function cround(x) { 27 | throw new Error('mathlab.round: no round for complex number'); 28 | } 29 | 30 | function sround(x) { 31 | throw new Error('mathlab.round: round for sparse matrix not exist'); 32 | } 33 | 34 | /** 35 | * Pointwise Math.round(x) 36 | * 37 | * @export 38 | * @param {Number|Array|Complex|Sparse} m 39 | * @returns {Number|Array|Complex|Sparse} 40 | * @example 41 | * 42 | * round(1) 43 | * // returns Math.round(1) 44 | * round([1, 2]) 45 | * // returns [Math.round(1), Math.round(2)] 46 | * round([[1,2],[1,3]]) 47 | * // returns [ [Math.round(1), Math.round(2)], [Math.round(1), Math.round(3)] ] 48 | */ -------------------------------------------------------------------------------- /lib/rrshift.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return crrshift(m1, m2); 11 | case 'Sparse': 12 | return srrshift(m1, m2); 13 | default: 14 | return rrshift(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var rrshift = (0, _pointwise2.default)(function (x, y) { 29 | return x >>> y; 30 | }); 31 | var srrshift = (0, _spPointwise2.default)(function (x, y) { 32 | return x >>> y; 33 | }); 34 | 35 | function crrshift(x, y) { 36 | throw new Error('mathlab.rrshift: no rrshift for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise rrshift 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * rrshift(1, 2) 49 | * // returns 1 >>> 2 50 | * rrshift([1, 2], [2, 2]) 51 | * // returns [1 >>> 2, 2 >>> 2] 52 | * rrshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 >>> 2, 1 >>> 2], [1 >>> 2, 2 >>> 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/rshift.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return crshift(m1, m2); 11 | case 'Sparse': 12 | return srshift(m1, m2); 13 | default: 14 | return rshift(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 27 | 28 | var rshift = (0, _pointwise2.default)(function (x, y) { 29 | return x >> y; 30 | }); 31 | var srshift = (0, _spPointwise2.default)(function (x, y) { 32 | return x >> y; 33 | }); 34 | 35 | function crshift(x, y) { 36 | throw new Error('mathlab.rshift: no rshift for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise rshift 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m1 44 | * @param {Number|Array|Complex|Sparse} m2 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * rshift(1, 2) 49 | * // returns 1 >> 2 50 | * rshift([1, 2], [2, 2]) 51 | * // returns [1 >> 2, 2 >> 2] 52 | * rshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 53 | * // returns [ [2 >> 2, 1 >> 2], [1 >> 2, 2 >> 2] ] 54 | */ -------------------------------------------------------------------------------- /lib/same.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; 8 | 9 | exports.default = same; 10 | /** 11 | * x and y are entrywise identical 12 | * 13 | * @export 14 | * @param {Array} x 15 | * @param {Array} y 16 | * @returns {Boolean} 17 | * @example 18 | * 19 | * same([1,2], [1,2,3]) // false 20 | * same([1, 2], [1, 2]) // true 21 | */ 22 | function same(x, y) { 23 | var i, n; 24 | if (!(x instanceof Array) || !(y instanceof Array)) { 25 | return false; 26 | } 27 | n = x.length; 28 | if (n !== y.length) { 29 | return false; 30 | } 31 | for (i = 0; i < n; i++) { 32 | if (x[i] === y[i]) { 33 | continue; 34 | } 35 | if (_typeof(x[i]) === "object") { 36 | if (!same(x[i], y[i])) return false; 37 | } else { 38 | return false; 39 | } 40 | } 41 | return true; 42 | } -------------------------------------------------------------------------------- /lib/setBlock.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = setBlock; 7 | 8 | var _dim = require('./dim'); 9 | 10 | var _dim2 = _interopRequireDefault(_dim); 11 | 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 13 | 14 | /** 15 | * Set a block of a matrix 16 | * 17 | * @param {Array} x 18 | * @param {Array} from 19 | * @param {Array} to 20 | * @param {Array} B 21 | * @returns {Array} 22 | * @example 23 | * 24 | * setBlock([[1,2,3], [3,4,5]], [0,0], [1,1],[[2,2],[2,2]]) 25 | * // [[2,2,3],[2,2,5]] 26 | */ 27 | function setBlock(x, from, to, B) { 28 | var s = (0, _dim2.default)(x); 29 | function foo(x, y, k) { 30 | var i, 31 | a = from[k], 32 | n = to[k] - a; 33 | if (k === s.length - 1) { 34 | for (i = n; i >= 0; i--) { 35 | x[i + a] = y[i]; 36 | } 37 | } 38 | for (i = n; i >= 0; i--) { 39 | foo(x[i + a], y[i], k + 1); 40 | } 41 | } 42 | foo(x, B, 0); 43 | return x; 44 | } -------------------------------------------------------------------------------- /lib/sin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return csin(m); 11 | case 'Sparse': 12 | return ssin(m); 13 | default: 14 | return sin(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _Sparse = require('./Sparse'); 31 | 32 | var _Sparse2 = _interopRequireDefault(_Sparse); 33 | 34 | var _neg = require('./neg'); 35 | 36 | var _neg2 = _interopRequireDefault(_neg); 37 | 38 | var _exp = require('./exp'); 39 | 40 | var _exp2 = _interopRequireDefault(_exp); 41 | 42 | var _div = require('./div'); 43 | 44 | var _div2 = _interopRequireDefault(_div); 45 | 46 | var _sub = require('./sub'); 47 | 48 | var _sub2 = _interopRequireDefault(_sub); 49 | 50 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 51 | 52 | var sin = (0, _pointwise2.default)(Math.sin); 53 | var ssin = (0, _spPointwise2.default)(Math.sin); 54 | 55 | function csin(x) { 56 | if (x.im) { 57 | return (0, _div2.default)((0, _sub2.default)((0, _exp2.default)(x), (0, _exp2.default)((0, _neg2.default)(x))), new _Complex2.default(0, 2)); 58 | // return x.exp().sub(x.neg().exp()).div(new Complex(0, 2)); 59 | } 60 | return new _Complex2.default(sin(x.re)); 61 | } 62 | 63 | /** 64 | * Pointwise Math.sin(x) 65 | * 66 | * @export 67 | * @param {Number|Array|Complex|Sparse} m 68 | * @returns {Number|Array|Complex|Sparse} 69 | * @example 70 | * 71 | * sin(1) 72 | * // returns Math.sin(1) 73 | * sin([1, 2]) 74 | * // returns [Math.sin(1), Math.sin(2)] 75 | * sin([[1,2],[1,3]]) 76 | * // returns [ [Math.sin(1), Math.sin(2)], [Math.sin(1), Math.sin(3)] ] 77 | */ -------------------------------------------------------------------------------- /lib/spPointwise.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = spPointwise; 7 | 8 | var _Sparse = require('./Sparse'); 9 | 10 | var _Sparse2 = _interopRequireDefault(_Sparse); 11 | 12 | var _pointwise = require('./pointwise'); 13 | 14 | var _pointwise2 = _interopRequireDefault(_pointwise); 15 | 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 17 | 18 | /** 19 | * Create pointwise function for sparse matrix operating on none zero arguments 20 | * 21 | * @export 22 | * @param {Function} fun 23 | * @returns {Function} 24 | * @example 25 | * 26 | * function _inc (x) { 27 | * return x + 1 28 | * } 29 | * const inc = spPointwise(_inc) 30 | * inc(new Sparse([1,0],[0,0])) // new Sparse([2,0],[0,0]) 31 | */ 32 | function spPointwise(fun) { 33 | return function (x) { 34 | return new _Sparse2.default({ 35 | col: x.col.slice(), // copy the array 36 | row: x.row.slice(), 37 | val: x.val.map(fun) 38 | }); 39 | }; 40 | } -------------------------------------------------------------------------------- /lib/spPointwise2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = spPointwise2; 7 | 8 | var _sup = require('./sup'); 9 | 10 | var _sup2 = _interopRequireDefault(_sup); 11 | 12 | var _rep = require('./rep'); 13 | 14 | var _rep2 = _interopRequireDefault(_rep); 15 | 16 | var _Sparse = require('./Sparse'); 17 | 18 | var _Sparse2 = _interopRequireDefault(_Sparse); 19 | 20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 21 | 22 | function spPointwise2(fun) { 23 | return function (X, Y) { 24 | var Xi = X.col, 25 | Xj = X.row, 26 | Xv = X.val; 27 | var Yi = Y.col, 28 | Yj = Y.row, 29 | Yv = Y.val; 30 | var n = Xi.length - 1, 31 | m = Math.max((0, _sup2.default)(Xj), (0, _sup2.default)(Yj)) + 1; 32 | var Zi = (0, _rep2.default)([n + 1], 0), 33 | Zj = [], 34 | Zv = []; 35 | var x = (0, _rep2.default)([m], 0), 36 | y = (0, _rep2.default)([m], 0); 37 | var xk, yk, zk; 38 | var i, 39 | j, 40 | j0, 41 | j1, 42 | k, 43 | p = 0; 44 | for (i = 0; i < n; ++i) { 45 | j0 = Xi[i]; 46 | j1 = Xi[i + 1]; 47 | for (j = j0; j !== j1; ++j) { 48 | k = Xj[j]; 49 | x[k] = 1; 50 | Zj[p] = k; 51 | ++p; 52 | } 53 | j0 = Yi[i]; 54 | j1 = Yi[i + 1]; 55 | for (j = j0; j !== j1; ++j) { 56 | k = Yj[j]; 57 | y[k] = Yv[j]; 58 | if (x[k] === 0) { 59 | Zj[p] = k;++p; 60 | } 61 | } 62 | Zi[i + 1] = p; 63 | j0 = Xi[i]; 64 | j1 = Xi[i + 1]; 65 | for (j = j0; j !== j1; ++j) { 66 | x[Xj[j]] = Xv[j]; 67 | }j0 = Zi[i]; 68 | j1 = Zi[i + 1]; 69 | for (j = j0; j !== j1; ++j) { 70 | k = Zj[j]; 71 | xk = x[k]; 72 | yk = y[k]; 73 | zk = fun(xk, yk); 74 | Zv[j] = zk; 75 | } 76 | j0 = Xi[i]; 77 | j1 = Xi[i + 1]; 78 | for (j = j0; j !== j1; ++j) { 79 | x[Xj[j]] = 0; 80 | }j0 = Yi[i]; 81 | j1 = Yi[i + 1]; 82 | for (j = j0; j !== j1; ++j) { 83 | y[Yj[j]] = 0; 84 | } 85 | } 86 | return new _Sparse2.default({ 87 | col: Zi, 88 | row: Zj, 89 | val: Zv 90 | }); 91 | }; 92 | } -------------------------------------------------------------------------------- /lib/sqrt.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return csqrt(m); 11 | case 'Sparse': 12 | return ssqrt(m); 13 | default: 14 | return sqrt(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var sqrt = (0, _pointwise2.default)(Math.sqrt); 33 | var ssqrt = (0, _spPointwise2.default)(Math.sqrt); 34 | 35 | function csqrt(x) { 36 | throw new Error('mathlab.sqrt: no sqrt for complex number'); 37 | } 38 | 39 | /** 40 | * Pointwise Math.sqrt(x) 41 | * 42 | * @export 43 | * @param {Number|Array|Complex|Sparse} m 44 | * @returns {Number|Array|Complex|Sparse} 45 | * @example 46 | * 47 | * sqrt(1) 48 | * // returns Math.sqrt(1) 49 | * sqrt([1, 2]) 50 | * // returns [Math.sqrt(1), Math.sqrt(2)] 51 | * sqrt([[1,2],[1,3]]) 52 | * // returns [ [Math.sqrt(1), Math.sqrt(2)], [Math.sqrt(1), Math.sqrt(3)] ] 53 | */ -------------------------------------------------------------------------------- /lib/sub.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m1, m2) { 8 | switch (m1.constructor.name) { 9 | case 'Complex': 10 | return csub(m1, m2); 11 | case 'Sparse': 12 | return ssub(m1, m2); 13 | default: 14 | return sub(m1, m2); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise2'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise2'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Complex = require('./Complex'); 27 | 28 | var _Complex2 = _interopRequireDefault(_Complex); 29 | 30 | var _neg = require('./neg'); 31 | 32 | var _neg2 = _interopRequireDefault(_neg); 33 | 34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 35 | 36 | var sub = (0, _pointwise2.default)(function (x, y) { 37 | return x - y; 38 | }); 39 | var ssub = (0, _spPointwise2.default)(function (x, y) { 40 | return x - y; 41 | }); 42 | 43 | function csub(x, y) { 44 | if (!(y instanceof _Complex2.default)) { 45 | y = new _Complex2.default(y); 46 | } 47 | if (x.im) { 48 | if (y.im) { 49 | return new _Complex2.default(sub(x.re, y.re), sub(x.im, y.im)); 50 | } 51 | return new _Complex2.default(sub(x.re, y.re), x.im); 52 | } 53 | if (y.im) { 54 | return new _Complex2.default(sub(x.re, y.re), (0, _neg2.default)(y.im)); 55 | } 56 | return new _Complex2.default(sub(x.re, y.re)); 57 | } 58 | 59 | /** 60 | * Pointwise sub 61 | * 62 | * @export 63 | * @param {Number|Array|Complex|Sparse} m1 64 | * @param {Number|Array|Complex|Sparse} m2 65 | * @returns {Number|Array|Complex|Sparse} 66 | * @example 67 | * 68 | * sub(1, 2) 69 | * // returns 1 - 2 70 | * sub([1, 2], [2, 2]) 71 | * // returns [1 - 2, 2 - 2] 72 | * sub([[2,1], [1,2]], [[2, 2], [2, 2]])) 73 | * // returns [ [2 - 2, 1 - 2], [1 - 2, 2 - 2] ] 74 | */ -------------------------------------------------------------------------------- /lib/sup.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; 8 | 9 | exports.default = sup; 10 | 11 | var _dim = require("./dim"); 12 | 13 | var _dim2 = _interopRequireDefault(_dim); 14 | 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 16 | 17 | function sup(x, s, k) { 18 | var accum = -Infinity, 19 | max = Math.max; 20 | if ((typeof x === "undefined" ? "undefined" : _typeof(x)) !== "object") { 21 | xi = x; 22 | accum = max(accum, xi);; 23 | return accum; 24 | } 25 | if (typeof s === "undefined") s = (0, _dim2.default)(x); 26 | if (typeof k === "undefined") k = 0; 27 | if (k === s.length - 1) return supV(x); 28 | var xi, 29 | n = x.length, 30 | i; 31 | for (i = n - 1; i !== -1; --i) { 32 | xi = arguments.callee(x[i]); 33 | accum = max(accum, xi); 34 | } 35 | return accum; 36 | } 37 | 38 | function supV(x) { 39 | var n = x.length; 40 | var i, xi; 41 | var accum = -Infinity, 42 | max = Math.max;; 43 | for (i = n - 1; i !== -1; --i) { 44 | xi = x[i]; 45 | accum = max(accum, xi);; 46 | } 47 | return accum; 48 | } -------------------------------------------------------------------------------- /lib/tan.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (m) { 8 | switch (m.constructor.name) { 9 | case 'Complex': 10 | return ctan(m); 11 | case 'Sparse': 12 | return stan(m); 13 | default: 14 | return tan(m); 15 | } 16 | }; 17 | 18 | var _pointwise = require('./pointwise'); 19 | 20 | var _pointwise2 = _interopRequireDefault(_pointwise); 21 | 22 | var _spPointwise = require('./spPointwise'); 23 | 24 | var _spPointwise2 = _interopRequireDefault(_spPointwise); 25 | 26 | var _Sparse = require('./Sparse'); 27 | 28 | var _Sparse2 = _interopRequireDefault(_Sparse); 29 | 30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 31 | 32 | var tan = (0, _pointwise2.default)(Math.tan); 33 | var stan = (0, _spPointwise2.default)(Math.tan); 34 | 35 | function ctan(x) { 36 | // TODO 37 | throw new Error('mathlab.tan: no tan for complex number'); 38 | } 39 | 40 | /** 41 | * Pointwise Math.tan(x) 42 | * 43 | * @export 44 | * @param {Number|Array|Complex|Sparse} m 45 | * @returns {Number|Array|Complex|Sparse} 46 | * @example 47 | * 48 | * tan(1) 49 | * // returns Math.tan(1) 50 | * tan([1, 2]) 51 | * // returns [Math.tan(1), Math.tan(2)] 52 | * tan([[1,2],[1,3]]) 53 | * // returns [ [Math.tan(1), Math.tan(2)], [Math.tan(1), Math.tan(3)] ] 54 | */ -------------------------------------------------------------------------------- /lib/tensor.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = tensor; 7 | 8 | var _mul = require('./mul'); 9 | 10 | var _mul2 = _interopRequireDefault(_mul); 11 | 12 | var _dim = require('./dim'); 13 | 14 | var _dim2 = _interopRequireDefault(_dim); 15 | 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 17 | 18 | /** 19 | * Tensor product ret[i][j] = x[i]*y[j] 20 | * 21 | * @export 22 | * @param {Array|Number} x 23 | * @param {Array|Number} y 24 | * @returns {Array|Number} 25 | * @example 26 | * 27 | * tensor([1,2],[3,4]) 28 | * // [[1,4], 29 | * // [6,8]] 30 | */ 31 | function tensor(x, y) { 32 | if (typeof x === "number" || typeof y === "number") return (0, _mul2.default)(x, y); 33 | var s1 = (0, _dim2.default)(x), 34 | s2 = (0, _dim2.default)(y); 35 | if (s1.length !== 1 || s2.length !== 1) { 36 | throw new Error('Mathlab.tensor: tensor product is only defined for vectors'); 37 | } 38 | var m = s1[0], 39 | n = s2[0], 40 | A = Array(m), 41 | Ai, 42 | i, 43 | j, 44 | xi; 45 | for (i = m - 1; i >= 0; i--) { 46 | Ai = Array(n); 47 | xi = x[i]; 48 | for (j = n - 1; j >= 3; --j) { 49 | Ai[j] = xi * y[j]; 50 | --j; 51 | Ai[j] = xi * y[j]; 52 | --j; 53 | Ai[j] = xi * y[j]; 54 | --j; 55 | Ai[j] = xi * y[j]; 56 | } 57 | while (j >= 0) { 58 | Ai[j] = xi * y[j];--j; 59 | } 60 | A[i] = Ai; 61 | } 62 | return A; 63 | } -------------------------------------------------------------------------------- /lib/transpose.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function (x) { 8 | switch (x.constructor.name) { 9 | case 'Complex': 10 | return ctranspose(x); 11 | case 'Sparse': 12 | return stranspose(x); 13 | default: 14 | return transpose(x); 15 | } 16 | }; 17 | 18 | var _Complex = require('./Complex'); 19 | 20 | var _Complex2 = _interopRequireDefault(_Complex); 21 | 22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 23 | 24 | function ctranspose(x) { 25 | // TODO: Check if is matrix 26 | var t = transpose; 27 | if (x.im) { 28 | return new _Complex2.default(t(x.re), t(x.im)); 29 | } 30 | return new _Complex2.default(t(x.re)); 31 | } 32 | 33 | /** 34 | * Matrix transpose 35 | * 36 | * @export 37 | * @param {Array} x 38 | * @returns {Array} 39 | * @example 40 | * 41 | * transpose([[1,2,3],[4,5,6]]) 42 | * //[[1,4], 43 | * // [2,5], 44 | * // [3,6]] 45 | */ 46 | 47 | 48 | function stranspose(x) { 49 | throw new Error('mathlab.transpose: transpose for sparse matrix not exist'); 50 | } 51 | 52 | function transpose(x) { 53 | var i, 54 | j, 55 | m = x.length, 56 | n = x[0].length, 57 | ret = Array(n), 58 | A0, 59 | A1, 60 | Bj; 61 | for (j = 0; j < n; j++) { 62 | ret[j] = Array(m); 63 | }for (i = m - 1; i >= 1; i -= 2) { 64 | A1 = x[i]; 65 | A0 = x[i - 1]; 66 | for (j = n - 1; j >= 1; --j) { 67 | Bj = ret[j];Bj[i] = A1[j];Bj[i - 1] = A0[j]; 68 | --j; 69 | Bj = ret[j];Bj[i] = A1[j];Bj[i - 1] = A0[j]; 70 | } 71 | if (j === 0) { 72 | Bj = ret[0];Bj[i] = A1[0];Bj[i - 1] = A0[0]; 73 | } 74 | } 75 | if (i === 0) { 76 | A0 = x[0]; 77 | for (j = n - 1; j >= 1; --j) { 78 | ret[j][0] = A0[j]; 79 | --j; 80 | ret[j][0] = A0[j]; 81 | } 82 | if (j === 0) { 83 | ret[0][0] = A0[0]; 84 | } 85 | } 86 | return ret; 87 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathlab", 3 | "version": "0.0.14", 4 | "description": "lodash like math lab", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "compile": "babel -d lib/ src/", 8 | "pretest": "npm run compile", 9 | "test": "mocha --recursive --require babel-register", 10 | "genDoc": "./node_modules/esdoc/out/src/ESDocCLI.js -c esdoc.json", 11 | "build:bundle": "browserify app.js -o dist/mathlab.js", 12 | "build:uglify": "uglifyjs dist/mathlab.js -mc --source-map dist/mathlab.min.js.map -o dist/mathlab.min.js", 13 | "build":"npm run build:bundle && npm run build:uglify", 14 | "prepublish": "npm run test && npm run genDoc && npm run compile && npm run build" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/timqian/mathlab.git" 19 | }, 20 | "keywords": [ 21 | "math", 22 | "javascript" 23 | ], 24 | "author": "timqian", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/timqian/mathlab/issues" 28 | }, 29 | "homepage": "https://github.com/timqian/mathlab#readme", 30 | "devDependencies": { 31 | "babel-preset-es2015": "^6.3.13", 32 | "babel-preset-stage-0": "^6.3.13", 33 | "browserify": "^13.1.0", 34 | "debug": "^2.2.0", 35 | "esdoc": "^0.4.7", 36 | "mocha": "^2.3.4", 37 | "should": "^9.0.2", 38 | "uglify-js": "^2.7.3" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /scripts/generateIndexInSrc.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | 3 | const allFiles = fs.readdirSync('./src/') 4 | let indexContent = '' 5 | allFiles 6 | .filter(filename => { 7 | if(filename === 'index.js') return false 8 | if(filename.includes('.js')) return true 9 | return false 10 | }) 11 | .forEach(filename => { 12 | // TODO: check if js file 13 | const functionName = filename.slice(0, -3) 14 | indexContent += `export ${functionName} from './${functionName}'\n` 15 | }) 16 | 17 | fs.writeFileSync('./src/index.js', indexContent) -------------------------------------------------------------------------------- /scripts/generatePointwiseFunctions.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs' 2 | 3 | const mathFuns = [ 4 | 'abs', 'acos', 'asin', 'atan', 5 | 'ceil', 'cos', 'exp', 'floor', 6 | 'log', 'round', 'sin', 'sqrt', 'tan' 7 | ] 8 | 9 | // src 10 | mathFuns.forEach(fun => { 11 | const src = ` 12 | import pointwise from './pointwise' 13 | 14 | /** 15 | * Pointwise Math.${fun}(x) 16 | * 17 | * @export 18 | * @param {Number|Array} m 19 | * @returns {Number|Array} 20 | * @example 21 | * 22 | * ${fun}(1) 23 | * // returns Math.${fun}(1) 24 | * ${fun}([1, 2]) 25 | * // returns [Math.${fun}(1), Math.${fun}(2)] 26 | * ${fun}([[1,2],[1,3]]) 27 | * // returns [ [Math.${fun}(1), Math.${fun}(2)], [Math.${fun}(1), Math.${fun}(3)] ] 28 | */ 29 | export default function (m) { 30 | return pointwise(Math.${fun})(m) 31 | } 32 | ` 33 | 34 | const test = ` 35 | import assert from 'assert' 36 | import should from 'should' 37 | import { ${fun} } from '../lib' 38 | 39 | describe('${fun}', () => { 40 | it('', () => { 41 | ${fun}(1).should.equal(Math.${fun}(1)) 42 | ${fun}([1, 2]).should.deepEqual([Math.${fun}(1), Math.${fun}(2)]) 43 | ${fun}([[1,2],[1,3]]).should.deepEqual([ [Math.${fun}(1), Math.${fun}(2)], [Math.${fun}(1), Math.${fun}(3)] ]) 44 | }) 45 | }) 46 | ` 47 | 48 | fs.writeFileSync(`./src/${fun}.js`, src) 49 | fs.writeFileSync(`./test/${fun}.js`, test) 50 | }) -------------------------------------------------------------------------------- /scripts/generatePointwiseFunctions2.js: -------------------------------------------------------------------------------- 1 | 2 | import fs from 'fs' 3 | 4 | const mathFuns2 = ['atan2','pow'] 5 | 6 | mathFuns2.forEach(fun => { 7 | const src = ` 8 | import pointwise from './pointwise' 9 | 10 | /** 11 | * Pointwise Math.${fun}(x, y) 12 | * 13 | * @export 14 | * @param {Number|Array} m1 15 | * @param {Number|Array} m2 16 | * @returns {Number|Array} 17 | * @example 18 | * 19 | * ${fun}(1, 2) 20 | * // returns Math.${fun}(1, 2) 21 | * ${fun}([1, 2], [2, 2]) 22 | * // returns [Math.${fun}(1, 2), Math.${fun}(2, 2)] 23 | * ${fun}([[2,1], [1,2]], [[2, 2], [2, 2]])) 24 | * // returns [ [Math.${fun}(2, 2), Math.${fun}(1, 2)], [Math.${fun}(1, 2), Math.${fun}(2, 2)] ] 25 | */ 26 | export default function (m1, m2) { 27 | return pointwise(Math.${fun})(m1, m2) 28 | } 29 | ` 30 | const test = ` 31 | import assert from 'assert' 32 | import should from 'should' 33 | import { ${fun} } from '../lib' 34 | 35 | describe('${fun}', () => { 36 | it('', () => { 37 | ${fun}(1, 2).should.equal(Math.${fun}(1, 2)) 38 | ${fun}([1, 2], [2, 2]).should.deepEqual([Math.${fun}(1, 2), Math.${fun}(2, 2)]) 39 | ${fun}([[2,1], [1,2]], [[2, 2], [2, 2]]) 40 | .should.deepEqual( 41 | [ [Math.${fun}(2, 2), Math.${fun}(1, 2)], [Math.${fun}(1, 2), Math.${fun}(2, 2)] ] 42 | ) 43 | }) 44 | }) 45 | ` 46 | fs.writeFileSync(`./src/${fun}.js`, src) 47 | fs.writeFileSync(`./test/${fun}.js`, test) 48 | }) -------------------------------------------------------------------------------- /scripts/generatePointwiseOps1.js: -------------------------------------------------------------------------------- 1 | 2 | import fs from 'fs' 3 | 4 | const ops1 = { 5 | neg: '-', 6 | not: '!', 7 | bnot: '~', 8 | clone: '' 9 | } 10 | 11 | Object.keys(ops1).forEach(key => { 12 | const op = ops1[key] 13 | const src = ` 14 | import pointwise from './pointwise' 15 | 16 | /** 17 | * Pointwise ${key} 18 | * 19 | * @export 20 | * @param {Number|Array} m 21 | * @returns {Number|Array} 22 | * @example 23 | * 24 | * ${key}(2) 25 | * // returns ${op}2 26 | * ${key}([1, 2]) 27 | * // returns [${op}1, ${op}2] 28 | * ${key}([[1, 2], [2, 2]])) 29 | * // returns [ [${op}1, ${op}2], [${op}2, ${op}2] ] 30 | */ 31 | export default function (m) { 32 | return pointwise(x => ${op}x)(m) 33 | } 34 | ` 35 | const test = ` 36 | import assert from 'assert' 37 | import should from 'should' 38 | import { ${key} } from '../lib' 39 | 40 | describe('${key}', () => { 41 | it('', () => { 42 | ${key}(1).should.equal(${op}1) 43 | ${key}([1, 2]).should.deepEqual([${op}1, ${op}2]) 44 | ${key}([[2,1], [1,2]]) 45 | .should.deepEqual( 46 | [ [${op} 2, ${op} 1], [${op} 1, ${op} 2] ] 47 | ) 48 | }) 49 | }) 50 | ` 51 | 52 | fs.writeFileSync(`./src/${key}.js`, src) 53 | fs.writeFileSync(`./test/${key}.js`, test) 54 | }) -------------------------------------------------------------------------------- /scripts/generatePointwiseOps2.js: -------------------------------------------------------------------------------- 1 | 2 | import fs from 'fs' 3 | 4 | const ops2 = { 5 | add: '+', 6 | sub: '-', 7 | mul: '*', 8 | div: '/', 9 | mod: '%', 10 | and: '&&', 11 | or: '||', 12 | eq: '===', 13 | neq: '!==', 14 | lt: '<', 15 | gt: '>', 16 | leq: '<=', 17 | geq: '>=', 18 | band: '&', 19 | bor: '|', 20 | bxor: '^', 21 | lshift: '<<', 22 | rshift: '>>', 23 | rrshift: '>>>' 24 | } 25 | 26 | Object.keys(ops2).forEach(key => { 27 | const op = ops2[key] 28 | const src = ` 29 | import pointwise from './pointwise' 30 | 31 | /** 32 | * Pointwise ${key} 33 | * 34 | * @export 35 | * @param {Number|Array} m1 36 | * @param {Number|Array} m2 37 | * @returns {Number|Array} 38 | * @example 39 | * 40 | * ${key}(1, 2) 41 | * // returns 1 ${op} 2 42 | * ${key}([1, 2], [2, 2]) 43 | * // returns [1 ${op} 2, 2 ${op} 2] 44 | * ${key}([[2,1], [1,2]], [[2, 2], [2, 2]])) 45 | * // returns [ [2 ${op} 2, 1 ${op} 2], [1 ${op} 2, 2 ${op} 2] ] 46 | */ 47 | export default function (m1, m2) { 48 | return pointwise((x, y) => x ${op} y)(m1, m2) 49 | } 50 | ` 51 | const test = ` 52 | import assert from 'assert' 53 | import should from 'should' 54 | import { ${key} } from '../lib' 55 | 56 | describe('${key}', () => { 57 | it('', () => { 58 | ${key}(1, 2).should.equal(1 ${op} 2) 59 | ${key}([1, 2], [2, 2]).should.deepEqual([1 ${op} 2, 2 ${op} 2]) 60 | ${key}([[2,1], [1,2]], [[2, 2], [2, 2]]) 61 | .should.deepEqual( 62 | [ [2 ${op} 2, 1 ${op} 2], [1 ${op} 2, 2 ${op} 2] ] 63 | ) 64 | }) 65 | }) 66 | ` 67 | 68 | fs.writeFileSync(`./src/${key}.js`, src) 69 | fs.writeFileSync(`./test/${key}.js`, test) 70 | }) -------------------------------------------------------------------------------- /scripts/generateTestTemplates.js: -------------------------------------------------------------------------------- 1 | // generate unexisted test templates 2 | 3 | import fs from 'fs'; 4 | 5 | const allFiles = fs.readdirSync('./src/') 6 | const existedFiles = fs.readdirSync('./test/') 7 | const newFiles = allFiles.filter(file => !existedFiles.includes(file)) 8 | 9 | newFiles.forEach(fileName => { 10 | const functionName = fileName.slice(0, -3) 11 | const data = 12 | `import assert from 'assert' 13 | import should from 'should' 14 | import { ${functionName} } from '../lib' 15 | 16 | describe('${functionName}', () => { 17 | it('', () => { 18 | ${functionName}().should.deepEqual() 19 | }) 20 | })` 21 | 22 | fs.writeFileSync(`./test/${fileName}`, data) 23 | }) -------------------------------------------------------------------------------- /scripts/src/dotMMbig.js: -------------------------------------------------------------------------------- 1 | import dotVV from './dotVV' 2 | 3 | export default function dotMMbig (x, y) { 4 | var p = y.length, v = Array(p) 5 | var m = x.length, n = y[0].length, A = new Array(m), xj 6 | var VV = dotVV 7 | var i,j,k,z 8 | --p 9 | --m 10 | for (i = m;i !== -1;--i) A[i] = Array(n) 11 | --n 12 | for (i = n;i !== -1;--i) { 13 | _getCol(y, i, v) 14 | for (j = m;j !== -1;--j) { 15 | z = 0 16 | xj = x[j] 17 | A[j][i] = VV(xj, v) 18 | } 19 | } 20 | return A 21 | } 22 | 23 | function _getCol (A, j, x) { 24 | var n = A.length, i 25 | for (i = n - 1;i > 0;--i) { 26 | x[i] = A[i][j] 27 | --i 28 | x[i] = A[i][j] 29 | } 30 | if (i === 0) x[0] = A[0][j] 31 | } -------------------------------------------------------------------------------- /scripts/src/dotMMsmall.js: -------------------------------------------------------------------------------- 1 | export default function dotMMsmall(x,y) { 2 | var i,j,k,p,q,r,ret,foo,bar,woo,i0,k0,p0,r0; 3 | p = x.length; q = y.length; r = y[0].length; 4 | ret = Array(p); 5 | for(i=p-1;i>=0;i--) { 6 | foo = Array(r); 7 | bar = x[i]; 8 | for(k=r-1;k>=0;k--) { 9 | woo = bar[q-1]*y[q-1][k]; 10 | for(j=q-2;j>=1;j-=2) { 11 | i0 = j-1; 12 | woo += bar[j]*y[j][k] + bar[i0]*y[i0][k]; 13 | } 14 | if(j===0) { woo += bar[0]*y[0][k]; } 15 | foo[k] = woo; 16 | } 17 | ret[i] = foo; 18 | } 19 | return ret; 20 | } -------------------------------------------------------------------------------- /scripts/src/dotMV.js: -------------------------------------------------------------------------------- 1 | import dotVV from './dotVV'; 2 | 3 | export default function dotMV(x,y) { 4 | var p = x.length, q = y.length,i; 5 | var ret = Array(p); 6 | for(i=p-1;i>=0;i--) { ret[i] = dotVV(x[i],y); } 7 | return ret; 8 | } -------------------------------------------------------------------------------- /scripts/src/dotVM.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Matrix product between vector and matrix 3 | * 4 | * @param {Array} x 5 | * @param {Array} y 6 | * @returns Array 7 | * @example 8 | */ 9 | export default function dotVM (x, y) { 10 | var i,j,k,p,q,r,ret,foo,bar,woo,i0,k0,p0,r0,s1,s2,s3,baz,accum 11 | p = x.length; q = y[0].length 12 | ret = Array(q) 13 | for (k = q - 1;k >= 0;k--) { 14 | woo = x[p - 1] * y[p - 1][k] 15 | for (j = p - 2;j >= 1;j -= 2) { 16 | i0 = j - 1 17 | woo += x[j] * y[j][k] + x[i0] * y[i0][k] 18 | } 19 | if (j === 0) { woo += x[0] * y[0][k]; } 20 | ret[k] = woo 21 | } 22 | return ret 23 | } 24 | -------------------------------------------------------------------------------- /scripts/src/dotVV.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Matrix product between vectors 3 | * 4 | * @export 5 | * @param {Array} x 6 | * @param {Array} y 7 | * @returns Number 8 | * @example 9 | * 10 | * dotVV([1, 2], [2, 1]) 11 | * // 4 12 | */ 13 | export default function dotVV (x, y) { 14 | var i,n = x.length,i1,ret = x[n - 1] * y[n - 1] 15 | for (i = n - 2; i >= 1; i -= 2) { 16 | i1 = i - 1 17 | ret += x[i] * y[i] + x[i1] * y[i1] 18 | } 19 | if (i === 0) { ret += x[0] * y[0]; } 20 | return ret 21 | } 22 | -------------------------------------------------------------------------------- /scripts/test/dotMMbig.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dotMMbig } from '../lib' 4 | 5 | describe('dotMMbig', () => { 6 | it('', () => { 7 | dotMMbig().should.deepEqual() 8 | }) 9 | }) -------------------------------------------------------------------------------- /scripts/test/dotMMsmall.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dotMMsmall } from '../lib' 4 | 5 | describe('dotMMsmall', () => { 6 | it('', () => { 7 | dotMMsmall().should.deepEqual() 8 | }) 9 | }) -------------------------------------------------------------------------------- /scripts/test/dotMV.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dotMV } from '../lib' 4 | 5 | describe('dotMV', () => { 6 | it('', () => { 7 | dotMV().should.deepEqual() 8 | }) 9 | }) -------------------------------------------------------------------------------- /scripts/test/dotVM.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dotVM } from '../lib' 4 | 5 | describe('dotVM', () => { 6 | it('', () => { 7 | dotVM().should.deepEqual() 8 | }) 9 | }) -------------------------------------------------------------------------------- /scripts/test/dotVV.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dotVV } from '../lib' 4 | 5 | describe('dotVV', () => { 6 | it('', () => { 7 | dotVV([1, 2], [2, 1]).should.deepEqual(4) 8 | }) 9 | }) -------------------------------------------------------------------------------- /src/Sparse.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | import rep from './rep' 3 | 4 | export default class Sparse { 5 | constructor(A) { 6 | if(!Array.isArray(A) && A.col){ 7 | this.col = A.col; 8 | this.row = A.row; 9 | this.val = A.val; 10 | } else { 11 | // TODO: throw error while A is not matrix 12 | var m = A.length, n, foo, i, j, counts = []; 13 | for (i = m - 1; i !== -1; --i) { 14 | foo = A[i]; 15 | for (j in foo) { 16 | j = parseInt(j); 17 | while (j >= counts.length) counts[counts.length] = 0; 18 | if (foo[j] !== 0) counts[j]++; 19 | } 20 | } 21 | var n = counts.length; 22 | var Ai = Array(n + 1); 23 | Ai[0] = 0; 24 | for (i = 0; i < n; ++i) Ai[i + 1] = Ai[i] + counts[i]; 25 | var Aj = Array(Ai[n]), Av = Array(Ai[n]); 26 | for (i = m - 1; i !== -1; --i) { 27 | foo = A[i]; 28 | for (j in foo) { 29 | if (foo[j] !== 0) { 30 | counts[j]--; 31 | Aj[Ai[j] + counts[j]] = i; 32 | Av[Ai[j] + counts[j]] = foo[j]; 33 | } 34 | } 35 | } 36 | 37 | this.col = Ai; 38 | this.row = Aj; 39 | this.val = Av; 40 | } 41 | } 42 | 43 | toFull() { 44 | var Ai = this.col, Aj = this.row, Av = this.val, 45 | s = dim(this), m = s[0], n = s[1], i,j,j0,j1,k; 46 | var B = rep([m,n],0); 47 | for(i=0;i=1;i-=2) { 60 | // i1 = i-1; 61 | // ret[i] = []; ret[i][i] = d[i]; 62 | // ret[i1] = []; ret[i1][i1] = d[i1]; 63 | // } 64 | // if(i===0) { ret[0] = []; ret[0][0] = d[i]; } 65 | // console.log(ret); 66 | // } 67 | 68 | -------------------------------------------------------------------------------- /src/abs.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Complex from './Complex' 5 | import Sparse from './Sparse' 6 | import mul from './mul' 7 | import sqrt from './sqrt' 8 | import add from './add' 9 | 10 | 11 | const abs = pointwise(Math.abs) 12 | const sabs = spPointwise(Math.abs) 13 | 14 | function cabs(x) { 15 | if (x.im) { 16 | return new Complex(sqrt(add(mul(x.re, x.re), mul(x.im, x.im)))); 17 | } 18 | return new Complex(abs(x.re)); 19 | } 20 | 21 | /** 22 | * Pointwise Math.abs(x) 23 | * 24 | * @export 25 | * @param {Number|Array|Complex|Sparse} m 26 | * @returns {Number|Array|Complex|Sparse} 27 | * @example 28 | * 29 | * abs(-1) // 1 30 | * abs([1, -2]) // [1, 2] 31 | * abs([[-1,2],[1,-3]]) // [[1,2],[1,3]] 32 | */ 33 | export default function (m) { 34 | switch (m.constructor.name) { 35 | case 'Complex': 36 | return cabs(m); 37 | case 'Sparse': 38 | return sabs(m); 39 | default: 40 | return abs(m); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/acos.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const acos = pointwise(Math.acos) 7 | const sacos = spPointwise(Math.acos) 8 | 9 | function cacos(x) { 10 | throw new Error('mathlab.acos: no acos for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.acos(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Sparse} m 18 | * @returns {Number|Array|Sparse} 19 | * @example 20 | * 21 | * acos(1) // Math.acos(1) 22 | * acos([1, 2]) // [Math.acos(1), Math.acos(2)] 23 | * acos([[1,2],[1,3]]) // [ [Math.acos(1), Math.acos(2)], [Math.acos(1), Math.acos(3)] ] 24 | */ 25 | export default function (m) { 26 | switch (m.constructor.name) { 27 | case 'Complex': 28 | return cacos(m); 29 | case 'Sparse': 30 | return sacos(m); 31 | default: 32 | return acos(m); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/add.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | import Complex from './Complex' 4 | 5 | const add = pointwise2((x, y) => x + y) 6 | const sadd = spPointwise2((x, y) => x + y) 7 | 8 | function cadd(x, y) { 9 | if (!(y instanceof Complex)) { y = new Complex(y); } 10 | if (x.im) { 11 | if (y.im) { 12 | return new Complex(add(x.re, y.re), add(x.im, y.im)); 13 | } 14 | return new Complex(add(x.re, y.re), x.im); 15 | } 16 | if (y.im) { 17 | return new Complex(add(x.re, y.re), y.im); 18 | } 19 | return new Complex(add(x.re, y.re)); 20 | } 21 | 22 | /** 23 | * Pointwise add 24 | * 25 | * @export 26 | * @param {Number|Array|Complex|Sparse} m1 27 | * @param {Number|Array|Complex|Sparse} m2 28 | * @returns {Number|Array|Complex|Sparse} 29 | * @example 30 | * 31 | * add(1, 2) // 3 32 | * add([1, 2], [2, 2]) // [3, 4] 33 | * add([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 + 2, 1 + 2], [1 + 2, 2 + 2] ] 34 | */ 35 | export default function (m1, m2) { 36 | switch (m1.constructor.name) { 37 | case 'Complex': 38 | return cadd(m1, m2); 39 | case 'Sparse': 40 | return sadd(m1, m2); 41 | default: 42 | return add(m1, m2); 43 | } 44 | } -------------------------------------------------------------------------------- /src/and.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const and = pointwise2((x, y) => x && y) 5 | const sand = spPointwise2((x, y) => x && y) 6 | 7 | function cand(x, y) { 8 | throw new Error('mathlab.and: no and for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise and 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * and(1, 0) // 0 21 | * and([1, 1], [1, 0]) // [1, 0] 22 | * and([[1,1], [1,1]], [[1,0], [0,0]])) // [[1,0], [0,0]] 23 | */ 24 | export default function (m1, m2) { 25 | switch (m1.constructor.name) { 26 | case 'Complex': 27 | return cand(m1, m2); 28 | case 'Sparse': 29 | return sand(m1, m2); 30 | default: 31 | return and(m1, m2); 32 | } 33 | } -------------------------------------------------------------------------------- /src/asin.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const asin = pointwise(Math.asin) 7 | const sasin = spPointwise(Math.asin) 8 | 9 | function casin(x) { 10 | throw new Error('mathlab.asin: no asin for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.asin(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Sparse} m 18 | * @returns {Number|Array|Sparse} 19 | * @example 20 | * 21 | * asin(1) // Math.asin(1) 22 | * asin([1, 2]) // [Math.asin(1), Math.asin(2)] 23 | * asin([[1,2],[1,3]]) // [ [Math.asin(1), Math.asin(2)], [Math.asin(1), Math.asin(3)] ] 24 | */ 25 | export default function (m) { 26 | switch (m.constructor.name) { 27 | case 'Complex': 28 | return casin(m); 29 | case 'Sparse': 30 | return sasin(m); 31 | default: 32 | return asin(m); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/atan.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const atan = pointwise(Math.atan) 7 | const satan = spPointwise(Math.atan) 8 | 9 | function catan(x) { 10 | throw new Error('mathlab.atan: no atan for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.atan(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Sparse} m 18 | * @returns {Number|Array|Sparse} 19 | * @example 20 | * 21 | * atan(1) // Math.atan(1) 22 | * atan([1, 2]) // [Math.atan(1), Math.atan(2)] 23 | * atan([[1,2],[1,3]]) // [ [Math.atan(1), Math.atan(2)], [Math.atan(1), Math.atan(3)] ] 24 | */ 25 | export default function (m) { 26 | switch (m.constructor.name) { 27 | case 'Complex': 28 | return catan(m); 29 | case 'Sparse': 30 | return satan(m); 31 | default: 32 | return atan(m); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/atan2.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise2 from './pointwise2' 3 | 4 | /** 5 | * Pointwise Math.atan2(x, y) 6 | * 7 | * @export 8 | * @param {Number|Array|Complex|Sparse} m1 9 | * @param {Number|Array|Complex|Sparse} m2 10 | * @returns {Number|Array|Complex|Sparse} 11 | * @example 12 | * 13 | * atan2(1, 2) // Math.atan2(1, 2) 14 | * atan2([1, 2], [2, 2]) // [Math.atan2(1, 2), Math.atan2(2, 2)] 15 | * atan2([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [Math.atan2(2, 2), Math.atan2(1, 2)], [Math.atan2(1, 2), Math.atan2(2, 2)] ] 16 | */ 17 | export default function (m1, m2) { 18 | // TODO 19 | return pointwise2(Math.atan2)(m1, m2) 20 | } 21 | -------------------------------------------------------------------------------- /src/band.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const band = pointwise2((x, y) => x & y); 5 | const sband = spPointwise2((x, y) => x & y); 6 | 7 | function cband(x, y) { 8 | throw new Error('mathlab.band: no band for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise band 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * band(1, 2) // 1 & 2 21 | * band([1, 2], [2, 2]) // [1 & 2, 2 & 2] 22 | * band([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 & 2, 1 & 2], [1 & 2, 2 & 2] ] 23 | */ 24 | export default function (m1, m2) { 25 | switch (m1.constructor.name) { 26 | case 'Complex': 27 | return cband(m1, m2); 28 | case 'Sparse': 29 | return sband(m1, m2); 30 | default: 31 | return band(m1, m2); 32 | } 33 | } -------------------------------------------------------------------------------- /src/bnot.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const bnot = pointwise(x=> ~x) 7 | const sbnot = spPointwise(x=> ~x) 8 | 9 | function cbnot(x) { 10 | throw new Error('mathlab.bnot: no bnot for complex number') 11 | } 12 | 13 | 14 | /** 15 | * Pointwise Math.bnot(x) 16 | * 17 | * @export 18 | * @param {Number|Array|Sparse} m 19 | * @returns {Number|Array|Sparse} 20 | * @example 21 | * 22 | * bnot(1) // ~1 23 | * bnot([1, 2]) // [~1, ~2] 24 | * bnot([[1,2],[1,3]]) // [[~1,~2],[~1,~3]] 25 | */ 26 | export default function (m) { 27 | switch (m.constructor.name) { 28 | case 'Complex': 29 | return cbnot(m); 30 | case 'Sparse': 31 | return sbnot(m); 32 | default: 33 | return bnot(m); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/bor.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const bor = pointwise2((x, y) => x | y); 5 | const sbor = spPointwise2((x, y) => x | y); 6 | 7 | function cbor(x, y) { 8 | throw new Error('mathlab.bor: no bor for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise bor 13 | * 14 | * @export 15 | * @param {Number|Array|Sparse} m1 16 | * @param {Number|Array|Sparse} m2 17 | * @returns {Number|Array|Sparse} 18 | * @example 19 | * 20 | * bor(1, 2) // 1 | 2 21 | * bor([1, 2], [2, 2]) // [1 | 2, 2 | 2] 22 | * bor([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 | 2, 1 | 2], [1 | 2, 2 | 2] ] 23 | */ 24 | export default function (m1, m2) { 25 | switch (m1.constructor.name) { 26 | case 'Complex': 27 | return cbor(m1, m2); 28 | case 'Sparse': 29 | return sbor(m1, m2); 30 | default: 31 | return bor(m1, m2); 32 | } 33 | } -------------------------------------------------------------------------------- /src/bxor.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const bxor = pointwise2((x, y) => x ^ y) 5 | const sbxor = spPointwise2((x, y) => x ^ y) 6 | 7 | function cbxor(x, y) { 8 | throw new Error('mathlab.bxor: no bxor for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise bxor 13 | * 14 | * @export 15 | * @param {Number|Array|Sparse} m1 16 | * @param {Number|Array|Sparse} m2 17 | * @returns {Number|Array|Sparse} 18 | * @example 19 | * 20 | * bxor(1, 2) // 1 ^ 2 21 | * bxor([1, 2], [2, 2]) // [1 ^ 2, 2 ^ 2] 22 | * bxor([[2,1], [1,2]], [[2, 2], [2, 2]])) // [ [2 ^ 2, 1 ^ 2], [1 ^ 2, 2 ^ 2] ] 23 | */ 24 | export default function (m1, m2) { 25 | switch (m1.constructor.name) { 26 | case 'Complex': 27 | return cbxor(m1, m2); 28 | case 'Sparse': 29 | return sbxor(m1, m2); 30 | default: 31 | return bxor(m1, m2); 32 | } 33 | } -------------------------------------------------------------------------------- /src/ceil.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const ceil = pointwise(Math.ceil) 7 | const sceil = spPointwise(Math.ceil) 8 | 9 | function cceil(x) { 10 | throw new Error('mathlab.ceil: no ceil for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.ceil(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Complex|Sparse} m 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * ceil(1) // Math.ceil(1) 22 | * ceil([1, 2]) // [Math.ceil(1), Math.ceil(2)] 23 | * ceil([[1,2],[1,3]]) // [ [Math.ceil(1), Math.ceil(2)], [Math.ceil(1), Math.ceil(3)] ] 24 | */ 25 | export default function (m) { 26 | switch (m.constructor.name) { 27 | case 'Complex': 28 | return cceil(m); 29 | case 'Sparse': 30 | return sceil(m); 31 | default: 32 | return ceil(m); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/clone.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const clone = pointwise(x => x) 7 | const sclone = spPointwise(x => x) 8 | 9 | function cclone(x) { 10 | throw new Error('mathlab.clone: no clone for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise clone(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Complex|Sparse} m 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * clone([[1,2],[1,3]]) // [[1,2],[1,3]] 22 | */ 23 | export default function (m) { 24 | switch (m.constructor.name) { 25 | case 'Complex': 26 | return cclone(m); 27 | case 'Sparse': 28 | return sclone(m); 29 | default: 30 | return clone(m); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/cos.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Complex from './Complex' 5 | import Sparse from './Sparse' 6 | import neg from './neg' 7 | import exp from './exp' 8 | import div from './div' 9 | import add from './add' 10 | 11 | const cos = pointwise(Math.cos) 12 | const scos = spPointwise(Math.cos) 13 | 14 | function ccos(x) { 15 | if (x.im) { 16 | return div(add(exp(x), exp(neg(x))), 2); 17 | } 18 | return new Complex(cos(x.re)); 19 | } 20 | 21 | /** 22 | * Pointwise Math.cos(x) 23 | * 24 | * @export 25 | * @param {Number|Array|Complex|Sparse} m 26 | * @returns {Number|Array|Complex|Sparse} 27 | * @example 28 | * 29 | * cos(1) 30 | * // returns Math.cos(1) 31 | * cos([1, 2]) 32 | * // returns [Math.cos(1), Math.cos(2)] 33 | * cos([[1,2],[1,3]]) 34 | * // returns [ [Math.cos(1), Math.cos(2)], [Math.cos(1), Math.cos(3)] ] 35 | */ 36 | export default function (m) { 37 | switch (m.constructor.name) { 38 | case 'Complex': 39 | return ccos(m); 40 | case 'Sparse': 41 | return scos(m); 42 | default: 43 | return cos(m); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/det.js: -------------------------------------------------------------------------------- 1 | // Determinant 2 | import dim from './dim' 3 | import clone from './clone' 4 | 5 | /** 6 | * Determinant of matix 7 | * 8 | * @export 9 | * @param {Array} x 10 | * @returns {Number} 11 | * @example 12 | * 13 | * det([[1, 2], [1, 2]]) 14 | * // 0 15 | */ 16 | export default function det (x) { 17 | // TODO 18 | var s = dim(x) 19 | if (s.length !== 2 || s[0] !== s[1]) { throw new Error('det() only works on square matrices'); } 20 | var n = s[0], ret = 1,i,j,k,A = clone(x),Aj,Ai,alpha,temp,k1,k2,k3 21 | for (j = 0;j < n - 1;j++) { 22 | k = j 23 | for (i = j + 1;i < n;i++) { if (Math.abs(A[i][j]) > Math.abs(A[k][j])) { k = i; } } 24 | if (k !== j) { 25 | temp = A[k]; A[k] = A[j]; A[j] = temp 26 | ret *= -1 27 | } 28 | Aj = A[j] 29 | for (i = j + 1;i < n;i++) { 30 | Ai = A[i] 31 | alpha = Ai[j] / Aj[j] 32 | for (k = j + 1;k < n - 1;k += 2) { 33 | k1 = k + 1 34 | Ai[k] -= Aj[k] * alpha 35 | Ai[k1] -= Aj[k1] * alpha 36 | } 37 | if (k !== n) { Ai[k] -= Aj[k] * alpha; } 38 | } 39 | if (Aj[j] === 0) { return 0; } 40 | ret *= Aj[j] 41 | } 42 | return ret * A[j][j] 43 | } 44 | -------------------------------------------------------------------------------- /src/diag.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Create diagonal matrix from vector 3 | * 4 | * @param {Array} d 5 | * @returns {Array} 6 | * @example 7 | * 8 | * diag([1, 2]) 9 | * // [[1, 0], 10 | * // [0, 2]] 11 | */ 12 | export default function diag (d) { 13 | var i, i1, j, n = d.length, A = Array(n), Ai 14 | for (i = n - 1; i >= 0; i--) { 15 | Ai = Array(n) 16 | i1 = i + 2 17 | for (j = n - 1; j >= i1; j -= 2) { 18 | Ai[j] = 0 19 | Ai[j - 1] = 0 20 | } 21 | if (j > i) { Ai[j] = 0; } 22 | Ai[i] = d[i] 23 | for (j = i - 1; j >= 1; j -= 2) { 24 | Ai[j] = 0 25 | Ai[j - 1] = 0 26 | } 27 | if (j === 0) { Ai[0] = 0; } 28 | A[i] = Ai 29 | } 30 | return A 31 | } 32 | -------------------------------------------------------------------------------- /src/dim.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get Array dimensions 3 | * 4 | * @export 5 | * @param {Array} x 6 | * @returns {Array} 7 | * @example 8 | * 9 | * dim([[1, 2, 3], [1, 2, 2]]) 10 | * // [2, 3] 11 | */ 12 | export default function(x) { 13 | switch (x.constructor.name) { 14 | case 'Complex': 15 | return cdim(x); 16 | case 'Sparse': 17 | return sdim(x); 18 | default: 19 | return dim(x); 20 | } 21 | } 22 | 23 | function cdim(x) { 24 | if (x.re) { 25 | return dim(x.re) 26 | } else { 27 | return dim(x.im) 28 | } 29 | } 30 | 31 | function sdim(x) { 32 | return [x.col.length-1, x.col.length-1]; 33 | } 34 | 35 | 36 | function dim (x) { 37 | if (typeof x === 'object') { 38 | if (typeof x[0] === 'object') { 39 | if (typeof x[0][0] === 'object') { 40 | throw new Error('mathlab: only support two demitional matrix for now') 41 | } 42 | return [x.length, x[0].length] 43 | } 44 | return [x.length] 45 | } 46 | return [] 47 | } 48 | -------------------------------------------------------------------------------- /src/div.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | import Complex from './Complex'; 4 | import mul from './mul'; 5 | 6 | const div = pointwise2((x, y) => x / y) 7 | const sdiv = spPointwise2((x, y) => x / y) 8 | 9 | function cdiv(x, y) { 10 | if (!(y instanceof Complex)) y = new Complex(y); 11 | if (y.im) { 12 | return mul(x, y.reciprocal()); 13 | } 14 | if (x.im) { 15 | return new Complex(div(x.re, y.re), div(x.im, y.re)); } 16 | return new Complex(div(x.re, y.re)); 17 | } 18 | 19 | /** 20 | * Pointwise div 21 | * 22 | * @export 23 | * @param {Number|Array|Complex|Sparse} m1 24 | * @param {Number|Array|Complex|Sparse} m2 25 | * @returns {Number|Array|Complex|Sparse} 26 | * @example 27 | * 28 | * div(1, 2) 29 | * // returns 1 / 2 30 | * div([1, 2], [2, 2]) 31 | * // returns [1 / 2, 2 / 2] 32 | * div([[2,1], [1,2]], [[2, 2], [2, 2]])) 33 | * // returns [ [2 / 2, 1 / 2], [1 / 2, 2 / 2] ] 34 | */ 35 | export default function (m1, m2) { 36 | switch (m1.constructor.name) { 37 | case 'Complex': 38 | return cdiv(m1, m2); 39 | case 'Sparse': 40 | return sdiv(m1, m2); 41 | default: 42 | return div(m1, m2); 43 | } 44 | } -------------------------------------------------------------------------------- /src/epsilon.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 2.220446049250313e-16 3 | * 4 | * @export 5 | */ 6 | export default 2.220446049250313e-16 -------------------------------------------------------------------------------- /src/eq.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const eq = pointwise2((x, y) => x === y); 5 | const seq = spPointwise2((x, y) => x === y); 6 | 7 | function ceq(x, y) { 8 | throw new Error('mathlab.eq: no eq for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise eq 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * eq(1, 2) 21 | * // returns 1 === 2 22 | * eq([1, 2], [2, 2]) 23 | * // returns [1 === 2, 2 === 2] 24 | * eq([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 === 2, 1 === 2], [1 === 2, 2 === 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return ceq(m1, m2); 31 | case 'Sparse': 32 | return seq(m1, m2); 33 | default: 34 | return eq(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/exp.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import cos from './cos' 5 | import sin from './sin' 6 | import mul from './mul' 7 | import Complex from './Complex' 8 | import Sparse from './Sparse' 9 | 10 | const exp = pointwise(Math.exp) 11 | const sexp = spPointwise(Math.exp) 12 | 13 | function cexp(x) { 14 | var ex = exp(x.re); 15 | if (x.im) { 16 | return new Complex(mul(cos(x.im), ex), mul(sin(x.im), ex)); 17 | } 18 | return new Complex(ex); 19 | } 20 | 21 | 22 | /** 23 | * Pointwise Math.exp(x) 24 | * 25 | * @export 26 | * @param {Number|Array|Complex|Sparse} m 27 | * @returns {Number|Array|Complex|Sparse} 28 | * @example 29 | * 30 | * exp(1) 31 | * // returns Math.exp(1) 32 | * exp([1, 2]) 33 | * // returns [Math.exp(1), Math.exp(2)] 34 | * exp([[1,2],[1,3]]) 35 | * // returns [ [Math.exp(1), Math.exp(2)], [Math.exp(1), Math.exp(3)] ] 36 | */ 37 | export default function (m) { 38 | switch (m.constructor.name) { 39 | case 'Complex': 40 | return cexp(m); 41 | case 'Sparse': 42 | return sexp(m); 43 | default: 44 | return exp(m); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/floor.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const floor = pointwise(Math.floor) 7 | const sfloor = spPointwise(Math.floor) 8 | 9 | function cfloor(x) { 10 | throw new Error('mathlab.floor: no floor for complex number') 11 | } 12 | 13 | 14 | /** 15 | * Pointwise Math.floor(x) 16 | * 17 | * @export 18 | * @param {Number|Array|Complex|Sparse} m 19 | * @returns {Number|Array|Complex|Sparse} 20 | * @example 21 | * 22 | * floor(1) 23 | * // returns Math.floor(1) 24 | * floor([1, 2]) 25 | * // returns [Math.floor(1), Math.floor(2)] 26 | * floor([[1,2],[1,3]]) 27 | * // returns [ [Math.floor(1), Math.floor(2)], [Math.floor(1), Math.floor(3)] ] 28 | */ 29 | export default function (m) { 30 | switch (m.constructor.name) { 31 | case 'Complex': 32 | return cfloor(m); 33 | case 'Sparse': 34 | return sfloor(m); 35 | default: 36 | return floor(m); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/geq.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const geq = pointwise2((x, y) => x >= y) 5 | const sgeq = spPointwise2((x, y) => x >= y) 6 | 7 | function cgeq(x, y) { 8 | throw new Error('mathlab.geq: no geq for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise geq 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * geq(1, 2) 21 | * // returns 1 >= 2 22 | * geq([1, 2], [2, 2]) 23 | * // returns [1 >= 2, 2 >= 2] 24 | * geq([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 >= 2, 1 >= 2], [1 >= 2, 2 >= 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return cgeq(m1, m2); 31 | case 'Sparse': 32 | return sgeq(m1, m2); 33 | default: 34 | return geq(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/getBlock.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | 3 | /** 4 | * Extract a block from a matrix 5 | * 6 | * @export 7 | * @param {Array} x 8 | * @param {Array} from from position 9 | * @param {Array} to to position 10 | * @returns {Array} 11 | * @example 12 | * 13 | * getBlock([[1,2,3], 14 | * [3,4,5]], [0,0], [1,1]) 15 | * // [[1, 2], 16 | * [3, 4]] 17 | */ 18 | export default function getBlock(x, from, to) { 19 | var s = dim(x); 20 | 21 | function foo(x, k) { 22 | var i, a = from[k], 23 | n = to[k] - a, 24 | ret = Array(n); 25 | if (k === s.length - 1) { 26 | for (i = n; i >= 0; i--) { 27 | ret[i] = x[i + a]; 28 | } 29 | return ret; 30 | } 31 | for (i = n; i >= 0; i--) { 32 | ret[i] = foo(x[i + a], k + 1); 33 | } 34 | return ret; 35 | } 36 | return foo(x, 0); 37 | } -------------------------------------------------------------------------------- /src/getDiag.js: -------------------------------------------------------------------------------- 1 | import Complex from './Complex' 2 | 3 | /** 4 | * Get the diagonal of a matrix 5 | * 6 | * @export 7 | * @param {Array} A 8 | * @returns {Array} 9 | * @example 10 | * 11 | * getDiag([[1, 3], [0, 2]]) 12 | * // [1, 2] 13 | */ 14 | export default function (A) { 15 | switch (A.constructor.name) { 16 | case 'Complex': 17 | return cgetDiag(A); 18 | case 'Sparse': 19 | return sgetDiag(A); 20 | default: 21 | return getDiag(A); 22 | } 23 | } 24 | 25 | function cgetDiag(A) { 26 | // TODO: Check if is matrix 27 | if (A.im) { 28 | return new Complex(getDiag(A.re), getDiag(A.im)); 29 | } 30 | return new Complex(getDiag(A.re)); 31 | } 32 | 33 | function sgetDiag(A) { 34 | throw new Error('mathlab.getDiag: getDiag for sparse matrix not exist') 35 | } 36 | 37 | function getDiag(A) { 38 | var n = Math.min(A.length, A[0].length),i,ret = Array(n) 39 | for (i = n - 1;i >= 1;--i) { 40 | ret[i] = A[i][i] 41 | --i 42 | ret[i] = A[i][i] 43 | } 44 | if (i === 0) { 45 | ret[0] = A[0][0] 46 | } 47 | return ret 48 | } 49 | -------------------------------------------------------------------------------- /src/gt.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const gt = pointwise2((x, y) => x > y); 5 | const sgt = spPointwise2((x, y) => x > y); 6 | 7 | function cgt(x, y) { 8 | throw new Error('mathlab.gt: no gt for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise gt 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * gt(1, 2) 21 | * // returns 1 > 2 22 | * gt([1, 2], [2, 2]) 23 | * // returns [1 > 2, 2 > 2] 24 | * gt([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 > 2, 1 > 2], [1 > 2, 2 > 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return cgt(m1, m2); 31 | case 'Sparse': 32 | return sgt(m1, m2); 33 | default: 34 | return gt(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/identity.js: -------------------------------------------------------------------------------- 1 | import diag from './diag'; 2 | import rep from './rep'; 3 | 4 | /** 5 | * Generate identity matrix of given size 6 | * 7 | * @param {Number} n 8 | * @returns {Array} 9 | * @example 10 | * 11 | * identity(2) 12 | * // [[1, 0], [0, 1]] 13 | */ 14 | export default function identity(n) { 15 | return diag(rep([n], 1)); 16 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export Complex from './Complex' 2 | export Sparse from './Sparse' 3 | export abs from './abs' 4 | export acos from './acos' 5 | export add from './add' 6 | export and from './and' 7 | export asin from './asin' 8 | export atan from './atan' 9 | export atan2 from './atan2' 10 | export band from './band' 11 | export bnot from './bnot' 12 | export bor from './bor' 13 | export bxor from './bxor' 14 | export ceil from './ceil' 15 | export clone from './clone' 16 | export cos from './cos' 17 | export det from './det' 18 | export diag from './diag' 19 | export dim from './dim' 20 | export div from './div' 21 | export dot from './dot' 22 | export eig from './eig' 23 | export epsilon from './epsilon' 24 | export eq from './eq' 25 | export exp from './exp' 26 | export floor from './floor' 27 | export geq from './geq' 28 | export getBlock from './getBlock' 29 | export getDiag from './getDiag' 30 | export gt from './gt' 31 | export identity from './identity' 32 | export inv from './inv' 33 | export leq from './leq' 34 | export linspace from './linspace' 35 | export log from './log' 36 | export lshift from './lshift' 37 | export lt from './lt' 38 | export mod from './mod' 39 | export mul from './mul' 40 | export neg from './neg' 41 | export negtranspose from './negtranspose' 42 | export neq from './neq' 43 | export norm2 from './norm2' 44 | export norm2Squared from './norm2Squared' 45 | export not from './not' 46 | export or from './or' 47 | export pointwise from './pointwise' 48 | export pointwise2 from './pointwise2' 49 | export pow from './pow' 50 | export random from './random' 51 | export reciprocal from './reciprocal' 52 | export rep from './rep' 53 | export round from './round' 54 | export rrshift from './rrshift' 55 | export rshift from './rshift' 56 | export same from './same' 57 | export setBlock from './setBlock' 58 | export sin from './sin' 59 | export sqrt from './sqrt' 60 | export sub from './sub' 61 | export tan from './tan' 62 | export tensor from './tensor' 63 | export transpose from './transpose' 64 | export { fft, ifft } from './fft' -------------------------------------------------------------------------------- /src/inv.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | import identity from './identity' 3 | import clone from './clone' 4 | 5 | /** 6 | * Inverse of an matrix 7 | * 8 | * @export 9 | * @param {Array} x 10 | * @returns {Array} 11 | * @example 12 | * 13 | * inv([[1,2],[3,4]]) 14 | * // [[ -2, 1], 15 | * // [ 1.5, -0.5]] 16 | */ 17 | export default function inv (x) { 18 | var s = dim(x), abs = Math.abs, m = s[0], n = s[1] 19 | var A = clone(x), Ai, Aj 20 | var I = identity(m), Ii, Ij 21 | var i,j,k,x 22 | for (j = 0;j < n;++j) { 23 | var i0 = -1 24 | var v0 = -1 25 | for (i = j;i !== m;++i) { k = abs(A[i][j]); if (k > v0) { i0 = i; v0 = k; } } 26 | Aj = A[i0]; A[i0] = A[j]; A[j] = Aj 27 | Ij = I[i0]; I[i0] = I[j]; I[j] = Ij 28 | x = Aj[j] 29 | for (k = j;k !== n;++k) Aj[k] /= x 30 | for (k = n - 1;k !== -1;--k) Ij[k] /= x 31 | for (i = m - 1;i !== -1;--i) { 32 | if (i !== j) { 33 | Ai = A[i] 34 | Ii = I[i] 35 | x = Ai[j] 36 | for (k = j + 1;k !== n;++k) Ai[k] -= Aj[k] * x 37 | for (k = n - 1;k > 0;--k) { Ii[k] -= Ij[k] * x; --k; Ii[k] -= Ij[k] * x; } 38 | if (k === 0) Ii[0] -= Ij[0] * x 39 | } 40 | } 41 | } 42 | return I 43 | } 44 | -------------------------------------------------------------------------------- /src/leq.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const leq = pointwise2((x, y) => x <= y); 5 | const sleq = spPointwise2((x, y) => x <= y); 6 | 7 | function cleq(x, y) { 8 | throw new Error('mathlab.leq: no leq for complex number') 9 | } 10 | 11 | 12 | /** 13 | * Pointwise leq 14 | * 15 | * @export 16 | * @param {Number|Array|Complex|Sparse} m1 17 | * @param {Number|Array|Complex|Sparse} m2 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * leq(1, 2) 22 | * // returns 1 <= 2 23 | * leq([1, 2], [2, 2]) 24 | * // returns [1 <= 2, 2 <= 2] 25 | * leq([[2,1], [1,2]], [[2, 2], [2, 2]])) 26 | * // returns [ [2 <= 2, 1 <= 2], [1 <= 2, 2 <= 2] ] 27 | */ 28 | export default function (m1, m2) { 29 | switch (m1.constructor.name) { 30 | case 'Complex': 31 | return cleq(m1, m2); 32 | case 'Sparse': 33 | return sleq(m1, m2); 34 | default: 35 | return leq(m1, m2); 36 | } 37 | } -------------------------------------------------------------------------------- /src/linspace.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Generate evenly spaced values 3 | * 4 | * @export 5 | * @param {Number} a 6 | * @param {Number} b 7 | * @param {Number} n 8 | * @returns {Array} 9 | * @example 10 | * 11 | * linspace(1, 2, 3) 12 | * // [1, 1.5, 2] 13 | */ 14 | export default function linspace (a, b, n) { 15 | if (typeof n === 'undefined') n = Math.max(Math.round(b - a) + 1, 1) 16 | if (n < 2) { 17 | return n === 1 ? [a] : [] 18 | } 19 | var i,ret = Array(n) 20 | n-- 21 | for (i = n;i >= 0;i--) { 22 | ret[i] = (i * b + (n - i) * a) / n 23 | } 24 | return ret 25 | } 26 | -------------------------------------------------------------------------------- /src/log.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | import Complex from './Complex' 6 | import atan2 from './atan2' 7 | import abs from './abs' 8 | 9 | const log = pointwise(Math.log) 10 | const slog = spPointwise(Math.log) 11 | 12 | function clog(x) { 13 | if (x.im) { 14 | var theta = new Complex(atan2(x.im, x.re)), 15 | r = abs(x); 16 | return new Complex(log(r.re), theta.re); 17 | } 18 | return new Complex(log(x.re)); 19 | } 20 | 21 | 22 | /** 23 | * Pointwise Math.log(x) 24 | * 25 | * @export 26 | * @param {Number|Array|Complex|Sparse} m 27 | * @returns {Number|Array|Complex|Sparse} 28 | * @example 29 | * 30 | * log(1) 31 | * // returns Math.log(1) 32 | * log([1, 2]) 33 | * // returns [Math.log(1), Math.log(2)] 34 | * log([[1,2],[1,3]]) 35 | * // returns [ [Math.log(1), Math.log(2)], [Math.log(1), Math.log(3)] ] 36 | */ 37 | export default function (m) { 38 | switch (m.constructor.name) { 39 | case 'Complex': 40 | return clog(m); 41 | case 'Sparse': 42 | return slog(m); 43 | default: 44 | return log(m); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/lshift.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const lshift = pointwise2((x, y) => x << y); 5 | const slshift = spPointwise2((x, y) => x << y); 6 | 7 | function clshift(x, y) { 8 | throw new Error('mathlab.lshift: no lshift for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise lshift 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * lshift(1, 2) 21 | * // returns 1 << 2 22 | * lshift([1, 2], [2, 2]) 23 | * // returns [1 << 2, 2 << 2] 24 | * lshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 << 2, 1 << 2], [1 << 2, 2 << 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return clshift(m1, m2); 31 | case 'Sparse': 32 | return slshift(m1, m2); 33 | default: 34 | return lshift(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/lt.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const lt = pointwise2((x, y) => x < y); 5 | const slt = spPointwise2((x, y) => x < y); 6 | 7 | function clt(x, y) { 8 | throw new Error('mathlab.lt: no lt for complex number') 9 | } 10 | 11 | 12 | /** 13 | * Pointwise lt 14 | * 15 | * @export 16 | * @param {Number|Array|Complex|Sparse} m1 17 | * @param {Number|Array|Complex|Sparse} m2 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * lt(1, 2) 22 | * // returns 1 < 2 23 | * lt([1, 2], [2, 2]) 24 | * // returns [1 < 2, 2 < 2] 25 | * lt([[2,1], [1,2]], [[2, 2], [2, 2]])) 26 | * // returns [ [2 < 2, 1 < 2], [1 < 2, 2 < 2] ] 27 | */ 28 | export default function (m1, m2) { 29 | switch (m1.constructor.name) { 30 | case 'Complex': 31 | return clt(m1, m2); 32 | case 'Sparse': 33 | return slt(m1, m2); 34 | default: 35 | return lt(m1, m2); 36 | } 37 | } -------------------------------------------------------------------------------- /src/mod.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const mod = pointwise2((x, y) => x % y); 5 | const smod = spPointwise2((x, y) => x % y); 6 | 7 | function cmod(x, y) { 8 | throw new Error('mathlab.mod: no mod for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise mod 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * mod(1, 2) 21 | * // returns 1 % 2 22 | * mod([1, 2], [2, 2]) 23 | * // returns [1 % 2, 2 % 2] 24 | * mod([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 % 2, 1 % 2], [1 % 2, 2 % 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return cmod(m1, m2); 31 | case 'Sparse': 32 | return smod(m1, m2); 33 | default: 34 | return mod(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/mul.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2'; 2 | import spPointwise2 from './spPointwise2' 3 | import Complex from './Complex'; 4 | import add from './add'; 5 | import sub from './sub'; 6 | 7 | // array mul 8 | const amul = pointwise2((x, y) => x * y) 9 | const smul = spPointwise2((x, y) => x * y) 10 | 11 | // complex array mul 12 | function cmul(x, y) { 13 | if(!(y instanceof Complex)) { y = new Complex(y); } 14 | if (x.im) { 15 | if (y.im) { 16 | return new Complex(sub(amul(x.re, y.re), amul(x.im, y.im)), add(amul(x.re, y.im), amul(x.im, y.re))); 17 | } 18 | return new Complex(amul(x.re, y.re), amul(x.im, y.re)); 19 | } 20 | if (y.im) { 21 | return new Complex(amul(x.re, y.re), amul(x.re, y.im)); 22 | } 23 | return new Complex(amul(x.re, y.re)); 24 | } 25 | 26 | 27 | /** 28 | * Pointwise mul 29 | * 30 | * @export 31 | * @param {Number|Array|Complex|Sparse|Object} m1 32 | * @param {Number|Array|Complex|Sparse|Object} m2 33 | * @returns {Number|Array|Complex|Sparse} 34 | * @example 35 | * 36 | * mul(1, 2) 37 | * // returns 1 * 2 38 | * mul([1, 2], [2, 2]) 39 | * // returns [1 * 2, 2 * 2] 40 | * mul([[2,1], [1,2]], [[2, 2], [2, 2]])) 41 | * // returns [ [2 * 2, 1 * 2], [1 * 2, 2 * 2] ] 42 | */ 43 | export default function (m1, m2) { 44 | // if (m1.constructor.name !== m2.constructor.name) { 45 | // throw new Error('mathlab.mul: argument type mismatch') 46 | // } 47 | 48 | switch (m1.constructor.name) { 49 | case 'Array': 50 | return amul(m1, m2); 51 | case 'Complex': 52 | return cmul(m1, m2); 53 | case 'Sparse': 54 | return smul(m1, m2); 55 | default: 56 | return amul(m1, m2); 57 | } 58 | } -------------------------------------------------------------------------------- /src/neg.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Complex from './Complex' 5 | import Sparse from './Sparse' 6 | 7 | const neg = pointwise(x=> -x) 8 | const sneg = spPointwise(x=> -x) 9 | 10 | function cneg(x) { 11 | if (x.im) { 12 | return new Complex(neg(x.re), neg(x.im)); 13 | } 14 | return new Complex(neg(x.re)); 15 | } 16 | 17 | 18 | /** 19 | * Pointwise Math.neg(x) 20 | * 21 | * @export 22 | * @param {Number|Array|Complex|Sparse} m 23 | * @returns {Number|Array|Complex|Sparse} 24 | * @example 25 | * 26 | * neg(1) 27 | * // returns Math.neg(1) 28 | * neg([1, 2]) 29 | * // returns [Math.neg(1), Math.neg(2)] 30 | * neg([[1,2],[1,3]]) 31 | * // returns [ [Math.neg(1), Math.neg(2)], [Math.neg(1), Math.neg(3)] ] 32 | */ 33 | export default function (m) { 34 | switch (m.constructor.name) { 35 | case 'Complex': 36 | return cneg(m); 37 | case 'Sparse': 38 | return sneg(m); 39 | default: 40 | return neg(m); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/negtranspose.js: -------------------------------------------------------------------------------- 1 | /** 2 | * neg transpose 3 | * 4 | * @export 5 | * @param {any} x 6 | * @returns 7 | */ 8 | export default function negtranspose(x) { 9 | var i,j,m = x.length,n = x[0].length, ret=Array(n),A0,A1,Bj; 10 | for(j=0;j=1;i-=2) { 12 | A1 = x[i]; 13 | A0 = x[i-1]; 14 | for(j=n-1;j>=1;--j) { 15 | Bj = ret[j]; Bj[i] = -A1[j]; Bj[i-1] = -A0[j]; 16 | --j; 17 | Bj = ret[j]; Bj[i] = -A1[j]; Bj[i-1] = -A0[j]; 18 | } 19 | if(j===0) { 20 | Bj = ret[0]; Bj[i] = -A1[0]; Bj[i-1] = -A0[0]; 21 | } 22 | } 23 | if(i===0) { 24 | A0 = x[0]; 25 | for(j=n-1;j>=1;--j) { 26 | ret[j][0] = -A0[j]; 27 | --j; 28 | ret[j][0] = -A0[j]; 29 | } 30 | if(j===0) { ret[0][0] = -A0[0]; } 31 | } 32 | return ret; 33 | } -------------------------------------------------------------------------------- /src/neq.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const neq = pointwise2((x, y) => x !== y) 5 | const sneq = spPointwise2((x, y) => x !== y) 6 | 7 | function cneq(x, y) { 8 | throw new Error('mathlab.neq: no neq for complex number') 9 | } 10 | 11 | 12 | /** 13 | * Pointwise neq 14 | * 15 | * @export 16 | * @param {Number|Array|Complex|Sparse} m1 17 | * @param {Number|Array|Complex|Sparse} m2 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * neq(1, 2) 22 | * // returns 1 !== 2 23 | * neq([1, 2], [2, 2]) 24 | * // returns [1 !== 2, 2 !== 2] 25 | * neq([[2,1], [1,2]], [[2, 2], [2, 2]])) 26 | * // returns [ [2 !== 2, 1 !== 2], [1 !== 2, 2 !== 2] ] 27 | */ 28 | export default function (m1, m2) { 29 | switch (m1.constructor.name) { 30 | case 'Complex': 31 | return cneq(m1, m2); 32 | case 'Sparse': 33 | return sneq(m1, m2); 34 | default: 35 | return neq(m1, m2); 36 | } 37 | } -------------------------------------------------------------------------------- /src/norm2.js: -------------------------------------------------------------------------------- 1 | import norm2Squared from './norm2Squared'; 2 | 3 | 4 | /** 5 | * Square root of the sum of the squares of the entries of x 6 | * 7 | * @export 8 | * @param {Array|Number} x 9 | * @returns {Number} 10 | * @example 11 | * 12 | * norm2(2) 13 | * // 2 14 | * norm2([2,2]) 15 | * // 2.828 16 | */ 17 | export default function (x, y) { 18 | switch (x.constructor.name) { 19 | case 'Complex': 20 | return cnorm2(x, y); 21 | case 'Sparse': 22 | return snorm2(x, y); 23 | default: 24 | return norm2(x, y); 25 | } 26 | } 27 | 28 | function cnorm2(x, y) { 29 | if (x.im) { 30 | var f = norm2Squared; 31 | return Math.sqrt(f(x.re) + f(x.im)); 32 | } 33 | return norm2(x.re); 34 | } 35 | 36 | function snorm2(x, y) { 37 | throw new Error('mathlab.norm2: norm2 for sparse matrix not exist') 38 | } 39 | 40 | function norm2(x) { 41 | return Math.sqrt(norm2Squared(x)); 42 | } -------------------------------------------------------------------------------- /src/norm2Squared.js: -------------------------------------------------------------------------------- 1 | import dim from './dim'; 2 | 3 | /** 4 | * Sum of the squares of the entries of x 5 | * 6 | * @export 7 | * @param {Array|Number} x 8 | * @returns {Number} 9 | * @example 10 | * 11 | * norm2Squared(2) 12 | * // 4 13 | * norm2Squared([2,2]) 14 | * // 8 15 | */ 16 | export default function nrom2Squared(x, s, k) { 17 | var accum = 0; 18 | if (typeof x !== "object") { 19 | xi = x; 20 | accum += xi * xi;; 21 | return accum; 22 | } 23 | if (typeof s === "undefined") s = dim(x); 24 | if (typeof k === "undefined") k = 0; 25 | if (k === s.length - 1) return norm2SquaredV(x); 26 | var xi; 27 | var n = x.length, 28 | i; 29 | for (i = n - 1; i !== -1; --i) { 30 | xi = nrom2Squared(x[i]); 31 | accum += xi * xi;; 32 | } 33 | return accum; 34 | } 35 | 36 | function norm2SquaredV(x) { 37 | var n = x.length; 38 | var i, xi; 39 | var accum = 0;; 40 | for (i = n - 1; i !== -1; --i) { 41 | xi = x[i]; 42 | accum += xi * xi; 43 | } 44 | return accum; 45 | } -------------------------------------------------------------------------------- /src/not.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const not = pointwise(x=> !x) 7 | const snot = spPointwise(x=> !x) 8 | 9 | function cnot(x) { 10 | throw new Error('mathlab.not: no not for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.not(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Complex|Sparse} m 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * not(1) 22 | * // returns Math.not(1) 23 | * not([1, 2]) 24 | * // returns [Math.not(1), Math.not(2)] 25 | * not([[1,2],[1,3]]) 26 | * // returns [ [Math.not(1), Math.not(2)], [Math.not(1), Math.not(3)] ] 27 | */ 28 | export default function (m) { 29 | switch (m.constructor.name) { 30 | case 'Complex': 31 | return cnot(m); 32 | case 'Sparse': 33 | return snot(m); 34 | default: 35 | return not(m); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/or.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const or = pointwise2((x, y) => x || y); 5 | const sor = spPointwise2((x, y) => x || y); 6 | 7 | function cor(x, y) { 8 | throw new Error('mathlab.or: no or for complex number') 9 | } 10 | 11 | 12 | /** 13 | * Pointwise or 14 | * 15 | * @export 16 | * @param {Number|Array|Complex|Sparse} m1 17 | * @param {Number|Array|Complex|Sparse} m2 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * or(1, 2) 22 | * // returns 1 || 2 23 | * or([1, 2], [2, 2]) 24 | * // returns [1 || 2, 2 || 2] 25 | * or([[2,1], [1,2]], [[2, 2], [2, 2]])) 26 | * // returns [ [2 || 2, 1 || 2], [1 || 2, 2 || 2] ] 27 | */ 28 | export default function (m1, m2) { 29 | switch (m1.constructor.name) { 30 | case 'Complex': 31 | return cor(m1, m2); 32 | case 'Sparse': 33 | return sor(m1, m2); 34 | default: 35 | return or(m1, m2); 36 | } 37 | } -------------------------------------------------------------------------------- /src/pointwise.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | 3 | /** 4 | * Create a pointwise function 5 | * 6 | * @export 7 | * @param {Function} fun 8 | * @returns {Function} 9 | * @example 10 | * 11 | * function _inc (x) { 12 | * return x + 1 13 | * } 14 | * const inc = pointwise(inc) 15 | * inc(1) // 2 16 | * inc([1, 2]) // [2, 3] 17 | * inc([[1, 2], [1, 3]]) // [[2, 3], [2, 4]] 18 | */ 19 | export default function pointwise(fun) { 20 | return function(m) { 21 | switch (dim(m).length) { 22 | case 0: 23 | return fun(m) 24 | case 1: 25 | return m.map(fun) 26 | case 2: 27 | return m.map(a => a.map(fun)) 28 | default: 29 | throw new Error('mathlab.pointwise: dimension of matrix should <= 2') 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/pointwise2.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | 3 | /** 4 | * Create a pointwise function of two arguments 5 | * 6 | * @export 7 | * @param {Function} fun 8 | * @returns {Function} 9 | * @example 10 | * 11 | * function _add (x, y) { 12 | * return x + y 13 | * } 14 | * const add = pointwise(_add) 15 | * add(1, 2) // 3 16 | * add([1, 2], [1, 2]) // [2, 4] 17 | * add([[1, 2], [1, 2]], [[1, 2], [1, 2]]) //[[2, 4], [2, 4]] 18 | */ 19 | export default function pointwise2(fun) { 20 | return function(m1, m2) { 21 | switch (dim(m1).length) { 22 | case 0: 23 | return fun(m1, m2) 24 | case 1: 25 | { 26 | if (typeof m2 === 'number') { 27 | return m1.map(x => fun(x, m2)) 28 | } 29 | return m1.map((x, i) => fun(x, m2[i])) 30 | } 31 | case 2: 32 | { 33 | if (typeof m2 === 'number') { 34 | return m1.map(mm1 => mm1.map(x => fun(x, m2))) 35 | } 36 | return m1.map((mm1, i) => mm1.map((x, j) => fun(x, m2[i][j]))) 37 | } 38 | 39 | default: 40 | throw new Error('mathlab.pointwise: dimension of given array should <= 2') 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /src/pow.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise2 from './pointwise2' 3 | 4 | /** 5 | * Pointwise Math.pow(x, y) 6 | * 7 | * @export 8 | * @param {Number|Array|Complex|Sparse} m1 9 | * @param {Number|Array|Complex|Sparse} m2 10 | * @returns {Number|Array|Complex|Sparse} 11 | * @example 12 | * 13 | * pow(1, 2) 14 | * // returns Math.pow(1, 2) 15 | * pow([1, 2], [2, 2]) 16 | * // returns [Math.pow(1, 2), Math.pow(2, 2)] 17 | * pow([[2,1], [1,2]], [[2, 2], [2, 2]])) 18 | * // returns [ [Math.pow(2, 2), Math.pow(1, 2)], [Math.pow(1, 2), Math.pow(2, 2)] ] 19 | */ 20 | export default function (m1, m2) { 21 | return pointwise2(Math.pow)(m1, m2) 22 | } 23 | -------------------------------------------------------------------------------- /src/random.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Create an Array of random numbers 4 | * 5 | * @export 6 | * @param {Array} size of the random matrix 7 | * @returns {Array} array of random numbers 8 | * @example 9 | * 10 | * random([2, 3]) 11 | * // [[0.05303,0.1537,0.7280], 12 | * [0.3839,0.08818,0.6316]] 13 | */ 14 | export default function random(s) { 15 | return _random(s,0); 16 | } 17 | 18 | function _random(s,k) { 19 | var i,n=s[k],ret=Array(n), rnd; 20 | if(k === s.length-1) { 21 | rnd = Math.random; 22 | for(i=n-1;i>=1;i-=2) { 23 | ret[i] = rnd(); 24 | ret[i-1] = rnd(); 25 | } 26 | if(i===0) { ret[0] = rnd(); } 27 | return ret; 28 | } 29 | for(i=n-1;i>=0;i--) ret[i] = _random(s,k+1); 30 | return ret; 31 | } -------------------------------------------------------------------------------- /src/reciprocal.js: -------------------------------------------------------------------------------- 1 | export function reciprocal() { 2 | var mul = numeric.mul, div = numeric.div; 3 | if(this.im) { 4 | var d = numeric.add(mul(this.re,this.re),mul(this.im,this.im)); 5 | return new numeric.Complex(div(this.re,d),div(numeric.neg(this.im),d)); 6 | } 7 | return new Complex(div(1,this.re)); 8 | } -------------------------------------------------------------------------------- /src/rep.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Create an Array by duplicating values 3 | * 4 | * @param {Array} s size of the Matrix 5 | * @param {Array} v value 6 | * @returns {Array} 7 | * @example 8 | * 9 | * rep([2,3], 0) 10 | * // [[0,0,0], 11 | * [0,0,0]] 12 | * 13 | * rep([3], 5) 14 | * // [5,5,5] 15 | */ 16 | export default function rep(s,v,k) { 17 | if(typeof k === "undefined") { k=0; } 18 | var n = s[k], ret = Array(n), i; 19 | if(k === s.length-1) { 20 | for(i=n-2;i>=0;i-=2) { ret[i+1] = v; ret[i] = v; } 21 | if(i===-1) { ret[0] = v; } 22 | return ret; 23 | } 24 | for(i=n-1;i>=0;i--) { ret[i] = rep(s,v,k+1); } 25 | return ret; 26 | } -------------------------------------------------------------------------------- /src/round.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | 4 | const round = pointwise(Math.round) 5 | 6 | function cround(x) { 7 | throw new Error('mathlab.round: no round for complex number') 8 | } 9 | 10 | function sround(x) { 11 | throw new Error('mathlab.round: round for sparse matrix not exist') 12 | } 13 | 14 | /** 15 | * Pointwise Math.round(x) 16 | * 17 | * @export 18 | * @param {Number|Array|Complex|Sparse} m 19 | * @returns {Number|Array|Complex|Sparse} 20 | * @example 21 | * 22 | * round(1) 23 | * // returns Math.round(1) 24 | * round([1, 2]) 25 | * // returns [Math.round(1), Math.round(2)] 26 | * round([[1,2],[1,3]]) 27 | * // returns [ [Math.round(1), Math.round(2)], [Math.round(1), Math.round(3)] ] 28 | */ 29 | export default function (m) { 30 | switch (m.constructor.name) { 31 | case 'Complex': 32 | return cround(m); 33 | case 'Sparse': 34 | return sround(m); 35 | default: 36 | return round(m); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/rrshift.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const rrshift = pointwise2((x, y) => x >>> y) 5 | const srrshift = spPointwise2((x, y) => x >>> y) 6 | 7 | function crrshift(x, y) { 8 | throw new Error('mathlab.rrshift: no rrshift for complex number') 9 | } 10 | 11 | 12 | /** 13 | * Pointwise rrshift 14 | * 15 | * @export 16 | * @param {Number|Array|Complex|Sparse} m1 17 | * @param {Number|Array|Complex|Sparse} m2 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * rrshift(1, 2) 22 | * // returns 1 >>> 2 23 | * rrshift([1, 2], [2, 2]) 24 | * // returns [1 >>> 2, 2 >>> 2] 25 | * rrshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 26 | * // returns [ [2 >>> 2, 1 >>> 2], [1 >>> 2, 2 >>> 2] ] 27 | */ 28 | export default function (m1, m2) { 29 | switch (m1.constructor.name) { 30 | case 'Complex': 31 | return crrshift(m1, m2); 32 | case 'Sparse': 33 | return srrshift(m1, m2); 34 | default: 35 | return rrshift(m1, m2); 36 | } 37 | } -------------------------------------------------------------------------------- /src/rshift.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | 4 | const rshift = pointwise2((x, y) => x >> y) 5 | const srshift = spPointwise2((x, y) => x >> y) 6 | 7 | function crshift(x, y) { 8 | throw new Error('mathlab.rshift: no rshift for complex number') 9 | } 10 | 11 | /** 12 | * Pointwise rshift 13 | * 14 | * @export 15 | * @param {Number|Array|Complex|Sparse} m1 16 | * @param {Number|Array|Complex|Sparse} m2 17 | * @returns {Number|Array|Complex|Sparse} 18 | * @example 19 | * 20 | * rshift(1, 2) 21 | * // returns 1 >> 2 22 | * rshift([1, 2], [2, 2]) 23 | * // returns [1 >> 2, 2 >> 2] 24 | * rshift([[2,1], [1,2]], [[2, 2], [2, 2]])) 25 | * // returns [ [2 >> 2, 1 >> 2], [1 >> 2, 2 >> 2] ] 26 | */ 27 | export default function (m1, m2) { 28 | switch (m1.constructor.name) { 29 | case 'Complex': 30 | return crshift(m1, m2); 31 | case 'Sparse': 32 | return srshift(m1, m2); 33 | default: 34 | return rshift(m1, m2); 35 | } 36 | } -------------------------------------------------------------------------------- /src/same.js: -------------------------------------------------------------------------------- 1 | /** 2 | * x and y are entrywise identical 3 | * 4 | * @export 5 | * @param {Array} x 6 | * @param {Array} y 7 | * @returns {Boolean} 8 | * @example 9 | * 10 | * same([1,2], [1,2,3]) // false 11 | * same([1, 2], [1, 2]) // true 12 | */ 13 | export default function same(x,y) { 14 | var i,n; 15 | if(!(x instanceof Array) || !(y instanceof Array)) { return false; } 16 | n = x.length; 17 | if(n !== y.length) { return false; } 18 | for(i=0;i=0;i--) { x[i+a] = y[i]; } } 21 | for(i=n;i>=0;i--) { foo(x[i+a],y[i],k+1); } 22 | } 23 | foo(x,B,0); 24 | return x; 25 | } -------------------------------------------------------------------------------- /src/sin.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Complex from './Complex' 5 | import Sparse from './Sparse' 6 | import neg from './neg' 7 | import exp from './exp' 8 | import div from './div' 9 | import sub from './sub' 10 | 11 | const sin = pointwise(Math.sin) 12 | const ssin = spPointwise(Math.sin) 13 | 14 | function csin(x) { 15 | if (x.im) { 16 | return div(sub(exp(x), exp(neg(x))), new Complex(0,2)) 17 | // return x.exp().sub(x.neg().exp()).div(new Complex(0, 2)); 18 | } 19 | return new Complex(sin(x.re)); 20 | } 21 | 22 | 23 | /** 24 | * Pointwise Math.sin(x) 25 | * 26 | * @export 27 | * @param {Number|Array|Complex|Sparse} m 28 | * @returns {Number|Array|Complex|Sparse} 29 | * @example 30 | * 31 | * sin(1) 32 | * // returns Math.sin(1) 33 | * sin([1, 2]) 34 | * // returns [Math.sin(1), Math.sin(2)] 35 | * sin([[1,2],[1,3]]) 36 | * // returns [ [Math.sin(1), Math.sin(2)], [Math.sin(1), Math.sin(3)] ] 37 | */ 38 | export default function (m) { 39 | switch (m.constructor.name) { 40 | case 'Complex': 41 | return csin(m); 42 | case 'Sparse': 43 | return ssin(m); 44 | default: 45 | return sin(m); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/spPointwise.js: -------------------------------------------------------------------------------- 1 | 2 | import Sparse from './Sparse' 3 | import pointwise from './pointwise' 4 | 5 | /** 6 | * Create pointwise function for sparse matrix operating on none zero arguments 7 | * 8 | * @export 9 | * @param {Function} fun 10 | * @returns {Function} 11 | * @example 12 | * 13 | * function _inc (x) { 14 | * return x + 1 15 | * } 16 | * const inc = spPointwise(_inc) 17 | * inc(new Sparse([1,0],[0,0])) // new Sparse([2,0],[0,0]) 18 | */ 19 | export default function spPointwise(fun) { 20 | return function (x) { 21 | return new Sparse({ 22 | col: x.col.slice(), // copy the array 23 | row: x.row.slice(), 24 | val: x.val.map(fun), 25 | }) 26 | } 27 | } -------------------------------------------------------------------------------- /src/spPointwise2.js: -------------------------------------------------------------------------------- 1 | import sup from './sup' 2 | import rep from './rep' 3 | import Sparse from './Sparse' 4 | 5 | export default function spPointwise2(fun) { 6 | return function (X, Y) { 7 | var Xi = X.col, Xj = X.row, Xv = X.val; 8 | var Yi = Y.col, Yj = Y.row, Yv = Y.val; 9 | var n = Xi.length - 1, m = Math.max(sup(Xj), sup(Yj)) + 1; 10 | var Zi = rep([n + 1], 0), Zj = [], Zv = []; 11 | var x = rep([m], 0), y = rep([m], 0); 12 | var xk, yk, zk; 13 | var i, j, j0, j1, k, p = 0; 14 | for (i = 0; i < n; ++i) { 15 | j0 = Xi[i]; 16 | j1 = Xi[i + 1]; 17 | for (j = j0; j !== j1; ++j) { 18 | k = Xj[j]; 19 | x[k] = 1; 20 | Zj[p] = k; 21 | ++p; 22 | } 23 | j0 = Yi[i]; 24 | j1 = Yi[i + 1]; 25 | for (j = j0; j !== j1; ++j) { 26 | k = Yj[j]; 27 | y[k] = Yv[j]; 28 | if (x[k] === 0) { Zj[p] = k;++p; } 29 | } 30 | Zi[i + 1] = p; 31 | j0 = Xi[i]; 32 | j1 = Xi[i + 1]; 33 | for (j = j0; j !== j1; ++j) x[Xj[j]] = Xv[j]; 34 | j0 = Zi[i]; 35 | j1 = Zi[i + 1]; 36 | for (j = j0; j !== j1; ++j) { 37 | k = Zj[j]; 38 | xk = x[k]; 39 | yk = y[k]; 40 | zk = fun(xk, yk); 41 | Zv[j] = zk; 42 | } 43 | j0 = Xi[i]; 44 | j1 = Xi[i + 1]; 45 | for (j = j0; j !== j1; ++j) x[Xj[j]] = 0; 46 | j0 = Yi[i]; 47 | j1 = Yi[i + 1]; 48 | for (j = j0; j !== j1; ++j) y[Yj[j]] = 0; 49 | } 50 | return new Sparse({ 51 | col: Zi, 52 | row: Zj, 53 | val: Zv, 54 | }) 55 | } 56 | } -------------------------------------------------------------------------------- /src/sqrt.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const sqrt = pointwise(Math.sqrt) 7 | const ssqrt = spPointwise(Math.sqrt) 8 | 9 | function csqrt(x) { 10 | throw new Error('mathlab.sqrt: no sqrt for complex number') 11 | } 12 | 13 | /** 14 | * Pointwise Math.sqrt(x) 15 | * 16 | * @export 17 | * @param {Number|Array|Complex|Sparse} m 18 | * @returns {Number|Array|Complex|Sparse} 19 | * @example 20 | * 21 | * sqrt(1) 22 | * // returns Math.sqrt(1) 23 | * sqrt([1, 2]) 24 | * // returns [Math.sqrt(1), Math.sqrt(2)] 25 | * sqrt([[1,2],[1,3]]) 26 | * // returns [ [Math.sqrt(1), Math.sqrt(2)], [Math.sqrt(1), Math.sqrt(3)] ] 27 | */ 28 | export default function (m) { 29 | switch (m.constructor.name) { 30 | case 'Complex': 31 | return csqrt(m); 32 | case 'Sparse': 33 | return ssqrt(m); 34 | default: 35 | return sqrt(m); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/sub.js: -------------------------------------------------------------------------------- 1 | import pointwise2 from './pointwise2' 2 | import spPointwise2 from './spPointwise2' 3 | import Complex from './Complex' 4 | import neg from './neg' 5 | 6 | const sub = pointwise2((x, y) => x - y) 7 | const ssub = spPointwise2((x, y) => x - y) 8 | 9 | function csub(x, y) { 10 | if (!(y instanceof Complex)) { y = new Complex(y); } 11 | if (x.im) { 12 | if (y.im) { 13 | return new Complex(sub(x.re, y.re), sub(x.im, y.im)); 14 | } 15 | return new Complex(sub(x.re, y.re), x.im); 16 | } 17 | if (y.im) { 18 | return new Complex(sub(x.re, y.re), neg(y.im)); 19 | } 20 | return new Complex(sub(x.re, y.re)); 21 | } 22 | 23 | 24 | /** 25 | * Pointwise sub 26 | * 27 | * @export 28 | * @param {Number|Array|Complex|Sparse} m1 29 | * @param {Number|Array|Complex|Sparse} m2 30 | * @returns {Number|Array|Complex|Sparse} 31 | * @example 32 | * 33 | * sub(1, 2) 34 | * // returns 1 - 2 35 | * sub([1, 2], [2, 2]) 36 | * // returns [1 - 2, 2 - 2] 37 | * sub([[2,1], [1,2]], [[2, 2], [2, 2]])) 38 | * // returns [ [2 - 2, 1 - 2], [1 - 2, 2 - 2] ] 39 | */ 40 | export default function (m1, m2) { 41 | switch (m1.constructor.name) { 42 | case 'Complex': 43 | return csub(m1, m2); 44 | case 'Sparse': 45 | return ssub(m1, m2); 46 | default: 47 | return sub(m1, m2); 48 | } 49 | } -------------------------------------------------------------------------------- /src/sup.js: -------------------------------------------------------------------------------- 1 | import dim from './dim' 2 | 3 | export default function sup(x, s, k) { 4 | var accum = -Infinity, max = Math.max; 5 | if (typeof x !== "object") { 6 | xi = x; 7 | accum = max(accum, xi);; 8 | return accum; 9 | } 10 | if (typeof s === "undefined") s = dim(x); 11 | if (typeof k === "undefined") k = 0; 12 | if (k === s.length - 1) return supV(x); 13 | var xi, n = x.length, i; 14 | for (i = n - 1; i !== -1; --i) { 15 | xi = arguments.callee(x[i]); 16 | accum = max(accum, xi); 17 | } 18 | return accum; 19 | } 20 | 21 | function supV(x) { 22 | var n = x.length; 23 | var i,xi; 24 | var accum = -Infinity, max = Math.max;; 25 | for(i=n-1;i!==-1;--i) { 26 | xi = x[i]; 27 | accum = max(accum,xi);; 28 | } 29 | return accum; 30 | } 31 | -------------------------------------------------------------------------------- /src/tan.js: -------------------------------------------------------------------------------- 1 | 2 | import pointwise from './pointwise' 3 | import spPointwise from './spPointwise' 4 | import Sparse from './Sparse' 5 | 6 | const tan = pointwise(Math.tan) 7 | const stan = spPointwise(Math.tan) 8 | 9 | function ctan(x) { 10 | // TODO 11 | throw new Error('mathlab.tan: no tan for complex number') 12 | } 13 | 14 | 15 | /** 16 | * Pointwise Math.tan(x) 17 | * 18 | * @export 19 | * @param {Number|Array|Complex|Sparse} m 20 | * @returns {Number|Array|Complex|Sparse} 21 | * @example 22 | * 23 | * tan(1) 24 | * // returns Math.tan(1) 25 | * tan([1, 2]) 26 | * // returns [Math.tan(1), Math.tan(2)] 27 | * tan([[1,2],[1,3]]) 28 | * // returns [ [Math.tan(1), Math.tan(2)], [Math.tan(1), Math.tan(3)] ] 29 | */ 30 | export default function (m) { 31 | switch (m.constructor.name) { 32 | case 'Complex': 33 | return ctan(m); 34 | case 'Sparse': 35 | return stan(m); 36 | default: 37 | return tan(m); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/tensor.js: -------------------------------------------------------------------------------- 1 | import mul from './mul' 2 | import dim from './dim' 3 | 4 | /** 5 | * Tensor product ret[i][j] = x[i]*y[j] 6 | * 7 | * @export 8 | * @param {Array|Number} x 9 | * @param {Array|Number} y 10 | * @returns {Array|Number} 11 | * @example 12 | * 13 | * tensor([1,2],[3,4]) 14 | * // [[1,4], 15 | * // [6,8]] 16 | */ 17 | export default function tensor(x,y) { 18 | if(typeof x === "number" || typeof y === "number") return mul(x,y); 19 | var s1 = dim(x), s2 = dim(y); 20 | if(s1.length !== 1 || s2.length !== 1) { 21 | throw new Error('Mathlab.tensor: tensor product is only defined for vectors'); 22 | } 23 | var m = s1[0], n = s2[0], A = Array(m), Ai, i,j,xi; 24 | for(i=m-1;i>=0;i--) { 25 | Ai = Array(n); 26 | xi = x[i]; 27 | for(j=n-1;j>=3;--j) { 28 | Ai[j] = xi * y[j]; 29 | --j; 30 | Ai[j] = xi * y[j]; 31 | --j; 32 | Ai[j] = xi * y[j]; 33 | --j; 34 | Ai[j] = xi * y[j]; 35 | } 36 | while(j>=0) { Ai[j] = xi * y[j]; --j; } 37 | A[i] = Ai; 38 | } 39 | return A; 40 | } -------------------------------------------------------------------------------- /src/transpose.js: -------------------------------------------------------------------------------- 1 | import Complex from './Complex' 2 | 3 | /** 4 | * Matrix transpose 5 | * 6 | * @export 7 | * @param {Array} x 8 | * @returns {Array} 9 | * @example 10 | * 11 | * transpose([[1,2,3],[4,5,6]]) 12 | * //[[1,4], 13 | * // [2,5], 14 | * // [3,6]] 15 | */ 16 | export default function (x) { 17 | switch (x.constructor.name) { 18 | case 'Complex': 19 | return ctranspose(x); 20 | case 'Sparse': 21 | return stranspose(x); 22 | default: 23 | return transpose(x); 24 | } 25 | } 26 | 27 | function ctranspose(x) { 28 | // TODO: Check if is matrix 29 | var t = transpose; 30 | if (x.im) { 31 | return new Complex(t(x.re), t(x.im)); 32 | } 33 | return new Complex(t(x.re)); 34 | } 35 | 36 | function stranspose(x) { 37 | throw new Error('mathlab.transpose: transpose for sparse matrix not exist') 38 | } 39 | 40 | function transpose (x) { 41 | var i,j,m = x.length,n = x[0].length, ret = Array(n),A0,A1,Bj 42 | for (j = 0;j < n;j++) ret[j] = Array(m) 43 | for (i = m - 1;i >= 1;i -= 2) { 44 | A1 = x[i] 45 | A0 = x[i - 1] 46 | for (j = n - 1;j >= 1;--j) { 47 | Bj = ret[j]; Bj[i] = A1[j]; Bj[i - 1] = A0[j] 48 | --j 49 | Bj = ret[j]; Bj[i] = A1[j]; Bj[i - 1] = A0[j] 50 | } 51 | if (j === 0) { 52 | Bj = ret[0]; Bj[i] = A1[0]; Bj[i - 1] = A0[0] 53 | } 54 | } 55 | if (i === 0) { 56 | A0 = x[0] 57 | for (j = n - 1;j >= 1;--j) { 58 | ret[j][0] = A0[j] 59 | --j 60 | ret[j][0] = A0[j] 61 | } 62 | if (j === 0) { ret[0][0] = A0[0]; } 63 | } 64 | return ret 65 | } 66 | -------------------------------------------------------------------------------- /test/Complex.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import {Complex} from '../lib' 5 | 6 | describe('Complex', () => { 7 | }) 8 | -------------------------------------------------------------------------------- /test/Sparse.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { Sparse } from '../lib' 4 | 5 | describe('Sparse', () => { 6 | it('', () => { 7 | const z = new Sparse([[1,0,0],[5,2,0],[1,0,0]]) 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /test/abs.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { abs, Complex, Sparse } from '../lib' 5 | 6 | const c1 = new Complex([1,2], [1,2]) 7 | describe('abs', () => { 8 | it('num & arr', () => { 9 | abs(1).should.equal(Math.abs(1)) 10 | abs([1, 2]).should.deepEqual([Math.abs(1), Math.abs(2)]) 11 | abs([[1,2],[1,3]]).should.deepEqual([ [Math.abs(1), Math.abs(2)], [Math.abs(1), Math.abs(3)] ]) 12 | }) 13 | 14 | it('Complex', () => { 15 | abs(new Complex(-1)).re.should.approximately(1, 0.01) 16 | // {x: [ 2.236, 2.828], y: } 17 | abs(new Complex([1, 2], [2, 2])).re[0].should.approximately(2.236, 0.01) 18 | }) 19 | 20 | it('Sparse', () => { 21 | abs(new Sparse([[1,0],[-2,1]])).toFull().should.deepEqual([[1,0],[2,1]]) 22 | // throw abs(new Sparse([[1,0],[-2,1]])) 23 | }) 24 | }) 25 | -------------------------------------------------------------------------------- /test/acos.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { acos, Complex, Sparse } from '../lib' 5 | 6 | describe('acos', () => { 7 | it('num & array & Complex', () => { 8 | acos(1).should.equal(Math.acos(1)) 9 | acos([1, 2]).should.deepEqual([Math.acos(1), Math.acos(2)]) 10 | acos([[1,2],[1,3]]).should.deepEqual([ [Math.acos(1), Math.acos(2)], [Math.acos(1), Math.acos(3)] ]) 11 | 12 | try { 13 | acos(new Complex([1,2])) 14 | } catch (error) { 15 | console.log(error); 16 | } 17 | }) 18 | 19 | it('Sparse', () =>{ 20 | acos(new Sparse([[1,2],[1,3]])).toFull() 21 | .should.deepEqual([ [Math.acos(1), Math.acos(2)], [Math.acos(1), Math.acos(3)] ]) 22 | }) 23 | }) 24 | -------------------------------------------------------------------------------- /test/add.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { add, Complex, Sparse } from '../lib' 5 | 6 | describe('add', () => { 7 | it('num & arr', () => { 8 | add(1, 2).should.equal(1 + 2) 9 | add([1, 2], [2, 2]).should.deepEqual([1 + 2, 2 + 2]) 10 | add([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 + 2, 1 + 2], [1 + 2, 2 + 2] ] 13 | ) 14 | }) 15 | 16 | it('Complex', () => { 17 | add(new Complex(1, 2), 2).re.should.equal(3) 18 | add(new Complex(1, 2), 2).im.should.equal(2) 19 | add(new Complex([1,2], [2,2]), new Complex([1,1],[1,1])) 20 | .re.should.deepEqual([2, 3]) 21 | add(new Complex([[1,2], [2,2]]), [[1,1],[1,1]]) 22 | .re.should.deepEqual([[2, 3],[3,3]]) 23 | add(new Complex(1,2), 1).re.should.equal(2) 24 | }) 25 | 26 | it('Sparse', () => { 27 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 28 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 29 | 30 | // {"col": [0,2,4,5], "row": [0,2,0,1,2], "val": [3,-2,11,7,5]} 31 | add(a, b).val.should.deepEqual([3,-2,11,7,5]) 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /test/and.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { and, Sparse } from '../lib' 5 | 6 | describe('and', () => { 7 | it('num & arr', () => { 8 | and(1, 2).should.equal(1 && 2) 9 | and([1, 2], [2, 2]).should.deepEqual([1 && 2, 2 && 2]) 10 | and([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 && 2, 1 && 2], [1 && 2, 2 && 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | and(a, b).val.should.deepEqual([ 2, 0, 9, 4, 0]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/asin.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { asin, Sparse } from '../lib' 5 | 6 | describe('asin', () => { 7 | it('num & arr', () => { 8 | asin(1).should.equal(Math.asin(1)) 9 | asin([1, 2]).should.deepEqual([Math.asin(1), Math.asin(2)]) 10 | asin([[1,2],[1,3]]).should.deepEqual([ [Math.asin(1), Math.asin(2)], [Math.asin(1), Math.asin(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | asin(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.asin(1), Math.asin(2)], [Math.asin(1), Math.asin(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/atan.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { atan, Sparse } from '../lib' 5 | 6 | describe('atan', () => { 7 | it('num & arr', () => { 8 | atan(1).should.equal(Math.atan(1)) 9 | atan([1, 2]).should.deepEqual([Math.atan(1), Math.atan(2)]) 10 | atan([[1,2],[1,3]]).should.deepEqual([ [Math.atan(1), Math.atan(2)], [Math.atan(1), Math.atan(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | atan(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.atan(1), Math.atan(2)], [Math.atan(1), Math.atan(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/atan2.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { atan2 } from '../lib' 5 | 6 | describe('atan2', () => { 7 | it('', () => { 8 | atan2(1, 2).should.equal(Math.atan2(1, 2)) 9 | atan2([1, 2], [2, 2]).should.deepEqual([Math.atan2(1, 2), Math.atan2(2, 2)]) 10 | atan2([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [Math.atan2(2, 2), Math.atan2(1, 2)], [Math.atan2(1, 2), Math.atan2(2, 2)] ] 13 | ) 14 | }) 15 | }) 16 | -------------------------------------------------------------------------------- /test/band.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { band, Sparse } from '../lib' 5 | 6 | describe('band', () => { 7 | it('num & arr', () => { 8 | band(1, 0).should.equal(1 & 0) 9 | band([1, 0], [0, 0]).should.deepEqual([1 & 0, 0 & 0]) 10 | band([[0,1], [1,0]], [[0, 0], [0, 0]]) 11 | .should.deepEqual( 12 | [ [0 & 0, 1 & 0], [1 & 0, 0 & 0] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | band(a, b).val.should.deepEqual([ 0, 0, 0, 0, 0]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/bnot.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { bnot, Sparse } from '../lib' 5 | 6 | describe('bnot', () => { 7 | it('', () => { 8 | bnot(1).should.equal(~1) 9 | bnot([1, 2]).should.deepEqual([~1, ~2]) 10 | bnot([[2,1], [1,2]]) 11 | .should.deepEqual( 12 | [ [~ 2, ~ 1], [~ 1, ~ 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () =>{ 17 | bnot(new Sparse([[1,2],[1,3]])).toFull() 18 | .should.deepEqual([ [~1, ~2], [~1, ~3] ]) 19 | }) 20 | }) 21 | -------------------------------------------------------------------------------- /test/bor.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { bor, Sparse } from '../lib' 5 | 6 | describe('bor', () => { 7 | it('arr', () => { 8 | bor(1, 2).should.equal(1 | 2) 9 | bor([1, 2], [2, 2]).should.deepEqual([1 | 2, 2 | 2]) 10 | bor([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 | 2, 1 | 2], [1 | 2, 2 | 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | bor(a, b).val.should.deepEqual( [ 3, -2, 11, 7, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/bxor.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { bxor, Sparse } from '../lib' 5 | 6 | describe('bxor', () => { 7 | it('arr', () => { 8 | bxor(1, 2).should.equal(1 ^ 2) 9 | bxor([1, 2], [2, 2]).should.deepEqual([1 ^ 2, 2 ^ 2]) 10 | bxor([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 ^ 2, 1 ^ 2], [1 ^ 2, 2 ^ 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | bxor(a, b).val.should.deepEqual( [ 3, -2, 11, 7, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/ceil.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { ceil, Sparse } from '../lib' 5 | 6 | describe('ceil', () => { 7 | it('num & arr', () => { 8 | ceil(1).should.equal(Math.ceil(1)) 9 | ceil([1, 2]).should.deepEqual([Math.ceil(1), Math.ceil(2)]) 10 | ceil([[1,2],[1,3]]).should.deepEqual([ [Math.ceil(1), Math.ceil(2)], [Math.ceil(1), Math.ceil(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | ceil(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.ceil(1), Math.ceil(2)], [Math.ceil(1), Math.ceil(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/clone.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { clone, Sparse } from '../lib' 5 | 6 | describe('clone', () => { 7 | it('', () => { 8 | clone(1).should.equal(1) 9 | clone([1, 2]).should.deepEqual([1, 2]) 10 | clone([[2,1], [1,2]]) 11 | .should.deepEqual( 12 | [ [ 2, 1], [ 1, 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () =>{ 17 | clone(new Sparse([[1,2],[1,3]])).toFull() 18 | .should.deepEqual([ [1, 2], [1, 3] ]) 19 | }) 20 | }) 21 | -------------------------------------------------------------------------------- /test/cos.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { cos, Complex, Sparse } from '../lib' 5 | 6 | describe('cos', () => { 7 | it('num & arr', () => { 8 | cos(1).should.equal(Math.cos(1)) 9 | cos([1, 2]).should.deepEqual([Math.cos(1), Math.cos(2)]) 10 | cos([[1,2],[1,3]]).should.deepEqual([ [Math.cos(1), Math.cos(2)], [Math.cos(1), Math.cos(3)] ]) 11 | }) 12 | 13 | it('Complex', () => { 14 | cos(new Complex(1)).re.should.approximately(0.540, 0.01) 15 | // {x: [ -0.6421, -1.566], y: [ 1.069, 3.298]} 16 | cos(new Complex([1, 2], [2, 2])).re[0].should.approximately(-0.642, 0.01) 17 | cos(new Complex([1, 2], [2, 2])).im[1].should.approximately(3.298, 0.01) 18 | }) 19 | 20 | it('Sparse', () =>{ 21 | cos(new Sparse([[1,2],[1,3]])).toFull() 22 | .should.deepEqual([ [Math.cos(1), Math.cos(2)], [Math.cos(1), Math.cos(3)] ]) 23 | }) 24 | }) 25 | -------------------------------------------------------------------------------- /test/det.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { det } from '../lib' 4 | 5 | describe('det', () => { 6 | it('', () => { 7 | det([[1, 2, 3],[1, 1, 1], [0, 1, 0]]).should.equal(2) 8 | }) 9 | }) -------------------------------------------------------------------------------- /test/diag.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { diag } from '../lib' 4 | 5 | describe('diag', function () { 6 | it('', function () { 7 | diag([1, 2]).should.deepEqual([[1, 0], [0, 2]]) 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /test/dim.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dim, Complex, Sparse } from '../lib' 4 | 5 | describe('dim', () => { 6 | it('num & arr', () => { 7 | dim(1).should.deepEqual([]) 8 | dim([2]).should.deepEqual([1]) 9 | dim([[1, 2, 3], [1, 2, 2]]).should.deepEqual([2, 3]) 10 | }) 11 | 12 | it('Complex', () =>{ 13 | dim(new Complex(1)).should.deepEqual([]) 14 | dim(new Complex(undefined,[1,2])).should.deepEqual([2]) 15 | }) 16 | 17 | it('Sparse', () => { 18 | dim(new Sparse([[0,0,0],[0,2,0],[1,0,0]])).should.deepEqual([3,3]) 19 | }) 20 | }) -------------------------------------------------------------------------------- /test/div.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { div, Complex, Sparse } from '../lib' 5 | 6 | const c1 = new Complex([1,2],[1,1]) 7 | const c2 = new Complex([2,2],[1,1]) 8 | 9 | describe('div', () => { 10 | it('num & arr', () => { 11 | div(1, 2).should.equal(1 / 2) 12 | div([1, 2], [2, 2]).should.deepEqual([1 / 2, 2 / 2]) 13 | div([[2,1], [1,2]], [[2, 2], [2, 2]]) 14 | .should.deepEqual( 15 | [ [2 / 2, 1 / 2], [1 / 2, 2 / 2] ] 16 | ) 17 | }) 18 | 19 | it('Complex', () => { 20 | div([4, 2], 2).should.deepEqual([2, 1]) 21 | div(c1, c2).re.should.deepEqual([ 0.6000000000000001, 1 ]) 22 | div(c1, c2).im.should.deepEqual([ 0.2, 0 ]) 23 | div(c1, 2).re.should.deepEqual([.5, 1]) 24 | }) 25 | 26 | it('Sparse', () => { 27 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 28 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 29 | 30 | div(a, b).val.should.deepEqual([0.5, -0, 0.2222222222222222, 0.75, Infinity]) 31 | }) 32 | }) 33 | -------------------------------------------------------------------------------- /test/dot.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { dot, Complex } from '../lib' 4 | 5 | describe('dot', () => { 6 | it('arr & num', () => { 7 | dot([1, 2], 4).should.deepEqual([4, 8]) 8 | dot(4, [1, 2]).should.deepEqual([4, 8]) 9 | dot([ 10 | [1, 1], 11 | [2, 1] 12 | ], [1, 2]).should.deepEqual([3, 4]) 13 | 14 | dot([1, 2], [ 15 | [1, 1], 16 | [2, 1] 17 | ]).should.deepEqual([5, 3]) 18 | 19 | dot(1, 2).should.deepEqual(2) 20 | 21 | }) 22 | 23 | it('Complex', () => { 24 | dot(new Complex(1), 2).re.should.be.equal(2) 25 | // {x: -2, y: 14} 26 | dot(new Complex([1, 2], [2, 2]), new Complex([2,2], [2,2])) 27 | .re.should.equal(-2) 28 | dot(new Complex([1, 2], [2, 2]), new Complex([2,2], [2,2])) 29 | .im.should.equal(14) 30 | }) 31 | }) -------------------------------------------------------------------------------- /test/eig.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { eig } from '../lib' 4 | 5 | describe('eig', () => { 6 | it('', () => { 7 | // { 8 | // lambda: {x: [ -4.284, 9.027, 6.257], y: }, 9 | // E: {x: [[ 0.7131, -0.5543, 0.4019], [ -0.2987, -0.2131, 0.9139], [ -0.6342, -0.8046, 0.057]], 10 | // y: } 11 | // } 12 | eig([[1,2,5],[3,5,-1],[7,-3,5]]) 13 | .lambda.re[0].should.be.approximately(-4.28, 0.01) 14 | eig([[1,2,5],[3,5,-1],[7,-3,5]]) 15 | .E.re[0][0].should.approximately(0.71, 0.1) 16 | }) 17 | }) -------------------------------------------------------------------------------- /test/epsilon.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { epsilon } from '../lib' 4 | 5 | describe('epsilon', () => { 6 | it('', () => { 7 | console.log(epsilon) 8 | }) 9 | }) -------------------------------------------------------------------------------- /test/eq.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { eq, Sparse } from '../lib' 5 | 6 | describe('eq', () => { 7 | it('arr', () => { 8 | eq(1, 2).should.equal(1 === 2) 9 | eq([1, 2], [2, 2]).should.deepEqual([1 === 2, 2 === 2]) 10 | eq([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 === 2, 1 === 2], [1 === 2, 2 === 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | eq(a, b).val.should.deepEqual([false,false,false,false,false]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/exp.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { exp, Complex, Sparse } from '../lib' 5 | 6 | describe('exp', () => { 7 | it('number & Array', () => { 8 | exp(1).should.equal(Math.exp(1)) 9 | exp([1, 2]).should.deepEqual([Math.exp(1), Math.exp(2)]) 10 | exp([[1,2],[1,3]]).should.deepEqual([ [Math.exp(1), Math.exp(2)], [Math.exp(1), Math.exp(3)] ]) 11 | }) 12 | 13 | it('Complex', () => { 14 | exp(new Complex(1)).re.should.approximately(2.718, 0.01) 15 | /**{x: [ -1.131, -3.075], y: [ 2.472, 6.719]} */ 16 | exp(new Complex([1, 2], [2, 2])).re[0].should.approximately(-1.131, 0.01) 17 | exp(new Complex([1, 2], [2, 2])).im[1].should.approximately(6.719, 0.01) 18 | }) 19 | 20 | it('Sparse', () =>{ 21 | exp(new Sparse([[1,2],[1,3]])).toFull() 22 | .should.deepEqual([ [Math.exp(1), Math.exp(2)], [Math.exp(1), Math.exp(3)] ]) 23 | }) 24 | }) 25 | -------------------------------------------------------------------------------- /test/fft.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { ifft, fft, Complex, Sparse } from '../lib' 5 | 6 | describe('fft', () => { 7 | it('number & Array', () => { 8 | // {re: [ 15, -2.5, -2.5, -2.5, -2.5], im: [ 6.439e-15, 3.441, 0.8123, -0.8123, -3.441]} 9 | fft([1,2,3,4,5]).re[0].should.approximately(15, 0.1) 10 | }) 11 | 12 | it('Complex', () => { 13 | fft(new Complex([1,2,3,4,5],[6,7,8,9,10])).im[0].should.approximately(40, 0.1) 14 | }) 15 | }) 16 | 17 | describe('ifft', () => { 18 | it('number & Array', () => { 19 | // {re: [ 15, -2.5, -2.5, -2.5, -2.5], im: [ 6.439e-15, 3.441, 0.8123, -0.8123, -3.441]} 20 | ifft( fft([1,2,3,4,5]) ).re[0].should.approximately(1, 0.1) 21 | }) 22 | 23 | it('Complex', () => { 24 | ifft( fft(new Complex([1,2,3,4,5],[6,7,8,9,10])) ).im[0].should.approximately(6, 0.1) 25 | }) 26 | }) -------------------------------------------------------------------------------- /test/floor.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { floor, Sparse } from '../lib' 5 | 6 | describe('floor', () => { 7 | it('', () => { 8 | floor(1).should.equal(Math.floor(1)) 9 | floor([1, 2]).should.deepEqual([Math.floor(1), Math.floor(2)]) 10 | floor([[1,2],[1,3]]).should.deepEqual([ [Math.floor(1), Math.floor(2)], [Math.floor(1), Math.floor(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | floor(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.floor(1), Math.floor(2)], [Math.floor(1), Math.floor(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/geq.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { geq, Sparse } from '../lib' 5 | 6 | describe('geq', () => { 7 | it('arr', () => { 8 | geq(1, 2).should.equal(1 >= 2) 9 | geq([1, 2], [2, 2]).should.deepEqual([1 >= 2, 2 >= 2]) 10 | geq([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 >= 2, 1 >= 2], [1 >= 2, 2 >= 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | geq(a, b).val.should.deepEqual([false,true,false,false,true]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/getBlock.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { getBlock } from '../lib' 5 | 6 | describe('getBlock', function () { 7 | it('', function () { 8 | getBlock([[1,2,3], 9 | [3,4,5]], [0,0], [1,1]).should.deepEqual([[1, 2], [3, 4]]) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /test/getDiag.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { getDiag, Complex } from '../lib' 5 | 6 | describe('getDiag', function () { 7 | it('Array', function () { 8 | getDiag([[1, 3], [0, 2]]).should.deepEqual([1, 2]) 9 | }) 10 | 11 | it('Complex', () =>{ 12 | getDiag(new Complex([[1, 3], [0, 2]], [[1, 3], [0, 2]])) 13 | .re.should.deepEqual([1,2]) 14 | getDiag(new Complex([[1, 3], [0, 2]], [[1, 3], [0, 2]])) 15 | .im.should.deepEqual([1,2]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/gt.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { gt, Sparse } from '../lib' 5 | 6 | describe('gt', () => { 7 | it('arr', () => { 8 | gt(1, 2).should.equal(1 > 2) 9 | gt([1, 2], [2, 2]).should.deepEqual([1 > 2, 2 > 2]) 10 | gt([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 > 2, 1 > 2], [1 > 2, 2 > 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | gt(a, b).val.should.deepEqual([false,true,false,false,true]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/identity.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { identity } from '../lib' 4 | 5 | describe('identity', () => { 6 | it('', () => { 7 | identity(2).should.deepEqual([ 8 | [1, 0], 9 | [0, 1] 10 | ]) 11 | }) 12 | }) -------------------------------------------------------------------------------- /test/inv.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { inv } from '../lib' 4 | 5 | describe('inv', () => { 6 | it('', () => { 7 | inv([[1,2],[3,4]])[0][0].should.be.approximately(-2, 0.001) 8 | inv([[1,2],[3,4]])[0][1].should.be.approximately(1, 0.001) 9 | inv([[1,2],[3,4]])[1][0].should.be.approximately(1.5, 0.001) 10 | inv([[1,2],[3,4]])[1][1].should.be.approximately(-0.5, 0.001) 11 | }) 12 | }) -------------------------------------------------------------------------------- /test/leq.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { leq, Sparse } from '../lib' 5 | 6 | describe('leq', () => { 7 | it('arr', () => { 8 | leq(1, 2).should.equal(1 <= 2) 9 | leq([1, 2], [2, 2]).should.deepEqual([1 <= 2, 2 <= 2]) 10 | leq([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 <= 2, 1 <= 2], [1 <= 2, 2 <= 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | leq(a, b).val.should.deepEqual([true,false,true,true,false]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/linspace.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { linspace } from '../lib' 4 | 5 | describe('linspace', () => { 6 | it('', () => { 7 | linspace(1, 2, 3).should.deepEqual([1, 1.5, 2]) 8 | }) 9 | }) -------------------------------------------------------------------------------- /test/log.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { log, Complex, Sparse } from '../lib' 5 | 6 | describe('log', () => { 7 | it('num & arr', () => { 8 | log(1).should.equal(Math.log(1)) 9 | log([1, 2]).should.deepEqual([Math.log(1), Math.log(2)]) 10 | log([[1,2],[1,3]]).should.deepEqual([ [Math.log(1), Math.log(2)], [Math.log(1), Math.log(3)] ]) 11 | }) 12 | 13 | it('Complex', () => { 14 | log(new Complex(1)).re.should.approximately(0, 0.01) 15 | // {x: [ 0.8047, 1.04], y: [ 1.107, 0.7854]} 16 | log(new Complex([1, 2], [2, 2])).re[0].should.approximately(0.804, 0.01) 17 | log(new Complex([1, 2], [2, 2])).im[1].should.approximately(0.785, 0.01) 18 | }) 19 | 20 | it('Sparse', () =>{ 21 | log(new Sparse([[1,2],[1,3]])).toFull() 22 | .should.deepEqual([ [Math.log(1), Math.log(2)], [Math.log(1), Math.log(3)] ]) 23 | }) 24 | }) 25 | -------------------------------------------------------------------------------- /test/lshift.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { lshift, Sparse } from '../lib' 5 | 6 | describe('lshift', () => { 7 | it('arr', () => { 8 | lshift(1, 2).should.equal(1 << 2) 9 | lshift([1, 2], [2, 2]).should.deepEqual([1 << 2, 2 << 2]) 10 | lshift([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 << 2, 1 << 2], [1 << 2, 2 << 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | lshift(a, b).val.should.deepEqual([ 4, 0, 1024, 48, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/lt.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { lt, Sparse } from '../lib' 5 | 6 | describe('lt', () => { 7 | it('arr', () => { 8 | lt(1, 2).should.equal(1 < 2) 9 | lt([1, 2], [2, 2]).should.deepEqual([1 < 2, 2 < 2]) 10 | lt([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 < 2, 1 < 2], [1 < 2, 2 < 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | lt(a, b).val.should.deepEqual([true,false,true,true,false]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/mod.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { mod, Sparse } from '../lib' 5 | 6 | describe('mod', () => { 7 | it('arr', () => { 8 | mod(1, 2).should.equal(1 % 2) 9 | mod([1, 2], [2, 2]).should.deepEqual([1 % 2, 2 % 2]) 10 | mod([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 % 2, 1 % 2], [1 % 2, 2 % 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | mod(a, b).val.should.deepEqual([1, 0, 2, 3, NaN]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/mul.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { mul, Complex, Sparse } from '../lib' 5 | 6 | const c1 = new Complex([1,2],[1,1]) 7 | const c2 = new Complex([2,2],[1,1]) 8 | 9 | describe('mul', () => { 10 | it('arr ', () => { 11 | mul(1, 2).should.equal(1 * 2) 12 | mul([1, 2], [2, 2]).should.deepEqual([1 * 2, 2 * 2]) 13 | mul([[2,1], [1,2]], [[2, 2], [2, 2]]) 14 | .should.deepEqual( 15 | [ [2 * 2, 1 * 2], [1 * 2, 2 * 2] ] 16 | ) 17 | mul(c1, c2).re.should.deepEqual([ 1, 3 ]) 18 | mul(c1, c2).im.should.deepEqual([ 3, 4 ]) 19 | mul(c1, 3).re.should.deepEqual([3, 6]) 20 | }) 21 | 22 | it('Sparse', () => { 23 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 24 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 25 | 26 | mul(a, b).val.should.deepEqual( [ 2, 0, 18, 12, 0]) 27 | }) 28 | }) 29 | -------------------------------------------------------------------------------- /test/neg.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { neg, Complex, Sparse } from '../lib' 5 | 6 | describe('neg', () => { 7 | it('num & array', () => { 8 | neg(1).should.equal(-1) 9 | neg([1, 2]).should.deepEqual([-1, -2]) 10 | neg([[2,1], [1,2]]) 11 | .should.deepEqual( 12 | [ [- 2, - 1], [- 1, - 2] ] 13 | ) 14 | }) 15 | 16 | it('Complex', () => { 17 | neg(new Complex(1)).re.should.equal(-1) 18 | neg(new Complex([1, 2], [2, 2])).im.should.deepEqual([-2,-2]) 19 | }) 20 | 21 | it('Sparse', () =>{ 22 | neg(new Sparse([[1,2],[1,3]])).toFull() 23 | .should.deepEqual([ [-1, -2], [-1, -3] ]) 24 | }) 25 | }) 26 | -------------------------------------------------------------------------------- /test/neq.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { neq, Sparse } from '../lib' 5 | 6 | describe('neq', () => { 7 | it('arr', () => { 8 | neq(1, 2).should.equal(1 !== 2) 9 | neq([1, 2], [2, 2]).should.deepEqual([1 !== 2, 2 !== 2]) 10 | neq([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 !== 2, 1 !== 2], [1 !== 2, 2 !== 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | neq(a, b).val.should.deepEqual([true,true,true,true,true]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/norm2.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { norm2, Complex } from '../lib' 5 | 6 | describe('norm2', () => { 7 | it('arr & num', () => { 8 | norm2(2).should.equal(2) 9 | norm2([2, 2]).should.be.approximately(2.8, 0.1) 10 | norm2([[2,2],[1,1]]).should.be.approximately(8.246, 0.01) 11 | }) 12 | 13 | it('Complex', () => { 14 | norm2(new Complex(2)).should.equal(2) 15 | norm2(new Complex([1,2], [2,2])).should.be.approximately(3.605, 0.1) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/not.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { not, Sparse } from '../lib' 5 | 6 | describe('not', () => { 7 | it('', () => { 8 | not(1).should.equal(!1) 9 | not([1, 2]).should.deepEqual([!1, !2]) 10 | not([[2,1], [1,2]]) 11 | .should.deepEqual( 12 | [ [! 2, ! 1], [! 1, ! 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () =>{ 17 | not(new Sparse([[1,2],[1,3]])).toFull() 18 | .should.deepEqual([ [!1, !2], [!1, !3] ]) 19 | }) 20 | }) 21 | -------------------------------------------------------------------------------- /test/or.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { or, Sparse } from '../lib' 5 | 6 | describe('or', () => { 7 | it('arr', () => { 8 | or(1, 2).should.equal(1 || 2) 9 | or([1, 2], [2, 2]).should.deepEqual([1 || 2, 2 || 2]) 10 | or([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 || 2, 1 || 2], [1 || 2, 2 || 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | or(a, b).val.should.deepEqual([ 1, -2, 2, 3, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/pointwise.js: -------------------------------------------------------------------------------- 1 | // import assert from 'assert' 2 | // import should from 'should' 3 | // import { pointwise } from '../lib' 4 | 5 | // describe('pointwise', () => { 6 | // it('', () => { 7 | // }) 8 | // }) -------------------------------------------------------------------------------- /test/pow.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { pow } from '../lib' 5 | 6 | describe('pow', () => { 7 | it('', () => { 8 | pow(1, 2).should.equal(Math.pow(1, 2)) 9 | pow([1, 2], [2, 2]).should.deepEqual([Math.pow(1, 2), Math.pow(2, 2)]) 10 | pow([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [Math.pow(2, 2), Math.pow(1, 2)], [Math.pow(1, 2), Math.pow(2, 2)] ] 13 | ) 14 | }) 15 | }) 16 | -------------------------------------------------------------------------------- /test/random.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { random, dim } from '../lib' 5 | 6 | describe('random', () => { 7 | it('', () => { 8 | dim(random([2,3])).should.deepEqual([2,3]) 9 | random([2,3])[0][0].should.be.above(0).below(1) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /test/rep.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { rep } from '../lib' 5 | 6 | describe('rep', () => { 7 | it('', () => { 8 | rep([2, 3], 0).should.deepEqual([[0,0,0],[0,0,0]]) 9 | rep([3], 5).should.deepEqual([5,5,5]) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /test/round.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { round } from '../lib' 5 | 6 | describe('round', () => { 7 | it('', () => { 8 | round(1).should.equal(Math.round(1)) 9 | round([1, 2]).should.deepEqual([Math.round(1), Math.round(2)]) 10 | round([[1,2],[1,3]]).should.deepEqual([ [Math.round(1), Math.round(2)], [Math.round(1), Math.round(3)] ]) 11 | }) 12 | }) 13 | -------------------------------------------------------------------------------- /test/rrshift.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { rrshift, Sparse } from '../lib' 5 | 6 | describe('rrshift', () => { 7 | it('', () => { 8 | rrshift(1, 2).should.equal(1 >>> 2) 9 | rrshift([1, 2], [2, 2]).should.deepEqual([1 >>> 2, 2 >>> 2]) 10 | rrshift([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 >>> 2, 1 >>> 2], [1 >>> 2, 2 >>> 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | rrshift(a, b).val.should.deepEqual([ 0, 0, 0, 0, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/rshift.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { rshift, Sparse } from '../lib' 5 | 6 | describe('rshift', () => { 7 | it('arr', () => { 8 | rshift(1, 2).should.equal(1 >> 2) 9 | rshift([1, 2], [2, 2]).should.deepEqual([1 >> 2, 2 >> 2]) 10 | rshift([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 >> 2, 1 >> 2], [1 >> 2, 2 >> 2] ] 13 | ) 14 | }) 15 | 16 | it('Sparse', () => { 17 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 18 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 19 | 20 | rshift(a, b).val.should.deepEqual([ 0, 0, 0, 0, 5]) 21 | }) 22 | }) 23 | -------------------------------------------------------------------------------- /test/same.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { same } from '../lib' 5 | 6 | describe('same', () => { 7 | it('', () => { 8 | same([1,2], [1,2,3]).should.be.equal(false) 9 | same([1, 2], [1, 2]).should.be.equal(true) 10 | same([[1,1],[1,2]], [[1,1], [1,2]]).should.be.equal(true) 11 | same([[0,1],[1,2]], [[1,1], [1,2]]).should.be.equal(false) 12 | }) 13 | }) 14 | -------------------------------------------------------------------------------- /test/setBlock.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { setBlock } from '../lib' 5 | 6 | describe('setBlock', () => { 7 | it('', () => { 8 | setBlock([[1,2,3], [3,4,5]], [0,0], [1,1],[[2,2],[2,2]]).should.deepEqual([[2,2,3],[2,2,5]]) 9 | }) 10 | }) 11 | -------------------------------------------------------------------------------- /test/sin.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { sin, Complex, Sparse } from '../lib' 5 | 6 | describe('sin', () => { 7 | it('num & arr', () => { 8 | sin(1).should.equal(Math.sin(1)) 9 | sin([1, 2]).should.deepEqual([Math.sin(1), Math.sin(2)]) 10 | sin([[1,2],[1,3]]).should.deepEqual([ [Math.sin(1), Math.sin(2)], [Math.sin(1), Math.sin(3)] ]) 11 | }) 12 | 13 | 14 | it('Complex', () => { 15 | sin(new Complex(1)).re.should.approximately(0.841, 0.01) 16 | // {x: [ 1.403, 3.421], y: [ 0.4891, 1.509]} 17 | sin(new Complex([1, 2], [2, 2])).re[0].should.approximately(1.403, 0.01) 18 | sin(new Complex([1, 2], [2, 2])).im[1].should.approximately(1.509, 0.01) 19 | }) 20 | 21 | it('Sparse', () =>{ 22 | sin(new Sparse([[1,2],[1,3]])).toFull() 23 | .should.deepEqual([ [Math.sin(1), Math.sin(2)], [Math.sin(1), Math.sin(3)] ]) 24 | }) 25 | }) 26 | -------------------------------------------------------------------------------- /test/sqrt.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { sqrt, Sparse } from '../lib' 5 | 6 | describe('sqrt', () => { 7 | it('num & arr', () => { 8 | sqrt(1).should.equal(Math.sqrt(1)) 9 | sqrt([1, 2]).should.deepEqual([Math.sqrt(1), Math.sqrt(2)]) 10 | sqrt([[1,2],[1,3]]).should.deepEqual([ [Math.sqrt(1), Math.sqrt(2)], [Math.sqrt(1), Math.sqrt(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | sqrt(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.sqrt(1), Math.sqrt(2)], [Math.sqrt(1), Math.sqrt(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/sub.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { sub, Complex, Sparse } from '../lib' 5 | 6 | describe('sub', () => { 7 | it('arr', () => { 8 | sub(1, 2).should.equal(1 - 2) 9 | sub([1, 2], [2, 2]).should.deepEqual([1 - 2, 2 - 2]) 10 | sub([[2,1], [1,2]], [[2, 2], [2, 2]]) 11 | .should.deepEqual( 12 | [ [2 - 2, 1 - 2], [1 - 2, 2 - 2] ] 13 | ) 14 | sub(new Complex(1, 2), 2).re.should.equal(-1) 15 | sub(new Complex(1, 2), 2).im.should.equal(2) 16 | sub(new Complex([1,2], [2,2]), new Complex([1,1],[1,1])) 17 | .re.should.deepEqual([0, 1]) 18 | sub(new Complex([[1,2], [2,2]]), [[1,1],[1,1]]) 19 | .re.should.deepEqual([[0,1],[1,1]]) 20 | }) 21 | 22 | it('Sparse', () => { 23 | const a = new Sparse([[1,2,0],[0,3,0],[0,0,5]]) 24 | const b = new Sparse([[2,9,0],[0,4,0],[-2,0,0]]) 25 | 26 | sub(a, b).val.should.deepEqual([ -1, 2, -7, -1, 5]) 27 | }) 28 | }) 29 | -------------------------------------------------------------------------------- /test/tan.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { tan, Sparse } from '../lib' 5 | 6 | describe('tan', () => { 7 | it('', () => { 8 | tan(1).should.equal(Math.tan(1)) 9 | tan([1, 2]).should.deepEqual([Math.tan(1), Math.tan(2)]) 10 | tan([[1,2],[1,3]]).should.deepEqual([ [Math.tan(1), Math.tan(2)], [Math.tan(1), Math.tan(3)] ]) 11 | }) 12 | 13 | it('Sparse', () =>{ 14 | tan(new Sparse([[1,2],[1,3]])).toFull() 15 | .should.deepEqual([ [Math.tan(1), Math.tan(2)], [Math.tan(1), Math.tan(3)] ]) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /test/tensor.js: -------------------------------------------------------------------------------- 1 | 2 | import assert from 'assert' 3 | import should from 'should' 4 | import { tensor } from '../lib' 5 | 6 | describe('tensor', () => { 7 | it('', () => { 8 | tensor(1, 2).should.equal(2) 9 | tensor([1, 2], [3, 4]).should.deepEqual([[3,4],[6,8]]) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /test/transpose.js: -------------------------------------------------------------------------------- 1 | import assert from 'assert' 2 | import should from 'should' 3 | import { transpose, Complex } from '../lib' 4 | 5 | describe('transpose', () => { 6 | it('arr', () => { 7 | transpose([[1,2,3],[4,5,6]]).should.deepEqual([[1,4], [2,5], [3,6]]) 8 | }) 9 | 10 | it('Complex', () =>{ 11 | transpose(new Complex([[1,2],[1,2]], [[1,2],[3,4]])) 12 | .re.should.deepEqual([[1,1],[2,2]]) 13 | transpose(new Complex([[1,2],[1,2]], [[1,2],[3,4]])) 14 | .im.should.deepEqual([[1,3],[2,4]]) 15 | }) 16 | }) --------------------------------------------------------------------------------