int main() { std::tr1::array<int,2> a1 = {}; check_array(a1); BOOST_STATIC_ASSERT((std::tr1::tuple_size<std::tr1::array<int,2> >::value == 2)); std::tr1::array<std::string,4> a2 = { "abc", "def", "", "y", }; check_array(a2); BOOST_STATIC_ASSERT((std::tr1::tuple_size<std::tr1::array<std::string,4> >::value == 4)); return 0; }
void stp_array_copy(stp_array_t *dest, const stp_array_t *source) { check_array(dest); check_array(source); dest->x_size = source->x_size; dest->y_size = source->y_size; if (dest->data) stp_sequence_destroy(dest->data); dest->data = stp_sequence_create_copy(source->data); }
// Fourier transform from x back to (complex) k: void XTable::transform(KTable& kt) const { check_array(); // Make a new copy of data array since measurement will overwrite: FFTW_Array<double> t_array = _array; fftw_plan plan = fftw_plan_dft_r2c_2d( _N,_N, t_array.get_fftw(), kt._array.get_fftw(), FFTW_ESTIMATE); #ifdef FFT_DEBUG if (plan==NULL) throw FFTInvalid(); #endif fftw_execute(plan); fftw_destroy_plan(plan); // Now scale the k spectrum and flip signs for x=0 in middle. double fac = _dx * _dx; size_t ind=0; for (int iy=0; iy<_N; iy++) { for (int ix=0; ix<=_N/2; ix++) { if ( (ix+iy)%2==0) kt._array[ind] *= fac; else kt._array[ind] *= -fac; ind++; } } kt._dk = 2*M_PI/(_N*_dx); }
// Transform to a single k point: std::complex<double> XTable::kval(double kx, double ky) const { check_array(); // Don't evaluate if k not in fundamental period kx*=_dx; ky*=_dx; #ifdef FFT_DEBUG if (std::abs(kx) > M_PI || std::abs(ky) > M_PI) throw FFTOutofRange("XTable::kval() args out of range"); #endif std::complex<double> I(0.,1.); std::complex<double> dxphase=std::exp(-I*kx); std::complex<double> dyphase=std::exp(-I*ky); std::complex<double> phase(1.,0.); std::complex<double> z; std::complex<double> sum=0.; const double* zptr=_array.get(); std::complex<double> yphase=std::exp(I*(ky*_N/2)); for (int iy=0; iy< _N; iy++) { phase = yphase; phase *= std::exp(I*(kx*_N/2)); for (int ix=0; ix< _N ; ix++) { sum += phase* (*(zptr++)); phase *= dxphase; } yphase *= dyphase; } sum *= _dx*_dx; return sum; }
static size_t check_array_1d(VALUE item, size_t size) { narray_t *na; size_t i, len; if (TYPE(item) == T_ARRAY) { len = RARRAY_LEN(item); if (size != len) { return 0; } for (i=0; i<len; i++) { if (!check_array(RARRAY_AREF(item,i))) { return 0; } } return 1; } if (RTEST(rb_obj_is_kind_of(item, cNArray))) { GetNArray(item,na); if (na->ndim == 1 && na->size == size) { return 1; } else { return 0; } } return 0; }
int *generate_random_subset(int Nmax, int Nmin, int *subset) { int i=0, num=0; if(Nmin > Nmax) { fprintf(stdout, "\nTrying to generate a subset %d larger than the dataset %d.\n",Nmin,Nmax); } else { srand ((int)time(NULL)); while (i <= Nmin) { num = rand()%Nmax; if(check_array(num,subset,i)==0) { subset[i]=num; i++; } } } return subset; }
int main() { Environment *env; pthread_t self; pthread_t child; int ret; self = pthread_self (); env = make_environment (); /* the parent thread uses sproc to create the new thread, passing a pointer to card as an argument */ ret = pthread_create (&child, NULL, entry, (void *) env); if (ret == -1) { perror ("pthread_create failed"); exit (-1); } loop_and_count (self, env); printf ("Parent done looping.\n"); ret = pthread_join (child, NULL); if (ret == -1) { perror ("pthread_join failed"); exit (-1); } check_array (env); return 0; }
// Integrate KTable over d^2k (sum of all pixels * dk * dk) std::complex<double> KTable::integratePixels() const { check_array(); std::complex<double> sum=0.; const std::complex<double>* zptr=_array.get(); // Do the positive y frequencies for (int iy=0; iy<= _N/2; iy++) { sum += *(zptr++); // x DC term for (int ix=1; ix< _N/2 ; ix++) { sum += *(zptr); sum += conj(*(zptr++)); } sum += *(zptr++); } // wrap to the negative ky's for (int iy=-_N/2+1; iy< 0; iy++) { sum += *(zptr++); // x DC term for (int ix=1; ix< _N/2 ; ix++) { sum += *(zptr); sum += conj(*(zptr++)); } sum += *(zptr++); } sum *= _dk*_dk; return sum; }
const stp_sequence_t * stp_array_get_sequence(const stp_array_t *array) { check_array(array); return array->data; }
void stp_array_destroy(stp_array_t *array) { check_array(array); array_dtor(array); stp_free(array); }
void KTable::operator*=(double scale) { clearCache(); // invalidate any stored interpolations check_array(); for (int i=0; i<_N*(_N/2+1); i++) _array[i] *= scale; }
void stp_array_set_data(stp_array_t *array, const double *data) { check_array(array); stp_sequence_set_data(array->data, array->x_size * array->y_size, data); }
void XTable::operator*=(double scale) { check_array(); clearCache(); // invalidate any stored interpolations for (int i=0; i<_N*_N; i++) _array[i] *= scale; }
// Make a new table that is function of old. boost::shared_ptr<KTable> KTable::function(KTable::function2 func) const { check_array(); boost::shared_ptr<KTable> lhs(new KTable(_N,_dk)); std::complex<double> val; double kx, ky; const std::complex<double>* zptr=_array.get(); std::complex<double>* lptr=lhs->_array.get(); // Do the positive y frequencies for (int iy=0; iy< _N/2; iy++) { ky = iy*_dk; for (int ix=0; ix<= _N/2 ; ix++) { kx = ix*_dk; val = *(zptr++); *(lptr++)= func(kx,ky,val); } } // wrap to the negative ky's for (int iy=-_N/2; iy< 0; iy++) { ky = iy*_dk; for (int ix=0; ix<= _N/2 ; ix++) { kx = ix*_dk; val = *(zptr++); *(lptr++)= func(kx,ky,val); } } return lhs; }
// add array A2_API inline void a2_addarray(struct a2_state* state){ int top = a2_top(state)-1; struct a2_obj* v = a2_getcstack(state->env_p, top); struct a2_obj* array = a2_getcstack(state->env_p, top-1); check_array(array); a2_array_add(a2_gcobj2array(obj_vX(array, obj)), v); }
void stp_array_get_size(const stp_array_t *array, int *x_size, int *y_size) { check_array(array); *x_size = array->x_size; *y_size = array->y_size; return; }
VALUE nst_check_compatibility(VALUE nst, VALUE ary) { VALUE defs, def, type, item; long len, i; narray_t *nt; if (TYPE(ary) != T_ARRAY) { if (nst==CLASS_OF(ary)) { // same Struct return Qtrue; } return Qfalse; } defs = nst_definitions(nst); len = RARRAY_LEN(defs); if (len != RARRAY_LEN(ary)) { //puts("pass2"); return Qfalse; } for (i=0; i<len; i++) { def = RARRAY_AREF(defs,i); type = RARRAY_AREF(def,1); GetNArray(type,nt); item = RARRAY_AREF(ary,i); if (nt->ndim == 0) { if (check_array(item)) { //puts("pass3"); return Qfalse; } } else if (nt->ndim == 1) { if (!check_array_1d(item, nt->size)) { //puts("pass4"); return Qfalse; } } else { // multi-dimension member volatile VALUE vnc; na_compose_t *nc; int j; //rb_p(item); vnc = na_ary_composition(item); //puts("pass2"); Data_Get_Struct(vnc, na_compose_t, nc); if (nt->ndim != nc->ndim) { return Qfalse; } for (j=0; j<nc->ndim; j++) { if (nc->shape[j] != nt->shape[j]) { return Qfalse; } } return Qtrue; } } return Qtrue; }
stp_array_t * stp_array_create_copy(const stp_array_t *array) { stp_array_t *ret; check_array(array); ret = stp_array_create(0, 0); /* gets freed next */ stp_array_copy(ret, array); return ret; }
void KTable::accumulate(const KTable& rhs, double scalar) { clearCache(); // invalidate any stored interpolations check_array(); if (_N != rhs._N) throw FFTError("KTable::accumulate() with mismatched sizes"); if (_dk != rhs._dk) throw FFTError("KTable::accumulate() with mismatched dk"); for (int i=0; i<_N*(_No2+1); ++i) _array[i] += scalar * rhs._array[i]; }
int stp_array_set_point(stp_array_t *array, int x, int y, double data) { check_array(array); if (((array->x_size * x) + y) >= (array->x_size * array->y_size)) return 0; return stp_sequence_set_point(array->data, (array->x_size * x) + y, data);}
void KTable::operator*=(const KTable& rhs) { clearCache(); // invalidate any stored interpolations check_array(); if (_N != rhs._N) throw FFTError("KTable::operator*=() with mismatched sizes"); if (_dk != rhs._dk) throw FFTError("KTable::operator*=() with mismatched dk"); for (int i=0; i<_N*(_No2+1); ++i) _array[i] *= rhs._array[i]; }
void XTable::accumulate(const XTable& rhs, double scalar) { check_array(); clearCache(); // invalidate any stored interpolations #ifdef FFT_DEBUG if (_N != rhs._N) throw FFTError("XTable::accumulate() with mismatched sizes"); #endif for (int i=0; i<_N*_N; i++) _array[i] += scalar * rhs._array[i]; }
void predexp_node_2_as_predexp(as_predexp_array *a, VALUE node) { check_hash(node); // node = { // bin: bin, // true: true, // filters: { // filter: :eq, // value: 1 // }, // and: [], // or: [] // } VALUE node_bin = rb_hash_aref(node, bin_sym); VALUE node_true = rb_hash_aref(node, predexp_true_sym); VALUE node_filters = rb_hash_aref(node, predexp_filters_sym); push_2_as_predexp(a, node_bin, node_true, node_filters); VALUE node_and = rb_hash_aref(node, predexp_and_sym); check_array(node_and); int length = rb_ary_len_int(node_and); if (length > 0) { for(int i = 0; i < length; i++){ VALUE node_and_obj = rb_ary_entry(node_and, i); predexp_node_2_as_predexp(a, node_and_obj); } insert_as_predexp_array(a, as_predexp_and(length + 1)); } VALUE node_or = rb_hash_aref(node, predexp_or_sym); check_array(node_or); length = rb_ary_len_int(node_or); if (length > 0) { for(int i = 0; i < length; i++){ VALUE node_or_obj = rb_ary_entry(node_or, i); predexp_node_2_as_predexp(a, node_or_obj); } insert_as_predexp_array(a, as_predexp_or(length + 1)); } }
// Transform to a single x point: // assumes (x,y) in physical units double KTable::xval(double x, double y) const { check_array(); x*=_dk; y*=_dk; // Don't evaluate if x not in fundamental period +-PI/dk: #ifdef FFT_DEBUG if (std::abs(x) > M_PI || std::abs(y) > M_PI) throw FFTOutofRange(" (x,y) too big in xval()"); #endif std::complex<double> I(0.,1.); std::complex<double> dxphase=std::exp(I*x); std::complex<double> dyphase=std::exp(I*y); std::complex<double> phase(1.,0.); std::complex<double> z; double sum=0.; // y DC terms first: const std::complex<double>* zptr=_array.get(); // Do the positive y frequencies std::complex<double> yphase=1.; for (int iy=0; iy< _N/2; iy++) { phase = yphase; z= *(zptr++); sum += (phase*z).real(); //x DC term for (int ix=1; ix< _N/2 ; ix++) { phase *= dxphase; z= *(zptr++); sum += (phase*z).real() * 2.; } phase *= dxphase; //ix=N/2 has no mirror: z= *(zptr++); sum += (phase*z).real(); yphase *= dyphase; } // wrap to the negative ky's yphase = std::exp(I*(y*(-_N/2))); for (int iy=-_N/2; iy< 0; iy++) { phase = yphase; z= *(zptr++); sum += (phase*z).real(); // x DC term for (int ix=1; ix< _N/2 ; ix++) { phase *= dxphase; z= *(zptr++); sum += (phase*z).real() * 2.; } phase *= dxphase; //ix=N/2 has no mirror: z= *(zptr++); sum += (phase*z).real(); yphase *= dyphase; } sum *= _dk*_dk/(4.*M_PI*M_PI); //inverse xform has 2pi in it. return sum; }
void stp_array_set_size(stp_array_t *array, int x_size, int y_size) { check_array(array); if (array->data) /* Free old data */ stp_sequence_destroy(array->data); array->x_size = x_size; array->y_size = y_size; array->data = stp_sequence_create(); stp_sequence_set_size(array->data, array->x_size * array->y_size); }
CAMLprim value ml_gsl_wavelet_transform (value w, value dir, value vf, value ws) { double *data = Double_array_val (Field (vf, 0)) + Long_val (Field (vf, 1)); size_t n = Long_val (Field (vf, 2)); size_t stride = Long_val (Field (vf, 3)); check_array (vf); gsl_wavelet_transform (Wavelet_val (w), data, stride, n, gsl_direction_val (dir), WS_val (ws)); return Val_unit; }
void KTable::kSet(int ix, int iy, std::complex<double> value) { check_array(); clearCache(); // invalidate any stored interpolations if (ix<0) { _array[index(ix,iy)]=conj(value); if (ix==-_N/2) _array[index(ix,-iy)]=value; } else { _array[index(ix,iy)]=value; if (ix==0 || ix==_N/2) _array[index(ix,-iy)]=conj(value); } }
static int array_new_indexed_plist_write(dict_param_list * plist, ref * parray, const ref * pwanted, gs_ref_memory_t *imem) { check_array(*parray); check_write(*parray); plist->u.w.write = array_new_indexed_param_write; ref_param_write_init((iparam_list *) plist, pwanted, imem); plist->dict = *parray; plist->int_keys = true; return 0; }
double XTable::integratePixels() const { check_array(); double sum=0.; const double* zptr=_array.get(); for (int iy=-_N/2; iy< _N/2; iy++) for (int ix=-_N/2; ix< _N/2; ix++) { sum += *(zptr++); } sum *= _dx*_dx; return (double) sum; }
// get array A2_API inline void a2_getarray(struct a2_state* state){ int top = a2_top(state)-1; struct a2_obj* k = a2_getcstack(state->env_p, top); struct a2_obj* array = a2_getcstack(state->env_p, top-1); struct a2_obj* _v = NULL; check_array(array); check_num(k); _v = a2_array_get(a2_gcobj2array(obj_vX(array, obj)), k); if(_v==NULL) obj_setX(k, A2_TNIL, point, NULL); else *k = *_v; }