SparseVector.java
7.99 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/******************************************************************************
* Compilation: javac SparseVector.java
* Execution: java SparseVector
* Dependencies: StdOut.java
*
* A sparse vector, implementing using a symbol table.
*
* [Not clear we need the instance variable N except for error checking.]
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code SparseVector} class represents a <em>d</em>-dimensional mathematical vector.
* Vectors are mutable: their values can be changed after they are created.
* It includes methods for addition, subtraction,
* dot product, scalar product, unit vector, and Euclidean norm.
* <p>
* The implementation is a symbol table of indices and values for which the vector
* coordinates are nonzero. This makes it efficient when most of the vector coordindates
* are zero.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
* See also {@link Vector} for an immutable (dense) vector data type.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class SparseVector {
private int d; // dimension
private ST<Integer, Double> st; // the vector, represented by index-value pairs
/**
* Initializes a d-dimensional zero vector.
* @param d the dimension of the vector
*/
public SparseVector(int d) {
this.d = d;
this.st = new ST<Integer, Double>();
}
/**
* Sets the ith coordinate of this vector to the specified value.
*
* @param i the index
* @param value the new value
* @throws IndexOutOfBoundsException unless i is between 0 and d-1
*/
public void put(int i, double value) {
if (i < 0 || i >= d) throw new IndexOutOfBoundsException("Illegal index");
if (value == 0.0) st.delete(i);
else st.put(i, value);
}
/**
* Returns the ith coordinate of this vector.
*
* @param i the index
* @return the value of the ith coordinate of this vector
* @throws IndexOutOfBoundsException unless i is between 0 and d-1
*/
public double get(int i) {
if (i < 0 || i >= d) throw new IndexOutOfBoundsException("Illegal index");
if (st.contains(i)) return st.get(i);
else return 0.0;
}
/**
* Returns the number of nonzero entries in this vector.
*
* @return the number of nonzero entries in this vector
*/
public int nnz() {
return st.size();
}
/**
* Returns the dimension of this vector.
*
* @return the dimension of this vector
* @deprecated Replaced by {@link #dimension()}.
*/
@Deprecated
public int size() {
return d;
}
/**
* Returns the dimension of this vector.
*
* @return the dimension of this vector
*/
public int dimension() {
return d;
}
/**
* Returns the inner product of this vector with the specified vector.
*
* @param that the other vector
* @return the dot product between this vector and that vector
* @throws IllegalArgumentException if the lengths of the two vectors are not equal
*/
public double dot(SparseVector that) {
if (this.d != that.d) throw new IllegalArgumentException("Vector lengths disagree");
double sum = 0.0;
// iterate over the vector with the fewest nonzeros
if (this.st.size() <= that.st.size()) {
for (int i : this.st.keys())
if (that.st.contains(i)) sum += this.get(i) * that.get(i);
}
else {
for (int i : that.st.keys())
if (this.st.contains(i)) sum += this.get(i) * that.get(i);
}
return sum;
}
/**
* Returns the inner product of this vector with the specified array.
*
* @param that the array
* @return the dot product between this vector and that array
* @throws IllegalArgumentException if the dimensions of the vector and the array are not equal
*/
public double dot(double[] that) {
double sum = 0.0;
for (int i : st.keys())
sum += that[i] * this.get(i);
return sum;
}
/**
* Returns the magnitude of this vector.
* This is also known as the L2 norm or the Euclidean norm.
*
* @return the magnitude of this vector
*/
public double magnitude() {
return Math.sqrt(this.dot(this));
}
/**
* Returns the Euclidean norm of this vector.
*
* @return the Euclidean norm of this vector
* @deprecated Replaced by {@link #magnitude()}.
*/
@Deprecated
public double norm() {
return Math.sqrt(this.dot(this));
}
/**
* Returns the scalar-vector product of this vector with the specified scalar.
*
* @param alpha the scalar
* @return the scalar-vector product of this vector with the specified scalar
*/
public SparseVector scale(double alpha) {
SparseVector c = new SparseVector(d);
for (int i : this.st.keys()) c.put(i, alpha * this.get(i));
return c;
}
/**
* Returns the sum of this vector and the specified vector.
*
* @param that the vector to add to this vector
* @return the sum of this vector and that vector
* @throws IllegalArgumentException if the dimensions of the two vectors are not equal
*/
public SparseVector plus(SparseVector that) {
if (this.d != that.d) throw new IllegalArgumentException("Vector lengths disagree");
SparseVector c = new SparseVector(d);
for (int i : this.st.keys()) c.put(i, this.get(i)); // c = this
for (int i : that.st.keys()) c.put(i, that.get(i) + c.get(i)); // c = c + that
return c;
}
/**
* Returns a string representation of this vector.
* @return a string representation of this vector, which consists of the
* the vector entries, separates by commas, enclosed in parentheses
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (int i : st.keys()) {
s.append("(" + i + ", " + st.get(i) + ") ");
}
return s.toString();
}
/**
* Unit tests the {@code SparseVector} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
SparseVector a = new SparseVector(10);
SparseVector b = new SparseVector(10);
a.put(3, 0.50);
a.put(9, 0.75);
a.put(6, 0.11);
a.put(6, 0.00);
b.put(3, 0.60);
b.put(4, 0.90);
StdOut.println("a = " + a);
StdOut.println("b = " + b);
StdOut.println("a dot b = " + a.dot(b));
StdOut.println("a + b = " + a.plus(b));
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/