(1) 内積と行列の積の計算
この内積計算の関数をまず作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double getInnerProduct(double a[], double b[]) { | |
double answer = 0; | |
for(int i = 0; i < a.length; i++) { | |
answer += a[i] * b[i]; | |
} | |
return answer; | |
} |
行列計算を実施する場合、この内積計算を呼び出します。ただし、Javaで内積を計算する時に、行ごとしか呼び出せず、列ごと呼び出すことができません。そのため転置行列作成をしておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double[][] getTranspose(double [][]a){ | |
double t[][] = new double[a[0].length][a.length]; | |
for(int i = 0; i < a[0].length; i++) { | |
for(int j = 0; j < a.length; j++) { | |
t[i][j] = a[j][i]; | |
} | |
} | |
return t; | |
} |
これを使って、行列の積を計算します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double[][] getProduct(double a[][], double b[][]) { | |
double answer[][] = new double[a.length][b[0].length]; | |
double t[][] = this.getTranspose(b); | |
for(int i = 0; i < a.length; i++) { | |
for(int j = 0; j < b[0].length; j++) { | |
answer[i][j] = this.getInnerProduct(a[i], t[j]); | |
} | |
} | |
return answer; | |
} |
0 件のコメント:
コメントを投稿