-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.java
173 lines (142 loc) · 4.84 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Fundamentals;
/**
*
* @author stefan0
*/
/*************************************************************************
* Compilation: javac Vector.java
* Execution: java Vector
*
* Implementation of a vector of real numbers.
*
* This class is implemented to be immutable: once the client program
* initialize a Vector, it cannot change any of its fields
* (N or data[i]) either directly or indirectly. Immutability is a
* very desirable feature of a data type.
*
* % java Vector
* x = [ 1.0 2.0 3.0 4.0 ]
* y = [ 5.0 2.0 4.0 1.0 ]
* z = [ 6.0 4.0 7.0 5.0 ]
* 10z = [ 60.0 40.0 70.0 50.0 ]
* |x| = 5.477225575051661
* <x, y> = 25.0
*
*
* Note that Vector is also the name of an unrelated Java library class.
*
*************************************************************************/
public class Vector {
private int N; // length of the vector
private double[] data; // array of vector's components
// create the zero vector of length n
public Vector(int n) {
N = n;
data = new double[N];
}
// create a vector from either an array or a vararg list
public Vector(double[] d) {
N = d.length;
// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}
// create a vector from either an array or a vararg list
// this constructor uses Java's vararg syntax to support
// a constructor that takes a variable number of arguments, such as
// Vector x = new Vector(1.0, 2.0, 3.0, 4.0);
// Vector y = new Vector(5.0, 2.0, 4.0, 1.0);
/*
public Vector(double... d) {
N = d.length;
// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}
*/
// return the length of the vector
public int length() {
return N;
}
// return the inner product of this Vector a and b
public double dot(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
double sum = 0.0;
for (int i = 0; i < N; i++)
sum = sum + (this.data[i] * that.data[i]);
return sum;
}
// return the Euclidean norm of this Vector
public double magnitude() {
return Math.sqrt(this.dot(this));
}
// return the Euclidean distance between this and that
public double distanceTo(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
return this.minus(that).magnitude();
}
// return this + that
public Vector plus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] + that.data[i];
return c;
}
// return this + that
public Vector minus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] - that.data[i];
return c;
}
// return the corresponding coordinate
public double cartesian(int i) {
return data[i];
}
// create and return a new object whose value is (this * factor)
public Vector times(double factor) {
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = factor * data[i];
return c;
}
// return the corresponding unit vector
public Vector direction() {
if (this.magnitude() == 0.0) throw new RuntimeException("Zero-vector has no direction");
return this.times(1.0 / this.magnitude());
}
// return a string representation of the vector
public String toString() {
String s = "";
for (int i = 0; i < N; i++)
s = s + data[i] + " ";
return s;
}
// test client
public static void main(String[] args) {
double[] xdata = { 1.0, 2.0, 3.0, 4.0 };
double[] ydata = { 5.0, 2.0, 4.0, 1.0 };
Vector x = new Vector(xdata);
Vector y = new Vector(ydata);
/*
StdOut.println(" x = " + x);
StdOut.println(" y = " + y);
Vector z = x.plus(y);
StdOut.println(" z = " + z);
z = z.times(10.0);
StdOut.println(" 10z = " + z);
StdOut.println(" |x| = " + x.magnitude());
StdOut.println(" <x, y> = " + x.dot(y));
StdOut.println("dist(x, y) = " + x.distanceTo(y));
StdOut.println("dir(x) = " + x.direction());
*/
}
}