2

サイズMX Nの Aという行列があります。行列全体で各列に対して同じ関数を呼び出す必要があります。これまで、各列を抽出し、 N まで反復する列に対して関数を呼び出してきました。ie (列数)

それを行うためのより良い/より速い方法はありますか?

どんな助けでも感謝します。ありがとう

4

1 に答える 1

1

現在、並列コンピューティングを使用してパフォーマンスを向上させることができれば可能です。

CPUはマルチコア/マルチスレッド。

たとえば、Java 8 ストリームと並列計算を使用できます。

例えば

行列ベクトル乗算

@Test 
  2 public static void matrixVectorProduct() {
  3     System.out.println("Matrix Vector multiplication"); 
  4     final int DIM = 5;
  5          
  6     int [][]a = new int[DIM][DIM]; 
  7     int counter = 1;
  8     for (int i = 0; i < a.length; i++) {
  9         for (int j = 0; j < a[0].length; j++) {
 10             a[i][j] = counter++; 
 11         } 
 12     } 
 13          
 14     int []v = new int[DIM]; 
 15     Arrays.fill(v, 5);        
 16     int []c = new int[DIM]; 
 17          
 18     IntStream.range(0, c.length) 
 19              .parallel() 
 20              .forEach( (i) ->  { 
 21                  IntStream.range(0, a.length)
 22                           .sequential() 
 23                           .forEach( (j) -> { c[i] += v[j] * a[i][j]; }); 
 24                          }); 
 25   
 26          
 27     int []expected = new int[]{75, 200, 325, 450, 575};
 28     assertArrayEquals(expected, c); 
 29          
 30     System.out.println("Matrix-Vector product: " + Arrays.toString(c));         
 31 }
于 2015-06-28T19:40:30.633 に答える