rotate.js 3.1 KB

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