2016年3月19日土曜日

N元連立1次方程式の解法プログラム(マルチスレッド版)

前、Fock-Joinフレームワークを使ったフィボナッチ数列を書いたとき、
連立方程式に応用したらもっとはやく計算できるんじゃね?と思ったので、作ってみた
連立方程式は前回同様クラメルの公式。今更だけど、連立方程式は掃き出し法が最強だと思う。。

前回との演算の違い

前の行列式を求めるロジックはこんな感じ
※総和を返すと書いているが、厳密には総和ではなく (-1)j * a0j の係数が付きます

これを単一スレッドで処理すればそりゃ遅くなる。
そこで今回は上図の水色部分をすべてスレッドにして平行処理させようと思う


プログラム

まずはマルチタスクにする行列式を求めるタスククラスから
継承元はRecursiveTask。型パラメータは戻値になるので行列式を返すDoubleで
メソッドはこの3つ
メソッド名効果
コンストラクタ行列式計算用の行列を設定
compute()並行処理内容。めっちゃ行列式の演算スレッド生成
演算ちょっと楽にするために3元まではサラス法や
余因子展開の行列を作るメソッド余因子行列をつくる
注目はcompute()のdefaultに入ったとき。
このときに余因子の数だけ並行処理して余因子の行列式を求める
  1. import java.util.concurrent.RecursiveTask;
  2.  
  3. /** 行列式を求めるタスククラス. */
  4. class Determinant extends RecursiveTask<Double> {
  5. /** 行列式を求める対象の行列 */
  6. final double[][] matrix;
  7. /** 行列式のコンストラクタ
  8. * @param matrix 行列
  9. */
  10. Determinant(double[][] matrix) {
  11. this.matrix = matrix;
  12. }
  13. /**
  14. * 並列処理
  15. * 3×3行列まではサラス法
  16. * それ以降は余因子展開から求めていく
  17. */
  18. @Override
  19. protected Double compute() {
  20. double ret = 0;
  21. switch (this.matrix.length) {
  22. case 1:
  23. ret = matrix[0][0];
  24. break;
  25. case 2:
  26. ret = matrix[0][0] * matrix[1][1]
  27. - matrix[1][0] * matrix[0][1];
  28. break;
  29. case 3:
  30. ret = matrix[0][0] * matrix[1][1] * matrix[2][2]
  31. + matrix[1][0] * matrix[2][1] * matrix[0][2]
  32. + matrix[2][0] * matrix[0][1] * matrix[1][2]
  33. - matrix[0][2] * matrix[1][1] * matrix[2][0]
  34. - matrix[1][0] * matrix[0][1] * matrix[2][2]
  35. - matrix[2][1] * matrix[1][2] * matrix[0][0];
  36. break;
  37. default:
  38. Determinant[] dets = new Determinant[matrix.length];
  39. for (int j = 0; j < matrix.length; j++) {
  40. dets[j] = new Determinant(getLaplaceMarix(this.matrix, 0, j));
  41. if(j == 0) {
  42. int sign = ((j % 2) == 0) ? 1 : -1;
  43. ret += sign * matrix[j][0] * dets[j].compute();
  44. } else {
  45. dets[j].fork();
  46. }
  47. }
  48. for (int j = 1; j < matrix.length; j++) {
  49. int sign = ((j % 2) == 0) ? 1 : -1;
  50. ret += sign * matrix[j][0] * dets[j].join();
  51. }
  52. break;
  53. }
  54. return ret;
  55. }
  56. /**
  57. * 余因子行列を生成
  58. * @param matrix 元の行列
  59. * @param line 縦
  60. * @param col 横
  61. * @return 余因子行列
  62. */
  63. static double[][] getLaplaceMarix(double[][] matrix, int line, int col) {
  64. int len = matrix.length;
  65. // 余因子展開で使う行列
  66. double laplace_marix[][] = new double[len - 1][len - 1];
  67. for(int i = 0, y = 0; i < len; i++) {
  68. if(i == col) continue;
  69. for(int j = 0, x = 0; j < len; j++){
  70. if(j == line)continue;
  71. laplace_marix[y][x] = matrix[i][j];
  72. x++;
  73. }
  74. y++;
  75. }
  76. return laplace_marix;
  77. }
  78. }
で、この行列式演算タスククラスを使って、連立方程式を組み立てるのはメイン文
必要な行列式のタスク数はN+1個(分母+解の個数分)なので、Fork-Joinのプールにそれぞれをサブミットで並行処理させてく
サブミットの戻値であるFutureを保持し、演算終了後に結果を取得し演算を返す
フィボナッチ数列を求めるときに使ってたインボークだと、1つの行列式を求めるのには問題ないが、今回はN+1つの行列式すべてを平行に動かすので、サブミットを使用
  1. import java.util.concurrent.ForkJoinPool;
  2. import java.util.concurrent.ForkJoinTask;
  3. import java.util.concurrent.ExecutionException;
  4.  
  5. public class Main {
  6. // メイン処理
  7. public static void main(String[] args) {
  8. // 連立方程式を行列化
  9. final double matrix[][] = {
  10. // x1 x2 x3 x4 b ※bは移行してるわけではないので、右辺のbを書けばOK
  11. { 2.0, 1.0, 3.0, 4.0, 2.0 }, // 式1: 2x + y + 3z + 4w = 2
  12. { 3.0, 2.0, 5.0, 2.0, 12.0 }, // 式2: 3x + 2y + 5z + 2w = 12
  13. { 3.0, 4.0, 1.0, -1.0, 4.0 }, // 式3: 3x + 4y + z - 1w = 4
  14. { -1.0, -3.0, 1.0, 3.0, -1.0 } // 式4:-1x - 3y + z + 3w = -1
  15. };
  16. // 演算の準備
  17. final ForkJoinPool pool = new ForkJoinPool();
  18. final java.util.List<ForkJoinTask<Double>> ret = new java.util.ArrayList<>();
  19. // 行列式の演算開始( 分母 -> 分子の末尾から降順 )
  20. for (int i = matrix.length; i >= 0; i--) {
  21. double[][] calc_matrix = changeColumn(matrix, i, matrix.length);
  22. Determinant task = new Determinant(calc_matrix);
  23. ret.add(pool.submit(task));
  24. }
  25. // 結果の出力
  26. try {
  27. // とりあえず共通の分母を先に取得
  28. double denominator = ret.remove(0).get();
  29. // 分子を昇順で取得し、解を計算・表示させていく
  30. java.util.Collections.reverse(ret);
  31. for (ForkJoinTask<Double> task : ret) {
  32. double result = task.get() / denominator;
  33. System.out.println("result = " + result);
  34. }
  35. } catch (InterruptedException | ExecutionException e) {
  36. e.printStackTrace();
  37. }
  38. // プールは最後に終了させる
  39. pool.shutdown();
  40. }
  41. // 行列の列(col1とcol2)を入れ替えた新しい行列を生成するメソッド
  42. static double[][] changeColumn(double[][] matrix, int col1, int col2) {
  43. // 列の入れ替え用に配列をコピーする
  44. double[][] temp_matrix = new double[matrix.length][];
  45. int j = 0;
  46. for (double[] m : matrix) temp_matrix[j++] = m.clone();
  47. // 列の入れ替え
  48. int n = matrix.length;
  49. for (int i = 0; i < n; i++) {
  50. double temp = temp_matrix[i][col1];
  51. temp_matrix[i][col1] = temp_matrix[i][col2];
  52. temp_matrix[i][col2] = temp;
  53. }
  54. return temp_matrix;
  55. }
  56. }


演算処理結果

どんぐらいはやくなったのか実験
2~13元の1次連立方程式を用意し、それぞれの計算時間を計測した
使用した計算式はこんな感じ
結果
n元
方程式
シングル
スレッド[sec]
マルチ
スレッド[sec]
20.000 0.015
30.000 0.000
40.000 0.000
50.000 0.000
60.000 0.015
70.000 0.015
80.016 0.016
90.172 0.079
101.844 0.919
11 21.81714.468
12284.669139.296
134110.5932399.709
グラフ化すると、
図:マルチ・シングル処理における連立方程式解速度


10元くらいまではそんなに変わらないけど、11から大体半分くらいの速度になるね
15元くらいになると、半日かけても処理が終わらなかった。やはり、高い元を有する連立方程式はやはり掃き出し法が最強か。。

0 件のコメント:

コメントを投稿