-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathVector.java
More file actions
88 lines (76 loc) · 2.2 KB
/
MathVector.java
File metadata and controls
88 lines (76 loc) · 2.2 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Created by niklas on 23.12.16.
*/
public class MathVector {
private double[] x;
public MathVector(double... vector) {
x = vector;
}
public static MathVector nullvector(int dimension) {
double[] temp = new double[dimension];
for (int i = 0; i < dimension; i++) {
temp[i] = 0.0;
}
return(new MathVector(temp));
}
public MathVector multiply(double a) {
double[] clone = x.clone();
for (int i = 0; i < x.length; i++) {
clone[i] *= a;
}
return(new MathVector(clone));
}
public MathVector divide(double a) {
return multiply(1/a);
}
public double abs() {
double temp = 0;
for (int i = 0; i < x.length; i++) {
temp += (x[i]*x[i]);
}
return Math.sqrt(temp);
}
public MathVector subtract(MathVector a) {
if (x.length != a.getDimension()) throw new RuntimeException("Dimension does not match!");
double[] clone = x.clone();
for (int i = 0; i < x.length; i++) {
clone[i] -= a.getXi(i);
}
return (new MathVector(clone));
}
public MathVector add(MathVector a) {
if (x.length != a.getDimension()) throw new RuntimeException("Dimension does not match!");
double[] clone = x.clone();
for (int i = 0; i < x.length; i++) {
clone[i] += a.getXi(i);
}
return (new MathVector(clone));
}
//TODO skalarprodukt, kreuzprodukt
public int getDimension() {
return x.length;
}
public double getXi(int i) {
return x[i];
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj.getClass() != MathVector.class) return false;
MathVector vec = (MathVector) obj;
if (vec.getDimension() != x.length) return false;
for (int i = 0; i < x.length; i++) {
if(x[i] != vec.getXi(i)) return false;
}
return true;
}
@Override
public String toString() {
String a = "";
for (int i = 0; i < x.length-1; i++) {
a += x[i] + " ";
}
a+=x[x.length-1];
return a;
}
}