Camera::Camera() { position = Vec3(150, 100, -112); rotation = Vec3(0, 0, 0); lookAt = Vec3(75,75,1); #ifndef _WIN32 // Send pointers to camera matrices to graphic engine GraphicsOGL4::getInstance()->updateProjectionMatrix(&projectionMatrix); GraphicsOGL4::getInstance()->updateViewMatrix(&viewMatrix); GraphicsOGL4::getInstance()->updateViewInverseMatrix(&viewInv); GraphicsOGL4::getInstance()->updateProjectionInverseMatrix(&projectionInv); #endif perspectiveFovLH(projectionMatrix, (float)PI * 0.5, (float)SCRWIDTH / SCRHEIGHT, 0.01f, 600.f); MatrixInversion(projectionInv, projectionMatrix); #ifdef _WIN32 CBOnce cbonce; cbonce.projection = projectionMatrix; cbonce.projectionInv = projectionInv; cbonce.lightPos = Vec4(500, 1000, -500, 1); cbonce.resolution = Vec2(SCRWIDTH, SCRHEIGHT); GraphicsDX11::getInstance()->updateCBOnce(cbonce); #endif //_ WIN32 }
int main(int argc, const char * argv[]) { int size=3; //Definir tamaño float **matrix = new float*[size]; for(int i =0;i<size;++i) matrix[i]=new float[size]; matrix[0][0]=0; matrix[0][1]=2; matrix[0][2]=0; matrix[1][0]=2; matrix[1][1]=1; matrix[1][2]=1; matrix[2][0]=2; matrix[2][1]=1; matrix[2][2]=2; //LLENAMOS LA MATRIX COMO EL EJEMPLO DEL PDF std::cout<<CalcDeterminant(matrix, size)<<"\n"; //PRINTEAMOS DETERMINANTE float **inv=MatrixInversion(matrix, size); //El apuntador Inv se llena aqui std::cout<<"\nOriginal\n"; //PRINTEAR MATRIX for(int i =0;i<size;++i) { for(int j =0;j<size;++j) std::cout<<matrix[i][j]<<"\t"; std::cout<<"\n"; } //PRINTEAR MATRIX INV std::cout<<"\nInversa\n"; for(int i =0;i<size;++i) { for(int j =0;j<size;++j) std::cout<<inv[i][j]<<"\t"; std::cout<<"\n"; } return 0; }
/* 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); }
void Camera::invertViewMatrix(){ for(int i=0; i<4 ; i++){ for(int j=0; j<4 ; j++){ matrixTemp[i][j]=viewMatrix[i+(4*j)]; } } MatrixInversion(matrixTemp,4,inverseTemp); for(int i=0; i<4 ; i++){ for(int j=0; j<4 ; j++){ inverseViewMatrix[i+(4*j)] = inverseTemp[i][j]; } } }
void Camera::update() { Vec3 up, pos, rot; Matrix rotationMatrix; float radianConv = (float)(PI)/180; //Used to convert from degree to radians //Setup up-, pos- and look-vectors up = Vec3(0,1,0); pos = position; //Set yaw, pitch and roll rotations in radians rot.x = rotation.x * radianConv; rot.y = rotation.y * radianConv; rot.z = rotation.z * radianConv; //Create rotation matrix rotationMatrix = yawPitchRoll(rot); //Transform lookAt and up vector by rotation matrix transformCoord(lookAt, lookAt, rotationMatrix); transformCoord(up, up, rotationMatrix); //Translate rotated camera position to location of viewer lookAt = pos + Vec3(0,0,1); //Create view matrix from vectors lookAtLHP(viewMatrix, lookAt, up, pos); MatrixInversion(viewInv, viewMatrix); #ifdef _WIN32 CBCameraMove cb; cb.cameraPos = pos; cb.cameraDir = lookAt - pos; cb.View = viewMatrix; cb.ViewInv = viewInv; GraphicsDX11::getInstance()->updateCBCameraMove(cb); #endif }
void CVICOSFirstStep::CalcInversedMatrix() { CVICOSContext* pSetup = GetSetup(); //CProblem* pProb = pSetup->GetProblem(); long iDim = pSetup->GetDim(); time tStart = pSetup->GetStart(), tFinish = pSetup->GetFinish(), tStep = pSetup->GetNStep(); long iStepCount = long((tFinish - tStart) / tStep) + 1; m_Xi.Create(m_X.GetCount()); for (long i = 0; i <= iStepCount; i++) { matrixpoint &dest = m_Xi[i], &src = m_X[i]; dest.Create(iDim, iDim); if (iDim == 1) { dest(0, 0) = 1 / src(0, 0); } else { MatrixInversion(src.GetData(), iDim, dest.GetData()); } std::cout << "t=" << (tStart + tStep * i) << std::endl; std::cout << "imatrix=" << std::endl << m_Xi[i] << std::endl << std::endl; } }
/* 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); }
RankFourTensor RankFourTensor::invSymm() { int error; double *mat; RankFourTensor result; unsigned int ntens = N*(N+1)/2; int nskip = N-1; // We use the LAPACK matrix inversion routine here. Form the matrix // // mat[0] mat[1] mat[2] mat[3] mat[4] mat[5] // mat[6] mat[7] mat[8] mat[9] mat[10] mat[11] // mat[12] mat[13] mat[14] mat[15] mat[16] mat[17] // mat[18] mat[19] mat[20] mat[21] mat[22] mat[23] // mat[24] mat[25] mat[26] mat[27] mat[28] mat[29] // mat[30] mat[31] mat[32] mat[33] mat[34] mat[35] // // This is filled from the indpendent components of C assuming // the symmetry C_ijkl = C_ijlk = C_jikl. // // If there are two rank-four tensors X and Y then the reason for // this filling becomes apparent if we want to calculate // X_ijkl*Y_klmn = Z_ijmn // For denote the "mat" versions of X, Y and Z by x, y and z. // Then // z_ab = x_ac*y_cb // Eg // z_00 = Z_0000 = X_0000*Y_0000 + X_0011*Y_1111 + X_0022*Y_2200 + 2*X_0001*Y_0100 + 2*X_0002*Y_0200 + 2*X_0012*Y_1200 (the factors of 2 come from the assumed symmetries) // z_03 = 2*Z_0001 = X_0000*2*Y_0001 + X_0011*2*Y_1101 + X_0022*2*Y_2201 + 2*X_0001*2*Y_0101 + 2*X_0002*2*Y_0201 + 2*X_0012*2*Y_1201 // z_22 = 2*Z_0102 = X_0100*2*Y_0002 + X_0111*2*X_1102 + X_0122*2*Y_2202 + 2*X_0101*2*Y_0102 + 2*X_0102*2*Y_0202 + 2*X_0112*2*Y_1202 // Finally, we use LAPACK to find x^-1, and put it back into rank-4 tensor form // // mat[0] = C(0,0,0,0) // mat[1] = C(0,0,1,1) // mat[2] = C(0,0,2,2) // mat[3] = C(0,0,0,1)*2 // mat[4] = C(0,0,0,2)*2 // mat[5] = C(0,0,1,2)*2 // mat[6] = C(1,1,0,0) // mat[7] = C(1,1,1,1) // mat[8] = C(1,1,2,2) // mat[9] = C(1,1,0,1)*2 // mat[10] = C(1,1,0,2)*2 // mat[11] = C(1,1,1,2)*2 // mat[12] = C(2,2,0,0) // mat[13] = C(2,2,1,1) // mat[14] = C(2,2,2,2) // mat[15] = C(2,2,0,1)*2 // mat[16] = C(2,2,0,2)*2 // mat[17] = C(2,2,1,2)*2 // mat[18] = C(0,1,0,0) // mat[19] = C(0,1,1,1) // mat[20] = C(0,1,2,2) // mat[21] = C(0,1,0,1)*2 // mat[22] = C(0,1,0,2)*2 // mat[23] = C(0,1,1,2)*2 // mat[24] = C(0,2,0,0) // mat[25] = C(0,2,1,1) // mat[26] = C(0,2,2,2) // mat[27] = C(0,2,0,1)*2 // mat[28] = C(0,2,0,2)*2 // mat[29] = C(0,2,1,2)*2 // mat[30] = C(1,2,0,0) // mat[31] = C(1,2,1,1) // mat[32] = C(1,2,2,2) // mat[33] = C(1,2,0,1)*2 // mat[34] = C(1,2,0,2)*2 // mat[35] = C(1,2,1,2)*2 mat=(double*)calloc(ntens*ntens, sizeof(double)); for (unsigned int i = 0; i < N; i++) for (unsigned int j = 0; j < N; j++) for (unsigned int k = 0; k < N; k++) for (unsigned int l = 0; l < N; l++) { if (i==j) { if (k==l) mat[i*ntens+k] = _vals[i][j][k][l]; else mat[i*ntens+nskip+k+l] += _vals[i][j][k][l]; } else // i!=j { if (k==l) mat[(nskip+i+j)*ntens+k] = _vals[i][j][k][l]; else mat[(nskip+i+j)*ntens+nskip+k+l] += _vals[i][j][k][l]; // note the +=, which results in double-counting and is rectified below } } for (unsigned int i = 3; i < ntens; i++) for (unsigned int j = 3; j < ntens; j++) mat[i*ntens+j] /= 2.0; // because of double-counting above // use LAPACK to find the inverse error=MatrixInversion(mat, ntens); if (error != 0) mooseError("Error in Matrix Inversion in RankFourTensor"); // build the resulting rank-four tensor // using the inverse of the above algorithm for (unsigned int i = 0; i < N; i++) for (unsigned int j = 0; j < N; j++) for (unsigned int k = 0; k < N; k++) for (unsigned int l = 0; l < N; l++) { if (i==j) { if (k==l) result(i,j,k,l)=mat[i*ntens+k]; else result(i,j,k,l)=mat[i*ntens+nskip+k+l]/2.0; } else // i!=j { if (k==l) result(i,j,k,l)=mat[(nskip+i+j)*ntens+k]; else result(i,j,k,l)=mat[(nskip+i+j)*ntens+nskip+k+l]/2.0; } } free(mat); return result; }