simple-squiggle

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

rotate.js (3035B)


      1 import { factory } from '../../utils/factory.js';
      2 import { arraySize } from '../../utils/array.js';
      3 var name = 'rotate';
      4 var dependencies = ['typed', 'multiply', 'rotationMatrix'];
      5 export var createRotate = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     multiply,
      9     rotationMatrix
     10   } = _ref;
     11 
     12   /**
     13      * Rotate a vector of size 1x2 counter-clockwise by a given angle
     14      * Rotate a vector of size 1x3 counter-clockwise by a given angle around the given axis
     15      *
     16      * Syntax:
     17      *
     18      *    math.rotate(w, theta)
     19      *    math.rotate(w, theta, v)
     20      *
     21      * Examples:
     22      *
     23      *    math.rotate([11, 12], math.pi / 2)                           // returns matrix([-12, 11])
     24      *    math.rotate(matrix([11, 12]), math.pi / 2)                   // returns matrix([-12, 11])
     25      *
     26      *    math.rotate([1, 0, 0], unit('90deg'), [0, 0, 1])             // returns matrix([0, 1, 0])
     27      *    math.rotate(matrix([1, 0, 0]), unit('90deg'), [0, 0, 1])     // returns matrix([0, 1, 0])
     28      *
     29      *    math.rotate([1, 0], math.complex(1 + i))                     // returns matrix([cos(1 + i) - sin(1 + i), sin(1 + i) + cos(1 + i)])
     30      *
     31      * See also:
     32      *
     33      *    matrix, rotationMatrix
     34      *
     35      * @param {Array | Matrix} w                             Vector to rotate
     36      * @param {number | BigNumber | Complex | Unit} theta    Rotation angle
     37      * @param {Array | Matrix} [v]                           Rotation axis
     38      * @return {Array | Matrix}                              Multiplication of the rotation matrix and w
     39      */
     40   return typed(name, {
     41     'Array , number | BigNumber | Complex | Unit': function ArrayNumberBigNumberComplexUnit(w, theta) {
     42       _validateSize(w, 2);
     43 
     44       var matrixRes = multiply(rotationMatrix(theta), w);
     45       return matrixRes.toArray();
     46     },
     47     'Matrix , number | BigNumber | Complex | Unit': function MatrixNumberBigNumberComplexUnit(w, theta) {
     48       _validateSize(w, 2);
     49 
     50       return multiply(rotationMatrix(theta), w);
     51     },
     52     'Array, number | BigNumber | Complex | Unit, Array | Matrix': function ArrayNumberBigNumberComplexUnitArrayMatrix(w, theta, v) {
     53       _validateSize(w, 3);
     54 
     55       var matrixRes = multiply(rotationMatrix(theta, v), w);
     56       return matrixRes;
     57     },
     58     'Matrix, number | BigNumber | Complex | Unit, Array | Matrix': function MatrixNumberBigNumberComplexUnitArrayMatrix(w, theta, v) {
     59       _validateSize(w, 3);
     60 
     61       return multiply(rotationMatrix(theta, v), w);
     62     }
     63   });
     64 
     65   function _validateSize(v, expectedSize) {
     66     var actualSize = Array.isArray(v) ? arraySize(v) : v.size();
     67 
     68     if (actualSize.length > 2) {
     69       throw new RangeError("Vector must be of dimensions 1x".concat(expectedSize));
     70     }
     71 
     72     if (actualSize.length === 2 && actualSize[1] !== 1) {
     73       throw new RangeError("Vector must be of dimensions 1x".concat(expectedSize));
     74     }
     75 
     76     if (actualSize[0] !== expectedSize) {
     77       throw new RangeError("Vector must be of dimensions 1x".concat(expectedSize));
     78     }
     79   }
     80 });