lnsc
Class LinearAlgebra

java.lang.Object
  |
  +--lnsc.LinearAlgebra

public final class LinearAlgebra
extends java.lang.Object

Set of linear algebra functions for vectors and matrices.

Since:
1.0

Method Summary
static double[][][] add3DMatrices(double[][][] m1, double[][][] m2)
          Computes the element wise addition of two 3D matrices.
static double[][][] adde3DMatrices(double[][][] m, double[][][] ma)
          Computes the addition of a 3Dmatrix to another one.
static double[][] addeMatrices(double[][] m, double[][] ma)
          Computes the addition of a matrix to another one.
static double[] addeVectors(double[] v, double[] va)
          Computes the addition of a vector to another one.
static double[][] addMatrices(double[][] m1, double[][] m2)
          Computes the addition of two matrices.
static double[] addVectors(double[] v1, double[] v2)
          Computes the addition of two vectors.
static double[][][] appendMatrixBelow(double[][][] m1, double[][][] m2)
          Creates a new matrix made of two matrices one above the other.
static double[][] appendMatrixBelow(double[][] m1, double[][] m2)
          Creates a new matrix made of two matrices one above the other.
static double[][] appendMatrixBesides(double[][] m1, double[][] m2)
          Creates a new matrix made of two matrices one beside the other.
static double[][] appendNewColumns(double[][] m, int count)
          Creates a new matrix which is like the original but with a few more columns (0's initialized).
static double[][] batchSubVectors(double[][] setA, double[][] setB)
          Applies subVectors(double[], double[]) on a batch of vector pairs.
static double[] concatenateCols(double[][] m)
          Creates a new vector from the concatenation of the columns of the given matrix.
static double[] concatenateRows(double[][] m)
          Creates a new vector from the concatenation of the rows of the given matrix.
static double[] concatenateVectors(double[] v1, double[] v2)
          Create a vector that is the concatenation of two other vectors.
static double[][] copyMatrix(double[][] m)
          Creates a copy of a given matrix.
static double[] copyVector(double[] v)
          Creates a copy of a given vector.
static double[][] cutInCols(double[] v, int rows)
          Creates a matrix from a vector by cutting in columns.
static double[][] cutInRows(double[] v, int cols)
          Creates a matrix from a vector by cutting in rows.
static void eMatrix(double[][] target, double[][] source)
          Copies a matrix into another matrix.
static double[][] extractColumns(double[][] m, int start, int count)
          Creates a new matrix which is like the original but with only the requested middle columns.
static int[][] extractColumns(int[][] m, int start, int count)
          Creates a new matrix which is like the original but with only the requested middle columns.
static double[][] extractRows(double[][] m, int start, int count)
          Creates a new matrix which is like the original but with only the requested middle rows.
static int[][] extractRows(int[][] m, int start, int count)
          Creates a new matrix which is like the original but with only the requested middle rows.
static double[] extractVector(double[] v, int start, int stop)
          Creates a vector by extract a contiguous portion of a larger vector.
static boolean is3DMatrix(java.lang.Object m)
          Checks whether a given object is of type double[][][] and whether sub matrices are of the same dimensions.
static boolean isMatrix(java.lang.Object m)
          Checks whether a given object is of type double[][] and whether sub vectors are of the same length.
static boolean isMatrix(java.lang.Object m, int rows, int cols)
          Checks whether a given object is of type double[rows][cols].
static boolean isSquareMatrix(double[][] m)
          Check whether the matrix is a 2D squared matrix (row = col).
static boolean isSquareMatrix(double[][][] m)
          Check whether the matrix is a 3D squared matrix (row = col = slabs).
static boolean isSquareMatrix(double[][][][] m)
          Check whether the matrix is a 4D squared matrix (row = col = slabs = hyperslabs).
static boolean isVector(java.lang.Object v)
          Checks whether a given object is of type double[].
static boolean isVector(java.lang.Object v, int size)
          Checks whether a given object is of type double[size].
static double[][] multMatrixMatrix(double[][] m1, double[][] m2)
          Computes the multiplication of two matrices.
static double[] multMatrixVector(double[][] m, double[] v)
          Computes the multiplication of a matrix times a column vector.
static double[][][] multScalar3DMatrix(double s, double[][][] m)
          Computes the multiplication of a matrix times a scalar.
static double[][] multScalarMatrix(double s, double[][] m)
          Computes the multiplication of a matrix times a scalar.
static double[] multScalarVector(double s, double[] v)
          Computes the multiplication of a vector times a scalar.
static double[] multVectorMatrix(double[] v, double[][] m)
          Computes the multiplication of a matrix times a vector.
static double[][] multVectorVector(double[] v1, double[] v2)
          Computes the multiplication of a vector times a vector.
static void normalizeVector(double[] v)
          Normalize a vector so that its (euclidien) norm sum to 1.
static void overwriteSubMatrix(int startRow, int startCol, double[][] source, double[][] target)
          Copies a sub matrix into a large matrix at a given position.
static void overwriteSubVector(int start, double[] source, double[] target)
          Copies a sub vector into a large vector at a given position.
static void overwriteSubVector(int start, int length, double[] source, double[] target)
          Copies a sub vector into another vector at a given position.
static double[] rotateVector(double[] v, double angle)
          Computes the 2D rotated vector by the given angle.
static double sign(double x)
          Returns the sign of a number.
static double[][] subMatrices(double[][] m1, double[][] m2)
          Computes the subtraction of two matrices.
static double[] subVectors(double[] v1, double[] v2)
          Computes the subtraction of two vectors.
static double[][] sumMatrices(double[][][] Ms)
          Sums elementwise a vector of matrices.
static double sumSquares(double[] v)
          Computes the sum of the squares of each element of a vector.
static double sumSquares(double[][] m)
          Computes the sum of the squares of each element of a matrix.
static double sumSquares(double[][][] m)
          Computes the sum of the squares of each element of a 3D matrix.
static java.lang.String toString(double[] v)
          Generates a Object.toString() representation of a vector.
static java.lang.String toString(double[][] m)
          Generates a Object.toString() representation of a matrix (as defined by isMatrix(java.lang.Object)).
static java.lang.String toString(double[][][] m)
          Generates a Object.toString() representation of a 3D matrix of integer (as defined by is3DMatrix(java.lang.Object)).
static java.lang.String toString(int[] v)
          Generates a Object.toString() representation of a vector of integer.
static java.lang.String toString(int[][] m)
          Generates a Object.toString() representation of a matrix of integer (as defined by isMatrix(java.lang.Object)).
static double[][] transposeMatrix(double[][] m)
          Creates the transposed matrix.
static double[][][][] transposeMatrix(double[][][][] m, int[] transposeOrder)
           
static double[][][] transposeMatrix(double[][][] m, int[] transposeOrder)
           
static double weightedSum(double[] ws, double[] Ss)
          Computes a weighted sum of scalars.
static double[] weightedSum(double[] ws, double[][] Vs)
          Computes a weighted summation of vectors.
static double[][] weightedSum(double[] ws, double[][][] Ms)
          Computes a weighted summation of matrices.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

add3DMatrices

public static final double[][][] add3DMatrices(double[][][] m1,
                                               double[][][] m2)
Computes the element wise addition of two 3D matrices.

Parameters:
m1 - The first 3D matrix
m2 - The second 3D matrix.
Returns:
The resulting matrix m1+m2;

adde3DMatrices

public static final double[][][] adde3DMatrices(double[][][] m,
                                                double[][][] ma)
Computes the addition of a 3Dmatrix to another one.

Parameters:
m - The source and target 3D matrix.
ma - The 3D matrix to add to it
Returns:
The resulting vector m+=ma (m);

addeMatrices

public static final double[][] addeMatrices(double[][] m,
                                            double[][] ma)
Computes the addition of a matrix to another one.

Parameters:
m - The source and target matrix.
ma - The matrix to add to it
Returns:
The resulting vector m+=ma (m);

addeVectors

public static final double[] addeVectors(double[] v,
                                         double[] va)
Computes the addition of a vector to another one.

Parameters:
v - The source and target vector.
va - The vector to add to it
Returns:
The resulting vector v+=va (v);

addMatrices

public static final double[][] addMatrices(double[][] m1,
                                           double[][] m2)
Computes the addition of two matrices.

Parameters:
m1 - The first matrix
m2 - The second matrix.
Returns:
The resulting matrix m1+m2;

addVectors

public static final double[] addVectors(double[] v1,
                                        double[] v2)
Computes the addition of two vectors.

Parameters:
v1 - The first vector.
v2 - The second vector.
Returns:
The resulting vector v1+v2;

appendMatrixBelow

public static final double[][][] appendMatrixBelow(double[][][] m1,
                                                   double[][][] m2)
Creates a new matrix made of two matrices one above the other.

Parameters:
m1 - The upper matrix (or block).
m2 - The lower matrix (or block).
Returns:
The resulting matrix Note: Slabs of matrices m1 and m2 will be reused, not copied.

appendMatrixBelow

public static final double[][] appendMatrixBelow(double[][] m1,
                                                 double[][] m2)
Creates a new matrix made of two matrices one above the other.

Parameters:
m1 - The upper matrix.
m2 - The lower matrix.
Returns:
The resulting matrix Note: Cols of matrices m1 and m2 will be reused, not copied.

appendMatrixBesides

public static final double[][] appendMatrixBesides(double[][] m1,
                                                   double[][] m2)
Creates a new matrix made of two matrices one beside the other.

Parameters:
m1 - The left matrix.
m2 - The right matrix.
Returns:
The resulting matrix Note: Elements are copided one by one.

appendNewColumns

public static final double[][] appendNewColumns(double[][] m,
                                                int count)
Creates a new matrix which is like the original but with a few more columns (0's initialized).

Parameters:
m - The matrix.
count - The number of new columns.
Returns:
The expended matrix.

batchSubVectors

public static final double[][] batchSubVectors(double[][] setA,
                                               double[][] setB)
Applies subVectors(double[], double[]) on a batch of vector pairs. That is, SetA[0]-SetB[0], SetA[1]-SetB[1], ...

Parameters:
setA - Array of first vectors in substraction.
setB - Array of second vectors in subtraction.
Returns:
Array of results.

concatenateCols

public static final double[] concatenateCols(double[][] m)
Creates a new vector from the concatenation of the columns of the given matrix. The reverse operation is cutInCols(double[], int).

Parameters:
m - The matrix to convert.
Returns:
The resulting vector.

concatenateRows

public static final double[] concatenateRows(double[][] m)
Creates a new vector from the concatenation of the rows of the given matrix. The reverse operation is cutInRows(double[], int).

Parameters:
m - The matrix to convert.
Returns:
The resulting vector.

concatenateVectors

public static final double[] concatenateVectors(double[] v1,
                                                double[] v2)
Create a vector that is the concatenation of two other vectors.

Parameters:
v1 - The first vector.
v2 - The second vector.
Returns:
The resulting concatenation [v1|v2].

copyMatrix

public static final double[][] copyMatrix(double[][] m)
Creates a copy of a given matrix.

Parameters:
m - The matrix to be copied.
Returns:
A copy of the given matrix.

copyVector

public static final double[] copyVector(double[] v)
Creates a copy of a given vector.

Parameters:
v - The vector to be copied.
Returns:
A copy of the given vector.

cutInCols

public static final double[][] cutInCols(double[] v,
                                         int rows)
Creates a matrix from a vector by cutting in columns. This operation is basically the reverse operation of concatenateCols(double[][]).

Parameters:
v - The vector to convert.
rows - Length of each columns extracted from the vector.
Returns:
The resulting matrix

cutInRows

public static final double[][] cutInRows(double[] v,
                                         int cols)
Creates a matrix from a vector by cutting in rows. This operation is basically the reverse operation of concatenateRows(double[][]).

Parameters:
v - The vector to convert.
cols - Length of each rows extracted from the vector.
Returns:
The resulting matrix

eMatrix

public static final void eMatrix(double[][] target,
                                 double[][] source)
Copies a matrix into another matrix.

Parameters:
target - Matrix that will be overwitten.
source - Matrix that will be read.

extractColumns

public static final double[][] extractColumns(double[][] m,
                                              int start,
                                              int count)
Creates a new matrix which is like the original but with only the requested middle columns. Note that elements are copied one by one.

Parameters:
m - A matrix.
start - First column to kept.
count - The number of column to kept.
Returns:
The reduced matrix.

extractColumns

public static final int[][] extractColumns(int[][] m,
                                           int start,
                                           int count)
Creates a new matrix which is like the original but with only the requested middle columns. Note that elements are copied one by one.

Parameters:
m - A matrix.
start - First column to kept.
count - The number of column to kept.
Returns:
The reduced matrix.

extractRows

public static final double[][] extractRows(double[][] m,
                                           int start,
                                           int count)
Creates a new matrix which is like the original but with only the requested middle rows. Note that elements are copied one by one.

Parameters:
m - A matrix.
start - First column to kept.
count - The number of column to kept.
Returns:
The reduced matrix.

extractRows

public static final int[][] extractRows(int[][] m,
                                        int start,
                                        int count)
Creates a new matrix which is like the original but with only the requested middle rows. Note that elements are copied one by one.

Parameters:
m - A matrix.
start - First column to kept.
count - The number of column to kept.
Returns:
The reduced matrix.

extractVector

public static final double[] extractVector(double[] v,
                                           int start,
                                           int stop)
Creates a vector by extract a contiguous portion of a larger vector.

Parameters:
v - The source vector.
start - Index from where to begin the extraction, that is, index of the first element of the new vector.
stop - Index from where to stop the extraction, that is, index of the last element of the new vector.
Returns:
The extracted vector v'=v[start..stop].

is3DMatrix

public static final boolean is3DMatrix(java.lang.Object m)
Checks whether a given object is of type double[][][] and whether sub matrices are of the same dimensions.

Parameters:
m - Object to be tested.
Returns:
true is the object is of type double[slab][colx][coly].

isMatrix

public static final boolean isMatrix(java.lang.Object m)
Checks whether a given object is of type double[][] and whether sub vectors are of the same length.

Parameters:
m - Object to be tested.
Returns:
true is the object is of type double[row][col].

isMatrix

public static final boolean isMatrix(java.lang.Object m,
                                     int rows,
                                     int cols)
Checks whether a given object is of type double[rows][cols].

Parameters:
m - Object to be tested.
rows - Number of rows to check for.
cols - Number of columns to check for.
Returns:
true is the object is of type double[rows][cols].

isSquareMatrix

public static boolean isSquareMatrix(double[][] m)
Check whether the matrix is a 2D squared matrix (row = col).

Parameters:
m - The matrix to check
Returns:
true if the number of rows equals the number of columns.

isSquareMatrix

public static boolean isSquareMatrix(double[][][] m)
Check whether the matrix is a 3D squared matrix (row = col = slabs).

Parameters:
m - The matrix to check
Returns:
true if the number of rows equals the number of columns and the number of slabs.

isSquareMatrix

public static boolean isSquareMatrix(double[][][][] m)
Check whether the matrix is a 4D squared matrix (row = col = slabs = hyperslabs).

Parameters:
m - The matrix to check
Returns:
true if the number of rows equals the number of columns, the number of slabs and the number of hyperslabs.

isVector

public static final boolean isVector(java.lang.Object v)
Checks whether a given object is of type double[].

Parameters:
v - Object to be tested.
Returns:
true is the object is of type double[].

isVector

public static final boolean isVector(java.lang.Object v,
                                     int size)
Checks whether a given object is of type double[size].

Parameters:
v - Object to be tested.
size - Size to check for.
Returns:
true is the object is of type double[size].

multMatrixMatrix

public static final double[][] multMatrixMatrix(double[][] m1,
                                                double[][] m2)
Computes the multiplication of two matrices.

Parameters:
m1 - The first matrix.
m2 - The second matrix.
Returns:
The resulting matrix.

multMatrixVector

public static final double[] multMatrixVector(double[][] m,
                                              double[] v)
Computes the multiplication of a matrix times a column vector.

Parameters:
m - The matrix.
v - The vector.
Returns:
The resulting vector.

multScalar3DMatrix

public static final double[][][] multScalar3DMatrix(double s,
                                                    double[][][] m)
Computes the multiplication of a matrix times a scalar.

Parameters:
s - The scalar.
m - The 3D matrix.
Returns:
The resulting matrix.

multScalarMatrix

public static final double[][] multScalarMatrix(double s,
                                                double[][] m)
Computes the multiplication of a matrix times a scalar.

Parameters:
s - The scalar.
m - The matrix.
Returns:
The resulting matrix.

multScalarVector

public static final double[] multScalarVector(double s,
                                              double[] v)
Computes the multiplication of a vector times a scalar.

Parameters:
s - The scalar.
v - The vector.
Returns:
The resulting vector.

multVectorMatrix

public static final double[] multVectorMatrix(double[] v,
                                              double[][] m)
Computes the multiplication of a matrix times a vector.

Parameters:
v - The vector.
m - The matrix.
Returns:
The resulting vector.

multVectorVector

public static final double[][] multVectorVector(double[] v1,
                                                double[] v2)
Computes the multiplication of a vector times a vector. It is assume that the first vector is a column vector (and the second one a row vector).

Parameters:
v1 - The first (column) vector.
v2 - The second (row) vector.
Returns:
The resulting matrix.

normalizeVector

public static void normalizeVector(double[] v)
Normalize a vector so that its (euclidien) norm sum to 1. Done in-place.

Parameters:
v - The vector to normalize.

overwriteSubMatrix

public static final void overwriteSubMatrix(int startRow,
                                            int startCol,
                                            double[][] source,
                                            double[][] target)
Copies a sub matrix into a large matrix at a given position.

Parameters:
startRow - Index of the first row to overwrite.
startCol - Index of the second row to overwrite.
source - Small matrix that will be read.
target - Large matrix that will be partly overwritten.

overwriteSubVector

public static final void overwriteSubVector(int start,
                                            double[] source,
                                            double[] target)
Copies a sub vector into a large vector at a given position.

Parameters:
start - Index of the first element to overwrite.
source - Small vector that will be read.
target - Large vector that will be partly overwritten.

overwriteSubVector

public static final void overwriteSubVector(int start,
                                            int length,
                                            double[] source,
                                            double[] target)
Copies a sub vector into another vector at a given position.

Parameters:
start - Index of the first element to overwrite
length - Length of the source vector to read
source - Small vector that will be read.
target - Large vector that will be partly overwritten.

rotateVector

public static final double[] rotateVector(double[] v,
                                          double angle)
Computes the 2D rotated vector by the given angle.

Parameters:
v - The vector to rotate.
angle - The angle of rotation in radian.
Returns:
The resulting vector;

sign

public static double sign(double x)
Returns the sign of a number.

Parameters:
x - The number to return the sign of.
Returns:
-1 for negative, 1 for positive, 0 otherwise.

subMatrices

public static final double[][] subMatrices(double[][] m1,
                                           double[][] m2)
Computes the subtraction of two matrices.

Parameters:
m1 - The first matrix.
m2 - The second matrix.
Returns:
The resulting matrix m1-m2;

subVectors

public static final double[] subVectors(double[] v1,
                                        double[] v2)
Computes the subtraction of two vectors.

Parameters:
v1 - The first vector.
v2 - The second vector.
Returns:
The resulting vector v1-v2;

sumMatrices

public static double[][] sumMatrices(double[][][] Ms)
Sums elementwise a vector of matrices.

Parameters:
Ms - Array of matrices.
Returns:
The matrix whose elements are the sum of the corresponding elements of all the given matrices.

sumSquares

public static double sumSquares(double[] v)
Computes the sum of the squares of each element of a vector.

Parameters:
v - The vector to compute the norm.
Returns:
The norm of the vector.

sumSquares

public static double sumSquares(double[][] m)
Computes the sum of the squares of each element of a matrix.

Parameters:
m - The matrix to compute the norm.
Returns:
The norm of the matrix.

sumSquares

public static double sumSquares(double[][][] m)
Computes the sum of the squares of each element of a 3D matrix.

Parameters:
m - The 3D matrix to compute the norm.
Returns:
The norm of the 3D matrix.

toString

public static final java.lang.String toString(double[] v)
Generates a Object.toString() representation of a vector.


toString

public static final java.lang.String toString(double[][] m)
Generates a Object.toString() representation of a matrix (as defined by isMatrix(java.lang.Object)).


toString

public static final java.lang.String toString(double[][][] m)
Generates a Object.toString() representation of a 3D matrix of integer (as defined by is3DMatrix(java.lang.Object)).


toString

public static final java.lang.String toString(int[] v)
Generates a Object.toString() representation of a vector of integer.


toString

public static final java.lang.String toString(int[][] m)
Generates a Object.toString() representation of a matrix of integer (as defined by isMatrix(java.lang.Object)).


transposeMatrix

public static double[][] transposeMatrix(double[][] m)
Creates the transposed matrix.

Parameters:
m - The matrix to be transposed.
Returns:
m transposed.

transposeMatrix

public static double[][][][] transposeMatrix(double[][][][] m,
                                             int[] transposeOrder)

transposeMatrix

public static double[][][] transposeMatrix(double[][][] m,
                                           int[] transposeOrder)

weightedSum

public static double weightedSum(double[] ws,
                                 double[] Ss)
Computes a weighted sum of scalars. Given two vector of scalars, it computes a weighted sum (like a dot product of two vectors).

Parameters:
ws - Vector of scalars (or weights)
Ss - Vector of scalars.
Returns:
The weighted sum (dot product).

weightedSum

public static double[] weightedSum(double[] ws,
                                   double[][] Vs)
Computes a weighted summation of vectors. Given a vector of scalar and a vector of vectors, it computes the multScalarVector(double, double[]) of each vector multiplied by its associated scalar and then call #sumVectors.

Parameters:
ws - Vector of scalars (or weights)
Vs - Vector of vectors.
Returns:
The weighted sum of the vectors weighted by the scalars.

weightedSum

public static double[][] weightedSum(double[] ws,
                                     double[][][] Ms)
Computes a weighted summation of matrices. Given a vector of scalar and a vector of matrices, it computes the multScalarMatrix(double, double[][]) of each matrix multiplied by its associated scalar and then call sumMatrices(double[][][]).

Parameters:
ws - Vector of scalars (or weights)
Ms - Vector of matrices.
Returns:
The weighted sum of the matrices weighted by the scalars.