int kk(int m,int n) { if(m==0) return n+1; if(n==0) return kk(m-1,1); else return kk(m-1,kk(m,n-1)); }
void Ardb::AddBlockKey(Context& ctx, const std::string& key) { DBItemKey kk(ctx.currentDB, key); ctx.GetBlockContext().keys.insert(kk); WriteLockGuard<SpinRWLock> guard(m_block_ctx_lock); m_block_context_table[kk].push_back(&ctx); }
InfoHandler::InfoHandler(QWidget *parent) : QObject(parent) { m_pTimer = new QTimer(this); connect(m_pTimer, SIGNAL(timeout()), this, SLOT(Ontimeout())); QString exeFileName = QApplication::applicationFilePath(); QFileInfo kk(exeFileName); QString apppath = kk.canonicalPath(); QString ss = QDir::currentPath(); QDir::setCurrent(apppath); list = XmlUtils::ReadXml("vv.xml"); index = 0; QList<QHash<QString,QString>> vd = XmlUtils::readXml("vv.xml"); // // foreach (QVariant v, list) // { // // QString ss = v.toMap().value("info").toString(); // //qDebug()<<ss; // } QDir::setCurrent(ss); run(); }
LOCAL_C void test3(CArrayFix<TInt>& aFix) { test.Next(_L("InsertIsqL")); TKeyArrayFix kk(0,ECmpTInt); TInt pos=0; TInt mod=47; TInt inc=23; TInt i=0; FOREVER { TInt ret; if (i&1) TRAP(ret,aFix.InsertIsqL(i,kk)) else { TRAP(ret,pos=aFix.InsertIsqL(i,kk)) if (ret==KErrNone) test(aFix[pos]==i); } if (ret==KErrAlreadyExists) break; i=(i+inc)%mod; } for(i=0;i<mod;i++) { test(aFix.FindIsq(i,kk,pos)==0); test(pos==i); TRAPD(r,aFix.InsertIsqL(i,kk)) test(r==KErrAlreadyExists); } }
int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: ./kk inputfile\n"); return 1; } char* file = argv[1]; int n = 100; // Read numbers from file FILE* f = fopen(file, "r"); if (f == NULL) { printf("Could not open file\n"); return 1; } // Input numbers into array nums uint64_t* nums = malloc(sizeof(uint64_t) * n); char* line = NULL; size_t read, len, i = 0; while ((read = getline(&line, &len, f)) != -1) { nums[i] = strtoull(line, NULL, 10); i++; } fclose(f); printf("%llu\n", kk(nums, n)); free(nums); // part2(); }
void IMG_CreateComplexTexture(unsigned char *texture_img, int width, int height) { nmppsSet_8u(texture_img, 0, width*height); // snmppcRand((unsigned)time( NULL ) ); int side=width>>2; int side2=side>>1; int side4=side>>2; RPoint border[4]; int y[4]; for(int j=0; j<height; j+=side) for(int i=0; i<width; i+=side) { for(int n=0; n<4; n++) y[n]=nmppcRand(); border[0]=RPoint(nmppcRandMinMax(0,side4-1)+i, y[0]%side4+j); border[1]=RPoint(nmppcRandMinMax(0,side4-1)+i, y[1]%side4+j+side2+side4); border[2]=RPoint(nmppcRandMinMax(0,side4-1)+i+side2+side4, y[1]%side4+j+side2+side4); border[3]=RPoint(nmppcRandMinMax(0,side4-1)+i+side2+side4, y[0]%side4+j); int color=100+nmppcRand()%156; for(int l=0; l<side; l++) for(int k=0; k<side; k++) { RPoint kk(i+k, j+l); if(PointInRectangle(kk, border)) { int m1 = (j+l < height-1)? j+l : height-1; int m2 = (i+k < width-1)? i+k : width-1; nmppsPut_8u(texture_img,width*m1+m2,color); } } } }
ResPolyRing* readRing(std::istream& infile) { // Hack job. // Input format: // <characteristic> // <n:#variables> // <weight_0> <weight_1> ... <weight_(n-1)> // grevlex | lex | ... int charac; int nvars; infile >> charac; infile >> nvars; std::vector<int> wts(nvars); for (int i=0; i<nvars; i++) { int a; infile >> a; wts.push_back(a); } MonomialOrdering mo(nvars, wts); MonomialInfo M(mo); M2::ARingZZp kk(101); ResGausser G(kk); ResPolyRing* R = new ResPolyRing(G,M); return R; }
bool Kyokumen::IsLegalMove(Teban teban, Te &te) const { if ((te.GetKoma() & teban) == 0) { return false; } if (IS_HANDPOS(te.GetFrom())) { if (GetHandNum(te.GetKoma()) == 0) { return false; } } else { if (GetBanInf(te.GetFrom()) != te.GetKoma()) { return false; } } if ((GetBanInf(te.GetTo()) & teban) != 0) { return false; } if (IsCorrectMove(te)) { Kyokumen kk(*this); kk.Move(teban, te, true); if (teban == SELF && kk.GetControlE(kk.GetKingS()) != 0) { return false; } if (teban == ENEMY && kk.GetControlS(kk.GetKingE()) != 0) { return false; } return true; } return false; }
int Kyokumen::MakeChecks(Teban teban, Te *teBuf, int *pin) { Kyokumen kk(*this); // 実際に動かしてみる局面. const Kiki *selfControl; // 手番側から見て、自分の利き. KomaPos enemyKing; // 手番側から見て、相手の玉の位置. if (teban == SELF) { selfControl = kk.GetControlSPtr(); enemyKing = GetKingE(); } else { selfControl = kk.GetControlEPtr(); enemyKing = GetKingS(); } int teNum = MakeLegalMoves(teban, teBuf, pin); // 実際に動かしてみて、王手だけを残す。 int outeNum = 0; for (int i=0; i < teNum; i++) { kk = *this; kk.Move(teban, teBuf[i], true); // 相手の玉に利きが付いていれば王手。 if (selfControl[enemyKing]) { teBuf[outeNum++] = teBuf[i]; // printf("MakeChecks "); // teBuf[i].Print(); // printf("\n"); } } //#if defined(DEBUG_PRINT) // printf("MakeChecks(0x%02x) return [%d]\n", teban, outeNum); //#endif // defined(DEBUG_PRINT). return outeNum; }
int main(){ int a; a=kk(2,1); printf("a=%d",a); return 0; }
void initStyle() // 加载qss { QString exeFileName = QApplication::applicationFilePath(); QFileInfo kk(exeFileName); QString apppath = kk.canonicalPath(); QDir::setCurrent(apppath/*QDir::currentPath()*/);//QApplication::applicationDirPath() QFile file(":/skin.qss"); file.open(QFile::ReadOnly); QString style = QString(file.readAll()); qApp->setStyleSheet(style); file.close(); }
int Kyokumen::CheckMate(Teban teban, int depth, int depthMax, Te *checks, Te &te) { int teNum = MakeChecks(teban, checks); if (teNum == 0) { TsumeHash::Add(m_KyokumenHashVal, m_HandHashVal, m_Hand+teban, -1, 0); //#if defined(DEBUG_PRINT) // printf("CheckMate(0x%02x, %d, %d, 0x%02x) return[1] [%d]\n", teban, depth, depthMax, te.GetKoma(), -1); //#endif // defined(DEBUG_PRINT). return -1; //詰まない. } TsumeVal *p = TsumeHash::DomSearchCheckMate(m_KyokumenHashVal, m_Hand+teban); if (p != nullptr) { te = p->te; //#if defined(DEBUG_PRINT) // printf("CheckMate(0x%02x, %d, %d, 0x%02x) return[2] [%d]\n", teban, depth, depthMax, te.GetKoma(), 1); //#endif // defined(DEBUG_PRINT). return 1; // 詰んだ. } int valmax = -1; for (int i=0; i < teNum; i++) { Kyokumen kk(*this); kk.Move(teban, checks[i], true); #if defined(DEBUG_PRINT_TSUMI) if (g_bDebugPrint) { printf("CheckMate depth[%d]", depth); checks[i].Print(); printf("\n"); } #endif // defined(DEBUG_PRINT_TSUMI). int val = kk.AntiCheckMate(teban^(SELF|ENEMY), depth+1, depthMax, checks+teNum); if (val > valmax) { valmax = val; } if (valmax == 1) { te = checks[i]; break; //詰んだ. } } if (valmax == 1) { //詰んだ. // ASSERT(IS_SELFKOMA(te.GetKoma())); TsumeHash::Add(m_KyokumenHashVal, m_HandHashVal, m_Hand+teban, 1, te); } else if (valmax == -1) { //本当に詰まなかった. TsumeHash::Add(m_KyokumenHashVal, m_HandHashVal, m_Hand+teban, -1, 0); } //#if defined(DEBUG_PRINT) // printf("CheckMate(0x%02x, %d, %d, 0x%02x) return[3] [%d]\n", teban, depth, depthMax, te.GetKoma(), valmax); //#endif // defined(DEBUG_PRINT). return valmax; }
result_type operator()(A0& out, const A1& in) const { size_t n = boost::proto::child_c<0>(in); value_type fn = value_type(n); table<value_type> ns = nt2::cons(of_size(1, 3), fn, fn/12, fn/20); table<i_type> ee(nt2::of_size(1, 3)); table<value_type> ff(nt2::of_size(1, 3)); // nt2::frexp(ns, ff, ee); nt2::frexp(ns(1), ff(1), ee(1)); nt2::frexp(ns(2), ff(2), ee(2)); nt2::frexp(ns(3), ff(3), ee(3)); BOOST_AUTO_TPL(kk, nt2::find(nt2::logical_and(eq(ff, nt2::Half<value_type>()), is_gtz(ee)))); BOOST_ASSERT_MSG(!nt2::isempty(kk), "n must be of form 2^m or 12*2^m or 20*2^m"); size_t k = kk(1); i_type e = nt2::minusone(ee(k)); out.resize(nt2::of_size(n, n)); table<value_type> h; if (k == 1) // n = 1 * 2^e; { h = nt2::One<value_type>(); } else if ( k == 2) // N = 12 * 2^e; { h = nt2::vertcat(nt2::ones(1,12,meta::as_<value_type>()), nt2::horzcat(nt2::ones(11,1,meta::as_<value_type>()), nt2::toeplitz(cons<value_type>(of_size(1, 11), -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1), cons<value_type>(of_size(1, 11), -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1) ) ) ); } else if ( k == 3) // N = 20 * 2^e; { h = vertcat(nt2::ones(1,20,meta::as_<value_type>()), nt2::horzcat(nt2::ones(19,1,meta::as_<value_type>()), nt2::hankel(cons<value_type>(of_size(1, 19),-1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1), cons<value_type>(of_size(1, 19),1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1) ) ) ); } // Kronecker product construction. for (i_type i = 1; i <= e; ++i) { table<value_type> h1 = nt2::vertcat(nt2::horzcat(h, h), nt2::horzcat(h, -h)); h = h1; //ALIASING } out = h; return out; }
int main(int argc, char* argv[]) { char* inputfile = argv[1]; FILE* fp; fp = fopen(inputfile, "r"); int* nums = construct_array(fp); int res = kk(nums); printf("%d\n", res); return 0; }
// Calculates residue for a prepartition solution uint64_t partitionResidue(uint64_t* nums, int* s, int n) { // Calculate new numbers based on prepartition uint64_t* newNums = calloc(n, sizeof(uint64_t)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[j] == i) { newNums[i] += nums[j]; } } } // Get residue uint64_t residue = kk(newNums, n); free(newNums); return residue; }
int numComponents(ListNode* head, vector<int>& G) { int cc = 0; //count of connected components unordered_set<int> kk(G.begin(), G.end()); ListNode* temp = head; while(temp) { //if we find a value in LL if (inG(temp->val,kk)) { cc += 1; //keep checking the next value till you do not find value in vector, that will be the end of one cc while(temp->next && inG(temp->next->val,kk)) { temp = temp->next; } } else { temp = temp->next; } } return cc; }
void Joseki::fromJoseki(const Kyokumen &shoki, Teban shokiTeban, Kyokumen &k, int tesu, int &teNum, Te te[], int hindo[]) { teNum = 0; int i; for (i=0; i < m_JosekiSize; i++) { Kyokumen kk(shoki); Teban teban = shokiTeban; int j; for (j=0; j < tesu; j++) { if (m_JosekiData[i][j*2] == 0 || m_JosekiData[i][j*2] == 0xff) { break; } Te te = Te(teban, m_JosekiData[i][j*2+1], m_JosekiData[i][j*2], kk); kk.Move(teban, te, false); if (teban == ENEMY) { teban = SELF; } else { teban = ENEMY; } } if (j == tesu && k == kk) { Te ret = Te(teban, m_JosekiData[i][j*2+1], m_JosekiData[i][j*2], kk); if (m_JosekiData[i][j*2] == 0 || m_JosekiData[i][j*2] == 0xff) { continue; } int l; for (l=0; l < teNum; l++) { if (ret == te[l]) { hindo[l]++; break; } } if (l == teNum) { te[l] = ret; hindo[l] = 1; teNum++; } } } if (m_Child != NULL && teNum == 0) { m_Child->fromJoseki(shoki, shokiTeban, k, tesu, teNum, te, hindo); } }
Status get_sub_dir(const std::string& db_dir, std::vector<std::string>* sub_dir) { if (!is_dir(db_dir.c_str())) { //printf("is not dir"); return FILE_ERROR; } DIR* dir = opendir(db_dir.c_str()); if (dir == NULL) { return FILE_ERROR; //printf("opendir error"); } struct dirent* dir_ptr; std::vector<std::string> file_list; while (dir_ptr = readdir(dir)) { std::string kk(dir_ptr->d_name); if (kk[0] == '.') { continue; } std::string now = db_dir + "/" + kk; if (is_dir(now.c_str())) { sub_dir->push_back(now); } } return OK; }
void talk(const std::string& filename = "data.dat", const std::string& title = "CMSSM spectrum generator run-time") { std::ifstream ifs(filename.c_str()); std::string line; if (!ifs.good()) { cout << "Error: could not open file: " << filename << endl; return; } const int NSG = 5; const double range_start = 0., range_stop = 0.7; TH1D* combined[NSG]; TH1D* valid[NSG]; TH1D* invalid[NSG]; ULong64_t integral_valid[NSG]; ULong64_t integral_invalid[NSG]; ULong64_t integral_combined[NSG]; combined[0] = new TH1D("combined[0]", "Softsusy 3.6.2" , 20, range_start, range_stop); combined[1] = new TH1D("combined[1]", "FlexibleSUSY-CMSSM" , 20, range_start, range_stop); combined[2] = new TH1D("combined[2]", "SPheno 3.3.7" , 20, range_start, range_stop); combined[3] = new TH1D("combined[3]", "SPhenoMSSM" , 20, range_start, range_stop); combined[4] = new TH1D("combined[4]", "FlexibleSUSY-CMSSMNoFV", 20, range_start, range_stop); valid[0] = new TH1D("valid[0]", "Softsusy 3.6.2" , 20, range_start, range_stop); valid[1] = new TH1D("valid[1]", "FlexibleSUSY-CMSSM 1.2.0" , 20, range_start, range_stop); valid[2] = new TH1D("valid[2]", "SPheno 3.3.7" , 20, range_start, range_stop); valid[3] = new TH1D("valid[3]", "SPhenoMSSM 3.3.7/4.5.8" , 20, range_start, range_stop); valid[4] = new TH1D("valid[4]", "FlexibleSUSY-CMSSMNoFV 1.2.0", 20, range_start, range_stop); invalid[0] = new TH1D("invalid[0]", "Softsusy" , 20, range_start, range_stop); invalid[1] = new TH1D("invalid[1]", "FlexibleSUSY-CMSSM" , 20, range_start, range_stop); invalid[2] = new TH1D("invalid[2]", "SPheno" , 20, range_start, range_stop); invalid[3] = new TH1D("invalid[3]", "SPhenoMSSM" , 20, range_start, range_stop); invalid[4] = new TH1D("invalid[4]", "FlexibleSUSY-CMSSMNoFV", 20, range_start, range_stop); for (int i = 0; i < NSG; i++) { combined[i]->SetStats(0); valid[i]->SetStats(0); invalid[i]->SetStats(0); } // initialize integrals for (int i = 0; i < NSG; i++) { integral_valid[i] = 0; integral_invalid[i] = 0; integral_combined[i] = 0; } while (getline(ifs,line)) { std::istringstream input(line); std::string word; input >> word; if (word != "" && word[0] != '#') { std::istringstream kk(line); double m0, m12, tanb, sgnmu, a0; double time[NSG]; int error[NSG]; kk >> m0 >> m12 >> tanb >> sgnmu >> a0; for (int i = 0; i < NSG; i++) { try { kk >> time[i] >> error[i]; } catch (...) { cout << "Error: time and error are not convertible for" " spectrum generator " << i << endl; } } for (int i = 0; i < NSG; i++) { combined[i]->Fill(time[i]); integral_combined[i]++; if (error[i] == 0) { valid[i]->Fill(time[i]); integral_valid[i]++; } else { invalid[i]->Fill(time[i]); integral_invalid[i]++; } } } }
returnValue ExportArithmeticStatement::exportCodeMultiply( FILE* file, BooleanType transposeRhs1, const char* _realString, const char* _intString, int _precision ) const { if ( ( lhs == 0 ) || ( rhs1 == 0 ) || ( rhs2 == 0 ) ) return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); char assignString[3]; switch ( op0 ) { case ESO_ASSIGN: strcpy( assignString,"=" ); break; case ESO_ADD_ASSIGN: strcpy( assignString,"+=" ); break; case ESO_SUBTRACT_ASSIGN: strcpy( assignString,"-=" ); break; default: return ACADOERROR( RET_UNABLE_TO_EXPORT_STATEMENT ); } uint nRowsRhs1; uint nColsRhs1; if ( transposeRhs1 == BT_FALSE ) { nRowsRhs1 = rhs1->getNumRows( ); nColsRhs1 = rhs1->getNumCols( ); } else { nRowsRhs1 = rhs1->getNumCols( ); nColsRhs1 = rhs1->getNumRows( ); } if ( ( nColsRhs1 != rhs2->getNumRows( ) ) || ( nRowsRhs1 != lhs->getNumRows( ) ) || ( rhs2->getNumCols( ) != lhs->getNumCols( ) ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); char sign[2] = "+"; if ( op2 != ESO_UNDEFINED ) { if ( ( rhs3->getNumRows( ) != lhs->getNumRows( ) ) || ( rhs3->getNumCols( ) != lhs->getNumCols( ) ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); if ( op2 == ESO_SUBTRACT ) sign[0] = '-'; } BooleanType allZero; // uint ii, kk; // uint iiRhs1, kkRhs1; ExportIndex ii( "run1" ), iiRhs1( "run1" ); ExportIndex kk( "run3" ), kkRhs1( "run3" ); uint iMax = getNumRows( ); if ( outerLoopVariable.isGiven( ) == BT_FALSE ) iMax = 1; for( uint i=0; i<iMax; ++i ) { if ( outerLoopVariable.isGiven( ) == BT_FALSE ) acadoFPrintf( file,"for ( %s=0; %s<%d; ++%s ){\n", ii.name,ii.name,getNumRows(),ii.name ); else ii == i; for( uint j=0; j<getNumCols( ); ++j ) { allZero = BT_TRUE; acadoFPrintf( file,"%s %s", lhs->get(ii,j),assignString ); for( uint k=0; k<nColsRhs1; ++k ) { kk == k; if ( transposeRhs1 == BT_FALSE ) { iiRhs1 = ii; kkRhs1 = kk; } else { iiRhs1 = kk; kkRhs1 = ii; } if ( ( rhs1->isZero(iiRhs1,kkRhs1) == BT_FALSE ) && ( rhs2->isZero(kk,j) == BT_FALSE ) ) { allZero = BT_FALSE; if ( rhs1->isOne(iiRhs1,kkRhs1) == BT_FALSE ) { acadoFPrintf( file," %s %s", sign,rhs1->get(iiRhs1,kkRhs1) ); if ( rhs2->isOne(kk,j) == BT_FALSE ) acadoFPrintf( file,"*%s", rhs2->get(kk,j) ); } else { if ( rhs2->isOne(kk,j) == BT_FALSE ) acadoFPrintf( file," %s %s", sign,rhs2->get(kk,j) ); else acadoFPrintf( file," %s 1.0", sign ); } } } if ( ( op2 == ESO_ADD ) || ( op2 == ESO_SUBTRACT ) ) acadoFPrintf( file," + %s;\n", rhs3->get(ii,j) ); if ( op2 == ESO_UNDEFINED ) { if ( allZero == BT_TRUE ) acadoFPrintf( file," 0.0;\n" ); else acadoFPrintf( file,";\n" ); } } } if ( outerLoopVariable.isGiven( ) == BT_FALSE ) acadoFPrintf( file,"}\n" ); return SUCCESSFUL_RETURN; }
LOCAL_C void test2(CArrayFix<TArr<TText,4> >& aFix) // { test(aFix.Length()==sizeof(TArr<TText,4>)); test.Next(_L("AppendL and insert strings of length 4")); TPtrC des1=_L("abcd"); TPtrC des2=_L("efgh"); aFix.AppendL(*(const TArr<TText,4>*)des1.Ptr()); aFix.AppendL(*(const TArr<TText,4>*)des2.Ptr()); test(aFix.Count()==2); TPtrC des3(&aFix[0][0],4); TPtrC des4(&aFix[1][0],4); test(des3==_L("abcd")); test(des4==_L("efgh")); aFix.InsertL(1,*(const TArr<TText,4>*)_S("ijkl")); test(aFix.Count()==3); TPtrC des5(&aFix[2][0],4); test(des3==_L("abcd")); test(des4==_L("ijkl")); test(des5==_L("efgh")); test.Next(_L("Reset and Compress")); aFix.Reset(); TBuf<0x10> buf1=_L("abcdefgh"); aFix.AppendL((const TArr<TText,4>*)buf1.Ptr(),2); aFix.Compress(); TPtrC des6(&aFix[0][0],4); test(des6==_L("abcd")); TPtrC des7(&aFix[1][0],4); test(des7==_L("efgh")); buf1=_L("ghighhxy"); aFix.InsertL(1,(const TArr<TText,4>*)buf1.Ptr(),2); aFix.Compress(); TPtrC des8(&aFix[0][0],4); test(des8==_L("abcd")); TPtrC des9(&aFix[1][0],4); test(des9==_L("ghig")); TPtrC des10(&aFix[2][0],4); test(des10==_L("hhxy")); TPtrC des11(&aFix[3][0],4); test(des11==_L("efgh")); test.Next(_L("Sort strings")); TKeyArrayFix kk(0,ECmpNormal,0x04); aFix.Sort(kk); TPtrC des12(&aFix[0][0],4); test(des12==_L("abcd")); TPtrC des13(&aFix[1][0],4); test(des13==_L("efgh")); TPtrC des14(&aFix[2][0],4); test(des14==_L("ghig")); TPtrC des15(&aFix[3][0],4); test(des15==_L("hhxy")); test.Next(_L("Find and FindIsq")); aFix.Compress(); test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk)==2); aFix.Compress(); test(aFix.InsertIsqAllowDuplicatesL(*(const TArr<TText,4>*)_S("ffff"),kk)==3); aFix.Compress(); TRAPD(r,aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk)) test(r==KErrAlreadyExists); TInt aPos=0; test(aFix.Find(*(const TArr<TText,4>*)_S("xxxx"),kk,aPos)==1); test(aPos==6); test(aFix.Find(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0); test(aPos==0); test(aFix.Find(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0); test(aPos==4); test(aFix.Find(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0); test(aPos==2); test(aFix.Find(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0); test(aPos==5); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("bbbb"),kk,aPos)!=0); test(aPos==1); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0); test(aPos==0); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0); test(aPos==4); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0); test(aPos==2); test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("fghz"),kk)==4); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("fghz"),kk,aPos)==0); test(aPos==4); test(aFix.FindIsq(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0); test(aPos==6); }
void runSample() { // Getting random numbers int n = 100; uint64_t* nums = malloc(sizeof(uint64_t) * n); for (int i = 0; i < n; i++) { nums[i] = getNum(); } // Generate sequence and partition (same result) int* s = sequenceGenerate(n); int* p = partitionGenerate(n); // Results and times uint64_t results[9]; struct timeval times[10]; gettimeofday(×[0], NULL); results[0] = kk(nums, n); gettimeofday(×[1], NULL); results[1] = sequenceResidue(nums, s, n); gettimeofday(×[2], NULL); results[2] = partitionResidue(nums, p, n); gettimeofday(×[3], NULL); results[3] = repeatedRandom(nums, s, n, 1); gettimeofday(×[4], NULL); results[4] = repeatedRandom(nums, p, n, 0); gettimeofday(×[5], NULL); results[5] = hillClimbing(nums, s, n, 1); gettimeofday(×[6], NULL); results[6] = hillClimbing(nums, p, n, 0); gettimeofday(×[7], NULL); results[7] = simulatedAnnealing(nums, s, n, 1); gettimeofday(×[8], NULL); results[8] = simulatedAnnealing(nums, p, n, 0); gettimeofday(×[9], NULL); // Difference between times int diff[9]; for (int i = 0; i < 9; i++) { diff[i] = (int) ((1000000 * (times[i + 1].tv_sec - times[i].tv_sec) + (times[i + 1].tv_usec - times[i].tv_usec)) / 1000); } // Writing data and times to file FILE* f = fopen("data/data.csv", "a"); if (f == NULL) { printf("Error opening file\n"); return; } fprintf(f, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld\n", results[0], results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8]); fclose(f); f = fopen("data/times.csv", "a"); if (f == NULL) { printf("Error opening file\n"); return; } fprintf(f, "%i,%i,%i,%i,%i,%i,%i,%i,%i\n", diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7], diff[8]); fclose(f); // Free all allocated pointers free(nums); free(s); free(p); }
int main(int argc, char **argv) { char *x, *me = argv[0]; char *last_msg = "\\c"; errmsg_iam(me); if (argc<2) { help: errmsg_iam(0); carp(Kill_Help); _exit(1); } argv++; x = argv[0]; if (x[0]=='-' && x[1]=='h') goto help; /* -h... --help */ opendevconsole(); /* ignore next signals */ set_sa(SIGQUIT); set_sa(SIGCHLD); set_sa(SIGHUP ); set_sa(SIGTSTP); set_sa(SIGTTIN); set_sa(SIGTTOU); while (argv[0] && argv[0][0]=='-') { int sig=0, cfg_wall=0; char *y = argv[0] + 1; if ((unsigned int)(*y - '0') < 10) { sig = x_atoi(y); goto again; } switch(*y) { case 'v': cfg_verbose++; if (y[1]=='v') cfg_verbose++; break; case 'q': set_sa(SIGINT); break; case 's': chk_opt(argv,x); deep_sleep(x); break; case 'W': ++cfg_wall; case 'M': chk_opt(argv,x); if (x[0] == '%' && x[1] == 0) x = last_msg; else last_msg = x; print_escape(x, cfg_wall); break; case 'E': chk_opt(argv,x); *argv = x; execve(argv[0], argv, environ); carp("Unable to exec: ", argv[0]); _exit(1); default: #ifdef NSVC_SIGNAL_CODED { unsigned char *S, *Sig = (unsigned char *)NSVC_SIGNAL_CODED; for (S=Sig; *S; S += 2) if ((unsigned char)*y == *S) { sig = S[1]; goto again; } } #else #define kk(C,S) case C: sig=S; break kk('t',SIGTERM); kk('a',SIGALRM); kk('p',SIGSTOP); kk('c',SIGCONT); kk('h',SIGHUP); kk('i',SIGINT); kk('k',SIGKILL); #endif goto help; } again: if (sig > 0) { sync(); if (cfg_verbose) msg("Sending signal: ", y); if (kill(-1, sig)) carp("Unable to send signal: ", y); } argv++; } sync(); return 0; }
LOCAL_C void testFix(CArrayFix<TBuf<0x10> >& aFix) // // Test all methods // { test.Next(_L("Test all methods")); test(aFix.Count()==0); test(aFix.Length()==sizeof(TBuf<0x10>)); aFix.Compress(); test(TRUE); aFix.Reset(); test(TRUE); TKeyArrayFix kk(0,ECmpNormal,0x10); test(TRUE); aFix.Sort(kk); test(TRUE); TBuf<0x10> aa(_L("aaaaa")); aFix.InsertL(0,aa); test(TRUE); aFix[0].Fill(' '); test(TRUE); TBuf<0x10> z(aFix[0]); z.Length(); test(TRUE); aFix[0].Fill('a'); test(TRUE); TInt pp; test(aFix.Find(aa,kk,pp)==0); test(pp==0); aFix.Delete(0); TBuf<0x10> bb(_L("bbbbb")); aFix.AppendL(bb); test(aFix.Count()==1); test(aFix.InsertIsqAllowDuplicatesL(aa,kk)==0); test(aFix.InsertIsqAllowDuplicatesL(bb,kk)==2); test(aFix.FindIsq(aa,kk,pp)==0); test(pp==0); aFix.Reset(); for(TInt index=0;index<KTestGranularity*7/2;index++) aFix.AppendL(aa); const TBuf<0x10> *end=NULL; const TBuf<0x10> *ptr=NULL; for(TInt index2=0;index2<KTestGranularity*7/2;index2++) { if (end==ptr) { end=aFix.End(index2); ptr=&aFix[index2]; TInt seglen=end-ptr; test(seglen==KTestGranularity || seglen==(aFix.Count()-index2)); } test(&aFix[index2]==ptr++); } const TBuf<0x10> *bak=NULL; ptr=NULL; for(TInt index3=KTestGranularity*7/2;index3>0;index3--) { if (bak==ptr) { bak=aFix.Back(index3); ptr=&aFix[index3-1]+1; TInt seglen=ptr-bak; test(seglen==KTestGranularity || seglen==index3 || seglen==index3%KTestGranularity); } test(&aFix[index3-1]==--ptr); } //Test ExpandL //Expand array in slot 1 TBuf16<0x10> exp; exp=_L("abc AbC"); aFix.InsertL(0,exp); aFix.InsertL(1,exp); aFix.InsertL(2,exp); exp=aFix.ExpandL(1); test(aFix[0]==_L("abc AbC")); test(aFix[1]==_L("")); test(aFix[2]==_L("abc AbC")); test(aFix[3]==_L("abc AbC")); //Test ResizeL and InsertReplL //Resize the array to containing 20 records, //copying a record into any new slots. TBuf<0x10> res(_L("bbbbb")); aFix.Reset(); aFix.ResizeL(20,res); for(TInt i=0;i<20;i++) { test(aFix[1]==_L("bbbbb")); } }
void KNDdeTorusCollocation::init(const KNVector& sol, const KNVector& par) { double* t1 = new double[NTAU+1]; double* t2 = new double[NTAU+1]; for (size_t i2 = 0; i2 < nint2; i2++) { for (size_t i1 = 0; i1 < nint1; i1++) { for (size_t j2 = 0; j2 < ndeg2; j2++) { for (size_t j1 = 0; j1 < ndeg1; j1++) { const size_t idx = idxmap(j1, j2, i1, i2); // this is a unique time1(idx) = ((double)i1 + col1(j1)) / nint1; time2(idx) = ((double)i2 + col2(j2)) / nint2; } } } } sys->p_tau(p_tau, time1, par); p_xx.clear(); for (size_t idx = 0; idx < time1.size(); ++idx) { t1[0] = time1(idx); t2[0] = time2(idx); for (size_t k = 0; k < NTAU; k++) { t1[1+k] = (t1[0] - p_tau(k,idx) / par(0)) - floor(t1[0] - p_tau(k,idx) / par(0)); t2[1+k] = (t2[0] - par(RHO) * p_tau(k,idx) / par(0)) - floor(t2[0] - par(RHO) * p_tau(k,idx) / par(0)); } for (size_t k = 0; k < NTAU + 1; k++) { size_t i1 = static_cast<size_t>(floor(nint1 * t1[k])); size_t i2 = static_cast<size_t>(floor(nint2 * t2[k])); // some error checking if ((t1[k] > 1.0) || (t2[k] > 1.0) || (t1[k] < 0.0) || (t2[k] < 0.0)) std::cout << "Er "; if ((i1 >= nint1) || (i2 >= nint2)) std::cout << "Ei "; // end error checking for (size_t j2 = 0; j2 < ndeg2 + 1; j2++) { for (size_t j1 = 0; j1 < ndeg1 + 1; j1++) { const size_t idxKK = idxkk(j1, j2, k); kk(idxKK,idx) = idxmap(j1, j2, i1, i2); ee(idxKK,idx) = idxKK; } } } // sorting comp aa(kk.pointer(0,idx)); std::sort(ee.pointer(0,idx), ee.pointer(0,idx) + (NTAU + 1)*(ndeg1 + 1)*(ndeg2 + 1), aa); // filtering same indices rr(ee(0,idx),idx) = 0; size_t nz = 0; for (size_t i = 1; i < (NTAU + 1)*(ndeg1 + 1)*(ndeg2 + 1); i++) { if (kk(ee(i-1,idx),idx) != kk(ee(i,idx),idx)) nz++; rr(ee(i,idx),idx) = nz; } // interpolate the solution for (size_t k = 1; k < NTAU + 1; k++) { const double c1 = nint1 * t1[k] - floor(nint1 * t1[k]); const double c2 = nint2 * t2[k] - floor(nint2 * t2[k]); for (size_t j2 = 0; j2 < ndeg2 + 1; j2++) { for (size_t j1 = 0; j1 < ndeg1 + 1; j1++) { const size_t idxKK = idxkk(j1, j2, k); const double cf = poly_lgr_eval(mesh1, j1, c1) * poly_lgr_eval(mesh2, j2, c2); for (size_t p = 0; p < NDIM; p++) { p_xx(p, k - 1, idx) += cf * sol(p + NDIM * kk(idxKK, idx)); } } } } // derivatives at all delays for (size_t k = 0; k < NTAU + 1; k++) { const double c1 = nint1 * t1[k] - floor(nint1 * t1[k]); const double c2 = nint2 * t2[k] - floor(nint2 * t2[k]); for (size_t j2 = 0; j2 < ndeg2 + 1; j2++) { for (size_t j1 = 0; j1 < ndeg1 + 1; j1++) { const size_t idxKK = idxkk(j1, j2, k); const double cf1 = poly_dlg_eval(mesh1, j1, c1) * nint1 * poly_lgr_eval(mesh2, j2, c2); const double cf2 = poly_lgr_eval(mesh1, j1, c1) * poly_dlg_eval(mesh2, j2, c2) * nint2; for (size_t p = 0; p < NDIM; p++) { p_xx(p, NTAU + 2*k, idx) += cf1 * sol(p + NDIM * kk(idxKK, idx)); p_xx(p, NTAU + 2*k + 1, idx) += cf2 * sol(p + NDIM * kk(idxKK, idx)); } } } } } // for (int idx = 0; idx < time1.size(); ++idx) std::cout<<p_xx(0, NTAU, idx)<<"\t"; // std::cout<<"\np_xx(0,...) was\n"; delete[] t2; delete[] t1; }
void KNDdeTorusCollocation::jacobian(KNSparseMatrix& A, KNArray1D< KNVector* > Avar, KNVector& rhs, KNVector& par, KNVector& /*sol*/, KNArray1D<size_t>& var) { A.clear(); rhs.clear(); for (size_t r = 0; r < var.size(); r++) Avar(r)->clear(); // rhs doesn't need to be cleared // creates kk, ee, rr & interpolates p_xx & gets p_tau // init(sol, par); // builds up the structure of the sparse matrix for (size_t idx = 0; idx < time1.size(); ++idx) { const size_t lend = NDIM * (rr(ee((NTAU+1)*(ndeg1+1)*(ndeg2+1)-1,idx),idx) + 1); for (size_t p = 0; p < NDIM; p++) A.newLine(lend); } // the right-hand side sys->p_rhs(p_fx, time1, p_xx, par, 0); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) { rhs(p + NDIM*idx) = par(0) * p_fx(p,idx) - p_xx(p, NTAU, idx) - par(RHO) * p_xx(p, NTAU + 1, idx); } } // derivatives w.r.t the parameters for (size_t r = 0; r < var.size(); r++) { KNVector& deri = *Avar(r); deri.clear(); //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the period //!!!!!!!!!!!!!!!!! if (var(r) == 0) { for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = -p_fx(p,idx); } sys->p_dtau(p_dtau, time1, par, 0); for (size_t k = 0; k < NTAU; k++) { size_t nx = 1, vx = k, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { const double d = p_tau(k,idx) / par(0) - p_dtau(k,idx); deri(p + NDIM*idx) -= p_dfx(p, q, idx) * d * (p_xx(q, NTAU + 2 * (k + 1), idx) + par(RHO) * p_xx(q, NTAU + 2 * (k + 1) + 1, idx)); } } } } } else //!!!!!!!!!!!!!!!!! //!END w.r.t the period //!!!!!!!!!!!!!!!!! // //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the ordinary parameters //!!!!!!!!!!!!!!!!! // derivatives w.r.t. the real parameters if (var(r) < NPAR) { size_t nx = 0, vx = 0, np = 1, vp = var(r); sys->p_deri(p_dfp, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = - par(0) * p_dfp(p, 0, idx); } } else //!!!!!!!!!!!!!!!!! //!END w.r.t the ordinary parameters //!!!!!!!!!!!!!!!!! // //!!!!!!!!!!!!!!!!! //!BEGIN w.r.t the rotation number //!!!!!!!!!!!!!!!!! if (var(r) == RHO) { for (size_t idx = 0; idx < time1.size(); ++idx) { for (size_t p = 0; p < NDIM; p++) deri(p + NDIM*idx) = p_xx(p, NTAU + 1, idx); } sys->p_dtau(p_dtau, time1, par, 0); for (size_t k = 0; k < NTAU; k++) { const size_t nx = 1, vx = k, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); for (size_t idx = 0; idx < time1.size(); ++idx) { const double d = -p_tau(k, idx); for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { deri(p + NDIM*idx) -= p_dfx(p, q, idx) * d * p_xx(q, NTAU + 2 * (k + 1) + 1, idx); } } } } } //!!!!!!!!!!!!!!!!! //!END w.r.t the rotation number //!!!!!!!!!!!!!!!!! else std::cout << "Jac:NNN\n"; } // fill the matrix for (size_t k = 0; k < NTAU + 1; k++) { if (k != 0) { // evaluate the solution const size_t nx = 1, vx = k - 1, np = 0, vp = 0; sys->p_deri(p_dfx, time1, p_xx, par, 0, nx, &vx, np, &vp, p_dummy); } for (size_t idx = 0; idx < time1.size(); ++idx) { double tk1, tk2; if (k != 0) { tk1 = time1(idx) - p_tau(k-1,idx) / par(0); tk2 = time2(idx) - par(RHO) * p_tau(k-1,idx) / par(0); } else { tk1 = time1(idx); tk2 = time2(idx); } const double c1 = nint1 * tk1 - floor(nint1 * tk1); const double c2 = nint2 * tk2 - floor(nint2 * tk2); // the real jacobian for (size_t l2 = 0; l2 < ndeg2 + 1; l2++) { for (size_t l1 = 0; l1 < ndeg1 + 1; l1++) { const size_t idxK = idxkk(l1, l2, k); if (k != 0) { const double cf = poly_lgr_eval(mesh1, l1, c1) * poly_lgr_eval(mesh2, l2, c2); for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { // jacobian of rhs A.writeIndex(p + NDIM*idx, q + NDIM*rr(idxK, idx)) = static_cast<int>(q + NDIM * kk(idxK, idx)); A.writeData(p + NDIM*idx, q + NDIM*rr(idxK, idx)) -= cf * par(0) * p_dfx(p, q, idx); } } } else { const double cf1 = poly_dlg_eval(mesh1, l1, c1) * nint1 * poly_lgr_eval(mesh2, l2, c2); const double cf2 = poly_lgr_eval(mesh1, l1, c1) * poly_dlg_eval(mesh2, l2, c2) * nint2; for (size_t p = 0; p < NDIM; p++) { for (size_t q = 0; q < NDIM; q++) { // derivative part of the jacobian A.writeIndex(p + NDIM*idx, q + NDIM*rr(idxK, idx)) = static_cast<int>(q + NDIM * kk(idxK, idx)); if (p == q) A.writeData(p + NDIM*idx, q + NDIM*rr(idxK, idx)) += cf1 + par(RHO) * cf2; } } } // error check if (kk(idxK, idx) > (ndeg1*nint1)*(ndeg2*nint2)) std::cout << "D" << kk(idxK, idx); } } } } }
int main() { struct str3 a; a = kk(); write_int(a.a); }