java - 计算矩阵指数的乘法和

标签 java algorithm matrix matrix-multiplication exponential

我遇到了以下问题:

对于给定的指数 N、2x2 矩阵 A 和极限 L,递归计算矩阵 S:

S = I + A + A^2 + A^3 + ... + A^N

其中 I 是单位矩阵。

如果矩阵S中任意一个元素大于等于L,则自减L直到 低于L。

我的算法如下:

// Pre-condition: 
// Parameters:
// An integer indicating an exponent
// A 2d 2x2 integer array must exist as an instance attribute
// Post-condition: The matrix resulting from the sum of multiplied matrices 
// i.e. A^2 + A^1 + I
public int[][] computeMatrixSum(int exp)
{
    if(exp == 0)
    {
        return new int[][]
        {
            new int[]{ 1,0 },
            new int[]{ 0,1 }
        };
    }
    else
    {
        int[][] matrixB = new int[matrix.length][matrix[0].length];
        int[][] matrixC = new int[matrix.length][matrix[0].length];

        matrixB = matrix;

        for(int expC = exp; expC > 1; expC--)
        {
            // Multiply the matrix
            for(int i = 0; i < matrix.length; i++)
            {
                for(int k = 0; k < matrixB[0].length; k++)
                {
                    for(int j = 0; j < matrix[0].length; j++)
                    {
                        matrixC[i][k] += matrix[i][j] * matrixB[j][k];
                    }
                }
            }
            matrixB = matrixC;
            matrixC = new int[matrix.length][matrix[0].length];
        }

        // Recursively calculate the sum of the other matrix products
        int[][] tmpSum = computeMatrixSum(exp-1);

        int[][] matrixSum = new int[matrixB.length][matrixB[0].length];

        for(int row = 0; row < matrixB.length; row++)
        {
            for(int col = 0; col < matrixB[0].length; col++)
            {
                matrixSum[row][col] = matrixB[row][col] + tmpSum[row][col];
            }
        }

        return matrixSum;
    }
}

// Pre-condition:
// Parameters: 
// An integer indicating the exponent to apply on the matrix
// An integer indicating the limit of the elements of the 2d matrix sum
// An 2d 2x2 integer array must exist as an instance attribute
// Post-condition: The matrix resulting from the sum of multiplied matrices
// that has elements that are not greater than the given limit
// i.e. A^2 + A^1 + I
public int[][] solve(int exp,int limit)
{
        int[][] matrixSum = computeMatrixSum(exp);

        for(int row = 0; row < matrixSum.length; row++)
        {
            for(int col = 0; col < matrixSum.length; col++)
            {
                while(matrixSum[row][col] >= limit)
                    matrixSum[row][col] -= limit;
            }
        }

        return matrixSum;
}

我的算法有效。但对于较大的 N 值来说它太慢了。这是因为我在将它们相乘时不断重新计算所有指数的结果。

我不知道有任何其他算法可以更有效地解决这个问题。

有人可以给点建议吗?

谢谢。

最佳答案

您可以申请 Horner's method如示例所示,将“单项式形式转换为计算效率高的形式”herehere对于多项式。您可以利用 JScienceMatrix以简化编码。

关于java - 计算矩阵指数的乘法和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648954/

相关文章:

java - Kryo 出现 NoClassDefFoundError

Java错误: Bad Operand Type for binary operator <

python - 如何找到 3 个大集合的交集的 100 个元素的列表?

algorithm - 我可以将 Dijkstra 算法用于负加权图吗?

python - 没有内存分配的 numpy tile

java - 是否有用于简单列表映射的 Java 8 实用程序?

Java:空循环

algorithm - SPOJ 问题 Flibonakki time limit exceed

arrays - 在matlab中计算3D点对的欧几里得距离

java - 整个矩阵的 block 列表 - java