1 /*
  2     Copyright 2008-2021
  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          * They 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          * Computes the cotangent of x.
543          * @function
544          * @param {Number} x The number the cotangent will be calculated of.
545          * @returns {Number} Cotangent of the given value.
546          */
547         cot: function (x) {
548             return 1 / Math.tan(x);
549         },
550 
551         /**
552          * Computes the inverse cotangent of x.
553          * @param {Number} x The number the inverse cotangent will be calculated of.
554          * @returns {Number} Inverse cotangent of the given value.
555          */
556         acot: function (x) {
557             return ((x >= 0) ? (0.5) : (-0.5)) * Math.PI - Math.atan(x);
558         },
559 
560         /**
561          * Compute n-th real root of a real number. n must be strictly positive integer.
562          * If n is odd, the real n-th root exists and is negative.
563          * For n even, for negative valuees of x NaN is returned
564          * @param  {Number} x radicand. Must be non-negative, if n even.
565          * @param  {Number} n index of the root. must be strictly positive integer.
566          * @returns {Number} returns real root or NaN
567          *
568          * @example
569          * nthroot(16, 4): 2
570          * nthroot(-27, 3): -3
571          * nthroot(-4, 2): NaN
572          */
573         nthroot: function(x, n) {
574             var inv = 1 / n;
575 
576             if (n <= 0 || Math.floor(n) !== n) {
577                 return NaN;
578             }
579 
580             if (x === 0.0) {
581                 return 0.0;
582             }
583 
584             if (x > 0) {
585                 return Math.exp(inv * Math.log(x));
586             }
587 
588             // From here on, x is negative
589             if (n % 2 === 1) {
590                 return -Math.exp(inv * Math.log(-x));
591             }
592 
593             // x negative, even root
594             return NaN;
595         },
596 
597         /**
598          * Computes cube root of real number
599          * Polyfill for Math.cbrt().
600          *
601          * @function
602          * @param  {Number} x Radicand
603          * @returns {Number} Cube root of x.
604          */
605         cbrt: Math.cbrt || function(x) {
606             return this.nthroot(x, 3);
607         },
608 
609         /**
610          * Compute base to the power of exponent.
611          * @param {Number} base
612          * @param {Number} exponent
613          * @returns {Number} base to the power of exponent.
614          */
615         pow: function (base, exponent) {
616             if (base === 0) {
617                 if (exponent === 0) {
618                     return 1;
619                 }
620                 return 0;
621             }
622 
623             // exponent is an integer
624             if (Math.floor(exponent) === exponent) {
625                 return Math.pow(base, exponent);
626             }
627 
628             // exponent is not an integer
629             if (base > 0) {
630                 return Math.exp(exponent * Math.log(base));
631             }
632 
633             return NaN;
634         },
635 
636         /**
637          * Compute base to the power of the rational exponent m / n.
638          * This function first reduces the fraction m/n and then computes
639          * JXG.Math.pow(base, m/n).
640          *
641          * This function is necessary to have the same results for e.g.
642          * (-8)^(1/3) = (-8)^(2/6) = -2
643          * @param {Number} base
644          * @param {Number} m numerator of exponent
645          * @param {Number} n denominator of exponent
646          * @returns {Number} base to the power of exponent.
647          */
648         ratpow: function(base, m, n) {
649             var g;
650             if (m === 0) {
651                 return 1;
652             }
653             if (n === 0) {
654                 return NaN;
655             }
656 
657             g = this.gcd(m, n);
658             return this.nthroot(this.pow(base, m / g), n / g);
659         },
660 
661         /**
662          * Logarithm to base 10.
663          * @param {Number} x
664          * @returns {Number} log10(x) Logarithm of x to base 10.
665          */
666         log10: function (x) {
667             return Math.log(x) / Math.log(10.0);
668         },
669 
670         /**
671          * Logarithm to base 2.
672          * @param {Number} x
673          * @returns {Number} log2(x) Logarithm of x to base 2.
674          */
675         log2: function (x) {
676             return Math.log(x) / Math.log(2.0);
677         },
678 
679         /**
680          * Logarithm to arbitrary base b. If b is not given, natural log is taken, i.e. b = e.
681          * @param {Number} x
682          * @param {Number} b base
683          * @returns {Number} log(x, b) Logarithm of x to base b, that is log(x)/log(b).
684          */
685         log: function (x, b) {
686             if (b !== undefined && Type.isNumber(b)) {
687                 return Math.log(x) / Math.log(b);
688             }
689 
690             return Math.log(x);
691         },
692 
693         /**
694          * The sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
695          *
696          * @function
697          * @param  {Number} x A Number
698          * @returns {[type]}  This function has 5 kinds of return values,
699          *    1, -1, 0, -0, NaN, which represent "positive number", "negative number", "positive zero", "negative zero"
700          *    and NaN respectively.
701          */
702         sign: Math.sign || function(x) {
703             x = +x; // convert to a number
704             if (x === 0 || isNaN(x)) {
705                 return x;
706             }
707             return x > 0 ? 1 : -1;
708         },
709 
710         /**
711          * A square & multiply algorithm to compute base to the power of exponent.
712          * Implementated by Wolfgang Riedl.
713          *
714          * @param {Number} base
715          * @param {Number} exponent
716          * @returns {Number} Base to the power of exponent
717          */
718         squampow: function (base, exponent) {
719             var result;
720 
721             if (Math.floor(exponent) === exponent) {
722                 // exponent is integer (could be zero)
723                 result = 1;
724 
725                 if (exponent < 0) {
726                     // invert: base
727                     base = 1.0 / base;
728                     exponent *= -1;
729                 }
730 
731                 while (exponent !== 0) {
732                     if (exponent & 1) {
733                         result *= base;
734                     }
735 
736                     exponent >>= 1;
737                     base *= base;
738                 }
739                 return result;
740             }
741 
742             return this.pow(base, exponent);
743         },
744 
745         /**
746          * Greatest common divisor (gcd) of two numbers.
747          * @see <a href="http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript">rosettacode.org</a>
748          *
749          * @param  {Number} a First number
750          * @param  {Number} b Second number
751          * @returns {Number}   gcd(a, b) if a and b are numbers, NaN else.
752          */
753         gcd: function (a,b) {
754             a = Math.abs(a);
755             b = Math.abs(b);
756 
757             if (!(Type.isNumber(a) && Type.isNumber(b))) {
758                 return NaN;
759             }
760             if (b > a) {
761                 var temp = a;
762                 a = b;
763                 b = temp;
764             }
765 
766             while (true) {
767                 a %= b;
768                 if (a === 0) { return b; }
769                 b %= a;
770                 if (b === 0) { return a; }
771             }
772         },
773 
774         /**
775          * Least common multiple (lcm) of two numbers.
776          *
777          * @param  {Number} a First number
778          * @param  {Number} b Second number
779          * @returns {Number}   lcm(a, b) if a and b are numbers, NaN else.
780          */
781         lcm: function (a,b) {
782             var ret;
783 
784             if (!(Type.isNumber(a) && Type.isNumber(b))) {
785                 return NaN;
786             }
787 
788             ret = a * b;
789             if (ret !== 0) {
790                 return ret / this.gcd(a, b);
791             }
792 
793             return 0;
794         },
795 
796         /**
797          * Normalize the standard form [c, b0, b1, a, k, r, q0, q1].
798          * @private
799          * @param {Array} stdform The standard form to be normalized.
800          * @returns {Array} The normalized standard form.
801          */
802         normalize: function (stdform) {
803             var n, signr,
804                 a2 = 2 * stdform[3],
805                 r = stdform[4] / a2;
806 
807             stdform[5] = r;
808             stdform[6] = -stdform[1] / a2;
809             stdform[7] = -stdform[2] / a2;
810 
811             if (!isFinite(r)) {
812                 n = Math.sqrt(stdform[1] * stdform[1] + stdform[2] * stdform[2]);
813 
814                 stdform[0] /= n;
815                 stdform[1] /= n;
816                 stdform[2] /= n;
817                 stdform[3] = 0;
818                 stdform[4] = 1;
819             } else if (Math.abs(r) >= 1) {
820                 stdform[0] = (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) / (2 * r);
821                 stdform[1] = -stdform[6] / r;
822                 stdform[2] = -stdform[7] / r;
823                 stdform[3] = 1 / (2 * r);
824                 stdform[4] = 1;
825             } else {
826                 signr = (r <= 0 ? -1 : 1);
827                 stdform[0] = signr * (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) * 0.5;
828                 stdform[1] = -signr * stdform[6];
829                 stdform[2] = -signr * stdform[7];
830                 stdform[3] = signr / 2;
831                 stdform[4] = signr * r;
832             }
833 
834             return stdform;
835         },
836 
837         /**
838          * Converts a two dimensional array to a one dimensional Float32Array that can be processed by WebGL.
839          * @param {Array} m A matrix in a two dimensional array.
840          * @returns {Float32Array} A one dimensional array containing the matrix in column wise notation. Provides a fall
841          * back to the default JavaScript Array if Float32Array is not available.
842          */
843         toGL: function (m) {
844             var v, i, j;
845 
846             if (typeof Float32Array === 'function') {
847                 v = new Float32Array(16);
848             } else {
849                 v = new Array(16);
850             }
851 
852             if (m.length !== 4 && m[0].length !== 4) {
853                 return v;
854             }
855 
856             for (i = 0; i < 4; i++) {
857                 for (j = 0; j < 4; j++) {
858                     v[i + 4 * j] = m[i][j];
859                 }
860             }
861 
862             return v;
863         }
864     };
865 
866     return JXG.Math;
867 });
868