f32 Matrix3x3::determinant() const { // Laplace expansion by 1th column f32 ret=m[0][0]*cofactor(0,0)+ -m[0][1]*cofactor(1,0)+ m[0][2]*cofactor(2,0); return ret; }
Real Matrix4::determinant() const { Real a = data[0][0] * cofactor(0,0); Real b = data[0][1] * cofactor(0,1); Real c = data[0][2] * cofactor(0,2); Real d = data[0][3] * cofactor(0,3); return a - b + c - d; }
T Matrix<rows, cols, T>::determinant() const { if (rows != cols) { // Makes no sense! Abort! throw(std::domain_error("Determinant for nonsquare matrix is undefined.")); } return((*this)(0, 0) * cofactor(0, 0) + (*this)(0, 1) * cofactor(0, 1) + (*this)(0, 2) * cofactor(0, 2)); }
int main() { float a[25][25],k,d; int i,j; printf("-------------------------------------------------------------\n"); printf("----------------made by C code champ ------------------------\n"); printf("-------------------------------------------------------------\n"); printf("\n C Program to find inverse of Matrix\n\n"); printf("Enter the order of the Matrix : "); scanf("%f",&k); printf("Enter the elements of %.0fX%.0f Matrix : \n",k,k); for (i=0;i<k;i++) { for (j=0;j<k;j++) { scanf("%f",&a[i][j]); } } d=determinant(a,k); printf("Determinant of the Matrix = %f",d); if (d==0) printf("\nInverse of Entered Matrix is not possible\n"); else cofactor(a,k); printf("\n\n**** Thanks for using the program!!! ****"); getch(); }
int main() { float*a,d; int k; int i,j; printf("-------------------------------------------------------------\n"); //printf("----------------made by C code champ ------------------------\n"); //printf("-------------------------------------------------------------\n"); //printf("\n C Program to find inverse of Matrix\n\n"); printf("Enter the order of the Matrix : "); scanf("%d",&k); printf("Enter the elements of %dX%d Matrix : \n",k,k); a=(float*)malloc(k*k*sizeof(float)); for (i=0;i<k;i++) { for (j=0;j<k;j++) { a[i*k+j]=i+j+1; } } printf("\noriginal matrix\n"); for (i=0;i<k;i++) { printf("\n"); for (j=0;j<k;j++) { printf("%f\t",a[i*k+j]); } } d=determinant(a,k); printf("Determinant of the Matrix = %f",d); if (d==0) {printf("\nInverse of Entered Matrix is not possible\n");} else cofactor(a,k); printf("\n\n**** Thanks for using the program!!! ****"); }
void adj(void) { int i, j, n; save(); p1 = pop(); if (istensor(p1) && p1->u.tensor->ndim == 2 && p1->u.tensor->dim[0] == p1->u.tensor->dim[1]) ; else stop("adj: square matrix expected"); n = p1->u.tensor->dim[0]; p2 = alloc_tensor(n * n); p2->u.tensor->ndim = 2; p2->u.tensor->dim[0] = n; p2->u.tensor->dim[1] = n; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { cofactor(p1, n, i, j); p2->u.tensor->elem[n * j + i] = pop(); /* transpose */ } push(p2); restore(); }
Matrix44 adjoint(){ Matrix44 a; for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ a.m[i+j*4]=cofactor(j,i); } } return a; }
Matrix inverseMatrix(const Matrix& m) { double D = determinant(m); if (fabs(D) < 1e-12) return m; // an error; matrix is not invertible double rD = 1.0 / D; Matrix result; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) result.m[i][j] = rD * cofactor(m, j, i); return result; }
inline T det() { if(N <= 1) { return *(this->data); } else { T c = static_cast<T>(0); const int rc = 0; for(int i = 0; i < N; ++i) { c += cofactor(i,rc); } return c; } }
DVectorMatrix DVectorMatrix::detinv(DVectorMatrix& mat) { DVectorMatrix *matrix_inv; matrix_inv = new DVectorMatrix(hosize, vesize); oas_t dete = mat.det(); for (vei_t k=0; k<mat.vesize; k++) { for (vei_t l=0; l<mat.hosize; l++) { matrix_inv->vector[k].set_comp((1/dete)*cofactor(mat,l,k), F64B_TYPE,l); } } return *matrix_inv; }
int main() { float a[25][25], k, d; int i, j; printf("Enter the order of the Matrix : "); scanf("%f", &k); printf("Enter the elements of %.0fX%.0f Matrix : \n", k, k); for (i = 0;i < k; i++) { for (j = 0;j < k; j++) { scanf("%f", &a[i][j]); } } d = determinant(a, k); if (d == 0)//inverse does not exist for a singular matrix printf("\nInverse of Entered Matrix is not possible\n"); else cofactor(a, k);//to find the cofactor of the matrix }
int main() { float a[25][25],k,d; int i,j; printf("Enter the order of the Matrix : "); scanf("%f",&k); printf("Enter the elements of %f X %f Matrix : \n",k,k); for (i=0;i<k;i++) { for (j=0;j<k;j++) { scanf("%f",&a[i][j]); } } d=determinant(a,k); printf("Determinant of the Matrix = %f",d); if (d==0) printf("\nInverse of Entered Matrix is not possible\n"); else cofactor(a,k); return 0; }
void eval_cofactor(void) { int i, j, n; push(cadr(p1)); eval(); p2 = pop(); if (istensor(p2) && p2->u.tensor->ndim == 2 && p2->u.tensor->dim[0] == p2->u.tensor->dim[1]) ; else stop("cofactor: 1st arg: square matrix expected"); n = p2->u.tensor->dim[0]; push(caddr(p1)); eval(); i = pop_integer(); if (i < 1 || i > n) stop("cofactor: 2nd arg: row index expected"); push(cadddr(p1)); eval(); j = pop_integer(); if (j < 1 || j > n) stop("cofactor: 3rd arg: column index expected"); cofactor(p2, n, i - 1, j - 1); }
matrix4x4 matrix4x4::inverse() const { jAssert( !fcmp( determinant(), 0 ) ); return ( cofactor().transpose() / determinant() ); }
Matrix4 Matrix4::adjoint() const { return cofactor().transpose(); }
float Matrix4::determinant() const { // Determinant is the dot product of the first row and the first row // of cofactors (i.e. the first col of the adjoint matrix) return cofactor().row(0).dot(row(0)); }
valmatrix<T> inverse_r2(const valmatrix<T>& m) { return transpose(cofactor(m)) * (inverse(determinant_r(m))); }
bool fullMatrix<double>::invert(fullMatrix<double> &result) const { if(_r != _c) return false; // Copy the matrix result.resize(_r,_c); // to find out Determinant double det = this->determinant(); if(det == 0) return false; // Matrix of cofactor put this in a function? fullMatrix<double> cofactor(_r,_c); if(_r == 2) { cofactor(0,0) = (*this)(1,1); cofactor(0,1) = -(*this)(1,0); cofactor(1,0) = -(*this)(0,1); cofactor(1,1) = (*this)(0,0); } else if(_r >= 3) { std::vector<std::vector<fullMatrix<double> > > temp(_r,std::vector<fullMatrix<double> >(_r,fullMatrix<double>(_r-1,_r-1))); for(int k1 = 0; k1 < _r; k1++) { for(int k2 = 0; k2 < _r; k2++) { int i1 = 0; for(int i = 0; i < _r; i++) { int j1 = 0; for(int j = 0; j < _r; j++) { if(k1 != i && k2 != j) temp[k1][k2](i1,j1++) = (*this)(i,j); } if(k1 != i) i1++; } } } bool flagPositive; for(int k1 = 0; k1 < _r; k1++) { flagPositive = (k1 % 2) == 0 ? true : false; for(int k2 = 0; k2 < _r; k2++) { if(flagPositive) { cofactor(k1,k2) = temp[k1][k2].determinant(); flagPositive = false; } else { cofactor(k1,k2) = -temp[k1][k2].determinant(); flagPositive = true; } } } } // end cofactor // inv = transpose of cofactor / Determinant for(int i = 0; i < _r; i++) { for(int j = 0; j < _c; j++) { result(j,i) = cofactor(i,j) / det; } } return true; }
Matrix<double> ObjectiveFunction::getInverseHessian(Vector<double> argument) { Matrix<double> inverseHessian(numberOfVariables, numberOfVariables, 0.0); Matrix<double> hessian = getHessian(argument); double hessianDeterminant = getDeterminant(hessian); if(hessianDeterminant == 0.0) { std::cout << "Error: ObjectiveFunction class. " << "Matrix<double> getInverseHessian(Vector<double>) method." << std::endl << "Hessian matrix is singular." << std::endl << std::endl; exit(1); } // Get cofactor matrix Matrix<double> cofactor(numberOfVariables, numberOfVariables, 0.0); Matrix<double> c(numberOfVariables-1, numberOfVariables-1, 0.0); for(int j = 0; j < numberOfVariables; j++) { for (int i = 0; i < numberOfVariables; i++) { // Form the adjoint a_ij int i1 = 0; for(int ii = 0; ii < numberOfVariables; ii++) { if(ii == i) { continue; } int j1 = 0; for(int jj = 0; jj < numberOfVariables; jj++) { if (jj == j) { continue; } c[i1][j1] = hessian[ii][jj]; j1++; } i1++; } double determinant = getDeterminant(c); cofactor[i][j] = pow(-1.0, i+j+2.0)*determinant; } } // Adjoint matrix is the transpose of cofactor matrix Matrix<double> adjoint(numberOfVariables, numberOfVariables, 0.0); double temp = 0.0; for(int i = 0; i < numberOfVariables; i++) { for (int j = 0; j < numberOfVariables; j++) { adjoint[i][j] = cofactor[j][i]; } } // Inverse matrix is adjoint matrix divided by matrix determinant for(int i = 0; i < numberOfVariables; i++) { for(int j = 0; j < numberOfVariables; j++) { inverseHessian[i][j] = adjoint[i][j]/hessianDeterminant; } } return(inverseHessian); }
int main() { miracl* mip=&precision; ECn Alice,Bob,sA,sB; ECn3 B6,Server,sS; ZZn6 sp,ap,bp; ZZn6 res,XX,YY; ZZn2 X; ZZn3 Qx,Qy; Big a,b,s,ss,p,q,x,y,B,cf,t,sru,T; int i,A; time_t seed; int qnr; mip->IOBASE=16; x="-D285DA0CFEF02F06F812"; // MNT elliptic curve parameters (Thanks to Drew Sutherland) p=x*x+1; q=x*x-x+1; t=x+1; cf=x*x+x+1; T=t-1; // cout << "t-1= " << T << endl; // cout << "p%24= " << p%24 << endl; time(&seed); irand((long)seed); A=-3; B="77479D33943B5B1F590B54258B72F316B3261D45"; ecurve(A,B,p,MR_PROJECTIVE); set_frobenius_constant(X); sru=pow((ZZn)-2,(p-1)/6); // x^6+2 is irreducible set_zzn3(-2,sru); mip->IOBASE=16; mip->TWIST=MR_QUADRATIC; // map Server to point on twisted curve E(Fp3) //See ftp://ftp.computing.dcu.ie/pub/resources/crypto/twists.pdf ss=rand(q); // TA's super-secret cout << "Mapping Server ID to point" << endl; Server=hash_and_map3((char *)"Server"); // Multiply by the cofactor - thank you NTL! // Server*=(p-1); // Server*=(p+1+t); cofactor(Server,x,X); cout << "Mapping Alice & Bob ID's to points" << endl; Alice=hash_and_map((char *)"Alice"); Bob= hash_and_map((char *)"Robert"); cout << "Alice, Bob and the Server visit Trusted Authority" << endl; sS=ss*Server; sA=ss*Alice; sB=ss*Bob; cout << "Alice and Server Key Exchange" << endl; a=rand(q); // Alice's random number s=rand(q); // Server's random number if (!ate(Server,sA,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } ap=powu(res,a); if (!ate(sS,Alice,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } sp=powu(res,s); cout << "Alice Key= " << H2(powu(sp,a)) << endl; cout << "Server Key= " << H2(powu(ap,s)) << endl; cout << "Bob and Server Key Exchange" << endl; b=rand(q); // Bob's random number s=rand(q); // Server's random number if (!ate(Server,sB,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } bp=powu(res,b); if (!ate(sS,Bob,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } sp=powu(res,s); cout << "Bob's Key= " << H2(powu(sp,b)) << endl; cout << "Server Key= " << H2(powu(bp,s)) << endl; return 0; }
f32 Matrix3x3::algebraicCofactor(u8 r, u8 c) const { f32 cf=cofactor(r,c); if (0!=(r+c)%2) cf*=-1.f; return cf; }
int main() { miracl* mip=&precision; ECn Alice,Bob,sA,sB; ECn3 B6,Server,sS; ZZn6 sp,ap,bp; ZZn6 res; ZZn2 X; Big a,b,s,ss,p,q,x,y,B,cf,t,sru,T; int i,A; time_t seed; mip->IOBASE=16; x="-D285DA0CFEF02F06F812"; // MNT elliptic curve parameters (Thanks to Drew Sutherland) p=x*x+1; q=x*x-x+1; t=x+1; cf=x*x+x+1; T=t-1; // cout << "t-1= " << T << endl; // cout << "p%24= " << p%24 << endl; time(&seed); irand((long)seed); A=-3; B="77479D33943B5B1F590B54258B72F316B3261D45"; #ifdef AFFINE ecurve(A,B,p,MR_AFFINE); #endif #ifdef PROJECTIVE ecurve(A,B,p,MR_PROJECTIVE); #endif set_frobenius_constant(X); sru=pow((ZZn)-2,(p-1)/6); // x^6+2 is irreducible set_zzn3(-2,sru); mip->IOBASE=16; mip->TWIST=MR_QUADRATIC; // map Server to point on twisted curve E(Fp3) ss=rand(q); // TA's super-secret cout << "Mapping Server ID to point" << endl; Server=hash_and_map3((char *)"Server"); cofactor(Server,x,X); cout << "Mapping Alice & Bob ID's to points" << endl; Alice=hash_and_map((char *)"Alice"); Bob= hash_and_map((char *)"Robert"); cout << "Alice, Bob and the Server visit Trusted Authority" << endl; sS=G2_mul(Server,ss,x,X); sA=ss*Alice; sB=ss*Bob; cout << "Alice and Server Key Exchange" << endl; a=rand(q); // Alice's random number s=rand(q); // Server's random number if (!ecap(sA,Server,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } ap=GT_pow(res,a,x,X);//powu(res,a); if (!ecap(Alice,sS,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } sp=GT_pow(res,s,x,X); cout << "Alice Key= " << H2(powu(sp,a)) << endl; cout << "Server Key= " << H2(powu(ap,s)) << endl; cout << "Bob and Server Key Exchange" << endl; b=rand(q); // Bob's random number s=rand(q); // Server's random number if (!ecap(sB,Server,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } bp=GT_pow(res,b,x,X); if (!ecap(Bob,sS,x,X,res)) cout << "Trouble" << endl; if (!member(res,x,X)) { cout << "Wrong group order - aborting" << endl; exit(0); } sp=GT_pow(res,s,x,X); cout << "Bob's Key= " << H2(powu(sp,b)) << endl; cout << "Server Key= " << H2(powu(bp,s)) << endl; return 0; }
float Matrix4::determinant() const { return cofactor().row(0).dot(row(0)); }