1 /* 2 Copyright 2008-2024 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.js"; 40 import Type from "../utils/type.js"; 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. Contains mathematics related methods which are 75 * specific to JSXGraph or which extend the JavaScript Math class. 76 * @namespace 77 */ 78 JXG.Math = { 79 /** 80 * eps defines the closeness to zero. If the absolute value of a given number is smaller 81 * than eps, it is considered to be equal to zero. 82 * @type Number 83 */ 84 eps: 0.000001, 85 86 /** 87 * Determine the relative difference between two numbers. 88 * @param {Number} a First number 89 * @param {Number} b Second number 90 * @returns {Number} Relative difference between a and b: |a-b| / max(|a|, |b|) 91 */ 92 relDif: function (a, b) { 93 var c = Math.abs(a), 94 d = Math.abs(b); 95 96 d = Math.max(c, d); 97 98 return d === 0.0 ? 0.0 : Math.abs(a - b) / d; 99 }, 100 101 /** 102 * The JavaScript implementation of the % operator returns the symmetric modulo. 103 * mod and "%" are both identical if a >= 0 and m >= 0 but the results differ if a or m < 0. 104 * @param {Number} a 105 * @param {Number} m 106 * @returns {Number} Mathematical modulo <tt>a mod m</tt> 107 */ 108 mod: function (a, m) { 109 return a - Math.floor(a / m) * m; 110 }, 111 112 /** 113 * Translate <code>x</code> into the interval <code>[a, b)</code> by adding 114 * a multiple of <code>b - a</code>. 115 * @param {Number} x 116 * @param {Number} a 117 * @param {Number} b 118 */ 119 wrap: function (x, a, b) { 120 return a + this.mod(x - a, b - a); 121 }, 122 123 /** 124 * Clamp <code>x</code> within the interval <code>[a, b]</code>. If 125 * <code>x</code> is below <code>a</code>, increase it to <code>a</code>. If 126 * it's above <code>b</code>, decrease it to <code>b</code>. 127 */ 128 clamp: function (x, a, b) { 129 return Math.min(Math.max(x, a), b); 130 }, 131 132 /** 133 * A way of clamping a periodic variable. If <code>x</code> is congruent mod 134 * <code>period</code> to a point in <code>[a, b]</code>, return that point. 135 * Otherwise, wrap it into <code>[mid - period/2, mid + period/2]</code>, 136 * where <code>mid</code> is the mean of <code>a</code> and <code>b</code>, 137 * and then clamp it to <code>[a, b]</code> from there. 138 */ 139 wrapAndClamp: function (x, a, b, period) { 140 var mid = 0.5 * (a + b), 141 half_period = 0.5 * period; 142 143 return this.clamp( 144 this.wrap( 145 x, 146 mid - half_period, 147 mid + half_period 148 ), 149 a, 150 b 151 ); 152 }, 153 154 /** 155 * Initializes a vector of size <tt>n</tt> wih coefficients set to the init value (default 0) 156 * @param {Number} n Length of the vector 157 * @param {Number} [init=0] Initial value for each coefficient 158 * @returns {Array} An array of length <tt>n</tt> 159 */ 160 vector: function (n, init) { 161 var r, i; 162 163 init = init || 0; 164 r = []; 165 166 for (i = 0; i < n; i++) { 167 r[i] = init; 168 } 169 170 return r; 171 }, 172 173 /** 174 * Initializes a matrix as an array of rows with the given value. 175 * @param {Number} n Number of rows 176 * @param {Number} [m=n] Number of columns 177 * @param {Number} [init=0] Initial value for each coefficient 178 * @returns {Array} A <tt>n</tt> times <tt>m</tt>-matrix represented by a 179 * two-dimensional array. The inner arrays hold the columns, the outer array holds the rows. 180 */ 181 matrix: function (n, m, init) { 182 var r, i, j; 183 184 init = init || 0; 185 m = m || n; 186 r = []; 187 188 for (i = 0; i < n; i++) { 189 r[i] = []; 190 191 for (j = 0; j < m; j++) { 192 r[i][j] = init; 193 } 194 } 195 196 return r; 197 }, 198 199 /** 200 * Generates an identity matrix. If n is a number and m is undefined or not a number, a square matrix is generated, 201 * if n and m are both numbers, an nxm matrix is generated. 202 * @param {Number} n Number of rows 203 * @param {Number} [m=n] Number of columns 204 * @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 205 * 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. 206 */ 207 identity: function (n, m) { 208 var r, i; 209 210 if (m === undef && typeof m !== "number") { 211 m = n; 212 } 213 214 r = this.matrix(n, m); 215 216 for (i = 0; i < Math.min(n, m); i++) { 217 r[i][i] = 1; 218 } 219 220 return r; 221 }, 222 223 /** 224 * Generates a 4x4 matrix for 3D to 2D projections. 225 * @param {Number} l Left 226 * @param {Number} r Right 227 * @param {Number} t Top 228 * @param {Number} b Bottom 229 * @param {Number} n Near 230 * @param {Number} f Far 231 * @returns {Array} 4x4 Matrix 232 */ 233 frustum: function (l, r, b, t, n, f) { 234 var ret = this.matrix(4, 4); 235 236 ret[0][0] = (n * 2) / (r - l); 237 ret[0][1] = 0; 238 ret[0][2] = (r + l) / (r - l); 239 ret[0][3] = 0; 240 241 ret[1][0] = 0; 242 ret[1][1] = (n * 2) / (t - b); 243 ret[1][2] = (t + b) / (t - b); 244 ret[1][3] = 0; 245 246 ret[2][0] = 0; 247 ret[2][1] = 0; 248 ret[2][2] = -(f + n) / (f - n); 249 ret[2][3] = -(f * n * 2) / (f - n); 250 251 ret[3][0] = 0; 252 ret[3][1] = 0; 253 ret[3][2] = -1; 254 ret[3][3] = 0; 255 256 return ret; 257 }, 258 259 /** 260 * Generates a 4x4 matrix for 3D to 2D projections. 261 * @param {Number} fov Field of view in vertical direction, given in rad. 262 * @param {Number} ratio Aspect ratio of the projection plane. 263 * @param {Number} n Near 264 * @param {Number} f Far 265 * @returns {Array} 4x4 Projection Matrix 266 */ 267 projection: function (fov, ratio, n, f) { 268 var t = n * Math.tan(fov / 2), 269 r = t * ratio; 270 271 return this.frustum(-r, r, -t, t, n, f); 272 }, 273 274 /** 275 * Multiplies a vector vec to a matrix mat: mat * vec. The matrix is interpreted by this function as an array of rows. 276 * Please note: This 277 * function does not check if the dimensions match. 278 * @param {Array} mat Two-dimensional array of numbers. The inner arrays describe the columns, the outer ones the matrix' rows. 279 * @param {Array} vec Array of numbers 280 * @returns {Array} Array of numbers containing the result 281 * @example 282 * var A = [[2, 1], 283 * [2, 3]], 284 * b = [4, 5], 285 * c; 286 * c = JXG.Math.matVecMult(A, b); 287 * // c === [13, 23]; 288 */ 289 matVecMult: function (mat, vec) { 290 var i, k, s, 291 m = mat.length, 292 n = vec.length, 293 res = []; 294 295 if (n === 3) { 296 for (i = 0; i < m; i++) { 297 res[i] = mat[i][0] * vec[0] + mat[i][1] * vec[1] + mat[i][2] * vec[2]; 298 } 299 } else { 300 for (i = 0; i < m; i++) { 301 s = 0; 302 for (k = 0; k < n; k++) { 303 s += mat[i][k] * vec[k]; 304 } 305 res[i] = s; 306 } 307 } 308 return res; 309 }, 310 311 /** 312 * Multiplies a vector vec to a matrix mat from the left: vec * mat. 313 * The matrix is interpreted by this function as an array of rows. 314 * Please note: This function does not check if the dimensions match. 315 * @param {Array} vec Array of numbers 316 * @param {Array} mat Two-dimensional array of numbers. The inner arrays describe the columns, 317 * the outer ones the matrix' rows. 318 * @returns {Array} Array of numbers containing the result 319 * @example 320 * var A = [[2, 1], 321 * [2, 3]], 322 * b = [4, 5], 323 * c; 324 * c = JXG.Math.vecMatMult(b, A); 325 * // c === [18, 16]; 326 */ 327 vecMatMult: function (vec, mat) { 328 var i, k, s, 329 m = mat.length, 330 n = vec.length, 331 res = []; 332 333 if (n === 3) { 334 for (i = 0; i < m; i++) { 335 res[i] = vec[0] * mat[0][i] + vec[1] * mat[1][i] + vec[2] * mat[2][i]; 336 } 337 } else { 338 for (i = 0; i < n; i++) { 339 s = 0; 340 for (k = 0; k < m; k++) { 341 s += vec[k] * mat[k][i]; 342 } 343 res[i] = s; 344 } 345 } 346 return res; 347 }, 348 349 /** 350 * Computes the product of the two matrices: mat1 * mat2. 351 * Returns a new matrix array. 352 * 353 * @param {Array} mat1 Two-dimensional array of numbers 354 * @param {Array} mat2 Two-dimensional array of numbers 355 * @returns {Array} Two-dimensional Array of numbers containing result 356 */ 357 matMatMult: function (mat1, mat2) { 358 var i, j, s, k, 359 m = mat1.length, 360 n = m > 0 ? mat2[0].length : 0, 361 m2 = mat2.length, 362 res = this.matrix(m, n); 363 364 for (i = 0; i < m; i++) { 365 for (j = 0; j < n; j++) { 366 s = 0; 367 for (k = 0; k < m2; k++) { 368 s += mat1[i][k] * mat2[k][j]; 369 } 370 res[i][j] = s; 371 } 372 } 373 return res; 374 }, 375 376 /** 377 * Multiply a matrix mat by a scalar alpha: mat * scalar 378 * 379 * @param {Array} mat Two-dimensional array of numbers 380 * @param {Number} alpha Scalar 381 * @returns {Array} Two-dimensional Array of numbers containing result 382 */ 383 matNumberMult: function (mat, alpha) { 384 var i, j, 385 m = mat.length, 386 n = m > 0 ? mat[0].length : 0, 387 res = this.matrix(m, n); 388 389 for (i = 0; i < m; i++) { 390 for (j = 0; j < n; j++) { 391 res[i][j] = mat[i][j] * alpha; 392 } 393 } 394 return res; 395 }, 396 397 /** 398 * Compute the sum of two matrices: mat1 + mat2. 399 * Returns a new matrix object. 400 * 401 * @param {Array} mat1 Two-dimensional array of numbers 402 * @param {Array} mat2 Two-dimensional array of numbers 403 * @returns {Array} Two-dimensional Array of numbers containing result 404 */ 405 matMatAdd: function (mat1, mat2) { 406 var i, j, 407 m = mat1.length, 408 n = m > 0 ? mat1[0].length : 0, 409 res = this.matrix(m, n); 410 411 for (i = 0; i < m; i++) { 412 for (j = 0; j < n; j++) { 413 res[i][j] = mat1[i][j] + mat2[i][j]; 414 } 415 } 416 return res; 417 }, 418 419 /** 420 * Transposes a matrix given as a two-dimensional array. 421 * @param {Array} M The matrix to be transposed 422 * @returns {Array} The transpose of M 423 */ 424 transpose: function (M) { 425 var MT, i, j, m, n; 426 427 // number of rows of M 428 m = M.length; 429 // number of columns of M 430 n = M.length > 0 ? M[0].length : 0; 431 MT = this.matrix(n, m); 432 433 for (i = 0; i < n; i++) { 434 for (j = 0; j < m; j++) { 435 MT[i][j] = M[j][i]; 436 } 437 } 438 439 return MT; 440 }, 441 442 /** 443 * Compute the inverse of an <i>(n x n)</i>-matrix by Gauss elimination. 444 * 445 * @param {Array} A matrix 446 * @returns {Array} Inverse matrix of A or empty array (i.e. []) in case A is singular. 447 */ 448 inverse: function (Ain) { 449 var i, j, k, r, s, 450 eps = this.eps * this.eps, 451 ma, swp, 452 n = Ain.length, 453 A = [], 454 p = [], 455 hv = []; 456 457 for (i = 0; i < n; i++) { 458 A[i] = []; 459 for (j = 0; j < n; j++) { 460 A[i][j] = Ain[i][j]; 461 } 462 p[i] = i; 463 } 464 465 for (j = 0; j < n; j++) { 466 // Pivot search 467 ma = Math.abs(A[j][j]); 468 r = j; 469 470 for (i = j + 1; i < n; i++) { 471 if (Math.abs(A[i][j]) > ma) { 472 ma = Math.abs(A[i][j]); 473 r = i; 474 } 475 } 476 477 // Singular matrix 478 if (ma <= eps) { 479 JXG.warn('JXG.Math.inverse: singular matrix'); 480 return []; 481 } 482 483 // swap rows: 484 if (r > j) { 485 for (k = 0; k < n; k++) { 486 swp = A[j][k]; 487 A[j][k] = A[r][k]; 488 A[r][k] = swp; 489 } 490 491 swp = p[j]; 492 p[j] = p[r]; 493 p[r] = swp; 494 } 495 496 // transformation: 497 s = 1.0 / A[j][j]; 498 for (i = 0; i < n; i++) { 499 A[i][j] *= s; 500 } 501 A[j][j] = s; 502 503 for (k = 0; k < n; k++) { 504 if (k !== j) { 505 for (i = 0; i < n; i++) { 506 if (i !== j) { 507 A[i][k] -= A[i][j] * A[j][k]; 508 } 509 } 510 A[j][k] = -s * A[j][k]; 511 } 512 } 513 } 514 515 // swap columns: 516 for (i = 0; i < n; i++) { 517 for (k = 0; k < n; k++) { 518 hv[p[k]] = A[i][k]; 519 } 520 for (k = 0; k < n; k++) { 521 A[i][k] = hv[k]; 522 } 523 } 524 525 return A; 526 }, 527 528 /** 529 * Trace of a square matrix, given as a two-dimensional array. 530 * @param {Array} M Square matrix 531 * @returns {Number} The trace of M, NaN if M is not square. 532 */ 533 trace: function (M) { 534 var i, m, n, 535 t = 0.0; 536 537 // number of rows of M 538 m = M.length; 539 // number of columns of M 540 n = M.length > 0 ? M[0].length : 0; 541 if (m !== n) { 542 return NaN; 543 } 544 for (i = 0; i < n; i++) { 545 t += M[i][i]; 546 } 547 548 return t; 549 }, 550 551 /** 552 * Inner product of two vectors a and b. n is the length of the vectors. 553 * @param {Array} a Vector 554 * @param {Array} b Vector 555 * @param {Number} [n] Length of the Vectors. If not given the length of the first vector is taken. 556 * @returns {Number} The inner product of a and b. 557 */ 558 innerProduct: function (a, b, n) { 559 var i, 560 s = 0; 561 562 if (n === undef || !Type.isNumber(n)) { 563 n = a.length; 564 } 565 566 for (i = 0; i < n; i++) { 567 s += a[i] * b[i]; 568 } 569 570 return s; 571 }, 572 573 /** 574 * Calculates the cross product of two vectors both of length three. 575 * In case of homogeneous coordinates this is either 576 * <ul> 577 * <li>the intersection of two lines</li> 578 * <li>the line through two points</li> 579 * </ul> 580 * @param {Array} c1 Homogeneous coordinates of line or point 1 581 * @param {Array} c2 Homogeneous coordinates of line or point 2 582 * @returns {Array} vector of length 3: homogeneous coordinates of the resulting point / line. 583 */ 584 crossProduct: function (c1, c2) { 585 return [ 586 c1[1] * c2[2] - c1[2] * c2[1], 587 c1[2] * c2[0] - c1[0] * c2[2], 588 c1[0] * c2[1] - c1[1] * c2[0] 589 ]; 590 }, 591 592 /** 593 * Euclidean norm of a vector. 594 * 595 * @param {Array} a Array containing a vector. 596 * @param {Number} n (Optional) length of the array. 597 * @returns {Number} Euclidean norm of the vector. 598 */ 599 norm: function (a, n) { 600 var i, 601 sum = 0.0; 602 603 if (n === undef || !Type.isNumber(n)) { 604 n = a.length; 605 } 606 607 for (i = 0; i < n; i++) { 608 sum += a[i] * a[i]; 609 } 610 611 return Math.sqrt(sum); 612 }, 613 614 /** 615 * Compute a * x + y for a scalar a and vectors x and y. 616 * 617 * @param {Number} a 618 * @param {Array} x 619 * @param {Array} y 620 * @returns 621 */ 622 axpy: function (a, x, y) { 623 var i, 624 le = x.length, 625 p = []; 626 for (i = 0; i < le; i++) { 627 p[i] = a * x[i] + y[i]; 628 } 629 return p; 630 }, 631 632 /** 633 * Compute the factorial of a positive integer. If a non-integer value 634 * is given, the fraction will be ignored. 635 * @function 636 * @param {Number} n 637 * @returns {Number} n! = n*(n-1)*...*2*1 638 */ 639 factorial: memoizer(function (n) { 640 if (n < 0) { 641 return NaN; 642 } 643 644 n = Math.floor(n); 645 646 if (n === 0 || n === 1) { 647 return 1; 648 } 649 650 return n * this.factorial(n - 1); 651 }), 652 653 /** 654 * Computes the binomial coefficient n over k. 655 * @function 656 * @param {Number} n Fraction will be ignored 657 * @param {Number} k Fraction will be ignored 658 * @returns {Number} The binomial coefficient n over k 659 */ 660 binomial: memoizer(function (n, k) { 661 var b, i; 662 663 if (k > n || k < 0) { 664 return NaN; 665 } 666 667 k = Math.round(k); 668 n = Math.round(n); 669 670 if (k === 0 || k === n) { 671 return 1; 672 } 673 674 b = 1; 675 676 for (i = 0; i < k; i++) { 677 b *= n - i; 678 b /= i + 1; 679 } 680 681 return b; 682 }), 683 684 /** 685 * Calculates the cosine hyperbolicus of x. 686 * @function 687 * @param {Number} x The number the cosine hyperbolicus will be calculated of. 688 * @returns {Number} Cosine hyperbolicus of the given value. 689 */ 690 cosh: 691 Math.cosh || 692 function (x) { 693 return (Math.exp(x) + Math.exp(-x)) * 0.5; 694 }, 695 696 /** 697 * Sine hyperbolicus of x. 698 * @function 699 * @param {Number} x The number the sine hyperbolicus will be calculated of. 700 * @returns {Number} Sine hyperbolicus of the given value. 701 */ 702 sinh: 703 Math.sinh || 704 function (x) { 705 return (Math.exp(x) - Math.exp(-x)) * 0.5; 706 }, 707 708 /** 709 * Hyperbolic arc-cosine of a number. 710 * 711 * @param {Number} x 712 * @returns {Number} 713 */ 714 acosh: 715 Math.acosh || 716 function (x) { 717 return Math.log(x + Math.sqrt(x * x - 1)); 718 }, 719 720 /** 721 * Hyperbolic arcsine of a number 722 * @param {Number} x 723 * @returns {Number} 724 */ 725 asinh: 726 Math.asinh || 727 function (x) { 728 if (x === -Infinity) { 729 return x; 730 } 731 return Math.log(x + Math.sqrt(x * x + 1)); 732 }, 733 734 /** 735 * Computes the cotangent of x. 736 * @function 737 * @param {Number} x The number the cotangent will be calculated of. 738 * @returns {Number} Cotangent of the given value. 739 */ 740 cot: function (x) { 741 return 1 / Math.tan(x); 742 }, 743 744 /** 745 * Computes the inverse cotangent of x. 746 * @param {Number} x The number the inverse cotangent will be calculated of. 747 * @returns {Number} Inverse cotangent of the given value. 748 */ 749 acot: function (x) { 750 return (x >= 0 ? 0.5 : -0.5) * Math.PI - Math.atan(x); 751 }, 752 753 /** 754 * Compute n-th real root of a real number. n must be strictly positive integer. 755 * If n is odd, the real n-th root exists and is negative. 756 * For n even, for negative valuees of x NaN is returned 757 * @param {Number} x radicand. Must be non-negative, if n even. 758 * @param {Number} n index of the root. must be strictly positive integer. 759 * @returns {Number} returns real root or NaN 760 * 761 * @example 762 * nthroot(16, 4): 2 763 * nthroot(-27, 3): -3 764 * nthroot(-4, 2): NaN 765 */ 766 nthroot: function (x, n) { 767 var inv = 1 / n; 768 769 if (n <= 0 || Math.floor(n) !== n) { 770 return NaN; 771 } 772 773 if (x === 0.0) { 774 return 0.0; 775 } 776 777 if (x > 0) { 778 return Math.exp(inv * Math.log(x)); 779 } 780 781 // From here on, x is negative 782 if (n % 2 === 1) { 783 return -Math.exp(inv * Math.log(-x)); 784 } 785 786 // x negative, even root 787 return NaN; 788 }, 789 790 /** 791 * Computes cube root of real number 792 * Polyfill for Math.cbrt(). 793 * 794 * @function 795 * @param {Number} x Radicand 796 * @returns {Number} Cube root of x. 797 */ 798 cbrt: 799 Math.cbrt || 800 function (x) { 801 return this.nthroot(x, 3); 802 }, 803 804 /** 805 * Compute base to the power of exponent. 806 * @param {Number} base 807 * @param {Number} exponent 808 * @returns {Number} base to the power of exponent. 809 */ 810 pow: function (base, exponent) { 811 if (base === 0) { 812 if (exponent === 0) { 813 return 1; 814 } 815 return 0; 816 } 817 818 // exponent is an integer 819 if (Math.floor(exponent) === exponent) { 820 return Math.pow(base, exponent); 821 } 822 823 // exponent is not an integer 824 if (base > 0) { 825 return Math.exp(exponent * Math.log(base)); 826 } 827 828 return NaN; 829 }, 830 831 /** 832 * Compute base to the power of the rational exponent m / n. 833 * This function first reduces the fraction m/n and then computes 834 * JXG.Math.pow(base, m/n). 835 * 836 * This function is necessary to have the same results for e.g. 837 * (-8)^(1/3) = (-8)^(2/6) = -2 838 * @param {Number} base 839 * @param {Number} m numerator of exponent 840 * @param {Number} n denominator of exponent 841 * @returns {Number} base to the power of exponent. 842 */ 843 ratpow: function (base, m, n) { 844 var g; 845 if (m === 0) { 846 return 1; 847 } 848 if (n === 0) { 849 return NaN; 850 } 851 852 g = this.gcd(m, n); 853 return this.nthroot(this.pow(base, m / g), n / g); 854 }, 855 856 /** 857 * Logarithm to base 10. 858 * @param {Number} x 859 * @returns {Number} log10(x) Logarithm of x to base 10. 860 */ 861 log10: function (x) { 862 return Math.log(x) / Math.log(10.0); 863 }, 864 865 /** 866 * Logarithm to base 2. 867 * @param {Number} x 868 * @returns {Number} log2(x) Logarithm of x to base 2. 869 */ 870 log2: function (x) { 871 return Math.log(x) / Math.log(2.0); 872 }, 873 874 /** 875 * Logarithm to arbitrary base b. If b is not given, natural log is taken, i.e. b = e. 876 * @param {Number} x 877 * @param {Number} b base 878 * @returns {Number} log(x, b) Logarithm of x to base b, that is log(x)/log(b). 879 */ 880 log: function (x, b) { 881 if (b !== undefined && Type.isNumber(b)) { 882 return Math.log(x) / Math.log(b); 883 } 884 885 return Math.log(x); 886 }, 887 888 /** 889 * The sign() function returns the sign of a number, indicating whether the number is positive, negative or zero. 890 * 891 * @function 892 * @param {Number} x A Number 893 * @returns {Number} This function has 5 kinds of return values, 894 * 1, -1, 0, -0, NaN, which represent "positive number", "negative number", "positive zero", "negative zero" 895 * and NaN respectively. 896 */ 897 sign: 898 Math.sign || 899 function (x) { 900 x = +x; // convert to a number 901 if (x === 0 || isNaN(x)) { 902 return x; 903 } 904 return x > 0 ? 1 : -1; 905 }, 906 907 /** 908 * A square & multiply algorithm to compute base to the power of exponent. 909 * Implementated by Wolfgang Riedl. 910 * 911 * @param {Number} base 912 * @param {Number} exponent 913 * @returns {Number} Base to the power of exponent 914 */ 915 squampow: function (base, exponent) { 916 var result; 917 918 if (Math.floor(exponent) === exponent) { 919 // exponent is integer (could be zero) 920 result = 1; 921 922 if (exponent < 0) { 923 // invert: base 924 base = 1.0 / base; 925 exponent *= -1; 926 } 927 928 while (exponent !== 0) { 929 if (exponent & 1) { 930 result *= base; 931 } 932 933 exponent >>= 1; 934 base *= base; 935 } 936 return result; 937 } 938 939 return this.pow(base, exponent); 940 }, 941 942 /** 943 * Greatest common divisor (gcd) of two numbers. 944 * See {@link <a href="https://rosettacode.org/wiki/Greatest_common_divisor#JavaScript">rosettacode.org</a>}. 945 * 946 * @param {Number} a First number 947 * @param {Number} b Second number 948 * @returns {Number} gcd(a, b) if a and b are numbers, NaN else. 949 */ 950 gcd: function (a, b) { 951 var tmp, 952 endless = true; 953 954 a = Math.abs(a); 955 b = Math.abs(b); 956 957 if (!(Type.isNumber(a) && Type.isNumber(b))) { 958 return NaN; 959 } 960 if (b > a) { 961 tmp = a; 962 a = b; 963 b = tmp; 964 } 965 966 while (endless) { 967 a %= b; 968 if (a === 0) { 969 return b; 970 } 971 b %= a; 972 if (b === 0) { 973 return a; 974 } 975 } 976 }, 977 978 /** 979 * Least common multiple (lcm) of two numbers. 980 * 981 * @param {Number} a First number 982 * @param {Number} b Second number 983 * @returns {Number} lcm(a, b) if a and b are numbers, NaN else. 984 */ 985 lcm: function (a, b) { 986 var ret; 987 988 if (!(Type.isNumber(a) && Type.isNumber(b))) { 989 return NaN; 990 } 991 992 ret = a * b; 993 if (ret !== 0) { 994 return ret / this.gcd(a, b); 995 } 996 997 return 0; 998 }, 999 1000 /** 1001 * Special use of Math.round function to round not only to integers but also to chosen decimal values. 1002 * 1003 * @param {Number} value Value to be rounded. 1004 * @param {Number} step Distance between the values to be rounded to. (default: 1.0) 1005 * @param {Number} [min] If set, it will be returned the maximum of value and min. 1006 * @param {Number} [max] If set, it will be returned the minimum of value and max. 1007 * @returns {Number} Fitted value. 1008 */ 1009 roundToStep: function (value, step, min, max) { 1010 var n = value, 1011 tmp, minOr0; 1012 1013 // for performance 1014 if (!Type.exists(step) && !Type.exists(min) && !Type.exists(max)) { 1015 return n; 1016 } 1017 1018 if (JXG.exists(max)) { 1019 n = Math.min(n, max); 1020 } 1021 if (JXG.exists(min)) { 1022 n = Math.max(n, min); 1023 } 1024 1025 minOr0 = min || 0; 1026 1027 if (JXG.exists(step)) { 1028 tmp = (n - minOr0) / step; 1029 if (Number.isInteger(tmp)) { 1030 return n; 1031 } 1032 1033 tmp = Math.round(tmp); 1034 n = minOr0 + tmp * step; 1035 } 1036 1037 if (JXG.exists(max)) { 1038 n = Math.min(n, max); 1039 } 1040 if (JXG.exists(min)) { 1041 n = Math.max(n, min); 1042 } 1043 1044 return n; 1045 }, 1046 1047 /** 1048 * Error function, see {@link https://en.wikipedia.org/wiki/Error_function}. 1049 * 1050 * @see JXG.Math.ProbFuncs.erf 1051 * @param {Number} x 1052 * @returns {Number} 1053 */ 1054 erf: function (x) { 1055 return this.ProbFuncs.erf(x); 1056 }, 1057 1058 /** 1059 * Complementary error function, i.e. 1 - erf(x). 1060 * 1061 * @see JXG.Math.erf 1062 * @see JXG.Math.ProbFuncs.erfc 1063 * @param {Number} x 1064 * @returns {Number} 1065 */ 1066 erfc: function (x) { 1067 return this.ProbFuncs.erfc(x); 1068 }, 1069 1070 /** 1071 * Inverse of error function 1072 * 1073 * @see JXG.Math.erf 1074 * @see JXG.Math.ProbFuncs.erfi 1075 * @param {Number} x 1076 * @returns {Number} 1077 */ 1078 erfi: function (x) { 1079 return this.ProbFuncs.erfi(x); 1080 }, 1081 1082 /** 1083 * Normal distribution function 1084 * 1085 * @see JXG.Math.ProbFuncs.ndtr 1086 * @param {Number} x 1087 * @returns {Number} 1088 */ 1089 ndtr: function (x) { 1090 return this.ProbFuncs.ndtr(x); 1091 }, 1092 1093 /** 1094 * Inverse of normal distribution function 1095 * 1096 * @see JXG.Math.ndtr 1097 * @see JXG.Math.ProbFuncs.ndtri 1098 * @param {Number} x 1099 * @returns {Number} 1100 */ 1101 ndtri: function (x) { 1102 return this.ProbFuncs.ndtri(x); 1103 }, 1104 1105 /** 1106 * Returns sqrt(a * a + b * b) for a variable number of arguments. 1107 * This is a naive implementation which might be faster than Math.hypot. 1108 * The latter is numerically more stable. 1109 * 1110 * @param {Number} a Variable number of arguments. 1111 * @returns Number 1112 */ 1113 hypot: function () { 1114 var i, le, a, sum; 1115 1116 le = arguments.length; 1117 for (i = 0, sum = 0.0; i < le; i++) { 1118 a = arguments[i]; 1119 sum += a * a; 1120 } 1121 return Math.sqrt(sum); 1122 }, 1123 1124 /** 1125 * Heaviside unit step function. Returns 0 for x <, 1 for x > 0, and 0.5 for x == 0. 1126 * 1127 * @param {Number} x 1128 * @returns Number 1129 */ 1130 hstep: function (x) { 1131 return (x > 0.0) ? 1 : 1132 ((x < 0.0) ? 0.0 : 0.5); 1133 }, 1134 1135 /** 1136 * Gamma function for real parameters by Lanczos approximation. 1137 * Implementation straight from {@link https://en.wikipedia.org/wiki/Lanczos_approximation}. 1138 * 1139 * @param {Number} z 1140 * @returns Number 1141 */ 1142 gamma: function (z) { 1143 var x, y, t, i, le, 1144 g = 7, 1145 // n = 9, 1146 p = [ 1147 1.0, 1148 676.5203681218851, 1149 -1259.1392167224028, 1150 771.32342877765313, 1151 -176.61502916214059, 1152 12.507343278686905, 1153 -0.13857109526572012, 1154 9.9843695780195716e-6, 1155 1.5056327351493116e-7 1156 ]; 1157 1158 if (z < 0.5) { 1159 y = Math.PI / (Math.sin(Math.PI * z) * this.gamma(1 - z)); // Reflection formula 1160 } else { 1161 z -= 1; 1162 x = p[0]; 1163 le = p.length; 1164 for (i = 1; i < le; i++) { 1165 x += p[i] / (z + i); 1166 } 1167 t = z + g + 0.5; 1168 y = Math.sqrt(2 * Math.PI) * Math.pow(t, z + 0.5) * Math.exp(-t) * x; 1169 } 1170 return y; 1171 }, 1172 1173 /* ******************** Comparisons and logical operators ************** */ 1174 1175 /** 1176 * Logical test: a < b? 1177 * 1178 * @param {Number} a 1179 * @param {Number} b 1180 * @returns {Boolean} 1181 */ 1182 lt: function (a, b) { 1183 return a < b; 1184 }, 1185 1186 /** 1187 * Logical test: a <= b? 1188 * 1189 * @param {Number} a 1190 * @param {Number} b 1191 * @returns {Boolean} 1192 */ 1193 leq: function (a, b) { 1194 return a <= b; 1195 }, 1196 1197 /** 1198 * Logical test: a > b? 1199 * 1200 * @param {Number} a 1201 * @param {Number} b 1202 * @returns {Boolean} 1203 */ 1204 gt: function (a, b) { 1205 return a > b; 1206 }, 1207 1208 /** 1209 * Logical test: a >= b? 1210 * 1211 * @param {Number} a 1212 * @param {Number} b 1213 * @returns {Boolean} 1214 */ 1215 geq: function (a, b) { 1216 return a >= b; 1217 }, 1218 1219 /** 1220 * Logical test: a === b? 1221 * 1222 * @param {Number} a 1223 * @param {Number} b 1224 * @returns {Boolean} 1225 */ 1226 eq: function (a, b) { 1227 return a === b; 1228 }, 1229 1230 /** 1231 * Logical test: a !== b? 1232 * 1233 * @param {Number} a 1234 * @param {Number} b 1235 * @returns {Boolean} 1236 */ 1237 neq: function (a, b) { 1238 return a !== b; 1239 }, 1240 1241 /** 1242 * Logical operator: a && b? 1243 * 1244 * @param {Boolean} a 1245 * @param {Boolean} b 1246 * @returns {Boolean} 1247 */ 1248 and: function (a, b) { 1249 return a && b; 1250 }, 1251 1252 /** 1253 * Logical operator: !a? 1254 * 1255 * @param {Boolean} a 1256 * @returns {Boolean} 1257 */ 1258 not: function (a) { 1259 return !a; 1260 }, 1261 1262 /** 1263 * Logical operator: a || b? 1264 * 1265 * @param {Boolean} a 1266 * @param {Boolean} b 1267 * @returns {Boolean} 1268 */ 1269 or: function (a, b) { 1270 return a || b; 1271 }, 1272 1273 /** 1274 * Logical operator: either a or b? 1275 * 1276 * @param {Boolean} a 1277 * @param {Boolean} b 1278 * @returns {Boolean} 1279 */ 1280 xor: function (a, b) { 1281 return (a || b) && !(a && b); 1282 }, 1283 1284 /** 1285 * 1286 * Convert a floating point number to sign + integer + fraction. 1287 * fraction is given as nominator and denominator. 1288 * <p> 1289 * Algorithm: approximate the floating point number 1290 * by a continued fraction and simultaneously keep track 1291 * of its convergents. 1292 * Inspired by {@link https://kevinboone.me/rationalize.html}. 1293 * 1294 * @param {Number} x Number which is to be converted 1295 * @param {Number} [order=0.001] Small number determining the approximation precision. 1296 * @returns {Array} [sign, leading, nominator, denominator] where sign is 1 or -1. 1297 * @see JXG.toFraction 1298 * 1299 * @example 1300 * JXG.Math.decToFraction(0.33333333); 1301 * // Result: [ 1, 0, 1, 3 ] 1302 * 1303 * JXG.Math.decToFraction(0); 1304 * // Result: [ 1, 0, 0, 1 ] 1305 * 1306 * JXG.Math.decToFraction(-10.66666666666667); 1307 * // Result: [-1, 10, 2, 3 ] 1308 */ 1309 decToFraction: function (x, order) { 1310 var lead, sign, a, 1311 n, n1, n2, 1312 d, d1, d2, 1313 it = 0, 1314 maxit = 20; 1315 1316 order = Type.def(order, 0.001); 1317 1318 // Round the number. 1319 // Otherwise, 0.999999999 would result in [0, 1, 1]. 1320 x = Math.round(x * 1.e12) * 1.e-12; 1321 1322 // Negative numbers: 1323 // The minus sign is handled in sign. 1324 sign = (x < 0) ? -1 : 1; 1325 x = Math.abs(x); 1326 1327 // From now on we consider x to be nonnegative. 1328 lead = Math.floor(x); 1329 x -= Math.floor(x); 1330 a = 0.0; 1331 n2 = 1.0; 1332 n = n1 = a; 1333 d2 = 0.0; 1334 d = d1 = 1.0; 1335 1336 while (x - Math.floor(x) > order && it < maxit) { 1337 x = 1 / (x - a); 1338 a = Math.floor(x); 1339 n = n2 + a * n1; 1340 d = d2 + a * d1; 1341 n2 = n1; 1342 d2 = d1; 1343 n1 = n; 1344 d1 = d; 1345 it++; 1346 } 1347 return [sign, lead, n, d]; 1348 }, 1349 1350 /* *************************** Normalize *************************** */ 1351 1352 /** 1353 * Normalize the standard form [c, b0, b1, a, k, r, q0, q1]. 1354 * @private 1355 * @param {Array} stdform The standard form to be normalized. 1356 * @returns {Array} The normalized standard form. 1357 */ 1358 normalize: function (stdform) { 1359 var n, 1360 signr, 1361 a2 = 2 * stdform[3], 1362 r = stdform[4] / a2; 1363 1364 stdform[5] = r; 1365 stdform[6] = -stdform[1] / a2; 1366 stdform[7] = -stdform[2] / a2; 1367 1368 if (!isFinite(r)) { 1369 n = this.hypot(stdform[1], stdform[2]); 1370 1371 stdform[0] /= n; 1372 stdform[1] /= n; 1373 stdform[2] /= n; 1374 stdform[3] = 0; 1375 stdform[4] = 1; 1376 } else if (Math.abs(r) >= 1) { 1377 stdform[0] = (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) / (2 * r); 1378 stdform[1] = -stdform[6] / r; 1379 stdform[2] = -stdform[7] / r; 1380 stdform[3] = 1 / (2 * r); 1381 stdform[4] = 1; 1382 } else { 1383 signr = r <= 0 ? -1 : 1; 1384 stdform[0] = 1385 signr * (stdform[6] * stdform[6] + stdform[7] * stdform[7] - r * r) * 0.5; 1386 stdform[1] = -signr * stdform[6]; 1387 stdform[2] = -signr * stdform[7]; 1388 stdform[3] = signr / 2; 1389 stdform[4] = signr * r; 1390 } 1391 1392 return stdform; 1393 }, 1394 1395 /** 1396 * Converts a two-dimensional array to a one-dimensional Float32Array that can be processed by WebGL. 1397 * @param {Array} m A matrix in a two-dimensional array. 1398 * @returns {Float32Array} A one-dimensional array containing the matrix in column wise notation. Provides a fall 1399 * back to the default JavaScript Array if Float32Array is not available. 1400 */ 1401 toGL: function (m) { 1402 var v, i, j; 1403 1404 if (typeof Float32Array === "function") { 1405 v = new Float32Array(16); 1406 } else { 1407 v = new Array(16); 1408 } 1409 1410 if (m.length !== 4 && m[0].length !== 4) { 1411 return v; 1412 } 1413 1414 for (i = 0; i < 4; i++) { 1415 for (j = 0; j < 4; j++) { 1416 v[i + 4 * j] = m[i][j]; 1417 } 1418 } 1419 1420 return v; 1421 }, 1422 1423 /** 1424 * Theorem of Vieta: Given a set of simple zeroes x_0, ..., x_n 1425 * of a polynomial f, compute the coefficients s_k, (k=0,...,n-1) 1426 * of the polynomial of the form. See {@link https://de.wikipedia.org/wiki/Elementarsymmetrisches_Polynom}. 1427 * <p> 1428 * f(x) = (x-x_0)*...*(x-x_n) = 1429 * x^n + sum_{k=1}^{n} (-1)^(k) s_{k-1} x^(n-k) 1430 * </p> 1431 * @param {Array} x Simple zeroes of the polynomial. 1432 * @returns {Array} Coefficients of the polynomial. 1433 * 1434 */ 1435 Vieta: function (x) { 1436 var n = x.length, 1437 s = [], 1438 m, 1439 k, 1440 y; 1441 1442 s = x.slice(); 1443 for (m = 1; m < n; ++m) { 1444 y = s[m]; 1445 s[m] *= s[m - 1]; 1446 for (k = m - 1; k >= 1; --k) { 1447 s[k] += s[k - 1] * y; 1448 } 1449 s[0] += y; 1450 } 1451 return s; 1452 } 1453 }; 1454 1455 export default JXG.Math; 1456