simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

rotationMatrix.js (6541B)


      1 import { isBigNumber } from '../../utils/is.js';
      2 import { factory } from '../../utils/factory.js';
      3 var name = 'rotationMatrix';
      4 var dependencies = ['typed', 'config', 'multiplyScalar', 'addScalar', 'unaryMinus', 'norm', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix', 'cos', 'sin'];
      5 export var createRotationMatrix = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     config,
      9     multiplyScalar,
     10     addScalar,
     11     unaryMinus,
     12     norm,
     13     BigNumber,
     14     matrix,
     15     DenseMatrix,
     16     SparseMatrix,
     17     cos,
     18     sin
     19   } = _ref;
     20 
     21   /**
     22    * Create a 2-dimensional counter-clockwise rotation matrix (2x2) for a given angle (expressed in radians).
     23    * Create a 2-dimensional counter-clockwise rotation matrix (3x3) by a given angle (expressed in radians) around a given axis (1x3).
     24    *
     25    * Syntax:
     26    *
     27    *    math.rotationMatrix(theta)
     28    *    math.rotationMatrix(theta, format)
     29    *    math.rotationMatrix(theta, [v])
     30    *    math.rotationMatrix(theta, [v], format)
     31    *
     32    * Examples:
     33    *
     34    *    math.rotationMatrix(math.pi / 2)                      // returns [[0, -1], [1, 0]]
     35    *    math.rotationMatrix(math.bignumber(1))                // returns [[bignumber(cos(1)), bignumber(-sin(1))], [bignumber(sin(1)), bignumber(cos(1))]]
     36    *    math.rotationMatrix(math.complex(1 + i))              // returns [[cos(1 + i), -sin(1 + i)], [sin(1 + i), cos(1 + i)]]
     37    *    math.rotationMatrix(math.unit('1rad'))                // returns [[cos(1), -sin(1)], [sin(1), cos(1)]]
     38    *
     39    *    math.rotationMatrix(math.pi / 2, [0, 1, 0])           // returns [[0, 0, 1], [0, 1, 0], [-1, 0, 0]]
     40    *    math.rotationMatrix(math.pi / 2, matrix([0, 1, 0]))   // returns matrix([[0, 0, 1], [0, 1, 0], [-1, 0, 0]])
     41    *
     42    *
     43    * See also:
     44    *
     45    *    matrix, cos, sin
     46    *
     47    *
     48    * @param {number | BigNumber | Complex | Unit} theta    Rotation angle
     49    * @param {Array | Matrix} [v]                           Rotation axis
     50    * @param {string} [format]                              Result Matrix storage format
     51    * @return {Array | Matrix}                              Rotation matrix
     52    */
     53   return typed(name, {
     54     '': function _() {
     55       return config.matrix === 'Matrix' ? matrix([]) : [];
     56     },
     57     string: function string(format) {
     58       return matrix(format);
     59     },
     60     'number | BigNumber | Complex | Unit': function numberBigNumberComplexUnit(theta) {
     61       return _rotationMatrix2x2(theta, config.matrix === 'Matrix' ? 'dense' : undefined);
     62     },
     63     'number | BigNumber | Complex | Unit, string': function numberBigNumberComplexUnitString(theta, format) {
     64       return _rotationMatrix2x2(theta, format);
     65     },
     66     'number | BigNumber | Complex | Unit, Array': function numberBigNumberComplexUnitArray(theta, v) {
     67       var matrixV = matrix(v);
     68 
     69       _validateVector(matrixV);
     70 
     71       return _rotationMatrix3x3(theta, matrixV, undefined);
     72     },
     73     'number | BigNumber | Complex | Unit, Matrix': function numberBigNumberComplexUnitMatrix(theta, v) {
     74       _validateVector(v);
     75 
     76       var storageType = v.storage() || (config.matrix === 'Matrix' ? 'dense' : undefined);
     77       return _rotationMatrix3x3(theta, v, storageType);
     78     },
     79     'number | BigNumber | Complex | Unit, Array, string': function numberBigNumberComplexUnitArrayString(theta, v, format) {
     80       var matrixV = matrix(v);
     81 
     82       _validateVector(matrixV);
     83 
     84       return _rotationMatrix3x3(theta, matrixV, format);
     85     },
     86     'number | BigNumber | Complex | Unit, Matrix, string': function numberBigNumberComplexUnitMatrixString(theta, v, format) {
     87       _validateVector(v);
     88 
     89       return _rotationMatrix3x3(theta, v, format);
     90     }
     91   });
     92   /**
     93    * Returns 2x2 matrix of 2D rotation of angle theta
     94    *
     95    * @param {number | BigNumber | Complex | Unit} theta  The rotation angle
     96    * @param {string} format                              The result Matrix storage format
     97    * @returns {Matrix}
     98    * @private
     99    */
    100 
    101   function _rotationMatrix2x2(theta, format) {
    102     var Big = isBigNumber(theta);
    103     var minusOne = Big ? new BigNumber(-1) : -1;
    104     var cosTheta = cos(theta);
    105     var sinTheta = sin(theta);
    106     var data = [[cosTheta, multiplyScalar(minusOne, sinTheta)], [sinTheta, cosTheta]];
    107     return _convertToFormat(data, format);
    108   }
    109 
    110   function _validateVector(v) {
    111     var size = v.size();
    112 
    113     if (size.length < 1 || size[0] !== 3) {
    114       throw new RangeError('Vector must be of dimensions 1x3');
    115     }
    116   }
    117 
    118   function _mul(array) {
    119     return array.reduce((p, curr) => multiplyScalar(p, curr));
    120   }
    121 
    122   function _convertToFormat(data, format) {
    123     if (format) {
    124       if (format === 'sparse') {
    125         return new SparseMatrix(data);
    126       }
    127 
    128       if (format === 'dense') {
    129         return new DenseMatrix(data);
    130       }
    131 
    132       throw new TypeError("Unknown matrix type \"".concat(format, "\""));
    133     }
    134 
    135     return data;
    136   }
    137   /**
    138    * Returns a 3x3 matrix of rotation of angle theta around vector v
    139    *
    140    * @param {number | BigNumber | Complex | Unit} theta The rotation angle
    141    * @param {Matrix} v                                  The rotation axis vector
    142    * @param {string} format                             The storage format of the resulting matrix
    143    * @returns {Matrix}
    144    * @private
    145    */
    146 
    147 
    148   function _rotationMatrix3x3(theta, v, format) {
    149     var normV = norm(v);
    150 
    151     if (normV === 0) {
    152       throw new RangeError('Rotation around zero vector');
    153     }
    154 
    155     var Big = isBigNumber(theta) ? BigNumber : null;
    156     var one = Big ? new Big(1) : 1;
    157     var minusOne = Big ? new Big(-1) : -1;
    158     var vx = Big ? new Big(v.get([0]) / normV) : v.get([0]) / normV;
    159     var vy = Big ? new Big(v.get([1]) / normV) : v.get([1]) / normV;
    160     var vz = Big ? new Big(v.get([2]) / normV) : v.get([2]) / normV;
    161     var c = cos(theta);
    162     var oneMinusC = addScalar(one, unaryMinus(c));
    163     var s = sin(theta);
    164     var r11 = addScalar(c, _mul([vx, vx, oneMinusC]));
    165     var r12 = addScalar(_mul([vx, vy, oneMinusC]), _mul([minusOne, vz, s]));
    166     var r13 = addScalar(_mul([vx, vz, oneMinusC]), _mul([vy, s]));
    167     var r21 = addScalar(_mul([vx, vy, oneMinusC]), _mul([vz, s]));
    168     var r22 = addScalar(c, _mul([vy, vy, oneMinusC]));
    169     var r23 = addScalar(_mul([vy, vz, oneMinusC]), _mul([minusOne, vx, s]));
    170     var r31 = addScalar(_mul([vx, vz, oneMinusC]), _mul([minusOne, vy, s]));
    171     var r32 = addScalar(_mul([vy, vz, oneMinusC]), _mul([vx, s]));
    172     var r33 = addScalar(c, _mul([vz, vz, oneMinusC]));
    173     var data = [[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]];
    174     return _convertToFormat(data, format);
    175   }
    176 });