Linear Algebra
C++ addon for nodejs to implement linear algebra efficiently
|
Inherits ObjectWrap.
Static Private Member Functions | |
static WrappedArray * | Create (int m, int n, Local< Context > context, Local< Array > array) |
static WrappedArray * | Create (int m, int n, float *data=NULL) |
static void | ToString (const FunctionCallbackInfo< Value > &args) |
static void | Inspect (const FunctionCallbackInfo< Value > &args) |
static void | Ones (const FunctionCallbackInfo< Value > &args) |
static void | Zeros (const FunctionCallbackInfo< Value > &args) |
static void | Eye (const FunctionCallbackInfo< Value > &args) |
static void | Diag (const FunctionCallbackInfo< Value > &args) |
static void | Read (const FunctionCallbackInfo< Value > &args) |
static void | Rand (const FunctionCallbackInfo< Value > &args) |
static void | Dup (const FunctionCallbackInfo< Value > &args) |
static void | Find (const FunctionCallbackInfo< Value > &args) |
static void | FindGreater (const FunctionCallbackInfo< Value > &args) |
static void | FindLessEqual (const FunctionCallbackInfo< Value > &args) |
static void | Neg (const FunctionCallbackInfo< Value > &args) |
static void | Log (const FunctionCallbackInfo< Value > &args) |
static void | Sqrt (const FunctionCallbackInfo< Value > &args) |
static void | Abs (const FunctionCallbackInfo< Value > &args) |
static void | Transpose (const FunctionCallbackInfo< Value > &args) |
static void | Hadamard (const FunctionCallbackInfo< Value > &args) |
static void | Mul (const FunctionCallbackInfo< Value > &args) |
static void | Mulp (const FunctionCallbackInfo< Value > &args) |
static void | Asum (const FunctionCallbackInfo< v8::Value > &args) |
static void | Sum (const FunctionCallbackInfo< v8::Value > &args) |
static void | Mean (const FunctionCallbackInfo< v8::Value > &args) |
static void | Norm (const FunctionCallbackInfo< v8::Value > &args) |
static void | Add (const FunctionCallbackInfo< v8::Value > &args) |
static void | Sub (const FunctionCallbackInfo< v8::Value > &args) |
static void | Inv (const FunctionCallbackInfo< v8::Value > &args) |
static void | Invp (const FunctionCallbackInfo< v8::Value > &args) |
static void | Pinv (const FunctionCallbackInfo< v8::Value > &args) |
static void | Svd (const FunctionCallbackInfo< v8::Value > &args) |
static void | Pca (const FunctionCallbackInfo< v8::Value > &args) |
static void | GetRows (const FunctionCallbackInfo< v8::Value > &args) |
static void | RemoveRow (const FunctionCallbackInfo< v8::Value > &args) |
static void | GetColumns (const FunctionCallbackInfo< v8::Value > &args) |
static void | RemoveColumn (const FunctionCallbackInfo< v8::Value > &args) |
static void | AppendColumns (const FunctionCallbackInfo< v8::Value > &args) |
static void | RotateColumns (const FunctionCallbackInfo< v8::Value > &args) |
static void | Reshape (const FunctionCallbackInfo< v8::Value > &args) |
static void | Solve (const v8::FunctionCallbackInfo< v8::Value > &args) |
static void | Get (const FunctionCallbackInfo< v8::Value > &args) |
static void | Set (const FunctionCallbackInfo< v8::Value > &args) |
static void | MakeIterator (const FunctionCallbackInfo< v8::Value > &args) |
Private Attributes | |
int | m_ |
int | n_ |
float * | data_ |
bool | isVector |
int | dataSize_ |
int | maxPrint_ |
char * | name_ |
Static Private Attributes | |
static v8::Persistent< v8::Function > | constructor |
This is the main class to represent a matrix. It is a nodejs compatible object.
Matrices are stored in column order, this is to simplify access to (some) libraries (esp. cuda). The internal data type is always a float, which is good enough for most situations. The comments for the C++ code contain some javascript examples
|
staticprivate |
Absolute value of matrix
Returns a new matrix which is an copy of the target with each element set to positive
It takes 0 args:
|
staticprivate |
Add two matrices
Performs one of
[in] | one | of
|
|
staticprivate |
Append columns to a matrix
Append column vectors. The target is expanded to have N+K columns. The matrix should have the same length as the matrix.m ( rows ). K is the width of the added column matrix
[in] | the | column(s) to append to the matrix |
|
staticprivate |
sum all absolute value of each element
|
inlinestaticprivate |
Create an array with some data provided in a javascript Array
[in] | m | number of rows in the matrix |
[in] | n | number of cols in the matrix |
[in] | array | data to put into the array - column major order |
|
inlinestaticprivate |
Create an array with some (optional) data. This will be the base call for creation for internal methods.
[in] | m | number of rows in the matrix |
[in] | n | number of cols in the matrix |
[in] | data | data to put into the array - column major order |
|
staticprivate |
Returns a new square matrix, where the principal diagonal is formed from a vector. The size of the array is the vector length squared. All other elements are zero.
a | WrappedArray containing a vector to use as the diagonal |
|
staticprivate |
Duplicate a matrix
Returns a new matrix which is an identical copy of the target
|
staticprivate |
Returns a new square identity matrix.
An identity matrix is a leading diagonal of 1.0 and all other elements are 0. Any matrix multiplied by (a suitable shaped) identity will return itself.
the | number of rows (m) and columns (n) |
|
staticprivate |
Find matching values in a matrix
Returns a new matrix where a matching element is set to the given parameter if the value matches the supplied number. An optional parameter defines the match accuracy ( since we are working with real numbers ). If the 2nd parameter is not given the orginal target element is dropped in.
[in] | the | number to find ( default 1 ) |
[in] | the | value to pop into the matching elements |
[in] | the | accuracy to find a match default 0.000001 |
|
staticprivate |
Keep greater values in a matrix
Returns a new matrix where a matching element is set to supplied value if an element is less than or equal to the supplied number, otherwise it's the target value. In other words keep the values that are > than the input and zero the rest. If the 2nd parameter is not given (null) the orginal target element is dropped in.
[in] | the | number to find ( default 1 ) |
[in] | the | value to pop into the matching elements |
|
staticprivate |
Keep less (or same) values in a matrix
Returns a new matrix where a matching element is set to the supplied value if an element is greater than the supplied number, otherwise it's the target value. In other words keep the values that are <= than the input and zero the rest. If the 2nd parameter is not given (null) the orginal target element is dropped in.
[in] | the | number to find ( default 1 ) |
[in] | the | value to pop into the matching elements |
|
staticprivate |
Get a value from the matrix
Returns a value from the matrix at the X,Y. If a single index is given it is used an an absolute address into the underlying store. Negative indices count from the end.
[in,default=0] | M the row index ( or the absolute index is N is missing ) |
[in,optional] | N the column index |
|
staticprivate |
Copy columns from the target
Build a new matrix containing some of the columns of the target. The target is unchanged. The input can be a single number or an array of column indices to copy from the target. The reulting array will be MxK, where k is the number of columns requested and M is the number of rows in the target.
[in,default=0] | the column indices to copy from the array , may be a number or an array of numbers |
|
staticprivate |
Copy rows from the target
Build a new matrix containing some of the rows or the target. The target is unchanged. The input can be a single number or an array of rows to copy from the target. The reulting array will be KxN, where k is the number of rows requested and N is the number of columns in the target.
[in,default=0] | the rows to copy from the matrix, may be a number or an array of numbers |
|
staticprivate |
Hadamard (Schur) multiply of two matrices
This is (VERY) different to normal matrix multipliy. Also the vector x matrix version just multiplies each column (or row) entry by the corresponding vector element.
It is the .* operator in Matlab or Octave
Performs one of
[in] | one | of
|
|
staticprivate |
internally calls ToString.
|
staticprivate |
Matrix inverse
Calculate the inverse of a matrix. A matrix inverse multiplied by itself is an identity matrix. The input matrix must be square. A newly created inverse is returned, the original remains intact.
|
staticprivate |
Matrix inverse
Calculate the inverse of a matrix. A matrix inverse multiplied by itself is an identity matrix. The input matrix must be square. The return is a promise ( no args ) or, if a callback function is provided undefined. A newly created inverse is resolved into the promise or the callback; the original remains intact.
[in,optional] | a callback function prototype = function(err,inv) { } |
|
staticprivate |
Log a matrix
Returns a new matrix which is an copy of the target with each element being the natural log of the target. Since we're not doing complex values (yet:o), negative and zero values will result in NaN
It takes 0 args:
|
staticprivate |
get javascript iterator
This creates an iterator object so we can iterate over the elements of a matrix.
|
staticprivate |
mean of rows or columns of a matrix
This calculates the mean of rows or columns of a matrix into a row or column vector. If the target is a vector this will return the mean of all the elements into a single number.
[in,default=0] | the dimension to inspect - 0 = mean of columns, 1 = mean of rows. |
|
staticprivate |
Mul - multiply a matrix
Perform matrix multiplication. The target must be MxK and the other must be KxN a new matrix of MxN is produced. This is fine for small matrices, if large matrices are to be multiplied, consider Mulp. If a scalar is passed in all elements in the array are multiplied by it.
the | other matrix or a number |
|
staticprivate |
multiply a matrix in non-blocking mode
Perform matrix multiplication. The target must be MxK and the other must be KxN a new matrix of MxN is produced. This will run in non-blocked mode.
There are two ways to use this, pass in an optional callback or accept a returned promise. Passing in a callback will prevent the Promise from being returned.
[in] | the | other matrix |
[in] | a | callback of prototype function(err,MATRIX){ } |
|
staticprivate |
Negate a matrix
Returns a new matrix which is an copy of the target with each element sign reversed
It takes 0 args:
|
staticprivate |
Euclidian norm of rows or columns of a matrix
This calculates the norms of a row or column of a matrix into a row or column vector. If the target is a vector this will return the norm as a number.
[in,default=0] | the dimension to sum - 0 = sum columns, 1 = sum rows |
|
staticprivate |
Returns a new matrix with all values set to 1.0
the | number of rows (m) defaults to 0 |
the | number of columns (n) defaults to m |
|
staticprivate |
Principal component analysis
Calculate the PCA factor of a matrix. The returned factor can be multiplies by any observation (row) or observations (the whole matrix) to get a reduced dimension set of features. The amount of information (variance) to keep is passed in as an argument.
IMPORTANT: the target features should be mean normalized for this to be accurate. Mean normalized data has a zero mean for each feature ( each col mean = 0 ).
The returned factor is a NxK array. Where K<M. We can reduce the dimensions of the target from M to K by multiplying the observation(s) by the factor.
In this example we extract the mean - in case we want to normalize unseen features.
the | amount of variance to return (0 to 1.0). Default is 0.97 |
|
staticprivate |
Get the pseudo inverse of a matrix
Calculate the pseudo inverse of a matrix. A matrix multiplied by its pseudo inverse is an identity matrix. A newly created inverse is returned, the original remains intact.
This seems to have a problem with vectors - don't invert a vector until we know what it may mean.
|
staticprivate |
Returns a new matrix with all values set to a random integer between -10 and 10 (inclusive)
The randomness isn't very good, it's used for testing.
|
staticprivate |
Read a matrix from a stream
This takes a stream which emits data events for each row. One such stream is the fast-csv.
a | stream that presents data events with a single Array of numbers |
|
staticprivate |
Remove a column from a matrix
Return a column vector which is the requested column of the target. The target is shrunk to have N-1 columns.
[in,default=0] | the column index (zero based) to remove from the array |
|
staticprivate |
Remove a row from a matrix
Return a vector which is the requested row of the target. The target is shrunk to have M-1 rows. This is not very efficient.
[in] | the | row index (zero based) to remove from the array default=0 |
|
staticprivate |
Reshape the matrix
Change the shape of the matrix. If the shape is smaller ( new M x new N < M x N ) the memory is left alone. Otherwise a new buffer is allocated, and the additional elements are initialized to zero. This operation is done in place - the target is changed. The default is to produce a (M*N)x1 vector is no paramters are given.
[in,default=m*n] | the number of rows to have in the new shape |
[in,default=1] | the number of columns to have in the new shape |
|
staticprivate |
Rotate columns in a matrix
Rotate column vectors. A positive number means rotate to the left, a negative number rotates to the right. The rotation count is internally and silently limited using modulus the number of columns.
If there is a requirement to rotate rows, transpose the matrix then call this, the transpose back.
| 1.00 2.00 3.00 4.00 5.00 | A = | 1.00 2.00 3.00 4.00 5.00 | | 1.00 2.00 3.00 4.00 5.00 | | 2.00 3.00 4.00 5.00 1.00 | B = | 2.00 3.00 4.00 5.00 1.00 | | 2.00 3.00 4.00 5.00 1.00 | | 4.00 5.00 1.00 2.00 3.00 | C = | 4.00 5.00 1.00 2.00 3.00 | | 4.00 5.00 1.00 2.00 3.00 |
[in,default=1] | the rotation count. |
|
staticprivate |
Set a value in the matrix
Overwrites a the value in the matrix at the X,Y. If the target is a vector only the first argument is used. If a single index is given it is used an an absolute address into the underlying store. Negative indices count from the end.
[in] | value | to set into the matrix |
[in,default=0] | M the row index ( or the absolute index is N is missing ) | |
[in,optional] | N the column index |
|
staticprivate |
Solves a function for its minimum
Solves the function starting at the given Array based starting position. After this is finished the array is set to the global minimum.
This requires a gradient function object to be passed as a parameter. The gradient function is an object with two methods:
The solver name parameter should be one of
[in] | the | function to solve |
|
staticprivate |
Square root a matrix
Returns a new matrix which is an copy of the target with each element being the square root of the target. Negative target values will result in NaN entries.
It takes 0 args:
|
staticprivate |
Subtract two matrices
Performs one of
[in] | one | of
|
|
staticprivate |
Sum the rows or columns of a matrix into a vector
This sums the rows or columns of a matrix into a row or column vector. If the target is a vector this will just sum all the elements into a single number.
[in,default=0] | the dimension to sum - 0 = sum columns, 1 = sum rows. |
|
staticprivate |
Singular Value Decomposition of a matrix
Calculate the SVD of a matrix. This will return an object with 3 attributes: U, S and VT. This factorizes the matrix to 3 components.
|
staticprivate |
returns a string representation of the matrix
This will print up to maxPrint rows and columns
|
staticprivate |
Transpose a matrix
Returns a new matrix which is an transpose of the target.
It takes 0 args:
|
staticprivate |
Returns a new matrix with all values set to .0
the | number of rows (m) defaults to 0 |
the | number of columns (n) defaults to m |
|
staticprivate |
a nodejs constructor for this object
|
private |
the data buffer holding the values
|
private |
private - used to remember the last data allocation size
|
private |
helper flag to see whether the target is a vector Mx1 or 1xN
|
private |
the number of rows in the matrix
|
private |
the number of rows & columns to print out in toString()
|
private |
the number of columns in the matrix
|
private |
The name of this matrix - useful for keeping track of things