static int exec_le(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); *ret = strm_bool_value(strm_value_flt(args[0])<=strm_value_flt(args[1])); return STRM_OK; }
static int exec_plus(strm_state* state, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); if (strm_str_p(*args)) { strm_string *str1 = strm_value_str(args[0]); strm_string *str2 = strm_value_str(args[1]); strm_string *str3 = strm_str_new(NULL, str1->len + str2->len); char *p; p = (char*)str3->ptr; memcpy(p, str1->ptr, str1->len); memcpy(p+str1->len, str2->ptr, str2->len); p[str3->len] = '\0'; *ret = strm_ptr_value(str3); return STRM_OK; } if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])+strm_value_int(args[1])); return STRM_OK; } if (strm_num_p(args[0])) { *ret = strm_flt_value(strm_value_flt(args[0])+strm_value_flt(args[1])); return STRM_OK; } return STRM_NG; }
static int exec_gt(strm_state* state, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); *ret = strm_bool_value(strm_value_flt(args[0])>strm_value_flt(args[1])); return STRM_OK; }
static int exec_lt(node_ctx* ctx, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); *ret = strm_bool_value(strm_value_flt(args[0])<strm_value_flt(args[1])); return 0; }
static int iter_correl(strm_stream* strm, strm_value data) { struct correl_data* d = strm->data; strm_value *v; double dx, dy; if (!strm_array_p(data) || strm_ary_len(data) != 2) { strm_raise(strm, "invalid data"); return STRM_NG; } v = strm_ary_ptr(data); if (!strm_number_p(v[0]) || !strm_number_p(v[1])) { strm_raise(strm, "correl() requires [num, num]"); return STRM_NG; } d->n++; dx = strm_value_flt(v[0]) - d->sx; d->sx += dx / d->n; dy = strm_value_flt(v[1]) - d->sy; d->sy += dy / d->n; d->sxx += (d->n-1) * dx * dx / d->n; d->syy += (d->n-1) * dy * dy / d->n; d->sxy += (d->n-1) * dx * dy / d->n; return STRM_OK; }
static int ary_correl(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_value* v; int i, len; double sx, sy, sxx, syy, sxy; strm_get_args(strm, argc, args, "a", &v, &len); sx = sy = sxx = syy = sxy = 0; for (i=0; i<len; i++) { strm_value data = v[i]; strm_value* dv; double dx, dy; if (!strm_array_p(data) || strm_ary_len(data) != 2) { /* skip invalid data */ continue; } dv = strm_ary_ptr(data); dx = strm_value_flt(dv[0]) - sx; sx += dx / (i+1); dy = strm_value_flt(dv[1]) - sy; sy += dy / (i+1); sxx += i * dx * dx / (i+1); syy += i * dy * dy / (i+1); sxy += i * dx * dy / (i+1); } sxx = sqrt(sxx / (len-1)); syy = sqrt(syy / (len-1)); sxy /= (len-1) * sxx * syy; *ret = strm_flt_value(sxy); return STRM_OK; }
static int exec_minus(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { if (argc == 1) { if (strm_int_p(args[0])) { *ret = strm_int_value(-strm_value_int(args[0])); return STRM_OK; } if (strm_flt_p(args[0])) { *ret = strm_flt_value(-strm_value_flt(args[0])); return STRM_OK; } return STRM_NG; } assert(argc == 2); if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])-strm_value_int(args[1])); return STRM_OK; } if (strm_num_p(args[0])) { *ret = strm_flt_value(strm_value_flt(args[0])-strm_value_flt(args[1])); return STRM_OK; } return STRM_NG; }
static int exec_plus(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); if (strm_string_p(*args)) { strm_string str1 = strm_value_str(args[0]); strm_string str2 = strm_value_str(args[1]); strm_string str3 = strm_str_new(NULL, strm_str_len(str1) + strm_str_len(str2)); char *p; p = (char*)strm_str_ptr(str3); memcpy(p, strm_str_ptr(str1), strm_str_len(str1)); memcpy(p+strm_str_len(str1), strm_str_ptr(str2), strm_str_len(str2)); p[strm_str_len(str3)] = '\0'; *ret = strm_str_value(str3); return STRM_OK; } if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])+strm_value_int(args[1])); return STRM_OK; } if (strm_num_p(args[0])) { *ret = strm_flt_value(strm_value_flt(args[0])+strm_value_flt(args[1])); return STRM_OK; } return STRM_NG; }
static int exec_minus(node_ctx* ctx, int argc, strm_value* args, strm_value* ret) { if (argc == 1) { if (strm_int_p(args[0])) { *ret = strm_int_value(-strm_value_int(args[0])); return 0; } if (strm_flt_p(args[0])) { *ret = strm_flt_value(-strm_value_flt(args[0])); return 0; } return 1; } assert(argc == 2); if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])-strm_value_int(args[1])); return 0; } else if (strm_num_p(args[0])) { *ret = strm_flt_value(strm_value_flt(args[0])-strm_value_flt(args[1])); return 0; } return 1; }
static int exec_mod(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])%strm_value_int(args[1])); return STRM_OK; } if (strm_num_p(args[0])) { *ret = strm_flt_value((int)strm_value_flt(args[0])%(int)strm_value_flt(args[1])); return STRM_OK; } return STRM_NG; }
static int exec_mult(strm_state* state, int argc, strm_value* args, strm_value* ret) { assert(argc == 2); if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])*strm_value_int(args[1])); return STRM_OK; } if (strm_num_p(args[0])) { *ret = strm_flt_value(strm_value_flt(args[0])*strm_value_flt(args[1])); return STRM_OK; } return STRM_NG; }
static int iter_stdev(strm_stream* strm, strm_value data) { struct stdev_data* d = strm->data; double x = strm_value_flt(data); d->num++; x -= d->s1; d->s1 += x/d->num; d->s2 += (d->num-1) * x * x / d->num; return STRM_OK; }
static int ary_sum_avg(strm_stream* strm, int argc, strm_value* args, strm_value* ret, int avg) { int i, len; strm_value* v; double sum = 0; double c = 0; strm_value func; strm_get_args(strm, argc, args, "a|v", &v, &len, &func); if (argc == 0) { for (i=0; i<len; i++) { double y = strm_value_flt(v[i]) - c; double t = sum + y; c = (t - sum) - y; sum = t; } } else { for (i=0; i<len; i++) { strm_value val; double f, y, t; val = convert_number(strm, v[i], func); f = strm_value_flt(val); y = f - c; t = sum + y; c = (t - sum) - y; sum = t; } } if (avg) { *ret = strm_flt_value(sum/len); } else { *ret = strm_flt_value(sum); } return STRM_OK; }
static int ary_var_stdev(strm_stream* strm, int argc, strm_value* args, strm_value* ret, int stdev) { strm_value func; strm_value* v; int i, len; double s1, s2; strm_get_args(strm, argc, args, "a|v", &v, &len, &func); s1 = s2 = 0.0; if (argc == 0) { for (i=0; i<len; i++) { double x = strm_value_flt(v[i]); x -= s1; s1 += x/(i+1); s2 += i * x * x / (i+1); } } else { for (i=0; i<len; i++) { strm_value val; double x; val = convert_number(strm, v[i], func); x = strm_value_flt(val); x -= s1; s1 += x/(i+1); s2 += i * x * x / (i+1); } } s2 = s2 / (i-1); if (stdev) { s2 = sqrt(s2); } *ret = strm_flt_value(s2); return STRM_OK; }
static int iter_stdevf(strm_stream* strm, strm_value data) { struct stdev_data* d = strm->data; double x; data = convert_number(strm, data, d->func); x = strm_value_flt(data); d->num++; x -= d->s1; d->s1 += x/d->num; d->s2 += (d->num-1) * x * x / d->num; return STRM_OK; }
int strm_value_eq(strm_value a, strm_value b) { if (a == b) return TRUE; if (strm_value_tag(a) != strm_value_tag(b)) goto typediff; switch (strm_value_tag(a)) { case STRM_TAG_STRUCT: return strm_ary_eq(a, b); case STRM_TAG_STRING_O: case STRM_TAG_STRING_F: return strm_str_eq(a, b); case STRM_TAG_CFUNC: return (strm_cfunc)(intptr_t)strm_value_val(a) == (strm_cfunc)(intptr_t)strm_value_val(b); case STRM_TAG_PTR: return strm_value_vptr(a) == strm_value_vptr(b); typediff: default: if (strm_number_p(a) && strm_number_p(b)) { return strm_value_flt(a) == strm_value_flt(b); } return FALSE; } }
static int iter_sumf(strm_stream* strm, strm_value data) { struct sum_data* d = strm->data; double f, y, t; data = convert_number(strm, data, d->func); f = strm_value_flt(data); y = f - d->c; t = d->sum + y; d->c = (t - d->sum) - y; d->sum = t; d->num++; return STRM_OK; }
static int iter_sum(strm_stream* strm, strm_value data) { struct sum_data* d = strm->data; double f, y, t; if (!strm_number_p(data)) { return STRM_NG; } f = strm_value_flt(data); y = f - d->c; t = d->sum + y; d->c = (t - d->sum) - y; d->sum = t; d->num++; return STRM_OK; }