itpp::cmat STC::diag_pow(const itpp::cmat &in_mat, double in_exp) //first input should be a diagonal square matrix with complex elements { register int n; int dim = in_mat.rows(); itpp::cmat out_mat(dim,dim); out_mat.zeros(); for (n=0; n<dim; n++) { out_mat(n,n) = std::pow(in_mat(n,n), in_exp); } return out_mat; }
bool probing_test(Epetra_CrsMatrix & in_mat, bool build_list){ const Epetra_CrsGraph & graph=in_mat.Graph(); Teuchos::ParameterList main, zoltan; if(build_list){ // zoltan.set("DISTANCE","2"); main.set("ZOLTAN",zoltan); } Isorropia::Epetra::Prober prober(Teuchos::rcp<const Epetra_CrsGraph>(&graph,false),main); Epetra_CrsMatrix out_mat(Copy,graph); int rv=prober.probe(in_mat,out_mat); if(rv!=0) {printf("ERROR: probing failed\n");return false;} #ifdef HAVE_EPETRAEXT EpetraExt::MatrixMatrix::Add(in_mat,false,1,out_mat,-1); double nrm=out_mat.NormInf()/in_mat.NormInf(); if(!in_mat.Comm().MyPID()) printf("diff norm = %22.16e\n",nrm); if(nrm < 1e-12) return true; else return false; #endif return true; }
Eigen::Matrix4f eigenFromTf(const tf::Transform& tf) { Eigen::Matrix4f out_mat; double mv[12]; tf.getBasis().getOpenGLSubMatrix(mv); tf::Vector3 origin = tf.getOrigin(); out_mat(0, 0) = mv[0]; out_mat(0, 1) = mv[4]; out_mat(0, 2) = mv[8]; out_mat(1, 0) = mv[1]; out_mat(1, 1) = mv[5]; out_mat(1, 2) = mv[9]; out_mat(2, 0) = mv[2]; out_mat(2, 1) = mv[6]; out_mat(2, 2) = mv[10]; out_mat(3, 0) = out_mat(3, 1) = out_mat(3, 2) = 0; out_mat(3, 3) = 1; out_mat(0, 3) = origin.x(); out_mat(1, 3) = origin.y(); out_mat(2, 3) = origin.z(); return out_mat; }
/* * Returns - * -2 unknown keyword * -1 error in processing keyword * 0 OK */ int multi_words( char *words[], int nwords ) { if ( strcmp( words[0], "rot" ) == 0 ) { mat_t mat; /* Expects rotations rx, ry, rz, in degrees */ if ( nwords < 4 ) return(-1); MAT_IDN( mat ); bn_mat_angles( mat, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "xlate" ) == 0 ) { mat_t mat; if ( nwords < 4 ) return(-1); /* Expects translations tx, ty, tz */ MAT_IDN( mat ); MAT_DELTAS( mat, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "rot_at" ) == 0 ) { mat_t mat; mat_t mat1; mat_t mat2; mat_t mat3; /* JG - Expects x, y, z, rx, ry, rz */ /* Translation back to the origin by (-x, -y, -z) */ /* is done first, then the rotation, and finally */ /* back into the original position by (+x, +y, +z). */ if ( nwords < 7 ) return(-1); MAT_IDN( mat1 ); MAT_IDN( mat2 ); MAT_IDN( mat3 ); MAT_DELTAS( mat1, -atof( words[1] ), -atof( words[2] ), -atof( words[3] ) ); bn_mat_angles( mat2, atof( words[4] ), atof( words[5] ), atof( words[6] ) ); MAT_DELTAS( mat3, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); bn_mat_mul( mat, mat2, mat1 ); bn_mat_mul2( mat3, mat ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "orient" ) == 0 ) { register int i; mat_t mat; double args[8]; /* Expects tx, ty, tz, rx, ry, rz, [scale]. */ /* All rotation is done first, then translation */ /* Note: word[0] and args[0] are the keyword */ if ( nwords < 6+1 ) return(-1); for ( i=1; i<6+1; i++ ) args[i] = 0; args[7] = 1.0; /* optional arg, default to 1 */ for ( i=1; i<nwords; i++ ) args[i] = atof( words[i] ); MAT_IDN( mat ); bn_mat_angles( mat, args[4], args[5], args[6] ); MAT_DELTAS( mat, args[1], args[2], args[3] ); if ( NEAR_ZERO( args[7], VDIVIDE_TOL ) ) { /* Nearly zero, signal error */ fprintf(stderr, "Orient scale arg is near zero ('%s')\n", words[7] ); return(-1); } else { mat[15] = 1 / args[7]; } out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "ae" ) == 0 ) { mat_t mat; fastf_t az, el; if ( nwords < 3 ) return(-1); /* Expects azimuth, elev, optional twist */ az = atof(words[1]); el = atof(words[2]); #if 0 if ( nwords == 3 ) twist = 0.0; else twist = atof(words[3]); #endif MAT_IDN( mat ); /* XXX does not take twist, for now XXX */ bn_mat_ae( mat, az, el ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "arb_rot_pt" ) == 0 ) { mat_t mat; point_t pt1, pt2; vect_t dir; fastf_t ang; if ( nwords < 1+3+3+1 ) return(-1); /* Expects point1, point2, angle */ VSET( pt1, atof(words[1]), atof(words[2]), atof(words[3]) ); VSET( pt2, atof(words[4]), atof(words[5]), atof(words[6]) ); ang = atof(words[7]) * bn_degtorad; VSUB2( dir, pt2, pt2 ); VUNITIZE(dir); MAT_IDN( mat ); bn_mat_arb_rot( mat, pt1, dir, ang ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "arb_rot_dir" ) == 0 ) { mat_t mat; point_t pt1; vect_t dir; fastf_t ang; if ( nwords < 1+3+3+1 ) return(-1); /* Expects point1, dir, angle */ VSET( pt1, atof(words[1]), atof(words[2]), atof(words[3]) ); VSET( dir, atof(words[4]), atof(words[5]), atof(words[6]) ); ang = atof(words[7]) * bn_degtorad; VUNITIZE(dir); MAT_IDN( mat ); bn_mat_arb_rot( mat, pt1, dir, ang ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "quat" ) == 0 ) { mat_t mat; quat_t quat; /* Usage: quat x, y, z, w */ if ( nwords < 5 ) return -1; QSET( quat, atof(words[1]), atof(words[2]), atof(words[3]), atof(words[4]) ); quat_quat2mat( mat, quat ); out_mat( mat, stdout); return 0; } if ( strcmp( words[0], "fromto" ) == 0 ) { mat_t mat; point_t cur; point_t next; vect_t from; vect_t to; /* Usage: fromto +Z cur_xyz next_xyz */ if ( nwords < 8 ) return -1; if ( strcmp( words[1], "+X" ) == 0 ) { VSET( from, 1, 0, 0 ); } else if ( strcmp( words[1], "-X" ) == 0 ) { VSET( from, -1, 0, 0 ); } else if ( strcmp( words[1], "+Y" ) == 0 ) { VSET( from, 0, 1, 0 ); } else if ( strcmp( words[1], "-Y" ) == 0 ) { VSET( from, 0, -1, 0 ); } else if ( strcmp( words[1], "+Z" ) == 0 ) { VSET( from, 0, 0, 1 ); } else if ( strcmp( words[1], "-Z" ) == 0 ) { VSET( from, 0, 0, -1 ); } else { fprintf(stderr, "fromto '%s' is not +/-XYZ\n", words[1]); return -1; } VSET( cur, atof(words[2]), atof(words[3]), atof(words[4]) ); VSET( next, atof(words[5]), atof(words[6]), atof(words[7]) ); VSUB2( to, next, cur ); VUNITIZE(to); bn_mat_fromto( mat, from, to ); /* Check to see if it worked. */ { vect_t got; MAT4X3VEC( got, mat, from ); if ( VDOT( got, to ) < 0.9 ) { bu_log("\ntabsub ERROR: At t=%s, bn_mat_fromto failed!\n", chanwords[0] ); VPRINT("\tfrom", from); VPRINT("\tto", to); VPRINT("\tgot", got); } } out_mat( mat, stdout ); return 0; } return(-2); /* Unknown keyword */ }
int main(){ long m, r, p, L, c, w, s, d, security, enc, dec, encMul, recommended; char tempChar; bool toPrint = false, toSave = false; //Scan parameters cout << "Enter HElib's keys paramter. Enter zero for the recommended values" << endl; while(true){ cout << "Enter the field of the computations (a prime number): "; cin >> p; if(isPrime(p)) break; cout << "Error! p must be a prime number! " << endl; } while(true){ recommended = 1; cout << "Enter r (recommended " << recommended <<"): "; cin >> r; if(r == 0) r = recommended; if(r > 0) break; cout << "Error! r must be a positive number!" << endl; } while(true){ recommended = 16; cout << "Enter L (recommended " << recommended <<"): "; cin >> L; if(L == 0) L = recommended; if(L > 1) break; cout << "Error! L must be a positive number!" << endl; } while(true){ recommended = 3; cout << "Enter c (recommended " << recommended <<"): "; cin >> c; if(c == 0) c = recommended; if(c > 1) break; cout << "Error! c must be a positive number!" << endl; } while(true){ recommended = 64; cout << "Enter w (recommended " << recommended <<"): "; cin >> w; if(w == 0) w = recommended; if(w > 1) break; cout << "Error! w must be a positive number!" << endl; } while(true){ recommended = 0; cout << "Enter d (recommended " << recommended <<"): "; cin >> d; if(d >= 0) break; cout << "Error! d must be a positive or zero!" << endl; } while(true){ recommended = 0; cout << "Enter s (recommended " << recommended <<"): "; cin >> s; if(s >= 0) break; cout << "Error! s must be a positive or zero!" << endl; } while(true){ recommended = 128; cout << "Enter security (recommended " << recommended << "): "; cin >> security; if(security == 0) security = recommended; if(security >= 1) break; cout << "Error! security must be a positive number " << endl; } ZZX G; m = FindM(security,L,c,p, d, s, 0); FHEcontext context(m, p, r); // initialize context buildModChain(context, L, c); // modify the context, adding primes to the modulus chain FHESecKey secretKey(context); // construct a secret key structure const FHEPubKey& publicKey = secretKey; // an "upcast": FHESecKey is a subclass of FHEPubKey //if(0 == d) G = context.alMod.getFactorsOverZZ()[0]; secretKey.GenSecKey(w); // actually generate a secret key with Hamming weight w addSome1DMatrices(secretKey); EncryptedArray ea(context, G); // constuct an Encrypted array object ea that is // associated with the given context and the polynomial G long nslots = ea.size(), field = power(p,r); cout << "nslots: " << nslots << endl ; cout << "Computations will be modulo " << field << endl; cout << "m: " << m << endl; unsigned int sz1, sz2, num; while(true){ cout << "Enter number of rows in the matrix: "; cin >> sz1; if(sz1 > 1 && sz1 <= nslots) break; cout << "Error! the value must be between 1 to " << nslots << "!" << endl; } while(true){ cout << "Enter number of columns in the matrix: "; cin >> sz2; if(sz1 > 2 && sz2 <= nslots) break; cout << "Error! the value must be between 1 to " << nslots << "!" << endl; } cout << "Enter the number you want to multiply the matrix by: "; cin >> num; MatSize sz(sz1, sz2); PTMatrix PTmat(sz,field); //random matrix in size origSize1 while(true){ cout << "Print the matrices?" << endl; cout << "Y for yes, N for no: "; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ toPrint = true; break; } if(tempChar == 'N' || tempChar == 'n'){ toPrint = false; break; } cout << "Error! invalid input!" << endl; } while(true){ cout << "Save the matrices?" << endl; cout << "Y for yes, N for no: "; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ toSave = true; break; } if(tempChar == 'N' || tempChar == 'n'){ toSave = false; break; } cout << "Error! invalid input!" << endl; } if(toPrint) PTmat.print(); if(toSave){ ofstream out_mat("mat.txt"); PTmat.save(out_mat); out_mat.close(); } //encryptions cout << "Encrypting the matrix..." << endl; resetTimers(); EncryptedMatrix encMat = PTmat.encrypt(ea, publicKey); enc = stopTimers("to encrypt the first matrix"); //Multiply by constant cout << "Multiplying the matricex by constant..." << endl; resetTimers(); encMat *= num; encMul = stopTimers("to multiply the matrix"); cout << "Decrypting the result..." << endl; resetTimers(); PTMatrix res = encMat.decrypt(ea, secretKey); dec = stopTimers("to decrypt the result"); if(toPrint) res.print("Solution: "); PTMatrix PTres = PTmat; for(unsigned int i=1; i < num; i++){ PTres += PTmat; } PTres %= field; if(toPrint) PTres.print("pt Solution: "); if(toSave){ ofstream out_res("mat_res.txt"), out_ptRes("mat_pt_res.txt"); res.save(out_res); PTres.save(out_ptRes); out_res.close(); out_ptRes.close(); } //PTres.print("pt result: "); cout << "\n\n----------------------------------------Summary------------------------------ " << endl; cout << "p: " << p << ", r: " << r << ", L: " << L << ", c: " << c << ", w: " << w << ", d: " << d << ", s: " << s << ", security: " << security << endl; cout << "nslots: " << nslots << "\nm: " << m << endl; cout << "It took " << enc << " clock ticks to encrypt the matrix" << endl; cout << "It took " << dec << " clock ticks to decrypt the result" << endl; cout << "It took " << encMul << " clock ticks to add the encrypted matrices" << endl; cout << "is correct? " << (res==PTres) << endl; return 0; }