rotate.js 2.9 KB

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