Skip to content

Commit

Permalink
Add tests for sparse tensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdwatters committed Aug 12, 2024
1 parent a21d7ac commit 6288165
Show file tree
Hide file tree
Showing 7 changed files with 1,249 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/flag4j/arrays/sparse/CooCTensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public CooCTensor set(CNumber value, int...index) {
// Copy old indices and insert new one.
int[][] newIndices = new int[indices.length + 1][getRank()];
ArrayUtils.deepCopy(indices, newIndices);
newIndices[indices.length + 1] = index;
newIndices[indices.length] = index;

// Copy old entries and insert new one.
CNumber[] newEntries = Arrays.copyOf(entries, entries.length+1);
Expand Down Expand Up @@ -674,7 +674,7 @@ public CooCTensor T() {
*/
@Override
public CNumber get(int... indices) {
ParameterChecks.assertEquals(indices.length, getRank());
ParameterChecks.assertValidIndex(shape, indices);

for(int i = 0; i < nnz; i++) {
if(Arrays.equals(this.indices[i], indices)) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/flag4j/arrays/sparse/CooTensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,13 @@ public CooTensor set(double value, int... index) {
// Copy old indices and insert new one.
int[][] newIndices = new int[indices.length + 1][getRank()];
ArrayUtils.deepCopy(indices, newIndices);
newIndices[indices.length + 1] = index;
newIndices[indices.length] = index;

// Copy old entries and insert new one.
double[] newEntries = Arrays.copyOf(entries, entries.length+1);
newEntries[newEntries.length-1] = value;

// Ensure indices are sorted.
dest = new CooTensor(shape, newEntries, newIndices);
dest.sortIndices();
}
Expand Down Expand Up @@ -658,7 +659,7 @@ public CooTensor T() {
*/
@Override
public Double get(int... indices) {
ParameterChecks.assertEquals(indices.length, getRank());
ParameterChecks.assertValidIndex(shape, indices);

for(int i = 0; i < nnz; i++) {
if(Arrays.equals(this.indices[i], indices)) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.flag4j.sparse_complex_tensor;

import org.flag4j.arrays.sparse.CooCTensor;
import org.flag4j.complex_numbers.CNumber;
import org.flag4j.core.Shape;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CooCTensorGetSetTests {
static CooCTensor A;
static Shape aShape;
static CNumber[] aEntries;
static int[][] aIndices;

static CooCTensor exp;
static Shape expShape;
static CNumber[] expEntries;
static int[][] expIndices;


@Test
void cooTensorSetTest() {
aShape = new Shape(3, 4, 5, 1);
aEntries = new CNumber[]{
new CNumber(1, 2), new CNumber(0.2324, -239.1),
new CNumber(5.1), new CNumber(0, 18.2)};
aIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
A = new CooCTensor(aShape, aEntries, aIndices);

// --------------- Sub-case 1 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new CNumber[]{
new CNumber(1, 2), new CNumber(2.41, -23.23),
new CNumber(5.1), new CNumber(0, 18.2)};
expIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooCTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(new CNumber(2.41, -23.23), 1, 2, 0, 0));

// --------------- Sub-case 2 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new CNumber[]{
new CNumber(1, 2), new CNumber(0.2324, -239.1), new CNumber(0, -9.2),
new CNumber(5.1), new CNumber(0, 18.2)};
expIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {1, 2, 4, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooCTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(new CNumber(0, -9.2), 1, 2, 4, 0));

// --------------- Sub-case 3 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new CNumber[]{
new CNumber(0.001), new CNumber(1, 2), new CNumber(0.2324, -239.1),
new CNumber(5.1), new CNumber(0, 18.2)};
expIndices = new int[][]{{0, 0, 0, 0}, {0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooCTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(0.001, 0, 0, 0, 0));

// --------------- Sub-case 4 ---------------
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, -1, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 0, 0, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 1, 2, 5, 0));
}


@Test
void cooTensorGetTest() {
aShape = new Shape(3, 4, 5, 1);
aEntries = new CNumber[]{
new CNumber(1, 2), new CNumber(0.2324, -239.1),
new CNumber(5.1), new CNumber(0, 18.2)};
aIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
A = new CooCTensor(aShape, aEntries, aIndices);

// --------------- Sub-case 1 ---------------
assertEquals(new CNumber(0.2324, -239.1), A.get(1, 2, 0, 0));

// --------------- Sub-case 2 ---------------
assertEquals(CNumber.ZERO, A.get(1, 2, 4, 0));

// --------------- Sub-case 3 ---------------
assertEquals(CNumber.ZERO, A.get(0, 0, 0, 0));

// --------------- Sub-case 4 ---------------
assertEquals(new CNumber(1, 2), A.get(0, 1, 0, 0));

// --------------- Sub-case 5 ---------------
assertEquals(new CNumber(5.1), A.get(2, 3, 2, 0));

// --------------- Sub-case 6 ---------------
assertThrows(IndexOutOfBoundsException.class, () -> A.get(-1, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(0, 0, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(1, 2, 5, 0));
}
}
91 changes: 91 additions & 0 deletions src/test/java/org/flag4j/sparse_tensor/CooTensorGetSetTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.flag4j.sparse_tensor;

import org.flag4j.arrays.sparse.CooTensor;
import org.flag4j.core.Shape;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CooTensorGetSetTests {
static CooTensor A;
static Shape aShape;
static double[] aEntries;
static int[][] aIndices;

static CooTensor exp;
static Shape expShape;
static double[] expEntries;
static int[][] expIndices;

static double expScalar;


@Test
void cooTensorSetTest() {
aShape = new Shape(3, 4, 5, 1);
aEntries = new double[]{1, 223.1333, -0.991233, 100.1234};
aIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
A = new CooTensor(aShape, aEntries, aIndices);

// --------------- Sub-case 1 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new double[]{1, -451.2, -0.991233, 100.1234};
expIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(-451.2, 1, 2, 0, 0));

// --------------- Sub-case 2 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new double[]{1, 223.1333, 32.1, -0.991233, 100.1234};
expIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {1, 2, 4, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(32.1, 1, 2, 4, 0));

// --------------- Sub-case 3 ---------------
expShape = new Shape(3, 4, 5, 1);
expEntries = new double[]{0.001, 1, 223.1333, -0.991233, 100.1234};
expIndices = new int[][]{{0, 0, 0, 0}, {0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
exp = new CooTensor(expShape, expEntries, expIndices);

assertEquals(exp, A.set(0.001, 0, 0, 0, 0));

// --------------- Sub-case 4 ---------------
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, -1, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 0, 0, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.set(5, 1, 2, 5, 0));
}


@Test
void cooTensorGetTest() {
aShape = new Shape(3, 4, 5, 1);
aEntries = new double[]{1, 223.1333, -0.991233, 100.1234};
aIndices = new int[][]{{0, 1, 0, 0}, {1, 2, 0, 0}, {2, 3, 2, 0}, {2, 3, 4, 1}};
A = new CooTensor(aShape, aEntries, aIndices);

// --------------- Sub-case 1 ---------------
assertEquals(223.1333, A.get(1, 2, 0, 0));

// --------------- Sub-case 2 ---------------
assertEquals(0, A.get(1, 2, 4, 0));

// --------------- Sub-case 3 ---------------
assertEquals(0, A.get(0, 0, 0, 0));

// --------------- Sub-case 4 ---------------
assertEquals(1, A.get(0, 1, 0, 0));

// --------------- Sub-case 5 ---------------
assertEquals(-0.991233, A.get(2, 3, 2, 0));

// --------------- Sub-case 6 ---------------
assertThrows(IndexOutOfBoundsException.class, () -> A.get(-1, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(0, 0, 0, 0, 0));
assertThrows(IndexOutOfBoundsException.class, () -> A.get(1, 2, 5, 0));
}
}
Loading

0 comments on commit 6288165

Please sign in to comment.