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