Permalink
Browse files

quadratic programming mainly

  • Loading branch information...
1 parent 9473592 commit 85ddf0090cc749181b16ccccc057c3454b86c8fc Sébastien Loisel committed Dec 28, 2011
View
@@ -414,9 +414,15 @@ <h1>Data manipulation</h1>
[[ "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
+"
</pre>
+Note that <tt>numeric.toCSV()</tt> uses <tt>numeric.prettyPrint()</tt> to print numbers. To obtain more
+digits, set <tt>numeric.precision</tt> 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):
<pre>
> numeric.getURL('tools/helloworld.txt').responseText
"Hello, world!"
@@ -572,6 +578,19 @@ <h1>Sparse linear algebra</h1>
> sparse.LUPsolve(lup,b)
[3,1,7]
</pre>
+
+<!--
+Some more tests.
+<pre>
+> sparse.dot([1,2,3],[,4,5])
+23
+> sparse.dot([1, ,3],[[4,5,],[,6,7],[1,,8]])
+[7,5,24]
+> sparse.dot([[3,1],[4,5,9],[,3,2]],[7,3])
+[24,43,9]
+</pre>
+-->
+
The <tt>sparse.scatter()</tt> and <tt>sparse.gather()</tt> functions can be used to convert between
sparse matrices and the coordinate encoding:
<pre>
@@ -637,15 +656,67 @@ <h1>Solving PDEs</h1>
[1,1,1,1,1,1,1,1,1]
</pre>
+You can also work on an L-shaped or arbitrary-shape domain:
+<pre>
+> 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]]
+</pre>
+
+<h1>Quadratic Programming (Alberto Santini)</h1>
+
+The Quadratic Programming function <tt>numeric.solveQP()</tt> is based on <a href="https://github.com/albertosantini/node-quadprog">Alberto Santini's
+quadprog</a>, which is itself a port of the corresponding
+R routines.
+
+<pre>
+> 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:"" }
+</pre>
+
<!--
<pre>
-> sparse.dot([1,2,3],[,4,5])
-23
-> sparse.dot([1, ,3],[[4,5,],[,6,7],[1,,8]])
-[7,5,24]
-> sparse.dot([[3,1],[4,5,9],[,3,2]],[7,3])
-[24,43,9]
+> D = numeric.identity(8); d = numeric.rep([8],0); A = [[1, 1, -1, 0, 0, 0, 0, 0, 0, 0],[-1, 1, 0, 1, 0, 0, 0, 0, 0, 0],[1, 1, 0, 0, -1, 0, 0, 0, 0, 0],[-1, 1, 0, 0, 0, 1, 0, 0, 0, 0],[1, 1, 0, 0, 0, 0, -1, 0, 0, 0],[-1, 1, 0, 0, 0, 0, 0, 1, 0, 0],[1, 1, 0, 0, 0, 0, 0, 0, -1, 0],[-1, 1, 0, 0, 0, 0, 0, 0, 0, 1]]; b = [1, 1, -1, 0, -1, 0, -1, 0, -1, 0]; numeric.solveQP(D,d,A,b,undefined,2)
+{ solution: [0.25,0,0.25,0,0.25,0,0.25,0],
+ value: [0.125],
+ unconstrained_solution:[0,0,0,0,0,0,0,0],
+ iterations: [3,0],
+ iact: [1,2,0,0,0,0,0,0,0,0],
+ message: ""}
</pre>
-->
+<h1>Seedrandom (David Bau)</h1>
+
+The object <tt>numeric.seedrandom</tt> is based on
+<a href="http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html">David Bau's <tt>seedrandom.js</tt></a>.
+This small library can be used to create better pseudorandom numbers than <tt>Math.random()</tt> which can
+furthermore be "seeded".
+<pre>
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+> numeric.seedrandom.random()
+0.6139
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+</pre>
+For performance reasons, <tt>numeric.random()</tt> uses the default <tt>Math.random()</tt>. If you want to use
+the seedrandom version, just do <tt>Math.random = numeric.seedrandom.random</tt>. Note that this may slightly
+decrease the performance of all <tt>Math</tt> operations.
+
<br><br><br>
File renamed without changes.
View
@@ -1,3 +1,5 @@
+(See also licenses in the resources subdirectory.)
+
Numeric Javascript
Copyright (C) 2011 by Sébastien Loisel
View
@@ -0,0 +1,23 @@
+(The MIT License)
+
+Copyright (c) 2011 Alberto Santini <albertosantini@gmail.com>
+
+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.
+
View
@@ -7,6 +7,7 @@ body {
font-size: 18px;
height: 100%;
}
+pre { font-size:15px; }
h1 {
font-size: 22px;
}
View
@@ -414,9 +414,15 @@ <h1>Data manipulation</h1>
[[ "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
+"
</pre>
+Note that <tt>numeric.toCSV()</tt> uses <tt>numeric.prettyPrint()</tt> to print numbers. To obtain more
+digits, set <tt>numeric.precision</tt> 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):
<pre>
> numeric.getURL('tools/helloworld.txt').responseText
"Hello, world!"
@@ -572,6 +578,19 @@ <h1>Sparse linear algebra</h1>
> sparse.LUPsolve(lup,b)
[3,1,7]
</pre>
+
+<!--
+Some more tests.
+<pre>
+> sparse.dot([1,2,3],[,4,5])
+23
+> sparse.dot([1, ,3],[[4,5,],[,6,7],[1,,8]])
+[7,5,24]
+> sparse.dot([[3,1],[4,5,9],[,3,2]],[7,3])
+[24,43,9]
+</pre>
+-->
+
The <tt>sparse.scatter()</tt> and <tt>sparse.gather()</tt> functions can be used to convert between
sparse matrices and the coordinate encoding:
<pre>
@@ -637,15 +656,67 @@ <h1>Solving PDEs</h1>
[1,1,1,1,1,1,1,1,1]
</pre>
+You can also work on an L-shaped or arbitrary-shape domain:
+<pre>
+> 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]]
+</pre>
+
+<h1>Quadratic Programming (Alberto Santini)</h1>
+
+The Quadratic Programming function <tt>numeric.solveQP()</tt> is based on <a href="https://github.com/albertosantini/node-quadprog">Alberto Santini's
+quadprog</a>, which is itself a port of the corresponding
+R routines.
+
+<pre>
+> 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:"" }
+</pre>
+
<!--
<pre>
-> sparse.dot([1,2,3],[,4,5])
-23
-> sparse.dot([1, ,3],[[4,5,],[,6,7],[1,,8]])
-[7,5,24]
-> sparse.dot([[3,1],[4,5,9],[,3,2]],[7,3])
-[24,43,9]
+> D = numeric.identity(8); d = numeric.rep([8],0); A = [[1, 1, -1, 0, 0, 0, 0, 0, 0, 0],[-1, 1, 0, 1, 0, 0, 0, 0, 0, 0],[1, 1, 0, 0, -1, 0, 0, 0, 0, 0],[-1, 1, 0, 0, 0, 1, 0, 0, 0, 0],[1, 1, 0, 0, 0, 0, -1, 0, 0, 0],[-1, 1, 0, 0, 0, 0, 0, 1, 0, 0],[1, 1, 0, 0, 0, 0, 0, 0, -1, 0],[-1, 1, 0, 0, 0, 0, 0, 0, 0, 1]]; b = [1, 1, -1, 0, -1, 0, -1, 0, -1, 0]; numeric.solveQP(D,d,A,b,undefined,2)
+{ solution: [0.25,0,0.25,0,0.25,0,0.25,0],
+ value: [0.125],
+ unconstrained_solution:[0,0,0,0,0,0,0,0],
+ iterations: [3,0],
+ iact: [1,2,0,0,0,0,0,0,0,0],
+ message: ""}
</pre>
-->
+<h1>Seedrandom (David Bau)</h1>
+
+The object <tt>numeric.seedrandom</tt> is based on
+<a href="http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html">David Bau's <tt>seedrandom.js</tt></a>.
+This small library can be used to create better pseudorandom numbers than <tt>Math.random()</tt> which can
+furthermore be "seeded".
+<pre>
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+> numeric.seedrandom.random()
+0.6139
+> numeric.seedrandom.seedrandom(3); numeric.seedrandom.random()
+0.7569
+</pre>
+For performance reasons, <tt>numeric.random()</tt> uses the default <tt>Math.random()</tt>. If you want to use
+the seedrandom version, just do <tt>Math.random = numeric.seedrandom.random</tt>. Note that this may slightly
+decrease the performance of all <tt>Math</tt> operations.
+
<br><br><br>
View
@@ -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<m;i++) {
+ row = [];
+ for(j=0;j<m;j++) { row[j] = numeric.prettyPrint(A[i][j]); }
+ ret[i] = row.join(', ');
+ }
+ return ret.join('\n')+'\n';
+}
+
numeric.getURL = function getURL(url) {
var client = new XMLHttpRequest();
client.open("GET",url,false);
@@ -1565,20 +1579,21 @@ coord.LUsolve = function LUsolve(lu,b) {
};
coord.grid = function grid(n,shape) {
- var ret = numeric.rep([n,n],-1);
+ if(typeof n === "number") n = [n,n];
+ var ret = numeric.rep(n,-1);
var i,j,count;
if(typeof shape !== "function") {
switch(shape) {
case 'L':
- shape = function(i,j) { return i!=0 && i!=(n-1) && j!=0 && j!=(n-1) && (i<n/2 || j<n/2); }
+ shape = function(i,j) { return (i>=n[0]/2 || j<n[1]/2); }
break;
default:
- shape = function(i,j) { return i!=0 && i!=(n-1) && j!=0 && j!=(n-1); };
+ shape = function(i,j) { return true; };
break;
}
}
count=0;
- for(i=0;i<n;i++) for(j=0;j<n;j++)
+ for(i=1;i<n[0]-1;i++) for(j=1;j<n[1]-1;j++)
if(shape(i,j)) {
ret[i][j] = count;
count++;
Oops, something went wrong.

0 comments on commit 85ddf00

Please sign in to comment.