-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathVector.java
72 lines (68 loc) · 1.69 KB
/
Vector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Arrays;
public class Vector {
int[] d;
public Vector(int... d) {
this.d = d;
}
public static Vector valueOf(int[] d) { // for implicit type conversion
return new Vector(d);
}
public Vector add(Vector a) {
assert d.length == a.d.length;
int[] n = d.clone();
for (int i = 0; i < a.d.length; i++)
n[i] += a.d[i];
return new Vector(n);
}
public Vector multiply(int a) {
int[] n = d.clone();
for (int i = 0; i < d.length; i++)
n[i] *= a;
return new Vector(n);
}
public Vector multiplyRev(int a) {
return multiply(a);
}
public int get(int i) {
return d[i];
}
public String toString() {
return Arrays.toString(d);
}
public boolean equals(Object obj) {
if (obj==null || obj.getClass()!=getClass()) return false;
return Arrays.equals(d, ((Vector)obj).d);
}
public Vector multiply(Matrix A) {
assert d.length == A.n();
int res[] = new int[A.m()];
for (int j = 0; j < A.m(); j++)
for (int i = 0; i < A.n(); i++)
res[j] += d[i] * A[i][j];
return new Vector(res);
}
public static class Matrix {
int d[][];
public Matrix(int d[][]) {
for (int[] a : d)
assert a.length == d[0].length;
this.d = d;
}
public int n() { return d.length; }
public int m() { return d[0].length; }
public Vector get(int i) { return new Vector(d[i]); }
public String toString() { return Arrays.deepToString(d); }
}
public static boolean test() {
Vector a = new Vector(1,2),
b = new int[]{2,3},
c = a + 2*b;
if (!"[5, 8]".equals(c.toString()))
return false;
Matrix A = new Matrix(new int[][] {{1,2,3}, {4,5,6}});
return (a*A).equals(new Vector(9,12,15));
}
public static void main(String[] args) {
System.out.println(test());
}
}