source: issm/trunk-jpl/src/m/miscellaneous/MatlabFuncs.js@ 26308

Last change on this file since 26308 was 26308, checked in by jdquinn, 4 years ago

CHG: MATLAB -> JS

File size: 9.7 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,
12 class
13
14TODO:
15- Implement,
16 sort
17 unique
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) {//{{{
38 for (let i = 0; i < A.length; ++i) {
39 if (A[i] !== 0) {
40 return true;
41 }
42 }
43 return false;
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) {//{{{
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;
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) {//{{{
84 console.log(X);
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) {//{{{
102 throw new Error(msg);
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) {//{{{
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 }
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) {//{{{
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 }
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) {//{{{
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 }
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) {//{{{
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;
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) {//{{{
242 return A.length;
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) {//{{{
261 return Math.max.apply(null, A);
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) {//{{{
280 return Math.min.apply(null, A);
281} //}}}/*
282
283/**
284 * FUNCTION sum - Sum of elements of an array
285 *
286 * Replicates behavior of MATLAB's 'sum' function
287 *
288 * Usage:
289 * S = sum(A)
290 *
291 * Sources:
292 * - https://www.mathworks.com/help/matlab/ref/sum.html
293 *
294 * TODO:
295 * - Implement support for multidimensional arrays
296 * - Implement 'nanflag' parameter
297 */
298function sum(A) {//{{{
299 return ArraySum(A);
300} //}}}/*
301
302/**
303 * FUNCTION ones - Create array of all ones
304 *
305 * Replicates behavior of MATLAB's 'ones' function
306 *
307 * Sources:
308 * - https://www.mathworks.com/help/matlab/ref/ones.html
309 *
310 * NOTE:
311 * - Current behavior is to initialize and return a 1D array of length 'size'
312 *
313 * TODO:
314 * - Implement actual behavior of MATLAB's 'ones' function
315 */
316function ones(size) {//{{{
317 return NewArrayFill(size, 1);
318} //}}}
319
320/**
321 * FUNCTION size - Array size
322 *
323 * Replicates behavior of MATLAB's 'size' function
324 *
325 * Usage:
326 * sz = size(A);
327 * szdim = size(A, dim);
328 *
329 * Sources:
330 * - https://www.mathworks.com/help/matlab/ref/size.html
331 *
332 * NOTE:
333 * - Not all functionality is implemented
334 * - In a 2D array, if length of all columns are not equal, unexpected behavior
335 * may result from subsequent processing based on the values returned from this
336 * function.
337 */
338function size(A, dim) {//{{{
339 let nargs = arguments.length;
340 if (nargs == 0) {
341 error('size: at least one argument is required');
342 } else if (nargs == 1) {
343 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
344 return [1, 1];
345 } else if (A.constructor != Array) {
346 error('size: A argument must be a number or an Array');
347 }
348 if (typeof(A) == 'undefined' || isNaN(A)) {
349 return [0];
350 } else if (A.length && A[0].constructor == Array) {
351 return [A.length, A[0].length];
352 } else {
353 return [A.length];
354 }
355 } else if (nargs == 2) {
356 if (typeof(dim) != 'number' || dim < 0 || dim > 1) {
357 error('size: dim argument must be a number between 0 and 1, inclusive');
358 }
359 if (dim == 0) {
360 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
361 return 1;
362 } else {
363 return A.length;
364 }
365 } else {
366 if (typeof(A) == 'number') { // scalar numbers are of size [1, 1]
367 return 1;
368 } else if (typeof(A) == 'undefined' || isNaN(A)) {
369 return 0;
370 } else if (A[0].constructor != Array) {
371 error('size: A[0] is not an Array');
372 }
373 return A[0].length;
374 }
375 } else {
376 error('size: functionality for more than 2 arguments is not currently implemented');
377 }
378} //}}}
379
380/**
381 * FUNCTION strcmpi - Compare strings (case insensitive)
382 *
383 * Replicates behavior of MATLAB's 'strcmpi' function
384 *
385 * Sources:
386 * - https://www.mathworks.com/help/matlab/ref/strcmpi.html
387 */
388function strcmpi(s1, s2) {//{{{
389 return s1.toLowerCase() == s2.toLowerCase();
390} //}}}
391
392/**
393 * FUNCTION sum - Sum of array elements
394 *
395 * Replicates behavior of MATLAB's 'sum' function
396 *
397 * Sum(A) returns the sum of the elements of A along the first array dimension
398 * whose size does not equal 1.
399 * - If A is a 1D array, then sum(A) returns the sum of the elements
400 *
401 * Usage:
402 * S = sum(A)
403 *
404 * Sources:
405 * - https://www.mathworks.com/help/matlab/ref/sum.html
406 *
407 * NOTE:
408 * - Not all functionality is implemented
409 */
410function sum(A) {//{{{
411 return A.reduce((x, y) => x + y);
412} //}}}
413
414/**
415 * FUNCTION zeros - Create array of all zeros
416 *
417 * Replicates behavior of MATLAB's 'zeros' function
418 *
419 * Sources:
420 * - https://www.mathworks.com/help/matlab/ref/zeros.html
421 *
422 * NOTE:
423 * - Current behavior is to initialize and return a 1D array of length 'size'
424 *
425 * TODO:
426 * - Implement actual behavior of MATLAB's 'ones' function
427 */
428function zeros(size, size2) {//{{{
429 if (size2 !== undefined) {
430 return NewArrayFill2D(size, size2, 0);
431 } else {
432 return NewArrayFill(size, 0);
433 }
434} //}}}
Note: See TracBrowser for help on using the repository browser.