void BK_algo(int64_t Graph[]) { //printf("BK_algo\n"); if(NULL == Graph ) { printf("graph is null\n"); return ; } int64_t R[LINE]={0},P[LINE] = {0},X[LINE] = {0}; int i ; for(i = 0 ; i < Count ; ++ i) { P[i] = Graph[i*SHARE + 0]; // printf("lld:%lld\t",P[i]); } BK(Graph,R,P,X); }
void RSA::createNewKey(char *filePrivateKey,char * filePublicKey) { PrGlib dnthang; ofstream PK(filePrivateKey); ofstream BK(filePublicKey); /*Generate p and q as strong primes */ ZZZ p=dnthang.PrG_generate_strong_prime(3072); ZZZ q=dnthang.PrG_generate_strong_prime(3072); ZZZ n=q*p; ZZZ phi=(p^1)*(q^1); ZZZ e,k; /*Find e such that gcd(e,phi)=1*/ do { gmp_randclass rr(gmp_randinit_default); rr.seed(time(NULL)); e =rr.get_z_bits(dnthang.PrG_get_length()); ZZZ num = e & 1; if (num == 0)e = e | 1; mpz_gcd (k.get_mpz_t(),e.get_mpz_t(), phi.get_mpz_t()); }while(k!=1); /*******************************/ /*Compute d= e^-1 mod n*/ ZZZ d; mpz_invert(d.get_mpz_t(),e.get_mpz_t(),phi.get_mpz_t()); /*private key*/ PK<<n.get_str()<<endl; PK<<d.get_str()<<endl; /*public key*/ BK<<n.get_str()<<endl; BK<<e.get_str()<<endl; PK.clear(); PK.close(); BK.clear(); BK.close(); }
void BK(int64_t *Graph, int64_t *R, int64_t *P, int64_t *X ) { if(0 == P[0] && 0 == X[0]) { // printf("get clique:\n"); int j ; printf("%lld:",USER); for(j=0;R[j] !=0 ; ++j) { printf("%lld\t",R[j]); } printf("\n"); } else if(0==P[0]) { } else { int i=0; int64_t P_copy[LINE]={0}; CopyArray(P,P_copy); while(P[i] != 0) { int64_t R_1[LINE]={0}, P_1[LINE]={0}, X_1[LINE] = {0}; CopyArray(R,R_1); AddNode(R_1,P[i]); int pos=Find(Graph,P[i]); Insect(P_copy,Graph+pos*SHARE+1,P_1); Insect(X,Graph+pos*SHARE+1,X_1); BK(Graph,R_1,P_1,X_1); DelNode(P_copy,P[i]); AddNode(X,P[i]); ++i; } } }
//form residual and tangent void ShellMITC9::formResidAndTangent( int tang_flag ) { // // six(6) nodal dof's ordered : // // - - // | u1 | <---plate membrane // | u2 | // |----------| // | w = u3 | <---plate bending // | theta1 | // | theta2 | // |----------| // | theta3 | <---drill // - - // // membrane strains ordered : // // strain(0) = eps00 i.e. (11)-strain // strain(1) = eps11 i.e. (22)-strain // strain(2) = gamma01 i.e. (12)-shear // // curvatures and shear strains ordered : // // strain(3) = kappa00 i.e. (11)-curvature // strain(4) = kappa11 i.e. (22)-curvature // strain(5) = 2*kappa01 i.e. 2*(12)-curvature // // strain(6) = gamma02 i.e. (13)-shear // strain(7) = gamma12 i.e. (23)-shear // // same ordering for moments/shears but no 2 // // Then, // epsilon00 = -z * kappa00 + eps00_membrane // epsilon11 = -z * kappa11 + eps11_membrane // gamma01 = 2*epsilon01 = -z * (2*kappa01) + gamma01_membrane // // Shear strains gamma02, gamma12 constant through cross section // static const int ndf = 6 ; //two membrane plus three bending plus one drill static const int nstress = 8 ; //three membrane, three moment, two shear static const int ngauss = 9 ; static const int numnodes = 9 ; int i, j, k, p, q ; int jj, kk ; int node ; int success ; double volume = 0.0 ; static double xsj ; // determinant jacaobian matrix static double dvol[ngauss] ; //volume element static Vector strain(nstress) ; //strain static double shp[3][numnodes] ; //shape functions at a gauss point static Vector residJ(ndf) ; //nodeJ residual static Matrix stiffJK(ndf,ndf) ; //nodeJK stiffness static Vector stress(nstress) ; //stress resultants static Matrix dd(nstress,nstress) ; //material tangent double epsDrill = 0.0 ; //drilling "strain" double tauDrill = 0.0 ; //drilling "stress" //---------B-matrices------------------------------------ static Matrix BJ(nstress,ndf) ; // B matrix node J static Matrix BJtran(ndf,nstress) ; static Matrix BK(nstress,ndf) ; // B matrix node k static Matrix BJtranD(ndf,nstress) ; static Matrix Bbend(3,3) ; // bending B matrix static Matrix Bshear(2,3) ; // shear B matrix static Matrix Bmembrane(3,2) ; // membrane B matrix static double BdrillJ[ndf] ; //drill B matrix static double BdrillK[ndf] ; double *drillPointer ; static double saveB[nstress][ndf][numnodes] ; //------------------------------------------------------- //zero stiffness and residual stiff.Zero( ) ; resid.Zero( ) ; //compute Jacobian and inverse at center double L1 = 0.0 ; double L2 = 0.0 ; //gauss loop for ( i = 0; i < ngauss; i++ ) { //get shape functions shape2d( sg[i], tg[i], xl, shp, xsj ) ; //volume element to also be saved dvol[i] = wg[i] * xsj ; volume += dvol[i] ; //zero the strains strain.Zero( ) ; epsDrill = 0.0 ; // j-node loop to compute strain for ( j = 0; j < numnodes; j++ ) { //compute B matrix Bmembrane = computeBmembrane( j, shp ) ; Bbend = computeBbend( j, shp ) ; Bshear = computeBshear( j, shp ) ; BJ = assembleB( Bmembrane, Bbend, Bshear ) ; //save the B-matrix for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ) { saveB[p][q][j] = BJ(p,q) ; }//end for q }//end for p //nodal "displacements" const Vector &ul = nodePointers[j]->getTrialDisp( ) ; //compute the strain //strain += (BJ*ul) ; strain.addMatrixVector(1.0, BJ,ul,1.0 ) ; //drilling B matrix drillPointer = computeBdrill( j, shp ) ; for (p=0; p<ndf; p++ ) { //BdrillJ[p] = *drillPointer++ ; BdrillJ[p] = *drillPointer ; //set p-th component drillPointer++ ; //pointer arithmetic }//end for p //drilling "strain" for ( p = 0; p < ndf; p++ ) epsDrill += BdrillJ[p]*ul(p) ; } // end for j //send the strain to the material success = materialPointers[i]->setTrialSectionDeformation( strain ) ; //compute the stress stress = materialPointers[i]->getStressResultant( ) ; //drilling "stress" tauDrill = Ktt * epsDrill ; //multiply by volume element stress *= dvol[i] ; tauDrill *= dvol[i] ; if ( tang_flag == 1 ) { dd = materialPointers[i]->getSectionTangent( ) ; dd *= dvol[i] ; } //end if tang_flag //residual and tangent calculations node loops jj = 0 ; for ( j = 0; j < numnodes; j++ ) { //extract BJ for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ) BJ(p,q) = saveB[p][q][j] ; }//end for p //multiply bending terms by (-1.0) for correct statement // of equilibrium for ( p = 3; p < 6; p++ ) { for ( q = 3; q < 6; q++ ) BJ(p,q) *= (-1.0) ; } //end for p //transpose //BJtran = transpose( 8, ndf, BJ ) ; for (p=0; p<ndf; p++) { for (q=0; q<nstress; q++) BJtran(p,q) = BJ(q,p) ; }//end for p //residJ = BJtran * stress ; residJ.addMatrixVector(0.0, BJtran,stress,1.0 ) ; //drilling B matrix drillPointer = computeBdrill( j, shp ) ; for (p=0; p<ndf; p++ ) { BdrillJ[p] = *drillPointer ; drillPointer++ ; }//end for p //residual including drill for ( p = 0; p < ndf; p++ ) resid( jj + p ) += ( residJ(p) + BdrillJ[p]*tauDrill ) ; if ( tang_flag == 1 ) { //BJtranD = BJtran * dd ; BJtranD.addMatrixProduct(0.0, BJtran,dd,1.0 ) ; for (p=0; p<ndf; p++) { BdrillJ[p] *= ( Ktt*dvol[i] ) ; }//end for p kk = 0 ; for ( k = 0; k < numnodes; k++ ) { //extract BK for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ){ BK(p,q) = saveB[p][q][k]; }//end for q }//end for p //drilling B matrix drillPointer = computeBdrill( k, shp ) ; for (p=0; p<ndf; p++ ) { BdrillK[p] = *drillPointer ; drillPointer++ ; }//end for p //stiffJK = BJtranD * BK ; // + transpose( 1,ndf,BdrillJ ) * BdrillK ; stiffJK.addMatrixProduct(0.0, BJtranD,BK,1.0 ) ; for ( p = 0; p < ndf; p++ ) { for ( q = 0; q < ndf; q++ ) { stiff( jj+p, kk+q ) += stiffJK(p,q) + ( BdrillJ[p]*BdrillK[q] ) ; }//end for q }//end for p kk += ndf ; } // end for k loop } // end if tang_flag jj += ndf ; } // end for j loop } //end for i gauss loop return ; }
//return secant matrix const Matrix& ShellMITC9::getInitialStiff( ) { if (Ki != 0) return *Ki; static const int ndf = 6 ; //two membrane plus three bending plus one drill static const int nstress = 8 ; //three membrane, three moment, two shear static const int ngauss = 9 ; static const int numnodes = 9 ; int i, j, k, p, q ; int jj, kk ; int node ; double volume = 0.0 ; static double xsj ; // determinant jacaobian matrix static double dvol[ngauss] ; //volume element static double shp[3][numnodes] ; //shape functions at a gauss point static Matrix stiffJK(ndf,ndf) ; //nodeJK stiffness static Matrix dd(nstress,nstress) ; //material tangent //static Matrix J0(2,2) ; //Jacobian at center //static Matrix J0inv(2,2) ; //inverse of Jacobian at center //---------B-matrices------------------------------------ static Matrix BJ(nstress,ndf) ; // B matrix node J static Matrix BJtran(ndf,nstress) ; static Matrix BK(nstress,ndf) ; // B matrix node k static Matrix BJtranD(ndf,nstress) ; static Matrix Bbend(3,3) ; // bending B matrix static Matrix Bshear(2,3) ; // shear B matrix static Matrix Bmembrane(3,2) ; // membrane B matrix static double BdrillJ[ndf] ; //drill B matrix static double BdrillK[ndf] ; double *drillPointer ; static double saveB[nstress][ndf][numnodes] ; //------------------------------------------------------- stiff.Zero( ) ; //compute Jacobian and inverse at center double L1 = 0.0 ; double L2 = 0.0 ; //computeJacobian( L1, L2, xl, J0, J0inv ) ; //gauss loop for ( i = 0; i < ngauss; i++ ) { //get shape functions shape2d( sg[i], tg[i], xl, shp, xsj ) ; //volume element to also be saved dvol[i] = wg[i] * xsj ; volume += dvol[i] ; // j-node loop to compute strain for ( j = 0; j < numnodes; j++ ) { //compute B matrix Bmembrane = computeBmembrane( j, shp ) ; Bbend = computeBbend( j, shp ) ; Bshear = computeBshear( j, shp ) ; BJ = assembleB( Bmembrane, Bbend, Bshear ) ; //save the B-matrix for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ) saveB[p][q][j] = BJ(p,q) ; }//end for p //drilling B matrix drillPointer = computeBdrill( j, shp ) ; for (p=0; p<ndf; p++ ) { //BdrillJ[p] = *drillPointer++ ; BdrillJ[p] = *drillPointer ; //set p-th component drillPointer++ ; //pointer arithmetic }//end for p } // end for j dd = materialPointers[i]->getInitialTangent( ) ; dd *= dvol[i] ; //residual and tangent calculations node loops jj = 0 ; for ( j = 0; j < numnodes; j++ ) { //extract BJ for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ) BJ(p,q) = saveB[p][q][j] ; }//end for p //multiply bending terms by (-1.0) for correct statement // of equilibrium for ( p = 3; p < 6; p++ ) { for ( q = 3; q < 6; q++ ) BJ(p,q) *= (-1.0) ; } //end for p //transpose //BJtran = transpose( 8, ndf, BJ ) ; for (p=0; p<ndf; p++) { for (q=0; q<nstress; q++) BJtran(p,q) = BJ(q,p) ; }//end for p //drilling B matrix drillPointer = computeBdrill( j, shp ) ; for (p=0; p<ndf; p++ ) { BdrillJ[p] = *drillPointer ; drillPointer++ ; }//end for p //BJtranD = BJtran * dd ; BJtranD.addMatrixProduct(0.0, BJtran,dd,1.0 ) ; for (p=0; p<ndf; p++) BdrillJ[p] *= ( Ktt*dvol[i] ) ; kk = 0 ; for ( k = 0; k < numnodes; k++ ) { //extract BK for (p=0; p<nstress; p++) { for (q=0; q<ndf; q++ ) BK(p,q) = saveB[p][q][k] ; }//end for p //drilling B matrix drillPointer = computeBdrill( k, shp ) ; for (p=0; p<ndf; p++ ) { BdrillK[p] = *drillPointer ; drillPointer++ ; }//end for p //stiffJK = BJtranD * BK ; // + transpose( 1,ndf,BdrillJ ) * BdrillK ; stiffJK.addMatrixProduct(0.0, BJtranD,BK,1.0 ) ; for ( p = 0; p < ndf; p++ ) { for ( q = 0; q < ndf; q++ ) { stiff( jj+p, kk+q ) += stiffJK(p,q) + ( BdrillJ[p]*BdrillK[q] ) ; }//end for q }//end for p kk += ndf ; } // end for k loop jj += ndf ; } // end for j loop } //end for i gauss loop Ki = new Matrix(stiff); return stiff ; }
// Choose an optimal number of levels between Kmin and Kmax size_t select_levels(const std::vector<double> & x, const std::vector< std::vector< size_t > > & B, size_t Kmin, size_t Kmax) { if (Kmin == Kmax) { return Kmin; } const std::string method = "normal"; // "uniform" or "normal" size_t Kopt = Kmin; const size_t base = 1; // The position of first element in x: 1 or 0. const size_t N = x.size() - base; double maxBIC; for(size_t K = Kmin; K <= Kmax; ++K) { std::vector< std::vector< size_t > > BK(B.begin(), B.begin()+K+1); ClusterResult result; // Backtrack the matrix to determine boundaries between the bins. backtrack(x, BK, result); size_t indexLeft = base; size_t indexRight; double loglikelihood = 0; double binLeft, binRight; for (size_t k = 0; k < K; ++k) { // Compute the likelihood size_t numPointsInBin = result.size[k+base]; indexRight = indexLeft + numPointsInBin - 1; /* Use mid point inbetween two clusters as boundary binLeft = ( indexLeft == base ) ? x[base] : (x[indexLeft-1] + x[indexLeft]) / 2; binRight = ( indexRight < N-1+base ) ? (x[indexRight] + x[indexRight+1]) / 2 : x[N-1+base]; */ if(x[indexLeft] < x[indexRight]) { binLeft = x[indexLeft]; binRight = x[indexRight]; } else if(x[indexLeft] == x[indexRight]) { binLeft = ( indexLeft == base ) ? x[base] : (x[indexLeft-1] + x[indexLeft]) / 2; binRight = ( indexRight < N-1+base ) ? (x[indexRight] + x[indexRight+1]) / 2 : x[N-1+base]; } else { throw "ERROR: binLeft > binRight"; // cout << "ERROR: binLeft > binRight" << endl; } double binWidth = binRight - binLeft; if(method == "uniform") { loglikelihood += numPointsInBin * std::log(numPointsInBin / binWidth / N); } else if(method == "normal") { double mean = 0.0; double variance = 0.0; for (size_t i = indexLeft; i <= indexRight; ++i) { mean += x[i]; variance += x[i] * x[i]; } mean /= numPointsInBin; if (numPointsInBin > 1) { variance = (variance - numPointsInBin * mean * mean) / (numPointsInBin - 1); } else { variance = 0; } if (variance > 0) { for (size_t i = indexLeft; i <= indexRight; ++i) { loglikelihood += - (x[i] - mean) * (x[i] - mean) / (2.0 * variance); } loglikelihood += numPointsInBin * (std::log(numPointsInBin / (double) N) - 0.5 * std::log ( 2 * M_PI * variance)); } else { loglikelihood += numPointsInBin * std::log(1.0 / binWidth / N); } } else { throw "ERROR: Wrong likelihood method!"; // cout << "ERROR: Wrong likelihood method" << endl; } indexLeft = indexRight + 1; } double BIC = 0.0; // Compute the Bayesian information criterion if (method == "uniform") { BIC = 2 * loglikelihood - (3 * K - 1) * std::log((double)N); // K-1 } else if(method == "normal") { BIC = 2 * loglikelihood - (3 * K - 1) * std::log((double)N); //(K*3-1) } // cout << ", Loglh=" << loglikelihood << ", BIC=" << BIC << endl; if (K == Kmin) { maxBIC = BIC; Kopt = Kmin; } else { if (BIC > maxBIC) { maxBIC = BIC; Kopt = K; } } } return Kopt; }