使用 2D ArrayLists 在 Java 中进行矩阵乘法
Matrix multiplication in Java using 2D ArrayLists
我正在尝试在 Java 中实现一个简单的矩阵乘法,我将矩阵存储在二维 ArrayList 中。看起来错误是由嵌套for循环内矩阵Result
的设置引起的,但我不明白为什么。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class Test {
public static void main(String args[]) {
int n = 2;
ArrayList<ArrayList<Integer>> Result =
new ArrayList<ArrayList<Integer>>(
Collections.nCopies(n, new ArrayList<Integer>(
Collections.nCopies(n, 0))));
ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
A.add(new ArrayList<Integer>(Arrays.asList(1, 6)));
A.add(new ArrayList<Integer>(Arrays.asList(2, 2)));
B.add(new ArrayList<Integer>(Arrays.asList(0, 9)));
B.add(new ArrayList<Integer>(Arrays.asList(5, 6)));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int val = A.get(i).get(k) * B.get(k).get(j);
Result.get(i).set(j, Result.get(i).get(j) + val);
}
}
}
}
}
代码产生结果:
A * B = [[40, 75], [40, 75]]
实际上应该是:
A * B = [[30, 45], [10, 30]]
Result
初始化不正确。
ArrayList<ArrayList<Integer>> Result = new ArrayList<ArrayList<Integer>>(
Collections.nCopies(n,
new ArrayList<Integer>(Collections.nCopies(n, 0))));
这将创建一个矩阵,其中两行(或两列,取决于您如何看待)实际上是同一行(列),被引用两次。
你可以这样初始化 Result
:
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < n; i++) {
result.add(new ArrayList<>(Collections.nCopies(n, 0)));
}
可以用arrays代替lists,因为三个矩阵乘法前后usually 有 恒定 大小。但是对于列表,您可以使用 mutable AtomicInteger
instead of an immutable Integer
。您的代码可能如下所示:
int n = 2;
// matrices
List<List<AtomicInteger>> a = List.of(
List.of(new AtomicInteger(1), new AtomicInteger(6)),
List.of(new AtomicInteger(2), new AtomicInteger(2)));
List<List<AtomicInteger>> b = List.of(
List.of(new AtomicInteger(0), new AtomicInteger(9)),
List.of(new AtomicInteger(5), new AtomicInteger(6)));
// resulting matrix
List<List<AtomicInteger>> c = List.of(
List.of(new AtomicInteger(0), new AtomicInteger(0)),
List.of(new AtomicInteger(0), new AtomicInteger(0)));
// matrix multiplication
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int val = a.get(i).get(k).get() * b.get(k).get(j).get();
c.get(i).get(j).getAndAdd(val);
}
}
}
// output
System.out.println(a); // [[1, 6], [2, 2]]
System.out.println(b); // [[0, 9], [5, 6]]
System.out.println(c); // [[30, 45], [10, 30]]
另请参阅:Floating point matrix multiplication
使用二维列表的方矩阵乘法:
int n = 2;
// three square matrices n×n
List<List<Integer>>
a = Arrays.asList(Arrays.asList(1, 6), Arrays.asList(2, 2)),
b = Arrays.asList(Arrays.asList(0, 9), Arrays.asList(5, 6)),
// resulting matrix
c = Arrays.asList(Arrays.asList(0, 0), Arrays.asList(0, 0));
// rows of the 'a' matrix
for (int i = 0; i < n; i++) {
// columns of the 'b' matrix
for (int j = 0; j < n; j++) {
// columns of the 'a' matrix,
// aka rows of the 'b' matrix
for (int k = 0; k < n; k++) {
// previous value
int val = c.get(i).get(j);
// sum of the products of the i-th row
// of 'a' and the j-th column of 'b'
val += a.get(i).get(k) * b.get(k).get(j);
// set new value
c.get(i).set(j, val);
}
}
}
// output
System.out.println(a); // [[1, 6], [2, 2]]
System.out.println(b); // [[0, 9], [5, 6]]
System.out.println(c); // [[30, 45], [10, 30]]
另请参阅:Algorithm of the matrix multiplication
我正在尝试在 Java 中实现一个简单的矩阵乘法,我将矩阵存储在二维 ArrayList 中。看起来错误是由嵌套for循环内矩阵Result
的设置引起的,但我不明白为什么。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class Test {
public static void main(String args[]) {
int n = 2;
ArrayList<ArrayList<Integer>> Result =
new ArrayList<ArrayList<Integer>>(
Collections.nCopies(n, new ArrayList<Integer>(
Collections.nCopies(n, 0))));
ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
A.add(new ArrayList<Integer>(Arrays.asList(1, 6)));
A.add(new ArrayList<Integer>(Arrays.asList(2, 2)));
B.add(new ArrayList<Integer>(Arrays.asList(0, 9)));
B.add(new ArrayList<Integer>(Arrays.asList(5, 6)));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int val = A.get(i).get(k) * B.get(k).get(j);
Result.get(i).set(j, Result.get(i).get(j) + val);
}
}
}
}
}
代码产生结果:
A * B = [[40, 75], [40, 75]]
实际上应该是:
A * B = [[30, 45], [10, 30]]
Result
初始化不正确。
ArrayList<ArrayList<Integer>> Result = new ArrayList<ArrayList<Integer>>(
Collections.nCopies(n,
new ArrayList<Integer>(Collections.nCopies(n, 0))));
这将创建一个矩阵,其中两行(或两列,取决于您如何看待)实际上是同一行(列),被引用两次。
你可以这样初始化 Result
:
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < n; i++) {
result.add(new ArrayList<>(Collections.nCopies(n, 0)));
}
可以用arrays代替lists,因为三个矩阵乘法前后usually 有 恒定 大小。但是对于列表,您可以使用 mutable AtomicInteger
instead of an immutable Integer
。您的代码可能如下所示:
int n = 2;
// matrices
List<List<AtomicInteger>> a = List.of(
List.of(new AtomicInteger(1), new AtomicInteger(6)),
List.of(new AtomicInteger(2), new AtomicInteger(2)));
List<List<AtomicInteger>> b = List.of(
List.of(new AtomicInteger(0), new AtomicInteger(9)),
List.of(new AtomicInteger(5), new AtomicInteger(6)));
// resulting matrix
List<List<AtomicInteger>> c = List.of(
List.of(new AtomicInteger(0), new AtomicInteger(0)),
List.of(new AtomicInteger(0), new AtomicInteger(0)));
// matrix multiplication
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int val = a.get(i).get(k).get() * b.get(k).get(j).get();
c.get(i).get(j).getAndAdd(val);
}
}
}
// output
System.out.println(a); // [[1, 6], [2, 2]]
System.out.println(b); // [[0, 9], [5, 6]]
System.out.println(c); // [[30, 45], [10, 30]]
另请参阅:Floating point matrix multiplication
使用二维列表的方矩阵乘法:
int n = 2;
// three square matrices n×n
List<List<Integer>>
a = Arrays.asList(Arrays.asList(1, 6), Arrays.asList(2, 2)),
b = Arrays.asList(Arrays.asList(0, 9), Arrays.asList(5, 6)),
// resulting matrix
c = Arrays.asList(Arrays.asList(0, 0), Arrays.asList(0, 0));
// rows of the 'a' matrix
for (int i = 0; i < n; i++) {
// columns of the 'b' matrix
for (int j = 0; j < n; j++) {
// columns of the 'a' matrix,
// aka rows of the 'b' matrix
for (int k = 0; k < n; k++) {
// previous value
int val = c.get(i).get(j);
// sum of the products of the i-th row
// of 'a' and the j-th column of 'b'
val += a.get(i).get(k) * b.get(k).get(j);
// set new value
c.get(i).set(j, val);
}
}
}
// output
System.out.println(a); // [[1, 6], [2, 2]]
System.out.println(b); // [[0, 9], [5, 6]]
System.out.println(c); // [[30, 45], [10, 30]]
另请参阅:Algorithm of the matrix multiplication