Skip to content

Commit 44a2b17

Browse files
ShadyBoukharyumar456
authored andcommitted
Changed Array.print() to Array.toString()
1 parent e27daa6 commit 44a2b17

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

com/arrayfire/Array.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Array extends ArrayFire implements AutoCloseable {
2929

3030
private native static long createArrayFromBoolean(int[] dims, boolean[] elems);
3131

32-
private native static void afPrint(long ref, String name);
32+
private native static String afPrint(long ref, String name);
3333

3434
// Global reference to JVM object
3535
// to persist between JNI calls
@@ -218,8 +218,13 @@ public boolean[] getBooleanArray() throws Exception {
218218
return Data.getBooleanArray(this);
219219
}
220220

221-
public void print(String name) {
222-
afPrint(ref, name);
221+
@Override
222+
public String toString() {
223+
return afPrint(ref, "No name");
224+
}
225+
226+
public String toString(String name) {
227+
return afPrint(ref, name);
223228
}
224229

225230
@Override

examples/HelloWorld.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ public static void main(String[] args) {
1111
Util.info();
1212
System.out.println("Create a 5-by-3 matrix of random floats on the GPU");
1313
Data.randu(a, new int[] { 5, 3 }, Array.FloatType);
14-
a.print("a");
14+
System.out.println(a.toString("a"));
1515

1616
System.out.println("Element-wise arithmetic");
1717
Arith.sin(b, a);
18-
b.print("b");
18+
System.out.println(a.toString("b"));
1919

2020
System.out.println("Fourier transform the result");
2121
Signal.fft(c, b);
22-
c.print("c");
22+
System.out.println(a.toString("c"));
2323

2424
System.out.println("Matmul b and c");
2525
Arith.mul(d, b, c);
26-
d.print("d");
26+
System.out.println(a.toString("d"));
2727

2828
System.out.println("Calculate weighted variance.");
2929
Array forVar = new Array();
3030
Array weights = new Array();
3131
Data.randn(forVar, new int[] { 5, 3 }, Array.DoubleType);
3232
Data.randn(weights, new int[] { 5, 3 }, Array.DoubleType);
33-
forVar.print("forVar");
33+
System.out.println(a.toString("forVar"));
3434

3535
double abc = Statistics.var(forVar, weights, Double.class);
3636
System.out.println(String.format("Variance is: %f", abc));
@@ -49,13 +49,13 @@ public static void main(String[] args) {
4949
data[i] = (float) (tmp);
5050
}
5151
Array e = new Array(dims, data);
52-
e.print("e");
52+
System.out.println(a.toString("e"));
5353

5454
System.out.println("Add e and random array");
5555
Array randa = new Array();
5656
Data.randu(randa, dims, Array.FloatType);
5757
Arith.add(f, e, randa);
58-
f.print("f");
58+
System.out.println(a.toString("f"));
5959

6060
System.out.println("Copy result back to host.");
6161
float[] result = f.getFloatArray();

src/array.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ JNIEXPORT void JNICALL ARRAY_FUNC(destroyArray)(JNIEnv *env, jclass clazz,
99
AF_CHECK_VOID(af_release_array(ARRAY(ref)));
1010
}
1111

12-
JNIEXPORT void JNICALL ARRAY_FUNC(afPrint)(JNIEnv *env, jclass clazz, jlong ref,
12+
JNIEXPORT jstring JNICALL ARRAY_FUNC(afPrint)(JNIEnv *env, jclass clazz, jlong ref,
1313
jstring name) {
1414
const char *str = env->GetStringUTFChars(name, 0);
15-
AF_CHECK_VOID(af_print_array_gen(str, ARRAY(ref), 3));
15+
char * res;
16+
af_array_to_string(&res, str, ARRAY(ref), 4, false);
17+
18+
//AF_CHECK_VOID(af_print_array_gen(str, ARRAY(ref), 3));
1619
env->ReleaseStringUTFChars(name, str);
20+
return env->NewStringUTF(res);
1721
}
1822

1923
JNIEXPORT jintArray JNICALL ARRAY_FUNC(getDims)(JNIEnv *env, jclass clazz,

src/statistics.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,27 @@ BEGIN_EXTERN_C
4747
return java::createJavaObject(env, java::JavaObjects::jtype, real, img); \
4848
}
4949

50-
JNIEXPORT jlong JNICALL STATISTICS_FUNC(afMean)(JNIEnv *env, jclass clazz,
51-
jlong ref, jint dim) {
52-
af_array ret = 0;
53-
AF_CHECK(af_mean(&ret, ARRAY(ref), dim));
54-
return JLONG(ret);
50+
#define INSTANTIATE_STAT(Name, name) \
51+
JNIEXPORT jlong JNICALL STATISTICS_FUNC(af##Name)(JNIEnv *env, jclass clazz, \
52+
jlong ref, jint dim) { \
53+
af_array ret = 0; \
54+
AF_CHECK(af_##name(&ret, ARRAY(ref), dim)); \
55+
return JLONG(ret); \
5556
}
5657

57-
JNIEXPORT jdouble JNICALL STATISTICS_FUNC(afMeanAll)(JNIEnv *env, jclass clazz,
58-
jlong ref) {
59-
double ret = 0;
60-
AF_CHECK(af_mean_all(&ret, NULL, ARRAY(ref)));
61-
return (jdouble)ret;
58+
INSTANTIATE_STAT(Mean, mean)
59+
INSTANTIATE_STAT(Stdev, stdev)
60+
61+
#define INSTANTIATE_STAT_ALL(Name, name) \
62+
JNIEXPORT jdouble JNICALL STATISTICS_FUNC(af##Name##All)(JNIEnv *env, jclass clazz, \
63+
jlong ref) { \
64+
double ret = 0; \
65+
AF_CHECK(af_##name##_all(&ret, NULL, ARRAY(ref))); \
66+
return (jdouble)ret; \
6267
}
6368

69+
INSTANTIATE_STAT_ALL(Mean, mean)
70+
6471
INSTANTIATE_MEAN(FloatComplex)
6572
INSTANTIATE_MEAN(DoubleComplex)
6673
INSTANTIATE_ALL_REAL_WEIGHTED(Mean, mean)

0 commit comments

Comments
 (0)