int main(int argc, const char * argv[]) { static const double endingCondition = 0.000000001; printSeparator(); printf(" This program will successively run iterations of the \n"); printf(" Jacobi Algorithm for diagonalization until some form of \n"); printf(" convergence is reacheed.\n"); printSeparator(); BOOL shouldSort = promptShouldUseSorting(); BOOL demoMode = promptShouldUseDemo(); int iterations = 1; if (!demoMode) { iterations = promptIterations(); } for (int i = 0; i < iterations; i++) { printSeparator(); printLine(); MatrixRef A = MatrixCreateRandomSymmetric(5); double offA = JacobiSquareSumOffDiagonal(A, NULL); if (demoMode) { printf("Starting 5x5 symmetric matrix: \n\n"); MatrixPrint(A); printLine(); printf("Starting off(A): %6.2f\n\n", offA); printSeparator(); printLine(); } MatrixRef B = Jacobi(A, shouldSort, endingCondition); if (demoMode) { printLine(); printSeparator(); printLine(); printf("Finishing matrix: \n\n"); MatrixPrint(B); } MatrixDestroy(B); B = NULL; MatrixDestroy(A); A = NULL; } #if defined(__MINGW32__) || defined(_WIN32) system("PAUSE"); #endif return 0; }
// generate a random private key void InvertibleRabinFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { int modulusSize = 2048; alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize); if (modulusSize < 16) throw InvalidArgument("InvertibleRabinFunction: specified modulus size is too small"); // VC70 workaround: putting these after primeParam causes overlapped stack allocation bool rFound=false, sFound=false; Integer t=2; const NameValuePairs &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize) ("EquivalentTo", 3)("Mod", 4); m_p.GenerateRandom(rng, primeParam); m_q.GenerateRandom(rng, primeParam); while (!(rFound && sFound)) { int jp = Jacobi(t, m_p); int jq = Jacobi(t, m_q); if (!rFound && jp==1 && jq==-1) { m_r = t; rFound = true; } if (!sFound && jp==-1 && jq==1) { m_s = t; sFound = true; } ++t; } m_n = m_p * m_q; m_u = m_q.InverseMod(m_p); }
void EigenSystemSolverRealSymmetricMatrix(const Array2 <doublevar > & Ain, Array1 <doublevar> & evals, Array2 <doublevar> & evecs){ //returns eigenvalues from largest to lowest and //eigenvectors, where for i-th eigenvalue, the eigenvector components are evecs(*,i) #ifdef USE_LAPACK //if LAPACK int N=Ain.dim[0]; Array2 <doublevar> Ain2(N,N); //need to copy the array!! Ain2=Ain; /* allocate and initialise the matrix */ Array1 <doublevar> W, Z, WORK; Array1 <int> ISUPPZ, IWORK; int M; /* allocate space for the output parameters and workspace arrays */ W.Resize(N); Z.Resize(N*N); ISUPPZ.Resize(2*N); WORK.Resize(26*N); IWORK.Resize(10*N); int info; /* get the eigenvalues and eigenvectors */ info=dsyevr('V', 'A', 'L', N, Ain2.v, N, 0.0, 0.0, 0, 0, dlamch('S'), &M, W.v, Z.v, N, ISUPPZ.v, WORK.v, 26*N, IWORK.v, 10*N); if(info>0) error("Internal error in the LAPACK routine dsyevr"); if(info<0) error("Problem with the input parameter of LAPACK routine dsyevr in position "-info); for (int i=0; i<N; i++) evals(i)=W[N-1-i]; for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { evecs(j,i)=Z[j+(N-1-i)*N]; } } //END OF LAPACK #else //IF NO LAPACK const int n = Ain.dim[0]; Array2 < dcomplex > Ain_complex(n,n); Array2 <dcomplex> evecs_complex(n,n); for (int i=0; i < n; i++) for (int j=0; j < n; j++) { Ain_complex(i,j)=dcomplex(Ain(i,j),0.0); } Jacobi(Ain_complex, evals, evecs_complex); for (int i=0; i < n; i++) for (int j=0; j < n; j++){ evecs(i,j)=real(evecs_complex(i,j)); } #endif //END OF NO LAPACK }
double PDECond::Passo(){ for(int i=1; i<X-1;i++){ for(int j=1; j<Y-1;j++){ if(M == mSOR) succ[i][j] = SOR(i,j); else if(M == mGaussSeidel) succ[i][j] = GaussSeidel(i,j); else succ[i][j] = Jacobi(i,j); } } return getErr();//ritorna l'errore la precisione e` decisa dall'utente }
bool ECP::DecodePoint(ECP::Point &P, BufferedTransformation &bt, unsigned int encodedPointLen) const { byte type; if (encodedPointLen < 1 || !bt.Get(type)) return false; switch (type) { case 0: P.identity = true; return true; case 2: case 3: { if (encodedPointLen != EncodedPointSize(true)) return false; Integer p = FieldSize(); P.identity = false; P.x.Decode(bt, GetField().MaxElementByteLength()); P.y = ((P.x*P.x+m_a)*P.x+m_b) % p; if (Jacobi(P.y, p) !=1) return false; P.y = ModularSquareRoot(P.y, p); if ((type & 1) != P.y.GetBit(0)) P.y = p-P.y; return true; } case 4: { if (encodedPointLen != EncodedPointSize(false)) return false; unsigned int len = GetField().MaxElementByteLength(); P.identity = false; P.x.Decode(bt, len); P.y.Decode(bt, len); return true; } default: return false; } }
Integer ModularSquareRoot(const Integer &a, const Integer &p) { if (p%4 == 3) return a_exp_b_mod_c(a, (p+1)/4, p); Integer q=p-1; unsigned int r=0; while (q.IsEven()) { r++; q >>= 1; } Integer n=2; while (Jacobi(n, p) != -1) ++n; Integer y = a_exp_b_mod_c(n, q, p); Integer x = a_exp_b_mod_c(a, (q-1)/2, p); Integer b = (x.Squared()%p)*a%p; x = a*x%p; Integer tempb, t; while (b != 1) { unsigned m=0; tempb = b; do { m++; b = b.Squared()%p; if (m==r) return Integer::Zero(); } while (b != 1); t = y; for (unsigned i=0; i<r-m-1; i++) t = t.Squared()%p; y = t.Squared()%p; r = m; x = x*t%p; b = tempb*y%p; } assert(x.Squared()%p == a); return x; }
bool IsStrongLucasProbablePrime(const Integer &n) { if (n <= 1) return false; if (n.IsEven()) return n==2; assert(n>2); Integer b=3; unsigned int i=0; int j; while ((j=Jacobi(b.Squared()-4, n)) == 1) { if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square return false; ++b; ++b; } if (j==0) return false; Integer n1 = n+1; unsigned int a; // calculate a = largest power of 2 that divides n1 for (a=0; ; a++) if (n1.GetBit(a)) break; Integer m = n1>>a; Integer z = Lucas(m, b, n); if (z==2 || z==n-2) return true; for (i=1; i<a; i++) { z = (z.Squared()-2)%n; if (z==n-2) return true; if (z==2) return false; } return false; }
// generate a random prime p of the form 2*r*q+delta, where q is also prime PrimeAndGenerator::PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits) { // no prime exists for delta = -1, qbits = 4, and pbits = 5 assert(qbits > 4); assert(pbits > qbits); Integer minQ = Integer::Power2(qbits-1); Integer maxQ = Integer::Power2(qbits) - 1; Integer minP = Integer::Power2(pbits-1); Integer maxP = Integer::Power2(pbits) - 1; do { q.Randomize(rng, minQ, maxQ, Integer::PRIME); } while (!p.Randomize(rng, minP, maxP, Integer::PRIME, delta%q, q)); // find a random g of order q if (delta==1) { do { Integer h(rng, 2, p-2, Integer::ANY); g = a_exp_b_mod_c(h, (p-1)/q, p); } while (g <= 1); assert(a_exp_b_mod_c(g, q, p)==1); } else { assert(delta==-1); do { Integer h(rng, 3, p-1, Integer::ANY); if (Jacobi(h*h-4, p)==1) continue; g = Lucas((p+1)/q, h, p); } while (g <= 2); assert(Lucas(q, g, p) == 2); } }
int WarpVolume::JacobiRotationMatrix(float xx, float yy, float zz, Matrix& M) {// (xx, yy, zz): the 3-D location // return 1 : successful // return -1: failed // resulting matrix: M float Jab[9], ang, ox,oy,oz; Vector I(1,0,0), J(0,1,0), K(0,0,1), omega; //cout <<" **111****" << endl; int res=Jacobi(xx,yy,zz, Jab); //cout <<" **222****" << endl; if (res == -1) return -1; omega = -(I*Jab[5]) - (J*Jab[6]) - (K*Jab[1]); //cout <<endl <<"omega=>"; omega.outputs(); omega.getXYZ(ox,oy,oz); //cout << endl << "omega <==" << endl; ang = omega.length(); //cout << "omega & ang :" << endl; //omega.outputs(); cout << "ang = " << ang << endl; M.loadIdentity(); M.setValue(0 , 0, ox*ox*(1-cos(ang)) + cos(ang)); M.setValue(0 , 1, ox*oy*(1-cos(ang)) - oz*sin(ang)); M.setValue(0 , 2, ox*oz*(1-cos(ang)) + ox*sin(ang)); M.setValue(1 , 0, ox*oy*(1-cos(ang)) + oz*sin(ang)); M.setValue(1 , 1, oy*oy*(1-cos(ang)) + cos(ang)); M.setValue(1 , 2, oy*oz*(1-cos(ang)) - ox*sin(ang)); M.setValue(2, 0, ox*oz*(1-cos(ang)) - oy*sin(ang)); M.setValue(2 , 1, oy*oz*(1-cos(ang)) + ox*sin(ang)); M.setValue(2 , 2, oz*oz*(1-cos(ang)) + cos(ang)); return 1; }
bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p) { Integer D = (b.Squared() - 4*a*c) % p; switch (Jacobi(D, p)) { default: assert(false); // not reached return false; case -1: return false; case 0: r1 = r2 = (-b*(a+a).InverseMod(p)) % p; assert(((r1.Squared()*a + r1*b + c) % p).IsZero()); return true; case 1: Integer s = ModularSquareRoot(D, p); Integer t = (a+a).InverseMod(p); r1 = (s-b)*t % p; r2 = (-s-b)*t % p; assert(((r1.Squared()*a + r1*b + c) % p).IsZero()); assert(((r2.Squared()*a + r2*b + c) % p).IsZero()); return true; } }
double CParam::calculate_log_cond_norm(CData &Data, int i_original, ColumnVector &item_by_rnorm, ColumnVector &tilde_y_i, ColumnVector &y_q, bool is_q, LowerTriangularMatrix &LSigma_1_i, ColumnVector &s_q) { // MODIFIED 2015/02/16 double log_cond_norm; if ( item_by_rnorm.sum() >= 1 ) { ColumnVector mu_z_i = Mu.column(z_in(i_original)); ColumnVector s_1_compact = Data.get_compact_vector(item_by_rnorm); ColumnVector Mu_1 = subvector(mu_z_i,s_1_compact); Matrix Sigma_1 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_1_compact,s_1_compact); // ADDED 2015/01/27 ColumnVector s_q_compact = Data.get_compact_vector(s_q) ; // MODIFIED 2015/02/16 ColumnVector VectorOne = s_q_compact ; VectorOne = 1 ; // MODIFIED 2015/02/16 ColumnVector s_0_compact = VectorOne - s_q_compact ; // MODIFIED 2015/02/16 int sum_s_0_comp = s_0_compact.sum() ; LowerTriangularMatrix LSigma_cond ; ColumnVector Mu_cond ; if ( sum_s_0_comp>0 ){ ColumnVector Mu_0 = subvector(mu_z_i,s_0_compact); // (s_1_compact.sum()) vector Matrix Sigma_0 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_0_compact,s_0_compact); Matrix Sigma_10 = Submatrix_elem_2(SIGMA[z_in(i_original)-1],s_1_compact,s_0_compact); ColumnVector y_tilde_compact = Data.get_compact_vector(tilde_y_i) ; ColumnVector y_tilde_0 = subvector(y_tilde_compact,s_0_compact) ; SymmetricMatrix Sigma_0_symm ; Sigma_0_symm << Sigma_0 ; LowerTriangularMatrix LSigma_0 = Cholesky(Sigma_0_symm) ; Mu_cond = Mu_1 + Sigma_10 * (LSigma_0.i()).t()*LSigma_0.i() * ( y_tilde_0-Mu_0 ) ; Matrix Sigma_cond = Sigma_1 - Sigma_10 * (LSigma_0.i()).t()*LSigma_0.i() * Sigma_10.t() ; SymmetricMatrix Sigma_cond_symm ; Sigma_cond_symm << Sigma_cond ; int sum_s_1_comp = s_1_compact.sum() ; DiagonalMatrix D(sum_s_1_comp) ; Matrix V(sum_s_1_comp,sum_s_1_comp) ; Jacobi(Sigma_cond_symm,D,V) ; int is_zero_exist = 0 ; for (int i_var=1; i_var<=sum_s_1_comp; i_var++){ if ( D(i_var) < 1e-9 ){ D(i_var) = 1e-9 ; is_zero_exist = 1 ; } } // for (int i_var=1; i_var<=sum_s_1_comp; i_var++) if ( is_zero_exist == 1 ){ Sigma_cond_symm << V * D * V.t() ; if ( msg_level >= 1 ) { Rprintf( " Warning: When generating y_j from conditional normal(Mu_-j,Sigma_-j), Sigma_-j is non-positive definite because of computation precision. The eigenvalues D(j,j) smaller than 1e-9 is replaced with 1e-9, and let Sigma_-j = V D V.t().\n"); } } // LSigma_cond = Cholesky(Sigma_cond_symm); // y_part = rMVN_fn(Mu_cond,LSigma_cond); // log_cond_norm = log_MVN_fn(y_part,Mu_cond,LSigma_cond) ; } else { Mu_cond = Mu_1 ; SymmetricMatrix Sigma_1_symm = Submatrix_elem(SIGMA[z_in(i_original)-1],s_1_compact); LSigma_cond = Cholesky(Sigma_1_symm) ; // SymmetricMatrix Sigma_1_symm ; Sigma_1_symm << Sigma_1 ; // LowerTriangularMatrix LSigma_1 = Cholesky_Sigma_star_symm(Sigma_1_symm); // y_part = rMVN_fn(Mu_1,LSigma_1); // log_cond_norm = log_MVN_fn(y_part,Mu_1,LSigma_1) ; } // if ( sum_s_0_comp>0 ) else ... // ADDED 2015/01/26 LowerTriangularMatrix LSigma_cond_i = LSigma_cond.i() ; // LowerTriangularMatrix LSigma_1 = Cholesky(Sigma_1); // LSigma_1_i = LSigma_1.i(); ColumnVector y_part; if (is_q) { y_part = rMVN_fn(Mu_cond,LSigma_cond); } else { ColumnVector y_i = (Y_in.row(i_original)).t(); y_part = subvector(y_i,item_by_rnorm); } log_cond_norm = log_MVN_fn(y_part,Mu_cond,LSigma_cond_i); if (is_q) { y_q = tilde_y_i; for ( int temp_j = 1,temp_count1 = 0; temp_j<=n_var; temp_j++ ){ if ( item_by_rnorm(temp_j)==1 ){ y_q(temp_j) = y_part(++temp_count1); } } } // if (is_q) } else { log_cond_norm = 0; if (is_q) { y_q = tilde_y_i;} } // if ( item_by_rnorm.sum() > = 1 ) else .. return log_cond_norm; }
void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, Matrix& V) { REPORT SymmetricMatrix A; Jacobi(X,D,A,V,true); }
void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A) { REPORT Matrix V; Jacobi(X,D,A,V,false); }
int getNormalModes(double** cartCoords, double* carthessian, double* masslist, int numCartesians, double* frequencies, double* normalmodes, int &nfreq, int decontaminate) { Matrix Hc(numCartesians*3,numCartesians*3); Hc << carthessian; // mass weighted cartesian Hessian int i,j; Matrix Hmwc(numCartesians*3,numCartesians*3); for (i=0; i<numCartesians; i++) { for (j=0; j<numCartesians; j++) { Hmwc << Hc(i+1,j+1)/sqrt(masslist[i]*masslist[j]); } } #ifdef DEBUG cout << "Hmwc:\n"; cout << setw(9) << setprecision(3) << (Hmwc); cout << "\n\n"; #endif if (!decontaminate) { Matrix V; DiagonalMatrix eigval; EigenValues(Hmwc,eigval,V); const float twopicinv = 1/(2*PI*LIGHTSPEED); // 2*pi*c for (i=0; i<numCartesians; i++) { frequencies[i] = twopicinv*sqrt(abs(eigval(i+1))); // Complex frequencies are marked with a negative sign if (eigval(i+1)<0) frequencies[i] *= -1.0f; } } // Moments of inertia tensor double Ixx=0, Iyy=0, Izz=0, Ixy=0, Ixz=0, Iyz=0, x, y, z, m, mtot=0.0; double Rcom[3]; for (i=0; i<numCartesians; i++) { x = cartCoords[i][0]; y = cartCoords[i][1]; z = cartCoords[i][2]; m = masslist[i]; // Center of mass Rcom[0] += m*x; Rcom[1] += m*y; Rcom[2] += m*z; mtot += m; Ixx += m*(y*y+z*z); Iyy += m*(x*x+z*z); Izz += m*(x*x+y*y); Ixy -= m*x*y; Ixz -= m*x*z; Iyz -= m*y*z; } Rcom[0] /= mtot; Rcom[1] /= mtot; Rcom[2] /= mtot; SymmetricMatrix I(3); I << Ixx << Ixy << Ixz << Ixy << Iyy << Iyz << Ixz << Iyz << Izz; // Diagonalize moments of intertia DiagonalMatrix eigval; Matrix X; Jacobi(I, eigval, X); #ifdef DEBUG cout << "I:\n"; cout << setw(9) << setprecision(3) << (I); cout << "\n\n"; cout << "X:\n"; cout << setw(9) << setprecision(3) << (X); cout << "\n\n"; #endif // Coords wrt COM Matrix R(numCartesians, 3); for (i=0; i<numCartesians; i++) { R(i,0) = cartCoords[i][0]-Rcom[0]; R(i,1) = cartCoords[i][1]-Rcom[1]; R(i,2) = cartCoords[i][2]-Rcom[2]; } #ifdef DEBUG cout << "R:\n"; cout << setw(9) << setprecision(3) << (R); cout << "\n\n"; #endif RowVector Px = R*I.Row(1); RowVector Py = R*I.Row(2); RowVector Pz = R*I.Row(3); double sqrm; IdentityMatrix E(3*numCartesians); Matrix D(3*numCartesians,3*numCartesians); D << E; for (i=0; i<numCartesians; i++) { sqrm = sqrt(masslist[i]); D(1,i*3+1) = D(2,i*3+2) = D(3,i*3+3) = sqrm; D(1,i*3+2) = D(1,i*3+3) = D(2,i*3+1) = D(2,i*3+3) = D(3,i*3+1) = D(3,i*3+2) = 0.0; for (j=1; j<=3; j++) { D(4,i*3+j) = (Py(i)*X(j,3)-Pz(i)*X(j,2))/sqrm; D(5,i*3+j) = (Pz(i)*X(j,1)-Px(i)*X(j,3))/sqrm; D(6,i*3+j) = (Px(i)*X(j,2)-Py(i)*X(j,1))/sqrm; } } #ifdef DEBUG cout << "D:\n"; cout << setw(9) << setprecision(3) << (D); cout << "\n\n"; #endif // Normalize D int ntransrot=6; double s[6]; RowVector Ri, Rj; for (i=0; i<6; i++) { //Ri = D.Row(i+1); //s[i] = DotProduct(Ri, Ri); s[i] = D.Row(i).NormFrobenius(); if (s[i]<1e-6) ntransrot--; else D.Column(i+1) *= 1.f/sqrt(s[i]); } // Remove the row with zero elements if (ntransrot<6) { int k=1; for (i=0; i<6; i++) { if (s[i]>=1e-6) { D.Row(k)=D.Row(i+1); k++; } } } #ifdef DEBUG cout << "D normalized:\n"; cout << setw(9) << setprecision(3) << (D); cout << "\n\n"; #endif int Nvib = 3*numCartesians-ntransrot; cout << "Nvib = 3N-"<<ntransrot<<" = "<<Nvib<<" nonredundant internal coordinates"<<"\n"; // We create a 3N*3N transformation matrix D which transforms from mass weighted // cartesian coordinates q to internal coordinates S = Dq, where rotation and // translation have been separated out. // The first five or six vectors are already linearly independent, // and we just orthogonalized them above. // Now the remaining 3N-6 vectors are generated using the stabilized // Gram-Schmidt algorithm. They are required to be orthogonal to the // translational rotational vectors. double dot , norm; for (i=1; i<=ntransrot; i++) { for (j=1; j<i; j++) { // Remove component of D_i that is parallel to D_k: // D_i = D_i - <D_i, D_k> * D_k Ri = D.Row(i); Rj = D.Row(j); dot = DotProduct(Rj, Ri); D.Row(i) -= dot*Rj; } // Normalize //norm = DotProduct(D.Row(i), D.Row(i)); norm = D.Row(i).NormFrobenius(); D.Row(i) *= 1/sqrt(norm); } int k, cik=0; for (i=ntransrot; i<=3*numCartesians; i++) { norm = 0.0; while (abs(norm) < 1e-12) { // Initialize i'th row with cik'th coord axis D.Row(i) = E.Row(cik); // Stabilized Gram-Schmidt: for (k=0; k<i; k++) { // Remove component of D_i that is parallel to D_k (k'th row): // D_i = D_i - <D_i, D_k> * D_k // <D_i, D_k> = D_k[cik], since D_i[j]=0 for j!=cik and D_i[cik]=1 D.Row(i) -= D(k,cik)*D.Row(k); } // Length of D_i: norm = D.Row(i).NormFrobenius(); // Check for the linear independence: // If norm==0 it means that all components parallel to another existing // vector (row of D) summed up to D_i, i.e the new D_i is linear dependent // on the other vectors. In this case we choose another D_cik for the // construction of D_i. if ( norm < 1e-6) { cik++; // take another vector norm = 0.0; } } D.Row(i) *= 1.f/norm; cik++; } #ifdef DEBUG cout << "D orthonormalized:\n"; cout << setw(9) << setprecision(3) << (D); cout << "\n\n"; #endif Matrix Hint; Hint = D.t() * Hmwc * D; Matrix V; EigenValues(Hint,eigval,V); frequencies = new double[Nvib]; const double scale = 1.f/(2.f*PI*LIGHTSPEED); for (i=0; i<Nvib; i++) { frequencies[i] = scale*sqrt(eigval(i+1)); // Complex frequencies are marked with a negative sign if (eigval(i+1)<0) frequencies[i] *= -1.0f; } return Nvib; }
double Fem3D::isoTet4(const double &xi, const double &eta, const double &mu, const DoubleVector &x, const DoubleVector &y, const DoubleVector &z, DoubleVector &N, DoubleVector &dNdX, DoubleVector &dNdY, DoubleVector &dNdZ) { const mtx::size_type elementNodes = 4; // значения производных функций формы в местных координатах DoubleVector dNdXi(elementNodes); DoubleVector dNdEta(elementNodes); DoubleVector dNdMu(elementNodes); // Якобиан DoubleMatrix Jacobi(3, 0.0); DoubleMatrix invJacobian(3); double jacobian; N(0) = 1.0 - xi - eta - mu; N(1) = xi; N(2) = eta; N(3) = mu; dNdXi(0) = -1.0; dNdXi(1) = 1.0; dNdXi(2) = 0.0; dNdXi(3) = 0.0; dNdEta(0) = -1.0; dNdEta(1) = 0.0; dNdEta(2) = 1.0; dNdEta(3) = 0.0; dNdMu(0) = -1.0; dNdMu(1) = 0.0; dNdMu(2) = 0.0; dNdMu(3) = 1.0; Jacobi(0, 0) = 0.0; Jacobi(0, 1) = 0.0; Jacobi(0, 2) = 0.0; Jacobi(1, 0) = 0.0; Jacobi(1, 1) = 0.0; Jacobi(1, 2) = 0.0; Jacobi(2, 0) = 0.0; Jacobi(2, 1) = 0.0; Jacobi(2, 2) = 0.0; for (mtx::size_type i = 0; i < elementNodes; i++) { Jacobi(0, 0) += dNdXi(i) * x[i]; Jacobi(0, 1) += dNdXi(i) * y[i]; Jacobi(0, 2) += dNdXi(i) * z[i]; Jacobi(1, 0) += dNdEta(i) * x[i]; Jacobi(1, 1) += dNdEta(i) * y[i]; Jacobi(1, 2) += dNdEta(i) * z[i]; Jacobi(2, 0) += dNdMu(i) * x[i]; Jacobi(2, 1) += dNdMu(i) * y[i]; Jacobi(2, 2) += dNdMu(i) * z[i]; } invJacobian = Jacobi.inverted3x3(); jacobian = Jacobi.det3x3(); for (mtx::size_type i = 0; i < elementNodes; i++) { dNdX(i) = invJacobian(0, 0) * dNdXi(i) + invJacobian(0, 1) * dNdEta(i) + invJacobian(0, 2) * dNdMu(i); dNdY(i) = invJacobian(1, 0) * dNdXi(i) + invJacobian(1, 1) * dNdEta(i) + invJacobian(1, 2) * dNdMu(i); dNdZ(i) = invJacobian(2, 0) * dNdXi(i) + invJacobian(2, 1) * dNdEta(i) + invJacobian(2, 2) * dNdMu(i); } return jacobian; }
void Calculate(int data) { glViewport(0, 0, g_iWidth, g_iHeight); Advect(Velocity.Ping, Velocity.Ping, Boundaries, Velocity.Pong, VelocityDissipation); SwapSurfaces(&Velocity); /*Advect(Velocity2.Ping, Velocity2.Ping, Boundaries, Velocity2.Pong, VelocityDissipation); SwapSurfaces(&Velocity2);*/ Advect(Velocity.Ping, Temperature.Ping, Boundaries, Temperature.Pong, TemperatureDissipation); SwapSurfaces(&Temperature); /*Advect(Velocity2.Ping, Temperature2.Ping, Boundaries, Temperature2.Pong, TemperatureDissipation); SwapSurfaces(&Temperature2);*/ Advect(Velocity.Ping, Density.Ping, Boundaries, Density.Pong, DensityDissipation); SwapSurfaces(&Density); /*Advect(Velocity2.Ping, Density2.Ping, Boundaries, Density2.Pong, DensityDissipation); SwapSurfaces(&Density2);*/ glutMouseFunc(Mouse); ApplyBuoyancy(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong, ax, ay); SwapSurfaces(&Velocity); /*ApplyBuoyancy2(Velocity.Ping, Temperature.Ping, Density.Ping, Velocity.Pong); SwapSurfaces(&Velocity);*/ ApplyImpulse(Temperature.Ping, ImpulsePosition, ImpulseTemperature); ApplyImpulse(Density.Ping, ImpulsePosition, ImpulseDensity); /*ApplyImpulse(Temperature2.Ping, ImpulsePosition2, ImpulseTemperature); ApplyImpulse(Density2.Ping, ImpulsePosition2, ImpulseDensity);*/ ComputeDivergence(Velocity.Ping, Boundaries, Divergence); ClearSurface(Pressure.Ping, 0); /*ComputeDivergence(Velocity2.Ping, Boundaries, Divergence2); ClearSurface(Pressure2.Ping, 0);*/ for (int i = 0; i < NumJacobiIterations; ++i) { Jacobi(Pressure.Ping, Divergence, Boundaries, Pressure.Pong); SwapSurfaces(&Pressure); } /*for (int i = 0; i < NumJacobiIterations; ++i) { Jacobi(Pressure2.Ping, Divergence2, Boundaries, Pressure2.Pong); SwapSurfaces(&Pressure2); }*/ SubtractGradient(Velocity.Ping, Pressure.Ping, Boundaries, Velocity.Pong); SwapSurfaces(&Velocity); /*SubtractGradient(Velocity2.Ping, Pressure2.Ping, Boundaries, Velocity2.Pong); SwapSurfaces(&Velocity2);*/ glutPostRedisplay(); glutTimerFunc(30, Calculate, 1); }
/* main function * */ int main(int argc, char **argv) { int subsize = SUB_SIZE; double A[subsize+2][subsize+2]; int count, tag, myid,np, i,j,k; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Comm_size(MPI_COMM_WORLD,&np); /* check processes */ if(np < N_PROCESS){ printf("!!!%d processes is needed!!!\n",N_PROCESS ); return -1; } /* set initiate ,all get 0.0 */ for(i=0;i<subsize+2;i++) for(j=0;j<subsize+2;j++) A[i][j]= 0.0; /* set four sides element to 8.0 */ for(i =0;i<3;i++){ if(myid == i){ for(j=1;j<subsize+1;j++) A[1][j]= 8.0; } } for(i =6;i<9;i++){ if(myid == i){ for(j=1;j<subsize+1;j++) A[subsize][j]= 8.0; } } for(i =0;i<9;i=i+3){ if(myid == i){ for(j=1;j<subsize+1;j++) A[j][1]= 8.0;} } for(i =2;i<9;i=i+3){ if(myid == i){ for(j=1;j<subsize+1;j++) A[j][subsize]= 8.0;} } if (myid ==0){ printf("----------Initiate Matrix --------\n");} for(i =0;i<9;i++){ if(myid ==i ){ CrossShow(A,myid,subsize);} MPI_Barrier(MPI_COMM_WORLD); } MPI_Barrier(MPI_COMM_WORLD); if (myid ==0){ printf("----------Passing massage -----begin---\n");} Cross(A,myid, subsize,np,&status); MPI_Barrier(MPI_COMM_WORLD); if (myid ==0){ printf("----------Passing massage -----end---\n"); printf("----------Matrix show after Passage --------\n");} for(i =0;i<9;i++){ if(myid ==i ){ CrossShow(A,myid,subsize);} MPI_Barrier(MPI_COMM_WORLD); } Jacobi(A, myid, subsize,np); MPI_Barrier(MPI_COMM_WORLD); if (myid ==0){ printf("----------Matrix show after Jacobi --------\n");} for(i =0;i<9;i++){ if(myid ==i ){ CrossShow(A,myid,subsize);} MPI_Barrier(MPI_COMM_WORLD); } MPI_Finalize(); return 0; }
/// Calcuate ellipsoids with same moments of inertia, centroids as grains int Dataset::calculate_ellipses() { int i; int done = 0; //#pragma omp parallel for for(i=0;i<num_grains;i++){ int j,k, index; SymmetricMatrix I2(3); I2 = 0; DiagonalMatrix D(3); double I1[3], A[3], I0, p[3], c[3], ax[3], I2x[3]; class GrainList *list; progress_callback((float)done/num_grains, "Calculating ellipses", false); I0 = grains[i].volume; I1[0] = 0; I1[1] = 0; I1[2] = 0; if(grains[i].volume > 1){ // Calculate 1st moments of inertia //-------------------------------------------------- for(list=grains[i].list_head; list!=NULL; list=list->next){ index = list->index; p[X] = (index % tx) *(double)steps[X]; p[Y] = ((index/dy) % ty)*(double)steps[Y]; p[Z] = ((index/dz) % tz)*(double)steps[Z]; for(j=0;j<3;j++){ I1[j] += p[j]; } } // Calculate centroid //-------------------------------------------------- for(j=0;j<3;j++){ c[j] = I1[j]/I0; } // Calculate 1st, 2nd moments of inertia //-------------------------------------------------- for(list=grains[i].list_head; list!=NULL; list=list->next){ index = list->index; p[X] = (index % tx) *(double)steps[X] - c[X]; p[Y] = ((index/dy) % ty)*(double)steps[Y] - c[Y]; p[Z] = ((index/dz) % tz)*(double)steps[Z] - c[Z]; I2(1,1) += p[1]*p[1] + p[2]*p[2]; // Newmat library uses I2(2,2) += p[0]*p[0] + p[2]*p[2]; // 1-indexed arrays I2(3,3) += p[0]*p[0] + p[1]*p[1]; I2(1,2) -= p[0]*p[1]; I2(1,3) -= p[0]*p[2]; I2(2,3) -= p[1]*p[2]; } for(j=1;j<=3;j++){ I2(j,j) = I2(j,j)/I0; for(k=j+1;k<=3;k++){ I2(j,k) = I2(j,k)/I0; } } // Diagonalise matrix Jacobi(I2,D); // Calcuate axes //-------------------------------------------------- for(j=0;j<3;j++){ A[j] = 15/(8*PI)*( D(j+1,j+1) + D((j+1)%3+1,(j+1)%3+1) - D((j+2)%3+1,(j+2)%3+1) ); } I2x[0] = D(1,1); I2x[1] = D(2,2); I2x[2] = D(3,3); for(j=0;j<3;j++){ ax[j] = sqrt(2.5*(I2x[(j+1)%3]+I2x[(j+2)%3]-I2x[j])); } for(j=0;j<3;j++){ grains[i].centroid[j] = c[j]; grains[i].axis[j] = ax[j]; //pow(pow(A[j],4)/(A[(j+1)%3]*A[(j+2)%3]),0.1); } // Sort axes such that a>b>c qsort(grains[i].axis,3,sizeof(float),compare_floats); if (isnan(grains[i].axis[0]) || isnan(grains[i].axis[1]) || isnan(grains[i].axis[2]) ) { //|| //isinf(grains[i].axis[0]) || isinf(grains[i].axis[1]) || isinf(grains[i].axis[2])) { grains[i].axis[0] = 1; grains[i].axis[1] = 1; grains[i].axis[2] = 1; } } #pragma omp atomic done++; } progress_callback(1, "Calculating ellipses", false); return 1; }
bool DH::ValidateDomainParameters(RandomNumberGenerator &rng) const { return VerifyPrime(rng, p) && VerifyPrime(rng, (p-1)/2) && g > 1 && g < p && Jacobi(g, p) == 1; }
main() { short m; double abscis[N]; double wt[N]; double left, right; printf("Table 25.8, k= %1d, n=%1d\n\n", k, N); Jacobi(N, FLOATk, ZERO, abscis, wt); for (m=0; m<N; m++) wt[m] /= (k+1); for (m=0; m<N; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nTable 25.4, n=9\n\n"); Radau_Jacobi(4, -0.5, ZERO, abscis, wt, &left); left *= 2; for (m=0; m<4; m++) abscis[m]= sqrt(abscis[m]); printf("%2d %20.18g %20.18g\n", 0, ZERO, left); for (m=0; m<4; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nTable 25.9, n= %1d\n\n", N); Laguerre(N, ZERO, abscis, wt); for (m=0; m<N; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nTable 25.10, n= %1d\n\n", N); Hermite(N, ZERO, abscis, wt); for (m=0; m<N; m++) wt[m] *= SqrtPI; for (m=0; m<N; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nSame, n= %1d\n\n", 2*N); Even_Hermite(2*N, ZERO, abscis, wt); for (m=0; m<N; m++) wt[m] *= SqrtPI; for (m=0; m<N; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nSame, n= %1d\n\n", 9); Odd_Hermite(9, ZERO, abscis, wt, &left); left *= SqrtPI; for (m=0; m<4; m++) wt[m] *= SqrtPI; printf("%2d %20.18g %20.18g\n", 0, ZERO, left); for (m=0; m<4; m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); getchar(); printf("\n\n\nTable 25.6, n= %1d\n\n", N+1); Lobatto_Jacobi(N-1, ZERO, ZERO, abscis, wt, &left, &right); for (m=0; m<(N-1); m++) wt[m] *= 2; left *= 2; right *= 2; for (m=0; m<(N-1); m++) abscis[m] = (2*abscis[m] - 1); printf(" %20.18g %20.18g\n", -1.0, left); for (m=0; m<(N-1); m++) printf("%2d %20.18g %20.18g\n", m, abscis[m], wt[m]); printf(" %20.18g %20.18g\n", 1.0, right); }
int Kalkreuter_qdp(QDP_ColorVector **eigVec, double *eigVal, Real Tolerance, Real RelTol, int Nvecs, int MaxIter, int Restart, int Kiters, QDP_Subset subset) { QLA_Real max_error = 1.0e+10; QLA_Real min_grad; QLA_Real *grad, *err; Matrix Array, V; QDP_ColorVector *vec; int total_iters=0; int i, j; int iter = 0; #ifdef DEBUG if(QDP_this_node==0) printf("begin Kalkreuter_qdp\n"); #endif prepare_Matrix(); Array = AllocateMatrix(Nvecs); /* Allocate the array */ V = AllocateMatrix(Nvecs); /* Allocate the Eigenvector matrix */ vec = QDP_create_V(); grad = malloc(Nvecs*sizeof(QLA_Real)); err = malloc(Nvecs*sizeof(QLA_Real)); /* Initiallize all the eigenvectors to a random vector */ for(j=0; j<Nvecs; j++) { grad[j] = 1.0e+10; QDP_V_eq_gaussian_S(eigVec[j], rand_state, QDP_all); eigVal[j] = 1.0e+16; //project_out_qdp(eigVec[j], eigVec, j, subset); //normalize_qdp(eigVec[j], subset); } #if 0 constructArray_qdp(eigVec, &Array, grad, subset); Jacobi(&Array, &V, JACOBI_TOL); sort_eigenvectors(&Array, &V); RotateBasis_qdp(eigVec, &V, subset); #endif while( (max_error>Tolerance) && (iter<Kiters) ) { iter++; min_grad = grad[0]/eigVal[0]; for(i=1; i<Nvecs; i++) { if(grad[i]<min_grad*eigVal[i]) min_grad = grad[i]/eigVal[i]; } RelTol = 0.3; for(j=0; j<Nvecs; j++) { if(grad[j]>Tolerance*eigVal[j]) { QLA_Real rt; rt = RelTol*min_grad*eigVal[j]/grad[j]; //rt = 1e-5/grad[j]; if(rt>RelTol) rt = RelTol; //rt = RelTol; QDP_V_eq_V(vec, eigVec[j], QDP_all); total_iters += Rayleigh_min_qdp(vec, eigVec, Tolerance, rt, j, MaxIter, Restart, subset); QDP_V_eq_V(eigVec[j], vec, QDP_all); } } constructArray_qdp(eigVec, &Array, grad, subset); for(i=0; i<Nvecs; i++) node0_printf("quot(%i) = %g +/- %8e |grad|=%g\n", i, Array.M[i][i].real, err[i], grad[i]); #ifdef DEBUG node0_printf("Eigenvalues before diagonalization\n"); for(i=0;i<Nvecs;i++) node0_printf("quot(%i) = %g |grad|=%g\n",i,Array.M[i][i].real,grad[i]); #endif Jacobi(&Array, &V, JACOBI_TOL); sort_eigenvectors(&Array, &V); RotateBasis_qdp(eigVec, &V, subset); constructArray_qdp(eigVec, &Array, grad, subset); /* find the maximum error */ max_error = 0.0; for(i=0; i<Nvecs; i++) { err[i] = eigVal[i]; eigVal[i] = Array.M[i][i].real; err[i] = fabs(err[i] - eigVal[i])/(1.0 - RelTol*RelTol); if(eigVal[i]>1e-10) { #ifndef STRICT_CONVERGENCE if(err[i]/eigVal[i]>max_error) max_error = err[i]/eigVal[i]; #else if(grad[i]/eigVal[i]>max_error) max_error = grad[i]/eigVal[i]; #endif } } node0_printf("\nEigenvalues after diagonalization at iteration %i\n",iter); for(i=0; i<Nvecs; i++) node0_printf("quot(%i) = %g +/- %8e |grad|=%g\n", i, eigVal[i], err[i], grad[i]); } node0_printf("BEGIN RESULTS\n"); for(i=0;i<Nvecs;i++){ node0_printf("Eigenvalue(%i) = %g +/- %8e\n", i,eigVal[i],err[i]); } #if 0 node0_printf("BEGIN EIGENVALUES\n"); for(i=0; i<Nvecs; i++) { double ev, er; ev = sqrt(eigVal[i]); er = err[i]/(2*ev); node0_printf("%.8g\t%g\n", ev, er); } node0_printf("END EIGENVALUES\n"); { QDP_Writer *qw; QDP_String *md; char evstring[100], *fn="eigenvecs.out"; md = QDP_string_create(); sprintf(evstring, "%i", Nvecs); QDP_string_set(md, evstring); qw = QDP_open_write(md, fn, QDP_SINGLEFILE); for(i=0; i<Nvecs; i++) { double ev, er; ev = sqrt(eigVal[i]); er = err[i]/(2*ev); sprintf(evstring, "%.8g\t%g", ev, er); QDP_string_set(md, evstring); QDP_write_V(qw, md, eigVec[i]); } QDP_close_write(qw); QDP_string_destroy(md); } #endif /** Deallocate the arrays **/ deAllocate(&V) ; deAllocate(&Array) ; free(err); free(grad); QDP_destroy_V(vec); cleanup_Matrix(); #ifdef DEBUG if(QDP_this_node==0) printf("end Kalkreuter_qdp\n"); #endif return total_iters; }
/************************** //Goldwasser-Micali HE system //len: length of params p,q **************************/ void GM(int len=512){ ZZ N, p, q, x; long m1, m2; ZZ BSm, HEm; //baseline and HE result ZZ c, c1, c2, cm1, cm2, r; //key gen start = std::clock(); GenPrime(p, len); GenPrime(q, len); mul(N, p, q); do{ RandomBnd(x, N); } while( (Jacobi(x,p)==1) || (Jacobi(x,q)==1)); cout<<"Jac:"<<Jacobi(x,p)<<Jacobi(x,q)<<endl; duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; cout<<"GM Setup:"<< duration <<'\n'; //Enc RandomBnd(m1,2); RandomBnd(m2,2); start = std::clock(); RandomBnd(r, N); MulMod(cm1,r,r,N); if(m1==1) MulMod(cm1,cm1,x,N); RandomBnd(r, N); MulMod(cm2,r,r,N); if(m2==1) MulMod(cm2,cm2,x,N); duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; cout<<"GM Enc:"<< duration <<'\n'; //Evaluation start = std::clock(); MulMod(c,cm1,cm2,N); duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; cout<<"GM Eval:"<< duration <<'\n'; //Dec start = std::clock(); HEm=Jacobi(c,p); duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; cout<<"GM Dec:"<< duration <<'\n'; if(HEm==1) HEm=0; else HEm=1; //baseline BSm=m1^m2; //xor //cout<<"plaintext:"<<m1<<m2<<BSm<<endl; assert(BSm==HEm); }
void Leaf::computeBboxOO() { //compute the covariance matrix double covMat[3][3], v[3][3]; areaWeightedCovarianceMatrix(covMat); DBGP("Cov mat:"); DBGST(print(covMat)); //perform singular value decomposition Jacobi(covMat, v); DBGP("eigenvalues:"); DBGST(print(covMat)); DBGP("eigenVectors:"); DBGST(print(v)); int first = 0, second = 1, third = 2; if (covMat[1][1] > covMat[0][0]) { std::swap(first, second); } if (covMat[2][2] > covMat[first][first]) { std::swap(first, third); } if (covMat[third][third] > covMat[second][second]) { std::swap(second, third); } DBGP("Eigenvalues: " << covMat[first][first] << " " << covMat[second][second] << " " << covMat[third][third]); //set up rotation matrix vec3 xAxis(v[0][first], v[1][first], v[2][first]); vec3 yAxis(v[0][second], v[1][second], v[2][second]); vec3 zAxis = normalise(xAxis) * normalise(yAxis); yAxis = zAxis * normalise(xAxis); xAxis = yAxis * zAxis; mat3 R(xAxis, yAxis, zAxis); DBGP("Matrix: " << R); //compute bbox extents vec3 halfSize, center; fitBox(R, center, halfSize); //rotate box so that x axis always points in the direction of largest extent first = 0; if (halfSize.y() > halfSize.x()) first = 1; if (halfSize.z() > halfSize[first]) first = 2; transf rotate = transf::IDENTITY; if (first == 1) { // y has the largest extent, rotate around z rotate = rotate_transf(M_PI/2.0, vec3(0,0,1)); } else if (first == 2) { // z has the largest extent, rotate around y rotate = rotate_transf(M_PI/2.0, vec3(0,1,0)); } halfSize = halfSize * rotate; for (int i=0; i<3; i++) { if (halfSize[i] < 0) halfSize[i] = -halfSize[i]; } mat3 RR; rotate.rotation().ToRotationMatrix(RR); R = RR * R; mBbox.halfSize = halfSize; mBbox.setTran( transf(R, center ) ); }
int main(){ int n = 200; double N = (double) n; double rho_max = 10.; double rho_min = 0.; double h = (rho_max - rho_min)/N; double omega_r = 1.; double TIME1, TIME2; vec rho(n), V(n-2), eigval; mat A = zeros<mat>(n-2,n-2); mat eigvec; clock_t start, finish, start2, finish2; // Declare start and finish time for(int i=1; i<n-1; i++){ rho[i] = rho_min + (i+1)*h; V[i] = omega_r*omega_r*rho[i]*rho[i] + 1./rho[i]; } rho[0] = 0.; rho[n-1] = rho_max; //Initializes the matrix A: A.diag(-1) += -1./(h*h); A.diag(+1) += -1./(h*h); A.diag() = (2./(h*h)) + V; // Eigenvector matrix: mat R = eye<mat>(n-2,n-2); // Built in eigenvalue/eigenvector procedure in armadillo: start2 = clock(); eig_sym(eigval, eigvec, A); finish2 = clock(); // Jacobi method: start = clock(); Jacobi(&A, &R, (n-2)); finish = clock(); TIME1 = (finish - start)/(double)CLOCKS_PER_SEC; TIME2 = (finish2 - start2)/(double)CLOCKS_PER_SEC; vec l; l = A.diag(); // Wavefunction for the lowest energy level: double MIN = max(l); int index = 0; for(int i=0; i<n-2; i++){ if(l(i) <= MIN){ MIN = l(i); index = i; } } vec u_1 = R.col(index); vec wave(n); for(int i=1; i<n-1; i++){ wave(i) = u_1(i-1); } // Dirichlet boundary conditions wave(0) = 0; wave(n-1) = 0; //Writing wave and rho to file: string filename; string Omega = static_cast<ostringstream*>( \ &(ostringstream() << omega_r*100) )->str(); filename = "wavefunc2_omega"+Omega+".dat"; ofile.open(filename.c_str(), ios::out); for(int i=0; i<n; i++){ ofile << rho[i] << ' ' << wave[i] << endl; } ofile.close(); vec lam = sort(l); cout << "omega_r = " << omega_r << endl; cout << "three lowest eigenvalues:" << endl; for(int i=0;i<3;i++){ cout << "lambda = " << lam(i) << endl; } cout << "computation time for jacobi.cpp with n = " << n << ": " << TIME1 << " s" << endl; cout << "computation time for built in procedure with n = " << n << ": " << TIME2 << " s" << endl; return 0; }
int main() { clock_t start, stop; float lambda0 = 1.0, largeLambda, smallLambda; float tol = 0.00005, error = 10.0 * tol; float normSq, norm; int maxIter = 200, iter = 0, i; InitializeMatVec(); for (i = 0; i < rowMat * colMat; i++) { printf("i = %d \t A = %f \n", i, A[i]); // column-wise } // step 1: query platform info status = clGetPlatformIDs(0, NULL, &numPlatforms); platforms = (cl_platform_id*)malloc(numPlatforms *sizeof(cl_platform_id)); status = clGetPlatformIDs(numPlatforms, platforms, NULL); if (status != 0) { printf("Step 1: Failed to discover available platforms. Error code = %d. \n", status); } // step 2: query devices status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, 0, NULL, &numDevices); devices = (cl_device_id*)malloc(numDevices *sizeof(cl_device_id)); status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, numDevices, devices, NULL); if (status != 0) { printf("Step 2: Failed to discover available devices. Error code = %d. \n", status); } // step 3: create context for matrix-vector multiplication powerContext = clCreateContext(NULL, numDevices, devices, NULL, NULL, &powerStatus); if (powerStatus != 0) { printf("Step 3 kernel Power. Failed to create the context. Error code = %d. \n", powerStatus); } // step 3: create context for Jacobi iterative method iPowerContext = clCreateContext(NULL, numDevices, devices, NULL, NULL, &iPowerStatus); if (iPowerStatus != 0) { printf("Step 3 kernel iPower. Failed to create the context. Error code = %d. \n", iPowerStatus); } // step 4: set up command queue for matrix-vector multiplication powerQueue = clCreateCommandQueue(powerContext, devices[0], 0, &powerStatus); if (powerStatus != 0) { printf("Step 4 kernel Power. Failed to set up the command queue. Error code = %d. \n", powerStatus); } // step 4: set up command queue for Jacobi iterative method iPowerQueue = clCreateCommandQueue(iPowerContext, devices[0], 0, &iPowerStatus); if (iPowerStatus != 0) { printf("Step 4 kernel iPower. Failed to set up the command queue. Error code = %d. \n", iPowerStatus); } start = clock(); /* CALCULATE LARGEST EIGENVALUE */ MatVecMult(B); // obtain C, where C = A * B while (error > tol && iter < maxIter) { normSq = VecMult(C, C); norm = sqrt(normSq); for (i = 0; i < rowVec * colVec; i++) { Chat[i] = C[i] / norm; // normalize C } // obtain Rayleigh quotient tranpose(Chat) * A * Chat MatVecMult(Chat); // 1st part of Rayleigh. Also updates old C with new C. largeLambda = VecMult(Chat, C); // continue with 2nd part error = fabs((largeLambda - lambda0) / largeLambda); // relative error lambda0 = largeLambda; // update old eigenvalue with new one iter += 1; } // reset variables for next calculation lambda0 = 1.0, error = 10.0 * tol, maxIter = 200, iter = 0, normSq = 0.0, norm = 0.0; /* CALCULATE SMALLEST EIGENVALUE */ Jacobi(B, B); // Solve A * B = B. The solution is labeled X. while (error > tol && iter < maxIter) { normSq = VecMult(X, X); norm = sqrt(normSq); for (i = 0; i < rowVec * colVec; i++) { Xhat[i] = X[i] / norm; // normalize X } // obtain Rayleigh quotient tranpose(Xhat) * A * Xhat MatVecMult(Xhat); // 1st part of Rayleigh: C = transpose(Xhat) * A smallLambda = VecMult(C, Xhat); // 2nd part of Rayleigh error = fabs((smallLambda - lambda0) / smallLambda); // relative error lambda0 = smallLambda; // update old smallest eigenvalue with new one Jacobi(X, Xhat); // update old solution with new one iter += 1; } stop = clock(); printf("The largest eigenvalue is %f \n", largeLambda); printf("The smallest eigenvalue is %f \n", smallLambda); printf("The condition number of the matrix is %f \n", fabs(largeLambda / smallLambda)); printf("OpenCL CPU elapsed time = %f seconds. \n", (double)(stop - start) / CLOCKS_PER_SEC); // step 14: free used memory clReleaseKernel(powerKernel); clReleaseKernel(iPowerKernel); clReleaseProgram(powerProgram); clReleaseProgram(iPowerProgram); clReleaseCommandQueue(powerQueue); clReleaseCommandQueue(iPowerQueue); clReleaseMemObject(bufferA); clReleaseMemObject(bufferB); clReleaseMemObject(bufferC); clReleaseMemObject(bufferX); clReleaseContext(powerContext); clReleaseContext(iPowerContext); free(A); free(B); free(C); free(Chat); free(X); free(Xhat); free(platforms); free(devices); if (status != 0) { printf("Step 13: Failed to deallocate memory. Error code = %d. \n", status); } }
// find the optimal superposition transform (translation, rotation, translation) // return rmsd resulting from it // both pointsets are unchanged float findSuperpositionTransform( vector<vector<float> > & from, vector<vector<float> > & onto, vector<float> & t1, vector<vector<float> > & rot, vector<float> & t2 ) { if(from.size() != onto.size()) { cout << "superpose called with unequal pointsets" << endl; exit(1); } // find center of mass and translate the pointsets so that they are at origin vector<float> CMfrom(3), CMonto(3); for(int i=0; i < 3; i++) { CMfrom[i] = 0; CMonto[i] = 0.; } for(int pi=0; pi < from.size(); pi++) for(int i=0; i < 3; i++) { CMfrom[i] += from[pi][i]; CMonto[i] += onto[pi][i]; } for(int i=0; i < 3; i++) { CMfrom[i] /= from.size(); CMonto[i] /= from.size(); } // find distance matrix vector<float> q(4,0), m(3,0), p(3,0); // minus and plus vector<vector<float> > Q(4,q); // quaternion //for(int qi=0; qi < 4; qi++) cout << "Q " << Q[qi][0] <<" "<< Q[qi][1] <<" "<< Q[qi][2] <<" "<< Q[qi][3] << endl; float xm, ym, zm, xp, yp, zp; for(int pi=0; pi < from.size(); pi++) { xm = from[pi][0]-CMfrom[0] - onto[pi][0]+CMonto[0]; xp = onto[pi][0]-CMonto[0] + from[pi][0]-CMfrom[0]; ym = from[pi][1]-CMfrom[1] - onto[pi][1]+CMonto[1]; yp = onto[pi][1]-CMonto[1] + from[pi][1]-CMfrom[1]; zm = from[pi][2]-CMfrom[2] - onto[pi][2]+CMonto[2]; zp = onto[pi][2]-CMonto[2] + from[pi][2]-CMfrom[2]; Q[0][0] += xm*xm + ym*ym + zm*zm; Q[1][1] += yp*yp + zp*zp + xm*xm; Q[2][2] += xp*xp + zp*zp + ym*ym; Q[3][3] += xp*xp + yp*yp + zm*zm; Q[0][1] += yp*zm - ym*zp; Q[0][2] += xm*zp - xp*zm; Q[0][3] += xp*ym - xm*yp; Q[1][2] += xm*ym - xp*yp; Q[1][3] += xm*zm - xp*zp; Q[2][3] += ym*zm - yp*zp; } Q[1][0]=Q[0][1]; Q[2][0]=Q[0][2]; Q[2][1]=Q[1][2]; Q[3][0]=Q[0][3]; Q[3][1]=Q[1][3]; Q[3][2]=Q[2][3]; //for(int qi=0; qi < 4; qi++) cout << "Q " << Q[qi][0] <<" "<< Q[qi][1] <<" "<< Q[qi][2] <<" "<< Q[qi][3] << endl; vector<float> eigvals(4,0); vector<vector<float> > eigvecs(4,eigvals); if(Jacobi(Q, eigvals, eigvecs)) { cout << "Jacobi did not converge" << endl; exit(1); } //for(int ei=0; ei < 4; ei++) cout <<"EIG "<< eigvals[ei] <<" : "<< eigvecs[0][ei] <<" "<< eigvecs[1][ei] <<" "<< eigvecs[2][ei] <<" "<< eigvecs[3][ei] << endl; float rmsd; if(eigvals[3] < 0) rmsd = 0; else rmsd = sqrt(eigvals[3]/from.size()); // construct rotation matrix from eigenvector with lowest eigenvalue float q1 = eigvecs[0][3], q2 = eigvecs[1][3], q3 = eigvecs[2][3], q4 = eigvecs[3][3]; float q1sqr=q1*q1, q2sqr=q2*q2, q3sqr=q3*q3, q4sqr=q4*q4; float q1q2=q1*q2, q1q3=q1*q3, q1q4=q1*q4; float q2q3=q2*q3, q2q4=q2*q4, q3q4=q3*q4; rot[0][0] = q1sqr + q2sqr - q3sqr -q4sqr; rot[0][1] = 2.0 * (q2q3 - q1q4); rot[0][2] = 2.0 * (q2q4 + q1q3); rot[1][0] = 2.0 * (q2q3 + q1q4); rot[1][1] = q1sqr + q3sqr - q2sqr - q4sqr; rot[1][2] = 2.0 * (q3q4 - q1q2); rot[2][0] = 2.0 * (q2q4 - q1q3); rot[2][1] = 2.0 * (q3q4 + q1q2); rot[2][2] = q1sqr + q4sqr -q2sqr - q3sqr; for(int i=0; i < 3; i++) t1[i] = -1 * CMfrom[i]; for(int i=0; i < 3; i++) t2[i] = CMonto[i]; return rmsd; }