Repository navigation
Expand file tree
/
Copy pathSparseVector.java
More file actions
164 lines (143 loc) · 4 KB
/
Copy pathSparseVector.java
File metadata and controls
164 lines (143 loc) · 4 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
/**
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on 5 Mar 2013
* Created by Andreas Prlic
*
* @since 3.0.6
*/
package org.biojava.nbio.structure.math;
import java.io.Serializable;
/**
*
* A sparse vector, implemented using a symbol table.
*
* Derived from http://introcs.cs.princeton.edu/java/44st/SparseVector.java.html
*
* For additional documentation, see <a href="http://introcs.cs.princeton.edu/44st">Section 4.4</a> of
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
*/
public class SparseVector implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1174668523213431927L;
private final int N; // length
private SymbolTable<Integer, Double> symbolTable; // the vector, represented by index-value pairs
/** Constructor. initialize the all 0s vector of length N
*
* @param N
*/
public SparseVector(int N) {
this.N = N;
this.symbolTable = new SymbolTable<Integer, Double>();
}
/** Setter method (should it be renamed to set?)
*
* @param i set symbolTable[i]
* @param value
*/
public void put(int i, double value) {
if (i < 0 || i >= N) throw new IllegalArgumentException("Illegal index " + i + " should be > 0 and < " + N);
if (value == 0.0) symbolTable.delete(i);
else symbolTable.put(i, value);
}
/** get a value
*
* @param i
* @return return symbolTable[i]
*/
public double get(int i) {
if (i < 0 || i >= N) throw new IllegalArgumentException("Illegal index " + i + " should be > 0 and < " + N);
if (symbolTable.contains(i)) return symbolTable.get(i);
else return 0.0;
}
// return the number of nonzero entries
public int nnz() {
return symbolTable.size();
}
// return the size of the vector
public int size() {
return N;
}
/** Calculates the dot product of this vector a with b
*
* @param b
* @return
*/
public double dot(SparseVector b) {
SparseVector a = this;
if (a.N != b.N) throw new IllegalArgumentException("Vector lengths disagree. " + a.N + " != " + b.N);
double sum = 0.0;
// iterate over the vector with the fewest nonzeros
if (a.symbolTable.size() <= b.symbolTable.size()) {
for (int i : a.symbolTable)
if (b.symbolTable.contains(i)) sum += a.get(i) * b.get(i);
}
else {
for (int i : b.symbolTable)
if (a.symbolTable.contains(i)) sum += a.get(i) * b.get(i);
}
return sum;
}
/** Calculates the 2-norm
*
* @return
*/
public double norm() {
SparseVector a = this;
return Math.sqrt(a.dot(a));
}
/** Calculates alpha * a
*
* @param alpha
* @return
*/
public SparseVector scale(double alpha) {
SparseVector a = this;
SparseVector c = new SparseVector(N);
for (int i : a.symbolTable) c.put(i, alpha * a.get(i));
return c;
}
/** Calcualtes return a + b
*
* @param b
* @return
*/
public SparseVector plus(SparseVector b) {
SparseVector a = this;
if (a.N != b.N) throw new IllegalArgumentException("Vector lengths disagree : " + a.N + " != " + b.N);
SparseVector c = new SparseVector(N);
for (int i : a.symbolTable) c.put(i, a.get(i)); // c = a
for (int i : b.symbolTable) c.put(i, b.get(i) + c.get(i)); // c = c + b
return c;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
for (int i : symbolTable) {
s.append("(");
s.append(i);
s.append(", ");
s.append(symbolTable.get(i));
s.append(") ");
}
return s.toString();
}
}