File: array_arith.c

package info (click to toggle)
staden 2.0.0%2Bb11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 21,556 kB
  • sloc: ansic: 240,603; tcl: 65,360; cpp: 12,854; makefile: 11,201; sh: 2,952; fortran: 2,033; perl: 63; awk: 46
file content (172 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download | duplicates (5)
1
2
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
#include <limits.h>
#include <float.h>
#include <math.h>

#include "misc.h"

/* general array stuff */

void fill_double_array ( double array[], int num_elements, double filler ) {
    int i;

    for (i=0;i<num_elements;i++) {
	array[i] = filler;
    }
}

void fill_int_array ( int array[], int num_elements, int filler ) {
    int i;

    for (i=0;i<num_elements;i++) {
	array[i] = filler;
    }
}

int max_int_array ( int array[], int num_elements ) {
    int i, max = INT_MIN;

    for (i=0;i<num_elements;i++) {
	max = MAX( max, array[i] );
    }
    return max;
}

int min_int_array ( int array[], int num_elements ) {
    int i, min = INT_MAX;

    for (i=0;i<num_elements;i++) {
	min = MIN( min, array[i] );
    }
    return min;
}

double max_double_array ( double array[], int num_elements ) {
    int i;
    double max = -DBL_MAX;

    for (i=0;i<num_elements;i++) {
	max = MAX( max, array[i] );
    }
    return max;
}

double min_double_array ( double array[], int num_elements ) {
    int i;
    double min = DBL_MAX;
    for (i=0;i<num_elements;i++) {
	min = MIN( min, array[i] );
    }
    return min;
}

void reset_zeroes ( double table[], int num_elements, double correction ) {
    int i;
    double small = DBL_EPSILON;

    for ( i=0; i<num_elements; i++ ) {
	if ( table[i] <= small ) table[i] = correction;
    }
}

double sum_double_array ( double array[], int num_elements ) {
    int i;
    double t=0.0;
    for ( i=0; i<num_elements; i++ ) {
	t += array[i];
    }
    return t;
}

void div_double_array ( double array[], int num_elements, double divisor ) {
    int i;
    double small = DBL_EPSILON;

/* sanity check */
    if (( divisor <= small ) && ( divisor >= 0.0 )) return;
    for ( i=0; i<num_elements; i++ ) {
	array[i] /= divisor;
    }
}

void mult_double_array ( double array[], int num_elements, double mult ) {
    int i;

    for ( i=0; i<num_elements; i++ ) {
	array[i] *= mult;

    }
}

void ratio_double_arrays ( double denominator[], double numerator[],
			  int num_elements ) {
  /* return the ratio of the elements in the arrays in denominator */

  int i;

  for(i=0;i<num_elements;i++) {
    if ( numerator[i] > DBL_EPSILON ) denominator[i] /= numerator[i];
  }
}

void scale_double_array ( double array[], int num_elements, double total ) {
    int i;
    double small = DBL_EPSILON;
    double count;

    /* scaling by division */

/* sanity check */
    if ( total <= small ) return;
    for ( i=0,count=0.0; i<num_elements; i++ ) {
	count += array[i];
    }

    if ( count < small ) return;
    count = total/count;
    for ( i=0; i<num_elements; i++ ) {
	array[i] *= count;
    }
}

void scale_double_array1 ( double array[], int num_elements, double total ) {

  /* want sum of array elements to be total, so add them and then subtract
     sum/num_elements from each
     */
  int i;
  double sum, mean;

  for ( i=0, sum=0.0; i<num_elements; i++ ) {
    sum += array[i];
  }
  mean = sum / num_elements;
  for ( i=0, sum=0.0; i<num_elements; i++ ) {
    array[i] -= mean;
  }
}


void log_double_array ( double array[], int num_elements ) {
    int i;

    for ( i=0; i<num_elements; i++ ) {
	if ( array[i] > 0.0 ) array[i] = log ( array[i] );
    }
}

void log10_double_array ( double array[], int num_elements ) {
    int i;

    for ( i=0; i<num_elements; i++ ) {
	if ( array[i] > 0.0 ) array[i] = log10 ( array[i] );
    }
}

void exp_double_array ( double array[], int num_elements ) {
    int i;

    for ( i=0; i<num_elements; i++ ) {
	array[i] = exp ( array[i] );
    }
}