将矩阵提升到幂方法JAVA [英] Raising a matrix to the power method JAVA

查看:98
本文介绍了将矩阵提升到幂方法JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难创建一种方法来将矩阵提升到幂。我尝试使用这个

I am having a really hard time creating a method to raise a matrix to the power. I tried using this

public static int powerMethod(int matrix, int power) {
    int temp = matrix ; 

    for (int i = power; i == 1; i--)
        temp = temp * matrix ; 



    return temp ;

但是返还WAYYY。只有第一个(1,1)矩阵元素在点上。

but the return is WAYYY off. Only the first (1,1) matrix element is on point.

我尝试在主体中使用该方法,因此

I tried using that method in a main like so

// Multiplying matrices
            for (i = 0; i < row; i++)
            {
                for (j = 0; j < column; j++) 
                {
                    for (l = 0; l < row; l++)
                    {
                        sum += matrix[i][l] * matrix[l][j] ;
                    }
                    matrix[i][j] = sum ;
                    sum = 0 ;
                }
                }

    // Solving Power of matrix
            for (i = 0; i < row; i++) {
                for (j = 0; j < column; j++) 
            matrixFinal[power][i][j] = Tools.powerMethod(matrix[i][j], power) ;
            }

其中power,row和column是int用户输入。

Where "power", "row", and "column" is an int that the user enters.

我有什么想法可以做到这一点?

Any ideas how I can do this??

谢谢!!!

推荐答案

这里有很多问题。

首先,你的矩阵平方算法有(常见)错误。你有:

First, your matrix squaring algorithm has a (common) error. You have:

for (i = 0; i < row; i++) {
    for (j = 0; j < column; j++) {
        for (l = 0; l < row; l++) {
            sum += matrix[i][l] * matrix[l][j] ;
        }
        matrix[i][j] = sum ;
        sum = 0 ;
    }
}

但是,您需要将结果存储在临时中第二个矩阵,因为当你执行 matrix [i] [j] = sum 时,它会用输出替换该位置的值,然后结果会变得不正确。此外,我建议将 sum 初始化为0 first ,因为它似乎是在此循环之外声明它,并且首先初始化它可以保护您免受任意值的影响在进入循环之前, sum 可能有。此外,行和并不是很清楚你的意思 - 确保你在整个过程中进行迭代矩阵。例如:

However, you need to store the result in a temporary second matrix, because when you do matrix[i][j] = sum, it replaces the value at that position with the output, then later results end up being incorrect. Also I suggest initializing sum to 0 first, since it appears you declare it outside of this loop, and initializing it first protects you against any arbitrary value sum may have before going into the loop. Furthermore, it is not immediately clear what you mean by row and column -- make sure you are iterating over the entire matrix. E.g.:

int temp[][] = new int[matrix.length];

for (i = 0; i < matrix.length; i++) {
    temp[i] = new int[matrix[i].length];
    for (j = 0; j < matrix[i].length; j++) {
        sum = 0 ;
        for (l = 0; l < matrix.length; l++) {
            sum += matrix[i][l] * matrix[l][j] ;
        }
        temp[i][j] = sum ;
    }
}

// the result is now in 'temp', you could do this if you wanted:
matrix = temp;

请注意 matrix.length matrix [i] .length 如果矩阵是正方形(它必须是,为了与自身相乘),在上面是可以互换的。

Note that matrix.length and matrix[i].length are fairly interchangeable above if the matrix is square (which it must be, in order to be multiplied by itself).

其次,你的乘法正方形一个矩阵。这意味着如果你反复应用它,你每次都要保持矩阵的平方,这意味着你只能计算本身就是2的幂的幂。

Secondly, your multiplication squares a matrix. This means if you repeatedly apply it, you keep squaring the matrix every time, which means you will only be able to compute powers that are themselves powers of two.

你的第三个问题是你的最后一点没有多大意义:

Your third issue is your final bit doesn't make much sense:

for (i = 0; i < row; i++) {
    for (j = 0; j < column; j++) 
        matrixFinal[power][i][j] = Tools.powerMethod(matrix[i][j], power) ;
}

目前还不清楚你要在这里做什么。最后一部分似乎是试图将个别元素提升到一定的力量。但这与将矩阵提升为幂是不同的。

It's not immediately clear what you are trying to do here. The final part seems to be trying to raise individual elements to a certain power. But this is not the same as raising a matrix to a power.

需要做的是定义一个合适的矩阵乘法方法,它可以乘以两个任意矩阵,例如:

What you need to do is define a proper matrix multiplication method that can multiply two arbitrary matrices, e.g.:

int[][] multiplyMatrices (int[][] a, int[][] b) {
    // compute and return a x b, similar to your existing multiplication
    // algorithm, and of course taking into account the comments about
    // the 'temp' output matrix above
}

然后计算功率变得简单:

Then computing a power becomes straightforward:

int[][] powerMatrix (int[][] a, int p) {
    int[][] result = a;
    for (int n = 1; n < p; ++ n)
        result = multiplyMatrices(result, a);
    return result;
}

这篇关于将矩阵提升到幂方法JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆