-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathAbstractFlatMatrix.java
More file actions
264 lines (236 loc) · 8.42 KB
/
AbstractFlatMatrix.java
File metadata and controls
264 lines (236 loc) · 8.42 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* ---------------------------------------------------------------------
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2016, Numenta, Inc. Unless you have an agreement
* with Numenta, Inc., for a separate license for this software code, the
* following terms and conditions apply:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
* ---------------------------------------------------------------------
*/
package org.numenta.nupic.util;
import java.io.Serializable;
import java.util.Arrays;
/**
* Base class for flat {@link Matrix} implementations.
*
* @author David Ray
* @author Jose Luis Martin
*
* @param <T> element type
*/
public abstract class AbstractFlatMatrix<T> implements FlatMatrix<T>, Serializable {
/** keep it simple */
private static final long serialVersionUID = 1L;
protected int[] dimensions;
protected int[] dimensionMultiples;
protected boolean isColumnMajor;
protected int numDimensions;
/**
* Constructs a new {@link AbstractFlatMatrix} object to be configured with specified
* dimensions and major ordering.
* @param dimensions the dimensions of this matrix
*/
public AbstractFlatMatrix(int[] dimensions) {
this(dimensions, false);
}
/**
* Constructs a new {@link AbstractFlatMatrix} object to be configured with specified
* dimensions and major ordering.
*
* @param dimensions the dimensions of this sparse array
* @param useColumnMajorOrdering flag indicating whether to use column ordering or
* row major ordering. if false (the default), then row
* major ordering will be used. If true, then column major
* ordering will be used.
*/
public AbstractFlatMatrix(int[] dimensions, boolean useColumnMajorOrdering) {
this.dimensions = dimensions;
this.numDimensions = dimensions.length;
this.dimensionMultiples = initDimensionMultiples(
useColumnMajorOrdering ? reverse(dimensions) : dimensions);
isColumnMajor = useColumnMajorOrdering;
}
/**
* Compute the flat index of a multidimensional array.
* @param indexes multidimensional indexes
* @return the flat array index;
*/
public int computeIndex(int[] indexes) {
return computeIndex(indexes, true);
}
/**
* Returns a flat index computed from the specified coordinates
* which represent a "dimensioned" index.
*
* @param coordinates an array of coordinates
* @param doCheck enforce validated comparison to locally stored dimensions
* @return a flat index
*/
public int computeIndex(int[] coordinates, boolean doCheck) {
if(doCheck) checkDims(coordinates);
int[] localMults = isColumnMajor ? reverse(dimensionMultiples) : dimensionMultiples;
int base = 0;
for(int i = 0;i < coordinates.length;i++) {
base += (localMults[i] * coordinates[i]);
}
return base;
}
/**
* Checks the indexes specified to see whether they are within the
* configured bounds and size parameters of this array configuration.
*
* @param index the array dimensions to check
*/
protected void checkDims(int[] index) {
if(index.length != numDimensions) {
throw new IllegalArgumentException("Specified coordinates exceed the configured array dimensions " +
"input dimensions: " + index.length + " > number of configured dimensions: " + numDimensions);
}
for(int i = 0;i < index.length - 1;i++) {
if(index[i] >= dimensions[i]) {
throw new IllegalArgumentException("Specified coordinates exceed the configured array dimensions " +
Arrays.toString(index) + " > " + Arrays.toString(dimensions));
}
}
}
/**
* Returns an array of coordinates calculated from
* a flat index.
*
* @param index specified flat index
* @return a coordinate array
*/
@Override
public int[] computeCoordinates(int index) {
int[] returnVal = new int[getNumDimensions()];
int base = index;
for(int i = 0;i < dimensionMultiples.length; i++) {
int quotient = base / dimensionMultiples[i];
base %= dimensionMultiples[i];
returnVal[i] = quotient;
}
return isColumnMajor ? reverse(returnVal) : returnVal;
}
/**
* Initializes internal helper array which is used for multidimensional
* index computation.
* @param dimensions matrix dimensions
* @return array for use in coordinates to flat index computation.
*/
protected int[] initDimensionMultiples(int[] dimensions) {
int holder = 1;
int len = dimensions.length;
int[] dimensionMultiples = new int[getNumDimensions()];
for(int i = 0;i < len;i++) {
holder *= (i == 0 ? 1 : dimensions[len - i]);
dimensionMultiples[len - 1 - i] = holder;
}
return dimensionMultiples;
}
/**
* Utility method to shrink a single dimension array by one index.
* @param array the array to shrink
* @return
*/
protected int[] copyInnerArray(int[] array) {
if(array.length == 1) return array;
int[] retVal = new int[array.length - 1];
System.arraycopy(array, 1, retVal, 0, array.length - 1);
return retVal;
}
/**
* Reverses the specified array.
* @param input
* @return
*/
public static int[] reverse(int[] input) {
int[] retVal = new int[input.length];
for(int i = input.length - 1, j = 0;i >= 0;i--, j++) {
retVal[j] = input[i];
}
return retVal;
}
@Override
public abstract T get(int index);
@Override
public abstract AbstractFlatMatrix<T> set(int index, T value);
@Override
public T get(int... indexes) {
return get(computeIndex(indexes));
}
@Override
public AbstractFlatMatrix<T> set(int[] indexes, T value) {
set(computeIndex(indexes), value);
return this;
}
public int getSize() {
return Arrays.stream(this.dimensions).reduce((n,i) -> n*i).getAsInt();
}
@Override
public int getMaxIndex() {
return getDimensions()[0] * Math.max(1, getDimensionMultiples()[0]) - 1;
}
@Override
public int[] getDimensions() {
return this.dimensions;
}
public void setDimensions(int[] dimensions) {
this.dimensions = dimensions;
}
@Override
public int getNumDimensions() {
return this.dimensions.length;
}
@Override
public int[] getDimensionMultiples() {
return this.dimensionMultiples;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(dimensionMultiples);
result = prime * result + Arrays.hashCode(dimensions);
result = prime * result + (isColumnMajor ? 1231 : 1237);
result = prime * result + numDimensions;
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
AbstractFlatMatrix other = (AbstractFlatMatrix)obj;
if(!Arrays.equals(dimensionMultiples, other.dimensionMultiples))
return false;
if(!Arrays.equals(dimensions, other.dimensions))
return false;
if(isColumnMajor != other.isColumnMajor)
return false;
if(numDimensions != other.numDimensions)
return false;
return true;
}
}