Java Matrix Transpose matrixTrans_x_diagonalMatrix_x_Matrix(double[][] mat, double[] diag)

Here you can find the source of matrixTrans_x_diagonalMatrix_x_Matrix(double[][] mat, double[] diag)

Description

First transposes a matrix, then multiplies it with another matrix that only contains elements on its diagonal (0 elswhere), then multiplies it again with the matrix.

License

Open Source License

Parameter

Parameter Description
mat The matrix (m x n).
diag The matrix that only contains elements on its diagonal, as a one-dimensional vector of size n x 1 that represents the matrix of size n x n.

Return

The resulting matrix of size n x n.

Declaration

public static double[][] matrixTrans_x_diagonalMatrix_x_Matrix(double[][] mat, double[] diag) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   ww w.j  a  va2  s .com*/
     * First transposes a matrix, then multiplies it with another matrix that
     * only contains elements on its diagonal (0 elswhere), then multiplies it
     * again with the matrix.
     * @param mat The matrix (m x n).
     * @param diag The matrix that only contains elements on its diagonal,
     * as a one-dimensional
     * vector of size n x 1 that represents the matrix of size n x n.
     * @return The resulting matrix of size n x n.
     */
    public static double[][] matrixTrans_x_diagonalMatrix_x_Matrix(double[][] mat, double[] diag) {
        final int m = mat.length;
        final int n = mat[0].length;

        // result is a symetrical square matrix
        double[][] res = new double[n][n];

        // row[] will hold intermediate results
        double[] row = new double[m];

        for (int r = 0; r < n; r++) {
            // compute row r of ATP
            for (int col = 0; col < m; col++)
                row[col] = mat[col][r] * diag[col];

            // multiply row r of ATP with the columns of A
            for (int j = r; j < n; j++) {
                for (int i = 0; i < m; i++)
                    res[r][j] += row[i] * mat[i][j];

                // mirror result on diagonal axis
                res[j][r] = res[r][j];
            }
        }
        return res;
    }
}

Related

  1. matrixTrans_x_diagonalMatrix(double[][] mat, double[] diag)
  2. matrixTrans_x_matrix(double[][] mat)
  3. matrixTransform(Object[][] datas)
  4. matrixTranspose(Object[][] matrix)
  5. transpose(boolean[][] inputMatrix)