Bounding ellipse - java

I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn't have to be axis aligned.
This is working in java (euch) using the AWT shapes, so I can use all the tools shape provides for checking containment/intersection of objects.

You're looking for the Minimum Volume Enclosing Ellipsoid, or in your 2D case, the minimum area. This optimization problem is convex and can be solved efficiently. Check out the MATLAB code in the link I've included - the implementation is trivial and doesn't require anything more complex than a matrix inversion.
Anyone interested in the math should read this document.
Also, plotting the ellipse is also simple - this can be found here, but you'll need a MATLAB-specific function to generate points on the ellipse.
But since the algorithm returns the equation of the ellipse in the matrix form,
you can use this code to see how you can convert the equation to the canonical form,
using Singular Value Decomposition (SVD). And then it's quite easy to plot the ellipse using the canonical form.
Here's the result of the MATLAB code on a set of 10 random 2D points (blue).
Other methods like PCA does not guarantee that the ellipse obtained from the decomposition (eigen/singular value) will be minimum bounding ellipse since points outside the ellipse is an indication of the variance.
EDIT:
So if anyone read the document, there are two ways to go about this in 2D: here's the pseudocode of the optimal algorithm - the suboptimal algorithm is clearly explained in the document:
Optimal algorithm:
Input: A 2x10 matrix P storing 10 2D points
and tolerance = tolerance for error.
Output: The equation of the ellipse in the matrix form,
i.e. a 2x2 matrix A and a 2x1 vector C representing
the center of the ellipse.
// Dimension of the points
d = 2;
// Number of points
N = 10;
// Add a row of 1s to the 2xN matrix P - so Q is 3xN now.
Q = [P;ones(1,N)]
// Initialize
count = 1;
err = 1;
//u is an Nx1 vector where each element is 1/N
u = (1/N) * ones(N,1)
// Khachiyan Algorithm
while err > tolerance
{
// Matrix multiplication:
// diag(u) : if u is a vector, places the elements of u
// in the diagonal of an NxN matrix of zeros
X = Q*diag(u)*Q'; // Q' - transpose of Q
// inv(X) returns the matrix inverse of X
// diag(M) when M is a matrix returns the diagonal vector of M
M = diag(Q' * inv(X) * Q); // Q' - transpose of Q
// Find the value and location of the maximum element in the vector M
maximum = max(M);
j = find_maximum_value_location(M);
// Calculate the step size for the ascent
step_size = (maximum - d -1)/((d+1)*(maximum-1));
// Calculate the new_u:
// Take the vector u, and multiply all the elements in it by (1-step_size)
new_u = (1 - step_size)*u ;
// Increment the jth element of new_u by step_size
new_u(j) = new_u(j) + step_size;
// Store the error by taking finding the square root of the SSD
// between new_u and u
// The SSD or sum-of-square-differences, takes two vectors
// of the same size, creates a new vector by finding the
// difference between corresponding elements, squaring
// each difference and adding them all together.
// So if the vectors were: a = [1 2 3] and b = [5 4 6], then:
// SSD = (1-5)^2 + (2-4)^2 + (3-6)^2;
// And the norm(a-b) = sqrt(SSD);
err = norm(new_u - u);
// Increment count and replace u
count = count + 1;
u = new_u;
}
// Put the elements of the vector u into the diagonal of a matrix
// U with the rest of the elements as 0
U = diag(u);
// Compute the A-matrix
A = (1/d) * inv(P * U * P' - (P * u)*(P*u)' );
// And the center,
c = P * u;

Thanks to Jacob's pseudocode I was able to implement the Minimum Volume Enclosing Ellipsoid (MVEE) in Java. There are public methods to get the center point, the "A" matrix, and a method to generate a list of coordinates that can be used to render the ellipse. The latter is based on MatLab code posted by Peter Lawrence in the comments to the original MVEE code. Note that the code references a class called "Eigen" - a modified version of Jama's EigenvalueDecomposition class (I took out Matrix class dependencies). I would add it but there is a 30k character limit to answers...
public class Ellipse {
private double[] center;
private double[][] A;
private double l1;
private double l2;
private double thu;
//**************************************************************************
//** Constructor
//**************************************************************************
/** #param P An array of points. Each entry in the array contains an x,y
* coordinate.
*/
public Ellipse(double[][] P, double tolerance){
// Dimension of the points
double d = 2;
// Number of points
int N = P.length;
// Rotate the array of points
P = transpose(P);
// Add a row of 1s to the 2xN matrix P - so Q is 3xN now.
//Q = [P;ones(1,N)]
double[][] Q = merge(P, ones(1,N));
// Initialize
int count = 1;
double err = 1;
//u is an Nx1 vector where each element is 1/N
//u = (1/N) * ones(N,1)
double[] u = new double[N];
for (int i=0; i<u.length; i++) u[i] = (1D/(double)N);
// Khachiyan Algorithm
while (err > tolerance){
// Matrix multiplication:
// diag(u) : if u is a vector, places the elements of u
// in the diagonal of an NxN matrix of zeros
//X = Q*diag(u)*Q'; // Q' - transpose of Q
double[][] X = multiply(multiply(Q,diag(u)), transpose(Q));
// inv(X) returns the matrix inverse of X
// diag(M) when M is a matrix returns the diagonal vector of M
//M = diag(Q' * inv(X) * Q); // Q' - transpose of Q
double[] M = diag(multiply(multiply(transpose(Q), inv(X)), Q));
//Find the value and location of the maximum element in the vector M
double maximum = max(M);
int j = find_maximum_value_location(M, maximum);
// Calculate the step size for the ascent
double step_size = (maximum - d -1)/((d+1)*(maximum-1));
// Calculate the new_u:
// Take the vector u, and multiply all the elements in it by (1-step_size)
double[] new_u = multiply((1 - step_size), u);
// Increment the jth element of new_u by step_size
new_u[j] = new_u[j] + step_size;
// Calculate error by taking finding the square root of the SSD
// between new_u and u
err = Math.sqrt(ssd(new_u, u));
// Increment count and replace u
count = count + 1;
u = new_u;
}
// Compute center point
//c = P * u
double[][] c = multiply(P, u);
center = transpose(c)[0];
// Put the elements of the vector u into the diagonal of a matrix
// U with the rest of the elements as 0
double[][] U = diag(u);
// Compute the A-matrix
//A = (1/d) * inv(P * U * P' - (P * u)*(P*u)' );
double[][] pup = multiply(multiply(P, U) , transpose(P));
double[][] pupu = multiply((multiply(P, u)), transpose(multiply(P, u)));
double[][] pup_pupu = minus(pup, pupu);
A = multiply((1/d), inv(pup_pupu));
// Compute Eigen vectors and values
//A=inv(A);
//[Ve,De]=eig(A);
Eigen eig = new Eigen(inv(A));
double[][] Ve = eig.getV(); //eigenvalues
double[][] De = eig.getD(); //right eigenvectors
reorderEigenVectors(De);
reorderEigenValues(Ve);
//v=sqrt(diag(De));
double[] v = sqrt(diag(De));
//[l1,Ie] = max(v);
l1 = max(v);
int Ie = find_maximum_value_location(v, l1); //off by one from MatLab but I think it's ok here
//veig=Ve(:,Ie);
double[] veig = new double[Ve.length];
for (int i=0; i<veig.length; i++){
veig[i] = Ve[Ie][i];
}
//thu=atan2(veig(2),veig(1));
thu = Math.atan2(veig[1], veig[0]);
//l2=v(setdiff([1 2],Ie));
l2 = v[setdiff(new int[]{0,1}, Ie)];
}
//**************************************************************************
//** getCenter
//**************************************************************************
/** Returns the center point of the ellipse
*/
public double[] getCenter(){
double[] pt = new double[2];
pt[0] = center[0];
pt[1] = center[1];
return pt;
}
//**************************************************************************
//** getMatrix
//**************************************************************************
/** Returns a matrix containing all the information regarding the shape of
* the ellipsoid. To get the radii and orientation of the ellipsoid take
* the Singular Value Decomposition of the matrix.
*/
public double[][] getMatrix(){
return A;
}
//**************************************************************************
//** getBoundingCoordinates
//**************************************************************************
/** Returns a list of coordinates that can be used to render the ellipse.
* #param numPoints The number of points used to represent the ellipse.
* The higher the number the more dense the ellipse outline, the more
* accurate the shape.
*/
public double[][] getBoundingCoordinates(int numPoints){
//tq=linspace(-pi,pi,50);
double[] tq = linspace(-Math.PI, Math.PI, numPoints);
//U=[cos(thu) -sin(thu);sin(thu) cos(thu)]*[l1*cos(tq);l2*sin(tq)];
double[][] U = multiply(
new double[][]{
createVector(Math.cos(thu), -Math.sin(thu)),
createVector(Math.sin(thu), Math.cos(thu))
},
new double[][]{
multiply(l1, cos(tq)),
multiply(l2, sin(tq))
}
);
//System.out.println(toString(transpose(U)));
double[][] coords = transpose(U);
for (int i=0; i<coords.length; i++){
double x = coords[i][0] + center[0];
double y = coords[i][1] + center[1];
coords[i][0] = x;
coords[i][1] = y;
}
return coords;
}
//**************************************************************************
//** reorderEigenVectors
//**************************************************************************
/** Eigen values generated from Apache Common Math and JAMA are different
* than MatLab. The vectors are in the reverse order than expected. This
* function will update the array to what we expect to see in MatLab.
*/
private void reorderEigenVectors(double[][] De){
rotateMatrix(De);
rotateMatrix(De);
}
//**************************************************************************
//** reorderEigenValues
//**************************************************************************
/** Eigen values generated from Apache Common Math and JAMA are different
* than MatLab. The vectors are in reverse order than expected and with an
* opposite sign. This function will update the array to what we expect to
* see in MatLab.
*/
private void reorderEigenValues(double[][] Ve){
rotateMatrix(Ve);
for (int i=0; i<Ve.length; i++){
for (int j=0; j<Ve[i].length; j++){
Ve[i][j] = -Ve[i][j];
}
}
}
//**************************************************************************
//** linspace
//**************************************************************************
private double[] linspace(double min, double max, int points) {
double[] d = new double[points];
for (int i = 0; i < points; i++){
d[i] = min + i * (max - min) / (points - 1);
}
return d;
}
//**************************************************************************
//** ssd
//**************************************************************************
/** Returns the sum-of-square-differences between tow arrays. Takes two
* vectors of the same size, creates a new vector by finding the difference
* between corresponding elements, squaring each difference and adding them
* all together. So if the vectors were: a = [1 2 3] and b = [5 4 6], then:
* SSD = (1-5)^2 + (2-4)^2 + (3-6)^2;
*/
private double ssd(double[] a, double[] b){
double ssd = 0;
for (int i=0; i<a.length; i++){
ssd += Math.pow(a[i]-b[i], 2);
}
return ssd;
}
//**************************************************************************
//** ones
//**************************************************************************
/** Creates an array of all ones. For example, ones(2,3) returns a 2-by-3
* array of ones.
<pre>
1 1 1
1 1 1
</pre>
* Reference: https://www.mathworks.com/help/matlab/ref/ones.html
*/
private double[][] ones(int rows, int cols){
double[][] arr = new double[rows][];
for (int i=0; i<arr.length; i++){
double[] row = new double[cols];
for (int j=0; j<row.length; j++){
row[j] = 1;
}
arr[i] = row;
}
return arr;
}
//**************************************************************************
//** merge
//**************************************************************************
/** Used to combine two arrays into one
*/
private double[][] merge(double[][] m1, double[][] m2) {
int x = 0;
double[][] out = new double[m1.length + m2.length][];
for (int i=0; i<m1.length; i++){
out[x] = m1[i];
x++;
}
for (int i=0; i<m2.length; i++){
out[x] = m2[i];
x++;
}
return out;
}
//**************************************************************************
//** multiply
//**************************************************************************
/** Used to multiply all the values in the vector (arr) by n. This is called
* scalar multiplication.
*/
private double[] multiply(double n, double[] arr){
double[] out = new double[arr.length];
for (int i=0; i<arr.length; i++){
out[i] = arr[i]*n;
}
return out;
}
//**************************************************************************
//** multiply
//**************************************************************************
/** Used to multiply all the values in the matrix (arr) by n
*/
private double[][] multiply(double n, double[][] arr){
double[][] out = new double[arr.length][];
for (int i=0; i<arr.length; i++){
double[] row = arr[i];
double[] r = new double[row.length];
for (int j=0; j<row.length; j++){
r[j] = row[j]*n;
}
out[i] = r;
}
return out;
}
//**************************************************************************
//** multiply
//**************************************************************************
/** Multiply a matrix with a vector by converting the vector to a matrix
*/
private double[][] multiply(double[][] P, double[] u){
double[][] m2 = new double[u.length][];
for (int i=0; i<m2.length; i++){
double[] row = new double[1];
row[0] = u[i];
m2[i] = row;
}
return multiply(P, m2);
}
//**************************************************************************
//** multiply
//**************************************************************************
/** Used to multiply two matrices. Credit:
* https://stackoverflow.com/a/23817780
*/
private double[][] multiply(double[][] m1, double[][] m2) {
int m1ColLength = m1[0].length; // m1 columns length
int m2RowLength = m2.length; // m2 rows length
if(m1ColLength != m2RowLength) return null; // matrix multiplication is not possible
int mRRowLength = m1.length; // m result rows length
int mRColLength = m2[0].length; // m result columns length
double[][] mResult = new double[mRRowLength][mRColLength];
for(int i = 0; i < mRRowLength; i++) { // rows from m1
for(int j = 0; j < mRColLength; j++) { // columns from m2
for(int k = 0; k < m1ColLength; k++) { // columns from m1
mResult[i][j] += m1[i][k] * m2[k][j];
}
}
}
return mResult;
}
//**************************************************************************
//** diag
//**************************************************************************
/** Returns a matrix for a given vector. The values in the vector will
* appear diagonally in the output.
* Reference: https://www.mathworks.com/help/matlab/ref/diag.html
*/
private double[][] diag(double[] arr){
double[][] out = new double[arr.length][];
for (int i=0; i<arr.length; i++){
double[] row = new double[arr.length];
for (int j=0; j<row.length; j++){
if (j==i) row[j] = arr[i];
else row[j] = 0;
}
out[i] = row;
}
return out;
}
//**************************************************************************
//** diag
//**************************************************************************
/** Returns a vector representing values that appear diagonally in the given
* matrix.
* Reference: https://www.mathworks.com/help/matlab/ref/diag.html
*/
private double[] diag(double[][] arr){
double[] out = new double[arr.length];
for (int i=0; i<arr.length; i++){
out[i] = arr[i][i];
}
return out;
}
//**************************************************************************
//** transpose
//**************************************************************************
/** Interchanges the row and column index for each element
* Reference: https://www.mathworks.com/help/matlab/ref/transpose.html
*/
private double[][] transpose(double[][] arr){
int rows = arr.length;
int cols = arr[0].length;
double[][] out = new double[cols][rows];
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
out[x][y] = arr[y][x];
}
}
return out;
}
//**************************************************************************
//** inv
//**************************************************************************
/** Returns the inverse of a matrix. Relies on 2 different implementations.
* The first implementation is more accurate (passes inverse check) but
* has the potential to fail. If so, falls back to second method that
* relies on partial-pivoting Gaussian elimination.
* Reference: https://www.mathworks.com/help/matlab/ref/inv.html
*/
private double[][] inv(double[][] matrix){
try{
return inv1(matrix);
}
catch(Exception e){
try{
return inv2(matrix);
}
catch(Exception ex){
throw new RuntimeException(ex);
}
}
}
//**************************************************************************
//** inv1
//**************************************************************************
/** Returns the inverse of a matrix. This implementation passes inverse
* check so I think it's valid but it has a tendency to fail. For example,
* the following matrix fails with a ArrayIndexOutOfBoundsException in the
* determinant method.
<pre>
1171.18 658.33
658.33 1039.55
</pre>
* Credit: https://github.com/rchen8/Algorithms/blob/master/Matrix.java
*/
private double[][] inv1(double[][] matrix){
double[][] inverse = new double[matrix.length][matrix.length];
// minors and cofactors
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
inverse[i][j] = Math.pow(-1, i + j)
* determinant(minor(matrix, i, j));
// adjugate and determinant
double det = 1.0 / determinant(matrix);
for (int i = 0; i < inverse.length; i++) {
for (int j = 0; j <= i; j++) {
double temp = inverse[i][j];
inverse[i][j] = inverse[j][i] * det;
inverse[j][i] = temp * det;
}
}
return inverse;
}
private static double determinant(double[][] matrix) {
if (matrix.length != matrix[0].length)
throw new IllegalStateException("invalid dimensions");
if (matrix.length == 2)
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
double det = 0;
for (int i = 0; i < matrix[0].length; i++)
det += Math.pow(-1, i) * matrix[0][i]
* determinant(minor(matrix, 0, i));
return det;
}
private static double[][] minor(double[][] matrix, int row, int column) {
double[][] minor = new double[matrix.length - 1][matrix.length - 1];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; i != row && j < matrix[i].length; j++)
if (j != column)
minor[i < row ? i : i - 1][j < column ? j : j - 1] = matrix[i][j];
return minor;
}
//**************************************************************************
//** inv2
//**************************************************************************
/** Returns the inverse of a matrix. This implementation successfully
* executes but does not pass the inverse check.
* Credit: https://www.sanfoundry.com/java-program-find-inverse-matrix/
*/
public static double[][] inv2(double a[][]){
int n = a.length;
double x[][] = new double[n][n];
double b[][] = new double[n][n];
int index[] = new int[n];
for (int i=0; i<n; ++i)
b[i][i] = 1;
//Transform the matrix into an upper triangle
gaussian(a, index);
//Update the matrix b[i][j] with the ratios stored
for (int i=0; i<n-1; ++i){
for (int j=i+1; j<n; ++j){
for (int k=0; k<n; ++k){
b[index[j]][k]
-= a[index[j]][i]*b[index[i]][k];
}
}
}
//Perform backward substitutions
for (int i=0; i<n; ++i){
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];
for (int j=n-2; j>=0; --j){
x[j][i] = b[index[j]][i];
for (int k=j+1; k<n; ++k){
x[j][i] -= a[index[j]][k]*x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}
// Method to carry out the partial-pivoting Gaussian
// elimination. Here index[] stores pivoting order.
public static void gaussian(double a[][], int index[]) {
int n = index.length;
double c[] = new double[n];
// Initialize the index
for (int i=0; i<n; ++i)
index[i] = i;
// Find the rescaling factors, one from each row
for (int i=0; i<n; ++i) {
double c1 = 0;
for (int j=0; j<n; ++j){
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}
// Search the pivoting element from each column
int k = 0;
for (int j=0; j<n-1; ++j){
double pi1 = 0;
for (int i=j; i<n; ++i){
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1) {
pi1 = pi0;
k = i;
}
}
// Interchange rows according to the pivoting order
int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i){
double pj = a[index[i]][j]/a[index[j]][j];
// Record pivoting ratios below the diagonal
a[index[i]][j] = pj;
// Modify other elements accordingly
for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}
//**************************************************************************
//** max
//**************************************************************************
/** Returns the max value in a vector
*/
private double max(double[] arr){
double max = arr[0];
for (double d : arr){
max = Math.max(d, max);
}
return max;
}
//**************************************************************************
//** find_maximum_value_location
//**************************************************************************
/** Returns the index of the max value in a vector
*/
private int find_maximum_value_location(double[] arr, double max){
for (int i=0; i<arr.length; i++){
if (arr[i]==max) return i;
}
return 0;
}
//**************************************************************************
//** minus
//**************************************************************************
/** Used to subtract array B from array A and returns the result
* Reference: https://www.mathworks.com/help/matlab/ref/minus.html
*/
private double[][] minus(double[][] a, double[][] b){
double[][] out = new double[a.length][];
for (int i=0; i<out.length; i++){
double[] row = new double[a[i].length];
for (int j=0; j<row.length; j++){
row[j] = a[i][j]-b[i][j];
}
out[i] = row;
}
return out;
}
//**************************************************************************
//** sqrt
//**************************************************************************
/** Returns the square root of each element in a vector
* Reference: https://www.mathworks.com/help/matlab/ref/sqrt.html
*/
private double[] sqrt(double[] arr){
double[] out = new double[arr.length];
for (int i=0; i<out.length; i++){
out[i] = Math.sqrt(arr[i]);
}
return out;
}
private double[] cos(double[] arr){
double[] out = new double[arr.length];
for (int i=0; i<out.length; i++){
out[i] = Math.cos(arr[i]);
}
return out;
}
private double[] sin(double[] arr){
double[] out = new double[arr.length];
for (int i=0; i<out.length; i++){
out[i] = Math.sin(arr[i]);
}
return out;
}
//**************************************************************************
//** setdiff
//**************************************************************************
/** Partial implementation of setdiff
*/
private int setdiff(int[] arr, int x){
for (int i : arr){
if (i!=x) return i;
}
return 0; //?
}
//**************************************************************************
//** rotateMatrix
//**************************************************************************
private void rotateMatrix(double mat[][]) {
int N = mat[0].length;
// Consider all squares one by one
for (int x = 0; x < N / 2; x++)
{
// Consider elements in group of 4 in
// current square
for (int y = x; y < N-x-1; y++)
{
// store current cell in temp variable
double temp = mat[x][y];
// move values from right to top
mat[x][y] = mat[y][N-1-x];
// move values from bottom to right
mat[y][N-1-x] = mat[N-1-x][N-1-y];
// move values from left to bottom
mat[N-1-x][N-1-y] = mat[N-1-y][x];
// assign temp to left
mat[N-1-y][x] = temp;
}
}
}
//**************************************************************************
//** createVector
//**************************************************************************
/** Used to generate a vector for testing purposes
*/
private double[] createVector(double ...d){
double[] arr = new double[d.length];
for (int i=0; i<arr.length; i++){
arr[i] = d[i];
}
return arr;
}
}
Here an example output using 10 random points and a tolerance of 0.001. The ellipse is rendered using straight lines connecting points generated via the Ellipse.getBoundingCoordinates() method using 50 points.

Related

Cholesky decomposition generating NaNs in Java

I'm not sure whether this is a maths.se or a SO question, but I'm going with SO as I think it's related to my Java.
I'm following a text book on Gaussian Processes (R&W) and implementing some examples in Java. One common step for several examples is to generate a Cholesky decomposition of a covariance matrix. In my attempt I can get successful results for matrices up to a limited size (33x33). However, for anything larger a NaN appears in the diagonal (at 32,32) and so all subsequent values in the matrix are likewise NaNs.
The code is shown below, and the source of the NaN is indicated in the cholesky method. Essentially the covariance element a[32][32] is 1.0, but the value of sum is a little over this (1.0000001423291431), so the square root is imaginary. So my questions are:
Is this an expected result from linear algebra, or, e.g., an
artefact of my implementation?
How is this problem best avoided in practice?
Note that I'm not looking for recommendations of libraries to use. This is simply for my own understanding.
Apologies for the length, but I've tried to provide a complete MWE:
import static org.junit.Assert.assertFalse;
import org.junit.Test;
public class CholeskyTest {
#Test
public void testCovCholesky() {
final int n = 34; // Test passes for n<34
final double[] xData = getSpread(-5, 5, n);
double[][] cov = covarianceSE(xData);
double[][] lower = cholesky(cov);
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) {
assertFalse("NaN at " + i + "," + j, Double.isNaN(lower[i][j]));
}
}
}
/**
* Generate n evenly space values from min to max inclusive
*/
private static double[] getSpread(final double min, final double max, final int n) {
final double[] values = new double[n];
final double delta = (max - min)/(n - 1);
for(int i=0; i<n; ++i) {
values[i] = min + i*delta;
}
return values;
}
/**
* Calculate the covariance matrix for the given observations using
* the squared exponential (SE) covariance function.
*/
private static double[][] covarianceSE (double[] v) {
final int m = v.length;
double[][] k = new double[m][];
for(int i=0; i<m; ++i) {
double vi = v[i];
double row[] = new double[m];
for(int j=0; j<m; ++j) {
double dist = vi - v[j];
row[j] = Math.exp(-0.5*dist*dist);
}
k[i] = row;
}
return k;
}
/**
* Calculate lower triangular matrix L such that LL^T = A
* Using Cholesky decomposition from
* https://rosettacode.org/wiki/Cholesky_decomposition#Java
*/
private static double[][] cholesky(double[][] a) {
final int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i == k) ? Math.sqrt(a[i][i] - sum) : // Source of NaN at 32,32
(1.0 / l[k][k] * (a[i][k] - sum));
}
}
return l;
}
}
Hmm, I think I've found an answer to my own question, from the same textbook I was following. From R&W p.201:
In practice it may be necessary to add a small multiple of the
identity matrix $\epsilon I$ to the covariance matrix for numerical
reasons. This is because the eigenvalues of the matrix K can decay
very rapidly [...] and without this stabilization the Cholesky
decomposition fails. The effect on the generated samples is to add
additional independent noise of variance $epsilon$.
So the following change seems to be sufficient:
private static double[][] cholesky(double[][] a) {
final int m = a.length;
double epsilon = 0.000001; // Small extra noise value
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i == k) ? Math.sqrt(a[i][i]+epsilon - sum) : // Add noise to diagonal values
(1.0 / l[k][k] * (a[i][k] - sum));
}
}
return l;
}
I just finished writing my own version of a Cholesky Decomposition routine in C++ and JavaScript. Instead of computing L, it computes U, but I would be curious to test it with the matrix that causes the NaN error. Would you be able to post the matrix here, or contact me (info in Profile.)

Java: Create a duplicate of an array without making it a reference

I have written a series of matrix operations where I take a 2 dimensional float array, treat it as a matrix, and perform matrix operations on it to acquire an inverse. My problem has been that although the array I am using with the class methods isn't part of the class, every time I run the method with the array as the parameter, the array itself also becomes modified.
First I will describe how I got the inverse of my matrix and then I will show the output.
The steps to taking the inverse of a matrix are as follows:
Get the cofactor matrix (i.e. create a matrix of matrix minors of the original matrix and then negate every other entry. If C = Cofactor Matrix, M = Matrix of Minors, i is the current row, and j is the current column, then C[ i ][ j ] = M[ i ][ j ]*( -1 )^( i + j )
Convert the cofactor matrix to the adjugate (also known as adjoint) matrix by transposing (replacing row, column entry by its analogous column, row entry and vice versa) the cofactor matrix. If C = Cofactor Matrix, A = Adjugate Matrix, i is the current row, and j is the current column, then A[ i ][ j ] = C[ j ][ i ]
Finally, take one over determinant of the original matrix and multiply the adjugate matrix by that value. If I = Inverse Matrix, A = Adjugate Matrix and D = Determinant, then I = (1/D)*A
In order to test if you have truly acquired the Matrix Inverse of a Matrix, one can multiply the original matrix by its inverse to get the identity matrix.
If I = Inverse, O = Original Matrix, and id = Identity Matrix then O*I = id
Now I will present the code where I implement these operations. For the sake of conciseness, I will not describe how to get the Matrix of Minors or the Determinant, but the problem I have been encountering will become apparent anyways.
public class MatrixOperations {
//Note: this method works fine. There are no problems.
public float determinant(float [][] a)
{
float [][] temp_mat;
float res = 0;
//assuming a square matrix
/*If it's a 2X2, then use the formula for a determinant of
2X2 matrices.*/
if(a.length == 2)
{
return a[0][0]*a[1][1]-a[0][1]*a[1][0];
}
/*Otherwise do the determinant formula recursively until your
determinant is made up of 2X2 matrix determinants and scalar products*/
else
{
temp_mat = new float[a.length-1][a.length-1];
int placej = 0;
int placei = 0;
for(int k = 0; k<a.length;k++)
{
for(int j = 0; j<a.length; j++)
{
for(int i = 1; i < a.length; i++)
{
placei = i-1;
if(j != k)
{
if(j < k)
{
temp_mat[placei][j] = a[i][j];
}
else if(j > k)
{
if (i == 1){
placej = j-1;
}
temp_mat[placei][placej] = a[i][j];
}
}
}
}
res+=a[0][k]*determinant(temp_mat)*(int)Math.pow(-1, k);
}
return res;
}
}
//Note: this method also works fine
//Scalar product method
public float[][] mul(float[][] m, float r)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j]= m[i][j]*r;
}
}
return res;
}
//Note: This method also works fine
public float[][] mul(float[][] m,float[][] n)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
res[i][j] += m[i][k]*m[k][i];
}
}
}
return res;
}
//The method for creating a matrix of minors
//Here I start having problems
public float[][] minor(float [][] m)
{
float [][] minor_mat = new float [m.length][m.length];
//If the matrix is greater than a 2X2, use this to generate a matrix of minors
if(m.length > 2)
{
float [][] current_minor = new float [m.length-1][m.length-1];
int placei = 0;
int placej = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
for(int l = 0; l < m.length; l++)
{
if(i != k && j != l)
{
if(k<i)
placei = k;
else if(k>i)
placei = k-1;
if(l<j)
placej = l;
else if(l>j)
placej = l-1;
current_minor[placei][placej] = m[k][l];
}
}
}
minor_mat[i][j] = this.determinant(current_minor);
}
}
}
//otherwise use the definition for 2X2 matrix of minors
else
{
//even though minor_mat is using m.clone() rather than m, when I return the result, m has still been modified for some reason.
minor_mat = m.clone()
float temp;
temp = minor_mat[0][0];
minor_mat[0][0] = minor_mat[1][1];
minor_mat[1][1] = temp;
temp = minor_mat[0][1];
minor_mat[0][1] = minor_mat[1][0];
minor_mat[1][0] = temp;
}
return minor_mat;
}
//the same problem occurs here as it did in the minor method
//m appears to get modified even though I only use m.clone()
public float[][] cofactor(float [][] m)
{
float[][] res = m.clone();
res = this.minor(res)
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j] = res[i][j]*(int)Math.pow(-1, i + j);
}
}
return res;
}
//The following transpose, adjugate, and inverse methods have the same problem
public float[][] transpose(float[][] m)
{
float[][] res = new float[m.length][m.length];
float temp = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
temp = m[i][j];
res[i][j] = m[j][i];
res[j][i] = temp;
}
}
return res;
}
public float[][] adjugate(float[][] m)
{
float[][] res = this.transpose(this.cofactor(m));
return res;
}
public float[][] inverse(float[][] m)
{
float[][] res = this.mul(this.adjugate(m), (1/this.determinant(m)));
return res;
}
//print out the matrix in square form
public void matrixprint(float [][] m)
{
for(int i = 0; i < m.length; i++)
{
System.out.println("");
for(int j = 0; j < m[i].length; j++){
System.out.print(m[i][j] + " ");
}
}
System.out.println("\n");
}
}
Now the main class and the main method that creates an instance of the MatrixOperations class and uses its methods on a 2X2 matrix.
public class Main {
public static void main(String[] args) {
MatrixOperations mo = new MatrixOperations();
//Create a 2X2 matrix called "matrix" and set its elements
//Then perform each step on "matrix" and finally test if you have acquired the correct inverse
float [][] matrix = new float[2][2];
matrix[0][0] = 2;
matrix [0][1] = 5;
matrix [1][0] = 4;
matrix [1][1] = 3;
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Minor = ");
mo.matrixprint(mo.minor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Cofactor = ");
mo.matrixprint(mo.cofactor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Adjugate = ");
mo.matrixprint(mo.adjugate(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Determinant = ");
System.out.println(mo.determinant(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Inverse = ");
mo.matrixprint(mo.inverse(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Identity = ");
mo.matrixprint(mo.mul(mo.inverse(matrix), matrix));
}
}
Now you will see that when I show the output, every time I use a method on "matrix", and reprint "matrix", "matrix" itself has been modified even though my methods only use a copy of "matrix" and not "matrix" itself.
Output:
Matrix =
2.0 5.0
4.0 3.0
Minor =
3.0 4.0
5.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Cofactor =
3.0 -4.0
-5.0 2.0
Matrix =
3.0 -4.0
-5.0 2.0
Adjugate =
3.0 5.0
4.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Determinant =
-14.0
Matrix =
3.0 4.0
5.0 2.0
Inverse =
-0.21428573 0.35714287
0.2857143 -0.14285715
Matrix =
3.0 -4.0
-5.0 2.0
Identity =
0.1479592 0.1479592
0.12244898 0.12244898
Any help/explanation as to why this happens would be appreciated.
This line does a shallow clone;
float[][] res = m.clone();
This copies the res which is array fo references to arrays. but not any of the arrays res points to. Most likely what you wanted is
float[][] res = new float[m.length][];
for (int i = 0; i < m.length; i++)
res[i] = m[i].clone();
It's because you are passing reference of matrix object in the methods of MatrixOperations class. It's not a copy of matrix object.
From Java doc:
Reference data type parameters, such as objects, are also passed into
methods by value. This means that when the method returns, the
passed-in reference still references the same object as before.
A two-dimensional array is just an array of arrays.
clone() on an array just does a shallow clone.
So you have a new cloned outer array, but it references the same entries (the inner arrays).
After cloning the outer array, iterate over the outer array and clone all inner arrays to get a deep clone.

Transposing a matrix from a 2D array

I'm self teaching myself some java and I'm stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array.
An example output is:
$ java Test1 22 333 44 555 6
Enter the number of rows (1-10): 0
ERROR: number not in specified range (1-10) !
and so on until you enter the correct number of rows and columns.
Original matrix
1 22
333 44
555 6
Transposed matrix
1 333 555`
22 44 6`
^ Should be the final output. Some help with the code would appreciated!
I would like to code to generate error messages if the number of rows or columns is outside the specified range. And for if to read the matrix elements from the command line and not generate them randomly.
import java.util.Scanner;
public class Test1 {
/** Main method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows (1-10): ");
int rows = input.nextInt();
System.out.print("Enter the number of columns (1-10): ");
int cols = input.nextInt();
// Create originalMatrix as rectangular two dimensional array
int[][] originalMatrix = new int[rows][cols];
// Assign random values to originalMatrix
for (int row = 0; row < originalMatrix.length; row++)
for (int col = 0; col < originalMatrix[row].length; col++) {
originalMatrix[row][col] = (int) (Math.random() * 1000);
}
// Print original matrix
System.out.println("\nOriginal matrix:");
printMatrix(originalMatrix);
// Transpose matrix
int[][] resultMatrix = transposeMatrix(originalMatrix);
// Print transposed matrix
System.out.println("\nTransposed matrix:");
printMatrix(resultMatrix);
}
/** The method for printing the contents of a matrix */
public static void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
}
/** The method for transposing a matrix */
public static int[][] transposeMatrix(int[][] matrix) {
// Code goes here...
}
}
This is a simple method that return an int[][] of the transposed matrix...
public static int[][] transposeMatrix(int[][] matrix){
int m = matrix.length;
int n = matrix[0].length;
int[][] transposedMatrix = new int[n][m];
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
transposedMatrix[x][y] = matrix[y][x];
}
}
return transposedMatrix;
}
Than to print a 2D matrix you can use a method like this:
public static String matrixToString(int[][] a){
int m = a.length;
int n = a[0].length;
String tmp = "";
for(int y = 0; y<m; y++){
for(int x = 0; x<n; x++){
tmp = tmp + a[y][x] + " ";
}
tmp = tmp + "\n";
}
return tmp;
}
The answer provided above is not efficient in terms of memory. It is using another array - transposedMatrix apart from array supplied as argument. This will lead to consume double memory. We can do this in-place as follows:
public void transposeMatrix(int[][] a)
{
int temp;
for(int i=0 ; i<(a.length/2 + 1); i++)
{
for(int j=i ; j<(a[0].length) ; j++)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
displayMatrix(a);
}
public void displayMatrix(int[][] a){
for(int i=0 ; i<a.length ; i++)
{
for(int j=0 ; j<a[0].length ; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
You can use the below class it has most of the methods you want.
/**
* Class representing square matrix of given size.
* It has methods to rotate by 90, 180 and 270
* And also to transpose and flip on either axis.
*
* I have used both space efficient methods in transpose and flip
* And simple but more space usage for rotation.
*
* This is using builder pattern hence, you can keep on applying
* methods say rotate90().rotate90() to get 180 turn.
*
*/
public class Matrix {
private int[][] matrix;
final int size;
public Matrix(final int size) {
this.size = size;
matrix = new int[size][size];
for (int i=0;i<size;i++)
for (int j=0;j<size;j++)
matrix[i][j] = i*size + j;
}
public Matrix rotate90() {
int[][] temp = new int[size][size];
for (int i=0;i<size;i++)
for (int j=0;j<size;j++)
temp[i][j] = matrix[size-1-j][i];
matrix = temp;
return this;
}
public Matrix rotate180() {
int[][] temp = new int[size][size];
for (int i=0;i<size;i++)
for (int j=0;j<size;j++)
temp[i][j] = matrix[size-1-i][size-1-j];
matrix = temp;
return this;
}
public Matrix rotate270() {
int[][] temp = new int[size][size];
for (int i=0;i<size;i++)
for (int j=0;j<size;j++)
temp[i][j] = matrix[j][size-1-i];
matrix = temp;
return this;
}
public Matrix transpose() {
for (int i=0; i<size-1; i++) {
for (int j=i+1; j<size; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
return this;
}
public Matrix flipVertical() {
for (int i=0; i<size; i++) {
for (int j=0; j<size/2; j++) {
int tmp = matrix[i][size-1-j];
matrix[i][size-1-j] = matrix[i][j];
matrix[i][j] = tmp;
}
}
return this;
}
public Matrix flipHorizontal() {
for (int i=0; i<size/2; i++) {
for (int j=0; j<size; j++) {
int tmp = matrix[size-1-i][j];
matrix[size-1-i][j] = matrix[i][j];
matrix[i][j] = tmp;
}
}
return this;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
sb.append("|");
sb.append(matrix[i][j]);
if (size > 3) {
sb.append("\t");
}
}
sb.append("|\n");
}
return sb.toString();
}
public static void main(String... args) {
Matrix m = new Matrix(3);
System.out.println(m);
//transpose and flipHorizontal is 270 turn (-90)
System.out.println(m.transpose());
System.out.println(m.flipHorizontal());
//rotate 90 further to bring it back to original position
System.out.println(m.rotate90());
//transpose and flip Vertical is 90 degree turn
System.out.println(m.transpose().flipVertical());
}
}
Output:
|0|1|2|
|3|4|5|
|6|7|8|
|0|3|6|
|1|4|7|
|2|5|8|
|2|5|8|
|1|4|7|
|0|3|6|
|0|1|2|
|3|4|5|
|6|7|8|
|6|3|0|
|7|4|1|
|8|5|2|
For a square matrix, instead of iterating through the entire array, you just iterate through the diagonally half of the 2D array and swap the values with the corresponding indices.
public void transposeMatrix(int[][] a) {
for(int i=0 ; i<n; i++) {
for(int j=0 ; j<i ; j++) {
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
Another Kotlin solution for n x m matrix
fun transpose(matrix: Array<Array<Double>>): Array<Array<Double>> {
return matrix[0].mapIndexed { col, _ ->
matrix.mapIndexed { row, _ ->
matrix[row][col]
}.toTypedArray()
}.toTypedArray()
}
fun printMatrix(matrix: Array<Array<Double>>) {
println(matrix.joinToString("\n") { it.contentToString() })
}
fun main() {
val matrix = arrayOf(
arrayOf( 1.0, 2.0, 3.0),
arrayOf( 4.0, 5.0, 6.0),
arrayOf( 7.0, 8.0, 9.0),
arrayOf( 1.0, 2.0, 3.0),
)
println("matrix:")
printMatrix(matrix)
println("transpose(matrix):")
printMatrix(transpose(matrix))
}
Output
matrix:
[1.0, 2.0, 3.0]
[4.0, 5.0, 6.0]
[7.0, 8.0, 9.0]
[1.0, 2.0, 3.0]
transpose(matrix):
[1.0, 4.0, 7.0, 1.0]
[2.0, 5.0, 8.0, 2.0]
[3.0, 6.0, 9.0, 3.0]
Try it in the Kotlin Playground
Here is Kotlin Solution!
fun displayMatrix(matrix: Array<IntArray>) {
for(row in matrix) {
for(column in row) {
print("$column ")
}
println()
}
}
fun main() {
//Create Array
val matrixA = arrayOf(intArrayOf(2, 3, 4), intArrayOf(5, 6, 4), intArrayOf(1, 6, 7), intArrayOf(0, 2, 8))
println("Dimension on X: ${matrixA[0].size}")
println("Dimension on Y: ${matrixA.size}")
displayMatrix(matrixA)
println()
val transposeMatrix = Array(matrixA[0].size) { IntArray(matrixA.size) }
for(i in 0..matrixA.size - 1) {
for(j in 0..matrixA[0].size - 1) {
transposeMatrix[j][i] = matrixA[i][j]
}
}
displayMatrix(transposeMatrix)
}

Calculating the exponential of a square matrix

I'm trying to write a method that calculates the exponential of a square matrix. In this instance, the matrix is a square array of value:
[1 0]
[0 10]
and the method should return a value of:
[e 0]
[0 e^10]
However, when I run my code, I get a range of values depending on what bits I've rearranged, non particularly close to the expected value.
The way the method works is to utilise the power series for the matrix, so basically for a matrix A, n steps and an identity matrix I:
exp(A) = I + A + 1/2!*AA + 1/3!*AAA + ... +1/n!*AAA..
The code follows here. The method where I'm having the issue is the method exponential(Matrix A, int nSteps). The methods involved are enclosed, and the Matrix objects take the arguments (int m, int n) to create an array of size double[m][n].
public static Matrix multiply(Matrix m1, Matrix m2){
if(m1.getN()!=m2.getM()) return null;
Matrix res = new Matrix(m1.getM(), m2.getN());
for(int i = 0; i < m1.getM(); i++){
for(int j = 0; j < m2.getN(); j++){
res.getArray()[i][j] = 0;
for(int k = 0; k < m1.getN(); k++){
res.getArray()[i][j] = res.getArray()[i][j] + m1.getArray()[i][k]*m2.getArray()[k][j];
}
}
}
return res;
}
public static Matrix identityMatrix(int M){
Matrix id = new Matrix(M, M);
for(int i = 0; i < id.getM(); i++){
for(int j = 0; j < id.getN(); j++){
if(i==j) id.getArray()[i][j] = 1;
else id.getArray()[i][j] = 0;
}
}
return id;
}
public static Matrix addMatrix(Matrix m1, Matrix m2){
Matrix m3 = new Matrix(m1.getM(), m2.getN());
for(int i = 0; i < m3.getM(); i++){
for(int j = 0; j < m3.getN(); j++){
m3.getArray()[i][j] = m1.getArray()[i][j] + m2.getArray()[i][j];
}
}
return m3;
}
public static Matrix scaleMatrix(Matrix m, double scale){
Matrix res = new Matrix(m.getM(), m.getN());
for(int i = 0; i < res.getM(); i++){
for(int j = 0; j < res.getN(); j++){
res.getArray()[i][j] = m.getArray()[i][j]*scale;
}
}
return res;
}
public static Matrix exponential(Matrix A, int nSteps){
Matrix runtot = identityMatrix(A.getM());
Matrix sum = identityMatrix(A.getM());
double factorial = 1.0;
for(int i = 1; i <= nSteps; i++){
sum = Matrix.multiply(Matrix.scaleMatrix(sum, factorial), A);
runtot = Matrix.addMatrix(runtot, sum);
factorial /= (double)i;
}
return runtot;
}
So my question is, how should I modify my code, so that I can input a matrix and a number of timesteps to calculate the exponential of said matrix after said timesteps?
My way to go would be to keep two accumulators :
the sum, which is your approximation of exp(A)
the nth term of the series M_n, that is A^n/n!
Note that there is a nice recursive relationship with M_n: M_{n+1} = M_n * A / (n+1)
Which yields :
public static Matrix exponential(Matrix A, int nSteps){
Matrix seriesTerm = identityMatrix(A.getM());
Matrix sum = identityMatrix(A.getM());
for(int i = 1; i <= nSteps; i++){
seriesTerm = Matrix.scaleMatrix(Matrix.multiply(seriesTerm,A),1.0/i);
sum = Matrix.addMatrix(seriesTerm, sum);
}
return sum;
}
I totally understand the sort of thrill that implementing such algorithms can give you. But if this is not a hobby project, I concur that you should that you should use a library for this kind of stuff. Making such computations precise and efficient is really not a trivial matter, and a huge wheel to reinvent.

Matrix multiplication - single-dimension * multi-dimensional

I need to multiply two matrices. I understand pretty well how matrices work however in Java I am finding this a bit complex, so I researched a bit and found this.
public static int[][] multiply(int a[][], int b[][]) {
int aRows = a.length,
aColumns = a[0].length,
bRows = b.length,
bColumns = b[0].length;
int[][] resultant = new int[aRows][bColumns];
for(int i = 0; i < aRows; i++) { // aRow
for(int j = 0; j < bColumns; j++) { // bColumn
for(int k = 0; k < aColumns; k++) { // aColumn
resultant[i][j] += a[i][k] * b[k][j];
}
}
}
return resultant;
This code works fine. However the problem with this is that I need to multiply a single dimension matrix (1*5) by a multidimensional matrix (5*4), so the result will be (1*4) matrix and later on in the same program multiply a (1*4) matrix by a (4*3) matrix resulting in (1*3).
And I need to store the single dimension matrix in a normal array (double []) not multidimensional one!
I altered this code to the following but it still doesn't resolve the correct results.
public static double[] multiplyMatrices(double[] A, double[][] B) {
int xA = A.length;
int yB = B[0].length;
double[] C = new double[yB];
for (int i = 0; i < yB; i++) { // bColumn
for (int j = 0; j < xA; j++) { // aColumn
C[i] += A[j] * B[j][i];
}
}
return C;
Thanks in advance for any tips you may give :)
You can use RealMatrix to make it easier.
RealMatrix result = MatrixUtils.createRealMatrix(a).multiply(MatrixUtils.createRealMatrix(b));
double[] array = result.getRow(0);

Categories