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