source: issm/trunk/src/m/array/arrayoperations.js@ 26744

Last change on this file since 26744 was 26744, checked in by Mathieu Morlighem, 3 years ago

merged trunk-jpl and trunk for revision 26742

File size: 29.0 KB
Line 
1function ArrayMax(array){ //{{{
2 //Calculate array max using for loop instead of Math.max.apply(null, array) to avoid recursive stack overflow in mobile browsers
3
4 var max=-Infinity;
5
6 for (var i=0;i<array.length; i++) {
7 max=Math.max(max,array[i]);
8 }
9
10 return max;
11} //}}}
12function ArrayMax2D(array){ //{{{
13
14 var max=-Infinity;
15
16 for (var i=0;i<array.length;i++){
17 var subarray=array[i];
18 max=Math.max(max,ArrayMax(subarray));
19 }
20
21 return max;
22} //}}}
23function ArrayMin(array){ //{{{
24 //Calculate array min using for loop instead of Math.min.apply(null, array) to avoid recursive stack overflow in mobile browsers
25
26 var min=Infinity;
27
28 for (var i=0;i<array.length; i++) {
29 min=Math.min(min,array[i]);
30 }
31
32 return min;
33} //}}}
34function ArrayMin2D(array){ //{{{
35
36 var min=Infinity;
37
38 for (var i=0;i<array.length;i++){
39 var subarray=array[i];
40 min=Math.min(min,ArrayMin(subarray));
41 }
42
43 return min;
44} //}}}
45function ArraySum(array){ //{{{
46 var sum=0;
47 for(var i=0;i<array.length;i++)sum+=array[i];
48 return sum;
49} //}}}
50function ArrayAdd(){ //{{{
51 //Takes in any number of scalars or arrays, and calculates the sum. Scalars are treated as similar length arrays of the scalar.
52 //Determine reference array and size
53 var size, array, arg, initial;
54 for (var a = 0; a < arguments.length; a++) {
55 arg = arguments[a];
56 if (arg instanceof Array || arg instanceof Float64Array) {
57 size = arg.length;
58 array = arg;
59 initial = a;
60 break;
61 }
62 }
63 //check internal consistency of arrays provided!:
64 for (var a = 0; a < arguments.length; a++) {
65 arg = arguments[a];
66 if ((arg instanceof Array || arg instanceof Float64Array) && arg.length != size) {
67 throw Error("ArrayAdd error message: arrays provided as arguments are not of the same length!");
68 } else if (!(arg instanceof Array || arg instanceof Float64Array) && typeof arg != 'number') {
69 throw Error("ArrayAdd error message: arguments provided are not of the type Array or Number!");
70 }
71 }
72 //do the result:
73 var result = array.slice(0);
74 for (var a = 0; a < arguments.length; a++) {
75 if (a != initial) {
76 arg = arguments[a];
77 if (arg instanceof Array || arg instanceof Float64Array) {
78 for(var i = 0; i < result.length; i++){
79 result[i] += arg[i];
80 }
81 } else if (typeof arg === 'number') {
82 for(var i = 0; i < result.length; i++){
83 result[i] += arg;
84 }
85 }
86 }
87 }
88 return result;
89} //}}}
90function ArraySubtract(){ //{{{
91 //Takes in any number of scalars or arrays, and calculates the subtraction. Scalars are treated as similar length arrays of the scalar.
92 //Determine reference array and size
93 var size, array, arg, initial;
94 for (var a = 0; a < arguments.length; a++) {
95 arg = arguments[a];
96 if (arg instanceof Array || arg instanceof Float64Array) {
97 size = arg.length;
98 array = arg;
99 initial = a;
100 break;
101 }
102 }
103 //check internal consistency of arrays provided!:
104 for (var a = 0; a < arguments.length; a++) {
105 arg = arguments[a];
106 if ((arg instanceof Array || arg instanceof Float64Array) && arg.length != size) {
107 throw Error("ArrayAdd error message: arrays provided as arguments are not of the same length!");
108 } else if (!(arg instanceof Array || arg instanceof Float64Array) && typeof arg != 'number') {
109 throw Error("ArrayAdd error message: arguments provided are not of the type Array or Number!");
110 }
111 }
112 //calculate the result, using the first argument to initialize:
113 var result = array.slice(0);
114 for (var a = 0; a < arguments.length; a++) {
115 if (a !== initial) {
116 arg = arguments[a];
117 if (arg instanceof Array || arg instanceof Float64Array) {
118 for(var i = 0; i < result.length; i++){
119 result[i] -= arg[i];
120 }
121 } else if (typeof arg === 'number') {
122 for(var i = 0; i < result.length; i++){
123 result[i] -= arg;
124 }
125 }
126 }
127 }
128 return result;
129} //}}}
130function ArraySubtract2D(){ //{{{
131 //Takes in any number of scalars or arrays, and calculates the subtraction. Scalars are treated as similar length arrays of the scalar.
132 //Determine reference array and size
133 var size, array, arg, initial;
134 for (var a = 0; a < arguments.length; a++) {
135 arg = arguments[a];
136 if (arg instanceof Array || arg instanceof Float64Array) {
137 size = arg.length;
138 array = arg;
139 initial = a;
140 break;
141 }
142 }
143 //check internal consistency of arrays provided!:
144 for (var a = 0; a < arguments.length; a++) {
145 arg = arguments[a];
146 if ((arg instanceof Array || arg instanceof Float64Array) && arg.length != size) {
147 throw Error("ArrayAdd error message: arrays provided as arguments are not of the same length!");
148 } else if (!(arg instanceof Array || arg instanceof Float64Array) && typeof arg != 'number') {
149 throw Error("ArrayAdd error message: arguments provided are not of the type Array or Number!");
150 }
151 }
152 //calculate the result, using the first argument to initialize:
153 var result = [];
154 for (var a = 0; a < arguments.length; a++) {
155 if (a !== initial) {
156 arg = arguments[a];
157 if (arg instanceof Array || arg instanceof Float64Array) {
158 for(var i = 0; i < array.length; i++){
159 result[i] = [];
160 for(var j = 0; j < array[i].length; j++){
161 result[i][j] = array[i][j] - arg[i][j];
162 }
163 }
164 } else if (typeof arg === 'number') {
165 for(var i = 0; i < array.length; i++){
166 result[i] = [];
167 for(var j = 0; j < array[i].length; j++){
168 result[i][j] = array[i][j] - arg;
169 }
170 }
171 }
172 }
173 }
174 return result;
175} //}}}
176function ArrayMultiply(){ //{{{
177 //Takes in any number of scalars or arrays, and calculates the product. Scalars are treated as similar length arrays of the scalar.
178 //Determine reference array and size
179 var size, array, arg, initial;
180 for (var a = 0; a < arguments.length; a++) {
181 arg = arguments[a];
182 if (arg instanceof Array || arg instanceof Float64Array) {
183 size = arg.length;
184 array = arg;
185 initial = a;
186 break;
187 }
188 }
189 //check internal consistency of arrays provided!:
190 for (var a = 0; a < arguments.length; a++) {
191 arg = arguments[a];
192 if ((arg instanceof Array || arg instanceof Float64Array) && arg.length != size) {
193 throw Error("ArrayAdd error message: arrays provided as arguments are not of the same length!");
194 } else if (!(arg instanceof Array || arg instanceof Float64Array) && typeof arg != 'number') {
195 throw Error("ArrayAdd error message: arguments provided are not of the type Array or Number!");
196 }
197 }
198 //do the result:
199 var result = array.slice(0);
200 for (var a = 0; a < arguments.length; a++) {
201 if (a !== initial) {
202 arg = arguments[a];
203 if (arg instanceof Array || arg instanceof Float64Array) {
204 for(var i = 0; i < result.length; i++){
205 result[i] *= arg[i];
206 }
207 } else if (typeof arg === 'number') {
208 for(var i = 0; i < result.length; i++){
209 result[i] *= arg;
210 }
211 }
212 }
213 }
214 return result;
215} //}}}
216function ArrayDivide(){ //{{{
217 //Takes in any number of scalars or arrays, and calculates the quotient. Scalars are treated as similar length arrays of the scalar.
218 //Determine reference array and size
219 var size, array, arg, initial;
220 for (var a = 0; a < arguments.length; a++) {
221 arg = arguments[a];
222 if (arg instanceof Array || arg instanceof Float64Array) {
223 size = arg.length;
224 array = arg;
225 initial = a;
226 break;
227 }
228 }
229 //check internal consistency of arrays provided!:
230 for (var a = 0; a < arguments.length; a++) {
231 arg = arguments[a];
232 if ((arg instanceof Array || arg instanceof Float64Array) && arg.length != size) {
233 throw Error("ArrayAdd error message: arrays provided as arguments are not of the same length!");
234 } else if (!(arg instanceof Array || arg instanceof Float64Array) && typeof arg != 'number') {
235 throw Error("ArrayAdd error message: arguments provided are not of the type Array or Number!");
236 }
237 }
238 //calculate the result, using the first argument to initialize:
239 var result = array.slice(0);
240 for (var a = 0; a < arguments.length; a++) {
241 if (a !== initial) {
242 arg = arguments[a];
243 if (arg instanceof Array || arg instanceof Float64Array) {
244 for(var i = 0; i < result.length; i++){
245 result[i] /= arg[i];
246 }
247 } else if (typeof arg === 'number') {
248 for(var i = 0; i < result.length; i++){
249 result[i] /= arg;
250 }
251 }
252 }
253 }
254 return result;
255} //}}}
256function ArrayMean(array){ //{{{
257 //Calculate the mean:
258 var sum = ArraySum(array);
259 return sum / array.length;
260
261} //}}}
262function ArraySTD(array){ //{{{
263 //Calculate the standard deviation:
264 var sum = ArraySum(array);
265 var differences = 0;
266 for(var i = 0; i < array.length; i++){
267 differences += (array[i] - sum) ** 2;
268 }
269 var variance = differences / array.length;
270 return Math.sqrt(variance);
271
272} //}}}
273function ArrayLerp(array1, array2, weight1, weight2, alpha){ //{{{
274 //Returns linear combination of array1 and array2, based on lerp coefficients weight1, weight2, and alpha.
275 //Parameters:
276 // array1 - array
277 // array2 - array
278 // weight1 - value associated with array1.
279 // weight2 - value associated with array2
280 // alpha - value associated with output array
281 //Output:
282 // result - linear combination of array1 and array2 based on the equation array1 * alpha +
283
284 var range = weight2 - weight1;
285 var normalizedAlpha = (alpha - weight1) / range;
286 var result = array1.slice();
287 for(var i = 0; i < array1.length; i++){
288 result[i] = array1[i] * normalizedAlpha + array2[i] * (1 - normalizedAlpha);
289 }
290 return result;
291
292} //}}}
293function ArrayTranspose(array){ //{{{
294 //Transposes 2d array.
295 var rows = array.length;
296 var cols = array[0].length;
297 var result = Create2DArray(cols, rows);
298
299 for(var i = 0; i < rows; i++) {
300 for(var j = 0; j < cols; j++) {
301 result[j][i] = array[i][j];
302 }
303 }
304 return result;
305
306} //}}}
307function ArrayXPY(){ //{{{
308 if (arguments.length<2)throw Error("ArrayXPY error message: sum has to be for at least two arrays!");
309
310 //check internal consistency of arrays provided!:
311 var firstarray=arguments[0];
312 var firstsize=firstarray.length;
313
314 for(var a=1;a<arguments.length;a++){
315 var array=arguments[a];
316 if(array.length!=firstsize)throw Error("ArrayXPY error message: arrays provided as arguments are not of the same length!");
317 }
318
319 //do the sum:
320 var sum=NewArrayFill(firstsize,0);
321 for(var a=0;a<arguments.length;a++){
322 var array=arguments[a];
323 for(var i=0;i<array.length;i++){
324 sum[i]+=array[i];
325 }
326 }
327 return sum;
328
329} //}}}
330function ArrayConcat(a, b) { //{{{
331 // Make sure that both typed arrays are of the same type
332 if(Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) {
333 throw 'The types of the two arguments passed for parameters a and b do not match.';
334 }
335 var c;
336 if(a instanceof Float64Array) {
337 c = new a.constructor(a.length + b.length);
338 c.set(a);
339 c.set(b, a.length);
340 }
341 else {
342 c = clone(a).concat(clone(b));
343 }
344 return c;
345} //}}}
346function ArrayCol(matrix, cols) { //{{{
347 var columns = [];
348 if (cols instanceof Array || cols instanceof Float64Array) {
349 for (var i = 0; i < matrix.length; i++){
350 var col = [];
351 for (var j = 0; j < cols.length; j++){
352 col.push(matrix[i][cols[j]]);
353 }
354 columns.push(col);
355 }
356
357 } else if (typeof cols == 'number') {
358 for (var i = 0; i < matrix.length; i++){
359 columns.push(matrix[i][cols]);
360 }
361 } else {
362 throw new Error("ArrayCol error: cols must be a single integer or an array with 2 integers!");
363 }
364 return columns;
365} //}}}
366function ListToMatrix(list, elementsPerSubArray) { //{{{
367 var matrix = [], i, k;
368
369 for (i = 0, k = -1; i < list.length; i++) {
370 if (i % elementsPerSubArray === 0) {
371 k++;
372 matrix[k] = [];
373 }
374
375 matrix[k].push(list[i]);
376 }
377
378 return matrix;
379} //}}}
380function MatrixToList(matrixin) { //{{{
381
382 var matrix=matrixin;
383
384 if (!IsArray(matrix[0])) return matrix;
385 else{
386 var width = matrix[0].length;
387 var length = matrix.length;
388 var list= new Array(width*length);
389
390 for(var i=0;i<length;i++){
391 for(var j=0;j<width;j++){
392 list[i*width+j]=matrix[i][j];
393 }
394 }
395 return list;
396 }
397} //}}}
398function IsArray(object) { //{{{
399
400 var type=Object.prototype.toString.call( object );
401 if( type === '[object Array]' ) return 1;
402 if( type === '[object Float64Array]' ) return 1;
403 if( type === '[object Float32Array]' ) return 1;
404 if( type === '[object Int32Array]' ) return 1;
405 if( type === '[object Int16Array]' ) return 1;
406 if( type === '[object Uint32Array]' ) return 1;
407 if( type === '[object Uint16Array]' ) return 1;
408 if( type === '[object Uint8Array]' ) return 1;
409 return 0;
410
411} //}}}
412function ArrayNot(array) { //{{{
413
414 var notarray=array.slice();
415 for (var i=0;i<array.length;i++)notarray[i]=-array[i];
416 return notarray;
417} //}}}
418function ArrayFlip(array) { //{{{
419
420 var notarray=array.slice();
421 for (var i=0;i<array.length;i++)notarray[i]=array[i]^1;
422 return notarray;
423} //}}}
424function ArrayCopy(array) { //{{{
425
426 var copy=[];
427 for(var i=0;i<array.length;i++)copy[i]=array[i];
428 return copy;
429} //}}}
430function ArrayPow(array,coefficient) { //{{{
431
432 var powarray=array.slice();
433 for (var i=0;i<array.length;i++)powarray[i]=Math.pow(array[i],coefficient);
434 return powarray;
435} //}}}
436function ArraySqrt(array) { //{{{
437
438 var sqrtarray=array.slice();
439 for (var i=0;i<array.length;i++)sqrtarray[i]=Math.sqrt(array[i]);
440 return sqrtarray;
441} //}}}
442function ArrayScale(array,alpha) { //{{{
443
444 var scalearray=array.slice();
445 for (var i=0;i<array.length;i++)scalearray[i]=array[i]*alpha;
446 return scalearray;
447} //}}}
448function ArrayMag(array1,array2) { //{{{
449
450 var arraymag=NewArrayFill(array1.length,0);
451 for (var i=0;i<array1.length;i++)arraymag[i]=Math.sqrt(Math.pow(array1[i],2)+Math.pow(array2[i],2));
452 return arraymag;
453} //}}}
454function ArrayAnyNaN(array) { //{{{
455
456 if(IsArray(array[0])){
457 for(var i=0;i<array.length;i++){
458 for(var j=0;j<array[0].length;j++){
459 if (isNaN(array[i][j])) return 1;
460 }
461 }
462 }
463 else{
464 for(var i=0;i<array.length;i++){
465 if (isNaN(array[i])) return 1;
466 }
467 }
468 return 0;
469} //}}}
470function ArrayUnique(arr,rows) { //{{{
471 if (arguments.length == 2){
472 if (rows == 'rows') {
473 //See Matlab unique function and https://stackoverflow.com/a/20339709/1905613
474 let equals = (a, b) => JSON.stringify(a) === JSON.stringify(b);
475 let uniques = [];
476 let indexA = [];
477 let indexC = [];
478 let itemsFound = {};;
479 for(let i = 0, l = arr.length; i < l; i++) {
480 let stringified = JSON.stringify(arr[i]);
481 if (typeof(itemsFound[stringified]) != 'undefined') {
482 indexC.push(itemsFound[stringified]);
483 continue;
484 }
485 uniques.push(arr[i]);
486 indexA.push(i);
487 itemsFound[stringified] = uniques.length-1;
488 indexC.push(itemsFound[stringified]);
489 }
490 //assert arr == uniques[indexC,:];
491 for (let i = 0; i < indexC.length; i++) {
492 if (!equals(arr[i], uniques[indexC[i]])) {
493 throw new Error('bad implementation');
494 }
495 }
496 //assert uniques == arr[indexA, :];
497 for (let i = 0; i < indexA.length; i++) {
498 if (!equals(uniques[i], arr[indexA[i]])) {
499 throw new Error('bad implementation');
500 }
501 }
502 let [uniquesSorted, indexInToOut, indexOutToIn] = ArraySortWithIndices(uniques);
503 //indexMapping is the index of the edge in the old array
504 indexCSorted = []; //indexC.length == arr.length
505 //assert uniquesSorted[i,:] = uniques[indexInToOut[i],:]
506 for (let i = 0; i < indexInToOut.length; i++) {
507 if (!equals(uniquesSorted[i], uniques[indexInToOut[i]])) {
508 console.log(i, uniquesSorted[indexInToOut[i]], uniques[i]);
509 throw new Error('bad implementation');
510 }
511 }
512 //assert uniques[i,:] = uniquesSorted[indexOutToIn[i],:]
513 for (let i = 0; i < indexOutToIn.length; i++) {
514 if (!equals(uniques[i], uniquesSorted[indexOutToIn[i]])) {
515 console.log(i, uniques[indexOutToIn[i]], uniquesSorted[i]);
516 throw new Error('bad implementation');
517 }
518 }
519 //GOAL: assert arr[i,:] == uniquesSorted[indexCSorted[i], :]
520 //GIVEN: assert arr[i,:] == uniques[indexC[i],:];
521 //GIVEN: assert uniquesSorted[i,:] = uniques[indexInToOut[i],:]
522 //GIVEN: assert uniques[i,:] = uniquesSorted[indexOutToIn[i],:]
523 //assert uniques[indexC[i],:] == uniquesSorted[indexOutToIn[indexC[i]],:]
524 //assert uniquesSorted[indexCSorted[i],:]; == uniquesSorted[indexOutToIn[indexC[i]],:];
525 for (let i = 0; i < arr.length; i++) {
526 indexCSorted[i] = indexOutToIn[indexC[i]];
527 }
528 for (let i = 0; i < indexC.length; i++) {
529 if (!equals(arr[i], uniquesSorted[indexCSorted[i]])) {
530 console.log(i, arr[i], uniquesSorted[indexCSorted[i]]);
531 throw new Error('bad implementation');
532 }
533 }
534
535 indexASorted = []; //indexA.length == uniques.length
536 //GOAL: uniquesSorted[i, :] == arr[indexASorted[i], :]
537 //GIVEN: assert arr[i,:] == uniques[indexC[i],:];
538 //GIVEN: assert arr[indexA[i],:] == uniques[i,:];
539 //GIVEN: assert uniques[indexInToOut[i],:] == uniquesSorted[i,:]
540 //GIVEN: assert uniques[i,:] = uniquesSorted[indexOutToIn[i],:]
541 //assert uniquesSorted[i,:] == uniques[indexInToOut[i],:]
542 //assert uniques[indexInToOut[i],:] == arr[indexA[indexInToOut[i]],:];
543 //assert indexA[indexInToOut] == indexASorted
544 //indexASorted == indexA[indexMapping[i]]
545 for (let i = 0; i < indexA.length; i++) {
546 indexASorted[i] = indexA[indexInToOut[i]];
547 }
548 //assert uniques == arr[indexA, :];
549 for (let i = 0; i < indexASorted.length; i++) {
550 if (!equals(uniquesSorted[i], arr[indexASorted[i]])) {
551 throw new Error('bad implementation');
552 }
553 }
554 console.log('Good uniques');
555 return [uniquesSorted, indexASorted, indexCSorted];
556 } else {
557 throw new Error('ArrayUnique non "rows" not supported');
558 }
559 } else {
560 return arr.reverse().filter(function (e, i, arr) {
561 return arr.indexOf(e, i+1) === -1;
562 }).reverse();
563 }
564} //}}}
565function ArraySortWithIndices(toSort, sortingFunction) { //{{{
566 //returns the sorted and index such that toSort[index[i]] == sorted[i]
567 let toSortCopy = [];
568 for (var i = 0; i < toSort.length; i++) {
569 toSortCopy[i] = [toSort[i], i];
570 }
571 if (typeof(sortingFunction) == 'undefined') {
572 let numeric2DFunction = function(a, b) {
573 if (a[0][0] == b[0][0]) {
574 return a[0][1] - b[0][1];
575 } else {
576 return a[0][0] - b[0][0];
577 }
578 };
579 sortingFunction = numeric2DFunction;
580 }
581 toSortCopy.sort(sortingFunction);
582 let indicesInToOut = [];
583 let indicesOutToIn = [];
584 let sorted = [];
585 for (var j = 0; j < toSortCopy.length; j++) {
586 indicesInToOut[j] = toSortCopy[j][1];
587 indicesOutToIn[toSortCopy[j][1]] = j;
588 sorted[j] = toSortCopy[j][0];
589 }
590 return [sorted, indicesInToOut, indicesOutToIn];
591} //}}}
592function ArraySort(array,dim) { //{{{
593 let numericFunction = function(a, b) {
594 return a - b;
595 };
596 let numeric2DFunction = function(a, b) {
597 return a[0] - b[0];
598 };
599 if (arguments.length == 2){
600 if (dim == 1) {
601 array.sort(numeric2DFunction);
602 } else if (dim == 2) {
603 for (let i = 0; i < array.length; i++) {
604 array[i].sort(numericFunction);
605 }
606 } else {
607 throw new Error('ArraySort dim > 2 not yet supported')
608 }
609 return array;
610 } else {
611 return array.sort(numericFunction);
612 }
613} //}}}
614function ArrayRange(lower, upper) { //{{{
615
616 var range = upper - lower + 1;
617 return Array.apply(null, Array(range)).map(function (val, ind) {return ind + lower;});
618
619} //}}}
620function ArrayAny(array) { //{{{
621 //Emulates Matlab 'any' function
622
623 for(var i=0;i<array.length;i++){
624 if (array[i]!=0)return 1;
625 }
626 return 0;
627} //}}}
628function ArrayAnyEqual(array,value) { //{{{
629
630 if(!isNaN(value)){
631 for(var i=0;i<array.length;i++){
632 if (array[i]==value)return 1;
633 }
634 }
635 else{
636 for(var i=0;i<array.length;i++){
637 if (isNaN(array[i]))return 1;
638 }
639 }
640 return 0;
641} //}}}
642function ArrayAnyBelowOrEqual(array,value) { //{{{
643
644 for(var i=0;i<array.length;i++){
645 if (array[i]<=value)return 1;
646 }
647 return 0;
648} //}}}
649function ArrayAnyBelowStrict(array,value) { //{{{
650
651 for(var i=0;i<array.length;i++){
652 if (array[i]<value)return 1;
653 }
654 return 0;
655} //}}}
656function ArrayAnyAboveOrEqual(array,value) { //{{{
657
658 for(var i=0;i<array.length;i++){
659 if (array[i]>=value)return 1;
660 }
661 return 0;
662} //}}}
663function ArrayAnyAboveStrict(array,value) { //{{{
664
665 for(var i=0;i<array.length;i++){
666 if (array[i]>value)return 1;
667 }
668 return 0;
669} //}}}
670function ArrayAnd(array1,array2) { //{{{
671 var array = new Array(array1.length);
672 for (var i=0;i<array1.length;i++) {
673 array[i]=array1[i] & array2[i];
674 }
675 return array;
676} //}}}
677function ArrayOr(array1,array2) { //{{{
678 var array = new Array(array1.length);
679 for (var i=0;i<array1.length;i++) {
680 array[i]=array1[i] | array2[i];
681 }
682 return array;
683} //}}}
684function ArrayEqual(array1,array2) { //{{{
685 var array = new Array(array1.length);
686
687 if (typeof(array1[0]) == 'number') {
688 if (typeof(array2) == 'number') {
689 for(var i=0;i<array1.length;i++){
690 array[i] = array1[i] == array2;
691 }
692 } else {
693 for(var i=0;i<array1.length;i++){
694 array[i] = array1[i] == array2[i];
695 }
696 }
697 } else { //provide support for 2d arrays
698 if (typeof(array2) == 'number') {
699 for(var i=0;i<array1.length;i++){
700 array[i] = new Array(array1[i].length);
701 for(var j=0;j<array1[i].length;j++){
702 array[i][j] = array1[i][j] == array2;
703 }
704 }
705 } else {
706 for(var i=0;i<array1.length;i++){
707 array[i] = new Array(array1[i].length);
708 for(var j=0;j<array1[i].length;j++){
709 array[i][j] = array1[i][j] == array2[i][j];
710 }
711 }
712 }
713 }
714 return array;
715} //}}}
716function ArrayLessThan(array1,array2) { //{{{
717 var array = new Array(array1.length);
718
719 if (typeof(array2) == 'number') {
720 for(var i=0;i<array1.length;i++){
721 array[i] = array1[i] < array2;
722 }
723 } else {
724 for(var i=0;i<array1.length;i++){
725 array[i] = array1[i] < array2[i];
726 }
727 }
728 return array;
729} //}}}
730function ArrayGreaterThan(array1,array2) { //{{{
731 var array = new Array(array1.length);
732 if (typeof(array2) == 'number') {
733 for(var i=0;i<array1.length;i++){
734 array[i] = array1[i] > array2;
735 }
736 } else {
737 for(var i=0;i<array1.length;i++){
738 array[i] = array1[i] > array2[i];
739 }
740 }
741 return array;
742} //}}}
743function ArrayLessEqualThan(array1,array2) { //{{{
744 var array = new Array(array1.length);
745 if (typeof(array2) == 'number') {
746 for(var i=0;i<array1.length;i++){
747 array[i] = array1[i] <= array2;
748 }
749 } else {
750 for(var i=0;i<array1.length;i++){
751 array[i] = array1[i] <= array2[i];
752 }
753 }
754 return array;
755} //}}}
756function ArrayGreaterEqualThan(array1,array2) { //{{{
757 var array = new Array(array1.length);
758 if (typeof(array2) == 'number') {
759 for(var i=0;i<array1.length;i++){
760 array[i] = array1[i] >= array2;
761 }
762 } else {
763 for(var i=0;i<array1.length;i++){
764 array[i] = array1[i] >= array2[i];
765 }
766 }
767 return array;
768} //}}}
769function ArrayIsMember(array1,array2) { //{{{
770
771 var array=NewArrayFill(array1.length,0);
772 for (var i=0;i<array1.length;i++){
773 for(var j=0;j<array2.length;j++){
774 if (array1[i] == array2[j]){
775 array[i]=1;
776 break;
777 }
778 }
779 }
780 return array;
781} //}}}
782function NewArrayFill(size,value) { //{{{
783 var array = new Array(size);
784
785 for (var i = 0; i < size; i++) {
786 array[i] = value;
787 }
788
789 return array;
790} //}}}
791function NewArrayFill2D(rows,cols,value) { //{{{
792 var arr=new Array(rows);
793
794 for (var i=0;i<rows;i++) {
795 arr[i] = NewArrayFill(cols,value);
796 }
797
798 return arr;
799} //}}}
800function NewArrayFillIncrement(start,size,increment) { //{{{
801 var array=new Array(size);
802
803 for(var i=0;i<size;i++){
804 array[i]=start+i*increment;
805 }
806
807 return array;
808} //}}}
809function ones(size) { //{{{
810 return NewArrayFill(size,1);
811} //}}}
812function zeros(size) { //{{{
813 return NewArrayFill(size,0);
814} //}}}
815function ArrayFind(array,value) { //{{{
816 var count=0;
817 var indices=[];
818
819 for (var i=0;i<array.length;i++){
820 if(array[i]===value){
821 indices.push(count);
822 }
823 count++;
824 }
825 return indices;
826} //}}}
827function ArrayFind2D(array,value) { //{{{
828 var count=0;
829 var indices=[];
830
831 for (var i=0;i<array.length;i++){
832 for (var j=0;j<array[i].length;j++){
833 if(array[i][j]===value){
834 indices.push(count);
835 }
836 count++;
837 }
838 }
839 return indices;
840} //}}}
841function ArrayFindNot(array,value) { //{{{
842 var count=0;
843 var indices=[];
844
845 for (var i=0;i<array.length;i++){
846 if(array[i]!==value){
847 indices.push(count);
848 }
849 count++;
850 }
851 return indices;
852} //}}}
853function ArrayFindNot2D(array,value) { //{{{
854 var count=0;
855 var indices=[];
856
857 for (var i=0;i<array.length;i++){
858 for (var j=0;j<array[i].length;j++){
859 if(array[i][j]!==value){
860 indices.push(count);
861 }
862 count++;
863 }
864 }
865 return indices;
866} //}}}
867function ArrayIndex(array1,array2,value) { //{{{
868 //Change behavior between get (if no value is provided) to set (if value to set is provided)
869 if (arguments.length == 2){
870 let data = []
871 if (typeof(array2[0]) == 'number') {
872 for (let i=0;i<array2.length;i++){
873 data.push(array1[array2[i]]);
874 }
875 } else {
876 //2d index array
877 for (let i=0;i<array2.length;i++){
878 let data2 = [];
879 for (let j=0;j<array2[i].length;j++){
880 data2.push(array1[array2[i][j]]);
881 }
882 data.push(data2);
883 }
884 }
885 return data;
886 } else {
887 for (var i=0;i<array2.length;i++){
888 array1[array2[i]]=value;
889 }
890 return array1;
891 }
892} //}}}
893function Create2DArray(rows,cols) { //{{{
894 var arr=new Array(rows);
895
896 for (var i=0;i<rows;i++) {
897 arr[i] = new Array(cols);
898 }
899
900 return arr;
901} //}}}
902function MapIsEmpty(map) { //{{{
903 for (var key in map){
904 if(map.hasOwnProperty(key)){
905 return false;
906 }
907 }
908 return true;
909} //}}}
910function clone(obj) {//{{{
911
912 var copy;
913
914 // Handle the 3 simple types, and null or undefined
915 if (null == obj || "object" != typeof obj) return obj;
916
917 // Handle Date
918 if (obj instanceof Date) {
919 copy = new Date();
920 copy.setTime(obj.getTime());
921 return copy;
922 }
923
924 // Handle Array
925 if (obj instanceof Array || arg instanceof Float64Array) {
926 copy = [];
927 for (var i = 0, len = obj.length; i < len; i++) {
928 copy[i] = clone(obj[i]);
929 }
930 return copy;
931 }
932
933 // Handle Object
934 if (obj instanceof Object) {
935 copy = {};
936 for (var attr in obj) {
937 if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
938 }
939 return copy;
940 }
941
942 throw new Error("Unable to copy obj! Its type isn't supported.");
943} //}}}
944function FloatFix(pointer,size) {//{{{
945
946 var buffer=new Float64Array(size);
947 for(var i=0;i<size;i++)buffer[i]=pointer[i];
948 return buffer;
949
950
951} //}}}
952function NullFix(pointer,value) {//{{{
953
954 if(pointer==null)return value;
955 else{
956 //check that the pointer values are not null:
957 if(IsArray(pointer)){
958 if(IsArray(pointer[0])){
959 for(var i=0;i<pointer.length;i++){
960 for(var j=0;j<pointer[0].length;j++){
961 if(pointer[i][j]==null)pointer[i][j]=value;
962 }
963 }
964 }
965 else{
966 for(var i=0;i<pointer.length;i++){
967 if(pointer[i]==null)pointer[i]=value;
968 }
969 }
970 }
971 return pointer;
972 }
973
974} //}}}
975function typedArraySliceSupport() { //{{{
976 //TypedArray compatibility for Safari/IE
977 if (typeof Int8Array !== 'undefined') {
978 if (!Int8Array.prototype.fill) { Int8Array.prototype.fill = Array.prototype.fill; }
979 if (!Int8Array.prototype.slice) { Int8Array.prototype.slice = Array.prototype.slice; }
980 }
981 if (typeof Uint8Array !== 'undefined') {
982 if (!Uint8Array.prototype.fill) { Uint8Array.prototype.fill = Array.prototype.fill; }
983 if (!Uint8Array.prototype.slice) { Uint8Array.prototype.slice = Array.prototype.slice; }
984 }
985 if (typeof Uint8ClampedArray !== 'undefined') {
986 if (!Uint8ClampedArray.prototype.fill) { Uint8ClampedArray.prototype.fill = Array.prototype.fill; }
987 if (!Uint8ClampedArray.prototype.slice) { Uint8ClampedArray.prototype.slice = Array.prototype.slice; }
988 }
989 if (typeof Int16Array !== 'undefined') {
990 if (!Int16Array.prototype.fill) { Int16Array.prototype.fill = Array.prototype.fill; }
991 if (!Int16Array.prototype.slice) { Int16Array.prototype.slice = Array.prototype.slice; }
992 }
993 if (typeof Uint16Array !== 'undefined') {
994 if (!Uint16Array.prototype.fill) { Uint16Array.prototype.fill = Array.prototype.fill; }
995 if (!Uint16Array.prototype.slice) { Uint16Array.prototype.slice = Array.prototype.slice; }
996 }
997 if (typeof Int32Array !== 'undefined') {
998 if (!Int32Array.prototype.fill) { Int32Array.prototype.fill = Array.prototype.fill; }
999 if (!Int32Array.prototype.slice) { Int32Array.prototype.slice = Array.prototype.slice; }
1000 }
1001 if (typeof Uint32Array !== 'undefined') {
1002 if (!Uint32Array.prototype.fill) { Uint32Array.prototype.fill = Array.prototype.fill; }
1003 if (!Uint32Array.prototype.slice) { Uint32Array.prototype.slice = Array.prototype.slice; }
1004 }
1005 if (typeof Float32Array !== 'undefined') {
1006 if (!Float32Array.prototype.fill) { Float32Array.prototype.fill = Array.prototype.fill; }
1007 if (!Float32Array.prototype.slice) { Float32Array.prototype.slice = Array.prototype.slice; }
1008 }
1009 if (typeof Float64Array !== 'undefined') {
1010 if (!Float64Array.prototype.fill) { Float64Array.prototype.fill = Array.prototype.fill; }
1011 if (!Float64Array.prototype.slice) { Float64Array.prototype.slice = Array.prototype.slice; }
1012 }
1013 if (typeof TypedArray !== 'undefined') {
1014 if (!TypedArray.prototype.fill) { TypedArray.prototype.fill = Array.prototype.fill; }
1015 if (!TypedArray.prototype.slice) { TypedArray.prototype.slice = Array.prototype.slice; }
1016 }
1017} //}}}
Note: See TracBrowser for help on using the repository browser.