void main(void) { char a[] = {'1', '2', '3', '4'}; int n = 3; cout << "The permutations of 123 are" << endl; Perm(a, 0, n-1); }
/* * Compute matrix P(m,n) with all possible (m) permutations of * a vector x of size n which can have values x(i) in [0,...,num(i)-1] */ DenseMatrix getPermutationMatrix(std::vector<unsigned int> num) { unsigned int n = num.size(); unsigned int m = 1; for (unsigned int val : num) m *= val; DenseMatrix Perm(m, n); for (unsigned int i = 0; i < n; ++i) { DenseVector vec(1); vec(0) = 1; for (unsigned int j = 0; j < n; ++j) { DenseVector temp = vec; DenseVector vecj = DenseVector::Ones(num.at(j)); if (i == j) { // Values [0,...,num(i)-1] for (unsigned int k = 0; k < vecj.size(); ++k) vecj(k) = k; } vec = Eigen::kroneckerProduct(temp, vecj); } assert(vec.size() == m); for (unsigned int k = 0; k < m; ++k) { Perm(k,i) = vec(k); } } return Perm; }
void Perm(T list[], int k, int m) {// Generate all permutations of list[k:m]. int i; if (k == m) {// list[k:m] has one permutation, output it for (i = 0; i <= m; i++) cout << list[i]; cout << endl; } else // list[k:m] has more than one permutation // generate these recursively for (i = k; i <= m; i++) { Swap(list[k], list[i]); Perm(list, k+1, m); Swap(list[k], list[i]); } }
void Perm(char list[], int k, int m) { //生成list [k:m ]的所有排列方式 int i; if (k == m) {//输出一个排列方式 for (i = 0; i <= m; i++) putchar(list[i]); putchar('\n'); } else // list[k:m ]有多个排列方式 // 递归地产生这些排列方式 for (i=k; i <= m; i++) { Swap (list[k], list[i]); Perm (list, k+1, m); Swap (list [k], list [i]); } }
void Perm(T list[], int k, int m) { int i; if (k == m) { for (i = 0; i <= m; ++i) { std::cout << list[i]; } std::cout << std::endl; } else { for (i = k; i <= m; ++i) { Swap(list[k], list[i]); Perm(list, k + 1, m); Swap(list[k], list[i]); } } }
//--------------------------------------------------------- void CWaterRetentionCapacity::Get_WaterRetention(CSG_Matrix &Data, double fC, CSG_Shape *pPoint) { int i; double fWRC = 0, fPerm = 0, fHe = 0, fK = 0, fCCC = 0, fCIL = 0, fTotalDepth = 0; CSG_Vector CCC (Data.Get_NRows()); CSG_Vector CIL (Data.Get_NRows()); CSG_Vector K (Data.Get_NRows()); CSG_Vector Perm(Data.Get_NRows()); CSG_Vector He (Data.Get_NRows()); CSG_Vector CRA (Data.Get_NRows()); for(i=0; i<Data.Get_NRows(); i++) { CCC [i] = Get_CCC(Data[i]); CIL [i] = Get_CIL(Data[i]); He [i] = Get_He (Data[i]); Perm[i] = Get_Permeability(CCC[i], CIL[i]); if( i > 0 ) { K[i] = Get_K(Perm[i - 1], Perm[i], fC); } CRA[i] = (double)((12.5 * He[i] + 12.5 * (50. - He[i]) * K[i] / 2.) * Data[i][1] / 100.); fTotalDepth += Data[i][0]; } for(i=0; i<Data.Get_NRows(); i++) { fWRC += Data[i][0] / fTotalDepth * CRA [i]; fCCC += Data[i][0] / fTotalDepth * CCC [i]; fCIL += Data[i][0] / fTotalDepth * CIL [i]; fPerm += Data[i][0] / fTotalDepth * Perm[i]; fHe += Data[i][0] / fTotalDepth * He [i]; fK += Data[i][0] / fTotalDepth * K [i]; } pPoint->Set_Value(0, fCCC ); pPoint->Set_Value(1, fCIL ); pPoint->Set_Value(2, fPerm); pPoint->Set_Value(3, fHe ); pPoint->Set_Value(4, fWRC ); }
void Perm(int list[], int i) { int j, temp; if (i == N - 1) { // add code here double re = 0, tt, t1, t2, t0; char aa = 0; for (i = 0; i < N; i ++) { re += dis[list[i]]; if (fabs(angle[list[i]][1]) > eps) t0 = (po[(list[i] + 1) % N][1] - po[list[i]][1]) / angle[list[i]][1]; else t0 = (po[(list[i] + 1) % N][0] - po[list[i]][0]) / angle[list[i]][0]; t1 = 1e100, t2 = -1e100; for (j = aa = 0; j < i; j ++) { if (fabs(tt = angle[list[i]][0] * angle[list[j]][1] - angle[list[j]][0] * angle[list[i]][1]) > eps) { tt = (angle[list[j]][0] * (po[list[i]][1] - po[list[j]][1]) + angle[list[j]][1] * (po[list[j]][0] - po[list[i]][0])) / tt; if (tt > eps) t1 = (tt > t1 ? t1 : tt); else if (tt < -eps) t2 = (tt > t2 ? tt : t2); else aa = 1; } } if (aa) { if (t0 > eps) t2 = 0; else t1 = 0; } if (t0 > eps) tt = min(dis2[list[i]][0], -t2), t0 = min(dis2[list[i]][1], t1 - dis[list[i]]); else tt = min(dis2[list[i]][0], t1), t0 = min(dis2[list[i]][1], -t2 - dis[list[i]]); re += tt + t0; } min = (re < min ? re : min); } else { temp = list[i]; for (j = i; j < N; j ++) { list[i] = list[j]; list[j] = temp; Perm(list, i + 1); list[j] = list[i]; } list[i] = temp; } }
TEST_F(PermutationTest, Inverse) { Vector<quantities::Length, World1> const vector1 = vector_; Vector<quantities::Length, World2> const vector2 = Vector<quantities::Length, World2>( R3(1.0 * Metre, 2.0 * Metre, 3.0 * Metre)); EXPECT_THAT(Perm(Perm::YZX).Inverse()(vector2).coordinates(), Eq<R3>({3.0 * Metre, 1.0 * Metre, 2.0 * Metre})); EXPECT_THAT(Perm(Perm::YXZ).Inverse()(vector2).coordinates(), Eq<R3>({2.0 * Metre, 1.0 * Metre, 3.0 * Metre})); std::vector<Perm> const all = {Perm(Perm::XYZ), Perm(Perm::YZX), Perm(Perm::ZXY), Perm(Perm::XZY), Perm(Perm::ZYX), Perm(Perm::YXZ)}; for (Perm const& p : all) { Permutation<World1, World1> const identity1 = p.Inverse() * p; EXPECT_THAT(identity1(vector1), Eq(vector1)); Permutation<World2, World2> const identity2 = p * p.Inverse(); EXPECT_THAT(identity2(vector2), Eq(vector2)); } }
TEST_F(PermutationTest, Forget) { EXPECT_THAT(Perm(Perm::XYZ).Forget()(vector_).coordinates(), Eq<R3>({1.0 * Metre, 2.0 * Metre, 3.0 * Metre})); EXPECT_THAT(Perm(Perm::YZX).Forget()(vector_).coordinates(), Eq<R3>({2.0 * Metre, 3.0 * Metre, 1.0 * Metre})); EXPECT_THAT(Perm(Perm::ZXY).Forget()(vector_).coordinates(), Eq<R3>({3.0 * Metre, 1.0 * Metre, 2.0 * Metre})); EXPECT_THAT(Perm(Perm::XZY).Forget()(vector_).coordinates(), AlmostEquals<R3>({1.0 * Metre, 3.0 * Metre, 2.0 * Metre}, 2)); EXPECT_THAT(Perm(Perm::ZYX).Forget()(vector_).coordinates(), AlmostEquals<R3>({3.0 * Metre, 2.0 * Metre, 1.0 * Metre}, 4)); EXPECT_THAT(Perm(Perm::YXZ).Forget()(vector_).coordinates(), AlmostEquals<R3>({2.0 * Metre, 1.0 * Metre, 3.0 * Metre}, 2)); }
int main() { int solution = 0; double currentMin = 10000000; const int limit = 10000000; const int primeSize = 10000; std::vector<int> primes; for (int i = 0; i < primeSize; i++) { if (Prime(i)) { primes.push_back(i); } } for (unsigned i = 1; i < primes.size(); i++) { for (int j = i - 1; j - 1 > 0; j--) { int first = primes[i]; int second = primes[j]; //Check if n passes max const int n = first * second; if (n > limit) { continue; } const double nd = static_cast<double>(n); int phi = n - first - second + 1; if (nd / phi < currentMin) { if (!Perm(std::to_string(n), std::to_string(phi))) { continue; } currentMin = nd / phi; solution = n; } } } std::cout << "Solution: " << solution << std::endl; }
void Run () { int list[10]; for (int i = 0; i < N; i ++) { list[i] = i; dis[i] = sqrt((po[i][0] - po[(i + 1) % N][0]) * (po[i][0] - po[(i + 1) % N][0]) + (po[i][1] - po[(i + 1) % N][1]) * (po[i][1] - po[(i + 1) % N][1])); angle[i][0] = (po[i][0] - po[(i + 1) % N][0]) / dis[i]; angle[i][1] = (po[i][1] - po[(i + 1) % N][1]) / dis[i]; double t0; if (fabs(angle[i][1]) > eps) t0 = (po[(i + 1) % N][1] - po[i][1]) / angle[i][1]; else t0 = (po[(i + 1) % N][0] - po[i][0]) / angle[i][0]; double t1 = 1e100, t2 = -1e100, tt; if (fabs(angle[i][0]) > eps) { if ((tt = -po[i][0] / angle[i][0]) > eps) t1 = (tt > t1 ? t1 : tt); else t2 = (tt > t2 ? tt : t2); if ((tt = (w - po[i][0]) / angle[i][0]) > eps) t1 = (tt > t1 ? t1 : tt); else t2 = (tt > t2 ? tt : t2); } if (fabs(angle[i][1]) > eps) { if ((tt = -po[i][1] / angle[i][1]) > eps) t1 = (tt > t1 ? t1 : tt); else t2 = (tt > t2 ? tt : t2); if ((tt = (h - po[i][1]) / angle[i][1]) > eps) t1 = (tt > t1 ? t1 : tt); else t2 = (tt > t2 ? tt : t2); } if (t0 > eps) dis2[i][0] = -t2, dis2[i][1] = t1 - dis[i]; else dis2[i][0] = t1, dis2[i][1] = -t2 - dis[i]; } min = 1e100; Perm(list, 0); cout.precision(3); cout.setf(ios::fixed); cout << min << endl; }
TEST_F(PermutationTest, XZY) { EXPECT_THAT(Perm(Perm::XZY)(vector_).coordinates(), Eq<R3>({1.0 * Metre, 3.0 * Metre, 2.0 * Metre})); }
TEST_F(PermutationTest, AppliedToBivector) { EXPECT_THAT(Perm(Perm::ZXY)(bivector_).coordinates(), Eq<R3>({3.0 * Metre, 1.0 * Metre, 2.0 * Metre})); EXPECT_THAT(Perm(Perm::ZYX)(bivector_).coordinates(), Eq<R3>({-3.0 * Metre, -2.0 * Metre, -1.0 * Metre})); }
TEST_F(PermutationTest, ZYX) { EXPECT_THAT(Perm(Perm::ZYX)(vector_).coordinates(), Eq<R3>({3.0 * Metre, 2.0 * Metre, 1.0 * Metre})); }
TEST_F(PermutationTest, AppliedToVector) { EXPECT_THAT(Perm(Perm::YZX)(vector_).coordinates(), Eq<R3>({2.0 * Metre, 3.0 * Metre, 1.0 * Metre})); EXPECT_THAT(Perm(Perm::YXZ)(vector_).coordinates(), Eq<R3>({2.0 * Metre, 1.0 * Metre, 3.0 * Metre})); }
int main() { char s[]="123"; Perm(s, 0, 2); return 0; }
double mapping(TString SimData, TString ExpData, TString background = "nobackground"){ /////////////////////// OPEN INPUT AND OUTPUT FILES /////////////////////// // open the data file ifstream input_exp; TString fullpathtofile = Form("%s",ExpData.Data() ) ; if(gVerbose>0) cout<<"opening file : |"<<ExpData<< "| ... "; input_exp.open( ExpData.Data() ); if (!input_exp) { cout<<"problem opening experimental data file " << fullpathtofile << endl; exit(-1);} else if(gVerbose>0) cout<<"Experimental file is opened "<<endl; //open the field files from comsol ifstream input_sim; fullpathtofile = Form("%s",SimData.Data() ) ; if(gVerbose>0) cout<<"opening file : |"<<SimData<< "| ... "; input_sim.open(SimData.Data() ) ; if(!input_sim.is_open()) { if(gVerbose>0) cout<<"problem opening simulation data file |"<< fullpathtofile <<"|"<< endl; SimData.ReplaceAll("./input/",""); fullpathtofile = Form("/data1/moukaddam/MagnetSimulation/%s",SimData.Data()) ; if(gVerbose>0) cout<<"trying main directory, opening file : |"<<fullpathtofile<< "| ... "; input_sim.open(fullpathtofile.Data() ) ; // from local input if not go seek in data 1 if (!input_sim.is_open()) { cout<<"problem opening simulation data file |"<< fullpathtofile <<"|"<< endl<<endl; exit(-1);} } else if(gVerbose>0) cout<<" Simulation file " << fullpathtofile << " is opened " <<endl; // create root output file ExpData.ReplaceAll("./input/",""); ExpData.ReplaceAll(".dat",""); ExpData.ReplaceAll(".txt",""); SimData.ReplaceAll("./input/",""); SimData.ReplaceAll(".dat",""); SimData.ReplaceAll(".txt",""); TString filename = "./output/compare_" + ExpData + "_" + SimData + ".root"; TFile outputFile(filename,"RECREATE"); TDirectory *dirquad[4] ; dirquad[0] = outputFile.mkdir("Quad_1"); dirquad[1] = outputFile.mkdir("Quad_2"); dirquad[2] = outputFile.mkdir("Quad_3"); dirquad[3] = outputFile.mkdir("Quad_4"); //Read the simulation file from comsol // dump the first lines from the top string s_buffer="buffer"; Int_t d_buffer=-1; Int_t counter1=0; // counter on the first lines Int_t counter2=0; // counter on the first lines // read the first lines Int_t dimX, dimY, dimZ; input_sim>>dimX>>dimY>>dimZ; if(gVerbose>0) cout<<"\nSimulated Field Table dimensions X : "<<dimX<<" Y : "<<dimY<<" Z : "<<dimZ<<endl; dimX=dimX+10; dimY=dimY+10; dimZ=dimZ+10; TH3D *f3DHistBx = new TH3D("f3DHistBX", "Bx" , dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); TH3D *f3DHistBy = new TH3D("f3DHistBY", "By" , dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); TH3D *f3DHistBz = new TH3D("f3DHistBZ", "Bz" , dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); TH3D *f3DHistBmag = new TH3D("f3DHistBMAG", "Bmag" , dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); TH3D *f3DHistBtan = new TH3D("f3DHistBTAN", "Btan" , dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); TH3D *f3DHistBdiff = new TH3D("f3DHistBDIFF", "Bdiff", dimX,-dimX, dimX, dimY,-dimY, dimY, dimZ, -dimZ, dimZ); d_buffer=-1; while (d_buffer != 0 && counter1< 15){ // find a solution for this with do while input_sim>>d_buffer>>s_buffer; counter2++; } getline(input_sim,s_buffer); if(gVerbose>0) cout<< " Number of skipped lines in comsol file : " <<counter2 << " last line content : "<<s_buffer<<endl; // read and fill SimulationPoint* SimPoint = new SimulationPoint(); Double_t X(0), Y(0), Z(0), EX(-100), EY(-100), EZ(-100), Perm(0); Double_t BX(0), BY(0), BZ(0); Int_t line=0; while ( !input_sim.eof() ) { //Clear parameters SimPoint->ClearParameters(); // Choose format if(counter2<9) input_sim >> X >> Y >> Z >> BX >> BY >> BZ >> Perm ; else input_sim >> X >> Y >> Z >> BX >> BY >> BZ >> EX >> EY >> EZ >> Perm ; SimPoint->ReadLineAndTreat(X,Y,Z,BX,BY,BZ,EX,EY,EZ,Perm ); //SimPoint->Show(); // fill in TH3D all the simulation data Int_t binNumber = f3DHistBx->FindBin(X,Y,Z); f3DHistBx->SetBinContent(binNumber,BX); f3DHistBy->SetBinContent(binNumber,BY); f3DHistBz->SetBinContent(binNumber,BZ); f3DHistBmag->SetBinContent(binNumber,SimPoint->fBFieldMag); f3DHistBtan->SetBinContent(binNumber,SimPoint->fBFieldTan); f3DHistBdiff->SetBinContent(binNumber,SimPoint->fBFieldDiff); //fBFieldMag-fBFieldTan //count the lines for inspection line++; if (line%5000 == 0) { if(gVerbose>0) printf("\r @line : %d ... Still reading ...",line); if(gVerbose>1) cout<< X<<" "<<Y <<" "<<Z <<" "<<BX <<" "<<BY<<" "<<BZ <<" "<< EX<<" "<< EY<<" "<< EZ<<" "<< Perm<<endl ; } }
TEST_F(PermutationTest, AppliedToTrivector) { EXPECT_THAT(Perm(Perm::XYZ)(trivector_).coordinates(), Eq(4.0 * Metre)); EXPECT_THAT(Perm(Perm::XZY)(trivector_).coordinates(), Eq(-4.0 * Metre)); }