private void DoTestSetTrueCount(AbstractSparseBinaryMatrix sm) { int[][] connectedSynapses = new int[][] { new int[] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, new int[] { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } }; for (int i = 0; i < sm.GetDimensions()[0]; i++) { for (int j = 0; j < sm.GetDimensions()[1]; j++) { sm.Set(connectedSynapses[i][j], i, j); } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { Assert.AreEqual(connectedSynapses[i][j], sm.GetIntValue(i, j)); } } for (int i = 0; i < 5; i++) { Assert.AreEqual(2, sm.GetTrueCount(i)); } }
private void DoTestArraySet(AbstractSparseBinaryMatrix sm) { int[] expected = { 1, 0, 0, 0, 1, 0, 1, 1, 1, 0 }; int[] values = { 1, 1, 1, 1, 1 }; int[] indexes = { 0, 4, 6, 7, 8 }; sm.Set(indexes, values); int[] dense = new int[sm.GetMaxIndex() + 1]; for (int i = 0; i < sm.GetMaxIndex() + 1; i++) { dense[i] = sm.GetIntValue(i); } Assert.IsTrue(Arrays.AreEqual(expected, dense)); }
private void DoTestBackingStoreAndSliceAccessManyDimensions(AbstractSparseBinaryMatrix sm) { /*set diagonal element to true*/ sm.Set(1, 0, 0, 0); sm.Set(1, 1, 1, 1); sm.Set(1, 2, 2, 2); sm.Set(1, 3, 3, 3); sm.Set(1, 4, 4, 4); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { if (k == j & j == i) { Assert.AreEqual(1, sm.GetIntValue(i, j, k)); } } } } SparseByteArray slice = (SparseByteArray)sm.GetSlice(4, 4); for (int i = 0; i < 5; i++) { Assert.AreEqual(1, sm.GetTrueCount(i)); } Console.WriteLine("slice:" + ArrayUtils.IntArrayToString(slice)); Assert.AreEqual(1, slice[4]); /*update first row to true, other to false*/ for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { if (0 == i) { sm.Set(1, i, j, k); } else { sm.Set(0, i, j, k); } } } } Assert.AreEqual(25, sm.GetTrueCounts()[0]); Assert.AreEqual(0, sm.GetTrueCounts()[1]); }
private void DoTestBackingStoreAndSliceAccess(AbstractSparseBinaryMatrix sm) { int[][] connectedSynapses = new int[][] { new int[] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, new int[] { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } }; for (int i = 0; i < sm.GetDimensions()[0]; i++) { for (int j = 0; j < sm.GetDimensions()[1]; j++) { sm.Set(connectedSynapses[i][j], i, j); } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { Assert.AreEqual(connectedSynapses[i][j], sm.GetIntValue(i, j)); } } for (int i = 0; i < connectedSynapses.Length; i++) { for (int j = 0; j < connectedSynapses[i].Length; j++) { Assert.AreEqual(connectedSynapses[i][j], ((SparseByteArray)sm.GetSlice(i))[j], 0); } } //Make sure warning is proper for exact access try { sm.GetSlice(0, 4); Assert.Fail(); } catch (Exception e) { Assert.AreEqual("This method only returns the array holding the specified index: " + Arrays.ToString(new int[] { 0, 4 }), e.Message); } }
private void DoTestGetSparseIndices(AbstractSparseBinaryMatrix sm) { int[] expected = { 0, 5, 11, 16, 22, 27, 33, 38, 44, 49 }; int[][] values = new int[][] { new int[] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, new int[] { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } }; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { sm.Set(values[i][j], new int[] { i, j }); } } int[] sdr = sm.GetSparseIndices(); Assert.IsTrue(Arrays.AreEqual(expected, sdr)); }
private void DoTestRightVecSumAtNzFast(AbstractSparseBinaryMatrix sm) { int[] dimensions = new int[] { 5, 10 }; int[][] connectedSynapses = new int[][] { new int[] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, new int[] { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } }; for (int i = 0; i < sm.GetDimensions()[0]; i++) { for (int j = 0; j < sm.GetDimensions()[1]; j++) { sm.Set(connectedSynapses[i][j], i, j); } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { Assert.AreEqual(connectedSynapses[i][j], sm.GetIntValue(i, j)); } } int[] inputVector = new int[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }; int[] results = new int[5]; int[] trueResults = new int[] { 1, 1, 1, 1, 1 }; sm.RightVecSumAtNZ(inputVector, results); for (int i = 0; i < results.Length; i++) { Assert.AreEqual(trueResults[i], results[i]); } /////////////////////// connectedSynapses = new int[][] { new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 }, new int[] { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 } }; sm = new SparseBinaryMatrix(dimensions); for (int i = 0; i < sm.GetDimensions()[0]; i++) { for (int j = 0; j < sm.GetDimensions()[1]; j++) { sm.Set(connectedSynapses[i][j], i, j); } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { Assert.AreEqual(connectedSynapses[i][j], sm.GetIntValue(i, j)); } } inputVector = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; results = new int[5]; trueResults = new int[] { 10, 8, 6, 4, 2 }; sm.RightVecSumAtNZ(inputVector, results); for (int i = 0; i < results.Length; i++) { Assert.AreEqual(trueResults[i], results[i]); } }