Here you can find the source of matrixProductVDVT(final double[][] V, final double[] d, final double[][] A, int n)
Parameter | Description |
---|---|
V | a quadratic <tt>n</tt>-by-<tt>n</tt> matrix |
d | the <tt>n</tt> diagonal entries of a (diagonal) matrix |
A | on return, contains <tt>V*D*V'</tt> which is symmetric and, if all <tt>d[i] > 0</tt>, positive definite |
n | size of the arrays to be multiplied |
public static void matrixProductVDVT(final double[][] V, final double[] d, final double[][] A, int n)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w ww.ja v a 2 s . c om * Multiplies the vector <tt>v</tt> (n) with diagonal matrix <tt>D</tt> (n-by-n) and then with * <tt>v'</tt> (transposed). The result is stored in the symmetric matrix <tt>A</tt> (n-by-n), * that is * * <pre> * A = v * D * v' * </pre> * * If all diagonal entries of <tt>D</tt> are greater than zero, the * resulting matrix is also positive definite. * <p> * Note that this method does no safety checks (null or length). * * @param V * a quadratic <tt>n</tt>-by-<tt>n</tt> matrix * @param d * the <tt>n</tt> diagonal entries of a (diagonal) matrix * @param A * on return, contains <tt>V*D*V'</tt> which is symmetric and, if all * <tt>d[i] > 0</tt>, positive definite * @param n * size of the arrays to be multiplied */ public static void matrixProductVDVT(final double[][] V, final double[] d, final double[][] A, int n) { assert hasShape(V, n, n); assert d != null; assert d.length >= n; assert hasShape(A, n, n); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // V D V^T A[i][j] = V[i][0] * d[0] * V[j][0]; for (int k = 1; k < n; k++) { A[i][j] += V[i][k] * d[k] * V[j][k]; } // A is symmetric A[j][i] = A[i][j]; } } } /** * Multiplies the matrix <tt>V</tt> (l1-by-l2) with diagonal matrix * <tt>D</tt> (l2-by-l2) and then with <tt>V'</tt> (transposed). The result is * stored in the symmetric matrix <tt>A</tt> (l1-by-l1), that is * * <pre> * A = V * D * V' * </pre> * * If all diagonal entries of <tt>D</tt> are greater than zero, the * resulting matrix is also positive definite. * <p> * Note that this method does no safety checks (null or length). * * @param v * a <tt>l1</tt>-by-<tt>l2</tt> matrix * @param d * the <tt>l2</tt> diagonal entries of a (diagonal) matrix * @param a * on return, contains <tt>V*D*V'</tt> which is symmetric and, if all * <tt>d[i] > 0</tt>, positive definite * @param l1 * number of rows of <tt>V</tt> * @param l2 * number of columns of <tt>V</tt> and number of rows and columns of <tt>D</tt> */ public static void matrixProductVDVT(final double[][] V, final double[] d, final double[][] A, int l1, int l2) { assert hasShape(V, l1, l2); assert d != null; assert d.length >= l2; assert hasShape(A, l1, l1); for (int i = 0; i < l1; i++) { for (int j = i; j < l1; j++) { // V D V^T A[i][j] = V[i][0] * d[0] * V[j][0]; for (int k = 1; k < l2; k++) { A[i][j] += V[i][k] * d[k] * V[j][k]; } // A is symmetric A[j][i] = A[i][j]; } } } /** * Multiplies the matrix <tt>V</tt> (l1-by-l2) with diagonal matrix * <tt>D</tt> (l2-by-l2) and then with the transposed of matrix <tt>W</tt> (l3-by-l2). * The result is stored in the symmetric matrix <tt>A</tt> (l1-by-l3), that is * * <pre> * A = V * D * W' * </pre> * * <p> * Note that this method does no safety checks (null or length). * * @param V * a <tt>l1</tt>-by-<tt>l2</tt> matrix * @param d * the <tt>l2</tt> diagonal entries of a (diagonal) matrix * @param W * a <tt>l3</tt>-by-<tt>l2</tt> matrix * @param A * on return, contains the <tt>l1</tt>-by-<tt>l3</tt> matrix <tt>V*D*W'</tt> * @param l1 * number of rows of <tt>V</tt> * @param l2 * number of columns of <tt>V</tt> and number of rows and columns of <tt>D</tt> */ public static void matrixProductVDVT(final double[][] V, final double[] d, final double[][] W, final double[][] A, int l1, int l2, int l3) { assert hasShape(V, l1, l2); assert d != null; assert d.length >= l2; assert hasShape(W, l3, l2); assert hasShape(A, l1, l3); for (int i = 0; i < l1; i++) { for (int j = 0; j < l3; j++) { // V D W^T A[i][j] = V[i][0] * d[0] * V[j][0]; for (int k = 1; k < l2; k++) { A[i][j] += V[i][k] * d[k] * V[j][k]; } } } } /** * Returns whether matrix mat has size l1-by-l2 or not * @param mat a matrix * @param l1 first dimension (lines) * @param l2 second dimension (columns) * @return */ public static boolean hasShape(final double[][] mat, int l1, int l2) { assert mat != null; assert l1 > 0; assert l2 > 0; if (mat.length != l1) { return false; } for (int i = 0; i < mat.length; i++) { if (mat[i].length != l2) { return false; } } return true; } public static boolean hasShape(final double[][][] mat3d, int l1, int l2, int l3) { assert mat3d != null; assert l1 > 0; assert l2 > 0; assert l3 > 0; if (mat3d.length != l1) { return false; } for (int i = 0; i < mat3d.length; i++) { if (mat3d[i].length != l2) { return false; } for (int j = 0; j < mat3d[i].length; j++) { if (mat3d[i][j].length != l3) { return false; } } } return true; } }