GimpParamDef * rb2GimpParamDefs (VALUE rbparamdefs, gint *count) { if (rbparamdefs == Qnil) { *count = 0; return NULL; } else { Check_Type(rbparamdefs, T_ARRAY); int num = RARRAY_LEN(RARRAY(rbparamdefs)); VALUE *arr = RARRAY_PTR(RARRAY(rbparamdefs)); GimpParamDef *gimpparamdefs = g_new(GimpParamDef, num); int i; for(i=0; i<num; i++) gimpparamdefs[i] = rb2GimpParamDef(arr[i]); *count = (gint)num; return gimpparamdefs; } }
/* * call-seq: * ary <=> other_ary -> -1, 0, +1 or nil * * Comparison---Returns an integer (-1, 0, or +1) * if this array is less than, equal to, or greater than <i>other_ary</i>. * Each object in each array is compared (using <=>). If any value isn't * equal, then that inequality is the return value. If all the * values found are equal, then the return is based on a * comparison of the array lengths. Thus, two arrays are * ``equal'' according to <code>Array#<=></code> if and only if they have * the same length and the value of each element is equal to the * value of the corresponding element in the other array. * * [ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1 * [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1 * */ mrb_value mrb_ary_cmp(mrb_state *mrb, mrb_value ary1) { mrb_value ary2; struct RArray *a1, *a2; mrb_value r = mrb_nil_value(); int i, len; mrb_get_args(mrb, "o", &ary2); if (mrb_type(ary2) != MRB_TT_ARRAY) return mrb_nil_value(); a1 = RARRAY(ary1); a2 = RARRAY(ary2); if (a1->len == a2->len && a1->ptr == a2->ptr) return mrb_fixnum_value(0); else { len = RARRAY_LEN(ary1); if (len > RARRAY_LEN(ary2)) { len = RARRAY_LEN(ary2); } for (i=0; i<len; i++) { r = mrb_funcall(mrb, ary_elt(ary1, i), "<=>", 1, ary_elt(ary2, i)); if (mrb_type(r) != MRB_TT_FIXNUM || mrb_fixnum(r) != 0) return r; } } len = a1->len - a2->len; return mrb_fixnum_value((len == 0)? 0: (len > 0)? 1: -1); }
/* * Sets X509_EXTENSIONs */ static VALUE ossl_x509crl_set_extensions(VALUE self, VALUE ary) { X509_CRL *crl; X509_EXTENSION *ext; int i; Check_Type(ary, T_ARRAY); /* All ary members should be X509 Extensions */ for (i=0; i<RARRAY(ary)->len; i++) { OSSL_Check_Kind(RARRAY(ary)->ptr[i], cX509Ext); } GetX509CRL(self, crl); sk_X509_EXTENSION_pop_free(crl->crl->extensions, X509_EXTENSION_free); crl->crl->extensions = NULL; for (i=0; i<RARRAY(ary)->len; i++) { ext = DupX509ExtPtr(RARRAY(ary)->ptr[i]); if(!X509_CRL_add_ext(crl, ext, -1)) { /* DUPs ext - FREE it */ X509_EXTENSION_free(ext); ossl_raise(eX509CRLError, NULL); } X509_EXTENSION_free(ext); } return ary; }
static VALUE rbosa_elementlist_new (int argc, VALUE *argv, VALUE self) { OSErr error; AEDescList list; VALUE ary; int i; rb_scan_args (argc, argv, "01", &ary); if (!NIL_P (ary)) Check_Type (ary, T_ARRAY); error = AECreateList (NULL, 0, false, &list); if (error != noErr) rb_raise (rb_eRuntimeError, "Cannot create Apple Event descriptor list : %s (%d)", error_code_to_string (error), error); if (!NIL_P (ary)) { for (i = 0; i < RARRAY (ary)->len; i++) __rbosa_elementlist_add (&list, RARRAY (ary)->ptr[i], i + 1); } return rbosa_element_make (self, &list, Qnil); }
static VALUE ossl_x509crl_set_revoked(VALUE self, VALUE ary) { X509_CRL *crl; X509_REVOKED *rev; int i; Check_Type(ary, T_ARRAY); /* All ary members should be X509 Revoked */ for (i=0; i<RARRAY(ary)->len; i++) { OSSL_Check_Kind(RARRAY(ary)->ptr[i], cX509Rev); } GetX509CRL(self, crl); sk_X509_REVOKED_pop_free(crl->crl->revoked, X509_REVOKED_free); crl->crl->revoked = NULL; for (i=0; i<RARRAY(ary)->len; i++) { rev = DupX509RevokedPtr(RARRAY(ary)->ptr[i]); if (!X509_CRL_add0_revoked(crl, rev)) { /* NO DUP - don't free! */ ossl_raise(eX509CRLError, NULL); } } X509_CRL_sort(crl); return ary; }
VALUE munge_xpath_namespace( VALUE orig_expr, xmlChar *root_ns ) { VALUE path_bits = rb_str_split( orig_expr, "/" ); VALUE ns_prefix = rb_str_new2( (const char*)root_ns ); VALUE ns_indic = rb_str_new2( ":" ); VALUE slash = rb_str_new2( "/" ); VALUE path_bit, str_idx; VALUE ret_ary = rb_ary_new(); long i; rb_str_append( ns_prefix, ns_indic ); for (i=0; i<RARRAY(path_bits)->len; i++) { path_bit = RARRAY(path_bits)->ptr[i]; if (RSTRING_LEN(path_bit) > 0) { str_idx = rb_funcall( path_bit, rb_intern( "index" ), 1, ns_indic ); if (str_idx == Qnil || str_idx == Qfalse) // didn't find the :, so it looks like we don't have a namespace path_bit = rb_str_plus( ns_prefix, path_bit ); } rb_ary_push( ret_ary, path_bit ); } return rb_ary_join( ret_ary, slash ); }
static VALUE rb_gsl_blas_drotm2(VALUE obj, VALUE xx, VALUE yy, VALUE PP) { gsl_vector *x = NULL, *y = NULL, *p = NULL, *xnew = NULL, *ynew = NULL; int flag = 0, i; CHECK_VECTOR(xx); CHECK_VECTOR(yy); Data_Get_Struct(xx, gsl_vector, x); Data_Get_Struct(yy, gsl_vector, y); if (rb_obj_is_kind_of(PP, cgsl_vector)) { Data_Get_Struct(PP, gsl_vector, p); } else { if (TYPE(PP) != T_ARRAY) rb_raise(rb_eTypeError, "wrong argument type %s (Array of Vector expected", rb_class2name(CLASS_OF(PP))); p = gsl_vector_alloc(RARRAY(PP)->len); for (i = 0; i < RARRAY(PP)->len; i++) { gsl_vector_set(p, i, rb_ary_entry(PP, i)); } flag = 1; } xnew = gsl_vector_alloc(x->size); ynew = gsl_vector_alloc(y->size); gsl_vector_memcpy(xnew, x); gsl_vector_memcpy(ynew, y); gsl_blas_drotm(xnew, ynew, p->data); if (flag == 1) gsl_vector_free(p); return rb_ary_new3(2, Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, xnew), Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, ynew)); }
static VALUE dnssd_tr_encode(VALUE self) { long i; VALUE buf; /* Declare ary volatile to prevent it from being reclaimed when: * buf is allocated later, key/values are converted to strings */ volatile VALUE ary = rb_funcall2(self, rb_intern("to_a"), 0, 0); /* array of key, value pairs */ VALUE *ptr = RARRAY(ary)->ptr; buf = rb_str_buf_new(dnssd_tr_convert_pairs(ary)); for(i=0; i<RARRAY(ary)->len; i++) { uint8_t len; VALUE key = RARRAY(ptr[i])->ptr[0]; VALUE value = RARRAY(ptr[i])->ptr[1]; if (!NIL_P(value)) { len = (uint8_t)(RSTRING(key)->len + RSTRING(value)->len + 1); rb_str_buf_cat(buf, &len, 1); rb_str_buf_append(buf, key); rb_str_buf_cat(buf, "=", 1); rb_str_buf_append(buf, value); } else { len = (uint8_t)RSTRING(key)->len; rb_str_buf_cat(buf, &len, 1); rb_str_buf_append(buf, key); } } return buf; }
/* * Document-method: offset * * call-seq: * mtch.offset(n) => array * mtch.offset => array * mtch.offset(symbol) => array * * Returns a two-element array containing the beginning and ending offsets of * the <em>n</em>th match. * * m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.") * m.offset(0) #=> [1, 7] * m.offset(4) #=> [6, 7] * * If no arguments are given, the offsets of the entire * sequence are returned. * * m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.") * m.offset #=> [1, 7] * * If the argument is a symbol, then the offsets of the * corresponding named group are returned, or <code>nil</code> * if the group does not exist. * * m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138") * m.end(:middle) #=> [3, 4] */ static VALUE og_oniguruma_match_offset(int argc, VALUE *argv, VALUE self) { VALUE idx, first, k, nargv[2]; rb_scan_args(argc, argv, "0*", &idx); first = rb_ary_entry(idx, 0); if (SYMBOL_P(first)) { k = og_oniguruma_match_to_index(self, first); if (!NIL_P(k)) { nargv[0] = k; nargv[1] = (VALUE)NULL; return rb_funcall3(self, rb_intern("offset_without_oniguruma"), 1, nargv); } else return Qnil; } else if (RARRAY(idx)->len == 0) { nargv[0] = INT2FIX(0); nargv[1] = (VALUE)NULL; return rb_funcall3(self, rb_intern("offset_without_oniguruma"), 1, nargv); } return rb_funcall3(self, rb_intern("offset_without_oniguruma"), RARRAY(idx)->len, RARRAY(idx)->ptr); }
static value_t* ruby_to_value(VALUE v) { switch (TYPE(v)) { case T_NIL: return value_new_void(); case T_BIGNUM: case T_FIXNUM: return value_new_int32(NUM2INT(v)); case T_TRUE: return value_new_boolean(true); case T_FALSE: return value_new_boolean(false); case T_FLOAT: return value_new_float32(NUM2DBL(v)); case T_SYMBOL: return value_new_string(rb_id2name(SYM2ID(v))); case T_STRING: return value_new_string(StringValuePtr(v)); case T_ARRAY: { /* process Array */ value_t*array = array_new(); int len = RARRAY(v)->len; int i; for(i=0;i<len;i++) { volatile VALUE item = RARRAY(v)->ptr[i]; array_append(array, ruby_to_value(item)); } return array; } default: /* raise exception */ rb_raise(rb_eTypeError, "not valid value"); } }
static VALUE compare_arrays(VALUE a, VALUE b, fun_array_cmp cmp) { struct array_comparison result = { 0, 0, RARRAY(a)->len, RARRAY(b)->len }; long * long_a; long * long_b; int i, j; result.union_size = result.a_size + result.b_size; if((result.a_size > 0) && (result.b_size > 0)) { COPYRUBYHASHARRAY(a, long_a); COPYRUBYHASHARRAY(b, long_b); for(i = 0; i < result.a_size; ++i) { for(j = 0; j < result.b_size; ++j) { if(long_a[i] == long_b[j]) { result.intersection_size++; } } } } return rb_float_new((*cmp)(result)); }
static void parse(pairmatcher_t *pm, VALUE tokenizer, VALUE reporter) { VALUE token_info; while ((token_info = get_token(tokenizer)) != Qnil) { VALUE token_type, token_text, token_lineno, token_byteno; VALUE token; Check_Type(token_info, T_ARRAY); if (RARRAY(token_info)->len != 8) { rb_raise(rb_eArgError, "unexpected token"); } token_type = RARRAY(token_info)->ptr[0]; token_text = RARRAY(token_info)->ptr[1]; token_lineno = RARRAY(token_info)->ptr[2]; token_byteno = RARRAY(token_info)->ptr[4]; token = rb_funcall(Fragment, id_new, 4, token_type, token_text, token_lineno, token_byteno); if (intertoken_p(pm, token_type)) { rb_funcall(reporter, id_call, 1, token); } else { put_token(pm, token, reporter); } } finish(pm, reporter); }
static struct cov_array * coverage_increase_counter_uncached(char *sourcefile, unsigned int sourceline, char mark_only) { struct cov_array *carray = NULL; if(sourcefile == NULL) { /* "can't happen", just ignore and avoid segfault */ return NULL; } else if(!st_lookup(coverinfo, (st_data_t)sourcefile, (st_data_t*)&carray)) { VALUE arr; arr = rb_hash_aref(oSCRIPT_LINES__, rb_str_new2(sourcefile)); if(NIL_P(arr)) return 0; rb_check_type(arr, T_ARRAY); carray = calloc(1, sizeof(struct cov_array)); carray->ptr = calloc(RARRAY(arr)->len, sizeof(unsigned int)); carray->len = RARRAY(arr)->len; st_insert(coverinfo, (st_data_t)strdup(sourcefile), (st_data_t) carray); } else { /* recovered carray, sanity check */ assert(carray && "failed to create valid carray"); } if(mark_only) { if(!carray->ptr[sourceline]) carray->ptr[sourceline] = 1; } else { if (carray && carray->len > sourceline) { carray->ptr[sourceline]++; } } return carray; }
octave_value OR_StructMatrix::to_octave() { int i, row_index, column_index; VALUE row, cell; VALUE cells = rb_iv_get(ruby_val, "@cells"); VALUE names = rb_iv_get(ruby_val, "@names"); int number_of_keys = RARRAY(names)->len; int number_of_rows = FIX2INT(rb_iv_get(ruby_val, "@m")); int number_of_columns = FIX2INT(rb_iv_get(ruby_val, "@n")); string_vector keys = string_vector(); for (i = 0; i < number_of_keys; i++) { keys.append(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr)); } Octave_map struct_matrix = Octave_map(dim_vector(number_of_rows, number_of_columns), Cell(keys)); for (row_index = 0; row_index < number_of_rows; row_index++) { row = RARRAY(cells)->ptr[row_index]; for (column_index = 0; column_index < number_of_columns; column_index++) { cell = RARRAY(row)->ptr[column_index]; for (i = 0; i < number_of_keys; i++) { struct_matrix.contents(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr))(row_index, column_index) = OR_Variable(rb_hash_aref(cell, rb_str_new2(RSTRING(RARRAY(names)->ptr[i])->ptr))).to_octave(); } } } return struct_matrix; }
/** @method union_list( array_of_rects ) Returns a new array representing a rectangle covering all rectangles in @array_of_rects. */ static VALUE rb_array_union_list(VALUE self, VALUE other_rects) { int i; double left; double right; double top; double bottom; double l,r,t,b; VALUE rect; if(RARRAY(other_rects)->len==0){ return Qnil; } rect=rb_ary_entry(other_rects, 0); left=array_get_x(rect); right=array_get_w(rect)+left; top=array_get_y(rect); bottom=array_get_h(rect)+top; for(i=1; i<RARRAY(other_rects)->len; i++){ rect=rb_ary_entry(other_rects, i); l=array_get_x(rect); r=array_get_w(rect)+l; t=array_get_y(rect); b=array_get_h(rect)+t; left=RUDL_MIN(left, l); right=RUDL_MAX(right, r); top=RUDL_MIN(top, t); bottom=RUDL_MAX(bottom, b); } return new_rect(left, top, right-left, bottom-top); }
/* call-seq: _set_params(pars,vars,set_derivs) * * Sets <tt>@params</tt> using MINUIT parameters _pars_ and kinematic * variables _vars_ (if <tt>:dcs</tt>). If _set_derivs_ is <tt>true</tt>, then * <tt>@dparams</tt> is set also. */ VALUE rb_dataset_set_params(VALUE __self,VALUE __pars,VALUE __vars, VALUE __set_derivs){ static ID set_pars_id = rb_intern("set_pars"); static ID value_id = rb_intern("value"); static ID deriv_id = rb_intern("deriv"); VALUE amps = rb_iv_get(__self,"@amps"); VectorDbl2D *params = get_cpp_ptr(rb_iv_get(__self,"@params"),__VectorDbl2D__); VectorDbl3D *dparams = get_cpp_ptr(rb_iv_get(__self,"@dparams"),__VectorDbl3D__); int num_ic = RARRAY(amps)->len,num_pars = RARRAY(__pars)->len; for(int ic = 0; ic < num_ic; ic++){ // loop over incoherent wavesets VALUE ic_amps = rb_ary_entry(amps,ic); int num_amps = RARRAY(ic_amps)->len; for(int a = 0; a < num_amps; a++){ // loop over amps in this waveset VALUE amp = rb_ary_entry(ic_amps,a); // current amp if(rb_iv_get(amp,"@use") == Qfalse){ (*params)[ic][a] = 0.0; for(int par = 0; par < num_pars; par++) (*dparams)[ic][a][par] = 0.; continue; } rb_funcall(amp,set_pars_id,1,__pars); // call Amp#set_pars on it (*params)[ic][a] = CPP_COMPLEX(float,rb_funcall(amp,value_id,1,__vars)); if(__set_derivs == Qfalse) continue; for(int par = 0; par < num_pars; par++){ // loop over parameters if(rb_ary_entry(__pars,par) == Qnil) continue; // amp doesn't use par (*dparams)[ic][a][par] = CPP_COMPLEX(double,rb_funcall(amp,deriv_id,2,INT2NUM(par),__vars)); } } } return __self; }
static HashSet * frt_get_fields(VALUE rfields) { VALUE rval; HashSet *fields; char *s, *p, *str; if (rfields == Qnil) return NULL; fields = hs_new_str(&free); if (TYPE(rfields) == T_ARRAY) { int i; for (i = 0; i < RARRAY(rfields)->len; i++) { rval = rb_obj_as_string(RARRAY(rfields)->ptr[i]); hs_add(fields, estrdup(RSTRING(rval)->ptr)); } } else { rval = rb_obj_as_string(rfields); if (strcmp("*", RSTRING(rval)->ptr) == 0) { hs_destroy(fields); fields = NULL; } else { s = str = estrdup(RSTRING(rval)->ptr); while ((p = strchr(s, '|')) != '\0') { *p = '\0'; hs_add(fields, estrdup(s)); s = p + 1; } hs_add(fields, estrdup(s)); free(str); } } return fields; }
// Used to close io handle static VALUE io_close(VALUE val) { int i; for(i=0;i<3;i++) { if(rb_funcall(RARRAY(val)->ptr[i], rb_intern("closed?"),0)==Qfalse) rb_funcall(RARRAY(val)->ptr[i], rb_intern("close"),0); } return Qnil; }
static VALUE array_spec_RARRAY_ptr_assign(VALUE self, VALUE ary, VALUE content, VALUE length) { int i; for (i = 0; i < NUM2INT(length); i++) { RARRAY(ary)->ptr[i] = content; } RARRAY(ary)->len = i; return ary; }
/* call-seq: fcn_val(flag,pars,derivs) -> chi2 * * Calculates chi2 given MINUIT parameters _pars_. If _flag_ is 2, derivatives * are calculated and set in _derivs_. */ static VALUE rb_dcs_fcn_val(VALUE __self,VALUE __flag,VALUE __pars, VALUE __derivs){ static ID cs_id = rb_intern("cs"); static ID cs_err_id = rb_intern("cs_err"); static ID vars_id = rb_intern("vars"); int num_pars = RARRAY(__pars)->len; // length of MINUIT parameter array double chi2 = 0.0; double phsp = NUM2DBL(rb_iv_get(__self,"@phsp_factor")); VALUE dcs_pts = rb_iv_get(__self,"@dcs_pts"); VALUE do_derivs = (NUM2INT(__flag) == 2) ? Qtrue : Qfalse; int num_pts = RARRAY(dcs_pts)->len; VectorFlt3D *amp_vals = get_cpp_ptr(rb_iv_get(__self,"@amp_vals"),__VectorFlt3D__); VectorDbl2D *params = get_cpp_ptr(rb_iv_get(__self,"@params"),__VectorDbl2D__); VectorDbl3D *dparams = get_cpp_ptr(rb_iv_get(__self,"@dparams"),__VectorDbl3D__); double dchi2dpar[num_pars]; for(int p = 0; p < num_pars; p++) dchi2dpar[p] = 0.; for(int pt = 0; pt < num_pts; pt++){ // loop over dsigma pts VALUE dcs_pt = rb_ary_entry(dcs_pts,pt); double cs = NUM2DBL(rb_funcall(dcs_pt,cs_id,0)); double cs_err = NUM2DBL(rb_funcall(dcs_pt,cs_err_id,0)); rb_dataset_set_params(__self,__pars,rb_funcall(dcs_pt,vars_id,0), do_derivs); int num_ic = (int)(*amp_vals)[pt].size(); double intensity = 0.0; complex<double> dcsdpar[num_pars]; for(int p = 0; p < num_pars; p++) dcsdpar[p] = 0.; for(int ic = 0; ic < num_ic; ic++){ // loop over incoherent wavesets complex<double> amp_tot = get_amp_total((*amp_vals)[pt][ic],(*params)[ic]); intensity += (amp_tot*conj(amp_tot)).real(); if(do_derivs == Qfalse) continue; int num_amps = (int)(*amp_vals)[pt][ic].size(); for(int a = 0; a < num_amps; a++){ // loop over amps in this waveset complex<double> amp_prod = (*amp_vals)[pt][ic][a]*conj(amp_tot); for(int p = 0; p < num_pars; p++) dcsdpar[p] += (*dparams)[ic][a][p]*amp_prod; } } double cs_calc = phsp*intensity; double diff = cs - cs_calc; chi2 += diff*diff/(cs_err*cs_err); // add this pt's chi^2 to total if(do_derivs == Qtrue){ for(int p = 1; p < num_pars; p++){ complex<double> dsdp = dcsdpar[p]*phsp; dchi2dpar[p] += 2*((2/(cs_err*cs_err))*(cs_calc - cs)*dsdp).real(); } for(int p = 1; p < num_pars; p++){ // set the derivatives if(rb_ary_entry(__derivs,p) != Qnil){ rb_ary_store(__derivs,p,rb_float_new(dchi2dpar[p])); } } } } return rb_float_new(chi2); }
VALUE rb_struct_members(VALUE s) { VALUE members = rb_struct_s_members(rb_obj_class(s)); if (RSTRUCT(s)->len != RARRAY(members)->len) { rb_raise(rb_eTypeError, "struct size differs (%d required %d given)", RARRAY(members)->len, RSTRUCT(s)->len); } return members; }
static int matching_open_depth(pairmatcher_t *pm, VALUE open_token, VALUE pair_def) { int i; for (i = RARRAY(pm->pair_stack)->len - 1; 0 <= i; i--) { if (pair_get_pair_def(RARRAY(pm->pair_stack)->ptr[i]) == pair_def) { return i; } } return -1; }
VALUE intersys_query_bind_params(VALUE self, VALUE params) { int i; struct rbQuery* query; Check_Type(params, T_ARRAY); Data_Get_Struct(self, struct rbQuery, query); for(i = 0; i < RARRAY(params)->len; i++) { query_bind_one_param(query->query, i, RARRAY(params)->ptr[i]); } return self; }
/* call-seq: calc_dcs(pars) -> Array * * Returns the Array of calculated differential cross section points using * amps w/ <tt>amp.use == true</tt>. */ VALUE rb_dcs_calc_dcs(VALUE __self,VALUE __pars,VALUE __cov_matrix){ static ID vars_id = rb_intern("vars"); VALUE dcs_pts = rb_iv_get(__self,"@dcs_pts"); double phsp = NUM2DBL(rb_iv_get(__self,"@phsp_factor")); int num_pts = RARRAY(dcs_pts)->len; int num_pars = RARRAY(__pars)->len; // length of MINUIT parameter array double dIdpar[num_pars]; VALUE dcs = rb_ary_new2(num_pts); VALUE dcs_error = rb_ary_new2(num_pts); VectorFlt3D *amp_vals = get_cpp_ptr(rb_iv_get(__self,"@amp_vals"),__VectorFlt3D__); VectorDbl2D *params = get_cpp_ptr(rb_iv_get(__self,"@params"),__VectorDbl2D__); VectorDbl3D *dparams = get_cpp_ptr(rb_iv_get(__self,"@dparams"),__VectorDbl3D__); VALUE rb_cov_ary = rb_funcall(__cov_matrix,rb_intern("to_a"),0); double cov_matrix[num_pars][num_pars]; for(int i = 0; i < num_pars; i++){ for(int j = 0; j < num_pars; j++){ cov_matrix[i][j] = NUM2DBL(rb_ary_entry(rb_ary_entry(rb_cov_ary,i),j)); } } for(int pt = 0; pt < num_pts; pt++){ // loop over dsigma pts VALUE dcs_pt = rb_ary_entry(dcs_pts,pt); rb_dataset_set_params(__self,__pars,rb_funcall(dcs_pt,vars_id,0),Qtrue); int num_ic = (int)(*amp_vals)[pt].size(); double intensity = 0.0; for(int p = 0; p < num_pars; p++) dIdpar[p] = 0.; for(int ic = 0; ic < num_ic; ic++){ // loop over incoherent wavesets complex<double> amp_tot = get_amp_total((*amp_vals)[pt][ic],(*params)[ic]); intensity += (amp_tot*conj(amp_tot)).real(); int num_amps = (int)(*amp_vals)[pt][ic].size(); for(int a = 0; a < num_amps; a++){ // loop over amps in this waveset complex<double> amp_prod = (*amp_vals)[pt][ic][a]*conj(amp_tot); for(int p = 0; p < num_pars; p++) dIdpar[p] += 2*((*dparams)[ic][a][p]*amp_prod).real(); } } double error2 = 0; for(int i = 0; i < num_pars; i++){ for(int j = 0; j < num_pars; j++){ error2 += dIdpar[i]*cov_matrix[i][j]*dIdpar[j]; } } rb_ary_store(dcs,pt,rb_float_new(phsp*intensity)); rb_ary_store(dcs_error,pt,rb_float_new(phsp*sqrt(error2))); } VALUE ret_ary = rb_ary_new2(2); rb_ary_store(ret_ary,0,dcs); rb_ary_store(ret_ary,1,dcs_error); return ret_ary; }
VALUE ary_copy(VALUE dst, VALUE src) { int i; Check_Type(src, T_ARRAY); Check_Type(dst, T_ARRAY); for (i = 0; i < RARRAY(src)->len; i++){ ary_push(dst, RARRAY(src)->ptr[i]); } return dst; }
VALUE concat_func(VALUE args) { int i; char buffer[500] = {0}; for(i = 0; i < RARRAY(args)->len; ++i) { VALUE v = RARRAY(args)->ptr[i]; strcat(buffer, StringValuePtr(v)); strcat(buffer, "_"); } buffer[strlen(buffer) - 1] = 0; return rb_str_new2(buffer); }
// ---------------------------------------------------------------------------- void RubyClassHandler::ShowRubyError(int error) // ---------------------------------------------------------------------------- { // Fetch the status error for rb_protected calls // Other non-protected calls may throw an exception to the // console/log and terminate the script. eg, wrong parameter count. #if not defined(LOGGING) return; #endif if(error == 0) return; wxString clog = wxEmptyString; wxString endl = _T("\n"); VALUE lasterr = rb_gv_get("$!"); // class VALUE klass = rb_class_path(CLASS_OF(lasterr)); clog << "class = " << RSTRING(klass)->ptr << endl; // message VALUE message = rb_obj_as_string(lasterr); clog << "message = " << RSTRING(message)->ptr << endl; // backtrace if(!NIL_P(ruby_errinfo)) { //-std::ostringstream o; wxString o = ""; VALUE ary = rb_funcall( ruby_errinfo, rb_intern("backtrace"), 0); int c; for (c=0; c<RARRAY(ary)->len; c++) { o << "\tfrom " << clog << "\tfrom " << RSTRING(RARRAY(ary)->ptr[c])->ptr << "\n"; } //-clog << "backtrace = " << o.str() << endl; clog << "backtrace = " << o << endl; } //-throw runtime_error("ruby_error"); #if defined(LOGGING) LOGIT( _T("Ruby Error:[%s]"), clog.c_str()); #endif ID method = rb_intern("puts"); if (MethodExists(rb_mKernel, _T("puts"))) rb_funcall(rb_mKernel, method, 1, rb_str_new2(clog.c_str() )); }
void Sandbox::Import(const char * c) { VALUE constants = rb_funcall(rb_str_new2(c), rb_intern("split"), 1, rb_str_new2("::")); VALUE constant = rb_cObject; for(int index = 0; index < RARRAY(constants)->len; ++index) { int status = 0; VALUE args[] = {constant, RARRAY(constants)->ptr[index]}; constant = rb_protect(ConstGetProtect, reinterpret_cast<VALUE>(args), &status); CheckStatus(status); } rb_funcall(sandbox_, rb_intern("import"), 1, constant); }
static VALUE ary_subseq(VALUE ary, int beg, int len) { VALUE ret; if (len == 0) return Qnil; ret = ary_alloc(); FL_SET(ret, ELTS_SHARED); RARRAY(ret)->aux.shared = ary; RARRAY(ret)->ptr = RARRAY(ary)->ptr + beg; RARRAY(ret)->len = len; return ret; }
VALUE rodbres_each(VALUE klass) { VALUE *ary; int n; Data_Get_Struct(klass, VALUE, ary); for(n=0; n < RARRAY(ary)->len; n++) { rb_yield(RARRAY(ary)->ptr[n]); } return klass; }