1 /*
  2     Copyright 2008-2023
  3         Matthias Ehmann,
  4         Michael Gerhaeuser,
  5         Carsten Miller,
  6         Bianca Valentin,
  7         Alfred Wassermann,
  8         Peter Wilfahrt
  9 
 10     This file is part of JSXGraph.
 11 
 12     JSXGraph is free software dual licensed under the GNU LGPL or MIT License.
 13 
 14     You can redistribute it and/or modify it under the terms of the
 15 
 16       * GNU Lesser General Public License as published by
 17         the Free Software Foundation, either version 3 of the License, or
 18         (at your option) any later version
 19       OR
 20       * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT
 21 
 22     JSXGraph is distributed in the hope that it will be useful,
 23     but WITHOUT ANY WARRANTY; without even the implied warranty of
 24     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 25     GNU Lesser General Public License for more details.
 26 
 27     You should have received a copy of the GNU Lesser General Public License and
 28     the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/>
 29     and <https://opensource.org/licenses/MIT/>.
 30  */
 31 
 32 /*global JXG: true, define: true, Float32Array: true */
 33 /*jslint nomen: true, plusplus: true, bitwise: true*/
 34 
 35 /**
 36  * @fileoverview In this file the namespace JXG.Math is defined, which is the base namespace
 37  * for namespaces like JXG.Math.Numerics, JXG.Math.Plot, JXG.Math.Statistics, JXG.Math.Clip etc.
 38  */
 39 import JXG from "../jxg";
 40 import Type from "../utils/type";
 41 
 42 var undef,
 43     /*
 44      * Dynamic programming approach for recursive functions.
 45      * From "Speed up your JavaScript, Part 3" by Nicholas C. Zakas.
 46      * @see JXG.Math.factorial
 47      * @see JXG.Math.binomial
 48      * http://blog.thejit.org/2008/09/05/memoization-in-javascript/
 49      *
 50      * This method is hidden, because it is only used in JXG.Math. If someone wants
 51      * to use it in JSXGraph outside of JXG.Math, it should be moved to jsxgraph.js
 52      */
 53     memoizer = function (f) {
 54         var cache, join;
 55 
 56         if (f.memo) {
 57             return f.memo;
 58         }
 59 
 60         cache = {};
 61         join = Array.prototype.join;
 62 
 63         f.memo = function () {
 64             var key = join.call(arguments);
 65 
 66             // Seems to be a bit faster than "if (a in b)"
 67             return cache[key] !== undef ? cache[key] : (cache[key] = f.apply(this, arguments));
 68         };
 69 
 70         return f.memo;
 71     };
 72 
 73 /**
 74  * Math namespace.
 75  * @namespace
 76  */
 77 JXG.Math = {
 78     /**
 79      * eps defines the closeness to zero. If the absolute value of a given number is smaller
 80      * than eps, it is considered to be equal to zero.
 81      * @type Number
 82      */
 83     eps: 0.000001,
 84 
 85     /**
 86      * Determine the relative difference between two numbers.
 87      * @param  {Number} a First number
 88      * @param  {Number} b Second number
 89      * @returns {Number}  Relative difference between a and b: |a-b| / max(|a|, |b|)
 90      */
 91     relDif: function (a, b) {
 92         var c = Math.abs(a),
 93             d = Math.abs(b);
 94 
 95         d = Math.max(c, d);
 96 
 97         return d === 0.0 ? 0.0 : Math.abs(a - b) / d;
 98     },
 99 
100     /**
101      * The JavaScript implementation of the % operator returns the symmetric modulo.
102      * mod and "%" are both identical if a >= 0 and m >= 0 but the results differ if a or m < 0.
103      * @param {Number} a
104      * @param {Number} m
105      * @returns {Number} Mathematical modulo <tt>a mod m</tt>
106      */
107     mod: function (a, m) {
108         return a - Math.floor(a / m) * m;
109     },
110 
111     /**
112      * Initializes a vector as an array with the coefficients set to the given value resp. zero.
113      * @param {Number} n Length of the vector
114      * @param {Number} [init=0] Initial value for each coefficient
115      * @returns {Array} A <tt>n</tt> times <tt>m</tt>-matrix represented by a
116      * two-dimensional array. The inner arrays hold the columns, the outer array holds the rows.
117      */
118     vector: function (n, init) {
119         var r, i;
120 
121         init = init || 0;
122         r = [];
123 
124         for (i = 0; i < n; i++) {
125             r[i] = init;
126         }
127 
128         return r;
129     },
130 
131     /**
132      * Initializes a matrix as an array of rows with the given value.
133      * @param {Number} n Number of rows
134      * @param {Number} [m=n] Number of columns
135      * @param {Number} [init=0] Initial value for each coefficient
136      * @returns {Array} A <tt>n</tt> times <tt>m</tt>-matrix represented by a
137      * two-dimensional array. The inner arrays hold the columns, the outer array holds the rows.
138      */
139     matrix: function (n, m, init) {
140         var r, i, j;
141 
142         init = init || 0;
143         m = m || n;
144         r = [];
145 
146         for (i = 0; i < n; i++) {
147             r[i] = [];
148 
149             for (j = 0; j < m; j++) {
150                 r[i][j] = init;
151             }
152         }
153 
154         return r;
155     },
156 
157     /**
158      * Generates an identity matrix. If n is a number and m is undefined or not a number, a square matrix is generated,
159      * if n and m are both numbers, an nxm matrix is generated.
160      * @param {Number} n Number of rows
161      * @param {Number} [m=n] Number of columns
162      * @returns {Array} A square matrix of length <tt>n</tt> with all coefficients equal to 0 except a_(i,i), i out of (1, ..., n), if <tt>m</tt> is undefined or not a number
163      * or a <tt>n</tt> times <tt>m</tt>-matrix with a_(i,j) = 0 and a_(i,i) = 1 if m is a number.
164      */
165     identity: function (n, m) {
166         var r, i;
167 
168         if (m === undef && typeof m !== "number") {
169             m = n;
170         }
171 
172         r = this.matrix(n, m);
173 
174         for (i = 0; i < Math.min(n, m); i++) {
175             r[i][i] = 1;
176         }
177 
178         return r;
179     },
180 
181     /**
182      * Generates a 4x4 matrix for 3D to 2D projections.
183      * @param {Number} l Left
184      * @param {Number} r Right
185      * @param {Number} t Top
186      * @param {Number} b Bottom
187      * @param {Number} n Near
188      * @param {Number} f Far
189      * @returns {Array} 4x4 Matrix
190      */
191     frustum: function (l, r, b, t, n, f) {
192         var ret = this.matrix(4, 4);
193 
194         ret[0][0] = (n * 2) / (r - l);
195         ret[0][1] = 0;
196         ret[0][2] = (r + l) / (r - l);
197         ret[0][3] = 0;
198 
199         ret[1][0] = 0;
200         ret[1][1] = (n * 2) / (t - b);
201         ret[1][2] = (t + b) / (t - b);
202         ret[1][3] = 0;
203 
204         ret[2][0] = 0;
205         ret[2][1] = 0;
206         ret[2][2] = -(f + n) / (f - n);
207         ret[2][3] = -(f * n * 2) / (f - n);
208 
209         ret[3][0] = 0;
210         ret[3][1] = 0;
211         ret[3][2] = -1;
212         ret[3][3] = 0;
213 
214         return ret;
215     },
216 
217     /**
218      * Generates a 4x4 matrix for 3D to 2D projections.
219      * @param {Number} fov Field of view in vertical direction, given in rad.
220      * @param {Number} ratio Aspect ratio of the projection plane.
221      * @param {Number} n Near
222      * @param {Number} f Far
223      * @returns {Array} 4x4 Projection Matrix
224      */
225     projection: function (fov, ratio, n, f) {
226         var t = n * Math.tan(fov / 2),
227             r = t * ratio;
228 
229         return this.frustum(-r, r, -t, t, n, f);
230     },
231 
232     /**
233      * Multiplies a vector vec to a matrix mat: mat * vec. The matrix is interpreted by this function as an array of rows. Please note: This
234      * function does not check if the dimensions match.
235      * @param {Array} mat Two dimensional array of numbers. The inner arrays describe the columns, the outer ones the matrix' rows.
236      * @param {Array} vec Array of numbers
237      * @returns {Array} Array of numbers containing the result
238      * @example
239      * var A = [[2, 1],
240      *          [1, 3]],
241      *     b = [4, 5],
242      *     c;
243      * c = JXG.Math.matVecMult(A, b)
244      * // c === [13, 19];
245      */
246     matVecMult: function (mat, vec) {
247         var i,
248             s,
249             k,
250             m = mat.length,
251             n = vec.length,
252             res = [];
253 
254         if (n === 3) {
255             for (i = 0; i < m; i++) {
256                 res[i] = mat[i][0] * vec[0] + mat[i][1] * vec[1] + mat[i][2] * vec[2];
257             }
258         } else {
259             for (i = 0; i < m; i++) {
260                 s = 0;
261                 for (k = 0; k < n; k++) {
262                     s += mat[i][k] * vec[k];
263                 }
264                 res[i] = s;
265             }
266         }
267         return res;
268     },
269 
270     /**
271      * Computes the product of the two matrices mat1*mat2.
272      * @param {Array} mat1 Two dimensional array of numbers
273      * @param {Array} mat2 Two dimensional array of numbers
274      * @returns {Array} Two dimensional Array of numbers containing result
275      */
276     matMatMult: function (mat1, mat2) {
277         var i,
278             j,
279             s,
280             k,
281             m = mat1.length,
282             n = m > 0 ? mat2[0].length : 0,
283             m2 = mat2.length,
284             res = this.matrix(m, n);
285 
286         for (i = 0; i < m; i++) {
287             for (j = 0; j < n; j++) {
288                 s = 0;
289                 for (k = 0; k < m2; k++) {
290                     s += mat1[i][k] * mat2[k][j];
291                 }
292                 res[i][j] = s;
293             }
294         }
295         return res;
296     },
297 
298     /**
299      * Transposes a matrix given as a two dimensional array.
300      * @param {Array} M The matrix to be transposed
301      * @returns {Array} The transpose of M
302      */
303     transpose: function (M) {
304         var MT, i, j, m, n;
305 
306         // number of rows of M
307         m = M.length;
308         // number of columns of M
309         n = M.length > 0 ? M[0].length : 0;
310         MT = this.matrix(n, m);
311 
312         for (i = 0; i < n; i++) {
313             for (j = 0; j < m; j++) {
314                 MT[i][j] = M[j][i];
315             }
316         }
317 
318         return MT;
319     },
320 
321     /**
322      * Compute the inverse of an nxn matrix with Gauss elimination.
323      * @param {Array} Ain
324      * @returns {Array} Inverse matrix of Ain
325      */
326     inverse: function (Ain) {
327         var i,
328             j,
329             k,
330             s,
331             ma,
332             r,
333             swp,
334             n = Ain.length,
335             A = [],
336             p = [],
337             hv = [];
338 
339         for (i = 0; i < n; i++) {
340             A[i] = [];
341             for (j = 0; j < n; j++) {
342                 A[i][j] = Ain[i][j];
343             }
344             p[i] = i;
345         }
346 
347         for (j = 0; j < n; j++) {
348             // pivot search:
349             ma = Math.abs(A[j][j]);
350             r = j;
351 
352             for (i = j + 1; i < n; i++) {
353                 if (Math.abs(A[i][j]) > ma) {
354                     ma = Math.abs(A[i][j]);
355                     r = i;
356                 }
357             }
358 
359             // Singular matrix
360             if (ma <= this.eps) {
361                 return [];
362             }
363 
364             // swap rows:
365             if (r > j) {
366                 for (k = 0; k < n; k++) {
367                     swp = A[j][k];
368                     A[j][k] = A[r][k];
369                     A[r][k] = swp;
370                 }
371 
372                 swp = p[j];
373                 p[j] = p[r];
374                 p[r] = swp;
375             }
376 
377             // transformation:
378             s = 1.0 / A[j][j];
379             for (i = 0; i < n; i++) {
380                 A[i][j] *= s;
381             }
382             A[j][j] = s;
383 
384             for (k = 0; k < n; k++) {
385                 if (k !== j) {
386                     for (i = 0; i < n; i++) {
387                         if (i !== j) {
388                             A[i][k] -= A[i][j] * A[j][k];
389                         }
390                     }
391                     A[j][k] = -s * A[j][k];
392                 }
393             }
394         }
395 
396         // swap columns:
397         for (i = 0; i < n; i++) {
398             for (k = 0; k < n; k++) {
399                 hv[p[k]] = A[i][k];
400             }
401             for (k = 0; k < n; k++) {
402                 A[i][k] = hv[k];
403             }
404         }
405 
406         return A;
407     },
408 
409     /**
410      * Inner product of two vectors a and b. n is the length of the vectors.
411      * @param {Array} a Vector
412      * @param {Array} b Vector
413      * @param {Number} [n] Length of the Vectors. If not given the length of the first vector is taken.
414      * @returns {Number} The inner product of a and b.
415      */
416     innerProduct: function (a, b, n) {
417         var i,
418             s = 0;
419 
420         if (n === undef || !Type.isNumber(n)) {
421             n = a.length;
422         }
423 
424         for (i = 0; i < n; i++) {
425             s += a[i] * b[i];
426         }
427 
428         return s;
429     },
430 
431     /**
432      * Calculates the cross product of two vectors both of length three.
433      * In case of homogeneous coordinates this is either
434      * <ul>
435      * <li>the intersection of two lines</li>
436      * <li>the line through two points</li>
437      * </ul>
438      * @param {Array} c1 Homogeneous coordinates of line or point 1
439      * @param {Array} c2 Homogeneous coordinates of line or point 2
440      * @returns {Array} vector of length 3: homogeneous coordinates of the resulting point / line.
441      */
442     crossProduct: function (c1, c2) {
443         return [
444             c1[1] * c2[2] - c1[2] * c2[1],
445             c1[2] * c2[0] - c1[0] * c2[2],
446             c1[0] * c2[1] - c1[1] * c2[0]
447         ];
448     },
449 
450     /**
451      * Euclidean norm of a vector.
452      *
453      * @param {Array} a Array containing a vector.
454      * @param {Number} n (Optional) length of the array.
455      * @returns {Number} Euclidean norm of the vector.
456      */
457     norm: function (a, n) {
458         var i,
459             sum = 0.0;
460 
461         if (n === undef || !Type.isNumber(n)) {
462             n = a.length;
463         }
464 
465         for (i = 0; i < n; i++) {
466             sum += a[i] * a[i];
467         }
468 
469         return Math.sqrt(sum);
470     },
471 
472     axpy: function (a, x, y) {
473         var i,
474             le = x.length,
475             p = [];
476         for (i = 0; i < le; i++) {
477             p[i] = a * x[i] + y[i];
478         }
479         return p;
480     },
481 
482     /**
483      * Compute the factorial of a positive integer. If a non-integer value
484      * is given, the fraction will be ignored.
485      * @function
486      * @param {Number} n
487      * @returns {Number} n! = n*(n-1)*...*2*1
488      */
489     factorial: memoizer(function (n) {
490         if (n < 0) {
491             return NaN;
492         }
493 
494         n = Math.floor(n);
495 
496         if (n === 0 || n === 1) {
497             return 1;
498         }
499 
500         return n * this.factorial(n - 1);
501     }),
502 
503     /**
504      * Computes the binomial coefficient n over k.
505      * @function
506      * @param {Number} n Fraction will be ignored
507      * @param {Number} k Fraction will be ignored
508      * @returns {Number} The binomial coefficient n over k
509      */
510     binomial: memoizer(function (n, k) {
511         var b, i;
512 
513         if (k > n || k < 0) {
514             return NaN;
515         }
516 
517         k = Math.round(k);
518         n = Math.round(n);
519 
520         if (k === 0 || k === n) {
521             return 1;
522         }
523 
524         b = 1;
525 
526         for (i = 0; i < k; i++) {
527             b *= n - i;
528             b /= i + 1;
529         }
530 
531         return b;
532     }),
533 
534     /**
535      * Calculates the cosine hyperbolicus of x.
536      * @function
537      * @param {Number} x The number the cosine hyperbolicus will be calculated of.
538      * @returns {Number} Cosine hyperbolicus of the given value.
539      */
540     cosh:
541         Math.cosh ||
542         function (x) {
543             return (Math.exp(x) + Math.exp(-x)) * 0.5;
544         },
545 
546     /**
547      * Sine hyperbolicus of x.
548      * @function
549      * @param {Number} x The number the sine hyperbolicus will be calculated of.
550      * @returns {Number} Sine hyperbolicus of the given value.
551      */
552     sinh:
553         Math.sinh ||
554         function (x) {
555             return (Math.exp(x) - Math.exp(-x)) * 0.5;
556         },
557 
558     /**
559      * Hyperbolic arc-cosine of a number.
560      *
561      * @param {Number} x
562      * @returns {Number}
563      */
564     acosh:
565         Math.acosh ||
566         function (x) {
567             return Math.log(x + Math.sqrt(x * x - 1));
568         },
569 
570     /**
571      * Hyperbolic arcsine of a number
572      * @param {Number} x
573      * @returns {Number}
574      */
575     asinh:
576         Math.asinh ||
577         function (x) {
578             if (x === -Infinity) {
579                 return x;
580             }
581             return Math.log(x + Math.sqrt(x * x + 1));
582         },
583 
584     /**
585      * Computes the cotangent of x.
586      * @function
587      * @param {Number} x The number the cotangent will be calculated of.
588      * @returns {Number} Cotangent of the given value.
589      */
590     cot: function (x) {
591         return 1 / Math.tan(x);
592     },
593 
594     /**
595      * Computes the inverse cotangent of x.
596      * @param {Number} x The number the inverse cotangent will be calculated of.
597      * @returns {Number} Inverse cotangent of the given value.
598      */
599     acot: function (x) {
600         return (x >= 0 ? 0.5 : -0.5) * Math.PI - Math.atan(x);
601     },
602 
603     /**
604      * Compute n-th real root of a real number. n must be strictly positive integer.
605      * If n is odd, the real n-th root exists and is negative.
606      * For n even, for negative valuees of x NaN is returned
607      * @param  {Number} x radicand. Must be non-negative, if n even.
608      * @param  {Number} n index of the root. must be strictly positive integer.
609      * @returns {Number} returns real root or NaN
610      *
611      * @example
612      * nthroot(16, 4): 2
613      * nthroot(-27, 3): -3
614      * nthroot(-4, 2): NaN
615      */
616     nthroot: function (x, n) {
617         var inv = 1 / n;
618 
619         if (n <= 0 || Math.floor(n) !== n) {
620             return NaN;
621         }
622 
623         if (x === 0.0) {
624             return 0.0;
625         }
626 
627         if (x > 0) {
628             return Math.exp(inv * Math.log(x));
629         }
630 
631         // From here on, x is negative
632         if (n % 2 === 1) {
633             return -Math.exp(inv * Math.log(-x));
634         }
635 
636         // x negative, even root
637         return NaN;
638     },
639 
640     /**
641      * Computes cube root of real number
642      * Polyfill for Math.cbrt().
643      *
644      * @function
645      * @param  {Number} x Radicand
646      * @returns {Number} Cube root of x.
647      */
648     cbrt:
649         Math.cbrt ||
650         function (x) {
651             return this.nthroot(x, 3);
652         },
653 
654     /**
655      * Compute base to the power of exponent.
656      * @param {Number} base
657      * @param {Number} exponent
658      * @returns {Number} base to the power of exponent.
659      */
660     pow: function (base, exponent) {
661         if (base === 0) {
662             if (exponent === 0) {
663                 return 1;
664             }
665             return 0;
666         }
667 
668         // exponent is an integer
669         if (Math.floor(exponent) === exponent) {
670             return Math.pow(base, exponent);
671         }
672 
673         // exponent is not an integer
674         if (base > 0) {
675             return Math.exp(exponent * Math.log(base));
676         }
677 
678         return NaN;
679     },
680 
681     /**
682      * Compute base to the power of the rational exponent m / n.
683      * This function first reduces the fraction m/n and then computes
684      * JXG.Math.pow(base, m/n).
685      *
686      * This function is necessary to have the same results for e.g.
687      * (-8)^(1/3) = (-8)^(2/6) = -2
688      * @param {Number} base
689      * @param {Number} m numerator of exponent
690      * @param {Number} n denominator of exponent
691      * @returns {Number} base to the power of exponent.
692      */
693     ratpow: function (base, m, n) {
694         var g;
695         if (m === 0) {
696             return 1;
697         }
698         if (n === 0) {
699             return NaN;
700         }
701 
702         g = this.gcd(m, n);
703         return this.nthroot(this.pow(base, m / g), n / g);
704     },
705 
706     /**
707      * Logarithm to base 10.
708      * @param {Number} x
709      * @returns {Number} log10(x) Logarithm of x to base 10.
710      */
711     log10: function (x) {
712         return Math.log(x) / Math.log(10.0);
713     },
714 
715     /**
716      * Logarithm to base 2.
717      * @param {Number} x
718      * @returns {Number} log2(x) Logarithm of x to base 2.
719      */
720     log2: function (x) {
721         return Math.log(x) / Math.log(2.0);
722     },
723 
724     /**
725      * Logarithm to arbitrary base b. If b is not given, natural log is taken, i.e. b = e.
726      * @param {Number} x
727      * @param {Number} b base
728      * @returns {Number} log(x, b) Logarithm of x to base b, that is log(x)/log(b).
729      */
730     log: function (x, b) {
731         if (b !== undefined && Type.isNumber(b)) {
732             return Math.log(x) / Math.log(b);
733         }
734 
735         return Math.log(x);
736     },
737 
738     /**
739      * The sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
740      *
741      * @function
742      * @param  {Number} x A Number
743      * @returns {[type]}  This function has 5 kinds of return values,
744      *    1, -1, 0, -0, NaN, which represent "positive number", "negative number", "positive zero", "negative zero"
745      *    and NaN respectively.
746      */
747     sign:
748         Math.sign ||
749         function (x) {
750             x = +x; // convert to a number
751             if (x === 0 || isNaN(x)) {
752                 return x;
753             }
754             return x > 0 ? 1 : -1;
755         },
756 
757     /**
758      * A square & multiply algorithm to compute base to the power of exponent.
759      * Implementated by Wolfgang Riedl.
760      *
761      * @param {Number} base
762      * @param {Number} exponent
763      * @returns {Number} Base to the power of exponent
764      */
765     squampow: function (base, exponent) {
766         var result;
767 
768         if (Math.floor(exponent) === exponent) {
769             // exponent is integer (could be zero)
770             result = 1;
771 
772             if (exponent < 0) {
773                 // invert: base
774                 base = 1.0 / base;
775                 exponent *= -1;
776             }
777 
778             while (exponent !== 0) {
779                 if (exponent & 1) {
780                     result *= base;
781                 }
782 
783                 exponent >>= 1;
784                 base *= base;
785             }
786             return result;
787         }
788 
789         return this.pow(base, exponent);
790     },
791 
792     /**
793      * Greatest common divisor (gcd) of two numbers.
794      * @see <a href="https://rosettacode.org/wiki/Greatest_common_divisor#JavaScript">rosettacode.org</a>
795      *
796      * @param  {Number} a First number
797      * @param  {Number} b Second number
798      * @returns {Number}   gcd(a, b) if a and b are numbers, NaN else.
799      */
800     gcd: function (a, b) {
801         var tmp,
802             endless = true;
803 
804         a = Math.abs(a);
805         b = Math.abs(b);
806 
807         if (!(Type.isNumber(a) && Type.isNumber(b))) {
808             return NaN;
809         }
810         if (b > a) {
811             tmp = a;
812             a = b;
813             b = tmp;
814         }
815 
816         while (endless) {
817             a %= b;
818             if (a === 0) {
819                 return b;
820             }
821             b %= a;
822             if (b === 0) {
823                 return a;
824             }
825         }
826     },
827 
828     /**
829      * Least common multiple (lcm) of two numbers.
830      *
831      * @param  {Number} a First number
832      * @param  {Number} b Second number
833      * @returns {Number}   lcm(a, b) if a and b are numbers, NaN else.
834      */
835     lcm: function (a, b) {
836         var ret;
837 
838         if (!(Type.isNumber(a) && Type.isNumber(b))) {
839             return NaN;
840         }
841 
842         ret = a * b;
843         if (ret !== 0) {
844             return ret / this.gcd(a, b);
845         }
846 
847         return 0;
848     },
849 
850     /**
851      *  Error function, see {@link https://en.wikipedia.org/wiki/Error_function}.
852      *
853      * @see JXG.Math.PropFunc.erf
854      * @param  {Number} x
855      * @returns {Number}
856      */
857     erf: function (x) {
858         return this.ProbFuncs.erf(x);
859     },
860 
861     /**
862      * Complementary error function, i.e. 1 - erf(x).
863      *
864      * @see JXG.Math.erf
865      * @see JXG.Math.PropFunc.erfc
866      * @param  {Number} x
867      * @returns {Number}
868      */
869     erfc: function (x) {
870         return this.ProbFuncs.erfc(x);
871     },
872 
873     /**
874      * Inverse of error function
875      *
876      * @see JXG.Math.erf
877      * @see JXG.Math.PropFunc.erfi
878      * @param  {Number} x
879      * @returns {Number}
880      */
881     erfi: function (x) {
882         return this.ProbFuncs.erfi(x);
883     },
884 
885     /**
886      * Normal distribution function
887      *
888      * @see JXG.Math.PropFunc.ndtr
889      * @param  {Number} x
890      * @returns {Number}
891      */
892     ndtr: function (x) {
893         return this.ProbFuncs.ndtr(x);
894     },
895 
896     /**
897      * Inverse of normal distribution function
898      *
899      * @see JXG.Math.ndtr
900      * @see JXG.Math.PropFunc.ndtri
901      * @param  {Number} x
902      * @returns {Number}
903      */
904     ndtri: function (x) {
905         return this.ProbFuncs.ndtri(x);
906     },
907 
908     /* ********************  Comparisons and logical operators ************** */
909 
910     /**
911      * Logical test: a < b?
912      *
913      * @param {Number} a
914      * @param {Number} b
915      * @returns {Boolean}
916      */
917     lt: function (a, b) {
918         return a < b;
919     },
920 
921     /**
922      * Logical test: a <= b?
923      *
924      * @param {Number} a
925      * @param {Number} b
926      * @returns {Boolean}
927      */
928     leq: function (a, b) {
929         return a <= b;
930     },
931 
932     /**
933      * Logical test: a > b?
934      *
935      * @param {Number} a
936      * @param {Number} b
937      * @returns {Boolean}
938      */
939     gt: function (a, b) {
940         return a > b;
941     },
942 
943     /**
944      * Logical test: a >= b?
945      *
946      * @param {Number} a
947      * @param {Number} b
948      * @returns {Boolean}
949      */
950     geq: function (a, b) {
951         return a >= b;
952     },
953 
954     /**
955      * Logical test: a === b?
956      *
957      * @param {Number} a
958      * @param {Number} b
959      * @returns {Boolean}
960      */
961     eq: function (a, b) {
962         return a === b;
963     },
964 
965     /**
966      * Logical test: a !== b?
967      *
968      * @param {Number} a
969      * @param {Number} b
970      * @returns {Boolean}
971      */
972     neq: function (a, b) {
973         return a !== b;
974     },
975 
976     /**
977      * Logical operator: a && b?
978      *
979      * @param {Boolean} a
980      * @param {Boolean} b
981      * @returns {Boolean}
982      */
983     and: function (a, b) {
984         return a && b;
985     },
986 
987     /**
988      * Logical operator: !a?
989      *
990      * @param {Boolean} a
991      * @returns {Boolean}
992      */
993     not: function (a) {
994         return !a;
995     },
996 
997     /**
998      * Logical operator: a || b?
999      *
1000      * @param {Boolean} a
1001      * @param {Boolean} b
1002      * @returns {Boolean}
1003      */
1004     or: function (a, b) {
1005         return a || b;
1006     },
1007 
1008     /**
1009      * Logical operator: either a or b?
1010      *
1011      * @param {Boolean} a
1012      * @param {Boolean} b
1013      * @returns {Boolean}
1014      */
1015     xor: function (a, b) {
1016         return (a || b) && !(a && b);
1017     },
1018 
1019     /* *************************** Normalize *************************** */
1020 
1021     /**
1022      * Normalize the standard form [c, b0, b1, a, k, r, q0, q1].
1023      * @private
1024      * @param {Array} stdform The standard form to be normalized.
1025      * @returns {Array} The normalized standard form.
1026      */
1027     normalize: function (stdform) {
1028         var n,
1029             signr,
1030             a2 = 2 * stdform[3],
1031             r = stdform[4] / a2;
1032 
1033         stdform[5] = r;
1034         stdform[6] = -stdform[1] / a2;
1035         stdform[7] = -stdform[2] / a2;
1036 
1037         if (!isFinite(r)) {
1038             n = Math.sqrt(stdform[1] * stdform[1] + stdform[2] * stdform[2]);
1039 
1040             stdform[0] /= n;
1041             stdform[1] /= n;
1042             stdform[2] /= n;
1043             stdform[3] = 0;
1044             stdform[4] = 1;
1045         } else if (Math.abs(r) >= 1) {
1046             stdform[0] = (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) / (2 * r);
1047             stdform[1] = -stdform[6] / r;
1048             stdform[2] = -stdform[7] / r;
1049             stdform[3] = 1 / (2 * r);
1050             stdform[4] = 1;
1051         } else {
1052             signr = r <= 0 ? -1 : 1;
1053             stdform[0] =
1054                 signr * (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) * 0.5;
1055             stdform[1] = -signr * stdform[6];
1056             stdform[2] = -signr * stdform[7];
1057             stdform[3] = signr / 2;
1058             stdform[4] = signr * r;
1059         }
1060 
1061         return stdform;
1062     },
1063 
1064     /**
1065      * Converts a two dimensional array to a one dimensional Float32Array that can be processed by WebGL.
1066      * @param {Array} m A matrix in a two dimensional array.
1067      * @returns {Float32Array} A one dimensional array containing the matrix in column wise notation. Provides a fall
1068      * back to the default JavaScript Array if Float32Array is not available.
1069      */
1070     toGL: function (m) {
1071         var v, i, j;
1072 
1073         if (typeof Float32Array === "function") {
1074             v = new Float32Array(16);
1075         } else {
1076             v = new Array(16);
1077         }
1078 
1079         if (m.length !== 4 && m[0].length !== 4) {
1080             return v;
1081         }
1082 
1083         for (i = 0; i < 4; i++) {
1084             for (j = 0; j < 4; j++) {
1085                 v[i + 4 * j] = m[i][j];
1086             }
1087         }
1088 
1089         return v;
1090     },
1091 
1092     /**
1093      * Theorem of Vieta: Given a set of simple zeroes x_0, ..., x_n
1094      * of a polynomial f, compute the coefficients s_k, (k=0,...,n-1)
1095      * of the polynomial of the form. See {@link https://de.wikipedia.org/wiki/Elementarsymmetrisches_Polynom}.
1096      * <p>
1097      *  f(x) = (x-x_0)*...*(x-x_n) =
1098      *  x^n + sum_{k=1}^{n} (-1)^(k) s_{k-1} x^(n-k)
1099      * </p>
1100      * @param {Array} x Simple zeroes of the polynomial.
1101      * @returns {Array} Coefficients of the polynomial.
1102      *
1103      */
1104     Vieta: function (x) {
1105         var n = x.length,
1106             s = [],
1107             m,
1108             k,
1109             y;
1110 
1111         s = x.slice();
1112         for (m = 1; m < n; ++m) {
1113             y = s[m];
1114             s[m] *= s[m - 1];
1115             for (k = m - 1; k >= 1; --k) {
1116                 s[k] += s[k - 1] * y;
1117             }
1118             s[0] += y;
1119         }
1120         return s;
1121     }
1122 };
1123 
1124 export default JXG.Math;
1125