-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCArrayUtilities.m
More file actions
323 lines (214 loc) · 7.42 KB
/
Copy pathBCArrayUtilities.m
File metadata and controls
323 lines (214 loc) · 7.42 KB
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
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
//
// BCArrayUtilities.c
// Xynk
//
// Created by Tom Houpt on 13/5/27.
//
//
// 14/10/11 -- converted summations to Compensated Kahan Summation
#include <stdio.h>
#import "BCArrayUtilities.h"
#define square(x) ((x)*(x))
// NOTE: make sure that zero length arrays are handled properly...
double sumOfArray(double *theArray, NSInteger arrayCount) {
double sum = 0.0;
double correction = 0.0, corrected_next_term = 0.0, new_sum = 0.0;
NSInteger i;
if (0 < arrayCount) {
for (i=0; i< arrayCount;i++) {
corrected_next_term = theArray[i] - correction;
new_sum = sum + corrected_next_term;
correction = (new_sum - sum) - corrected_next_term;
sum = new_sum;
}
}
return sum;
}
double productOfArray(double *theArray, NSInteger arrayCount) {
double product = 1.0;
NSInteger i;
for (i=0; i< arrayCount;i++) {
product*= theArray[i];
}
return product;
}
double meanOfArray(double *theArray, NSInteger arrayCount) {
double mean = 0.0;
mean = sumOfArray(theArray,arrayCount);
mean /= (double)arrayCount;
return mean;
}
double sumOfSquaredDeviationsOfArray(double *theArray, NSInteger arrayCount) {
// sum across array (theArray[i] - theMean)^2
double sumSquared = 0.0;
double correction = 0.0, corrected_next_term = 0.0, new_sum = 0.0;
double theMean = meanOfArray(theArray,arrayCount);
NSInteger i;
// for (i=0; i< arrayCount;i++) {
//
// sumSquared += square(theArray[i] - theMean);
// }
if (0 < arrayCount) {
for (i=0; i< arrayCount;i++) {
corrected_next_term = square(theArray[0] - theMean) - correction;
new_sum = sumSquared + corrected_next_term;
correction = (new_sum - sumSquared) - corrected_next_term;
sumSquared = new_sum;
}
}
return sumSquared;
}
// -------------------------------------------------------------------------------------
NSInteger sumOfIntArray(NSInteger *theArray, NSInteger arrayCount) {
NSInteger sum = 0;
NSInteger i;
for (i=0; i< arrayCount;i++) {
sum+= theArray[i];
}
return sum;
}
NSInteger productOfIntArray(NSInteger *theArray, NSInteger arrayCount) {
NSInteger product = 1;
NSInteger i;
for (i=0; i< arrayCount;i++) {
product*= theArray[i];
}
return product;
}
double meanOfIntArray(NSInteger *theArray, NSInteger arrayCount) {
double mean = 0.0;
mean = sumOfIntArray(theArray,arrayCount);
mean /= (double)arrayCount;
return mean;
}
// -------------------------------------------------------------------------------------
double sumOfNSArray(NSArray<NSNumber *> *theArray) {
double sum = 0.0;
double correction = 0.0, corrected_next_term = 0.0, new_sum = 0.0;
NSUInteger i;
NSUInteger arrayCount = [theArray count];
// for (i=0; i< arrayCount;i++) {
// sum+= [[theArray objectAtIndex:i] doubleValue];
// }
if (0 < arrayCount) {
for (i=0; i< arrayCount;i++) {
corrected_next_term = [[theArray objectAtIndex:i] doubleValue] - correction;
new_sum = sum + corrected_next_term;
correction = (new_sum - sum) - corrected_next_term;
sum = new_sum;
}
}
return sum;
}
double productOfNSArray(NSArray<NSNumber *> *theArray) {
double product = 1.0;
NSUInteger i;
NSUInteger arrayCount = [theArray count];
for (i=0; i< arrayCount;i++) {
product*= [[theArray objectAtIndex:i] doubleValue];
}
return product;
}
double meanOfNSArray(NSArray<NSNumber *> *theArray) {
// TODO: change this from naive to incremental calculation of mean
double mean = 0.0;
NSUInteger arrayCount = [theArray count];
mean = sumOfNSArray(theArray);
mean /= (double)arrayCount;
return mean;
}
double sumOfSquaredDeviationsOfNSArray(NSArray<NSNumber *> *theArray) {
double theMean = meanOfNSArray(theArray);
return sumOfSquaredDeviationsOfNSArrayFromMean(theArray,theMean);
}
double sumOfSquaredDeviationsOfNSArrayFromMean(NSArray<NSNumber *> *theArray,double theMean) {
// sum across array (theArray[i] - theMean)^2
double sum = 0.0;
double squared_difference = 0;
double compensation = 0.0,corrected_next_term = 0.0,total=0.0;
NSUInteger arrayCount = [theArray count];
// naive:
// for (i=0; i< arrayCount;i++) {
// sumSquared += square([[theArray objectAtIndex:i] doubleValue] - theMean);
// }
if (0 < arrayCount) {
for (NSInteger i=0; i< arrayCount;i++) {
squared_difference = square([[theArray objectAtIndex:i] doubleValue] - theMean);
corrected_next_term = squared_difference - compensation;
total = sum + corrected_next_term;
compensation = (total - sum) - corrected_next_term;
sum = total;
}
}
return sum;
}
/** return the overall mean of an NSarray of NSArrays of NSNumbers
TODO: https://en.wikipedia.org/wiki/Kahan_summation_algorithm
or just incremental averaging
@param theGroups an NSArray of NSArray's of NSNumbers
@return the mean of the all the array elements as a double
*/
double populationMeanOfNSArrayOfGroups(NSArray<NSArray *> *theGroups) {
NSInteger count = 0;
double sum = 0;
for (NSArray *group in theGroups) {
for (NSNumber *observation in group) {
sum+= [observation doubleValue];
count++;
}
}
return (sum / count);
}
/** calculate the deviation (𝛕) of the given group from the given population mean
@param populationMean the mean value of entire experiment (ie. all groups)
@param theGroup one of the groups within the experiment
@return the deviation (𝛕) of theGroup from the given populationMean
*/
double deviationFromPopulationMeanOfNSArray(double populationMean,NSArray<NSNumber *> *theGroup) {
// TODO: make this incremental ?
// TODO: check that this is correct deviation
double deviation = 0;
NSInteger count = 0;
for (NSNumber *observation in theGroup) {
deviation+= fabs((populationMean - [observation doubleValue]));
count++;
}
deviation /= count;
return deviation;
}
// MIDMEAN
// mean of values between 25th and 75th percentile
// includes 25th and 75th percentile
#undef square
void MoveObjectToEndOfNSArray(id theObject,NSMutableArray *theArray) {
NSUInteger objectIndex = [theArray indexOfObject: theObject];
if (objectIndex != NSNotFound) {
[theArray removeObjectAtIndex: objectIndex];
}
[theArray addObject: theObject];
}
@implementation NSArray (Reverse)
- (NSArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end
@implementation NSMutableArray (Reverse)
- (void)reverse {
if ([self count] <= 1) {
return;
}
NSUInteger i = 0;
NSUInteger j = [self count] - 1;
while (i < j) {
[self exchangeObjectAtIndex:i
withObjectAtIndex:j];
i++;
j--;
}
}
@end