/******************************************************************* Subroutine to compute the Mean of Matrix matrix *X: the pointer to the matrix char direction: 'c' - compute the mean of each column 'r' - compute the mean of each row vector *mean: the pointer to the mean vector *******************************************************************/ int mmean(matrix *X, char direction, vector *mean) { int row_l, row_n; int i; int result; row_l = X->n; row_n = X->m; if (direction == 'c') { // compute the mean of each column mean->l = row_l; msum(X, 'c', mean); for (i=0; i<row_l; i++) { *(mean->pr + i) /= row_n; } result = 1; } else if (direction == 'r') { // compute the mean of each row mean->l = row_n; msum(X, 'r', mean); for (i=0; i<row_n; i++) { *(mean->pr + i) /= row_l; } result = 1; } else { result = 0; printf("the direction parameter should be 'c' or 'r'"); } return result; }
int main() { freopen("sdmin.in","r",stdin); freopen("sdmin.out","w",stdout); scanf("%d %d",&n,&sus); for(int i=1; i<=n; ++i) scanf("%d %d %d",&seg[i].xs,&seg[i].xd,&seg[i].y); //cautare ternara double left=-2000001,right=2000001; for(;right-left>EPS;) { double lt=(2.0*left + right)/3.0,rt=(left + 2.0*right)/3.0; double flt=msum(lt),drt=msum(rt); if(flt<drt) right=rt; else left=lt; } printf("%.6lf %.6lf",msum(left),left); return 0; }
double *_FR(double *d, double *dat, double *prob, double T, int N) { double t=T; int i=0; int j=1; do { double q = sum(prob,i, j); double s = msum(prob, dat, i, j); double sc = abs(abs(s)/sqrt(abs(q))); d[j]=sc; if(sc>t) { double scc = sc; while(sc>t & j<N & scc/q >= sc) { scc=sc; j++; q = sum(prob,i, j); s = msum(prob, dat, i, j); sc = abs(abs(s)/sqrt(abs(q))); } for(int k=i;k<j;k++) { d[k]=sc; } i=j; } else { i++; } j++; } while(j<N); return(d); }
SEXP exactmean(SEXP x){ SEXP ans; listnode expansion; size_t n; expansion.next = NULL; n = dplRlength(x); ans = PROTECT(allocVector(REALSXP, 1)); /* Note: x must be a numeric vector */ REAL(ans)[0] = msum(REAL(x), n, &expansion) / n; UNPROTECT(1); return ans; }
/******************************************************************* Part of Sub-level Kurtosis Calculate: sum(Zjk*ones(1,p).*(data_proj))./sum(Zjk) *******************************************************************/ void kurtmodel(matrix *mZjk, double sumZjk, matrix *data, vector *meanZjk) { int i; matrix Mt; mnew(&Mt, data->m, data->n); mmDotMul(mZjk, data, &Mt); msum(&Mt, 'c', meanZjk); for (i=0; i<(meanZjk->l); i++) { *(meanZjk->pr + i) /= sumZjk; }; mdelete(&Mt); }
inline RealType log_pdf(const dirichlet_distribution<RealType, Policy>& dist, const std::vector<T>& x, bool extended_precision) { if (!extended_precision) { return log_pdf(dist, x); } std::vector<RealType> alpha = dist.alpha(); std::vector<RealType> tmp(alpha.size(), 0.0); for (size_t i = 0; i < alpha.size(); i++) { tmp[i] = (alpha[i]-1.0)*std::log(x[i]); } return msum(tmp) - mbeta_log(alpha); }
void ckurtmodel(matrix *mZjk, double sumZjk, matrix *data_re, matrix *data_im, vector *meanZjk_re, vector *meanZjk_im) { int i; matrix Mt_re; matrix Mt_im; matrix mZjk_im; mnew(&Mt_re, data_re->m, data_re->n); mnew(&Mt_im, data_im->m, data_im->n); mnew(&mZjk_im, mZjk->m, mZjk->n); cmmDotMul(mZjk, &mZjk_im, data_re, data_im, &Mt_re, &Mt_im); msum(&Mt_re, 'c', meanZjk_re); msum(&Mt_im, 'c', meanZjk_im); for (i=0; i<(meanZjk_re->l); i++) { *(meanZjk_re->pr + i) /= sumZjk; *(meanZjk_im->pr + i) /= sumZjk; }; mdelete(&Mt_re); mdelete(&Mt_im); mdelete(&mZjk_im); }
/* Written by Mikko Korpela */ SEXP sens2(SEXP x){ SEXP ans; size_t i, n; double previous, this, next; double *x_const; dplr_double sum1, sum2; listnode tmp, *tmp_p; n = dplRlength(x); ans = PROTECT(allocVector(REALSXP, 1)); if(n < 2){ REAL(ans)[0] = R_NaN; UNPROTECT(1); return ans; } /* Note: x must be a numeric vector */ x_const = REAL(x); /* Setup for grow_exp and msum */ tmp.next = NULL; tmp.valid = FALSE; tmp_p = &tmp; /* In the sum of absolute differences between consecutive elements of an array, each number will appear multiplied by -2, -1, 0, 1, or 2 (first and last number by -1, 0, or 1) */ this = x_const[0]; next = x_const[1]; if(this > next){ grow_exp(tmp_p, this); } else if(this < next){ grow_exp(tmp_p, -this); } for(i = 1; i < n-1; i++){ previous = x_const[i-1]; this = x_const[i]; next = x_const[i+1]; if(this > previous){ if(this > next){ grow_exp(tmp_p, this); grow_exp(tmp_p, this); } else if(this == next){ grow_exp(tmp_p, this); } } else if(this < previous){ if(this < next){ grow_exp(tmp_p, -this); grow_exp(tmp_p, -this); } else if(this == next){ grow_exp(tmp_p, -this); } } else if(this > next){ grow_exp(tmp_p, this); } else if(this < next){ grow_exp(tmp_p, -this); } } this = x_const[n-1]; previous = x_const[n-2]; if(this > previous){ grow_exp(tmp_p, this); } else if(this < previous){ grow_exp(tmp_p, -this); } /* Sum of absolute differences */ sum1 = 0.0f; while(tmp_p != NULL && tmp_p->valid == TRUE){ sum1 += tmp_p->data; tmp_p = tmp_p->next; } sum2 = msum(x_const, n, &tmp); REAL(ans)[0] = sum1/(sum2-sum2/n); UNPROTECT(1); return ans; }