github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/Permutation.cpp (about) 1 /* 2 * Permutation.cpp 3 * 4 * Created on: 22.10.2010 5 * Author: stephaniebayer 6 */ 7 8 #include "Permutation.h" 9 #include <stdio.h> 10 #include <time.h> 11 #include <vector> 12 #include <fstream> 13 #include "FakeZZ.h" 14 NTL_CLIENT 15 16 17 Permutation::Permutation() { 18 // TODO Auto-generated constructor stub 19 20 } 21 22 Permutation::~Permutation() { 23 // TODO Auto-generated destructor stub 24 } 25 26 //creates permuation of the numbers 1 to N as a vector 27 vector<long>* Permutation::permutation(long N){ 28 29 vector<long>* v = new vector<long >(N); 30 long i, r, temp; 31 //vector containing the values 1 to N ordered 32 for (i=0; i<N; i++){ 33 v->at(i)= i+1; 34 } 35 36 //create N times a random number <N, calculates r = i+r%N and switchs the values v[i] and v[r] 37 for (i=0; i<N; i++){ 38 r = RandomBnd(N); 39 temp = (*v)[i]; 40 r=(i+r)%N; 41 v->at(i)=v->at(r); 42 v->at(r)=temp; 43 } 44 45 /* 46 string name = "permu.txt"; 47 ofstream ost; 48 ost.open(name.c_str()); 49 for(i=0; i<N; i++){ 50 ost<<v->at(i)<<" "; 51 } 52 ost.close(); 53 */ 54 return v; 55 56 } 57 58 /* calculate a mxn matrix containing a permutation from N = n*m values. For each value in the vector it calculates the 59 * associated position in a matrix and saves this. 60 * For example N 0 15, m=3, n = 5, if the first value in the permutation vector is 7, then the element in the matrix, 61 * the position (0,0) would be (1,1)*/ 62 void Permutation::perm_matrix(vector<vector<vector<long>* >* >* pi, long n, long m){ 63 64 vector<long>* v=0; 65 long i,j,k,t_1,t_2; 66 vector<vector<long>* >* r=0; 67 vector<long>* el= 0; 68 //generates random permutation 69 v = permutation(n*m); 70 for (i=0; i< m; i++){ 71 72 r=new vector<vector<long>* >(n); 73 for (j=0; j<n; j++){ 74 75 k= i*n +j; 76 t_1 = v->at(k)/n; 77 78 t_2 = v->at(k)%n; 79 if (t_2 == 0) 80 { 81 t_1 = t_1 -1; 82 t_2 = n-1; 83 } 84 else 85 { 86 t_2 = t_2-1; 87 } 88 el=new vector<long>(2); 89 el->at(0)=t_1; 90 el->at(1)= t_2; 91 r->at(j)=el; 92 } 93 pi->at(i)=r; 94 } 95 96 delete v; 97 } 98 99 void Permutation::perm_matrix(vector<vector<vector<long>* >* >* pi, const vector<long>& v, long n, long m){ 100 long i,j,k,t_1,t_2; 101 vector<vector<long>* >* r=nullptr; 102 vector<long>* el= nullptr; 103 //generates random permutation 104 for (i=0; i< m; i++){ 105 106 r=new vector<vector<long>* >(n); 107 for (j=0; j<n; j++){ 108 109 k= i*n +j; 110 t_1 = v.at(k)/n; 111 112 t_2 = v.at(k)%n; 113 if (t_2 == 0) 114 { 115 t_1 = t_1 -1; 116 t_2 = n-1; 117 } 118 else 119 { 120 t_2 = t_2-1; 121 } 122 el=new vector<long>(2); 123 el->at(0)=t_1; 124 el->at(1)= t_2; 125 r->at(j)=el; 126 } 127 pi->at(i)=r; 128 } 129 }