예제 #1
0
void ComputeMeanAndVariance(Array2d<double>& data,Array2dC<double>& avg,Array2d<double>& cov,const bool subtractMean)
{
    avg.Create(1,data.ncol); avg.Zero();
    cov.Create(data.ncol,data.ncol); cov.Zero();
    for(int i=0;i<data.nrow;i++)
    {
        for(int j=0;j<data.ncol;j++) avg.buf[j] += data.p[i][j];
        for(int j=0;j<data.ncol;j++)
            for(int k=0;k<data.ncol;k++)
                cov.p[j][k] += data.p[i][j]*data.p[i][k];
    }
    const double r = 1.0/data.nrow;
    for(int i=0;i<data.ncol;i++) avg.buf[i] *= r;
    if(subtractMean)
    {
        for(int i=0;i<data.ncol;i++)
            for(int j=0;j<data.ncol;j++)
                cov.p[i][j] = cov.p[i][j]*r-avg.buf[i]*avg.buf[j];
    }
    else
    {
        for(int i=0;i<data.ncol;i++)
            for(int j=0;j<data.ncol;j++)
                cov.p[i][j] = cov.p[i][j]*r;
    }
}
예제 #2
0
void SVD(Array2d<double>& a,double* w,Array2d<double>& v)
{   // w -- signular values; a = UWV', where U is stored in a
    // NOTE: a.nrow >= a.ncol is required
    Array2dC<double*> nr_a(1,a.nrow+1),nr_v(1,a.ncol+1);
    v.Create(a.ncol,a.ncol);
    for(int i=0;i<a.nrow;i++) nr_a.buf[i+1] = a.p[i] - 1;
    for(int i=0;i<a.ncol;i++) nr_v.buf[i+1] = v.p[i] - 1;
    svdcmp(nr_a.buf,a.nrow,a.ncol,w-1,nr_v.buf);
}