double DotProduct(const SparseMatrix& lhs, const SparseMatrix& rhs) { double result = 0.; for (int lQ = 0; lQ < lhs.nrows(); ++lQ) for (int rQ = 0; rQ < lhs.ncols (); ++rQ) if (lhs.allowed(lQ, rQ) && rhs.allowed(lQ, rQ)) result += MatrixDotProduct(lhs.operator_element(lQ, rQ), rhs.operator_element(lQ, rQ)); return result; }
/* x are the indipendent value of the polynomial equation y are the dependent value */ void OrdinaryLeastSquares(matrix *x, dvector *y, dvector *coefficients) { /*y->size = x->row * * Z' = n x m => n = x->col; m = x->row * * Z'Z = m x n * n x m = m x m => m = x->row * * Z'y = * * */ matrix *x_t; /* Z' that is x transposed */ matrix *x_x_t; /* Z'*Z is the product between x and x_t */ matrix *x_x_t_i; /* x_x_t_i inverted matrix (Z' * Z')^-1 */ dvector *x_t_y; /* Z'* y = x->col */ /* transpose x to x_t*/ NewMatrix(&x_t, x->col, x->row); MatrixTranspose(x, x_t); /* Z'*Z */ NewMatrix(&x_x_t, x->col, x->col); MatrixDotProduct(x_t, x, x_x_t); /* (Z'*Z)^-1 */ initMatrix(&x_x_t_i); MatrixInversion(x_x_t, &x_x_t_i); /* create a vector for store the results of Z'y */ NewDVector(&x_t_y, x->col); /* Z'y */ MatrixDVectorDotProduct(x_t, y, x_t_y); /* final data coefficient value */ DVectorResize(&coefficients, x->col); MatrixDVectorDotProduct(x_x_t_i, x_t_y, coefficients); /* for(i = 0; i < 10; i++) printf("%f\n", coefficients->data[i]); */ DelDVector(&x_t_y); DelMatrix(&x_x_t_i); DelMatrix(&x_x_t); DelMatrix(&x_t); }
/* last column must be an integer column which define the classes */ void LDA(matrix *mx, uivector *y, LDAMODEL *lda) { size_t i, j, l, k, cc, imin, imax; array *classes; array *S; matrix *X, *X_T, *Sb, *Sw, *InvSw_Sb; dvector *mutot; dvector *classmu; dvector *evect_, *ldfeature; matrix *covmx; lda->nclass = 0; imin = imax = y->data[0]; for(i = 1; i < y->size; i++){ if(y->data[i] > imax){ imax = y->data[i]; } if(y->data[i] < imin){ imin = y->data[i]; } } /* get the number of classes */ if(imin == 0){ lda->class_start = 0; lda->nclass = imax + 1; } else{ lda->class_start = 1; lda->nclass = imax; } /*printf("nclass %d\n", (int)lda->nclass);*/ /* Copy data */ NewMatrix(&X, mx->row, mx->col); MatrixCopy(mx, &X); MatrixCheck(X); /* for(j = 0; j < mx->col-1; j++){ for(i = 0; i < mx->row; i++){ X->data[i][j] = mx->data[i][j]; } } */ /*create classes of objects */ NewArray(&classes, lda->nclass); UIVectorResize(&lda->classid, mx->row); j = 0; if(imin == 0){ for(k = 0; k < lda->nclass; k++){ cc = 0; for(i = 0; i < X->row; i++){ if(y->data[i] == k) cc++; else continue; } NewArrayMatrix(&classes, k, cc, X->col); cc = 0; for(i = 0; i < X->row; i++){ if(y->data[i] == k){ for(l = 0; l < X->col; l++){ classes->m[k]->data[cc][l] = X->data[i][l]; } lda->classid->data[j] = i; j++; cc++; } else{ continue; } } } } else{ for(k = 0; k < lda->nclass; k++){ cc = 0; for(i = 0; i < X->row; i++){ if(y->data[i] == k+1) cc++; else continue; } NewArrayMatrix(&classes, k, cc, X->col); cc = 0; for(i = 0; i < X->row; i++){ if(y->data[i] == k+1){ for(l = 0; l < X->col; l++){ classes->m[k]->data[cc][l] = X->data[i][l]; } lda->classid->data[j] = i; j++; cc++; } else{ continue; } } } } /*puts("Classes"); PrintArray(classes);*/ /* Compute the prior probability */ for(k = 0; k < classes->order; k++){ DVectorAppend(&lda->pprob, (classes->m[k]->row/(double)X->row)); } /* puts("Prior Probability"); PrintDVector(lda->pprob); */ /*Compute the mean of each class*/ for(k = 0; k < classes->order; k++){ initDVector(&classmu); MatrixColAverage(classes->m[k], &classmu); MatrixAppendRow(&lda->mu, classmu); DelDVector(&classmu); } /*puts("Class Mu"); FindNan(lda->mu); PrintMatrix(lda->mu);*/ /*Calculate the total mean of samples..*/ initDVector(&mutot); MatrixColAverage(X, &mutot); /*puts("Mu tot"); PrintDVector(mutot);*/ /*NewDVector(&mutot, mu->col); for(k = 0; k < mu->row; k++){ for(i = 0; i < mu->col; i++){ mutot->data[i] += mu->data[k][i]; } } for(i = 0; i < mutot->size; i++){ mutot->data[i] /= nclasses; }*/ /*Centering data before computing the scatter matrix*/ for(k = 0; k < classes->order; k++){ for(i = 0; i < classes->m[k]->row; i++){ for(j = 0; j < classes->m[k]->col; j++){ classes->m[k]->data[i][j] -= mutot->data[j]; } } } /* puts("Classes"); for(i = 0; i < classes->order; i++){ FindNan(classes->m[i]); } PrintArray(classes); */ /*Compute the scatter matrix * S = nobj - 1 * covmx */ initArray(&S); NewMatrix(&covmx, X->col, X->col); for(k = 0; k < classes->order; k++){ matrix *m_T; NewMatrix(&m_T, classes->m[k]->col, classes->m[k]->row); MatrixTranspose(classes->m[k], m_T); MatrixDotProduct(m_T, classes->m[k], covmx); for(i = 0; i < covmx->row; i++){ for(j = 0; j < covmx->col; j++){ covmx->data[i][j] /= classes->m[k]->row; } } ArrayAppendMatrix(&S, covmx); MatrixSet(covmx, 0.f); DelMatrix(&m_T); } /* puts("Scatter Matrix"); for(i = 0; i < S->order; i++) FindNan(S->m[i]); PrintArray(S);*/ /* Compute the class scatter which represent the covariance matrix */ NewMatrix(&Sw, X->col, X->col); for(k = 0; k < S->order; k++){ for(i = 0; i < S->m[k]->row; i++){ for(j = 0; j < S->m[k]->col; j++){ Sw->data[i][j] += (double)(classes->m[k]->row/(double)X->row) * S->m[k]->data[i][j]; } } } /* puts("Class scatter matrix Sw"); FindNan(Sw); PrintMatrix(Sw); */ /*Compute the between class scatter matrix Sb*/ NewMatrix(&Sb, X->col, X->col); for(k = 0; k < lda->mu->row; k++){ /*for each class of object*/ cc = classes->m[k]->row; for(i = 0; i < Sb->row; i++){ for(j = 0; j < Sb->col; j++){ Sb->data[i][j] += cc * (lda->mu->data[k][i] - mutot->data[i]) * (lda->mu->data[k][j] - mutot->data[j]); } } } /* puts("Between class scatter matrix Sb"); FindNan(Sb); PrintMatrix(Sb); */ /* Computing the LDA projection */ /*puts("Compute Matrix Inversion");*/ MatrixInversion(Sw, &lda->inv_cov); double ss = 0.f; for(i = 0; i < lda->inv_cov->row; i++){ for(j = 0; j < lda->inv_cov->col; j++){ ss += square(lda->inv_cov->data[i][j]); } if(_isnan_(ss)) break; } if(FLOAT_EQ(ss, 0.f, EPSILON) || _isnan_(ss)){ /*Do SVD as pseudoinversion accordin to Liu et al. because matrix is nonsingular * * JUN LIU et al, Int. J. Patt. Recogn. Artif. Intell. 21, 1265 (2007). DOI: 10.1142/S0218001407005946 * EFFICIENT PSEUDOINVERSE LINEAR DISCRIMINANT ANALYSIS AND ITS NONLINEAR FORM FOR FACE RECOGNITION * * * Sw`^-1 = Q * G^-1 * Q_T * Q G AND Q_T come from SVD */ MatrixPseudoinversion(Sw, &lda->inv_cov); /* NewMatrix(&A_T, Sw->col, Sw->row); MatrixInversion(Sw, &A_T); NewMatrix(&A_T_Sw, A_T->row, Sw->col); MatrixDotProduct(A_T, Sw, A_T_Sw); initMatrix(&A_T_Sw_inv); MatrixInversion(A_T_Sw, &A_T_Sw_inv); MatrixDotProduct(A_T_Sw_inv, A_T, lda->inv_cov); DelMatrix(&A_T); DelMatrix(&A_T_Sw); DelMatrix(&A_T_Sw_inv); */ } /*puts("Inverted Covariance Matrix from Sw"); FindNan(lda->inv_cov); PrintMatrix(lda->inv_cov); */ /*puts("Compute Matrix Dot Product");*/ NewMatrix(&InvSw_Sb, lda->inv_cov->row, Sb->col); MatrixDotProduct(lda->inv_cov, Sb, InvSw_Sb); /*puts("InvSw_Sb"); PrintMatrix(InvSw_Sb);*/ /*puts("Compute Eigenvectors");*/ EVectEval(InvSw_Sb, &lda->eval, &lda->evect); /*EvectEval3(InvSw_Sb, InvSw_Sb->row, &lda->eval, &lda->evect);*/ /*EVectEval(InvSw_Sb, &lda->eval, &lda->evect); */ /* Calculate the new projection in the feature space * * and the multivariate normal distribution */ /* Remove centering data */ for(k = 0; k < classes->order; k++){ for(i = 0; i < classes->m[k]->row; i++){ for(j = 0; j < classes->m[k]->col; j++){ classes->m[k]->data[i][j] += mutot->data[j]; } } } initMatrix(&X_T); for(k = 0; k < classes->order; k++){ /*printf("row %d col %d\n", (int)classes->m[k]->row, (int)classes->m[k]->col);*/ AddArrayMatrix(&lda->features, classes->m[k]->row, classes->m[k]->col); AddArrayMatrix(&lda->mnpdf, classes->m[k]->row, classes->m[k]->col); } NewDVector(&evect_, lda->evect->row); initDVector(&ldfeature); ResizeMatrix(&lda->fmean, classes->order, lda->evect->col); ResizeMatrix(&lda->fsdev, classes->order, lda->evect->col); for(l = 0; l < lda->evect->col; l++){ for(i = 0; i < lda->evect->row; i++){ evect_->data[i] = lda->evect->data[i][l]; } for(k = 0; k < classes->order; k++){ ResizeMatrix(&X_T, classes->m[k]->col, classes->m[k]->row); MatrixTranspose(classes->m[k], X_T); DVectorResize(&ldfeature, classes->m[k]->row); DVectorMatrixDotProduct(X_T, evect_, ldfeature); for(i = 0; i < ldfeature->size; i++){ lda->features->m[k]->data[i][l] = ldfeature->data[i]; } /* Calculate the multivariate normal distribution */ double mean = 0.f, sdev = 0.f; DVectorMean(ldfeature, &mean); DVectorSDEV(ldfeature, &sdev); lda->fmean->data[k][l] = mean; lda->fsdev->data[k][l] = sdev; for(i = 0; i < ldfeature->size; i++){ lda->mnpdf->m[k]->data[i][l] = 1./sqrt(2 * pi* sdev) * exp(-square((ldfeature->data[i] - mean)/sdev)/2.f); } } } DelDVector(&evect_); DelMatrix(&covmx); DelDVector(&ldfeature); DelDVector(&mutot); DelMatrix(&Sb); DelMatrix(&InvSw_Sb); DelArray(&classes); DelArray(&S); DelMatrix(&Sw); DelMatrix(&X_T); DelMatrix(&X); }
void SpinAdapted::MatrixNormalise(Matrix& a) { double norm = MatrixDotProduct(a, a); MatrixScale(1./sqrt(norm), a); }