assign3/mat.cpp
// returns the covariance between two matrices
// WARNING: This is NOT the unbiased covariance in which
// you divide by (n - 1)!  In this routine we divide by n.
// This matrix is not necessarily symmetric!
//
// WARNING: allocates new matrix for answer
returns the covariance between two matrices
WARNING: This is NOT the unbiased covariance in which
you divide by (n - 1).  In this routine we divide by n.
This matrix is not necessarily symmetric!

allocates new matrix for answer
returns the covariance between two matrices WARNING : This is NOT the unbiased covariance in which you divide by ( n - 1 ) . In this routine we divide by n . This matrix is not necessarily symmetric! allocates new matrix for answer
Matrix Matrix::cov(Matrix &other)
{
    assertDefined("cov");
    assertRowsEqual(other, "cov");
    double inv;
    double *mean, *meano;
    mean = new double [maxc];
    for (int c=0; c<maxc; c++) {
        double sum;
        sum = 0;
        for (int r=0; r<maxr; r++) {
            sum += m[r][c];
        }
        mean[c] = sum/maxr;
    }
    meano = new double [other.maxc];
    for (int c=0; c<other.maxc; c++) {
        double sum;
        sum = 0;
        for (int r=0; r<other.maxr; r++) {
            sum += other.m[r][c];
        }
        meano[c] = sum/maxr;
    }
    Matrix out(maxc, other.maxc);
    inv = 1.0/maxr;
    for (int r=0; r<maxc; r++) {
        for (int c=0; c<other.maxc; c++) {
            double sum;
            sum = 0;
            for (int i=0; i<maxr; i++) {       
                sum += (m[i][r]-mean[r]) * (other.m[i][c]-meano[c]);  
            }
            out.m[r][c] = sum * inv;
        }
    }
    out.defined = true;
    delete [] mean;
    delete [] meano;
    return out;
}
Matrix Matrix :: cov ( Matrix & other ) { assertDefined ( " " ) ; assertRowsEqual ( other , " " ) ; double inv ; double * mean , * meano ; mean = new double [ maxc ] ; for ( int c = 0 ; c < maxc ; c ++ ) { double sum ; sum = 0 ; for ( int r = 0 ; r < maxr ; r ++ ) { sum += m [ r ] [ c ] ; } mean [ c ] = sum / maxr ; } meano = new double [ other . maxc ] ; for ( int c = 0 ; c < other . maxc ; c ++ ) { double sum ; sum = 0 ; for ( int r = 0 ; r < other . maxr ; r ++ ) { sum += other . m [ r ] [ c ] ; } meano [ c ] = sum / maxr ; } Matrix out ( maxc , other . maxc ) ; inv = 1.0 / maxr ; for ( int r = 0 ; r < maxc ; r ++ ) { for ( int c = 0 ; c < other . maxc ; c ++ ) { double sum ; sum = 0 ; for ( int i = 0 ; i < maxr ; i ++ ) { sum += ( m [ i ] [ r ] - mean [ r ] ) * ( other . m [ i ] [ c ] - meano [ c ] ) ; } out . m [ r ] [ c ] = sum * inv ; } } out . defined = true ; delete [ ] mean ; delete [ ] meano ; return out ; }
returns the covariance between two matrices
WARNING: This is NOT the unbiased covariance in which
you divide by (n - 1)!
returns the covariance between two matrices WARNING : This is NOT the unbiased covariance in which you divide by ( n - 1 ) !
// sum over rows // go down the transpose //            out.m[r][c] = out.m[c][r] = sum * inv;