Ich habe den folgenden Code. Ich möchte eine Matrix angeben und sie zu einer bestimmten Macht multiplizieren.Multiplizieren einer Matrix mit der gleichen Leistung mit Java
import java.util.Scanner;
class Test
{
public static void main(String args[])
{
int m, n, c, d, k, Ind, x;
double sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the power by which you want the Matrix to be multiplyed by: ");
Ind = in.nextInt();
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();
double Mtr[][] = new double[m][n];
double multiply[][] = new double[m][n];
System.out.println("Enter the elements of first matrix");
for (c = 0 ; c < m ; c++)
for (d = 0 ; d < n ; d++)
Mtr[c][d] = in.nextInt();
if (Ind ==1)
for (c = 0 ; c < m ; c++)
{
for (d = 0 ; d < n ; d++)
System.out.print(Mtr[c][d]+"\t");
System.out.print("\n");
}
else
{
for (c = 0 ; c < m ; c++)
{
for (x=0; x < Ind; x++)
{
for (d = 0 ; d < n ; d++)
{
for (k = 0 ; k < m ; k++)
{
sum = sum + Mtr[c][k]*Mtr[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
}
System.out.println("Product of entered matrices:-");
for (c = 0 ; c < m ; c++)
{
for (d = 0 ; d < n ; d++)
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
}
Das Problem, das ich habe, ist, ich bin nicht sicher, wo die Schleife anzuwenden, um die Matrix durch die Kraft zu vervielfachen, die ich eingegeben habe. Ich glaube, ich könnte müssen auch die Logik so ändern, dass
sum = sum + Mtr[c][k]*multiply[k][d]
welche Ausgabe werden Sie erwarten und was erhalten Sie? – Rockstar
Wenn ich die 2 x 2-Matrix eingeben {1 2} {3 4}. Ich bekomme das Ergebnis {7 10} {15 22}. Egal, welche Kraft ich betrete. Ich erwarte für eine Potenz von drei das Ergebnis {37 54} {81 128}. – Dinks123
http://stackoverflow.com/questions/22900872/raising-a-matrix-to-the-power-method-java Ich hoffe, das hilft. – eldo