# sloisel/numeric

1 parent 9473592 commit 85ddf0090cc749181b16ccccc057c3454b86c8fc Sébastien Loisel committed Dec 28, 2011
 @@ -414,9 +414,15 @@

Data manipulation

[[ "a", "b", "c"], [ 1, 2.3, 0.3], [ 4.000e6, -5.3e-8, 62800]] +> numeric.toCSV([[1.23456789123,2],[3,4]]) +"1.235,2 + 3,4 +" +Note that numeric.toCSV() uses numeric.prettyPrint() to print numbers. To obtain more +digits, set numeric.precision to the desired number of digits of accuracy. -You can also fetch a URL: +You can also fetch a URL (a thin wrapper around XMLHttpRequest):
> numeric.getURL('tools/helloworld.txt').responseText
"Hello, world!"
@@ -572,6 +578,19 @@

Sparse linear algebra

> sparse.LUPsolve(lup,b) [3,1,7]
+ + + The sparse.scatter() and sparse.gather() functions can be used to convert between sparse matrices and the coordinate encoding:
@@ -637,15 +656,67 @@

Solving PDEs

[1,1,1,1,1,1,1,1,1]
+You can also work on an L-shaped or arbitrary-shape domain: +
+> coord.grid(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]]
+> coord.grid(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]]
+
+ +

+ +The Quadratic Programming function numeric.solveQP() is based on Alberto Santini's +quadprog, which is itself a port of the corresponding +R routines. + +
+> numeric.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:"" }
+
+ +

Seedrandom (David Bau)

+ +The object numeric.seedrandom is based on +David Bau's seedrandom.js. +This small library can be used to create better pseudorandom numbers than Math.random() which can +furthermore be "seeded". +
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+> numeric.seedrandom.random()
+0.6139
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+
+For performance reasons, numeric.random() uses the default Math.random(). If you want to use +the seedrandom version, just do Math.random = numeric.seedrandom.random. Note that this may slightly +decrease the performance of all Math operations. +

File renamed without changes.
 @@ -0,0 +1,23 @@ +(The MIT License) + +Copyright (c) 2011 Alberto Santini + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +
 @@ -7,6 +7,7 @@ body { font-size: 18px; height: 100%; } +pre { font-size:15px; } h1 { font-size: 22px; }
 @@ -414,9 +414,15 @@

Data manipulation

[[ "a", "b", "c"], [ 1, 2.3, 0.3], [ 4.000e6, -5.3e-8, 62800]] +> numeric.toCSV([[1.23456789123,2],[3,4]]) +"1.235,2 + 3,4 +" +Note that numeric.toCSV() uses numeric.prettyPrint() to print numbers. To obtain more +digits, set numeric.precision to the desired number of digits of accuracy. -You can also fetch a URL: +You can also fetch a URL (a thin wrapper around XMLHttpRequest):
> numeric.getURL('tools/helloworld.txt').responseText
"Hello, world!"
@@ -572,6 +578,19 @@

Sparse linear algebra

> sparse.LUPsolve(lup,b) [3,1,7]
+ + + The sparse.scatter() and sparse.gather() functions can be used to convert between sparse matrices and the coordinate encoding:
@@ -637,15 +656,67 @@

Solving PDEs

[1,1,1,1,1,1,1,1,1]
+You can also work on an L-shaped or arbitrary-shape domain: +
+> coord.grid(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]]
+> coord.grid(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]]
+
+ +

+ +The Quadratic Programming function numeric.solveQP() is based on Alberto Santini's +quadprog, which is itself a port of the corresponding +R routines. + +
+> numeric.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:"" }
+
+ +

Seedrandom (David Bau)

+ +The object numeric.seedrandom is based on +David Bau's seedrandom.js. +This small library can be used to create better pseudorandom numbers than Math.random() which can +furthermore be "seeded". +
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+> numeric.seedrandom.random()
+0.6139
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+
+For performance reasons, numeric.random() uses the default Math.random(). If you want to use +the seedrandom version, just do Math.random = numeric.seedrandom.random. Note that this may slightly +decrease the performance of all Math operations. +

 @@ -121,6 +121,20 @@ numeric.parseCSV = function parseCSV(t) { return ret; } +numeric.toCSV = function toCSV(A) { + var s = numeric.dim(A); + var i,j,m,n,row,ret; + m = s[0]; + n = s[1]; + ret = []; + for(i=0;i=n[0]/2 || j