Skip to content

Commit

Permalink
Merge pull request #3 from stefan-zobel/1.4.1
Browse files Browse the repository at this point in the history
Merge 1.4.1 branch
  • Loading branch information
stefan-zobel authored Sep 3, 2023
2 parents b38de23 + eccbefd commit c40a863
Show file tree
Hide file tree
Showing 4 changed files with 502 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>jamu</artifactId>
<packaging>jar</packaging>
<version>1.4.0</version>
<version>1.4.1-SNAPSHOT</version>
<name>JAMU</name>
<description>Java Matrix Utilities built on top of Intel MKL</description>
<url>https://github.com/stefan-zobel/JAMU/</url>
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/net/jamu/matrix/TensorD.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,73 @@ public TensorD copy() {
return new TensorD(this);
}

/**
* Randomly permutes the matrices in this tensor in place using a default
* source of randomness. All permutations occur with approximately equal
* probability.
*
* @return this tensor with matrices randomly permuted
* @since 1.4.1
*/
public TensorD shuffle() {
return shuffle(null);
}

/**
* Randomly permutes the matrices in this tensor in place using a default
* source of randomness seeded by the given {@code seed}. All permutations
* occur with approximately equal probability.
*
* @param seed the initial seed to use for the PRNG
* @return this tensor with matrices randomly permuted
* @since 1.4.1
*/
public TensorD shuffle(long seed) {
return shuffle(new XoShiRo256StarStar(seed));
}

/**
* Rescales all elements in this tensor into the range
* {@code [lowerBound, upperBound]}.
*
* @param lowerBound
* the minimum value of an element after rescaling
* @param upperBound
* the maximum value of an element after rescaling
* @return this tensor with all elements rescaled in-place
*/
public TensorD rescaleInplace(double lowerBound, double upperBound) {
double[] _a = a;
double _min = Double.MAX_VALUE;
double _max = -Double.MAX_VALUE;
for (int i = 0; i < _a.length; ++i) {
double x = a[i];
if (x < _min) {
_min = x;
}
if (x > _max) {
_max = x;
}
}
double scale = upperBound - lowerBound;
double dataScale = (_min == _max) ? Double.MIN_NORMAL : (_max - _min);
for (int i = 0; i < _a.length; ++i) {
a[i] = lowerBound + (((a[i] - _min) * scale) / dataScale);
}
return this;
}

private TensorD shuffle(XoShiRo256StarStar rng) {
int _stride = stride();
double[] _a = a;
double[] tmp = new double[_stride];
XoShiRo256StarStar rnd = (rng == null) ? new XoShiRo256StarStar() : rng;
for (int i = depth; i > 1; --i) {
swap((i - 1) * _stride, _a, tmp, _stride, rnd.nextInt(i) * _stride);
}
return this;
}

private TensorD create(int rows, int cols, int depth) {
return new TensorD(rows, cols, depth);
}
Expand All @@ -788,4 +855,12 @@ private double[] copyForAppend(double[] newArray) {
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}

private static void swap(int aoff1, double[] a, double[] tmp, int len, int aoff2) {
if (aoff1 != aoff2) {
System.arraycopy(a, aoff1, tmp, 0, len);
System.arraycopy(a, aoff2, a, aoff1, len);
System.arraycopy(tmp, 0, a, aoff2, len);
}
}
}
75 changes: 75 additions & 0 deletions src/main/java/net/jamu/matrix/TensorF.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,73 @@ public TensorF copy() {
return new TensorF(this);
}

/**
* Randomly permutes the matrices in this tensor in place using a default
* source of randomness. All permutations occur with approximately equal
* probability.
*
* @return this tensor with matrices randomly permuted
* @since 1.4.1
*/
public TensorF shuffle() {
return shuffle(null);
}

/**
* Randomly permutes the matrices in this tensor in place using a default
* source of randomness seeded by the given {@code seed}. All permutations
* occur with approximately equal probability.
*
* @param seed the initial seed to use for the PRNG
* @return this tensor with matrices randomly permuted
* @since 1.4.1
*/
public TensorF shuffle(long seed) {
return shuffle(new XoShiRo256StarStar(seed));
}

/**
* Rescales all elements in this tensor into the range
* {@code [lowerBound, upperBound]}.
*
* @param lowerBound
* the minimum value of an element after rescaling
* @param upperBound
* the maximum value of an element after rescaling
* @return this tensor with all elements rescaled in-place
*/
public TensorF rescaleInplace(float lowerBound, float upperBound) {
float[] _a = a;
float _min = Float.MAX_VALUE;
float _max = -Float.MAX_VALUE;
for (int i = 0; i < _a.length; ++i) {
float x = a[i];
if (x < _min) {
_min = x;
}
if (x > _max) {
_max = x;
}
}
float scale = upperBound - lowerBound;
float dataScale = (_min == _max) ? Float.MIN_NORMAL : (_max - _min);
for (int i = 0; i < _a.length; ++i) {
a[i] = lowerBound + (((a[i] - _min) * scale) / dataScale);
}
return this;
}

private TensorF shuffle(XoShiRo256StarStar rng) {
int _stride = stride();
float[] _a = a;
float[] tmp = new float[_stride];
XoShiRo256StarStar rnd = (rng == null) ? new XoShiRo256StarStar() : rng;
for (int i = depth; i > 1; --i) {
swap((i - 1) * _stride, _a, tmp, _stride, rnd.nextInt(i) * _stride);
}
return this;
}

private TensorF create(int rows, int cols, int depth) {
return new TensorF(rows, cols, depth);
}
Expand All @@ -788,4 +855,12 @@ private float[] copyForAppend(float[] newArray) {
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}

private static void swap(int aoff1, float[] a, float[] tmp, int len, int aoff2) {
if (aoff1 != aoff2) {
System.arraycopy(a, aoff1, tmp, 0, len);
System.arraycopy(a, aoff2, a, aoff1, len);
System.arraycopy(tmp, 0, a, aoff2, len);
}
}
}
Loading

0 comments on commit c40a863

Please sign in to comment.