Skip to content

Commit

Permalink
Misc utilities added to the java side; reduce warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cottam committed Sep 1, 2013
1 parent 0c26fad commit eaaa7a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ jep/*
*.pyc
bin
TestResults
*.swp
12 changes: 10 additions & 2 deletions Numpy4J/src/np/NPArray.java
Expand Up @@ -23,7 +23,7 @@ public class NPArray {
* and set to match the type's byte order. The buffer view DOES NOT duplicate the
* backing bytes, just the header information.
*/
private NPArray(ByteBuffer buffer, NPType type){
public NPArray(ByteBuffer buffer, NPType type){
if (buffer.order() != type.byteorder().nio) {
buffer = buffer.duplicate();
buffer.order(type.byteorder().nio);
Expand Down Expand Up @@ -83,13 +83,21 @@ public void arange(int start, int step) {
}
}



/**Present the same buffer, with different type information.**/
public NPArray recast(NPType type) {return new NPArray(buffer, type);}

/**Number of items to show in toString()**/
public static int DISPLAY_LIMIT=10;

public String toString() {
StringBuilder b = new StringBuilder();
b.append("NPArray[");
b.append(size());
b.append("]{");
b.append("]");
b.append(type.toString());
b.append("{");

for (int i=0; i<DISPLAY_LIMIT && i < size(); i++) {
b.append(getFlat(i).toString());
Expand Down
16 changes: 11 additions & 5 deletions Numpy4J/src/np/NPType.java
Expand Up @@ -9,13 +9,17 @@ public enum RAWTYPE {int8(1), int16(2), int32(4), int64(8), float32(4), float64(
RAWTYPE(int bytes) {this.bytes = bytes;}
}

enum BYTE_ORDER {
big(ByteOrder.BIG_ENDIAN), little(ByteOrder.LITTLE_ENDIAN), NATIVE(ByteOrder.nativeOrder());
public enum BYTE_ORDER {
big(ByteOrder.BIG_ENDIAN,">"), little(ByteOrder.LITTLE_ENDIAN,"<"), NATIVE(ByteOrder.nativeOrder(), "=");
public final ByteOrder nio;
BYTE_ORDER(ByteOrder nio) {this.nio = nio;}
public final String symbol;
BYTE_ORDER(ByteOrder nio, String symbol) {
this.nio = nio;
this.symbol = symbol;
}
}

enum ORDER {c,fortran,DEFER}
public enum ORDER {c,fortran,DEFER}

private final RAWTYPE rawtype;
private final ORDER order;
Expand All @@ -36,9 +40,11 @@ public NPType(RAWTYPE rawtype, ORDER order, BYTE_ORDER byteorder) {
public NPType order(ORDER order) {return new NPType(this.rawtype, order, this.byteorder);}
public ORDER order() {return order;}

public NPType byteOrder(BYTE_ORDER byteorder) {return new NPType(this.rawtype, this.order, byteorder);}
public NPType byteorder(BYTE_ORDER byteorder) {return new NPType(this.rawtype, this.order, byteorder);}
public BYTE_ORDER byteorder() {return byteorder;}

public String toString() {return String.format("[%s, %s]", rawtype.toString(), byteorder.symbol);}

/**NPType with values that can be dered set to deferred and others with sensible defaults.*/
public static NPType defer() {return new NPType(RAWTYPE.DEFER, ORDER.DEFER, BYTE_ORDER.NATIVE);}
}
4 changes: 2 additions & 2 deletions Numpy4J/src/np/test/Exec.java
Expand Up @@ -7,14 +7,14 @@ public class Exec {
public static void main(String[] args){
NPType type = new NPType();
NPArray array = NPArray.allocate(type, 10);
array.arange();
array.arange(1,1);
JNIBridge bridge= new JNIBridge();

System.out.printf("Input: %s\n", array);

System.out.printf("Max: %s\n", bridge.max(array));
System.out.printf("Mult: %s\n", bridge.mult(array, array));
System.out.printf("Min: %s\n", bridge.min(array));
System.out.printf("Log: %s\n", bridge.log(array));
System.out.printf("Mult: %s\n", bridge.mult(array, array));
}
}
14 changes: 8 additions & 6 deletions Numpy4J/src/np/test/TestBridge.java
Expand Up @@ -4,27 +4,29 @@
import org.junit.Test;

import np.*;
import static np.NPType.RAWTYPE.*;

public class TestBridge {

@Test
public void multiply() {
NPType t = new NPType(NPType.RAWTYPE.int32);
NPType t = new NPType(int32);
NPArray array = NPArray.allocate(t, 10);
array.arange();
NPArray result = JNIBridge.mult(array,array);

assertEquals(array.size(), result.size());
assertEquals(array.type().rawtype(), result.type().rawtype());
for (int i=0; i<array.size(); i++) {
assertEquals("Error at position " + i, i*i, result.getFlatInt(i));
}
}

@Test
public void max() {
NPType t = new NPType(NPType.RAWTYPE.int32);
NPType t = new NPType(int32);

for (int i=10; i<1000000; i+=5423) {
for (int i=10; i<100000; i+=523) {
NPArray array = NPArray.allocate(t, i);
array.arange();
long max = JNIBridge.max(array);
Expand All @@ -34,9 +36,9 @@ public void max() {

@Test
public void min() {
NPType t = new NPType(NPType.RAWTYPE.int32);
NPType t = new NPType(int32);

for (int i=10; i<1000000; i+=2345) {
for (int i=10; i<100000; i+=235) {
NPArray array = NPArray.allocate(t, i);
array.arange(10, -1);
long min = JNIBridge.min(array);
Expand All @@ -46,7 +48,7 @@ public void min() {

@Test
public void log() {
NPType t = new NPType(NPType.RAWTYPE.int32);
NPType t = new NPType(int32);

NPArray array = NPArray.allocate(t,3000);
array.arange(1,3);
Expand Down

0 comments on commit eaaa7a5

Please sign in to comment.