static void test_mixed_index_list() { Tensor<float, 4> tensor(2,3,5,7); tensor.setRandom(); int dim2 = 1; int dim4 = 3; auto reduction_axis = make_index_list(0, dim2, 2, dim4); VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 0); VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1); VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 2); VERIFY_IS_EQUAL(internal::array_get<3>(reduction_axis), 3); VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[0]), 0); VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[1]), 1); VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[2]), 2); VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[3]), 3); typedef IndexList<type2index<0>, int, type2index<2>, int> ReductionIndices; ReductionIndices reduction_indices; reduction_indices.set(1, 1); reduction_indices.set(3, 3); EIGEN_STATIC_ASSERT((internal::array_get<0>(reduction_indices) == 0), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::array_get<2>(reduction_indices) == 2), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>()(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>()(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>()(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>()(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); #if 0 EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionIndices>()() == false), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionIndices>()() == false), YOU_MADE_A_PROGRAMMING_MISTAKE); #endif typedef IndexList<type2index<0>, type2index<1>, type2index<2>, type2index<3>> ReductionList; ReductionList reduction_list; EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>()(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>()(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>()(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>()(3, 3) == true), YOU_MADE_A_PROGRAMMING_MISTAKE); #if 0 EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionList>()() == true), YOU_MADE_A_PROGRAMMING_MISTAKE); EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionList>()() == true), YOU_MADE_A_PROGRAMMING_MISTAKE); #endif Tensor<float, 0> result1 = tensor.sum(reduction_axis); Tensor<float, 0> result2 = tensor.sum(reduction_indices); Tensor<float, 0> result3 = tensor.sum(reduction_list); float expected = 0.0f; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 5; ++k) { for (int l = 0; l < 7; ++l) { expected += tensor(i,j,k,l); } } } } VERIFY_IS_APPROX(result1(), expected); VERIFY_IS_APPROX(result2(), expected); VERIFY_IS_APPROX(result3(), expected); }
void ResourceBundleTest::TestConstruction() { UErrorCode err = U_ZERO_ERROR; Locale locale("te", "IN"); const char* testdatapath=loadTestData(err); if(U_FAILURE(err)) { dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath) + ", " + UnicodeString(u_errorName(err))); return; } /* Make sure that users using te_IN for the default locale don't get test failures. */ Locale originalDefault; if (Locale::getDefault() == Locale("te_IN")) { Locale::setDefault(Locale("en_US"), err); } ResourceBundle test1((UnicodeString)testdatapath, err); ResourceBundle test2(testdatapath, locale, err); //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err); //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err); UnicodeString result1(test1.getStringEx("string_in_Root_te_te_IN", err)); UnicodeString result2(test2.getStringEx("string_in_Root_te_te_IN", err)); if (U_FAILURE(err)) { errln("Something threw an error in TestConstruction()"); return; } logln("for string_in_Root_te_te_IN, default.txt had " + result1); logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2); if (result1 != "ROOT" || result2 != "TE_IN") errln("Construction test failed; run verbose for more information"); const char* version1; const char* version2; version1 = test1.getVersionNumber(); version2 = test2.getVersionNumber(); char *versionID1 = new char[1+strlen(version1)]; // + 1 for zero byte char *versionID2 = new char[1+ strlen(version2)]; // + 1 for zero byte strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed. strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed. logln(UnicodeString("getVersionNumber on default.txt returned ") + version1); logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2); if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0) errln("getVersionNumber() failed"); delete[] versionID1; delete[] versionID2; /* Restore the default locale for the other tests. */ Locale::setDefault(originalDefault, err); }
void expr_abstractor::operator()(unsigned base, unsigned num_bound, expr* const* bound, expr* n, expr_ref& result) { expr * curr = 0, *b = 0; SASSERT(n->get_ref_count() > 0); m_stack.push_back(n); for (unsigned i = 0; i < num_bound; ++i) { b = bound[i]; expr* v = m.mk_var(base + num_bound - i - 1, m.get_sort(b)); m_pinned.push_back(v); m_map.insert(b, v); } while(!m_stack.empty()) { curr = m_stack.back(); if (m_map.contains(curr)) { m_stack.pop_back(); continue; } switch(curr->get_kind()) { case AST_VAR: { m_map.insert(curr, curr); m_stack.pop_back(); break; } case AST_APP: { app* a = to_app(curr); bool all_visited = true; m_args.reset(); for (unsigned i = 0; i < a->get_num_args(); ++i) { if (!m_map.find(a->get_arg(i), b)) { m_stack.push_back(a->get_arg(i)); all_visited = false; } else { m_args.push_back(b); } } if (all_visited) { b = m.mk_app(a->get_decl(), m_args.size(), m_args.c_ptr()); m_pinned.push_back(b); m_map.insert(curr, b); m_stack.pop_back(); } break; } case AST_QUANTIFIER: { quantifier* q = to_quantifier(curr); expr_ref_buffer patterns(m); expr_ref result1(m); unsigned new_base = base + q->get_num_decls(); for (unsigned i = 0; i < q->get_num_patterns(); ++i) { expr_abstract(m, new_base, num_bound, bound, q->get_pattern(i), result1); patterns.push_back(result1.get()); } expr_abstract(m, new_base, num_bound, bound, q->get_expr(), result1); b = m.update_quantifier(q, patterns.size(), patterns.c_ptr(), result1.get()); m_pinned.push_back(b); m_map.insert(curr, b); m_stack.pop_back(); break; } default: UNREACHABLE(); } } VERIFY (m_map.find(n, b)); result = b; m_pinned.reset(); m_map.reset(); m_stack.reset(); m_args.reset(); }
void test_eigen(const std::string& fn, bool is_symm) { std::cout << "Reading..." << "\n"; std::size_t sz; // read file std::fstream f(fn.c_str(), std::fstream::in); //read size of input matrix read_matrix_size(f, sz); if (viennacl::is_row_major<MatrixLayout>::value) std::cout << "Testing row-major matrix of size " << sz << "-by-" << sz << std::endl; else std::cout << "Testing column-major matrix of size " << sz << "-by-" << sz << std::endl; viennacl::matrix<ScalarType> A_input(sz, sz), A_ref(sz, sz), Q(sz, sz); // reference vector with reference values from file std::vector<ScalarType> eigen_ref_re(sz); // calculated real eigenvalues std::vector<ScalarType> eigen_re(sz); // calculated im. eigenvalues std::vector<ScalarType> eigen_im(sz); // read input matrix from file read_matrix_body(f, A_input); // read reference eigenvalues from file read_vector_body(f, eigen_ref_re); f.close(); A_ref = A_input; std::cout << "Calculation..." << "\n"; Timer timer; timer.start(); // Start the calculation if(is_symm) viennacl::linalg::qr_method_sym(A_input, Q, eigen_re); else viennacl::linalg::qr_method_nsm(A_input, Q, eigen_re, eigen_im); /* std::cout << "\n\n Matrix A: \n\n"; matrix_print(A_input); std::cout << "\n\n"; std::cout << "\n\n Matrix Q: \n\n"; matrix_print(Q); std::cout << "\n\n"; */ double time_spend = timer.get(); std::cout << "Verification..." << "\n"; bool is_hessenberg = check_hessenberg(A_input); bool is_tridiag = check_tridiag(A_input); ublas::matrix<ScalarType> A_ref_ublas(sz, sz), A_input_ublas(sz, sz), Q_ublas(sz, sz), result1(sz, sz), result2(sz, sz); viennacl::copy(A_ref, A_ref_ublas); viennacl::copy(A_input, A_input_ublas); viennacl::copy(Q, Q_ublas); // compute result1 = ublas::prod(Q_ublas, A_input_ublas); (terribly slow when using ublas directly) for (std::size_t i=0; i<result1.size1(); ++i) for (std::size_t j=0; j<result1.size2(); ++j) { ScalarType value = 0; for (std::size_t k=0; k<Q_ublas.size2(); ++k) value += Q_ublas(i, k) * A_input_ublas(k, j); result1(i,j) = value; } // compute result2 = ublas::prod(A_ref_ublas, Q_ublas); (terribly slow when using ublas directly) for (std::size_t i=0; i<result2.size1(); ++i) for (std::size_t j=0; j<result2.size2(); ++j) { ScalarType value = 0; for (std::size_t k=0; k<A_ref_ublas.size2(); ++k) value += A_ref_ublas(i, k) * Q_ublas(k, j); result2(i,j) = value; } ScalarType prods_diff = matrix_compare(result1, result2); ScalarType eigen_diff = vector_compare(eigen_re, eigen_ref_re); bool is_ok = is_hessenberg; if(is_symm) is_ok = is_ok && is_tridiag; is_ok = is_ok && (eigen_diff < EPS); is_ok = is_ok && (prods_diff < EPS); // std::cout << A_ref << "\n"; // std::cout << A_input << "\n"; // std::cout << Q << "\n"; // std::cout << eigen_re << "\n"; // std::cout << eigen_im << "\n"; // std::cout << eigen_ref_re << "\n"; // std::cout << eigen_ref_im << "\n"; // std::cout << result1 << "\n"; // std::cout << result2 << "\n"; // std::cout << eigen_ref << "\n"; // std::cout << eigen << "\n"; printf("%6s [%dx%d] %40s time = %.4f\n", is_ok?"[[OK]]":"[FAIL]", (int)A_ref.size1(), (int)A_ref.size2(), fn.c_str(), time_spend); printf("tridiagonal = %d, hessenberg = %d prod-diff = %f eigen-diff = %f\n", is_tridiag, is_hessenberg, prods_diff, eigen_diff); std::cout << std::endl << std::endl; if (!is_ok) exit(EXIT_FAILURE); }
expr_ref bind_variables::abstract(expr* term, cache_t& cache, unsigned scope) { unsigned sz = m_todo.size(); m_todo.push_back(term); m_args.reset(); expr* b, *arg; while (m_todo.size() > sz) { expr* e = m_todo.back(); if (cache.contains(e)) { m_todo.pop_back(); continue; } switch(e->get_kind()) { case AST_VAR: { SASSERT(to_var(e)->get_idx() < scope); // mixing bound variables and free is possible for the caller, // but not proper use. // So we assert here even though we don't check for it. cache.insert(e, e); m_todo.pop_back(); break; } case AST_APP: { app* a = to_app(e); var2bound::obj_map_entry* w = m_var2bound.find_core(a); if (w) { var* v = w->get_data().m_value; if (!v) { // allocate a bound index. v = m.mk_var(m_names.size(), m.get_sort(a)); m_names.push_back(a->get_decl()->get_name()); m_bound.push_back(m.get_sort(a)); w->get_data().m_value = v; m_pinned.push_back(v); } if (scope == 0) { cache.insert(e, v); } else { var* v1 = m.mk_var(scope + v->get_idx(), m.get_sort(v)); m_pinned.push_back(v1); cache.insert(e, v1); } m_todo.pop_back(); break; } bool all_visited = true; bool some_diff = false; m_args.reset(); for (unsigned i = 0; i < a->get_num_args(); ++i) { arg = a->get_arg(i); if (!cache.find(arg, b)) { m_todo.push_back(arg); all_visited = false; } else if (all_visited) { m_args.push_back(b); if (b != arg) { some_diff = true; } } } if (all_visited) { if (some_diff) { b = m.mk_app(a->get_decl(), m_args.size(), m_args.c_ptr()); m_pinned.push_back(b); } else { b = a; } cache.insert(e, b); m_todo.pop_back(); } break; } case AST_QUANTIFIER: { quantifier* q = to_quantifier(e); expr_ref_buffer patterns(m); expr_ref result1(m); unsigned new_scope = scope + q->get_num_decls(); cache_t new_cache; for (unsigned i = 0; i < q->get_num_patterns(); ++i) { patterns.push_back(abstract(q->get_pattern(i), new_cache, new_scope)); } result1 = abstract(q->get_expr(), new_cache, new_scope); b = m.update_quantifier(q, patterns.size(), patterns.c_ptr(), result1.get()); m_pinned.push_back(b); cache.insert(e, b); m_todo.pop_back(); break; } default: UNREACHABLE(); } } return expr_ref(cache.find(term), m); }
int main(int argc, char* argv[]) { /*if (argc!=2) exit(0); double T = atof(argv[1]); printf(" T=%.3f\n",T);*/ system("rm lambda.T*"); GRID grid("params"); int N= grid.get_N(); Result result1(&grid); Result result2(&grid); Result result3(&grid); for (double T=0.30; T>0.05; T-=0.01) { for (double U=1.5; U<4.0; U+=0.1) { char FN[300]; sprintf( FN, "CHM.U%.3f.T%.3f", U, T); char FN2[300]; sprintf( FN2, "%s.FAILED", FN); if ((not FileExists(FN))and(not FileExists(FN2))) continue; char ldFN[300]; sprintf(ldFN,"lambdas_and_diffs.U%.3f.T%.3f",U,T); FILE* ldFile = fopen(ldFN,"w"); double lambdas[100]; int counter=0; for(int it=1; it<100; it++) { char FN[300]; sprintf( FN, "CHM.U%.3f.T%.3f.it%d", U, T, it); if(not result1.ReadFromFile(FN)) break; sprintf( FN, "CHM.U%.3f.T%.3f.it%d", U, T, it+1); if(not result2.ReadFromFile(FN)) break; sprintf( FN, "CHM.U%.3f.T%.3f.it%d", U, T, it+2); if(not result3.ReadFromFile(FN)) break; double sum = 0; for(int i=0; i<N; i++) sum += sqr( real(result2.G[i]-result1.G[i])) + sqr( imag(result2.G[i]-result1.G[i])); double diff = sqrt(sum)/(2.0*N); double simple_diff = abs( imag(result2.G[N/2]-result1.G[N/2]) ); double lambda = CalcLambda(N, result1.G, result2.G, result3.G); fprintf(ldFile,"%d %.15le %.15le %.15le\n",it,diff,lambda, simple_diff); lambdas[it-1]=lambda; counter++; complex<double>* dG = new complex<double>[N]; for(int i=0; i<N; i++) dG[i] = ((result2.G[i]-result1.G[i])/diff)/(2.0*((double) N)); sprintf( FN, "dG.U%.3f.T%.3f.it%d", U, T, it+1); PrintFunc(FN,N,dG,result1.omega); } fclose(ldFile); char lFN[300]; sprintf(lFN,"lambda.T%.3f",T); FILE* lFile = fopen(lFN,"a"); fprintf(lFile,"%.15le %.15le\n",U,FindLambda(counter,lambdas) ); fclose(lFile); } } return 0; }
void test_recursive_variant() { typedef boost::make_recursive_variant< int , std::vector<boost::recursive_variant_> >::type var1_t; std::vector<var1_t> vec1; vec1.push_back(3); vec1.push_back(5); vec1.push_back(vec1); vec1.push_back(7); var1_t var1(vec1); std::string result1( boost::apply_visitor( vector_printer(), var1 ) ); std::cout << "result1: " << result1 << '\n'; BOOST_CHECK(result1 == "( 3 5 ( 3 5 ) 7 ) "); typedef boost::make_recursive_variant< boost::variant<int, double> , std::vector<boost::recursive_variant_> >::type var2_t; std::vector<var2_t> vec2; vec2.push_back(boost::variant<int, double>(3)); vec2.push_back(boost::variant<int, double>(3.5)); vec2.push_back(vec2); vec2.push_back(boost::variant<int, double>(7)); var2_t var2(vec2); std::string result2( boost::apply_visitor( vector_printer(), var2 ) ); std::cout << "result2: " << result2 << '\n'; BOOST_CHECK(result2 == "( 3 3.5 ( 3 3.5 ) 7 ) "); typedef boost::make_recursive_variant< int , std::vector< boost::variant< double , std::vector<boost::recursive_variant_> > > >::type var3_t; typedef boost::variant<double, std::vector<var3_t> > var4_t; std::vector<var3_t> vec3; vec3.push_back(3); vec3.push_back(5); std::vector<var4_t> vec4; vec4.push_back(3.5); vec4.push_back(vec3); vec3.push_back(vec4); vec3.push_back(7); var4_t var4(vec3); std::string result3( boost::apply_visitor( vector_printer(), var4 ) ); std::cout << "result2: " << result3 << '\n'; BOOST_CHECK(result3 == "( 3 5 ( 3.5 ( 3 5 ) ) 7 ) "); typedef boost::make_recursive_variant< double, std::vector<var1_t> >::type var5_t; std::vector<var5_t> vec5; vec5.push_back(3.5); vec5.push_back(vec1); vec5.push_back(17.25); std::string result5( vector_printer()(vec5) ); std::cout << "result5: " << result5 << '\n'; BOOST_CHECK(result5 == "( 3.5 ( 3 5 ( 3 5 ) 7 ) 17.25 ) "); typedef boost::make_recursive_variant< int, std::map<int, boost::recursive_variant_> >::type var6_t; var6_t var6; }
static void execute(const Token *expr, std::map<unsigned int, MathLib::bigint> * const programMemory, MathLib::bigint *result, bool *error) { if (!expr) *error = true; else if (expr->isNumber()) *result = MathLib::toLongNumber(expr->str()); else if (expr->varId() > 0) { const std::map<unsigned int, MathLib::bigint>::const_iterator var = programMemory->find(expr->varId()); if (var == programMemory->end()) *error = true; else *result = var->second; } else if (expr->isComparisonOp()) { MathLib::bigint result1(0), result2(0); execute(expr->astOperand1(), programMemory, &result1, error); execute(expr->astOperand2(), programMemory, &result2, error); if (expr->str() == "<") *result = result1 < result2; else if (expr->str() == "<=") *result = result1 <= result2; else if (expr->str() == ">") *result = result1 > result2; else if (expr->str() == ">=") *result = result1 >= result2; else if (expr->str() == "==") *result = result1 == result2; else if (expr->str() == "!=") *result = result1 != result2; } else if (expr->str() == "=") { execute(expr->astOperand2(), programMemory, result, error); if (!*error && expr->astOperand1() && expr->astOperand1()->varId()) (*programMemory)[expr->astOperand1()->varId()] = *result; else *error = true; } else if (expr->str() == "++" || expr->str() == "--") { if (!expr->astOperand1() || expr->astOperand1()->varId() == 0U) *error = true; else { std::map<unsigned int, MathLib::bigint>::iterator var = programMemory->find(expr->astOperand1()->varId()); if (var == programMemory->end()) *error = true; else { if (var->second == 0 && expr->str() == "--" && expr->astOperand1()->variable() && expr->astOperand1()->variable()->typeStartToken()->isUnsigned()) *error = true; // overflow *result = var->second + (expr->str() == "++" ? 1 : -1); var->second = *result; } } } else if (expr->isArithmeticalOp() && expr->astOperand1() && expr->astOperand2()) { MathLib::bigint result1(0), result2(0); execute(expr->astOperand1(), programMemory, &result1, error); execute(expr->astOperand2(), programMemory, &result2, error); if (expr->str() == "+") *result = result1 + result2; else if (expr->str() == "-") *result = result1 - result2; else if (expr->str() == "*") *result = result1 * result2; else if (result2 == 0) *error = true; else if (expr->str() == "/") *result = result1 / result2; else if (expr->str() == "%") *result = result1 % result2; } else if (expr->str() == "&&") { bool error1 = false; execute(expr->astOperand1(), programMemory, result, &error1); if (!error1 && *result == 0) *result = 0; else { bool error2 = false; execute(expr->astOperand2(), programMemory, result, &error2); if (error1 && error2) *error = true; if (error2) *result = 1; else *result = !!*result; } } else if (expr->str() == "||") { execute(expr->astOperand1(), programMemory, result, error); if (*result == 0 && *error == false) execute(expr->astOperand2(), programMemory, result, error); } else *error = true; }