void ia_test_all(void) { ia_create_test(); ia_range_test(); ia_of_string_test(); ia_fn_test(); ia_of_da_test(); ia_contains_test(); ia_fill_test(); ia_fill_from_to_test(); ia_index_test(); ia_index_from_test(); ia_index_fn_test(); ia_last_index_test(); ia_last_index_from_test(); ia_last_index_fn_test(); ia_sort_test(); ia_sort_dec_test(); ia_insert_test(); ia_remove_test(); ia_each_test(); ia_each_state_test(); ia_foldl_test(); ia_foldr_test(); ia_filter_test(); // ia_filter_state_test(); ia_choose_test(); ia_exists_test(); ia_forall_test(); #if 0 Array a = ia_fn(20, ia_rnd, 5); ia_println(a); // get number of different numbers in array ia_sort(a); ia_println(a); int n = a_length(a); int k = (n > 0) ? 1 : 0; for (int i = 1; i < n; i++) { if (ia_get(a, i-1) != ia_get(a, i)) { k++; } } printiln(k); a_free(a); #endif #if 0 // timing int n = 2000000000; Array a = ia_create(n, 0); time_function( ia_fill_fn(a, fn_id) ); a_free(a); a = ia_create(n, 0); time_function( ia_id(a) ); a_free(a); #endif }
Array da_of_ia(Array array) { ia_assert_element_size(array); int n = a_length(array); Array result = da_create(n, 0.0); int *src = array->a; double *dst = result->a; for (int i = 0; i < n; i++) { dst[i] = src[i]; } return result; }
bool da_forall_state(Array array, DoubleIntDoubleAnyToBool predicate, double x, Any state) { assert_function_not_null(predicate); da_assert_element_size(array); double *a = array->a; for (int i = 0; i < a_length(array); i++) { if (!predicate(a[i], i, x, state)) { return false; } } return true; }
bool da_exists(Array array, DoubleIntDoubleToBool predicate, double x) { assert_function_not_null(predicate); da_assert_element_size(array); double *a = array->a; for (int i = 0; i < a_length(array); i++) { if (predicate(a[i], i, x)) { return true; } } return false; }
Array ia_of_da(Array array) { da_assert_element_size(array); int n = a_length(array); Array result = ia_create(n, 0); double *a = array->a; int *b = result->a; for (int i = 0; i < n; i++) { b[i] = round(a[i]); } return result; }
bool ia_forall_state(Array array, IntIntIntAnyToBool predicate, int x, Any state) { assert_function_not_null(predicate); ia_assert_element_size(array); int *a = array->a; for (int i = 0; i < a_length(array); i++) { if (!predicate(a[i], i, x, state)) { return false; } } return true; }
bool sa_forall(Array array, StringIntStringToBool predicate, String x) { assert_function_not_null(predicate); sa_assert_element_size(array); String *a = array->a; for (int i = 0; i < a_length(array); i++) { if (!predicate(a[i], i, x)) { return false; } } return true; }
bool ia_exists(Array array, IntIntIntToBool predicate, int x) { assert_function_not_null(predicate); ia_assert_element_size(array); int *a = array->a; for (int i = 0; i < a_length(array); i++) { if (predicate(a[i], i, x)) { return true; } } return false; }
bool sa_exists_state(Array array, StringIntStringAnyToBool predicate, String x, Any state) { assert_function_not_null(predicate); sa_assert_element_size(array); String *a = array->a; for (int i = 0; i < a_length(array); i++) { if (predicate(a[i], i, x, state)) { return true; } } return false; }
static void a_sub_test(void) { printsln((String)__func__); Array array, array2, sub; array = sa_of_string("1, 2, 3, 4"); sub = a_sub(array, 0, a_length(array)); sa_check_expect(array, sub); a_free(sub); sub = a_sub(array, -1, a_length(array) + 1); sa_check_expect(array, sub); a_free(sub); array2 = sa_of_string("2, 3, 4"); // sa_println(array2); // printiln(a_length(array2)); sub = a_sub(array, 1, a_length(array)); sa_check_expect(array2, sub); sa_free(array2); a_free(sub); array2 = sa_of_string("2, 3"); sub = a_sub(array, 1, a_length(array) - 1); sa_check_expect(array2, sub); sa_free(array2); a_free(sub); array2 = sa_create(0, ""); sub = a_sub(array, 1, 1); sa_check_expect(array2, sub); a_free(sub); sub = a_sub(array, 2, 1); sa_check_expect(array2, sub); sa_free(array2); a_free(sub); sa_free(array); }
// Return an array that contains every other element of // the provided array, i.e., the result array contains // the elements at the even indices of the provided array. Array every_other(Array array) { int n = a_length(array); int m = (n + 1) / 2; Array result = ia_create(m, 0); // Iterate over the even indices in the original order. // Copy each element at an even index to the result array. for (int i = 0; i < n; i += 2) { assert(i < n); assert(i >= 0 && (i % 2) == 0); // printiln(i); ia_set(result, i / 2, ia_get(array, i)); } // return ia_create(0, 0); return result; }
bool contains_negative(Array array) { // Iterate over the elements of the array (any order). // For each array element, check whether it is negative. // If so return true, otherwise continue // until all of the elements have been inspected. // If there is no negative value, answer false. int n = a_length(array); for (int i = 0; i < n; i++) { assert(i < n); assert(i >= 0); if (ia_get(array, i) < 0) { return true; } } return false; }
void reportElement_toFile(FILE* f,FILE* reso, bool J2, double theta, Particle pa) { double q = hypot(hypot(pa.qx, pa.qy), pa.qz); double p = hypot(hypot(pa.px, pa.py), pa.pz); double a = a_length(q, p); double Energy = 0; if (J2) { Energy = calcE_J2(q, p, pa); } else { Energy = calcE(q, p); } double h = h_moment(pa); double e = e_anomaly(a, h); double I = I_anomaly(h, pa); double OMEGA = OMEGA_anomaly(I, h, pa); fprintf(f, "%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\n", pa.qx, pa.qy, pa.qz, Energy, a, h, e, I, theta); fprintf(reso, "%f %f\n", a*(pa.qx/q*cos(theta)+pa.qy/q*sin(theta)), a*(pa.qy/q*cos(theta)-pa.qx/q*sin(theta))); }
int main(void) { // every_other_test(); // return 0; Array temps = ia_of_string( "7, 6, 5, 6, 6, 5, 6, 6, 6, 7, 7, 9, 10," "11, 11, 12, 13, 12, 10, 9, 9, 8, 7, 4"); ia_println(temps); printiln(a_length(temps)); printiln(ia_get(temps, 0)); printiln(ia_get(temps, 23)); printsln("contains_negative_test:"); contains_negative_test(); printsln("every_other_test:"); every_other_test(); a_free(temps); return 0; }
static void a_sub_test(void) { printsln((String)__func__); Array a, ac, ex; IntPair ip; a = a_create(3, sizeof(IntPair)); ip = make_int_pair(1, 10); a_set(a, 0, &ip); ip = make_int_pair(2, 20); a_set(a, 1, &ip); ip = make_int_pair(3, 30); a_set(a, 2, &ip); a_println(a, print_int_pair); ex = a_create(2, sizeof(IntPair)); ip = make_int_pair(1, 10); a_set(ex, 0, &ip); ip = make_int_pair(2, 20); a_set(ex, 1, &ip); ac = a_sub(a, 0, 2); a_check_expect(ac, ex); ip = make_int_pair(2, 20); a_set(ex, 0, &ip); ip = make_int_pair(3, 30); a_set(ex, 1, &ip); a_free(ac); ac = a_sub(a, 1, 3); a_check_expect(ac, ex); a_free(a); a_free(ac); a_free(ex); Array array, array2, sub; array = ia_of_string("1 2 3 4"); sub = a_sub(array, 0, a_length(array)); ia_check_expect(array, sub); a_free(sub); sub = a_sub(array, -1, a_length(array) + 1); ia_check_expect(array, sub); a_free(sub); array2 = ia_of_string("2 3 4"); sub = a_sub(array, 1, a_length(array)); ia_check_expect(sub, array2); a_free(array2); a_free(sub); array2 = ia_of_string("2 3"); sub = a_sub(array, 1, a_length(array) - 1); ia_check_expect(sub, array2); a_free(array2); a_free(sub); array2 = ia_of_string(""); sub = a_sub(array, 1, 1); ia_check_expect(sub, array2); a_free(sub); sub = a_sub(array, 2, 1); ia_check_expect(sub, array2); a_free(array2); a_free(sub); a_free(array); array = da_range(1, 10, 1); sub = a_sub(array, 0, array->n); da_check_within(sub, array); a_free(sub); sub = a_sub(array, -10, array->n + 10); da_check_within(sub, array); a_free(sub); array2 = da_range(2, 10, 1); sub = a_sub(array, 1, array->n); da_check_within(sub, array2); a_free(array2); a_free(sub); array2 = da_range(2, 9, 1); sub = a_sub(array, 1, array->n - 1); da_check_within(sub, array2); a_free(array2); a_free(sub); array2 = da_range(0, 0, 1); sub = a_sub(array, 1, 1); da_check_within(sub, array2); a_free(sub); sub = a_sub(array, 2, 1); da_check_within(sub, array2); a_free(array2); a_free(sub); array2 = da_range(3, 6, 1); sub = a_sub(array, 2, 5); da_check_within(sub, array2); a_free(array2); a_free(sub); a_free(array); }
int main() { Particle particle; std::ofstream ofs; ofs.open( "kep" ); for (int i=0;i<1000000;i++) { double x = particle.qx; double y = particle.qy; // for (int s=0;i<sympI;i++) { symp2(&particle, dT); /* particle.px+=calc(x, y) *symp_u[i]* dT; particle.py+=calc(y, x) *symp_u[i]* dT; particle.qx+=particle.px/M*symp_k[i]* dT; particle.qy+=particle.py/M*symp_k[i]* dT; */ // } // std::cout << "px = " << particle.px << ", py = " << particle.py << std::endl; // std::cout << "qx = " << particle.qx << ", px = " << particle.px << std::endl; if (i%500==0) { ofs << particle.qx << " " << particle.qy << " " << 0.0 << " " << calcE(particle) << " " << a_length(particle) << std::endl; } } ofs.close(); }