Search in sources :

Example 61 with DBIDArrayIter

use of de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter in project elki by elki-project.

the class RelationUtil method relationAsMatrix.

/**
 * <em>Copy</em> a relation into a double matrix.
 *
 * This is <em>not recommended</em> unless you need to modify the data
 * temporarily.
 *
 * @param relation Relation
 * @param ids IDs, with well-defined order (i.e. array)
 * @return Data matrix
 */
public static double[][] relationAsMatrix(final Relation<? extends NumberVector> relation, ArrayDBIDs ids) {
    final int rowdim = ids.size();
    final int coldim = dimensionality(relation);
    double[][] mat = new double[rowdim][coldim];
    int r = 0;
    for (DBIDArrayIter iter = ids.iter(); iter.valid(); iter.advance(), r++) {
        NumberVector vec = relation.get(iter);
        double[] row = mat[r];
        for (int c = 0; c < coldim; c++) {
            row[c] = vec.doubleValue(c);
        }
    }
    assert (r == rowdim);
    return mat;
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)

Example 62 with DBIDArrayIter

use of de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter in project elki by elki-project.

the class WeightedQuickUnionRangeDBIDsTest method testTree.

@Test
public void testTree() {
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(8);
    UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(7)));
    uf.union(i1.seek(0), i2.seek(1));
    assertTrue(uf.isConnected(i1.seek(0), i2.seek(1)));
    uf.union(i1.seek(2), i2.seek(3));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(2)));
    uf.union(i1.seek(0), i2.seek(2));
    assertTrue(uf.isConnected(i1.seek(3), i2.seek(1)));
    uf.union(i1.seek(4), i2.seek(5));
    uf.union(i1.seek(6), i2.seek(7));
    uf.union(i1.seek(4), i2.seek(6));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(4)));
    uf.union(i1.seek(0), i2.seek(4));
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            assertTrue(uf.isConnected(i1.seek(i), i2.seek(j)));
        }
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Example 63 with DBIDArrayIter

use of de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter in project elki by elki-project.

the class WeightedQuickUnionRangeDBIDsTest method testBruteForce.

@Test
public void testBruteForce() {
    final Random r = new Random(0L);
    final int size = 100;
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(size);
    UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    int[] c = new int[size];
    for (int i = 0; i < size; i++) {
        c[i] = i;
    }
    int numc = size;
    while (numc > 1) {
        // Two randoms, with o1 < o2
        int o2 = r.nextInt(size - 1) + 1, o1 = r.nextInt(o2);
        final int c1 = c[o1], c2 = c[o2];
        final boolean ufc = uf.isConnected(i1.seek(o1), i2.seek(o2));
        assertEquals(c1 == c2, ufc);
        // always
        uf.union(i1, i2);
        if (c1 != c2) {
            for (int j = 0; j < size; j++) {
                if (c[j] == c1) {
                    c[j] = c2;
                }
            }
            --numc;
        }
        assertEquals(numc, uf.getRoots().size());
    }
}
Also used : Random(java.util.Random) DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Example 64 with DBIDArrayIter

use of de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter in project elki by elki-project.

the class WeightedQuickUnionStaticDBIDsTest method testTree.

@Test
public void testTree() {
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(8);
    UnionFind uf = new WeightedQuickUnionStaticDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(7)));
    uf.union(i1.seek(0), i2.seek(1));
    assertTrue(uf.isConnected(i1.seek(0), i2.seek(1)));
    uf.union(i1.seek(2), i2.seek(3));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(2)));
    uf.union(i1.seek(0), i2.seek(2));
    assertTrue(uf.isConnected(i1.seek(3), i2.seek(1)));
    uf.union(i1.seek(4), i2.seek(5));
    uf.union(i1.seek(6), i2.seek(7));
    uf.union(i1.seek(4), i2.seek(6));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(4)));
    uf.union(i1.seek(0), i2.seek(4));
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            assertTrue(uf.isConnected(i1.seek(i), i2.seek(j)));
        }
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Aggregations

DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)64 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)17 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)15 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)15 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)14 DBIDRange (de.lmu.ifi.dbs.elki.database.ids.DBIDRange)13 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)12 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)9 Test (org.junit.Test)9 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)8 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)6 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)5 IOException (java.io.IOException)5 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)4 DBIDVar (de.lmu.ifi.dbs.elki.database.ids.DBIDVar)4 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)4 Cluster (de.lmu.ifi.dbs.elki.data.Cluster)3 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)3 SortDBIDsBySingleDimension (de.lmu.ifi.dbs.elki.data.VectorUtil.SortDBIDsBySingleDimension)3 ClusterModel (de.lmu.ifi.dbs.elki.data.model.ClusterModel)3