訊息
|
程式:NumericTest.js
var util = require('util');
var nu=require('./numeric'); // 注意,./ 代表 circle 與此程式放在同一個資料夾底下。
var title=function(str) {
console.log("> "+exp+"\n"+nu.prettyPrint(eval(exp))+"\n");
}
var run=function(exp) {
console.log("> "+exp+"\n"+nu.prettyPrint(eval(exp))+"\n");
}
// Numerical analysis in Javascript
run("A = [[1,2,3],[4,5,6]]");
run("x = [7,8,9]");
run("b = nu.dot(A,x)");
run("y = [10,1,2]");
run("nu['+'](x,y)");
run("nu['>'](x,y)");
run("nu.add(x,y)");
run("nu.add([1,2],[3,4],[5,6],[7,8])");
run("A = [[1,2,3],[4,5,6],[7,1,9]]");
run("nu.inv(A)");
run("pi = 3.141592653589793");
run("nu.precision = 10");
run("pi");
run("nu.precision = 4");
run("pi");
run("nu.identity(100)");
run("I4 = nu.identity(4)");
run("nu.largeArray = 2; I4");
run("nu.largeArray =50; I4");
// Math Object functions
run("nu.exp([1,2])");
run("nu.exp([[1,2],[3,4]])");
run("nu.abs([-2,3])");
run("nu.acos([0.1,0.2])");
run("nu.asin([0.1,0.2])");
run("nu.atan([1,2])");
run("nu.atan2([1,2],[3,4])");
run("nu.ceil([-2.2,3.3])");
run("nu.floor([-2.2,3.3])");
run("nu.log([1,2])");
run("nu.pow([2,3],[0.25,7.1])");
run("nu.round([-2.2,3.3])");
run("nu.sin([1,2])");
run("nu.sqrt([1,2])");
run("nu.tan([1,2])");
// Utility functions
run("nu.dim([1,2])");
run("nu.dim([[1,2,3],[4,5,6]])");
run("nu.same([1,2],[1,2])");
run("nu.same([1,2],[1,2,3])");
run("nu.same([1,2],[[1],[2]])");
run("nu.same([[1,2],[3,4]],[[1,2],[3,4]])");
run("nu.same([[1,2],[3,4]],[[1,2],[3,5]])");
run("nu.same([[1,2],[2,4]],[[1,2],[3,4]])");
run("nu.rep([3],5)");
run("nu.rep([2,3],0)");
run("sum = nu.mapreduce('accum += xi','0'); sum([1,2,3])");
run("sum([[1,2,3],[4,5,6]])");
run("nu.any([false,true])");
run("nu.any([[0,0,3.14],[0,false,0]])");
run("nu.any([0,0,false])");
run("nu.all([false,true])");
run("nu.all([[1,4,3.14],['no',true,-1]])");
run("nu.all([0,0,false])");
run("add = nu.pointwise(['x[i]','y[i]'],'ret[i] = x[i]+y[i];'); add([1,2],[3,4])");
run("nu.diag([1,2,3])");
run("nu.identity(3)");
run("nu.random([2,3])");
run("nu.linspace(1,5)");
run("nu.linspace(1,3,5)");
// Arithmetic operations
run("nu.addVV([1,2],[3,4])");
run("nu.addVS([1,2],3)");
run("nu.add(1,[2,3])");
run("nu.add([1,2,3],[4,5,6])");
run("nu.sub([1,2],[3,4])");
run("nu.mul([1,2],[3,4])");
run("nu.div([1,2],[3,4])");
run("v = [1,2,3,4]; nu.addeq(v,3); v");
run("nu.subeq([1,2,3],[5,3,1])");
run("nu.neg([1,-2,3])");
run("nu.isFinite([10,NaN,Infinity])");
run("nu.isNaN([10,NaN,Infinity])");
// Linear algebra
run("nu.dotVV([1,2],[3,4])");
run("nu.dotVM([1,2],[[3,4],[5,6]])");
run("nu.dotMV([[1,2],[3,4]],[5,6])");
run("nu.dotMMbig([[1,2],[3,4]],[[5,6],[7,8]])");
run("nu.dotMMsmall([[1,2],[3,4]],[[5,6],[7,8]])");
run("nu.dot([1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9])");
run("nu.dot([1,2,3],[4,5,6])");
run("nu.dot([[1,2,3],[4,5,6]],[7,8,9])");
run("nu.solve([[1,2],[3,4]],[17,39])");
run("LU = nu.LU([[1,2],[3,4]])");
run("nu.LUsolve(LU,[17,39])");
run("nu.det([[1,2],[3,4]])");
run("nu.det([[6,8,4,2,8,5],[3,5,2,4,9,2],[7,6,8,3,4,5],[5,5,2,8,1,6],[3,2,2,4,2,2],[8,3,2,2,4,1]])");
run("nu.inv([[1,2],[3,4]])");
run("nu.transpose([[1,2,3],[4,5,6]])");
run("nu.transpose([[1,2,3,4,5,6,7,8,9,10,11,12]])");
run("nu.norm2([1,2])");
run("nu.tensor([1,2],[3,4,5])");
// Data manipulation
run("nu.parseDate(['1/13/2013','2001-5-9, 9:31'])");
run("nu.parseFloat(['12','0.1'])");
run("nu.parseCSV('a,b,c\\n1,2.3,.3\\n4e6,-5.3e-8,6.28e+4')");
run("nu.toCSV([[1.23456789123,2],[3,4]])");
// run("nu.getURL('tools/helloworld.txt').responseText");
// Complex linear algebra
run("z = new nu.T(3,4)");
run("z.add(5)");
run("w = new nu.T(2,8)");
run("z.add(w)");
run("z.mul(w)");
run("z.div(w)");
run("z.sub(w)");
run("z = new nu.T([1,2],[3,4])");
run("z.abs()");
run("z.conj()");
run("z.norm2()");
run("z.exp()");
run("z.cos()");
run("z.sin()");
run("z.log()");
run("A = new nu.T([[1,2],[3,4]],[[0,1],[2,-1]])");
run("A.inv()");
run("A.inv().dot(A)");
run("A.get([1,1])");
run("A.transpose()");
run("A.transjugate()");
run("nu.T.rep([2,2],new nu.T(2,3))");
// Eigenvalues
run("A = [[1,2,5],[3,5,-1],[7,-3,5]]");
run("B = nu.eig(A)");
run("C = B.E.dot(nu.T.diag(B.lambda)).dot(B.E.inv())");
// Singular value decomposition (Shanti Rao)
run("A = [[22,10,2,3,7],[14,7,10,0,8],[-1,13,-1,-11,3],[-3,-2,13,-2,4],[9,8,1,-2,4],[9,1,-7,5,-1],[2,-6,6,5,1],[4,5,0,-2,2]]");
run("nu.svd(A)");
// Sparse linear algebra
run("A = [[1,2,0],[0,3,0],[2,0,5]]; SA = nu.ccsSparse(A);");
run("A = nu.ccsSparse([[ 3, 5, 8,10, 8],[ 7,10, 3, 5, 3], [ 6, 3, 5, 1, 8], [ 2, 6, 7, 1, 2], [ 1, 2, 9, 3, 9]])");
run("nu.ccsFull(A)");
run("nu.ccsDot(nu.ccsSparse([[1,2,3],[4,5,6]]),nu.ccsSparse([[7,8],[9,10],[11,12]]))");
run("M = [[0,1,3,6],[0,0,1,0,1,2],[3,-1,2,3,-2,4]]; b = [9,3,2]; x = nu.ccsTSolve(M,b)");
run("nu.ccsDot(M,[[0,3],[0,1,2],x])");
run("LUP = nu.ccsLUP(A)");
run("nu.ccsFull(nu.ccsDot(LUP.L,LUP.U))");
run("x = nu.ccsLUPSolve(LUP,[96,63,82,51,89])");
run("X = nu.trunc(nu.ccsFull(nu.ccsLUPSolve(LUP,A)),1e-15)"); // Solve LUX = PA
run("nu.ccsLUP(A,0.4).P");
run("A = nu.ccsSparse([[1,2,0],[0,3,0],[0,0,5]])");
run("B = nu.ccsSparse([[2,9,0],[0,4,0],[-2,0,0]])");
run("nu.ccsadd(A,B)");
run("X = [[0,0,1,1,2,2],[0,1,1,2,2,3],[1,2,3,4,5,6]]");
run("SX = nu.ccsScatter(X)");
run("nu.ccsGather(SX)");
// Coordinate matrices
run("lu = nu.cLU([[0,0,1,1,1,2,2],[0,1,0,1,2,1,2],[2,-1,-1,2,-1,-1,2]])");
run("nu.cLUsolve(lu,[5,-8,13])");
run("g = nu.cgrid(5)");
run("coordL = nu.cdelsq(g)");
run("L = nu.sscatter(coordL)"); // Just to see what it looks like
run("lu = nu.cLU(coordL); x = nu.cLUsolve(lu,[1,1,1,1,1,1,1,1,1]);");
run("nu.cdotMV(coordL,x)");
run("G = nu.rep([5,5],0); for(i=0;i<5;i++) for(j=0;j<5;j++) if(g[i][j]>=0) G[i][j] = x[g[i][j]]; G");
run("nu.imageURL(nu.mul([G,G,G],200))");
run("nu.cgrid(6,'L')");
run("nu.cgrid(5,function(i,j) { return i!==2 || j!==2; })");
// Cubic splines
run("nu.spline([1,2,3,4,5],[1,2,1,3,2]).at(nu.linspace(1,5,10))");
run("nu.spline([1,2,3,4,5],[1,2,1,3,2],0,0).at(nu.linspace(1,5,10))");
run("nu.spline([1,2,3,4],[0.8415,0.04718,-0.8887,0.8415],'periodic').at(nu.linspace(1,4,10))");
run("nu.spline([1,2,3],[[0,1],[1,0],[0,1]]).at(1.78)");
run("xs = [0,1,2,3]; nu.spline(xs,nu.sin(xs)).diff().at(1.5)");
run("xs = nu.linspace(0,30,31); ys = nu.sin(xs); s = nu.spline(xs,ys).roots()");
// Fast Fourier Transforms
run("z = (new nu.T([1,2,3,4,5],[6,7,8,9,10])).fft()");
run("z.ifft()");
// Quadratic Programming (Alberto Santini)
run("nu.solveQP([[1,0,0],[0,1,0],[0,0,1]],[0,5,0],[[-4,2,0],[-3,1,-2],[0,0,1]],[-8,2,0])");
// Unconstrained optimization
run("sqr = function(x) { return x*x; }; nu.uncmin(function(x) { return sqr(10*(x[1]-x[0]*x[0])) + sqr(1-x[0]); },[-1.2,1]).solution");
run("f = function(x) { return sqr(-13+x[0]+((5-x[1])*x[1]-2)*x[1])+sqr(-29+x[0]+((x[1]+1)*x[1]-14)*x[1]); }; x0 = nu.uncmin(f,[0.5,-2]).solution");
run("f = function(x) { return sqr(1e4*x[0]*x[1]-1)+sqr(Math.exp(-x[0])+Math.exp(-x[1])-1.0001); }; x0 = nu.uncmin(f,[0,1]).solution");
run("f = function(x) { return sqr(x[0]-1e6)+sqr(x[1]-2e-6)+sqr(x[0]*x[1]-2)}; x0 = nu.uncmin(f,[0,1]).solution");
run("f = function(x) { return sqr(1.5-x[0]*(1-x[1]))+sqr(2.25-x[0]*(1-x[1]*x[1]))+sqr(2.625-x[0]*(1-x[1]*x[1]*x[1])); }; x0 = nu.uncmin(f,[1,1]).solution");
run("f = function(x) { var ret = 0,i; for(i=1;i<=10;i++) ret+=sqr(2+2*i-Math.exp(i*x[0])-Math.exp(i*x[1])); return ret; }; x0 = nu.uncmin(f,[0.3,0.4]).solution");
run("y = [0.14,0.18,0.22,0.25,0.29,0.32,0.35,0.39,0.37,0.58,0.73,0.96,1.34,2.10,4.39]; f = function(x) { var ret = 0,i; for(i=1;i<=15;i++) ret+=sqr(y[i-1]-(x[0]+i/((16-i)*x[1]+Math.min(i,16-i)*x[2]))); return ret; }; x0 = nu.uncmin(f,[1,1,1]).solution");
run("y = [0.0009,0.0044,0.0175,0.0540,0.1295,0.2420,0.3521,0.3989,0.3521,0.2420,0.1295,0.0540,0.0175,0.0044,0.0009]; f = function(x) { var ret = 0,i; for(i=1;i<=15;i++) ret+=sqr(x[0]*Math.exp(-x[1]*sqr((8-i)/2-x[2])/2)-y[i-1]); return ret; }; x0 = nu.div(nu.round(nu.mul(nu.uncmin(f,[1,1,1]).solution,1000)),1000)");
run("f = function(x) { return sqr(x[0]+10*x[1])+5*sqr(x[2]-x[3])+sqr(sqr(x[1]-2*x[2]))+10*sqr(x[0]-x[3]); }; x0 = nu.div(nu.round(nu.mul(nu.uncmin(f,[3,-1,0,1]).solution,1e5)),1e5)");
run("f = function(x) { return sqr(10*(x[1]-x[0]*x[0]))+sqr(1-x[0])+90*sqr(x[3]-x[2]*x[2])+sqr(1-x[2])+10*sqr(x[1]+x[3]-2)+0.1*sqr(x[1]-x[3]); }; x0 = nu.uncmin(f,[-3,-1,-3,-1]).solution");
run("y = [0.1957,0.1947,0.1735,0.1600,0.0844,0.0627,0.0456,0.0342,0.0323,0.0235,0.0246]; u = [4,2,1,0.5,0.25,0.167,0.125,0.1,0.0833,0.0714,0.0625]; f = function(x) { var ret=0, i; for(i=0;i<11;++i) ret += sqr(y[i]-x[0]*(u[i]*u[i]+u[i]*x[1])/(u[i]*u[i]+u[i]*x[2]+x[3])); return ret; }; x0 = nu.uncmin(f,[0.25,0.39,0.415,0.39]).solution");
run("y = [0.844,0.908,0.932,0.936,0.925,0.908,0.881,0.850,0.818,0.784,0.751,0.718,0.685,0.658,0.628,0.603,0.580,0.558,0.538,0.522,0.506,0.490,0.478,0.467,0.457,0.448,0.438,0.431,0.424,0.420,0.414,0.411,0.406]; f = function(x) { var ret=0, i; for(i=0;i<33;++i) ret += sqr(y[i]-(x[0]+x[1]*Math.exp(-10*i*x[3])+x[2]*Math.exp(-10*i*x[4]))); return ret; }; x0 = nu.uncmin(f,[0.5,1.5,-1,0.01,0.02]).solution");
run("f = function(x) { var ret=0, i,ti,yi,exp=Math.exp; for(i=1;i<=13;++i) { ti = 0.1*i; yi = exp(-ti)-5*exp(-10*ti)+3*exp(-4*ti); ret += sqr(x[2]*exp(-ti*x[0])-x[3]*exp(-ti*x[1])+x[5]*exp(-ti*x[4])-yi); } return ret; }; x0 = nu.uncmin(f,[1,2,1,1,1,1],1e-14).solution; f(x0)<1e-20;");
run("z = []; cb = function(i,x,f,g,H) { z.push({i:i, x:x, f:f, g:g, H:H }); }; x0 = nu.uncmin(function(x) { return Math.cos(2*x[0]); },[1],1e-10,function(x) { return [-2*Math.sin(2*x[0])]; },100,cb)");
run("z");
// Solving ODEs
run("sol = nu.dopri(0,1,1,function(t,y) { return y; })");
run("sol.at([0.3,0.7])");
run("nu.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; }).at([0,0.5*Math.PI,Math.PI,1.5*Math.PI,2*Math.PI])");
run("nu.dopri(0,20,[2,0],function(t,y) { return [y[1], (1-y[0]*y[0])*y[1]-y[0]]; }).at([18,19,20])");
run("sol = nu.dopri(0,2,1,function (x,y) { return y; },1e-8,100,function (x,y) { return y-1.3; })");
run("sol = nu.dopri(0,2,1,function(x,y) { return y; },undefined,50,function(x,y) { return [y-1.5,Math.sin(y-1.5)]; })");
// Seedrandom (David Bau)
run("nu.seedrandom.seedrandom(3); nu.seedrandom.random()");
run("nu.seedrandom.random()");
run("nu.seedrandom.seedrandom(3); nu.seedrandom.random()");
執行結果
> A = [[1,2,3],[4,5,6]]
[[ 1, 2, 3],
[ 4, 5, 6]]
> x = [7,8,9]
[ 7, 8, 9]
> b = nu.dot(A,x)
[ 50, 122]
> y = [10,1,2]
[ 10, 1, 2]
> nu['+'](x,y)
[ 17, 9, 11]
> nu['>'](x,y)
[false,true,true]
> nu.add(x,y)
[ 17, 9, 11]
> nu.add([1,2],[3,4],[5,6],[7,8])
[ 16, 20]
> A = [[1,2,3],[4,5,6],[7,1,9]]
[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 1, 9]]
> nu.inv(A)
[[ -0.9286, 0.3571, 0.07143],
[ -0.1429, 0.2857, -0.1429],
[ 0.7381, -0.3095, 0.07143]]
> pi = 3.141592653589793
3.142
> nu.precision = 10
10
> pi
3.141592654
> nu.precision = 4
4
> pi
3.142
> nu.identity(100)
...Large Array...
> I4 = nu.identity(4)
[[ 1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1]]
> nu.largeArray = 2; I4
...Large Array...
> nu.largeArray =50; I4
[[ 1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1]]
> nu.exp([1,2])
[ 2.718, 7.389]
> nu.exp([[1,2],[3,4]])
[[ 2.718, 7.389],
[ 20.09, 54.6]]
> nu.abs([-2,3])
[ 2, 3]
> nu.acos([0.1,0.2])
[ 1.471, 1.369]
> nu.asin([0.1,0.2])
[ 0.1002, 0.2014]
> nu.atan([1,2])
[ 0.7854, 1.107]
> nu.atan2([1,2],[3,4])
[ 0.3218, 0.4636]
> nu.ceil([-2.2,3.3])
[ -2, 4]
> nu.floor([-2.2,3.3])
[ -3, 3]
> nu.log([1,2])
[ 0, 0.6931]
> nu.pow([2,3],[0.25,7.1])
[ 1.189, 2441]
> nu.round([-2.2,3.3])
[ -2, 3]
> nu.sin([1,2])
[ 0.8415, 0.9093]
> nu.sqrt([1,2])
[ 1, 1.414]
> nu.tan([1,2])
[ 1.557, -2.185]
> nu.dim([1,2])
[ 2]
> nu.dim([[1,2,3],[4,5,6]])
[ 2, 3]
> nu.same([1,2],[1,2])
true
> nu.same([1,2],[1,2,3])
false
> nu.same([1,2],[[1],[2]])
false
> nu.same([[1,2],[3,4]],[[1,2],[3,4]])
true
> nu.same([[1,2],[3,4]],[[1,2],[3,5]])
false
> nu.same([[1,2],[2,4]],[[1,2],[3,4]])
false
> nu.rep([3],5)
[ 5, 5, 5]
> nu.rep([2,3],0)
[[ 0, 0, 0],
[ 0, 0, 0]]
> sum = nu.mapreduce('accum += xi','0'); sum([1,2,3])
6
> sum([[1,2,3],[4,5,6]])
21
> nu.any([false,true])
true
> nu.any([[0,0,3.14],[0,false,0]])
true
> nu.any([0,0,false])
false
> nu.all([false,true])
false
> nu.all([[1,4,3.14],['no',true,-1]])
true
> nu.all([0,0,false])
false
> add = nu.pointwise(['x[i]','y[i]'],'ret[i] = x[i]+y[i];'); add([1,2],[3,4])
[ 4, 6]
> nu.diag([1,2,3])
[[ 1, 0, 0],
[ 0, 2, 0],
[ 0, 0, 3]]
> nu.identity(3)
[[ 1, 0, 0],
[ 0, 1, 0],
[ 0, 0, 1]]
> nu.random([2,3])
[[ 0.6544, 0.1819, 0.1794],
[ 0.09216, 0.7122, 0.9166]]
> nu.linspace(1,5)
[ 1, 2, 3, 4, 5]
> nu.linspace(1,3,5)
[ 1, 1.5, 2, 2.5, 3]
> nu.addVV([1,2],[3,4])
[ 4, 6]
> nu.addVS([1,2],3)
[ 4, 5]
> nu.add(1,[2,3])
[ 3, 4]
> nu.add([1,2,3],[4,5,6])
[ 5, 7, 9]
> nu.sub([1,2],[3,4])
[ -2, -2]
> nu.mul([1,2],[3,4])
[ 3, 8]
> nu.div([1,2],[3,4])
[ 0.3333, 0.5]
> v = [1,2,3,4]; nu.addeq(v,3); v
[ 4, 5, 6, 7]
> nu.subeq([1,2,3],[5,3,1])
[ -4, -1, 2]
> nu.neg([1,-2,3])
[ -1, 2, -3]
> nu.isFinite([10,NaN,Infinity])
[true,false,false]
> nu.isNaN([10,NaN,Infinity])
[false,true,false]
> nu.dotVV([1,2],[3,4])
11
> nu.dotVM([1,2],[[3,4],[5,6]])
[ 13, 16]
> nu.dotMV([[1,2],[3,4]],[5,6])
[ 17, 39]
> nu.dotMMbig([[1,2],[3,4]],[[5,6],[7,8]])
[[ 19, 22],
[ 43, 50]]
> nu.dotMMsmall([[1,2],[3,4]],[[5,6],[7,8]])
[[ 19, 22],
[ 43, 50]]
> nu.dot([1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9])
285
> nu.dot([1,2,3],[4,5,6])
32
> nu.dot([[1,2,3],[4,5,6]],[7,8,9])
[ 50, 122]
> nu.solve([[1,2],[3,4]],[17,39])
[ 5, 6]
> LU = nu.LU([[1,2],[3,4]])
{LU:
[[ 3, 4],
[ 0.3333, 0.6667]],
P:
[ 1, 1]}
> nu.LUsolve(LU,[17,39])
[ 5, 6]
> nu.det([[1,2],[3,4]])
-2
> nu.det([[6,8,4,2,8,5],[3,5,2,4,9,2],[7,6,8,3,4,5],[5,5,2,8,1,6],[3,2,2,4,2,2],[8,3,2,2,4,1]])
-1404
> nu.inv([[1,2],[3,4]])
[[ -2, 1],
[ 1.5, -0.5]]
> nu.transpose([[1,2,3],[4,5,6]])
[[ 1, 4],
[ 2, 5],
[ 3, 6]]
> nu.transpose([[1,2,3,4,5,6,7,8,9,10,11,12]])
[[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[ 10],
[ 11],
[ 12]]
> nu.norm2([1,2])
2.236
> nu.tensor([1,2],[3,4,5])
[[ 3, 4, 5],
[ 6, 8, 10]]
> nu.parseDate(['1/13/2013','2001-5-9, 9:31'])
[ 1.358e12, 9.894e11]
> nu.parseFloat(['12','0.1'])
[ 12, 0.1]
> nu.parseCSV('a,b,c\n1,2.3,.3\n4e6,-5.3e-8,6.28e+4')
[["a","b","c"],
[ 1, 2.3, 0.3],
[ 4e6, -5.3e-8, 62800]]
> nu.toCSV([[1.23456789123,2],[3,4]])
"1.23456789123, 2
3, 4
"
> z = new nu.T(3,4)
{x:
3,
y:
4}
> z.add(5)
{x:
8,
y:
4}
> w = new nu.T(2,8)
{x:
2,
y:
8}
> z.add(w)
{x:
5,
y:
12}
> z.mul(w)
{x:
-26,
y:
32}
> z.div(w)
{x:
0.5588,
y:
-0.2353}
> z.sub(w)
{x:
1,
y:
-4}
> z = new nu.T([1,2],[3,4])
{x:
[ 1, 2],
y:
[ 3, 4]}
> z.abs()
{x:
[ 3.162, 4.472],
y:
}
> z.conj()
{x:
[ 1, 2],
y:
[ -3, -4]}
> z.norm2()
5.477
> z.exp()
{x:
[ -2.691, -4.83],
y:
[ 0.3836, -5.592]}
> z.cos()
{x:
[ -1.528, -2.459],
y:
[ 0.1658, -2.745]}
> z.sin()
{x:
[ 0.2178, -2.847],
y:
[ 1.163, 2.371]}
> z.log()
{x:
[ 1.151, 1.498],
y:
[ 1.249, 1.107]}
> A = new nu.T([[1,2],[3,4]],[[0,1],[2,-1]])
{x:
[[ 1, 2],
[ 3, 4]],
y:
[[ 0, 1],
[ 2, -1]]}
> A.inv()
{x:
[[ 0.125, 0.125],
[ 0.25, 0]],
y:
[[ 0.5, -0.25],
[ -0.375, 0.125]]}
> A.inv().dot(A)
{x:
[[ 1, 0],
[ 0, 1]],
y:
[[ 0, -2.776e-17],
[ 0, 0]]}
> A.get([1,1])
{x:
4,
y:
-1}
> A.transpose()
{x:
[[ 1, 3],
[ 2, 4]],
y:
[[ 0, 2],
[ 1, -1]]}
> A.transjugate()
{x:
[[ 1, 3],
[ 2, 4]],
y:
[[ 0, -2],
[ -1, 1]]}
> nu.T.rep([2,2],new nu.T(2,3))
{x:
[[ 2, 2],
[ 2, 2]],
y:
[[ 3, 3],
[ 3, 3]]}
> A = [[1,2,5],[3,5,-1],[7,-3,5]]
[[ 1, 2, 5],
[ 3, 5, -1],
[ 7, -3, 5]]
> B = nu.eig(A)
{lambda:
{x:
[ -4.284, 9.027, 6.257],
y:
},
E:
{x:
[[ 0.7131, -0.5543, 0.4019],
[ -0.2987, -0.2131, 0.9139],
[ -0.6342, -0.8046, 0.057]],
y:
}}
> C = B.E.dot(nu.T.diag(B.lambda)).dot(B.E.inv())
{x:
[[ 1, 2, 5],
[ 3, 5, -1],
[ 7, -3, 5]],
y:
}
> A = [[22,10,2,3,7],[14,7,10,0,8],[-1,13,-1,-11,3],[-3,-2,13,-2,4],[9,8,1,-2,4],[9,1,-7,5,-1],[2,-6,6,5,1],[4,5,0,-2,2]]
[[ 22, 10, 2, 3, 7],
[ 14, 7, 10, 0, 8],
[ -1, 13, -1, -11, 3],
[ -3, -2, 13, -2, 4],
[ 9, 8, 1, -2, 4],
[ 9, 1, -7, 5, -1],
[ 2, -6, 6, 5, 1],
[ 4, 5, 0, -2, 2]]
> nu.svd(A)
{U:
[[ -0.7071, -0.1581, 0.1768, 0.2494, 0.4625],
[ -0.5303, -0.1581, -0.3536, 0.1556, -0.4984],
[ -0.1768, 0.7906, -0.1768, -0.1546, 0.3967],
[ -1.506e-17, -0.1581, -0.7071, -0.3277, 0.1],
[ -0.3536, 0.1581, 1.954e-15, -0.07265, -0.2084],
[ -0.1768, -0.1581, 0.5303, -0.5726, -0.05555],
[ -7.109e-18, -0.4743, -0.1768, -0.3142, 0.4959],
[ -0.1768, 0.1581, 1.915e-15, -0.592, -0.2791]],
S:
[ 35.33, 20, 19.6, 0, 0],
V:
[[ -0.8006, -0.3162, 0.2887, -0.4191, 0],
[ -0.4804, 0.6325, 7.768e-15, 0.4405, 0.4185],
[ -0.1601, -0.3162, -0.866, -0.052, 0.3488],
[ 4.684e-17, -0.6325, 0.2887, 0.6761, 0.2442],
[ -0.3203, 3.594e-15, -0.2887, 0.413, -0.8022]]}
> A = [[1,2,0],[0,3,0],[2,0,5]]; SA = nu.ccsSparse(A);
[[ 0, 2, 4, 5],
[ 0, 2, 0, 1, 2],
[ 1, 2, 2, 3, 5]]
> A = nu.ccsSparse([[ 3, 5, 8,10, 8],[ 7,10, 3, 5, 3], [ 6, 3, 5, 1, 8], [ 2, 6, 7, 1, 2], [ 1, 2, 9, 3, 9]])
[[ 0, 5, 10, 15, 20, 25],
[ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
[ 3, 7, 6, 2, 1, 5, 10, 3, 6, 2, 8, 3, 5, 7, 9, 10, 5, 1, 1, 3, 8, 3, 8, 2, 9]]
> nu.ccsFull(A)
[[ 3, 5, 8, 10, 8],
[ 7, 10, 3, 5, 3],
[ 6, 3, 5, 1, 8],
[ 2, 6, 7, 1, 2],
[ 1, 2, 9, 3, 9]]
> nu.ccsDot(nu.ccsSparse([[1,2,3],[4,5,6]]),nu.ccsSparse([[7,8],[9,10],[11,12]]))
[[ 0, 2, 4],
[ 0, 1, 0, 1],
[ 58, 139, 64, 154]]
> M = [[0,1,3,6],[0,0,1,0,1,2],[3,-1,2,3,-2,4]]; b = [9,3,2]; x = nu.ccsTSolve(M,b)
[ 3.167, 2, 0.5]
> nu.ccsDot(M,[[0,3],[0,1,2],x])
[[ 0, 3],
[ 0, 1, 2],
[ 9, 3, 2]]
> LUP = nu.ccsLUP(A)
{L:
[[ 0, 5, 9, 12, 14, 15],
[ 0, 2, 4, 1, 3, 1, 3, 4, 2, 2, 4, 3, 3, 4, 4],
[ 1, 0.1429, 0.2857, 0.8571, 0.4286, 1, -0.1282, -0.5641, -0.1026, 1, 0.8517, 0.7965, 1, -0.67, 1]],
U:
[[ 0, 1, 3, 6, 10, 15],
[ 0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4],
[ 7, 10, -5.571, 3, 2.429, 8.821, 5, -3.286, 1.949, 5.884, 3, 5.429, 9.128, 0.1395, -3.476]],
P:
[ 1, 2, 4, 0, 3],
Pinv:
[ 3, 0, 1, 4, 2]}
> nu.ccsFull(nu.ccsDot(LUP.L,LUP.U))
[[ 7, 10, 3, 5, 3],
[ 6, 3, 5, 1, 8],
[ 1, 2, 9, 3, 9],
[ 3, 5, 8, 10, 8],
[ 2, 6, 7, 1, 2]]
> x = nu.ccsLUPSolve(LUP,[96,63,82,51,89])
[ 3, 1, 4, 1, 5]
> X = nu.trunc(nu.ccsFull(nu.ccsLUPSolve(LUP,A)),1e-15)
[[ 1, 0, 0, 0, 0],
[ 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, 1]]
> nu.ccsLUP(A,0.4).P
[ 0, 2, 1, 3, 4]
> A = nu.ccsSparse([[1,2,0],[0,3,0],[0,0,5]])
[[ 0, 1, 3, 4],
[ 0, 0, 1, 2],
[ 1, 2, 3, 5]]
> B = nu.ccsSparse([[2,9,0],[0,4,0],[-2,0,0]])
[[ 0, 2, 4, 4],
[ 0, 2, 0, 1],
[ 2, -2, 9, 4]]
> nu.ccsadd(A,B)
[[ 0, 2, 4, 5],
[ 0, 2, 0, 1, 2],
[ 3, -2, 11, 7, 5]]
> X = [[0,0,1,1,2,2],[0,1,1,2,2,3],[1,2,3,4,5,6]]
[[ 0, 0, 1, 1, 2, 2],
[ 0, 1, 1, 2, 2, 3],
[ 1, 2, 3, 4, 5, 6]]
> SX = nu.ccsScatter(X)
[[ 0, 1, 3, 5, 6],
[ 0, 0, 1, 1, 2, 2],
[ 1, 2, 3, 4, 5, 6]]
> nu.ccsGather(SX)
[[ 0, 0, 1, 1, 2, 2],
[ 0, 1, 1, 2, 2, 3],
[ 1, 2, 3, 4, 5, 6]]
> lu = nu.cLU([[0,0,1,1,1,2,2],[0,1,0,1,2,1,2],[2,-1,-1,2,-1,-1,2]])
{U:
[[ 0, 0, 1, 1, 2],
[ 0, 1, 1, 2, 2],
[ 2, -1, 1.5, -1, 1.333]],
L:
[[ 0, 1, 1, 2, 2],
[ 0, 0, 1, 1, 2],
[ 1, -0.5, 1, -0.6667, 1]]}
> nu.cLUsolve(lu,[5,-8,13])
[ 3, 1, 7]
> g = nu.cgrid(5)
[[ -1, -1, -1, -1, -1],
[ -1, 0, 1, 2, -1],
[ -1, 3, 4, 5, -1],
[ -1, 6, 7, 8, -1],
[ -1, -1, -1, -1, -1]]
> coordL = nu.cdelsq(g)
[[ 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8],
[ 1, 3, 0, 0, 2, 4, 1, 1, 5, 2, 0, 4, 6, 3, 1, 3, 5, 7, 4, 2, 4, 8, 5, 3, 7, 6, 4, 6, 8, 7, 5, 7, 8],
[ -1, -1, 4, -1, -1, -1, 4, -1, -1, 4, -1, -1, -1, 4, -1, -1, -1, -1, 4, -1, -1, -1, 4, -1, -1, 4, -1, -1, -1, 4, -1, -1, 4]]
> L = nu.sscatter(coordL)
[[ 4, -1, , -1],
[ -1, 4, -1, , -1],
[ , -1, 4, , , -1],
[ -1, , , 4, -1, , -1],
[ , -1, , -1, 4, -1, , -1],
[ , , -1, , -1, 4, , , -1],
[ , , , -1, , , 4, -1],
[ , , , , -1, , -1, 4, -1],
[ , , , , , -1, , -1, 4]]
> lu = nu.cLU(coordL); x = nu.cLUsolve(lu,[1,1,1,1,1,1,1,1,1]);
[ 0.6875, 0.875, 0.6875, 0.875, 1.125, 0.875, 0.6875, 0.875, 0.6875]
> nu.cdotMV(coordL,x)
[ 1, 1, 1, 1, 1, 1, 1, 1, 1]
> G = nu.rep([5,5],0); for(i=0;i<5;i++) for(j=0;j<5;j++) if(g[i][j]>=0) G[i][j] = x[g[i][j]]; G
[[ 0, 0, 0, 0, 0],
[ 0, 0.6875, 0.875, 0.6875, 0],
[ 0, 0.875, 1.125, 0.875, 0],
[ 0, 0.6875, 0.875, 0.6875, 0],
[ 0, 0, 0, 0, 0]]
> nu.imageURL(nu.mul([G,G,G],200))
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAcElEQVQIHQARAO7/AAAAAAAAAAAAAAAAAAAAAAAAEADv/wAAAIqKiq+vr4mJiQAAAAAAEADv/wAAAK+vr+Hh4a+vrwAAAAAAEADv/wAAAIqKiq+vr4qKigAAAAABEADv/wAAAAAAAAAAAAAAAAAAAACRjRFNqL3leAAAAABJRU5ErkJggg=="
> nu.cgrid(6,'L')
[[ -1, -1, -1, -1, -1, -1],
[ -1, 0, 1, -1, -1, -1],
[ -1, 2, 3, -1, -1, -1],
[ -1, 4, 5, 6, 7, -1],
[ -1, 8, 9, 10, 11, -1],
[ -1, -1, -1, -1, -1, -1]]
> nu.cgrid(5,function(i,j) { return i!==2 || j!==2; })
[[ -1, -1, -1, -1, -1],
[ -1, 0, 1, 2, -1],
[ -1, 3, -1, 4, -1],
[ -1, 5, 6, 7, -1],
[ -1, -1, -1, -1, -1]]
> nu.spline([1,2,3,4,5],[1,2,1,3,2]).at(nu.linspace(1,5,10))
[ 1, 1.731, 2.039, 1.604, 1.019, 1.294, 2.364, 3.085, 2.82, 2]
> nu.spline([1,2,3,4,5],[1,2,1,3,2],0,0).at(nu.linspace(1,5,10))
[ 1, 1.435, 1.98, 1.669, 1.034, 1.316, 2.442, 3.017, 2.482, 2]
> nu.spline([1,2,3,4],[0.8415,0.04718,-0.8887,0.8415],'periodic').at(nu.linspace(1,4,10))
[ 0.8415, 0.9024, 0.5788, 0.04718, -0.5106, -0.8919, -0.8887, -0.3918, 0.3131, 0.8415]
> nu.spline([1,2,3],[[0,1],[1,0],[0,1]]).at(1.78)
[ 0.9327, 0.06728]
> xs = [0,1,2,3]; nu.spline(xs,nu.sin(xs)).diff().at(1.5)
0.07302
> xs = nu.linspace(0,30,31); ys = nu.sin(xs); s = nu.spline(xs,ys).roots()
[ 0, 3.142, 6.284, 9.425, 12.57, 15.71, 18.85, 21.99, 25.13, 28.27]
> z = (new nu.T([1,2,3,4,5],[6,7,8,9,10])).fft()
{x:
[ 15, -5.941, -3.312, -1.688, 0.941],
y:
[ 40, 0.941, -1.688, -3.312, -5.941]}
> z.ifft()
{x:
[ 1, 2, 3, 4, 5],
y:
[ 6, 7, 8, 9, 10]}
> nu.solveQP([[1,0,0],[0,1,0],[0,0,1]],[0,5,0],[[-4,2,0],[-3,1,-2],[0,0,1]],[-8,2,0])
{solution:
[ 0.4762, 1.048, 2.095],
value:
[ -2.381],
unconstrained_solution:
[ 0, 5, 0],
iterations:
[ 3, 0],
iact:
[ 3, 2, 0],
message:
""}
> sqr = function(x) { return x*x; }; nu.uncmin(function(x) { return sqr(10*(x[1]-x[0]*x[0])) + sqr(1-x[0]); },[-1.2,1]).solution
[ 1, 1]
> f = function(x) { return sqr(-13+x[0]+((5-x[1])*x[1]-2)*x[1])+sqr(-29+x[0]+((x[1]+1)*x[1]-14)*x[1]); }; x0 = nu.uncmin(f,[0.5,-2]).solution
[ 11.41, -0.8968]
> f = function(x) { return sqr(1e4*x[0]*x[1]-1)+sqr(Math.exp(-x[0])+Math.exp(-x[1])-1.0001); }; x0 = nu.uncmin(f,[0,1]).solution
[ 1.098e-5, 9.106]
> f = function(x) { return sqr(x[0]-1e6)+sqr(x[1]-2e-6)+sqr(x[0]*x[1]-2)}; x0 = nu.uncmin(f,[0,1]).solution
[ 1e6, 2e-6]
> f = function(x) { return sqr(1.5-x[0]*(1-x[1]))+sqr(2.25-x[0]*(1-x[1]*x[1]))+sqr(2.625-x[0]*(1-x[1]*x[1]*x[1])); }; x0 = nu.uncmin(f,[1,1]).solution
[ 3, 0.5]
> f = function(x) { var ret = 0,i; for(i=1;i<=10;i++) ret+=sqr(2+2*i-Math.exp(i*x[0])-Math.exp(i*x[1])); return ret; }; x0 = nu.uncmin(f,[0.3,0.4]).solution
[ 0.2578, 0.2578]
> y = [0.14,0.18,0.22,0.25,0.29,0.32,0.35,0.39,0.37,0.58,0.73,0.96,1.34,2.10,4.39]; f = function(x) { var ret = 0,i; for(i=1;i<=15;i++) ret+=sqr(y[i-1]-(x[0]+i/((16-i)*x[1]+Math.min(i,16-i)*x[2]))); return ret; }; x0 = nu.uncmin(f,[1,1,1]).solution
[ 0.08241, 1.133, 2.344]
> y = [0.0009,0.0044,0.0175,0.0540,0.1295,0.2420,0.3521,0.3989,0.3521,0.2420,0.1295,0.0540,0.0175,0.0044,0.0009]; f = function(x) { var ret = 0,i; for(i=1;i<=15;i++) ret+=sqr(x[0]*Math.exp(-x[1]*sqr((8-i)/2-x[2])/2)-y[i-1]); return ret; }; x0 = nu.div(nu.round(nu.mul(nu.uncmin(f,[1,1,1]).solution,1000)),1000)
[ 0.399, 1, 0]
> f = function(x) { return sqr(x[0]+10*x[1])+5*sqr(x[2]-x[3])+sqr(sqr(x[1]-2*x[2]))+10*sqr(x[0]-x[3]); }; x0 = nu.div(nu.round(nu.mul(nu.uncmin(f,[3,-1,0,1]).solution,1e5)),1e5)
[ 0, 0, 0, 0]
> f = function(x) { return sqr(10*(x[1]-x[0]*x[0]))+sqr(1-x[0])+90*sqr(x[3]-x[2]*x[2])+sqr(1-x[2])+10*sqr(x[1]+x[3]-2)+0.1*sqr(x[1]-x[3]); }; x0 = nu.uncmin(f,[-3,-1,-3,-1]).solution
[ 1, 1, 1, 1]
> y = [0.1957,0.1947,0.1735,0.1600,0.0844,0.0627,0.0456,0.0342,0.0323,0.0235,0.0246]; u = [4,2,1,0.5,0.25,0.167,0.125,0.1,0.0833,0.0714,0.0625]; f = function(x) { var ret=0, i; for(i=0;i<11;++i) ret += sqr(y[i]-x[0]*(u[i]*u[i]+u[i]*x[1])/(u[i]*u[i]+u[i]*x[2]+x[3])); return ret; }; x0 = nu.uncmin(f,[0.25,0.39,0.415,0.39]).solution
[ 0.1928, 0.1913, 0.1231, 0.1361]
> y = [0.844,0.908,0.932,0.936,0.925,0.908,0.881,0.850,0.818,0.784,0.751,0.718,0.685,0.658,0.628,0.603,0.580,0.558,0.538,0.522,0.506,0.490,0.478,0.467,0.457,0.448,0.438,0.431,0.424,0.420,0.414,0.411,0.406]; f = function(x) { var ret=0, i; for(i=0;i<33;++i) ret += sqr(y[i]-(x[0]+x[1]*Math.exp(-10*i*x[3])+x[2]*Math.exp(-10*i*x[4]))); return ret; }; x0 = nu.uncmin(f,[0.5,1.5,-1,0.01,0.02]).solution
[ 0.3754, 1.936, -1.465, 0.01287, 0.02212]
> f = function(x) { var ret=0, i,ti,yi,exp=Math.exp; for(i=1;i<=13;++i) { ti = 0.1*i; yi = exp(-ti)-5*exp(-10*ti)+3*exp(-4*ti); ret += sqr(x[2]*exp(-ti*x[0])-x[3]*exp(-ti*x[1])+x[5]*exp(-ti*x[4])-yi); } return ret; }; x0 = nu.uncmin(f,[1,2,1,1,1,1],1e-14).solution; f(x0)<1e-20;
true
> z = []; cb = function(i,x,f,g,H) { z.push({i:i, x:x, f:f, g:g, H:H }); }; x0 = nu.uncmin(function(x) { return Math.cos(2*x[0]); },[1],1e-10,function(x) { return [-2*Math.sin(2*x[0])]; },100,cb)
{solution:
[ 1.571],
f:
-1,
gradient:
[ 2.242e-11],
invHessian:
[[ 0.25]],
iterations:
6,
message:
"Newton step smaller than tol"}
> z
[{i:
0,
x:
[ 1],
f:
-0.4161,
g:
[ -1.819],
H:
[[ 1]]},
{i:
2,
x:
[ 1.909],
f:
-0.7795,
g:
[ 1.253],
H:
[[ 0.296]]},
{i:
3,
x:
[ 1.538],
f:
-0.9979,
g:
[ -0.1296],
H:
[[ 0.2683]]},
{i:
4,
x:
[ 1.573],
f:
-1,
g:
[ 9.392e-3],
H:
[[ 0.2502]]},
{i:
5,
x:
[ 1.571],
f:
-1,
g:
[ -6.105e-6],
H:
[[ 0.25]]},
{i:
6,
x:
[ 1.571],
f:
-1,
g:
[ 2.242e-11],
H:
[[ 0.25]]}]
> sol = nu.dopri(0,1,1,function(t,y) { return y; })
{x:
[ 0, 0.1, 0.1522, 0.361, 0.5792, 0.7843, 0.9813, 1],
y:
[ 1, 1.105, 1.164, 1.435, 1.785, 2.191, 2.668, 2.718],
f:
[ 1, 1.105, 1.164, 1.435, 1.785, 2.191, 2.668, 2.718],
ymid:
[ 1.051, 1.134, 1.293, 1.6, 1.977, 2.418, 2.693],
iterations:
8,
events:
,
message:
""}
> sol.at([0.3,0.7])
[ 1.35, 2.014]
> nu.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; }).at([0,0.5*Math.PI,Math.PI,1.5*Math.PI,2*Math.PI])
[[ 3, 0],
[ -9.534e-8, -3],
[ -3, 2.768e-7],
[ 3.63e-7, 3],
[ 3, -3.065e-7]]
> nu.dopri(0,20,[2,0],function(t,y) { return [y[1], (1-y[0]*y[0])*y[1]-y[0]]; }).at([18,19,20])
[[ -1.208, 0.9916],
[ 0.4258, 2.535],
[ 2.008, -0.04251]]
> sol = nu.dopri(0,2,1,function (x,y) { return y; },1e-8,100,function (x,y) { return y-1.3; })
{x:
[ 0, 0.0181, 0.09051, 0.1822, 0.2624],
y:
[ 1, 1.018, 1.095, 1.2, 1.3],
f:
[ 1, 1.018, 1.095, 1.2, 1.3],
ymid:
[ 1.009, 1.056, 1.146, 1.249],
iterations:
5,
events:
true,
message:
""}
> sol = nu.dopri(0,2,1,function(x,y) { return y; },undefined,50,function(x,y) { return [y-1.5,Math.sin(y-1.5)]; })
{x:
[ 0, 0.2, 0.4055],
y:
[ 1, 1.221, 1.5],
f:
[ 1, 1.221, 1.5],
ymid:
[ 1.105, 1.354],
iterations:
2,
events:
[true,true],
message:
""}
> nu.seedrandom.seedrandom(3); nu.seedrandom.random()
0.7569
> nu.seedrandom.random()
0.6139
> nu.seedrandom.seedrandom(3); nu.seedrandom.random()
0.7569
Facebook
|
Post preview:
Close preview