1 /****************************************************************************** 2 3 # # # #### #### # # ##### # # 4 ## ## # # # # # # # # # # 5 # ## # # #### # # # ##### ###### 6 # # # # # # # # # ### # # 7 # # # # # # # # # # # ### # # 8 # # # #### #### ###### # ##### ### # # 9 10 ******************************************************************************/ 11 /* This file is part of MAPMAKER 3.0b, Copyright 1987-1992, Whitehead Institute 12 for Biomedical Research. All rights reserved. See READ.ME for license. */ 13 14 /***** GETTING THE TIME (code is in syscode.c) *****/ 15 /* DO NOT use the time() system function alone, it's not portable! */ 16 17 char *time_string(); /* no args; returns NULL if it fails */ 18 real usertime(); /* args: bool do_reset; */ 19 /* returns time in seconds, or -1.0 if it fails: do_reset makes it work like 20 a stopwatch (it starts counting when lib_init is executed) */ 21 22 /***** SUBPROCESSES (code is in syscode.c) *****/ 23 24 /* These both return FALSE on failure, TRUE if OK */ 25 bool shell_command(); /* arg: char *command; defined in system.h */ 26 bool subshell(); /* no args. returns T/F */ 27 28 29 /***** GET/SET DIRECTORY (code is in syscode.c) *****/ 30 31 /* These both also return FALSE on failure, TRUE if OK */ 32 bool get_directory(); /* args: char *str; str>=PATH_LENGTH+1 chars */ 33 bool change_directory(); /* args: char *str; str is directory name */ 34 35 36 /***** SORT OPERATIONS FOR SIMPLE ARRAYS (code is in mathlib.c) *****/ 37 38 /* effective declarations: 39 void isort(); args: int *data; int array_len; 40 void lsort(); args: long *data; int array_len; 41 void rsort(); args: real *data; int array_len; 42 void ssort(); args: real *data; int array_len; STRINGS, NOT SHORTS! 43 void inv_isort(); args: int *data; int array_len; ascending order 44 void inv_rsort(); well, figure it out! */ 45 46 47 #define isort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(int),icomp); 48 #define lsort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(long),lcomp); 49 #define rsort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(real),rcomp); 50 #define ssort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(char*),scomp); 51 #define inv_isort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(int),inv_icomp); 52 #define inv_rsort(p,n) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(real),inv_rcomp); 53 54 int icomp(), lcomp(), scomp(), rcomp(), inv_icomp(), inv_rcomp(); 55 56 /* For arrays of pointers to things (eg: structs) use: 57 void psort(); args: <type> *data; int array_len; <type>; int comp_func(); 58 59 Comp_func() will be passed two ptrs to ptrs to <type>s: you should 60 derefernce these carefully (as shown below) for portability. For 61 Example, here we sort an array of ptrs to FOOs on the field "key": 62 Note that FOOs could have variable sizes (only the pointers to them 63 are getting shuffled). 64 65 \* Make and sort an array of pointers to FOOs *\ 66 typedef struct { int key; ... } FOO; 67 FOO **data; 68 array(data, length, FOO*); 69 for (i=0; i<length; i++) single(data[i], FOO); \* or use parray() *\ 70 ... 71 int compare_foos(p1,p2) 72 PSORT_COMPARE_TYPE(FOO) **p1, **p2; \* ptrs to ptrs to FOOs *\ 73 { 74 int key1, key2; 75 key1=(*(FOO**)p1)->key; 76 key2=(*(FOO**)p2)->key; 77 78 if (key1 < key2) return(-1); 79 else if (key1==key2) return(0); 80 else return(1); 81 } 82 ... 83 psort(data, 100, FOO, compare_foos); 84 85 You may want to look in system.h to see how these work in order to write 86 your own sorting routines. */ 87 88 #define psort(p,n,t,f) qsort(QSORT_CAST(p),(QSORT_LENGTH)n,sizeof(t*),f) 89 #define PSORT_COMPARE_TYPE(t) QSORT_COMPARE_PTR_TO(t) 90 91 92 /***** OTHER USEFUL OPERATIONS FOR REAL NUMBER ARRAYS *****/ 93 94 real rmean(); /* real *data; int array_len; returns mean */ 95 real rmaxin(); /* real *data; int array_len; returns largest */ 96 real rminin(); /* real *data; int array_len; returns smallest */ 97 real rmedian(); /* real *data; int array_len; returns median */ 98 real rmiddle(); /* real *data; int array_len; returns middle entry */ 99 void rcopy(); /* real *to, *from; int array_len; copies array */ 100 101 /* This one needs work */ 102 bool rhistogram(); /* real *data; int array_len, min_num_buckets;... */ 103 /* real foo, bar; (unused) - returns T or F */ 104 105 /***** AND MATRICIES... *****/ 106 107 void mat_invert(); /* args: real **m; int size; real **m_inverse; */ 108 /* Invert square matrix m by Gauss' method, side-effecting the 109 (already allocated!) matrix m_inverse. m_inverse should be 2*size 110 columns (2nd index) by size rows (first index) - its left square will be 111 left with the result (ignore the right side). */ 112 113 void mat_mult(); /* args: real **m, **m2; int size; real **m_times_m2; */ 114 /* Multiply square matricies m and m2, side-effecting the (already allocated!) 115 matrix m_times_m2, which should be the same size as m and m2 */ 116 117 void array_times_matrix(); /* real *a, **m; int rows, columns; real *result; */ 118 /* Multiply array a (length=rows) times matrix b (indicies=[row][column]), 119 side effecting the (already allocated!) array c (length=columns). */ 120 121 122 /***** SUPPORT FOR SOME GENERALLY USEFUL MATH FUNCTIONS *****/ 123 124 typedef struct { 125 int entries; 126 real start, increment, mean; 127 real *deviation, *prob; /* [entries], prob is cumulative */ 128 } DISTRIBUTION; 129 130 DISTRIBUTION *make_distribution(); 131 /* args: real increment,limit,mean,(*function)(); 132 Makes a probability distribution for d= mean-limit to mean+limit by 133 increment, where function(d) is the probability of deviation d from mean. */ 134 135 DISTRIBUTION *make_normal_distribution(); 136 /* args: real mu, sigma, increment, limit; 137 Makes a normal probability distribution with sigma about mean mu, where 138 increment and limit are both expressed as fractions of sigma (eg std 139 deviations, unlike the case in make_distribution()) */ 140 141 real pick_from_distribution(); /* args: DISTRIBUTION *dist; real *prob; 142 Return a randomly chosen deviation d from the distribution, and optionally 143 set *prob (if non-NULL) to d's CUMULATIVE probability. */ 144 145