Skip to content

Commit

Permalink
feat: drop numeric in favor of small snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
austince committed Dec 14, 2019
1 parent f1a11d2 commit c7a2e3d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 49 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# maptastic

=========

Javascript/CSS projection mapping utility. Put your internets on things!
Javascript/CSS projection mapping utility with zero dependencies. Put your internets on things!

![maptastic animation](https://glowbox.github.io/maptasticjs/images/maptastic.gif "Maptastic JS")

Expand Down Expand Up @@ -31,8 +29,6 @@ When you include `maptastic.js` in your page, a new class `maptastic.Maptastic`

<div id="so-simple">This is pretty simple.</div>

<!-- The only dependency is on `numeric`, which can be loaded from cdnjs -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeric/1.2.6/numeric.js"></script>
<script src="./dist/maptastic.js"></script>
<script>
new maptastic.Maptastic("so-simple");
Expand Down
1 change: 0 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
</pre>
<iframe src="//player.vimeo.com/video/108531008" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen id="video-test"></iframe>

<script src="../node_modules/numeric/numeric-1.2.6.min.js"></script>
<script src="../dist/maptastic.js"></script>
<script>
new maptastic.Maptastic("video-test", "controls");
Expand Down
1 change: 0 additions & 1 deletion example/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<div id="so-simple">This is pretty simple.</div>

<script src="../node_modules/numeric/numeric-1.2.6.min.js"></script>
<script src="../dist/maptastic.js"></script>
<script>
new maptastic.Maptastic("so-simple");
Expand Down
1 change: 0 additions & 1 deletion example/three.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</head>
<body>

<script src="../node_modules/numeric/numeric-1.2.6.min.js"></script>
<script src="../dist/maptastic.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r69/three.js"></script>
<script>
Expand Down
63 changes: 27 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
"rollup": "^1.27.9",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-uglify": "^6.0.3"
"rollup-plugin-uglify": "^6.0.4"
},
"dependencies": {
"numeric": "^1.2.6"
}
"dependencies": {}
}
2 changes: 1 addition & 1 deletion src/maptastic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { solve } from 'numeric';
import solve from './solve';

/**
* @typedef {Object} Config
Expand Down
164 changes: 164 additions & 0 deletions src/solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* @fileoverview A snippet from the {@link https://github.com/sloisel/numeric | numeric} project, as it is unmaintained and doesn't export correctly.
* @see {@link https://github.com/sloisel/numeric/blob/656fa1254be540f428710738ca9c1539625777f1/src/numeric.js#L3001-L3089 | Source}
*/

/**
* @see {@link https://github.com/sloisel/numeric/blob/master/src/numeric.js#L315-L329}
*/
function _foreach2(x, s, k, f) {
if (k === s.length - 1) {
return f(x);
}
var i, n = s[k], ret = Array(n);
for (i = n - 1; i >= 0; i--) {
ret[i] = _foreach2(x[i], s, k + 1, f);
}
return ret;
}


function _dim(x) {
var ret = [];
while (typeof x === "object") {
ret.push(x.length);
x = x[0];
}
return ret;
}

/**
* @see {@link https://github.com/sloisel/numeric/blob/656fa1254be540f428710738ca9c1539625777f1/src/numeric.js#L309-L329}
* @param {*} x
*/
function dim(x) {
var y, z;
if (typeof x === "object") {
y = x[0];
if (typeof y === "object") {
z = y[0];
if (typeof z === "object") {
return _dim(x);
}
return [x.length, y.length];
}
return [x.length];
}
return [];
}


function cloneV(x) {
var _n = x.length;
var i, ret = Array(_n);

for (i = _n - 1; i !== -1; --i) {
ret[i] = (x[i]);
}
return ret;
}

function clone(x) {
if (typeof x !== "object") {
return (x);
}
var s = dim(x);
return _foreach2(x, s, 0, cloneV);
}

// 11. Ax = b
/**
* @param {*} A
* @param {boolean} [fast=false]
* @returns {{ LU: *, P: * }}
*/
function LU(A, fast = false) {
var abs = Math.abs;
var i, j, k, absAjk, Akk, Ak, Pk, Ai;
var max;
var n = A.length, n1 = n - 1;
var P = new Array(n);
if (!fast) {
A = clone(A);
}

for (k = 0; k < n; ++k) {
Pk = k;
Ak = A[k];
max = abs(Ak[k]);
for (j = k + 1; j < n; ++j) {
absAjk = abs(A[j][k]);
if (max < absAjk) {
max = absAjk;
Pk = j;
}
}
P[k] = Pk;

if (Pk != k) {
A[k] = A[Pk];
A[Pk] = Ak;
Ak = A[k];
}

Akk = Ak[k];

for (i = k + 1; i < n; ++i) {
A[i][k] /= Akk;
}

for (i = k + 1; i < n; ++i) {
Ai = A[i];
for (j = k + 1; j < n1; ++j) {
Ai[j] -= Ai[k] * Ak[j];
++j;
Ai[j] -= Ai[k] * Ak[j];
}
if (j === n1) Ai[j] -= Ai[k] * Ak[j];
}
}

return {
LU: A,
P: P
};
}

function LUsolve(LUP, b) {
var i, j;
var LU = LUP.LU;
var n = LU.length;
var x = clone(b);
var P = LUP.P;
var Pi, LUi, tmp;

for (i = n - 1; i !== -1; --i) x[i] = b[i];
for (i = 0; i < n; ++i) {
Pi = P[i];
if (P[i] !== i) {
tmp = x[i];
x[i] = x[Pi];
x[Pi] = tmp;
}

LUi = LU[i];
for (j = 0; j < i; ++j) {
x[i] -= x[j] * LUi[j];
}
}

for (i = n - 1; i >= 0; --i) {
LUi = LU[i];
for (j = i + 1; j < n; ++j) {
x[i] -= x[j] * LUi[j];
}

x[i] /= LUi[i];
}

return x;
}

export default function solve(A, b, fast) {
return LUsolve(LU(A, fast), b);
}

0 comments on commit c7a2e3d

Please sign in to comment.