expression_tree GCD(const expression_tree::operands_t& ops, enviroment& env) { if ( ops.size() == 0 ) { env.raise_error("GCD", "called without arguments"); return expression_tree::make_operator("GCD", ops); } /*else if ( ops.size() == 1 ) { return ops[0]; }*/ else { bool all_integer = std::find_if( ops.begin(), ops.end(), [](const expression_tree& expr) { return expr.get_type() != expression_tree::EXACT_NUMBER || !is_mpq_integer(expr.get_exact_number()); } ) == ops.end(); if ( all_integer ) { std::vector<mpz_class> integers(ops.size()); for ( unsigned i = 0; i < ops.size(); ++i ) { integers[i] = ops[i].get_exact_number().get_num(); } return expression_tree::make_exact_number( mpq_class( integer_gcd(integers) ) ); } else { return expression_tree::make_operator("GCD", ops); } } }
int main() { SICPStream *s = integers(1); for (int i=5; i>=0; --i) { printf("%p, %d\n", s, FIRST(s)); SICPStream *s1 = REST(s); delete s; s = s1; } return 0; }
int main() { functions(); multi_variable_functions(); derivation(); more_about_derivation(); higher_level_derivatives(); more_functions(); higher_order_functions(); simplifications(); integers(); #ifdef AUTO_DERIVE_COMPLEX_TESTS complex_variables(); #endif }
void test_vector(void) { auto sum_the_ints = [](int a, int i) -> int { return a + i; }; auto sum_the_squares = [](int a, int i) -> int { return a + i*i; }; int some_ints[] = { 1, 2, 3, 4, 5 }; mtr::vector<int> integers(some_ints, 5); auto array = integers.append(6); int sum = array.foreach(0)(sum_the_ints); int sum_of_squares = array.foreach(0)(sum_the_squares); std::cout << "array count: " << array.length() << ", " << sum << ", " << sum_of_squares << std::endl; mtr::vector<test_class> v1; auto v2 = v1.append(test_class()); auto v3 = v2.append(test_class()); printf("All done with test_vector.\n"); }
int main(){ List<int> integers(10); for(int i = 0; i < integers.length; i++){ integers.set(i, i * 100); printf("%d ", integers.get(i)); } printf("\n"); // this loop should print: 0 100 200 300 400 500 600 700 800 900 List<Point *> points(5); for(int i = 0; i < points.length; i++){ Point * p = new Point; p->x = i * 10; p->y = i * 100; points.set(i, p); printf("(%d, %d) ", points.get(i)->x, points.get(i)->y); delete p; } printf("\n"); // this loop should print: (0, 0) (10, 100) (20, 200) (30, 300) (40, 400) }
int _tmain(int argc, _TCHAR* argv[]) { std::cout << "max (1,2) is " << max(1,2) << std::endl; std::cout << "max (4,3) is " << max(4,3) << std::endl; std::cout << "max (5,6) is " << max(5,6) << std::endl; // use a called out specialization.. this requires the use of a specific // implementation when things may be ambiguous.. std::cout << "max (33,2.0) is " << max<double>(33,2.0) << std::endl; // use our class template Accum<int> integers(0); integers += 3; integers += 7; std::cout << integers.GetTotal() << std::endl; Accum<std::string> strings(""); strings += "hello"; strings += " "; strings += "world"; std::cout << strings.GetTotal() << std::endl; //integers += "testing"; //strings += 4; Accum<Something> somethings(0); Something some1(55); Something some2(66); somethings += some1; somethings += some2; std::cout << somethings.GetTotal() << std::endl; return 0; }
SICPStream *integers(int starting) { return PAIR(starting, integers(starting + 1)); }