-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathArray.java
More file actions
220 lines (161 loc) · 5.55 KB
/
Array.java
File metadata and controls
220 lines (161 loc) · 5.55 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
package com.arrayfire;
public class Array extends AFLibLoader implements AutoCloseable {
private native static void destroyArray(long ref);
private native static int[] getDims(long ref);
private native static int getType(long ref);
private native static long createEmptyArray(int[] dims, int type);
private native static long createArrayFromFloat(int[] dims, float[] elems);
private native static long createArrayFromDouble(int[] dims, double[] elems);
private native static long createArrayFromFloatComplex(int[] dims, FloatComplex[] elems);
private native static long createArrayFromDoubleComplex(int[] dims, DoubleComplex[] elems);
private native static long createArrayFromInt(int[] dims, int[] elems);
private native static long createArrayFromBoolean(int[] dims, boolean[] elems);
private native static String afToString(long ref, String name);
// Global reference to JVM object
// to persist between JNI calls
protected long ref;
public Array() {
ref = 0;
}
protected Array(long other_ref) {
ref = other_ref;
}
public Array(int[] dims, ArrayFire.Type type) throws Exception {
int[] adims = Array.dim4(dims);
set(createEmptyArray(adims, type.getType()));
}
public Array(int[] dims, float[] elems) throws IllegalArgumentException {
int[] adims = Array.dim4(dims);
int total_size = 1;
for (int dim : adims) {
total_size *= dim;
}
if (elems == null) {
throw new IllegalArgumentException("Null elems object provided");
}
if (elems.length > total_size || elems.length < total_size) {
throw new IllegalArgumentException("Mismatching dims and array size");
}
set(createArrayFromFloat(adims, elems));
}
public Array(int[] dims, double[] elems) throws IllegalArgumentException {
int[] adims = Array.dim4(dims);
int total_size = 1;
for (int dim : adims) {
total_size *= dim;
}
if (elems == null) {
throw new IllegalArgumentException("Null elems object provided");
}
if (elems.length > total_size || elems.length < total_size) {
throw new IllegalArgumentException("Mismatching dims and array size");
}
set(createArrayFromDouble(adims, elems));
}
public Array(int[] dims, int[] elems) throws IllegalArgumentException {
int[] adims = Array.dim4(dims);
int total_size = 1;
for (int dim : adims) {
total_size *= dim;
}
if (elems == null) {
throw new IllegalArgumentException("Null elems object provided");
}
if (elems.length > total_size || elems.length < total_size) {
throw new IllegalArgumentException("Mismatching dims and array size");
}
set(createArrayFromInt(adims, elems));
}
public Array(int[] dims, FloatComplex[] elems) throws Exception {
int[] adims = Array.dim4(dims);
int total_size = 1;
for (int dim : adims) {
total_size *= dim;
}
if (elems == null) {
throw new Exception("Null elems object provided");
}
if (elems.length > total_size || elems.length < total_size) {
throw new Exception("Mismatching dims and array size");
}
set(createArrayFromFloatComplex(adims, elems));
}
public Array(int[] dims, DoubleComplex[] elems) throws IllegalArgumentException {
int[] adims = Array.dim4(dims);
int total_size = 1;
for (int dim : adims) {
total_size *= dim;
}
if (elems == null) {
throw new IllegalArgumentException("Null elems object provided");
}
if (elems.length > total_size || elems.length < total_size) {
throw new IllegalArgumentException("Mismatching dims and array size");
}
set(createArrayFromDoubleComplex(adims, elems));
}
protected void set(long other_ref) {
if (ref != 0)
destroyArray(ref);
ref = other_ref;
}
public int[] dims() {
return getDims(ref);
}
public ArrayFire.Type type() {
return ArrayFire.Type.fromInt(getType(ref));
}
protected static int[] dim4(int[] dims) throws IllegalArgumentException {
if (dims == null) {
throw new IllegalArgumentException("Null dimensions object provided");
} else if (dims.length > 4) {
throw new IllegalArgumentException("ArrayFire supports up to 4 dimensions only");
}
int[] adims = new int[] { 1, 1, 1, 1 };
System.arraycopy(dims, 0, adims, 0, dims.length);
return adims;
}
protected void assertType(ArrayFire.Type ty) throws Exception {
ArrayFire.Type myType = type();
if (myType != ty) {
String str = "Type mismatch: ";
str = str + "Requested " + ty;
str = str + ". Found " + myType;
throw new IllegalArgumentException(str);
}
return;
}
public float[] getFloatArray() throws Exception {
return Data.getFloatArray(this);
}
public double[] getDoubleArray() throws Exception {
return Data.getDoubleArray(this);
}
public FloatComplex[] getFloatComplexArray() throws Exception {
return Data.getFloatComplexArray(this);
}
public DoubleComplex[] getDoubleComplexArray() throws Exception {
return Data.getDoubleComplexArray(this);
}
public int[] getIntArray() throws Exception {
return Data.getIntArray(this);
}
public boolean[] getBooleanArray() throws Exception {
return Data.getBooleanArray(this);
}
public boolean isBool() {
return type() == ArrayFire.Type.Boolean;
}
@Override
public String toString() {
return afToString(ref, "No name");
}
public String toString(String name) {
return afToString(ref, name);
}
@Override
public void close() throws Exception {
if (ref != 0)
destroyArray(ref);
}
}