int main() { int n, i, v; char verb[4], prep[4]; block *blocks[MAX]; block *a, *b; for(i = 0; i < MAX; i++) table[i] = blocks[i] = NULL; /* Read number of blocks `n`. */ Si(n); /* Initialize world with `n` blocks. */ for(i = 0; i < n; i++) { table[i] = blocks[i] = (block *) malloc(sizeof(block)); blocks[i]->above = NULL; blocks[i]->below = NULL; blocks[i]->table_loc = i; blocks[i]->value = i; } while(1) { /* Read the verb. */ Ss(verb); /* Exit when verb is quit. */ if(verb[0] == 'q') break; /* Read the object. */ Si(v); a = blocks[v]; /* Read the preposition. */ Ss(prep); /* Read the complement. */ Si(v); b = blocks[v]; /* Ignore action if it is illegal. */ if(a->table_loc == b->table_loc) continue; /* When verb is `move`, put back that are above the object `a`. */ if(verb[0] == 'm') put_back(a); /* When preposition is `onto`, put back the blocks that are above the complement `b`. */ if(prep[1] == 'n') put_back(b); /* Pile object `a` over complement `b`. */ stack(a, b); } print_table(n); for(i = 0; i < n; i++) free(blocks[i]); return 0; }
//============================================================================= Mat patch_models:: inv_simil(const Mat &S) { Mat Si(2,3,CV_32F); float d = S.fl(0,0)*S.fl(1,1) - S.fl(1,0)*S.fl(0,1); Si.fl(0,0) = S.fl(1,1)/d; Si.fl(0,1) = -S.fl(0,1)/d; Si.fl(1,1) = S.fl(0,0)/d; Si.fl(1,0) = -S.fl(1,0)/d; Mat Ri = Si(Rect(0,0,2,2)); Mat t = -Ri*S.col(2),St = Si.col(2); t.copyTo(St); return Si; }
void PFilter::predict_observation(Correlated_additive_observe_model& observe_model, FM::Vec& z_pred, FM::SymMatrix& R_pred) { z_pred.clear(); // mean const std::size_t nSamples = S.size2(); for (std::size_t i = 0; i != nSamples; ++i) { FM::ColMatrix::Column Si(S,i); z_pred.plus_assign (observe_model.h(Si)); } z_pred /= Float(S.size2()); R_pred.clear(); // Covariance for (std::size_t i = 0; i != nSamples; ++i) { FM::ColMatrix::Column Si(S,i); R_pred.plus_assign (FM::outer_prod(observe_model.h(Si)-z_pred, observe_model.h(Si)-z_pred)); } R_pred /= Float(nSamples); }
/* * find the location of queen q */ void solve_queen_at(int q, int* i, int* j) { if(q < 0 || q >= SOLVE_NQUEENS || !queen_on(q)) *i = *j = -1; else { *i = Si(q); *j = Sj(q); } }
/** Execute the algorithm. */ void CalculateUMatrix::exec() { double a=this->getProperty("a"); double b=this->getProperty("b"); double c=this->getProperty("c"); double alpha=this->getProperty("alpha"); double beta=this->getProperty("beta"); double gamma=this->getProperty("gamma"); OrientedLattice o(a,b,c,alpha,beta,gamma); Matrix<double> B=o.getB(); double H,K,L; PeaksWorkspace_sptr ws; ws = boost::dynamic_pointer_cast<PeaksWorkspace>(AnalysisDataService::Instance().retrieve(this->getProperty("PeaksWorkspace")) ); if (!ws) throw std::runtime_error("Problems reading the peaks workspace"); Matrix<double> Hi(4,4),Si(4,4),HS(4,4),zero(4,4); for (int i=0;i<ws->getNumberPeaks();i++) { Peak p=ws->getPeaks()[i]; H=p.getH(); K=p.getK(); L=p.getL(); if(H*H+K*K+L*L>0) { V3D Qhkl=B*V3D(H,K,L); Hi[0][0]=0.; Hi[0][1]=-Qhkl.X(); Hi[0][2]=-Qhkl.Y(); Hi[0][3]=-Qhkl.Z(); Hi[1][0]=Qhkl.X(); Hi[1][1]=0.; Hi[1][2]=Qhkl.Z(); Hi[1][3]=-Qhkl.Y(); Hi[2][0]=Qhkl.Y(); Hi[2][1]=-Qhkl.Z(); Hi[2][2]=0.; Hi[2][3]=Qhkl.X(); Hi[3][0]=Qhkl.Z(); Hi[3][1]=Qhkl.Y(); Hi[3][2]=-Qhkl.X(); Hi[3][3]=0.; V3D Qgon=p.getQSampleFrame(); Si[0][0]=0.; Si[0][1]=-Qgon.X(); Si[0][2]=-Qgon.Y(); Si[0][3]=-Qgon.Z(); Si[1][0]=Qgon.X(); Si[1][1]=0.; Si[1][2]=-Qgon.Z(); Si[1][3]=Qgon.Y(); Si[2][0]=Qgon.Y(); Si[2][1]=Qgon.Z(); Si[2][2]=0.; Si[2][3]=-Qgon.X(); Si[3][0]=Qgon.Z(); Si[3][1]=-Qgon.Y(); Si[3][2]=Qgon.X(); Si[3][3]=0.; HS+=(Hi*Si); } } //check if HS is 0 if (HS==zero) throw std::invalid_argument("The peaks workspace is not indexed or something really bad happened"); Matrix<double> Eval; Matrix<double> Diag; HS.Diagonalise(Eval,Diag); Eval.sortEigen(Diag); Mantid::Kernel::Quat qR(Eval[0][0],Eval[1][0],Eval[2][0],Eval[3][0]);//the first column corresponds to the highest eigenvalue DblMatrix U(qR.getRotation()); o.setU(U); ws->mutableSample().setOrientedLattice(new OrientedLattice(o)); }
int main() { int n; int i, j; int s; Si(n); s = 0; for(i = 0; i < n; i++) for(j = 0; j < n; j++) s += (n - i) * (n - j); printf("Number of sub-rectangles for square matrix %dx%d is %d\n", n, n, s); return 0; }
void Sample_filter::predict (Functional_predict_model& f) /* Predict samples forward * Pre : S represent the prior distribution * Post: S represent the predicted distribution */ { // Predict particles S using supplied predict model const std::size_t nSamples = S.size2(); for (std::size_t i = 0; i != nSamples; ++i) { FM::ColMatrix::Column Si(S,i); FM::noalias(Si) = f.fx(Si); } }
/* * set the board to either add or remove queen q */ static int sr_queen(int q, int inc) { int i, j; int qi = Si(q); int qj = Sj(q); if(!queen_on(q)) return -1; // spot board[qi][qj] += inc; // row for(j = 0; j < SOLVE_SIZE; ++j) if(j != qj) board[qi][j] += inc; // col for(i = 0; i < SOLVE_SIZE; ++i) if(i != qi) board[i][qj] += inc; // diag - right & down for(i = qi + 1, j = qj + 1; i < SOLVE_SIZE && j < SOLVE_SIZE; ++i, ++j) board[i][j] += inc; // diag - right & up for(i = qi - 1, j = qj + 1; i >= 0 && j < SOLVE_SIZE; --i, ++j) board[i][j] += inc; // diag - left & down for(i = qi + 1, j = qj - 1; i < SOLVE_SIZE && j >= 0; ++i, --j) board[i][j] += inc; // diag - left & up for(i = qi - 1, j = qj - 1; i >= 0 && j >= 0; --i, --j) board[i][j] += inc; return 0; }
/** Execute the algorithm. */ void CalculateUMatrix::exec() { double a=this->getProperty("a"); double b=this->getProperty("b"); double c=this->getProperty("c"); double alpha=this->getProperty("alpha"); double beta=this->getProperty("beta"); double gamma=this->getProperty("gamma"); OrientedLattice o(a,b,c,alpha,beta,gamma); Matrix<double> B=o.getB(); double H,K,L; PeaksWorkspace_sptr ws; ws = AnalysisDataService::Instance().retrieveWS<PeaksWorkspace>(this->getProperty("PeaksWorkspace") ); if (!ws) throw std::runtime_error("Problems reading the peaks workspace"); size_t nIndexedpeaks=0; bool found2nc=false; V3D old(0,0,0); Matrix<double> Hi(4,4),Si(4,4),HS(4,4),zero(4,4); for (int i=0;i<ws->getNumberPeaks();i++) { Peak p=ws->getPeaks()[i]; H=p.getH(); K=p.getK(); L=p.getL(); if(H*H+K*K+L*L>0) { nIndexedpeaks++; if (!found2nc) { if (nIndexedpeaks==1) { old=V3D(H,K,L); } else { if (!old.coLinear(V3D(0,0,0),V3D(H,K,L))) found2nc=true; } } V3D Qhkl=B*V3D(H,K,L); Hi[0][0]=0.; Hi[0][1]=-Qhkl.X(); Hi[0][2]=-Qhkl.Y(); Hi[0][3]=-Qhkl.Z(); Hi[1][0]=Qhkl.X(); Hi[1][1]=0.; Hi[1][2]=Qhkl.Z(); Hi[1][3]=-Qhkl.Y(); Hi[2][0]=Qhkl.Y(); Hi[2][1]=-Qhkl.Z(); Hi[2][2]=0.; Hi[2][3]=Qhkl.X(); Hi[3][0]=Qhkl.Z(); Hi[3][1]=Qhkl.Y(); Hi[3][2]=-Qhkl.X(); Hi[3][3]=0.; V3D Qgon=p.getQSampleFrame(); Si[0][0]=0.; Si[0][1]=-Qgon.X(); Si[0][2]=-Qgon.Y(); Si[0][3]=-Qgon.Z(); Si[1][0]=Qgon.X(); Si[1][1]=0.; Si[1][2]=-Qgon.Z(); Si[1][3]=Qgon.Y(); Si[2][0]=Qgon.Y(); Si[2][1]=Qgon.Z(); Si[2][2]=0.; Si[2][3]=-Qgon.X(); Si[3][0]=Qgon.Z(); Si[3][1]=-Qgon.Y(); Si[3][2]=Qgon.X(); Si[3][3]=0.; HS+=(Hi*Si); } } //check if enough peaks are indexed or if HS is 0 if ((nIndexedpeaks<2) || (found2nc==false)) throw std::invalid_argument("Less then two non-colinear peaks indexed"); if (HS==zero) throw std::invalid_argument("Something really bad happened"); Matrix<double> Eval; Matrix<double> Diag; HS.Diagonalise(Eval,Diag); Eval.sortEigen(Diag); Mantid::Kernel::Quat qR(Eval[0][0],Eval[1][0],Eval[2][0],Eval[3][0]);//the first column corresponds to the highest eigenvalue DblMatrix U(qR.getRotation()); o.setU(U); ws->mutableSample().setOrientedLattice(new OrientedLattice(o)); }
int main() { //INITIALIZE INPUTS time_t seconds; seconds = time(NULL); long seed = (long)seconds; RandomClass r; r.initialize(seed); string SIMU_TYPE; int NPARTICLES, MAXIMUM_COLL; double TEQ; ifstream file; file.open("input.txt"); if (file.fail()){ cout << "NO INPUT FILE" << endl; } else { file >> SIMU_TYPE; //case sensitive file >> NPARTICLES; file >> MAXIMUM_COLL; file >> TEQ; } // READ GEOMETRY FILES reflective_bdrs ref("reflective.txt"); ref.show(); prescribed_bdrs presc("prescribed.txt"); presc.show(); periodic_bdrs per("periodic.txt"); per.show(); // READ MATERIAL PROPERTIES materials Si("dataSi.txt", TEQ); Si.show_all(); // DECLARE AND READ THE HEAT FLUX AND TEMPERATURE DETECTORS detector_array_H H_detect("H_detectors.txt", "times.txt"); detector_array_T T_detect("T_detectors.txt", "times.txt"); // PRINT GEOMETRY IN FILE particle part(MAXIMUM_COLL); // READ SOURCE FILES sources src(&Si, &presc, "volumetric.txt", "body_force.txt", "initial.txt", NPARTICLES); src.display_type(); cout << src.Np << endl; cout << src.Nv << endl; cout << src.Nb << endl; cout << src.Ni << endl; src.display_vol(); src.display_bod(); src.display_init(); matlab_write_geometry(&ref, &presc, &per, &src, &T_detect, &H_detect, "geometry.m"); if (strcmp(SIMU_TYPE.c_str(),"BOTH")==0 || strcmp(SIMU_TYPE.c_str(),"BACKWARD")==0 || strcmp(SIMU_TYPE.c_str(),"ADJOINT")==0) { // READ SOURCE FILES sources src_adj(&Si, "T_detectors.txt", "H_detectors.txt", NPARTICLES); adjoint_detectors adj_det(&presc, "volumetric.txt", "body_force.txt", "initial.txt", "times.txt", &src_adj); src.display_type(); H_detect.show(); cout << endl; T_detect.show(); cout << endl << "STARTING ADJOINT SIMULATION" << endl; // LOOP OVER PARTICLES cout << src_adj.Nv << endl; while (src_adj.not_empty) { if (src_adj.remaining_particles[src_adj.src_parser]==1){ cout << src_adj.src_parser + 1 << " out of " << src_adj.Ntot << " adjoint sources " << endl; } src_adj.emit_adjoint(&part,&r); while (part.alive){ part.initiate_move(&r); part.move(&ref, &presc, &per); //H_detect.measure(&part); //T_detect.measure(&part, &Si); adj_det.measure(&part, &src_adj); part.finish_move(&Si,&r, &ref, &presc, &per); } } // DISPLAY RESULTS IN TERMINAL AND RECORD THEM adj_det.show_results(); adj_det.write("adj_T_results.txt","adj_H_results.txt"); //cout << adj_det.Nt <<endl; //cout << "displaying pointers" << endl; //cout << adj_det.steady_H << " " << adj_det.steady_T+11 << " " << src_adj.cumul_energies+100 <<endl; } double TEST = 0; if (strcmp(SIMU_TYPE.c_str(),"BOTH")==0 || strcmp(SIMU_TYPE.c_str(),"FORWARD")==0 || strcmp(SIMU_TYPE.c_str(),"REGULAR")==0 || strcmp(SIMU_TYPE.c_str(),"DIRECT")==0) { H_detect.show(); cout << endl; T_detect.show(); cout << endl << "STARTING FORWARD SIMULATION" << endl; // LOOP OVER PARTICLES for (int i=0; i<src.Npart; i++){ print_percent(i, src.Npart); src.emit(&part,&r); if (0<part.pt0.x && part.pt0.x<1e-7 && 0<part.pt0.y && part.pt0.y<1e-7) TEST = TEST + part.weight/Si.C/1e-14; // cout << part.t << " " << src.t_max << endl; while (part.alive){ part.initiate_move(&r); part.move(&ref, &presc, &per); H_detect.measure(&part); T_detect.measure(&part, &Si); part.finish_move(&Si,&r, &ref, &presc, &per); } } // RECORD RESULTS H_detect.write("results_H.txt"); T_detect.write("results_T.txt"); // DISPLAYING RESULTS H_detect.show_results(); T_detect.show_results(); } return 0; }
int main() { int n; Si(n); label(n); return 0; }
/* * make the next solve move */ int solve_next_move() { int moved = 0; int on; // done - found solution if(queen == SOLVE_NQUEENS) { sprintf(move_buff[(move_count++)%MOVE_WRAP], "Complete!!!"); return 1; } // done - did not find solution if(queen < 0) { sprintf(move_buff[(move_count++)%MOVE_WRAP], "Failed."); return -1; } // should only run once or twice while(!moved) { on = queen_on(queen); moved = 1; // from last time if(on) { remove_queen(queen); } // add to board else { stack[queen].di = rand()%SOLVE_SIZE; stack[queen].dj = rand()%SOLVE_SIZE; stack[queen].i = 0; } // find open spot do { // increment position ++stack[queen].j; if(stack[queen].j == SOLVE_SIZE){ stack[queen].j = 0; ++stack[queen].i; // exhausted - backtrack if(stack[queen].i == SOLVE_SIZE) { stack[queen].i = stack[queen].j = -1; --queen; if(on){ mn = 0; sprintf(move_buff[(move_count++)%MOVE_WRAP], "...Exhausted spaces for Queen %d.", queen+2); return 0; } mn = 1; sprintf(move_buff[(move_count++)%MOVE_WRAP], "...No place for Queen %d.", queen+2); moved = 0; break; } } } while(!solve_spot_open(Si(queen), Sj(queen))); } mn = 0; sprintf(move_buff[(move_count++)%MOVE_WRAP], "Move Queen %d to %c%d.", queen+1, Si(queen)+'A', Sj(queen)+1); // place queen set_queen(queen); // get next queen ++queen; return 0; }