Example #1
0
int main(int argc, char * argv[])
{
add(2,2);
sub(2,2);
mul(2,2);
add(5,6);
sub(9,7);
mul(9,9);
    
add(12,13);
sub(10,10);


xyc(10,43);
sub(10,10);
sub(101,110);

}
/*************************************************************************
Simple test 1: ellipsoid in NF-dimensional space.
compare k-means centers with random centers
*************************************************************************/
static void simpletest1(int nvars,
     int nc,
     int passcount,
     bool& converrors,
     bool& othererrors,
     bool& simpleerrors)
{
    int npoints;
    int majoraxis;
    ap::real_2d_array xy;
    ap::real_1d_array tmp;
    double v;
    int i;
    int j;
    int info;
    ap::real_2d_array c;
    ap::integer_1d_array xyc;
    int pass;
    int restarts;
    double ekmeans;
    double erandom;
    double dclosest;
    int cclosest;

    npoints = nc*100;
    restarts = 5;
    passcount = 10;
    tmp.setbounds(0, nvars-1);
    for(pass = 1; pass <= passcount; pass++)
    {
        
        //
        // Fill
        //
        xy.setbounds(0, npoints-1, 0, nvars-1);
        majoraxis = ap::randominteger(nvars);
        for(i = 0; i <= npoints-1; i++)
        {
            rsphere(xy, nvars, i);
            xy(i,majoraxis) = nc*xy(i,majoraxis);
        }
        
        //
        // Test
        //
        kmeansgenerate(xy, npoints, nvars, nc, restarts, info, c, xyc);
        if( info<0 )
        {
            converrors = true;
            return;
        }
        
        //
        // Test that XYC is correct mapping to cluster centers
        //
        for(i = 0; i <= npoints-1; i++)
        {
            cclosest = -1;
            dclosest = ap::maxrealnumber;
            for(j = 0; j <= nc-1; j++)
            {
                ap::vmove(&tmp(0), &xy(i, 0), ap::vlen(0,nvars-1));
                ap::vsub(tmp.getvector(0, nvars-1), c.getcolumn(j, 0, nvars-1));
                v = ap::vdotproduct(&tmp(0), &tmp(0), ap::vlen(0,nvars-1));
                if( v<dclosest )
                {
                    cclosest = j;
                    dclosest = v;
                }
            }
            if( cclosest!=xyc(i) )
            {
                othererrors = true;
                return;
            }
        }
        
        //
        // Use first NC rows of XY as random centers
        // (XY is totally random, so it is as good as any other choice).
        //
        // Compare potential functions.
        //
        ekmeans = 0;
        for(i = 0; i <= npoints-1; i++)
        {
            ap::vmove(&tmp(0), &xy(i, 0), ap::vlen(0,nvars-1));
            ap::vsub(tmp.getvector(0, nvars-1), c.getcolumn(xyc(i), 0, nvars-1));
            v = ap::vdotproduct(&tmp(0), &tmp(0), ap::vlen(0,nvars-1));
            ekmeans = ekmeans+v;
        }
        erandom = 0;
        for(i = 0; i <= npoints-1; i++)
        {
            dclosest = ap::maxrealnumber;
            for(j = 0; j <= nc-1; j++)
            {
                ap::vmove(&tmp(0), &xy(i, 0), ap::vlen(0,nvars-1));
                ap::vsub(&tmp(0), &xy(j, 0), ap::vlen(0,nvars-1));
                v = ap::vdotproduct(&tmp(0), &tmp(0), ap::vlen(0,nvars-1));
                if( v<dclosest )
                {
                    dclosest = v;
                }
            }
            erandom = erandom+v;
        }
        if( erandom<ekmeans )
        {
            simpleerrors = true;
            return;
        }
    }
}