Package com.actelion.research.calc
Class MatrixFunctions
- java.lang.Object
-
- com.actelion.research.calc.MatrixFunctions
-
public class MatrixFunctions extends java.lang.Object
Title: MatrixFunctions
Description: Matrix operations for which a direct access to the matrix member variables is not necessary
References http://www.ini.uzh.ch/~fred/java/Matrix.java https://introcs.cs.princeton.edu/java/95linear/Cholesky.java.htmlCopyright: Actelion Ltd., Inc. All Rights Reserved This software is the proprietary information of Actelion Pharmaceuticals, Ltd. Use is subject to license terms.
- Version:
- 1.0 20.11.2003 MvK: Start implementation 10.06.2004 MvK read matrix.
- Author:
- Modest von Korff
-
-
Constructor Summary
Constructors Constructor Description MatrixFunctions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Matrix
appendCols(Matrix ma0, Matrix ma1)
static Matrix
appendRows(Matrix ma0, Matrix ma1)
static Matrix
appendRows(java.util.List<Matrix> liMatrix)
static IntegerDouble
calculateMaxSimilarity(Matrix X, double[] arr)
static IntegerDouble[]
calculateMaxSimilarity(Matrix X, double[] arr, int nMostSimilar)
static double[]
calculateMaxSimilarity(Matrix XTrain, Matrix Xtest)
static Matrix
calculateSimilarityMatrixRowWise(Matrix X1, Matrix X2)
static Matrix
cholesky(Matrix A)
static void
columnIntoDoubleArray(Matrix A, int col, DoubleArray da)
static Matrix
convert2Binary(Matrix A, double thresh)
static int
countFieldsBiggerThan(Matrix ma, int row, double thresh)
static Matrix
countFieldsBiggerThanThreshColWise(Matrix ma, double thresh)
static int
countFieldsBiggerThanThreshColWise(Matrix ma, int col, double thresh)
static Matrix
create(java.util.List<double[]> li)
List is already used as a constructor for Matrix.static java.util.List<IdentifiedObject<double[]>>
createIdentifiedObject(Matrix A)
static boolean
equals(double d1, double d2)
static double
getCorrPearson(Matrix A, int col1, int col2)
static double
getCorrPearson(Matrix A, Matrix B)
static double
getCorrSpearman(Matrix A, Matrix B)
static double
getCovariance(Matrix A, Matrix B)
static double
getCovarianceCentered(Matrix A, Matrix B)
static Matrix
getDistanceMatrix(java.util.List<java.awt.Point> li)
static Matrix
getDistanceMatrixTanimotoInv(Matrix ma1, Matrix ma2)
Calculates the inverse Tanimoto coefficient from row wise comparison of the two input matrices.static double
getDistanceTanimotoInv(Matrix ma1, int row1, Matrix ma2, int row2)
static double
getDistTanimotoInvReduced(Matrix ma1, int row1, Matrix ma2, int row2)
Only fields are considered which are not nut 0 in both or in one of the rows we from where the distance is calculated.static Matrix
getDistTanimotoInvReduced(Matrix ma1, Matrix ma2)
static java.util.List<java.awt.Point>
getIndicesUniqueMaxColumnWise(Matrix maIn)
static java.util.List<java.awt.Point>
getIndicesUniqueMaxRowWise(Matrix maIn)
static Matrix
getKMeanClusters(Matrix ma, int k)
03.10.04 MvKstatic java.util.List<java.awt.Point>
getMooreNeighborhood(java.awt.Point p, int r, Matrix ma)
static java.util.List<java.awt.Point>
getMooreNeighborhood(java.awt.Point p, Matrix ma)
A square-shaped neighborhood that can be used to define a set of cells surrounding a given point.static IntVec
getNonZeroCols(Matrix X)
static java.util.List<java.awt.Point>
getPoints(Matrix ma)
static Matrix
getRandomMatrix(int rows, int cols)
generates a matrix with double values between 0 (inclusive) and 1 (exclusive).static Matrix
getRowMinUnique(Matrix ma)
static Matrix
getScaled(Matrix ma)
Centers and divides by standard deviation.static Matrix
getScaledByFactor(Matrix ma, double fac)
static double
getSumSquaredDiff(Matrix ma1, int row1, Matrix ma2, int row2)
static double
getTotalSumSquaredDiffRowWise(Matrix ma1, Matrix ma2)
static double[][]
id(int n)
Returns a (n x n) identity matrix.static Matrix
inv(Matrix A)
Returns the inverse of a matrix.static boolean
isEqualCol(Matrix A, int colA, Matrix B, int colB, double thresh)
static boolean
isSquare(Matrix A)
static boolean
isSymmetric(Matrix A)
static Matrix
read(java.io.File fiMatrix)
static Matrix
read(java.io.File fiMatrix, boolean skipFirstLine)
static Matrix
read(java.io.InputStream is)
static Matrix
read(java.io.InputStream is, boolean skipFirstLine)
static Matrix
read(java.lang.String s)
static Matrix
readAsLineBase64Encoded(java.lang.String s)
static Matrix
readCSV(java.io.File fi)
static Matrix
rnorm(int n, double mean, double sd)
static Matrix[]
split(Matrix A, int row)
Splits the matrix row wise into two matrices.static Matrix[]
splitCol(Matrix A, int col)
static double[]
upperTriangle(Matrix maQuadratic)
static void
vecvec2Matrix(java.util.Vector<java.util.Vector<java.lang.Double>> vecvec, Matrix ma)
Converts a vector of vectors into doubles, each vector results in a row in the matrix.static void
writeQuadraticSymmetricMatrixPairwise(Matrix ma, java.io.File fiTxt)
-
-
-
Method Detail
-
split
public static Matrix[] split(Matrix A, int row)
Splits the matrix row wise into two matrices.- Parameters:
A
-row
-- Returns:
-
equals
public static boolean equals(double d1, double d2)
-
id
public static double[][] id(int n)
Returns a (n x n) identity matrix. (The elements on the diagonal have value1.0
, all others are set to0.0
).- Parameters:
n
- the size of the matrix- Returns:
- the identity matrix
-
inv
public static Matrix inv(Matrix A)
Returns the inverse of a matrix. The method uses a LU decomposition with pivot. There is no check on the size (is it a square matrix) or the rank (is it inversible, ie det doesn't equal 0).- Parameters:
A
-- Returns:
- A^(-1)
-
isSymmetric
public static boolean isSymmetric(Matrix A)
-
isSquare
public static boolean isSquare(Matrix A)
-
cholesky
public static Matrix cholesky(Matrix A)
- Parameters:
A
-- Returns:
- Cholesky factor L of psd matrix A = L L^T
-
calculateSimilarityMatrixRowWise
public static Matrix calculateSimilarityMatrixRowWise(Matrix X1, Matrix X2)
-
calculateMaxSimilarity
public static IntegerDouble[] calculateMaxSimilarity(Matrix X, double[] arr, int nMostSimilar)
-
calculateMaxSimilarity
public static IntegerDouble calculateMaxSimilarity(Matrix X, double[] arr)
-
upperTriangle
public static double[] upperTriangle(Matrix maQuadratic)
- Parameters:
maQuadratic
-- Returns:
- upper triangle from a quadratic matrix as an array.
-
create
public static Matrix create(java.util.List<double[]> li)
List is already used as a constructor for Matrix. So we have to place this method here.- Parameters:
li
-- Returns:
-
countFieldsBiggerThan
public static int countFieldsBiggerThan(Matrix ma, int row, double thresh)
-
countFieldsBiggerThanThreshColWise
public static int countFieldsBiggerThanThreshColWise(Matrix ma, int col, double thresh)
-
countFieldsBiggerThanThreshColWise
public static Matrix countFieldsBiggerThanThreshColWise(Matrix ma, double thresh)
-
getDistanceMatrixTanimotoInv
public static Matrix getDistanceMatrixTanimotoInv(Matrix ma1, Matrix ma2)
Calculates the inverse Tanimoto coefficient from row wise comparison of the two input matrices.- Parameters:
ma1
-ma2
-- Returns:
- complete distance matrix calculated between all rows from the two input matrices.
-
getDistanceMatrix
public static Matrix getDistanceMatrix(java.util.List<java.awt.Point> li)
-
getDistanceTanimotoInv
public static double getDistanceTanimotoInv(Matrix ma1, int row1, Matrix ma2, int row2)
- Parameters:
ma1
- Matrixrow1
- rowma2
- Matrixrow2
- row- Returns:
- maximum distance = 0, minimum distance = 1
-
getSumSquaredDiff
public static double getSumSquaredDiff(Matrix ma1, int row1, Matrix ma2, int row2)
-
getTotalSumSquaredDiffRowWise
public static double getTotalSumSquaredDiffRowWise(Matrix ma1, Matrix ma2)
-
getDistTanimotoInvReduced
public static double getDistTanimotoInvReduced(Matrix ma1, int row1, Matrix ma2, int row2)
Only fields are considered which are not nut 0 in both or in one of the rows we from where the distance is calculated.- Parameters:
ma1
- Matrixrow1
- rowma2
- Matrixrow2
- row- Returns:
- maximum distance = 0, minimum distance = 1
-
getMooreNeighborhood
public static java.util.List<java.awt.Point> getMooreNeighborhood(java.awt.Point p, Matrix ma)
A square-shaped neighborhood that can be used to define a set of cells surrounding a given point.- Parameters:
p
-ma
-- Returns:
-
getMooreNeighborhood
public static java.util.List<java.awt.Point> getMooreNeighborhood(java.awt.Point p, int r, Matrix ma)
-
getRowMinUnique
public static Matrix getRowMinUnique(Matrix ma)
- Parameters:
ma
- Matrix with ma.rows and 1 col. Containing the min value from each row.- Returns:
-
getPoints
public static java.util.List<java.awt.Point> getPoints(Matrix ma)
- Parameters:
ma
-- Returns:
- list with indices Point(row,col) for values>0.
-
getIndicesUniqueMaxRowWise
public static java.util.List<java.awt.Point> getIndicesUniqueMaxRowWise(Matrix maIn)
-
getIndicesUniqueMaxColumnWise
public static java.util.List<java.awt.Point> getIndicesUniqueMaxColumnWise(Matrix maIn)
-
getScaled
public static Matrix getScaled(Matrix ma)
Centers and divides by standard deviation.- Parameters:
ma
-- Returns:
-
getKMeanClusters
public static Matrix getKMeanClusters(Matrix ma, int k)
03.10.04 MvK- Parameters:
ma
- matrix with objects in rowsk
- number of desired cluster- Returns:
- Matrix of cluster centers, k rows and cols equal ma.cols.
-
getCorrPearson
public static double getCorrPearson(Matrix A, int col1, int col2)
-
getRandomMatrix
public static Matrix getRandomMatrix(int rows, int cols)
generates a matrix with double values between 0 (inclusive) and 1 (exclusive).- Parameters:
rows
- rowscols
- columns- Returns:
- matrix
-
rnorm
public static Matrix rnorm(int n, double mean, double sd)
- Parameters:
n
-mean
-sd
-- Returns:
- random one column matrix with given mean and standard deviation.
-
vecvec2Matrix
public static void vecvec2Matrix(java.util.Vector<java.util.Vector<java.lang.Double>> vecvec, Matrix ma)
Converts a vector of vectors into doubles, each vector results in a row in the matrix. All vectors have to be of equal length or a runtime exception is thrown.- Parameters:
vecvec
- vector on vectors, has to be converted into doublesma
- resulting matrix
-
readCSV
public static Matrix readCSV(java.io.File fi) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public static Matrix read(java.io.File fiMatrix) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public static Matrix read(java.io.File fiMatrix, boolean skipFirstLine) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public static Matrix read(java.lang.String s) throws java.io.IOException
- Throws:
java.io.IOException
-
readAsLineBase64Encoded
public static Matrix readAsLineBase64Encoded(java.lang.String s) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public static Matrix read(java.io.InputStream is)
-
read
public static Matrix read(java.io.InputStream is, boolean skipFirstLine)
-
writeQuadraticSymmetricMatrixPairwise
public static void writeQuadraticSymmetricMatrixPairwise(Matrix ma, java.io.File fiTxt) throws java.io.IOException
- Throws:
java.io.IOException
-
columnIntoDoubleArray
public static void columnIntoDoubleArray(Matrix A, int col, DoubleArray da)
-
createIdentifiedObject
public static java.util.List<IdentifiedObject<double[]>> createIdentifiedObject(Matrix A)
-
-