source: issm/trunk/src/m/miscellaneous/MatlabFuncs.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: 14.1 KB
RevLine 
[26308]1/*
2A collection of functions that replicate the behavior of MATLAB built-in
3functions of the same, respective name.
4
5Where possible, users are encouraged to use native and/or the most efficient
6methods in JavaScript, but we provide these functions as a way to make
7translations from the MATLAB to the JavaScript ISSM API more seamless.
8
9NOTE:
10- We cannot implement the following MATLAB built-in functions by name as their
11names are reserved keywords in JavaScript,
[26310]12 class
[26308]13
14TODO:
15- Implement,
[26310]16 sort
17 unique
[26308]18
19Sources:
20- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords
21*/
22
23/**
24 * FUNCTION any - Determine if any array elements are nonzero
25 *
26 * Replicates behavior of MATLAB's 'any' function.
27 *
28 * Usage:
29 * B = any(A);
30 *
31 * Sources:
32 * - https://www.mathworks.com/help/matlab/ref/any.html
33 *
34 * NOTE:
35 * - Only basic functionality is implemented
36 */
37function any(A) {//{{{
[26310]38 for (let i = 0; i < A.length; ++i) {
39 if (A[i] !== 0) {
40 return true;
41 }
42 }
43 return false;
[26308]44} //}}}
45
46/**
47 * FUNCTION diff - Differences and approximate derivatives
48 *
49 * Replicates behavior of MATLAB's 'diff' function.
50 *
51 * Y = diff(X) calculates differences between adjacent elements of X.
52 *
53 * Usage:
54 * Y = diff(X);
55 *
56 * Sources:
57 * - https://www.mathworks.com/help/matlab/ref/diff.html
58 *
59 * NOTE:
60 * - Not all functionality is implemented
61 */
62function diff(X) {//{{{
[26310]63 let diffs = [];
64 for (let i = 0; i < (X.length - 1); ++i) {
65 diffs[i] = X[i + 1] - X[i];
66 }
67 return diffs;
[26308]68} //}}}
69
70/**
71 * FUNCTION disp - Display value of variable
72 *
73 * Replicates behavior of MATLAB's 'disp' function.
74 *
75 * Output is logged to console.
76 *
77 * Usage:
78 * disp(X);
79 *
80 * Sources:
81 * - https://www.mathworks.com/help/matlab/ref/disp.html
82 */
83function disp(X) {//{{{
[26310]84 console.log(X);
[26308]85} //}}}
86
87/**
88 * FUNCTION error - Throw error and display message
89 *
90 * Replicates behavior of MATLAB's 'error' function
91 *
92 * Usage:
93 * error(msg);
94 *
95 * Sources:
96 * - https://www.mathworks.com/help/matlab/ref/error.html
97 *
98 * NOTE:
99 * - Only basic functionality is implemented
100 */
101function error(msg) {//{{{
[26310]102 throw new Error(msg);
[26308]103} //}}}
104
105/**
106 * FUNCTION find - Find indices and values of nonzero elements
107 *
108 * Replicates behavior of MATLAB's 'find' function
109 *
110 * Sources:
111 * - https://www.mathworks.com/help/matlab/ref/find.html
112 *
113 * TODO:
114 * - Implement support for multidimensional arrays
115 * - Implement 'n' parameter
116 * - Implement 'direction' parameter
117 */
118function find(X, n, direction) {//{{{
[26310]119 if (typeof(X[0]) == 'number') {
120 let indices = [];
121 for (let i = 0; i < X.length; ++i) {
122 if (X[i] != 0) {
123 indices.push(i);
124 }
125 }
126 return indices;
127 } else { //TODO: If 2d array, assume return rows & cols - try to find a way to not always return rows/cols
128 let rowindices = [];
129 let colindices = [];
130 for (let i = 0; i < X.length; ++i) {
131 for (let j = 0; j < X[i].length; ++j) {
132 if (X[i][j] != 0) {
133 rowindices.push(i);
134 colindices.push(j);
135 }
136 }
137 }
138 return [rowindices, colindices];
139 }
[26308]140} //}}}
141
142/**
143 * FUNCTION isempty - Determine whether array is empty
144 *
145 * Replicates behavior of MATLAB's 'isempty' function
146 *
147 * Sources:
148 * - https://www.mathworks.com/help/matlab/ref/isempty.html
149 *
150 * TODO:
151 * - Implement support for multidimensional arrays
152 */
153function isempty(A) {//{{{
[26310]154 //NOTE: Expanded for clarity/debugging. Can reduce later.
155 if (A === undefined) {
156 return 1; //TODO: Fix this: for now, treat undefined as empty
157 } else {
158 return A.length == 0;
159 }
[26308]160} //}}}
161
162/**
163 * FUNCTION ismember - Array elements that are members of set array
164 *
165 * Replicates basic behavior of MATLAB's 'ismember' function with an important
166 * modification: because of the way we use ismember under MATLAB (to determine
167 * if *any* element of A is in B) and because the truthiness of an empty array
168 * in JavaScript is true, we return 0 if the A is not in B.
169 *
170 * Sources:
171 * - https://www.mathworks.com/help/matlab/ref/double.ismember.html
172 *
173 * TODO:
174 * - Implement support for multidimensional arrays
175 */
176function ismember(A, B) {//{{{
[26310]177 let b = B;
178 if (typeof(B) == 'number' || typeof(B) == 'string') {
179 b = [B];
180 }
181 let indices = zeros(A.length);
182 for (let i = 0; i < A.length; ++i) {
183 for (let j = 0; j < b.length; ++j) {
184 if (A[i] === b[j]) {
185 indices[i] = 1;
186 }
187 }
188 }
189 if (indices.length) {
190 return indices;
191 } else {
192 return 0;
193 }
[26308]194} //}}}
195
196/**
197 * FUNCTION isnan - Determine which array elements are NaN
198 *
199 * Replicates behavior of MATLAB's 'isnan' function
200 *
201 * Usage:
202 * TF = isnan(A)
203 *
204 * Sources:
205 * - https://www.mathworks.com/help/matlab/ref/isnan.html
206 * - https://medium.com/coding-in-simple-english/how-to-check-for-nan-in-javascript-4294e555b447
207 *
208 * NOTE:
209 * - Not to be confused with JavaScript built-in function isNaN
210 * - Not to be confused with function Number.isNaN
211 */
212function isnan(A) {//{{{
[26310]213 if (A.constructor !== Array) {
214 error('isnan: argument must be an array')
215 }
216 is_nan = [];
217 for (let i = 0; i < A.length; ++i) {
218 if (Number.isNaN(A[i])) {
219 is_nan.push(1);
220 } else {
221 is_nan.push(0);
222 }
223 }
224 return is_nan;
[26308]225} //}}}/*
226
227/**
228 * FUNCTION length - Length of largest array dimension
229 *
230 * Replicates behavior of MATLAB's 'length' function
231 *
232 * Usage:
233 * L = length(X)
234 *
235 * Sources:
236 * - https://www.mathworks.com/help/matlab/ref/length.html
237 *
238 * TODO:
239 * - Implement support for multidimensional arrays
240 */
241function length(A) {//{{{
[26310]242 return A.length;
[26308]243} //}}}/*
244
245/**
246 * FUNCTION max - Maximum elements of an array
247 *
248 * Replicates behavior of MATLAB's 'max' function
249 *
250 * Usage:
251 * M = max(A)
252 *
253 * Sources:
254 * - https://www.mathworks.com/help/matlab/ref/max.html
255 *
256 * TODO:
257 * - Implement support for multidimensional arrays
258 * - Implement 'nanflag' parameter
259 */
260function max(A) {//{{{
[26310]261 return Math.max.apply(null, A);
[26308]262} //}}}/*
263
264/**
265 * FUNCTION min - Minimum elements of an array
266 *
267 * Replicates behavior of MATLAB's 'min' function
268 *
269 * Usage:
270 * M = min(A)
271 *
272 * Sources:
273 * - https://www.mathworks.com/help/matlab/ref/min.html
274 *
275 * TODO:
276 * - Implement support for multidimensional arrays
277 * - Implement 'nanflag' parameter
278 */
279function min(A) {//{{{
[26310]280 return Math.min.apply(null, A);
[26308]281} //}}}/*
282
283/**
[26310]284 * FUNCTION ones - Create array of all ones
[26308]285 *
[26310]286 * Replicates behavior of MATLAB's 'zeros' function
287 *
[26308]288 * Usage:
[26310]289 * X = ones
290 * X = ones(n)
291 * X = ones(sz1,...,szN)
292 * X = ones(sz)
[26308]293 *
294 * Sources:
295 * - https://www.mathworks.com/help/matlab/ref/ones.html
296 *
297 * TODO:
[26310]298 * - Create lower-level function to handle both ones and zeros functions as
299 * they are essentially the same
300 * - Implement functionality for more than 2 dimensions
[26308]301 */
[26310]302function ones() {//{{{
303 nargs = arguments.length;
304 if (nargs == 0) {
305 return 1;
306 } else if (nargs == 1) {
307 let arg = arguments[0];
308 if (typeof(arg) == 'number') {
309 return NewArrayFill(arg, 1);
310 } else if (arg.constructor == Array) {
311 return ones(...arg); // spread array of sizes
312 } else {
313 error('ones: functionality for greater than 2 dimensions is not currently implemented')
314 }
315 } else if (nargs == 2) {
316 return NewArrayFill2D(arguments[0], arguments[1], 1);
317 } else {
318 error('ones: functionality for greater than 2 dimensions is not currently implemented')
319 }
[26308]320} //}}}
321
322/**
323 * FUNCTION size - Array size
324 *
325 * Replicates behavior of MATLAB's 'size' function
326 *
327 * Usage:
328 * sz = size(A);
329 * szdim = size(A, dim);
330 *
331 * Sources:
332 * - https://www.mathworks.com/help/matlab/ref/size.html
333 *
334 * NOTE:
335 * - Not all functionality is implemented
336 * - In a 2D array, if length of all columns are not equal, unexpected behavior
337 * may result from subsequent processing based on the values returned from this
338 * function.
339 */
340function size(A, dim) {//{{{
[26310]341 let nargs = arguments.length;
342 if (nargs == 0) {
343 error('size: at least one argument is required');
344 } else if (nargs == 1) {
345 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
346 return [1, 1];
347 } else if (A.constructor != Array) {
348 error('size: A argument must be a number or an Array');
349 }
350 if (typeof(A) == 'undefined' || isNaN(A)) {
351 return [0];
352 } else if (A.length && A[0].constructor == Array) {
353 return [A.length, A[0].length];
354 } else {
355 return [A.length];
356 }
357 } else if (nargs == 2) {
358 if (typeof(dim) != 'number' || dim < 0 || dim > 1) {
359 error('size: dim argument must be a number between 0 and 1, inclusive');
360 }
361 if (dim == 0) {
362 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
363 return 1;
364 } else {
365 return A.length;
366 }
367 } else {
368 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
369 return 1;
370 } else if (typeof(A) == 'undefined' || isNaN(A)) {
371 return 0;
372 } else if (A[0].constructor != Array) {
373 error('size: A[0] is not an Array');
374 }
375 return A[0].length;
376 }
377 } else {
378 error('size: functionality for more than 2 arguments is not currently implemented');
379 }
[26308]380} //}}}
381
382/**
383 * FUNCTION strcmpi - Compare strings (case insensitive)
384 *
385 * Replicates behavior of MATLAB's 'strcmpi' function
386 *
387 * Sources:
388 * - https://www.mathworks.com/help/matlab/ref/strcmpi.html
389 */
390function strcmpi(s1, s2) {//{{{
[26310]391 return s1.toLowerCase() == s2.toLowerCase();
[26308]392} //}}}
393
394/**
[26310]395 * FUNCTION sum - Sum of elements of an array
[26308]396 *
397 * Replicates behavior of MATLAB's 'sum' function
398 *
399 * Usage:
400 * S = sum(A)
[26310]401 *
[26308]402 * Sources:
403 * - https://www.mathworks.com/help/matlab/ref/sum.html
404 *
[26310]405 * TODO:
406 * - Implement support for multidimensional arrays
407 * - Implement 'nanflag' parameter
[26308]408 */
409function sum(A) {//{{{
[26310]410 return ArraySum(A);
[26308]411} //}}}
412
413/**
414 * FUNCTION zeros - Create array of all zeros
415 *
416 * Replicates behavior of MATLAB's 'zeros' function
[26310]417 *
418 * Usage:
419 * X = zeros
420 * X = zeros(n)
421 * X = zeros(sz1,...,szN)
422 * X = zeros(sz)
[26308]423 *
424 * Sources:
425 * - https://www.mathworks.com/help/matlab/ref/zeros.html
426 *
427 * TODO:
[26310]428 * - Create lower-level function to handle both ones and zeros functions as
429 * they are essentially the same
430 * - Implement functionality for more than 2 dimensions
[26308]431 */
[26310]432function zeros() {//{{{
433 nargs = arguments.length;
434 if (nargs == 0) {
435 return 0;
436 } else if (nargs == 1) {
437 let arg = arguments[0];
438 if (typeof(arg) == 'number') {
439 return NewArrayFill(arg, 0);
440 } else if (arg.constructor == Array) {
441 return zeros(...arg); // spread array of sizes
442 } else {
443 error('zeros: functionality for greater than 2 dimensions is not currently implemented')
444 }
445 } else if (nargs == 2) {
446 return NewArrayFill2D(arguments[0], arguments[1], 0);
447 } else {
448 error('zeros: functionality for greater than 2 dimensions is not currently implemented')
449 }
[26308]450} //}}}
[26310]451
452/**
453 * FUNCTIONS sin, cos, tan, asin, acos, atan2 - trig functions that work with radians
454 *
455 * Replicates behavior of MATLAB's trig functions
456 *
457 * Sources:
458 * - https://www.mathworks.com/help/matlab/trigonometry.html
459 *
460 */
461function sin(X) {//{{{
462 let result = NewArrayFill(size, X.length);
463 for (let i = 0; i < X.length; ++i) {
464 result[i] = Math.sin(X[i]);
465 }
466 return result;
467} //}}}
468
469function cos(X) {//{{{
470 let result = NewArrayFill(size, X.length);
471 for (let i = 0; i < X.length; ++i) {
472 result[i] = Math.cos(X[i]);
473 }
474 return result;
475} //}}}
476
477function tan(X) {//{{{
478 let result = NewArrayFill(size, X.length);
479 for (let i = 0; i < X.length; ++i) {
480 result[i] = Math.tan(X[i]);
481 }
482 return result;
483} //}}}
484
485function asin(X) {//{{{
486 let result = NewArrayFill(size, X.length);
487 for (let i = 0; i < X.length; ++i) {
488 result[i] = Math.asin(X[i]);
489 }
490 return result;
491} //}}}
492
493function acos(X) {//{{{
494 let result = NewArrayFill(size, X.length);
495 for (let i = 0; i < X.length; ++i) {
496 result[i] = Math.acos(X[i]);
497 }
498 return result;
499} //}}}
500
501// TODO: Test that the arguments do not need to be reversed, as they are in MATLAB and Python
502function atan2(X, Y) {//{{{
503 let result = NewArrayFill(size, X.length);
504 for (let i = 0; i < X.length; ++i) {
505 result[i] = Math.atan2(X[i], Y[i]);
506 }
507 return result;
508} //}}}
509
510/**
511 * FUNCTIONS sind, cosd, tand, asind, acosd, atan2d - trig functions that work with degrees
512 *
513 * Replicates behavior of MATLAB's trig functions
514 *
515 * Sources:
516 * - https://www.mathworks.com/help/matlab/trigonometry.html
517 *
518 */
519function sind(X) {//{{{
520 let result = NewArrayFill(size, X.length);
521 for (let i = 0; i < X.length; ++i) {
522 result[i] = Math.sin(X[i] * DEG2RAD);
523 }
524 return result;
525} //}}}
526
527function cosd(X) {//{{{
528 let result = NewArrayFill(size, X.length);
529 for (let i = 0; i < X.length; ++i) {
530 result[i] = Math.cos(X[i] * DEG2RAD);
531 }
532 return result;
533} //}}}
534
535function tand(X) {//{{{
536 let result = NewArrayFill(size, X.length);
537 for (let i = 0; i < X.length; ++i) {
538 result[i] = Math.tan(X[i] * DEG2RAD);
539 }
540 return result;
541} //}}}
542
543function asind(X) {//{{{
544 let result = NewArrayFill(size, X.length);
545 for (let i = 0; i < X.length; ++i) {
546 result[i] = RAD2DEG * Math.asin(X[i]);
547 }
548 return result;
549} //}}}
550
551function acosd(X) {//{{{
552 let result = NewArrayFill(size, X.length);
553 for (let i = 0; i < X.length; ++i) {
554 result[i] = RAD2DEG * Math.acos(X[i]);
555 }
556 return result;
557} //}}}
558
559// TODO: Test that the arguments do not need to be reversed, as they are in MATLAB and Python
560function atan2d(X, Y) {//{{{
561 let result = NewArrayFill(size, X.length);
562 for (let i = 0; i < X.length; ++i) {
563 result[i] = RAD2DEG * Math.atan2(X[i], Y[i]);
564 }
565 return result;
566} //}}}
Note: See TracBrowser for help on using the repository browser.