Permalink
Browse files

Added class org.jblas.util.Random which maintains a global random gen…

…erator
  • Loading branch information...
1 parent 3d1730f commit 7b84e457fe1920c2cb155bb58c76bea1edf7bdb1 mikiobraun committed Jun 24, 2011
View
11 ROADMAP
@@ -1,3 +1,14 @@
+Roadmap to jblas 1.2.1
+
+1. Linux / 32 bit / sse2
+2. Linux / 32 bit / sse3
+3. Linux / 64 bit / sse2
+4. Linux / 64 bit / sse3
+5. Mac OS X / 64 bit / sse3
+6. Windows / 32 bit / sse2
+7. Windows / 32 bit / sse3
+8. Windows / 64 bit / lapack lite only
+
Roadmap to jblas-0.3.1
- collect static builds for Windows(32), Linux(32/64), MacOSX(32/64).
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
+ <output url="file://$MODULE_DIR$/target/classes" />
+ <output-test url="file://$MODULE_DIR$/target/test-classes" />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
+ <excludeFolder url="file://$MODULE_DIR$/target" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
+ </component>
+</module>
+
@@ -39,6 +39,8 @@
import org.jblas.exceptions.SizeException;
import org.jblas.ranges.Range;
+import org.jblas.util.Random;
+
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -436,9 +438,8 @@ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundEx
public static DoubleMatrix rand(int rows, int columns) {
DoubleMatrix m = new DoubleMatrix(rows, columns);
- java.util.Random r = new java.util.Random();
for (int i = 0; i < rows * columns; i++) {
- m.data[i] = r.nextDouble();
+ m.data[i] = (double) Random.nextDouble();
}
return m;
@@ -453,9 +454,8 @@ public static DoubleMatrix rand(int len) {
public static DoubleMatrix randn(int rows, int columns) {
DoubleMatrix m = new DoubleMatrix(rows, columns);
- java.util.Random r = new java.util.Random();
for (int i = 0; i < rows * columns; i++) {
- m.data[i] = (double) r.nextGaussian();
+ m.data[i] = (double) Random.nextGaussian();
}
return m;
@@ -904,6 +904,7 @@ public DoubleMatrix put(Range rs, Range cs, DoubleMatrix x) {
x.checkColumns(cs.length());
for (; rs.hasMore(); rs.next()) {
+ cs.init(0, columns);
for (; cs.hasMore(); cs.next()) {
put(rs.value(), cs.value(), x.get(rs.index(), cs.index()));
}
@@ -39,6 +39,8 @@
import org.jblas.exceptions.SizeException;
import org.jblas.ranges.Range;
+import org.jblas.util.Random;
+
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -436,9 +438,8 @@ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundEx
public static FloatMatrix rand(int rows, int columns) {
FloatMatrix m = new FloatMatrix(rows, columns);
- java.util.Random r = new java.util.Random();
for (int i = 0; i < rows * columns; i++) {
- m.data[i] = r.nextFloat();
+ m.data[i] = (float) Random.nextFloat();
}
return m;
@@ -453,9 +454,8 @@ public static FloatMatrix rand(int len) {
public static FloatMatrix randn(int rows, int columns) {
FloatMatrix m = new FloatMatrix(rows, columns);
- java.util.Random r = new java.util.Random();
for (int i = 0; i < rows * columns; i++) {
- m.data[i] = (float) r.nextGaussian();
+ m.data[i] = (float) Random.nextGaussian();
}
return m;
@@ -904,6 +904,7 @@ public FloatMatrix put(Range rs, Range cs, FloatMatrix x) {
x.checkColumns(cs.length());
for (; rs.hasMore(); rs.next()) {
+ cs.init(0, columns);
for (; cs.hasMore(); cs.next()) {
put(rs.value(), cs.value(), x.get(rs.index(), cs.index()));
}
@@ -0,0 +1,33 @@
+package org.jblas.util;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: mikio
+ * Date: 6/24/11
+ * Time: 10:45 AM
+ * To change this template use File | Settings | File Templates.
+ */
+
+public class Random {
+ private static java.util.Random r = new java.util.Random();
+
+ public static void seed(long newSeed) {
+ r = new java.util.Random(newSeed);
+ }
+
+ public static double nextDouble() {
+ return r.nextDouble();
+ }
+
+ public static float nextFloat() {
+ return r.nextFloat();
+ }
+
+ public static int nextInt(int max) {
+ return r.nextInt(max);
+ }
+
+ public static double nextGaussian() {
+ return r.nextGaussian();
+ }
+}
@@ -39,6 +39,8 @@
import java.io.File;
import java.io.PrintStream;
import junit.framework.TestCase;
+import org.jblas.util.Random;
+
import java.util.Arrays;
import static org.jblas.ranges.RangeUtils.*;
@@ -640,8 +642,24 @@ public void testLoadAsciiFile() {
}
public void testRanges() {
- // Hm... Broken?
- //System.out.printf("Ranges: %s\n", A.get(interval(0, 2), interval(0, 1)).toString());
- //assertEquals(new DoubleMatrix(3, 2, 1.0, 2.0, 3.0, 5.0, 6.0, 7.0), );
+ DoubleMatrix A = new DoubleMatrix(3, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
+ DoubleMatrix B = new DoubleMatrix(2, 3, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0);
+
+ A.put(interval(0, 2), interval(0, 3), B);
+
+ /*assertEquals(-1.0, A.get(0, 0));
+ assertEquals(-2.0, A.get(0, 1));
+ assertEquals(-3.0, A.get(0, 2));
+ assertEquals(-4.0, A.get(1, 0));
+ assertEquals(-5.0, A.get(1, 1));
+ assertEquals(-6.0, A.get(1, 2));*/
+ }
+
+ public void testRandWithSeed() {
+ Random.seed(1);
+ DoubleMatrix A = DoubleMatrix.rand(3, 3);
+ Random.seed(1);
+ DoubleMatrix B = DoubleMatrix.rand(3, 3);
+ assertEquals(0.0, A.sub(B).normmax(), 1e-9);
}
}
@@ -39,6 +39,8 @@
import java.io.File;
import java.io.PrintStream;
import junit.framework.TestCase;
+import org.jblas.util.Random;
+
import java.util.Arrays;
import static org.jblas.ranges.RangeUtils.*;
@@ -640,8 +642,24 @@ public void testLoadAsciiFile() {
}
public void testRanges() {
- // Hm... Broken?
- //System.out.printf("Ranges: %s\n", A.get(interval(0, 2), interval(0, 1)).toString());
- //assertEquals(new FloatMatrix(3, 2, 1.0f, 2.0f, 3.0f, 5.0f, 6.0f, 7.0f), );
+ FloatMatrix A = new FloatMatrix(3, 3, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);
+ FloatMatrix B = new FloatMatrix(2, 3, -1.0f, -2.0f, -3.0f, -4.0f, -5.0f, -6.0f);
+
+ A.put(interval(0, 2), interval(0, 3), B);
+
+ /*assertEquals(-1.0f, A.get(0, 0));
+ assertEquals(-2.0f, A.get(0, 1));
+ assertEquals(-3.0f, A.get(0, 2));
+ assertEquals(-4.0f, A.get(1, 0));
+ assertEquals(-5.0f, A.get(1, 1));
+ assertEquals(-6.0f, A.get(1, 2));*/
+ }
+
+ public void testRandWithSeed() {
+ Random.seed(1);
+ FloatMatrix A = FloatMatrix.rand(3, 3);
+ Random.seed(1);
+ FloatMatrix B = FloatMatrix.rand(3, 3);
+ assertEquals(0.0f, A.sub(B).normmax(), 1e-9);
}
}

0 comments on commit 7b84e45

Please sign in to comment.