46#include <gsl/gsl_linalg.h>
51 #include <sys/param.h>
55 #define access _access
64int Util::MUTEX_INIT(
MUTEX *mutex)
67 *mutex = CreateMutex(0, FALSE, 0);
70 return pthread_mutex_init(mutex, NULL);
78 return (WaitForSingleObject(*mutex, INFINITE)==WAIT_FAILED?1:0);
80 return pthread_mutex_lock(mutex);
88 return (ReleaseMutex(*mutex)==0);
90 return pthread_mutex_unlock(mutex);
105 for (
size_t i = 0; i < n; i += 2) {
106 float f = data[i] * sin(data[i + 1]);
107 data[i] = data[i] * cos(data[i + 1]);
120 for (
size_t i = 0; i < n; i += 2) {
121 double f = data[i] * sin(data[i + 1]);
122 data[i] = data[i] * cos(data[i + 1]);
135 for (
size_t i = 0; i < n; i += 2) {
145 else if(ny!=1 && nz==1) {
149 for (i=0; i<ny/2; i++) {
150 for (j=0; j<nx; j++) {
159 size_t i, j, k, l, ii, jj;
160 char * t=(
char *)malloc(
sizeof(
float)*nx);
165 for (j=ii=0; j<nz/2; ++j) {
166 for (i=0; i<ny; ++i,ii+=nx) {
167 memcpy(t,data+ii,jj);
169 memcpy(data+ii,data+ii+k,jj);
170 memcpy(data+ii+k,t,jj);
173 memcpy(data+ii,data+ii+l,jj);
174 memcpy(data+ii+l,t,jj);
192 int fdes = fileno(file);
196 fl.l_whence = SEEK_SET;
200 fl.l_pid = _getpid();
206 fl.l_sysid = getpid();
210 if (fcntl(fdes, F_SETLKW, &fl) == -1) {
211 LOGERR(
"file locking error! NFS problem?");
214 for (i = 0; i < 5; i++) {
215 if (fcntl(fdes, F_SETLKW, &fl) != -1) {
228 LOGERR(
"Fatal file locking error");
241 if (!first_block || !magic) {
245 const char *buf =
static_cast < const char *
>(first_block);
247 if (strncmp(buf, magic, strlen(magic)) == 0) {
255 return access(filename.c_str(), F_OK) == 0;
267 float *buf =
new float[nx];
268 size_t row_size = nx *
sizeof(float);
270 for (
size_t i = 0; i < ny / 2; i++) {
271 memcpy(buf, &data[i * nx], row_size);
272 memcpy(&data[i * nx], &data[(ny - 1 - i) * nx], row_size);
273 memcpy(&data[(ny - 1 - i) * nx], buf, row_size);
285 std::transform(s.begin(), s.end(), ret.begin(), ::tolower);
296 while (ic < max_size && str[ic] != 0) {
297 if (! isascii (str[ic])) {
312 return strncmp(s1, s2, strlen(s2)) == 0;
324 if (!slines || !(*slines)) {
331 while (*str !=
'\n' && *str !=
'\0') {
332 result.push_back(*str);
344 int nimg=data.size();
345 if (nvec==0) nvec=nimg;
346 vector<EMData *> ret(nvec);
347 if (nimg==0)
return ret;
348 int pixels=data[0]->get_xsize()*data[0]->get_ysize()*data[0]->get_zsize();
351 gsl_vector *work=gsl_vector_alloc(nimg);
352 gsl_vector *S=gsl_vector_alloc(nimg);
353 gsl_matrix *A=gsl_matrix_alloc(pixels,nimg);
354 gsl_matrix *
V=gsl_matrix_alloc(nimg,nimg);
355 gsl_matrix *X=gsl_matrix_alloc(nimg,nimg);
358 for (im=0; im<nimg; im++) {
359 for (z=0,i=0; z<data[0]->get_zsize(); z++) {
360 for (
y=0;
y<data[0]->get_ysize();
y++) {
361 for (
x=0;
x<data[0]->get_xsize();
x++,i++) {
369 gsl_linalg_SV_decomp_mod (A,X,
V, S, work);
371 for (im=0; im<nvec; im++) {
372 EMData *a=data[0]->copy_head();
374 for (z=0,i=0; z<data[0]->get_zsize(); z++) {
375 for (
y=0;
y<data[0]->get_ysize();
y++) {
376 for (
x=0;
x<data[0]->get_xsize();
x++,i++) {
377 a->set_value_at(
x,
y,z,
static_cast<float>(gsl_matrix_get(A,i,im)));
387 if (!s || !float_var || !p_val) {
390 size_t n = strlen(float_var);
391 if (strncmp(s, float_var, n) == 0) {
392 *p_val = (float) atof(&s[n]);
401 if (!s || !float_var || !p_v1 || !p_v2) {
405 size_t n = strlen(float_var);
406 if (strncmp(s, float_var, n) == 0) {
407 sscanf(&s[n],
"%f,%f", p_v1, p_v2);
415 int *p_v0,
float *p_v1,
float *p_v2)
417 if (!s || !float_var || !p_v0 || !p_v1 || !p_v2) {
421 size_t n = strlen(float_var);
423 if (strncmp(s, float_var, n) == 0) {
426 sscanf(&s[n + 1],
"%f,%f", p_v1, p_v2);
438 if (!s || !int_var || !p_val) {
442 size_t n = strlen(int_var);
443 if (strncmp(s, int_var, n) == 0) {
444 *p_val = atoi(&s[n]);
452 if (!s || !int_var || !p_v1 || !p_v2) {
456 size_t n = strlen(int_var);
457 if (strncmp(s, int_var, n) == 0) {
458 sscanf(&s[n],
"%d,%d", p_v1, p_v2);
466 if (!s || !int_var || !p_v0 || !p_v1 || !p_v2) {
470 size_t n = strlen(int_var);
472 if (strncmp(s, int_var, n) == 0) {
475 sscanf(&s[n + 1],
"%d,%d", p_v1, p_v2);
488 Assert(old_filename !=
"");
497 if (filename.empty())
500 auto pos = filename.rfind(
'.');
502 return pos!=string::npos ? filename.substr(0, pos) : filename;
507 if (filename ==
"") {
515 const char * c = strrchr(filename.c_str(), s);
528 if (filename.empty())
531 auto pos = filename.rfind(
'.');
532 if (pos != string::npos)
533 return filename.substr(pos+1);
539 float *slope,
float *intercept,
bool ignore_zero,
float absmax)
543 if (!data_x || !data_y || !slope || !intercept) {
552 for (
size_t i = 0; i < nitems; i++) {
553 if ((!ignore_zero || (data_x[i] != 0 && data_y[i] != 0))&&(!absmax ||(data_y[i]<absmax && data_y[i]>-absmax))) {
554 double y = data_y[i];
568 double div = sum * sum_xx - sum_x * sum_x;
573 *intercept = (float) ((sum_xx * sum_y - sum_x * sum_xy) /
div);
574 *slope = (float) ((sum * sum_xy - sum_x * sum_y) /
div);
581double Sx=0,Sy=0,Sxy=0,Sxx=0,Syy=0,Sz=0,Sxz=0,Syz=0,S=0;
582for (i=0; i<p.size(); i+=3) {
593double d=S*Sxy*Sxy - 2*Sx*Sxy*Sy + Sxx*Sy*Sy + Sx*Sx*Syy - S*Sxx*Syy;
597ret[0]=
static_cast<float>(-((Sxy*Sxz*Sy - Sx*Sxz*Syy + Sx*Sxy*Syz - Sxx*Sy*Syz - Sxy*Sxy*Sz +Sxx*Syy*Sz)/d));
598ret[1]=
static_cast<float>(-((-Sxz*Sy*Sy + S*Sxz*Syy - S*Sxy*Syz + Sx*Sy*Syz + Sxy*Sy*Sz -Sx*Syy*Sz) /d));
599ret[2]=
static_cast<float>(-((-S*Sxy*Sxz + Sx*Sxz*Sy - Sx*Sx*Syz + S*Sxx*Syz + Sx*Sxy*Sz -Sxx*Sy*Sz) /d));
605 const string & filename)
607 Assert(x_array.size() > 0);
608 Assert(y_array.size() > 0);
611 if (x_array.size() != y_array.size()) {
612 LOGERR(
"array x and array y have different size: %d != %d\n",
613 x_array.size(), y_array.size());
617 FILE *out = fopen(filename.c_str(),
"wb");
622 for (
size_t i = 0; i < x_array.size(); i++) {
623 fprintf(out,
"%g\t%g\n", x_array[i], y_array[i]);
629 const string & filename)
632 Assert(y_array.size() > 0);
635 FILE *out = fopen(filename.c_str(),
"wb");
640 for (
size_t i = 0; i < y_array.size(); i++) {
641 fprintf(out,
"%g\t%g\n", x0 + dx * i, y_array[i]);
648 size_t array_size,
const string & filename)
658 FILE *out = fopen(filename.c_str(),
"wb");
663 for (
size_t i = 0; i < array_size; i++) {
664 fprintf(out,
"%g\t%g\n", x0 + dx * i, y_array[i]);
673 float *pivot ;
int *pivotPerm;
676 float *pLeft = left;
int *pLeftPerm = leftPerm;
677 float *pRight = right;
int *pRightPerm = rightPerm;
678 float scratch = *left;
int scratchPerm = *leftPerm;
680 while (pLeft < pRight) {
681 while ((*pRight > scratch) && (pLeft < pRight)) {
682 pRight--; pRightPerm--;
684 if (pLeft != pRight) {
685 *pLeft = *pRight; *pLeftPerm = *pRightPerm;
686 pLeft++; pLeftPerm++;
688 while ((*pLeft < scratch) && (pLeft < pRight)) {
689 pLeft++; pLeftPerm++;
691 if (pLeft != pRight) {
692 *pRight = *pLeft; *pRightPerm = *pLeftPerm;
693 pRight--; pRightPerm--;
696 *pLeft = scratch; *pLeftPerm = scratchPerm;
697 pivot = pLeft; pivotPerm= pLeftPerm;
700 sort_mat(left, pivot - 1,leftPerm,pivotPerm-1);
703 sort_mat(pivot + 1, right,pivotPerm+1,rightPerm);
744static float *mem = (
float *)malloc(4*128*128);
749if (
x>=dim ||
y>=dim) {
750 if (
x>2048 ||
y>2048)
return (
float)hypot((
float)
x,(
float)
y);
753 mem=(
float*)realloc(mem,4*dim*dim);
754 for (
int y=0;
y<dim;
y++) {
755 for (
int x=0;
x<dim;
x++) {
756 mem[
x+
y*dim]=hypot((
float)
x,(
float)
y);
766static short *mem = (
short *)malloc(2*128*128);
771if (
x>=dim ||
y>=dim) {
772 if (
x>4095 ||
y>4095)
return (
short)hypot((
float)
x,(
float)
y);
775 mem=(
short*)realloc(mem,2*dim*dim);
776 for (
int y=0;
y<dim;
y++) {
777 for (
int x=0;
x<dim;
x++) {
789static float *mem = (
float *)malloc(
sizeof(
float)*1000);
790static bool needinit=
true;
794 for (
int i=0; i<1000; i++) mem[i]=(
float)exp(-i/50.0);
796if (f>0 || f<-19.98)
return exp(f);
797int g=(int)(-f*50.0+0.5);
805if (f>=1.0)
return 0.0;
806if (f<=-1.0)
return M_PI;
808static float *mem = (
float *)malloc(
sizeof(
float)*2001);
809static bool needinit=
true;
814 for (
int i=0; i<=2000; i++) mem[i]=(
float)acos(i/1000.0-1.0);
816float f2=f*1000.0f+1000.0f;
831static float *mem = (
float *)malloc(
sizeof(
float)*1000);
832static bool needinit=
true;
836 for (
int i=0; i<1000; i++) mem[i]=(
float)exp(-i*i/125000.0f);
838if (f>1.998)
return exp(-2.0f*f*f);
839int g=(int)(fabs(f)*500.0f+0.5f);
851void Util::find_max(
const float *data,
size_t nitems,
float *max_val,
int *max_index)
855 if (!data || !max_val || !max_index) {
858 float max = -FLT_MAX;
861 for (
size_t i = 0; i < nitems; i++) {
868 *max_val = (float)max;
876 float *max_val,
float *min_val,
877 int *max_index,
int *min_index)
881 if (!data || !max_val || !min_val || !max_index || !min_index) {
884 float max = -FLT_MAX;
889 for (
size_t i = 0; i < nitems; i++) {
918 if (data.size() == 0)
EmptyContainerException(
"Error, attempting to call get stats on an empty container (vector<double>)");
920 double sum = accumulate(data.begin(), data.end(), 0.0);
922 double mean = sum /
static_cast<double> (data.size());
924 double std_dev = 0.0, skewness = 0.0, kurtosis = 0.0;
929 vector<double> data_mm(data.size());
931 vector<double> data_mm_sq(data.size());
934 transform(data.begin(), data.end(), data_mm.begin(), std::bind2nd(std::minus<double>(), mean));
937 transform(data_mm.begin(), data_mm.end(), data_mm.begin(), data_mm_sq.begin(), std::multiplies<double>());
940 double square_sum = accumulate(data_mm_sq.begin(), data_mm_sq.end(), 0.0);
944 double std_dev_sq = std_dev * std_dev;
947 double cubic_sum = inner_product(data_mm.begin(), data_mm.end(),data_mm_sq.begin(), 0.0);
950 double quartic_sum = inner_product(data_mm_sq.begin(), data_mm_sq.end(),data_mm_sq.begin(), 0.0);
954 skewness = cubic_sum / ((data.size()-1) * std_dev_sq * std_dev );
955 kurtosis = quartic_sum / ((data.size()-1) * std_dev_sq * std_dev_sq );
960 parms[
"mean"] = mean;
961 parms[
"std_dev"] = std_dev;
962 parms[
"skewness"] = skewness;
963 parms[
"kurtosis"] = kurtosis;
971 if (data.size() == 0)
EmptyContainerException(
"Error, attempting to call get stats on an empty container (vector<double>)");
973 double square_sum = 0.0, sum = 0.0, cube_sum = 0.0, quart_sum = 0.0;
974 for( vector<float>::const_iterator it = data.begin(); it != data.end(); ++it )
984 double mean = sum/(double)data.size();
986 double std_dev = 0.0, skewness = 0.0, kurtosis = 0.0;
993 double square_mean = mean*mean;
994 double cube_mean = mean*square_mean;
996 double square_std_dev = std_dev*std_dev;
1000 double cubic_sum = cube_sum - 3*
square_sum*mean + 3*sum*square_mean - cube_mean*data.size();
1002 skewness = cubic_sum/((data.size()-1)*square_std_dev*std_dev);
1006 double quartic_sum = quart_sum - 4*cube_sum*mean + 6*
square_sum*square_mean - 4*sum*cube_mean + square_mean*square_mean*data.size();
1008 kurtosis = quartic_sum /( (data.size()-1)*square_std_dev*square_std_dev);
1012 parms[
"mean"] = mean;
1013 parms[
"std_dev"] = std_dev;
1014 parms[
"skewness"] = skewness;
1015 parms[
"kurtosis"] = kurtosis;
1024 if (low>16384)
return 16384;
1039 vector<float> ret(curve);
1040 if (first<1) first=1;
1045 for (
int i=first; i<ret.size()-1; i++) {
1046 float q= (ret[i-1]+ret[i+1])/2.0;
1047 if (ret[i]>q) { ret[i]=q; cont=1; }
1056vector<float>
Util::windowdot(
const vector<float>&curveA,
const vector<float>&curveB,
int window,
int normA) {
1057 if (window<=0) { printf(
"Warning, %d window in windowdot\n",window); window=1; }
1060 vector<float> ret(curveA.size(),0.0f);
1061 vector<float> suba(ws,0.0f);
1062 vector<float> subb(ws,0.0f);
1065 for (
int i=window; i<curveA.size()-window; i++) {
1066 double asig=0,bsig=0,amean=0,bmean=0;
1069 for (
int j=0,k=i-window; k<=i+window; j++,k++) {
1074 asig +=suba[j]*suba[j];
1075 bsig +=subb[j]*subb[j];
1079 asig=
sqrt(asig/(
double)ws-amean*amean);
1080 bsig=
sqrt(bsig/(
double)ws-bmean*bmean);
1085 for (
int j=0; j<ws; j++) {
1086 subb[j]=(subb[j]-bmean)/bsig;
1087 if (normA) suba[j]=(suba[j]-amean)/asig;
1089 dot+=suba[j]*subb[j];
1091 ret[i]=
dot/(float)ws;
1098#include <sys/types.h>
1099#include <sys/socket.h>
1109 unsigned char data[1024]; };
1117 printf(
"No cache mirroring on Big endian machines yet\n");
1129 int l = recv(sock,&pkt,1044,0);
1131 if (obj!=-1) printf(
"Timeout with incomplete obj %d %d/%d\n",obj,i,(
int)fill.size());
1135 printf(
"Bad packet from broadcast");
1139 if (strncmp(pkt.
hdr,
"EMAN",4)!=0)
continue;
1142 if (obj!=pkt.
oseq) {
1144 ret.resize(pkt.
len);
1145 fill.resize((pkt.
len-1)/1024+1);
1146 for (i=0; i<fill.size(); i++) fill[i]=0;
1148 if (obj==-1) printf(
"Something wierd happened. please report\n");
1152 ret.replace(pkt.
pseq*1024,l-20,(
char *)pkt.
data,l-20);
1155 for (i=0; i<fill.size(); i++) {
1156 if (fill[i]!=1)
break;
1161 if (i==fill.size())
return ret;
1169 time_t t0 = time(0);
1170 struct tm *t = localtime(&t0);
1172 sprintf(label,
"%d/%02d/%04d %d:%02d",
1173 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, t->tm_min);
1174 return string(label);
1180 if (argc > 1 && strncmp(argv[1],
"-v", 2) == 0) {
1182 strcpy(level_str, argv[1] + 2);
1191 out <<
"Printing 3D Integer data: " << str << std::endl;
1192 const multi_array_types::size_type* sizes = mat.shape();
1193 int nx = sizes[0], ny = sizes[1], nz = sizes[2];
1194 const multi_array_types::index* indices = mat.index_bases();
1195 int bx = indices[0], by = indices[1], bz = indices[2];
1196 for (
int iz = bz; iz < nz+bz; iz++) {
1197 cout <<
"(z = " << iz <<
" slice)" << endl;
1198 for (
int ix = bx; ix < nx+bx; ix++) {
1199 for (
int iy = by; iy < ny+by; iy++) {
1200 cout << setiosflags(ios::fixed) << setw(5)
1201 << mat[ix][iy][iz] <<
" ";
1208#include <gsl/gsl_matrix.h>
1209#include <gsl/gsl_vector.h>
1210#include <gsl/gsl_linalg.h>
1212void printmatrix( gsl_matrix* M,
const int n,
const int m,
const string& message =
"")
1214 cout << message << endl;
1215 for(
int i = 0; i < n; ++i ){
1216 for (
int j = 0; j < m; ++j ){
1217 cout << gsl_matrix_get(M,i,j) <<
"\t";
1223void printvector( gsl_vector* M,
const int n,
const string& message =
"")
1225 cout << message << endl;
1226 for(
int i = 0; i < n; ++i ){
1227 cout << gsl_vector_get(M,i) <<
"\t";
1235 int discs = (int)(1+2*freq_cutoff/dfreq);
1237 float* W =
new float[discs];
1239 int fc = (int)(2*freq_cutoff + 1);
1240 gsl_matrix* M = gsl_matrix_calloc(fc,fc);
1242 gsl_vector* rhs = gsl_vector_calloc(fc);
1243 cout << i++ << endl;
1244 for(
int k = -freq_cutoff; k <= freq_cutoff; ++k){
1245 for(
int kp = -freq_cutoff; kp <= freq_cutoff; ++kp){
1246 int kdiff =abs( k-kp);
1247 int evenoddfac = ( kdiff % 2 == 0 ? 1 : -1);
1250 float val = sin(M_PI*(
float)kdiff*r)/(sin(M_PI*(
float)kdiff/(
float)P))*(alpha+2.0f*beta*evenoddfac);
1251 gsl_matrix_set(M,
int(k+freq_cutoff),
int(kp+freq_cutoff),val);
1254 gsl_matrix_set(M,
int(k+freq_cutoff),
int(k+freq_cutoff),r*P* (alpha+2*beta));
1255 float val = alpha*sin(M_PI*k*r)/(sin(M_PI*(
float)k/(
float)P));
1257 gsl_vector_set(rhs,
int(k+freq_cutoff),val);
1262 gsl_vector_set(rhs,
int(freq_cutoff),alpha*r*P);
1263 gsl_matrix*
V = gsl_matrix_calloc(fc,fc);
1264 gsl_vector* S = gsl_vector_calloc(fc);
1265 gsl_vector* soln = gsl_vector_calloc(fc);
1266 gsl_linalg_SV_decomp(M,
V,S,soln);
1268 gsl_linalg_SV_solve(M,
V, S, rhs, soln);
1273 for(
float q = (
float)(-freq_cutoff); q <= (float)(freq_cutoff); q+= dfreq){
1275 for(
int k = -freq_cutoff; k <= freq_cutoff; ++k){
1278 dtemp=(1/(float) P)* (float)gsl_vector_get(soln,
int(k+freq_cutoff)) * sin(M_PI*(q-k))/sin(M_PI*(q-k)/((float) P));
1280 dtemp = (1/(float) P)* (float)gsl_vector_get(soln,
int(k+freq_cutoff)) * P;
1285 cout << W[Count] <<
" ";
1296 plane[0] = p1[
y]*(p2[z]-p3[z])+p2[
y]*(p3[z]-p1[z])+p3[
y]*(p1[z]-p2[z]);
1297 plane[1] = p1[z]*(p2[
x]-p3[
x])+p2[z]*(p3[
x]-p1[
x])+p3[z]*(p1[
x]-p2[
x]);
1298 plane[2] = p1[
x]*(p2[
y]-p3[
y])+p2[
x]*(p3[
y]-p1[
y])+p3[
x]*(p1[
y]-p2[
y]);
1299 plane[3] = p1[
x]*(p2[
y]*p3[z]-p3[
y]*p2[z])+p2[
x]*(p3[
y]*p1[z]-p1[
y]*p3[z])+p3[
x]*(p1[
y]*p2[z]-p2[
y]*p1[z]);
1300 plane[3] = -plane[3];
1309 Vec2f w = point - p1;
1311 float udotu = u.
dot(u);
1312 float udotv = u.
dot(v);
1313 float udotw = u.
dot(w);
1314 float vdotv = v.
dot(v);
1315 float vdotw = v.
dot(w);
1317 float d = 1.0f/(udotv*udotv - udotu*vdotv);
1318 float s = udotv*vdotw - vdotv*udotw;
1321 float t = udotv*udotw - udotu*vdotw;
1335 if ( s >= 0 && t >= 0 && (s+t) <= 1 )
return true;
static bool is_host_big_endian()
Dict is a dictionary to store <string, EMObject> pair.
EMData stores an image's data and defines core image processing routines.
void set_level(int level)
The wrapper class for gsl's random number generater.
void set_seed(unsigned long long seed)
Set the seed for the random number generator.
static Randnum * Instance()
long long get_irand(long long lo, long long hi) const
This function returns a random integer from lo to hi inclusive.
unsigned long long get_seed()
Get the current random number seed.
float get_gauss_rand(float mean, float sigma) const
Return a Gaussian random number.
float get_frand(double lo=0.0, double hi=1.0) const
This function returns a random float from lo inclusive to hi.
static float * getBaldwinGridWeights(const int &freq_cutoff, const float &P, const float &r, const float &dfreq=1, const float &alpha=0.5, const float &beta=0.2)
static float fast_gauss_B2(const float &f)
Returns an approximate of exp(-2x^2) using a cached table uses actual function outside the cached ran...
static void calc_least_square_fit(size_t nitems, const float *data_x, const float *data_y, float *p_slope, float *p_intercept, bool ignore_zero, float absmax=0)
calculate the least square fit value.
static int MUTEX_UNLOCK(MUTEX *mutex)
static string remove_filename_ext(const string &filename)
Remove a filename's extension and return the new filename.
static string recv_broadcast(int port)
static string str_to_lower(const string &s)
Return a lower case version of the argument string.
static bool get_str_float(const char *str, const char *float_var, float *p_val)
Extract the float value from a variable=value string with format like "XYZ=1.1", where 'str' is "XYZ=...
static void flip_complex_phase(float *data, size_t n)
flip the phase of a complex data array.
static int file_lock_wait(FILE *file)
lock a file.
static short hypot_fast_int(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
static int MUTEX_LOCK(MUTEX *mutex)
static float fast_acos(const float &f)
Returns an approximate of acos(x) using a cached table and linear interpolation tolerates values very...
static vector< EMData * > svdcmp(const vector< EMData * > &data, int nvec)
Perform singular value decomposition on a set of images.
static bool point_is_in_triangle_2d(const Vec2f &p1, const Vec2f &p2, const Vec2f &p3, const Vec2f &actual_point)
Determines if a point is in a 2D triangle using the Barycentric method, which is a fast way of perfor...
static int square(int n)
Calculate a number's square.
static Dict get_stats(const vector< float > &data)
Get the mean, standard deviation, skewness and kurtosis of the input data.
static Vec3f calc_bilinear_least_square(const vector< float > &points)
calculate bilinear least-square fit, z = a + b x + c y Takes a set of x,y,z vectors and produces an a...
static void flip_image(float *data, size_t nx, size_t ny)
Vertically flip the data of a 2D real image.
static string get_time_label()
Get the current time in a string with format "mm/dd/yyyy hh:mm".
static bool sstrncmp(const char *s1, const char *s2)
Safe string compare.
static bool get_str_int(const char *str, const char *int_var, int *p_val)
Extract the int value from a variable=value string with format like "XYZ=1", where 'str' is "XYZ=1"; ...
static void set_log_level(int argc, char *argv[])
Set program logging level through command line option "-v N", where N is the level.
static void find_max(const float *data, size_t nitems, float *p_max_val, int *p_max_index=0)
Find the maximum value and (optional) its index in an array.
static void set_randnum_seed(unsigned long long seed)
Set the seed for Randnum class.
static float get_frand(int low, int high)
Get a float random number between low and high, [low, high)
static string get_filename_ext(const string &filename)
Get a filename's extension.
static void find_min_and_max(const float *data, size_t nitems, float *p_max_val, float *p_min_val, int *p_max_index=0, int *p_min_index=0)
Find the maximum value and (optional) its index, minimum value and (optional) its index in an array.
static bool is_file_exist(const string &filename)
check whether a file exists or not
static void replace_non_ascii(char *str, int max_size, char repl_char='?')
Replace any non-ASCII characters in a C string with a given character.
static string sbasename(const string &filename)
Get a filename's basename.
static void rotate_phase_origin(float *data, size_t nx, size_t ny, size_t nz)
rotate data vertically by ny/2, to make the mrc phase origin compliance with EMAN1 and TNF reader
static void printMatI3D(MIArray3D &mat, const string str=string(""), ostream &out=std::cout)
Print a 3D integer matrix to a file stream (std out by default).
static vector< float > nonconvex(const vector< float > &curve, int first=3)
Returns a non-convex version of a curve.
static int round(float x)
Get ceiling round of a float number x.
static void ap2ri(float *data, size_t n)
convert complex data array from Amplitude/Phase format into Real/Imaginary format.
static int calc_best_fft_size(int low)
Search the best FFT size with good primes.
static void save_data(const vector< float > &x_array, const vector< float > &y_array, const string &filename)
Save (x y) data array into a file.
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
static string get_line_from_string(char **str)
Extract a single line from a multi-line string.
static vector< float > windowdot(const vector< float > &curveA, const vector< float > &curveB, int window, int normA)
Computes a windowed dot product between curve A and curve B.
static float fast_exp(const float &f)
Returns an approximate of exp(x) using a cached table uses actual exp(x) outside the cached range.
static bool point_is_in_convex_polygon_2d(const Vec2f &p1, const Vec2f &p2, const Vec2f &p3, const Vec2f &p4, const Vec2f &actual_point)
Determines if a point is in a 2D convex polygon described by 4 points using the Barycentric method,...
static string int2str(int n)
Get a string format of an integer, e.g.
static float square_sum(float x, float y)
Calcuate (x*x + y*y).
static void sort_mat(float *left, float *right, int *leftPerm, int *rightPerm)
does a sort as in Matlab.
static int get_irand(int low, int high)
Get an integer random number between low and high, [low, high].
static Dict get_stats_cstyle(const vector< float > &data)
Performs the same calculations as in get_stats, but uses a single pass, optimized c approach Should p...
static unsigned long long get_randnum_seed()
Get the seed for Randnum class.
static void equation_of_plane(const Vec3f &p1, const Vec3f &p2, const Vec3f &p3, float *plane)
Determine the equation of a plane that intersects 3 points in 3D space.
static float get_gauss_rand(float mean, float sigma)
Get a Gaussian random number.
static float hypot_fast(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
The Vec2 is precisely the same as Vec3 except it works exclusively in 2D Note there are convenient ty...
Type dot(const Vec2< Type2 > &v) const
Calculate the dot product of 'this' vector with a second vector.
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
EMData * sqrt() const
return square root of current image
float get_value_at(int x, int y, int z) const
Get the pixel density value at coordinates (x,y,z).
void div(float f)
make each pixel value divided by a float number.
#define EmptyContainerException(desc)
#define FileAccessException(filename)
#define NullPointerException(desc)
double dot(const Vector3 &w, const Vector3 &v)
boost::multi_array< int, 3 > MIArray3D
void printvector(gsl_vector *M, const int n, const string &message="")
void printmatrix(gsl_matrix *M, const int n, const int m, const string &message="")
const int good_fft_sizes[]