Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 17ca994

Browse filesBrowse files
ShadyBoukharyumar456
authored andcommitted
Added range function and Seq class.
1 parent c9d8c02 commit 17ca994
Copy full SHA for 17ca994

File tree

Expand file treeCollapse file tree

8 files changed

+160
-5
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+160
-5
lines changed

‎CMakeLists.txt

Copy file name to clipboardExpand all lines: CMakeLists.txt
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ ADD_JAR(${AF_JAR}
3434
com/arrayfire/ArrayFireException.java
3535
com/arrayfire/Graphics.java
3636
com/arrayfire/Window.java
37+
com/arrayfire/Seq.java
38+
com/arrayfire/AFLibLoader.java
3739
)
3840

3941
ADD_DEPENDENCIES(${AF_JAR} ${AF_LIB})

‎com/arrayfire/AFLibLoader.java

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arrayfire;
2+
3+
class AFLibLoader {
4+
static {
5+
System.loadLibrary("af_java");
6+
}
7+
}

‎com/arrayfire/Array.java

Copy file name to clipboardExpand all lines: com/arrayfire/Array.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.arrayfire;
22

3-
public class Array implements AutoCloseable {
3+
public class Array extends AFLibLoader implements AutoCloseable {
44

55
private native static void destroyArray(long ref);
66

‎com/arrayfire/ArrayFire.java

Copy file name to clipboardExpand all lines: com/arrayfire/ArrayFire.java
+20-4Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.arrayfire.Util;
44

5-
public class ArrayFire {
6-
static {
7-
System.loadLibrary("af_java");
8-
}
5+
public class ArrayFire extends AFLibLoader {
96

107
/* ************* Algorithm ************* */
118

@@ -279,6 +276,24 @@ public static void identity(Array res, int[] dims) throws Exception {
279276
Data.identity(res, dims);
280277
}
281278

279+
public static Array range(int[] dims, int seqDim, Type type) {
280+
return Data.range(dims, seqDim, type);
281+
}
282+
283+
284+
public static Array range(int[] dims, int seqDim) {
285+
return Data.range(dims, seqDim, Type.Float);
286+
}
287+
288+
public static Array range(int[] dims) {
289+
return Data.range(dims, -1, Type.Float);
290+
}
291+
292+
293+
public static Array range(int dim) {
294+
return Data.range(new int[] {dim}, -1, Type.Float);
295+
}
296+
282297
/* ************* Image ************* */
283298

284299
public static void erode(Array res, Array a, Array b) throws Exception {
@@ -490,6 +505,7 @@ public static void info() {
490505
Util.info();
491506
}
492507

508+
493509
// Enums
494510

495511
public static enum Type {

‎com/arrayfire/Data.java

Copy file name to clipboardExpand all lines: com/arrayfire/Data.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Data {
2222

2323
private native static long createIdentityArray(int[] dims, int type);
2424

25+
private native static long afRange(int[] dims, int seqDim, int type);
26+
2527
public static float[] getFloatArray(Array A) throws Exception {
2628
A.assertType(ArrayFire.Type.Float);
2729
return getFloatFromArray(A.ref);
@@ -77,4 +79,8 @@ public static void identity(Array res, int[] dims) throws Exception {
7779
identity(res, dims, ArrayFire.Type.Float);
7880
}
7981

82+
public static Array range(int[] dims, int seqDim, ArrayFire.Type type) {
83+
int[] adims = Array.dim4(dims);
84+
return new Array(afRange(adims, seqDim, seqDim));
85+
}
8086
}

‎com/arrayfire/Seq.java

Copy file name to clipboard
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.arrayfire;
2+
3+
import static com.arrayfire.Util.*;
4+
import static com.arrayfire.ArrayFire.range;
5+
import static com.arrayfire.ArrayFire.add;
6+
import static com.arrayfire.ArrayFire.mul;
7+
8+
public class Seq extends AFLibLoader {
9+
// Start position of the sequence
10+
private double begin;
11+
// End position of the sequence (inclusive)
12+
private double end;
13+
// Step size between sequence values
14+
private double step;
15+
16+
private long size;
17+
private boolean gfor; // always false until implemented
18+
19+
public Seq() {
20+
this(0);
21+
}
22+
23+
public Seq(double length) {
24+
if (length < 0) {
25+
init(0, length, 1);
26+
} else {
27+
init(0, length - 1, 1);
28+
}
29+
}
30+
31+
public Seq(double begin, double end) throws Exception {
32+
this(begin, end, 1);
33+
}
34+
35+
public Seq(double begin, double end, double step) throws Exception {
36+
if (step == 0 && begin != end) { // Span
37+
throw new IllegalArgumentException("Invalid step size.");
38+
}
39+
if ((signbit(end) == signbit(begin)) && (signbit(end - begin) != signbit(step))) {
40+
throw new Exception("Sequence is invalid");
41+
}
42+
init(begin, end, step);
43+
}
44+
45+
public Seq(Seq other) {
46+
set(other);
47+
}
48+
49+
public void set(Seq other) {
50+
init(other.begin, other.end, other.step);
51+
}
52+
53+
public Seq negate() throws Exception {
54+
return new Seq(-begin, -end, -step);
55+
}
56+
57+
public Seq addOffset(double offset) throws Exception {
58+
return new Seq(begin + offset, end + offset, step);
59+
}
60+
61+
public Seq subtractOffset(double offset) throws Exception {
62+
return new Seq(begin - offset, end - offset, step);
63+
}
64+
65+
public Seq spaceBy(double factor) throws Exception {
66+
return new Seq(begin * factor, end * factor, step * factor);
67+
}
68+
69+
public Array toArray() throws Exception {
70+
double diff = end - begin;
71+
int len = (int)((diff + Math.abs(step) * (signbit(diff) == 0 ? 1 : -1)) / step);
72+
Array tmp = range(len);
73+
Array res = new Array();
74+
mul(res, (float) step, tmp);
75+
add(res, (float)begin, res);
76+
return res;
77+
}
78+
79+
private void init(double begin, double end, double step) {
80+
this.begin = begin;
81+
this.end = end;
82+
this.step = step;
83+
this.gfor = false;
84+
this.size = (long)(step != 0 ? Math.abs((end - begin) / step) + 1 : 0);
85+
}
86+
87+
@Override
88+
public boolean equals(Object obj) {
89+
if (obj == this) {
90+
return true;
91+
}
92+
if (obj == null || obj.getClass() != this.getClass()) {
93+
return false;
94+
}
95+
Seq seq = (Seq) obj;
96+
return this.begin == seq.begin && this.end == seq.end &&
97+
this.step == seq.step && size == seq.size;
98+
}
99+
100+
@Override
101+
public int hashCode() {
102+
final int prime = 31;
103+
int result = 1;
104+
result = prime * result + (int) ((int)begin ^ ((int)begin >>> 32));
105+
result = prime * result + (int) ((int) end ^ ((int) end >>> 32));
106+
result = prime * result + (int) ((int) step ^ ((int) step >>> 32));
107+
result = prime * result + (int) ((int) size ^ ((int) size >>> 32));
108+
return result;
109+
}
110+
}

‎com/arrayfire/Util.java

Copy file name to clipboardExpand all lines: com/arrayfire/Util.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public static float[] toFloatArray(String text, String delim) {
2727
}
2828
return ret_ary;
2929
}
30+
31+
public static int signbit(double x) {
32+
return x < 0 ? -1 : 0;
33+
}
3034
}

‎src/data.cpp

Copy file name to clipboardExpand all lines: src/data.cpp
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ JNIEXPORT jlong JNICALL DATA_FUNC(createRandnArray)(JNIEnv *env, jclass clazz,
2828
return JLONG(ret);
2929
}
3030

31+
JNIEXPORT jlong JNICALL DATA_FUNC(afRange)(JNIEnv *env, jclass clazz,
32+
jintArray dims, jint seqDim,
33+
jint type) {
34+
af_array ret = 0;
35+
jint *dimptr = env->GetIntArrayElements(dims, 0);
36+
dim_t tdims[4] = {dimptr[0], dimptr[1], dimptr[2], dimptr[3]};
37+
AF_CHECK(af_range(&ret, 4, tdims, seqDim, (af_dtype)type));
38+
env->ReleaseIntArrayElements(dims, dimptr, 0);
39+
}
40+
3141
JNIEXPORT jlong JNICALL DATA_FUNC(createConstantsArray)(
3242
JNIEnv *env, jclass clazz, jdouble val, jintArray dims, jint type) {
3343
af_array ret = 0;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.