private DoubleMatrix getDiffMatrix(int[] size, int k, int indices) { int dim = size.Length; DoubleMatrix d = getDiffMatrix(size[indices], k); int preProduct = 1; int postProduct = 1; for (int j = indices + 1; j < dim; j++) { preProduct *= size[j]; } for (int j = 0; j < indices; j++) { postProduct *= size[j]; } DoubleMatrix temp = d; if (preProduct != 1) { temp = (DoubleMatrix)_algebra.kroneckerProduct(DoubleMatrix.identity(preProduct), temp); } if (postProduct != 1) { temp = (DoubleMatrix)_algebra.kroneckerProduct(temp, DoubleMatrix.identity(postProduct)); } return(temp); }
/// <summary> /// Assume a tensor has been flattened to a vector as {A_{0,0}, A_{0,1},...._A_{0,m}, A_{1,0}, A_{1,1},...._A_{1,m},...,A_{n,0}, A_{n,1},...._A_{n,m}} /// (see <seealso cref="#flattenMatrix"/>) that is, the <b>last</b> index changes most rapidly. /// Given a matrix, M, that acts on the elements of one index only, i.e. /// $$y_{i, i_1, i_2, \dots,i_{k-1}, i_{k+1},\dots, i_n} = \sum_{i_k=0}^{N_k-1} M_{i,i_k} x_{i_1, i_2, \dots,i_k,\dots, i_n} $$ /// form the larger matrix that acts on the flattened vector. </summary> /// <param name="numElements"> The number of elements in each index. In the example above, this would be {n,m} </param> /// <param name="m"> the matrix M </param> /// <param name="index"> Which index does the matrix act on </param> /// <returns> A (larger) matrix which acts on the flattened vector </returns> public static DoubleMatrix getMatrixForFlattened(int[] numElements, DoubleMatrix m, int index) { ArgChecker.notEmpty(numElements, "numElements"); int dim = numElements.Length; ArgChecker.notNull(m, "m"); ArgChecker.isTrue(index >= 0 && index < dim, "indices outside range"); ArgChecker.isTrue(m.columnCount() == numElements[index], "columns in m ({}) do not match numElements for index ({})", m.columnCount(), numElements[index]); int postProduct = 1; int preProduct = 1; for (int j = index + 1; j < dim; j++) { preProduct *= numElements[j]; } for (int j = 0; j < index; j++) { postProduct *= numElements[j]; } DoubleMatrix temp = m; if (preProduct != 1) { temp = (DoubleMatrix)MA.kroneckerProduct(temp, DoubleMatrix.identity(preProduct)); } if (postProduct != 1) { temp = (DoubleMatrix)MA.kroneckerProduct(DoubleMatrix.identity(postProduct), temp); } return(temp); }