uint64_t quadtree::max_diff(const std::unique_ptr<node> & subroot, const std::unique_ptr<node> & leaf) const { if(isLeaf(leaf)){ return pow(int(leaf->element.red)-int(subroot->element.red),2)+ pow(int(leaf->element.green)-int(subroot->element.green),2)+ pow(int(leaf->element.blue)-int(subroot->element.blue),2); } int max = 0; int tmp; tmp = max_diff(subroot, leaf->northwest); if(tmp > max) max = tmp; tmp = max_diff(subroot, leaf->northeast); if(tmp > max) max = tmp; tmp = max_diff(subroot, leaf->southwest); if(tmp > max) max = tmp; tmp = max_diff(subroot, leaf->southeast); if(tmp > max) max = tmp; return max; }
std::vector<vec3f> displace_polyline(const std::vector<vec3f> &poly, const float min_length, const float fac_limit) const { std::vector<vec3f> res; std::vector<vec3f> to_add; std::vector<vec3f>::const_iterator input(poly.begin()); res.push_back(vlookup(*input++)); to_add.push_back(vlookup(*input++)); vec3f split; while(true) { while(!to_add.empty()) { while(max_diff(split, res.back(), to_add.back(), min_length, fac_limit)) { to_add.push_back(split); } res.push_back(to_add.back()); to_add.pop_back(); } if(input == poly.end()) return res; to_add.push_back(vlookup(*input++)); } }
boost::test_tools::predicate_result are_close(const F& a, const F& b, typename F::result_type eps) { typename F::result_type norma = a.marginal(); typename F::result_type normb = b.marginal(); if (a.arguments() == b.arguments() && max_diff(a, b) < eps && (norma > normb ? norma - normb : normb - norma) < eps) { return true; } else { boost::test_tools::predicate_result result(false); result.message() << "the two factors differ [\n" << a << "!=" << b << "]"; return result; } }
boost::test_tools::predicate_result are_close(const libgm::canonical_gaussian<T, Var>& a, const libgm::canonical_gaussian<T, Var>& b, T eps) { T multa = a.log_multiplier(); T multb = b.log_multiplier(); if (a.arguments() == b.arguments() && max_diff(a, b) < eps && std::abs(multa - multb) < eps) { return true; } else { boost::test_tools::predicate_result result(false); result.message() << "the two factors differ [\n" << a << "!=" << b << "]"; return result; } }
void displace_shapes(osm::shape_t &s, const float min_length, const float fac_limit, osm::network &net) const { osm::shape_t res; osm::shape_t to_add; osm::shape_t::iterator input(s.begin()); (*input)->xy = vlookup((*input)->xy); res.push_back(*input); ++input; (*input)->xy = vlookup((*input)->xy); to_add.push_back(*input); ++input; vec3f split; while(true) { while(!to_add.empty()) { while(max_diff(split, res.back()->xy, to_add.back()->xy, min_length, fac_limit)) { to_add.push_back(net.add_node(split, res.back()->is_overpass && to_add.back()->is_overpass)); } res.push_back(to_add.back()); to_add.pop_back(); } if(input == s.end()) { s.swap(res); return; } (*input)->xy = vlookup((*input)->xy); to_add.push_back(*input); ++input; } }
void container_test(X& r, T const&) { typedef BOOST_DEDUCED_TYPENAME X::iterator iterator; typedef BOOST_DEDUCED_TYPENAME X::const_iterator const_iterator; typedef BOOST_DEDUCED_TYPENAME X::difference_type difference_type; typedef BOOST_DEDUCED_TYPENAME X::size_type size_type; typedef BOOST_DEDUCED_TYPENAME boost::iterator_value<iterator>::type iterator_value_type; typedef BOOST_DEDUCED_TYPENAME boost::iterator_value<const_iterator>::type const_iterator_value_type; typedef BOOST_DEDUCED_TYPENAME boost::iterator_difference<iterator>::type iterator_difference_type; typedef BOOST_DEDUCED_TYPENAME boost::iterator_difference<const_iterator>::type const_iterator_difference_type; typedef BOOST_DEDUCED_TYPENAME X::value_type value_type; typedef BOOST_DEDUCED_TYPENAME X::reference reference; typedef BOOST_DEDUCED_TYPENAME X::const_reference const_reference; // value_type BOOST_MPL_ASSERT((boost::is_same<T, value_type>)); boost::function_requires<boost::CopyConstructibleConcept<X> >(); // reference_type / const_reference_type BOOST_MPL_ASSERT((boost::is_same<T&, reference>)); BOOST_MPL_ASSERT((boost::is_same<T const&, const_reference>)); // iterator boost::function_requires<boost::InputIteratorConcept<iterator> >(); BOOST_MPL_ASSERT((boost::is_same<T, iterator_value_type>)); BOOST_MPL_ASSERT((boost::is_convertible<iterator, const_iterator>)); // const_iterator boost::function_requires<boost::InputIteratorConcept<const_iterator> >(); BOOST_MPL_ASSERT((boost::is_same<T, const_iterator_value_type>)); // difference_type BOOST_MPL_ASSERT((boost::mpl::bool_< std::numeric_limits<difference_type>::is_signed>)); BOOST_MPL_ASSERT((boost::mpl::bool_< std::numeric_limits<difference_type>::is_integer>)); BOOST_MPL_ASSERT((boost::is_same<difference_type, iterator_difference_type>)); BOOST_MPL_ASSERT((boost::is_same<difference_type, const_iterator_difference_type>)); // size_type BOOST_MPL_ASSERT_NOT((boost::mpl::bool_< std::numeric_limits<size_type>::is_signed>)); BOOST_MPL_ASSERT((boost::mpl::bool_< std::numeric_limits<size_type>::is_integer>)); // size_type can represent any non-negative value type of difference_type // I'm not sure about either of these tests... size_type max_diff((std::numeric_limits<difference_type>::max)()); difference_type converted_diff(max_diff); BOOST_TEST((std::numeric_limits<difference_type>::max)() == converted_diff); BOOST_TEST( static_cast<comparison_type>( (std::numeric_limits<size_type>::max)()) > static_cast<comparison_type>( (std::numeric_limits<difference_type>::max)())); // I don't test the runtime post-conditions here. X u; BOOST_TEST(u.size() == 0); BOOST_TEST(X().size() == 0); X a,b; sink(X(a)); X u2(a); X u3 = a; X* ptr = new X(); X& a1 = *ptr; (&a1)->~X(); X const a_const; test::check_return_type<iterator>::equals(a.begin()); test::check_return_type<const_iterator>::equals(a_const.begin()); test::check_return_type<const_iterator>::equals(a.cbegin()); test::check_return_type<const_iterator>::equals(a_const.cbegin()); test::check_return_type<iterator>::equals(a.end()); test::check_return_type<const_iterator>::equals(a_const.end()); test::check_return_type<const_iterator>::equals(a.cend()); test::check_return_type<const_iterator>::equals(a_const.cend()); a.swap(b); test::check_return_type<X>::equals_ref(r = a); test::check_return_type<size_type>::equals(a.size()); test::check_return_type<size_type>::equals(a.max_size()); test::check_return_type<bool>::convertible(a.empty()); // Allocator typedef BOOST_DEDUCED_TYPENAME X::allocator_type allocator_type; test::check_return_type<allocator_type>::equals(a_const.get_allocator()); }
void test_admm_gauss(int mode) { int i; double * y; double * x; double * w; int n; int k; int family; int max_iter; int lam_flag; int obj_flag; double * lambda; int nlambda; double lambda_min_ratio; double * beta; double * pred; double * obj; int * iter; double rho; double obj_tol; double predict_zero_tol; /* Input parameters; will usually be given by parent function to tf_admm */ n = 8; k = 1; family = FAMILY_GAUSSIAN; max_iter = 5; lam_flag = 1; obj_flag = 1; nlambda = 1; lambda_min_ratio = 1e-4; rho = 1; obj_tol = 1e-12; y = (double *) malloc(n * sizeof(double)); x = (double *) malloc(n * sizeof(double)); w = (double *) malloc(n * sizeof(double)); lambda = (double *) malloc(nlambda * sizeof(double)); beta = (double *) malloc(n * nlambda * sizeof(double)); pred = (double *) malloc(n * sizeof(double)); obj = (double *) malloc(max_iter * nlambda * sizeof(double)); iter = (int *) malloc( nlambda * sizeof(int)); srand(5489); x[0] = 0; for (i = 1; i < n; i++) x[i] = x[i-1] + (rand() % 100)/100.; for (i = 0; i < n; i++) y[i] = x[i] * x[i] + 0.5 * (rand() % 100)/100.; for (i = 0; i < n; i++) w[i] = 0; lambda[0] = 1; /* Call the tf_admm function */ tf_admm(y, x, w, n, k, family, max_iter, lam_flag, obj_flag, lambda, nlambda, lambda_min_ratio, beta, obj, iter, rho, obj_tol); printf("\n--------------- (x,y) ---------------------------\n"); for (i = 0; i < n; i++) printf("%.3f\t%1.f\n", x[i], y[i]); printf("\n---------- lambda -------------------------------\n"); for (i = 0; i < nlambda; i++) printf("%f\n", lambda[i]); printf("\n---------- beta_1 -------------------------------\n"); for (i = 0; i < n; i++) printf("%f\n", beta[i]); /* printf("\n---------- beta_2 -------------------------------\n"); for (i = 0; i < n; i++) printf("%f\n", beta[i + n]); printf("\n---------- beta_3 -------------------------------\n"); for (i = 0; i < n; i++) printf("%f\n", beta[i + n*2]); */ predict_zero_tol = 1e-12; double err; for(i = 0; i < nlambda; i++) { int offset = i*n; tf_predict_gauss(beta + offset, x, n, k, x, n, pred, predict_zero_tol); err = max_diff(beta + offset, pred, n); printf("Prediction difference at input points=%E\n", err); if(!(err < 1e-12 )) printf("Prediction failed at input points (n=%d,k=%d,lam=%.4f,err=%E)\n",n,k,lambda[i], err); } /* Logistic loss */ family = FAMILY_LOGISTIC; double prob1, prob2; for (i = 0; i < n; i++) { prob1 = 1/ ( 1 + exp(-x[i]) ); prob2 = (rand() % 101)/100.; y[i] = prob1 <= prob2 ? 1 : 0; } printf("\n--------------- (x,y) ---------------------------\n"); for (i = 0; i < n; i++) printf("%.3f\t%1.f\n", x[i], y[i]); lambda[0] = 1e9; tf_admm(y, x, w, n, k, family, max_iter, lam_flag, obj_flag, lambda, nlambda, lambda_min_ratio, beta, obj, iter, rho, obj_tol); printf("\n---------- lambda -------------------------------\n"); for (i = 0; i < nlambda; i++) printf("%f\n", lambda[i]); printf("\n---------- beta_1 (logistic) -----------------------\n"); for (i = 0; i < n; i++) printf("%f\n", beta[i]); /* Free the allocated arrays */ free(y); free(x); free(w); free(lambda); free(beta); free(obj); free(iter); free(pred); }
//##################################################################### // Function Intersects //##################################################################### // From http://www.acm.org/tog/GraphicsGems/gems/BoxSphere.c template<class T,class TV> bool Intersects(const RANGE<TV>& box,const SPHERE<TV>& sphere,const T& thickness_over_two) { T dmin=0;TV min_diff=sphere.center-box.min_corner+thickness_over_two,max_diff=sphere.center-box.max_corner-thickness_over_two; for(int i=1;i<=TV::dimension;i++){if(min_diff(i)<0) dmin+=sqr(min_diff(i));else if(max_diff(i)>0) dmin+=sqr(max_diff(i));} return dmin<=sqr(sphere.radius); }
bool quadtree::isPrunable(const std::unique_ptr<node> & subroot, uint32_t tolerance) const { return max_diff(subroot, subroot) <= tolerance; }
static void channel_test (void) { static float float_data [1024] ; static float read_float [1024] ; static int read_int [1024] ; static short read_short [1024] ; unsigned int ch, k, position = 0 ; gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 0.9) ; for (ch = 1 ; ch <= 8 ; ch++) { SNDFILE *file ; SF_INFO wsfinfo, rsfinfo ; sf_count_t wframes = ARRAY_LEN (float_data) / ch ; double maxdiff ; char filename [256] ; snprintf (filename, sizeof (filename), "chan_%d.wav", ch) ; print_test_name (__func__, filename) ; sf_info_setup (&wsfinfo, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 48000, ch) ; sf_info_clear (&rsfinfo) ; /* Write the test file. */ file = test_open_file_or_die (filename, SFM_WRITE, &wsfinfo, SF_FALSE, __LINE__) ; test_writef_float_or_die (file, 0, float_data, wframes, __LINE__) ; sf_close (file) ; /* Read it as float and test. */ file = test_open_file_or_die (filename, SFM_READ, &rsfinfo, SF_FALSE, __LINE__) ; exit_if_true (rsfinfo.frames == 0, "\n\nLine %d : Frames in file %" PRId64 ".\n\n", __LINE__, rsfinfo.frames) ; exit_if_true (wframes != rsfinfo.frames, "\n\nLine %d : Wrote %" PRId64 ", read %" PRId64 " frames.\n\n", __LINE__, wframes, rsfinfo.frames) ; sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ; test_readf_float_or_die (file, 0, read_float, rsfinfo.frames, __LINE__) ; compare_float_or_die (float_data, read_float, ch * rsfinfo.frames, __LINE__) ; /* Read it as short and test. */ test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ; test_readf_short_or_die (file, 0, read_short, rsfinfo.frames, __LINE__) ; for (k = 0 ; k < ARRAY_LEN (read_float) ; k++) read_float [k] = read_short [k] * (0.9 / 0x8000) ; maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ; exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ; /* Read it as int and test. */ test_seek_or_die (file, 0, SEEK_SET, 0, ch, __LINE__) ; test_readf_int_or_die (file, 0, read_int, rsfinfo.frames, __LINE__) ; for (k = 0 ; k < ARRAY_LEN (read_float) ; k++) read_float [k] = read_int [k] * (0.9 / 0x80000000) ; maxdiff = max_diff (float_data, read_float, ch * rsfinfo.frames, &position) ; exit_if_true (maxdiff > 0.5, "\n\nLine %d : Max diff is %f at index %u\n\n", __LINE__, maxdiff, position) ; sf_close (file) ; unlink (filename) ; printf ("ok\n") ; } ; return ; } /* channel_test */
int main() { std::cout << "LUL" << std::endl; { BST<int> bst({3}); // 3 } { BST<int> bst({3, 4}); // 3 | // \ | // 4 | } { BST<int> bst({3, 4, 1}); // 3 | // / \ | // 1 4 | } { BST<int> bst({3, 4, 1, 2}); // 3 | // / \ | // 1 4 | // \ | // 2 | } { BST<int> bst({3,4,1,2,7}); // 3 | // / \ | // 1 4 | // \ \ | // 2 7 | std::cout << bst << std::endl; std::cout << bst.height() << std::endl; } /* BST<int> bst({3,4,1,2,7,3}); // 3 | // / \ | // 1 4 | // \ \ | // 2 7 | // \ | // 3 | // // */ std::vector<int> temp; int sign = 1; for (int i = 0; i < 1000; ++i) { sign *= -1; temp.push_back(i * sign); } BST<int> bst(temp.begin(), temp.end()); std::cout << bst << std::endl; // prints 1 2 3 3 4 7 std::cout << bst.size() << std::endl; //prints 6 std::cout << bst.min() << std::endl; // prints 1 std::cout << bst.max() << std::endl; // prints 7 std::cout << bst.height() << std::endl; // prints 4 std::cout << spine(bst).height() << std::endl; // prints 6 std::cout << spine(bst) << std::endl; std::cout << "FIND 4 : " << bst.find(4) << std::endl; // prints 4 7 std::cout << "FIND 11 : " << bst.find(11) << std::endl; //prints nothing (possibly one space) std::cout << max_diff(bst) << std::endl; //prints 3 return 0; }