util.c 11.8 KB
Newer Older
tilman.metz's avatar
tilman.metz committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
tilman.metz's avatar
tilman.metz committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
 *
 * This file is part of DENISE.
 * 
 * DENISE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * DENISE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DENISE. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
-----------------------------------------------------------------------------------------*/


#define SHIFT_IND 1
#define FREE_ARGUMENT char *

#include "fd.h"


void err(char err_text[]){
        extern int MYID;

        fprintf(stdout,"Message from PE %d\n",MYID);
        fprintf(stdout,"R U N - T I M E  E R R O R: \n");
        fprintf(stdout,"%s\n",err_text);
        fprintf(stdout,"...now exiting to system.\n");

        MPI_Abort(MPI_COMM_WORLD, 1);
        exit(1);
}

void warning(char warn_text[]){
	/* standard warnings handler */
	fprintf(stdout,"W A R N I N G   M E S S A G E: \n");
	fprintf(stdout,"%s\n",warn_text);
}


double maximum(float **a, int nx, int ny){
	double maxi=0.0;
	int i, j;


	for (j=1;j<=ny;j++)
		for (i=1;i<=nx;i++)
			if (fabs((double) a[i][j])>maxi) maxi=fabs((double)a[i][j]);
	return maxi;
}

float minimum_m(float **mat, int nx, int ny)
{
        float minm;
        int i,j;

        minm = mat[1][1];
        for (i=1;i<=nx;i++)
                for (j=1;j<=ny;j++)
                {
                        if (i*j==1) continue;
                        if (mat[j][i] < minm)
                        {
                                minm = mat[j][i];
                        }
                }
        return minm;
}

float maximum_m(float **mat, int nx, int ny)
{
        float maxm;
        int i,j;

        maxm = mat[1][1];
        for (i=1;i<=nx;i++)
                for (j=1;j<=ny;j++)
                {
                        if (i*j==1) continue;
                        if (mat[j][i] > maxm)
                        {
                                maxm = mat[j][i];
                        }
                }
        return maxm;
}




float *vector(int ni, int nj){
	float *a;
	int k;

	a=(float *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(float)));
	if (!a) err("util.c: allocation failure in function vector()");
	for (k=0;k<(nj-ni+1+SHIFT_IND);k++) a[k]=0.0;
	return a-ni+SHIFT_IND;
}


int *ivector(int ni, int nj){

	int *a;
	int k;

	a=(int *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(int)));
	if (!a) err("util.c: allocation failure in function ivector()");
	for (k=0;k<(nj-ni+1+SHIFT_IND);k++) a[k]=0;
	return a-ni+SHIFT_IND;
}

unsigned short int *usvector(int ni, int nj){

	unsigned short int *a;
	int k;

	a=(unsigned short int *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(unsigned short int)));
	if (!a) err("util.c: allocation failure in function usvector()");
	for (k=0;k<(nj-ni+1+SHIFT_IND);k++) a[k]=0;
	return a-ni+SHIFT_IND;
}


unsigned char *cvector(int ni, int nj){

	unsigned char *a;

	a=(unsigned char *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(unsigned char)));
	if (!a) err("util.c: allocation failure in function cvector()");
	return a-ni+SHIFT_IND;
}


unsigned long *lvector(int ni, int nj){

	unsigned long *a;
	int k;

	a=(unsigned long *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(unsigned long)));
	if (!a) err("util.c: allocation failure in function lvector()");
	for (k=0;k<(nj-ni+1+SHIFT_IND);k++) a[k]=0;
	return a-ni+SHIFT_IND;
}

double *dvector(int ni, int nj){

	double *a;
	int k;

	a=(double *)malloc((size_t) ((nj-ni+1+SHIFT_IND)*sizeof(double)));
	if (!a) err("util.c: allocation failure in function dvector()");
	for (k=0;k<(nj-ni+1+SHIFT_IND);k++) a[k]=0.0;
	return a-ni+SHIFT_IND;
}

float **fmatrix(int mrl, int mrh, int mcl, int mch){

	int k,l, mrow=mrh-mrl+1,mcol=mch-mcl+1;
	float **ma;

	ma=(float **) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(float*)));
	if (!ma) err("util.c: allocation failure 1 in function fmatrix() ");
	ma += SHIFT_IND;
	ma -= mrl;

	ma[mrl]=(float *) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(float)));
	if (!ma[mrl]) err("util.c: allocation failure 2 in function fmatrix() ");
	ma[mrl] += SHIFT_IND;
	ma[mrl] -= mcl;

	for (k=mrl+1;k<=mrh;k++) ma[k]=ma[k-1]+mcol;

	for (k=mrl;k<=mrh;k++)
		for (l=mcl;l<=mch;l++) ma[k][l]=0.0;

	return ma;
}

float **matrix(int mrl, int mrh, int mcl, int mch){

	int k,l, mrow=mrh-mrl+1,mcol=mch-mcl+1;
	float **ma;

	ma=(float **) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(float*)));
	if (!ma) err("util.c: allocation failure 1 in function matrix() ");
	ma += SHIFT_IND;
	ma -= mrl;

	ma[mrl]=(float *) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(float)));
	if (!ma[mrl]) err("util.c: allocation failure 2 in function matrix() ");
	ma[mrl] += SHIFT_IND;
	ma[mrl] -= mcl;

	for (k=mrl+1;k<=mrh;k++) ma[k]=ma[k-1]+mcol;

	for (k=mrl;k<=mrh;k++)
		for (l=mcl;l<=mch;l++) ma[k][l]=0.0;

	return ma;
}


double **dmatrix(int mrl, int mrh, int mcl, int mch){

	int k,l, mrow=mrh-mrl+1,mcol=mch-mcl+1;
	double **ma;

	ma=(double **) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(double*)));
	if (!ma) err("util.c: allocation failure 1 in function matrix() ");
	ma += SHIFT_IND;
	ma -= mrl;

	ma[mrl]=(double *) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(double)));
	if (!ma[mrl]) err("util.c: allocation failure 2 in function dmatrix() ");
	ma[mrl] += SHIFT_IND;
	ma[mrl] -= mcl;

	for (k=mrl+1;k<=mrh;k++) ma[k]=ma[k-1]+mcol;

	for (k=mrl;k<=mrh;k++)
		for (l=mcl;l<=mch;l++) ma[k][l]=0.0;

	return ma;
}

int **imatrix(int mrl, int mrh, int mcl, int mch){

	int k,l, mrow=mrh-mrl+1,mcol=mch-mcl+1;
	int **ma;

	ma=(int **) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(int*)));
	if (!ma) err("util.c: allocation failure 1 in function imatrix() ");
	ma += SHIFT_IND;
	ma -= mrl;

	ma[mrl]=(int *) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(int)));
	if (!ma[mrl]) err("util.c: allocation failure 2 in function imatrix() ");
	ma[mrl] += SHIFT_IND;
	ma[mrl] -= mcl;

	for (k=mrl+1;k<=mrh;k++) ma[k]=ma[k-1]+mcol;

	for (k=mrl;k<=mrh;k++)
		for (l=mcl;l<=mch;l++) ma[k][l]=0;

	return ma;
}

unsigned short int **usmatrix(int mrl, int mrh, int mcl, int mch){

	int k,l, mrow=mrh-mrl+1,mcol=mch-mcl+1;
	unsigned short int **ma;

	ma=(unsigned short int **) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(unsigned short int*)));
	if (!ma) err("util.c: allocation failure 1 in function usmatrix() ");
	ma += SHIFT_IND;
	ma -= mrl;

	ma[mrl]=(unsigned short int *) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(unsigned short int)));
	if (!ma[mrl]) err("util.c: allocation failure 2 in function usmatrix() ");
	ma[mrl] += SHIFT_IND;
	ma[mrl] -= mcl;

	for (k=mrl+1;k<=mrh;k++) ma[k]=ma[k-1]+mcol;

	for (k=mrl;k<=mrh;k++)
		for (l=mcl;l<=mch;l++) ma[k][l]=0;

	return ma;
}

float ***f3tensor(int mrl, int mrh, int mcl, int mch,int mdl, int mdh){

	int w,e,r, mrow=mrh-mrl+1,mcol=mch-mcl+1,mdep=mdh-mdl+1;
	float ***te;

	te=(float ***) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(float**)));
	if (!te) err("util.c: allocation failure 1 in function f3tensor() ");
	te += SHIFT_IND;
	te -= mrl;

	te[mrl]=(float **) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(float*)));
	if (!te[mrl]) err("util.c: allocation failure 2 in function f3tensor() ");
	te[mrl] += SHIFT_IND;
	te[mrl] -= mcl;

	te[mrl][mcl]=(float *) malloc((size_t)((mrow*mcol*mdep+SHIFT_IND)*sizeof(float)));
	if (!te[mrl][mcl]) err("util.c: allocation failure 3 in function f3tensor() ");
	te[mrl][mcl] += SHIFT_IND;
	te[mrl][mcl] -= mdl;

	for (e=mcl+1;e<=mch;e++) te[mrl][e]=te[mrl][e-1]+mdep;
	for (w=mrl+1;w<=mrh;w++){
		te[w]=te[w-1]+mcol;
		te[w][mcl]=te[w-1][mcl]+mcol*mdep;
		for (e=mcl+1;e<=mch;e++) te[w][e]=te[w][e-1]+mdep;
	}

	for (w=mrl;w<=mrh;w++)
		for (e=mcl;e<=mch;e++)
			for (r=mdl;r<=mdh;r++) te[w][e][r]=0.0;

	return te;
}


int ***i3tensor(int mrl, int mrh, int mcl, int mch,int mdl, int mdh){

	int w,e,r, mrow=mrh-mrl+1,mcol=mch-mcl+1,mdep=mdh-mdl+1;
	int ***te;

	te=(int ***) malloc((size_t) ((mrow+SHIFT_IND)*sizeof(int**)));
	if (!te) err("util.c: allocation failure 1 in function i3tensor() ");
	te += SHIFT_IND;
	te -= mrl;

	te[mrl]=(int **) malloc((size_t)((mrow*mcol+SHIFT_IND)*sizeof(int*)));
	if (!te[mrl]) err("util.c: allocation failure 2 in function i3tensor() ");
	te[mrl] += SHIFT_IND;
	te[mrl] -= mcl;

	te[mrl][mcl]=(int *) malloc((size_t)((mrow*mcol*mdep+SHIFT_IND)*sizeof(int)));
	if (!te[mrl][mcl]) err("util.c: allocation failure 3 in function i3tensor() ");
	te[mrl][mcl] += SHIFT_IND;
	te[mrl][mcl] -= mdl;
	
	for (e=mcl+1;e<=mch;e++) te[mrl][e]=te[mrl][e-1]+mdep;
	for (w=mrl+1;w<=mrh;w++){
		te[w]=te[w-1]+mcol;
		te[w][mcl]=te[w-1][mcl]+mcol*mdep;
		for (e=mcl+1;e<=mch;e++) te[w][e]=te[w][e-1]+mdep;
	}

	for (w=mrl;w<=mrh;w++)
		for (e=mcl;e<=mch;e++)
			for (r=mdl;r<=mdh;r++) te[w][e][r]=0.0;
	

	return te;
}


void free_vector(float *a, int ni, int nj){
	free((FREE_ARGUMENT) (a+ni-SHIFT_IND));
}

void free_ivector(int *a, int ni, int nj){
	free((FREE_ARGUMENT) (a+ni-SHIFT_IND));
}

void free_cvector(char *a, int ni, int nj){
	free((FREE_ARGUMENT) (a+ni-SHIFT_IND));
}

void free_dvector(double *a, int ni, int nj){
	free((FREE_ARGUMENT) (a+ni-SHIFT_IND));
        }
                
void free_matrix(float **ma, int mrl, int mrh, int mcl, int mch){
	free((FREE_ARGUMENT) (ma[mrl]+mcl-SHIFT_IND));
	free((FREE_ARGUMENT) (ma+mrl-SHIFT_IND));
}

void free_imatrix(int **ma, int mrl, int mrh, int mcl, int mch){
	free((FREE_ARGUMENT) (ma[mrl]+mcl-SHIFT_IND));
	free((FREE_ARGUMENT) (ma+mrl-SHIFT_IND));
}

void free_usmatrix(unsigned short int **ma, int mrl, int mrh, int mcl, int mch){
	free((FREE_ARGUMENT) (ma[mrl]+mcl-SHIFT_IND));
	free((FREE_ARGUMENT) (ma+mrl-SHIFT_IND));
}

void free_f3tensor(float ***te, int mrl, int mrh, int mcl, int mch, int mdl, int mdh){
	free((FREE_ARGUMENT) (te[mrl][mcl]+mdl-SHIFT_IND));
	free((FREE_ARGUMENT) (te[mrl]+mcl-SHIFT_IND));
	free((FREE_ARGUMENT) (te+mrl-SHIFT_IND));
}

void free_i3tensor(int ***te, int mrl, int mrh, int mcl, int mch, int mdl, int mdh){
	free((FREE_ARGUMENT) (te[mrl][mcl]+mdl-SHIFT_IND));
	free((FREE_ARGUMENT) (te[mrl]+mcl-SHIFT_IND));
	free((FREE_ARGUMENT) (te+mrl-SHIFT_IND));
}

void zero(float *A, int u_max){
	int u=0;
	while(++u<=u_max) *(A++)=0.0;
}

void normalize_data(float **data, int ntr, int ns){

	float *max_min_trace=NULL, *max_min_data_trace=NULL;
	float max, min, tmp;
	int i,j;


	max_min_trace = vector(1,ntr); 
		for(i=1;i<=ntr;i++){

      		max=0.0;
      		min=0.0;

     		for(j=2;j<=ns;j++){
      		/* Looking for max and min */
        	if(data[i][j]>max){max = data[i][j];}
        	if(data[i][j]<min){min = data[i][j];}

		if (max>(fabs(min))){tmp=max;}
		else{tmp=fabs(min);}
        	}
 
		max_min_trace[i]=tmp;	

	/* set maximum_values=0.0 to 1.0 */
	if(max_min_trace[i]==0.0){max_min_trace[i]=1.0;}
   

		for(j=2;j<=ns;j++){
		data[i][j] = data[i][j]/max_min_trace[i];
		}
	
	}
}

/*  quickSort
* This public-domain C implementation by Darel Rex Finley .
* http://alienryderflex.com/quicksort */

void quicksort(float *arr, int dummy, int elements) {

	/* number of levels: 64 is enough for 1.8e19 elements to be sorted */
	#define  MAX_LEVELS  64

	int	beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R, swap ;
	float	piv;

	beg[0]=0;
	end[0]=elements;

	while (i>=0) {
		L=beg[i]; R=end[i]-1;
		if (L<R) {
			piv=arr[L];
			while (L<R) {
				while (arr[R]>=piv && L<R) R--;
				if (L<R) arr[L++]=arr[R];
				while (arr[L]<=piv && L<R) L++;
				if (L<R) arr[R--]=arr[L];
			}
			arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
			if (end[i]-beg[i]>end[i-1]-beg[i-1]) {
			swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap;
			swap=end[i]; end[i]=end[i-1]; end[i-1]=swap;
			}
		}
		else	i--;
	}
}