Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisumbel committed Dec 20, 2011
0 parents commit 73f8f52
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .lock-wscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
argv = ['/usr/local/bin/node-waf', 'configure']
blddir = '/Users/kilnaar/src/node-lapack/build'
commands = {'dist': 0, 'configure': True, 'distcheck': 0, 'install': 0, 'build': 0, 'clean': 0, 'distclean': 0, 'check': 0, 'uninstall': 0}
cwd = '/Users/kilnaar/src/node-lapack'
environ = {'SSH_AUTH_SOCK': '/tmp/launch-eKeJxY/Listeners', 'TORQUEBOX_HOME': '/usr/local/torquebox-current', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'TERM_PROGRAM_VERSION': '273.1', 'DYLD_LIBRARY_PATH': '/usr/local/cuda/lib:', 'SCALA_HOME': '/usr/local/scala-2.8.1.final', 'SHELL': '/bin/bash', 'LOGNAME': 'kilnaar', 'USER': 'kilnaar', 'PATH': '/usr/local/cuda/bin:/usr/local/ironruby/bin:/opt/mongodb/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin:/usr/local/scala-2.8.1.final/bin:/usr/local/share/npm/bin:/usr/local/torquebox-current/jruby/bin:/usr/local/lib/node_modules/jasmine-node/bin:/Applications/Octave.app/Contents/Resources/bin', 'HOME': '/Users/kilnaar', 'DISPLAY': '/tmp/launch-T01JUM/org.x:0', '_': '/usr/local/bin/node-waf', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-color', 'Apple_PubSub_Socket_Render': '/tmp/launch-RrwDpk/Render', 'VERSIONER_PYTHON_VERSION': '2.6', 'SHLVL': '1', 'OLDPWD': '/Users/kilnaar/src', 'NODE_PATH': ':/usr/local/lib/node', '__CF_USER_TEXT_ENCODING': '0x1F5:0:0', 'PWD': '/Users/kilnaar/src/node-lapack', 'TMPDIR': '/var/folders/QA/QAKUzvpTFYmb676sUSrldU+++TI/-Tmp-/', 'JBOSS_HOME': '/usr/local/torquebox-current/jboss', 'JRUBY_HOME': '/usr/local/torquebox-current/jruby', 'COMMAND_MODE': 'unix2003'}
files = []
hash = 0
options = {'compile_targets': None, 'force': False, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'destdir': '', 'keep': False, 'zones': '', 'blddir': '', 'prefix': '/usr/local/', 'jobs': 4, 'srcdir': '', 'check_cxx_compiler': 'g++'}
srcdir = '/Users/kilnaar/src/node-lapack'
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

node-lapack
===========

a node.js wrapper for the high-performance LAPACK linear algebra library.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./lib/node-lapack')
41 changes: 41 additions & 0 deletions lib/node-lapack/fortranArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

var FFI = require('node-ffi');

function fortranArrayToJS(fortranArray, m, n) {
var array = [];
var a = fortranArray;
var elementSize = 4;
var rowWidth = elementSize * n;

for(var i = 0; i < m; i++) {
var row = [];
var rowStart = i * elementSize;

for(var j = 0; j < n; j++) {
a = fortranArray.seek(rowStart + (j * rowWidth));
row.push(a.getFloat());
}

array.push(row);
}

return array;
}

function jsToFortranArray(array) {
var m = array.length;
var n = array[0].length;
var elementSize = 4;
var fortranArrayStart = fortranArray = new FFI.Pointer(m * n * elementSize);
for(var i = 0; i < m; i++) {
for(var j = 0; j < n; j++) {
fortranArray.putFloat(array[i][j]);
fortranArray = fortranArray.seek(elementSize);
}
}

return fortranArrayStart;
}

module.exports.fortranArrayToJS = fortranArrayToJS;
module.exports.jsToFortranArray = jsToFortranArray;
3 changes: 3 additions & 0 deletions lib/node-lapack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

var lapack = require('./lapack.js');
exports.sgeqrf = lapack.sgeqrf;
45 changes: 45 additions & 0 deletions lib/node-lapack/lapack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

var fortranArray = require('./fortranArray');
var FFI = require('node-ffi');

var LAPACK = new FFI.Library('liblapack', {
"SGEQRF": ["void", ["pointer", "pointer", "pointer", "pointer", "pointer", "pointer", "pointer", "pointer"]]
});

var FORTRAN_INT = 4;

function sgeqrf(matrix) {
var m = matrix.length;
var n = matrix[0].length;
var f_m = new FFI.Pointer(FORTRAN_INT);
var f_n = new FFI.Pointer(FORTRAN_INT);
var f_lda = new FFI.Pointer(FORTRAN_INT);
var f_tau = new FFI.Pointer(m * n);
var f_info = new FFI.Pointer(FORTRAN_INT);
var f_lwork = new FFI.Pointer(FORTRAN_INT);
var f_work;
f_m.putInt32(m);
f_n.putInt32(n);
f_lda.putInt32(Math.max(1, m));
f_lwork.putInt32(-1);
var f_a = fortranArray.jsToFortranArray(matrix);

// get optimal size of workspace
f_work = new FFI.Pointer(FORTRAN_INT);
LAPACK.SGEQRF(f_m, f_n, f_a, f_lda, f_tau, f_work, f_lwork, f_info);
lwork = f_work.getFloat();

// allocate workspace
f_work = new FFI.Pointer(lwork);
f_lwork.putInt32(lwork);

// perform QR decomp
LAPACK.SGEQRF(f_m, f_n, f_a, f_lda, f_tau, f_work, f_lwork, f_info);

var r = fortranArray.fortranArrayToJS(f_a, m, n);
var q = fortranArray.fortranArrayToJS(f_tau, m, n);

return {Q: q, R: r};
}

exports.sgeqrf = sgeqrf;
24 changes: 24 additions & 0 deletions spec/approxEql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

var precision = 1e-6;

function approxEql(matrixA, matrixB) {
var i = matrixA.length;

while(i--) {
var j = matrixA[0].length;

while(j--) {
if((Math.abs(matrixA[i][j] - matrixB[i][j]) > precision)) {
console.log(matrixA[i][j]);
console.log(matrixB[i][j]);
console.log(Math.abs(matrixA[i][j] - matrixB[i][j]));

return false;
}
}
}

return true;
}

module.exports = approxEql;
25 changes: 25 additions & 0 deletions spec/lapack_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

var lapack = require('lib/node-lapack');
var approxEql = require('./approxEql');

describe('lapack', function() {
it('shoud qr', function() {
var qr = lapack.sgeqrf([
[2, 1, 1],
[1, 1, 1],
[1, 1, 3]
]);

expect(approxEql(qr.R,
[ [ -2.4494898319244385,
-1.632993221282959,
-2.4494895935058594 ],
[ 0.22474488615989685,
-0.5773501992225647,
-1.732050895690918 ],
[ 0.22474488615989685,
0.41421353816986084,
1.4142135381698608 ] ])).toBeTruthy();
});
});

0 comments on commit 73f8f52

Please sign in to comment.