Java Code Examples for gnu.trove.list.array.TIntArrayList#addAll()

The following examples show how to use gnu.trove.list.array.TIntArrayList#addAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Topology.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Get the points in the neighborhood of a point.
 *
 * A point's neighborhood is the n-dimensional hypercube with sides ranging
 * [center - radius, center + radius], inclusive. For example, if there are two
 * dimensions and the radius is 3, the neighborhood is 6x6. Neighborhoods are
 * truncated when they are near an edge.
 * 
 * @param centerIndex       The index of the point. The coordinates are expressed as a single index by
 *                          using the dimensions as a mixed radix definition. For example, in dimensions
 *                          42x10, the point [1, 4] is index 1*420 + 4*10 = 460.
 * @param radius            The radius of this neighborhood about the centerIndex.
 * @return  The points in the neighborhood, including centerIndex.
 */
public int[] neighborhood(int centerIndex, int radius) {
    centerPosition = coordinatesFromIndex(centerIndex);
    
    igs = IntStream.range(0, dimensions.length)
        .mapToObj(i -> 
            IntGenerator.of(Math.max(0, centerPosition[i] - radius), 
                Math.min(dimensions[i] - 1, centerPosition[i] + radius) + 1))
        .toArray(IntGenerator[]::new);
   
    List<TIntList> result = new ArrayList<>();
    result.add(new TIntArrayList());
    List<TIntList> interim = new ArrayList<>();
    for(IntGenerator pool : igs) {
        int size = result.size();
        interim.clear();
        interim.addAll(result);
        result.clear();
        for(int x = 0;x < size;x++) {
            TIntList lx = interim.get(x);
            pool.reset();
            for(int y = 0;y < pool.size();y++) {
                int py = pool.next();
                TIntArrayList tl = new TIntArrayList();
                tl.addAll(lx);
                tl.add(py);
                result.add(tl);
            }
        }
    }
    
    return result.stream().mapToInt(tl -> indexFromCoordinates(tl.toArray())).toArray();
}
 
Example 2
Source File: Topology.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Like {@link #neighborhood(int, int)}, except that the neighborhood isn't truncated when it's
 * near an edge. It wraps around to the other side.
 * 
 * @param centerIndex       The index of the point. The coordinates are expressed as a single index by
 *                          using the dimensions as a mixed radix definition. For example, in dimensions
 *                          42x10, the point [1, 4] is index 1*420 + 4*10 = 460.
 * @param radius            The radius of this neighborhood about the centerIndex.
 * @return  The points in the neighborhood, including centerIndex.
 */
public int[] wrappingNeighborhood(int centerIndex, int radius) {
    int[] cp = coordinatesFromIndex(centerIndex);
    
    IntGenerator[] igs = IntStream.range(0, dimensions.length)
        .mapToObj(i -> 
            new IntGenerator(cp[i] - radius, 
                Math.min((cp[i] - radius) + dimensions[i] - 1, cp[i] + radius) + 1))
        .toArray(IntGenerator[]::new);
    
    List<TIntList> result = new ArrayList<>();
    result.add(new TIntArrayList());
    List<TIntList> interim = new ArrayList<>();
    for(int i = 0;i < igs.length;i++) {
        IntGenerator pool = igs[i];
        int size = result.size();
        interim.clear();
        interim.addAll(result);
        result.clear();
        for(int x = 0;x < size;x++) {
            TIntList lx = interim.get(x);
            pool.reset();
            for(int y = 0;y < pool.size();y++) {
                int py = ArrayUtils.modulo(pool.next(), dimensions[i]);
                TIntArrayList tl = new TIntArrayList();
                tl.addAll(lx);
                tl.add(py);
                result.add(tl);
            }
        }
    }
    
    return result.stream().mapToInt(tl -> indexFromCoordinates(tl.toArray())).toArray();
}
 
Example 3
Source File: SpatialPoolerTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testMapPotential1D() {
    setupParameters();
    parameters.setInputDimensions(new int[] { 12 });
    parameters.setColumnDimensions(new int[] { 4 });
    parameters.setPotentialRadius(2);
    parameters.setPotentialPct(1);
    parameters.set(KEY.WRAP_AROUND, false);
    initSP();

    assertEquals(12, mem.getInputDimensions()[0]);
    assertEquals(4, mem.getColumnDimensions()[0]);
    assertEquals(2, mem.getPotentialRadius());

    // Test without wrapAround and potentialPct = 1
    int[] expected = new int[] { 0, 1, 2, 3 };
    int[] mask = sp.mapPotential(mem, 0, false);
    assertTrue(Arrays.equals(expected, mask));

    expected = new int[] { 5, 6, 7, 8, 9 };
    mask = sp.mapPotential(mem, 2, false);
    assertTrue(Arrays.equals(expected, mask));

    // Test with wrapAround and potentialPct = 1
    mem.setWrapAround(true);
    expected = new int[] { 0, 1, 2, 3, 11 };
    mask = sp.mapPotential(mem, 0, true);
    assertTrue(Arrays.equals(expected, mask));

    expected = new int[] { 0, 8, 9, 10, 11 };
    mask = sp.mapPotential(mem, 3, true);
    assertTrue(Arrays.equals(expected, mask));

    // Test with wrapAround and potentialPct < 1
    parameters.setPotentialPct(0.5);
    parameters.set(KEY.WRAP_AROUND, true);
    initSP();

    int[] supersetMask = new int[] { 0, 1, 2, 3, 11 }; 
    mask = sp.mapPotential(mem, 0, true);
    assertEquals(mask.length, 3);
    TIntArrayList unionList = new TIntArrayList(supersetMask);
    unionList.addAll(mask);
    int[] unionMask = ArrayUtils.unique(unionList.toArray());
    assertTrue(Arrays.equals(unionMask, supersetMask));
}
 
Example 4
Source File: ArrayUtils.java    From htm.java with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Sparse or due to the arrays containing the indexes of "on bits",
 * the <em>or</em> of which is equal to the mere combination of the two
 * arguments - eliminating duplicates and sorting.
 *
 * @param arg1
 * @param arg2
 * @return
 */
public static int[] sparseBinaryOr(int[] arg1, int[] arg2) {
    TIntArrayList t = new TIntArrayList(arg1);
    t.addAll(arg2);
    return unique(t.toArray());
}