Skip to content

Commit

Permalink
Scala wrapping of INDArray class of ND4j
Browse files Browse the repository at this point in the history
  • Loading branch information
sterglee committed Jan 4, 2019
1 parent 295dffd commit 60f6be4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Binary file modified ScalaLab.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/java/scalaExec/Interpreter/GlobalValues.java
Expand Up @@ -42,7 +42,7 @@ public class GlobalValues
{


static public String scalalabBuildVersion = "3-Jan-19";
static public String scalalabBuildVersion = "4-Jan-19";

static public boolean createNewInterpreterOnToggleLibraries = false; // controls whether to create a new interpreter on library switching along with a new classloader
// or to simply execute the proper import statements in order to wsitch libraries
Expand Down
@@ -0,0 +1,55 @@


// wrapping with more friendly user interface the class INDArray
// this example runs only with Deeplearning4j version of ScalaLab, i.e. ScalaLabDL4j.jar
class ND4jMat (x: RichDouble2DArray) {
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.util.ArrayUtil
import java.util.Arrays


var x1d = scalaSci.RichDouble2DArray.oneDDoubleArray(x)

var data = Nd4j.create( x1d, Array( x.Nrows, x.Ncols) )


def this(xp: INDArray) = this(xp.toDoubleMatrix )


def + ( that: INDArray) = new ND4jMat(data.add(that))


def - ( that: INDArray) = new ND4jMat(data.sub(that))



def * ( that: INDArray) = new ND4jMat(data.mmul(that))


def + ( that: ND4jMat):ND4jMat = this + that.data


def - ( that: ND4jMat):ND4jMat = this - that.data



def * ( that: ND4jMat):ND4jMat = this * that.data

//def - ( that: ND4jMat) = this.sub(that)
// def * ( that: ND4jMat) = this.mmul(that)

}



var N=4000

var x = rand(N,N)

var nx = new ND4jMat(x)


tic
var nxnx = nx * nx // multiply fast using ND4j
var tm=toc

0 comments on commit 60f6be4

Please sign in to comment.