cross.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createCross = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'cross';
  9. var dependencies = ['typed', 'matrix', 'subtract', 'multiply'];
  10. var createCross = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. matrix = _ref.matrix,
  13. subtract = _ref.subtract,
  14. multiply = _ref.multiply;
  15. /**
  16. * Calculate the cross product for two vectors in three dimensional space.
  17. * The cross product of `A = [a1, a2, a3]` and `B = [b1, b2, b3]` is defined
  18. * as:
  19. *
  20. * cross(A, B) = [
  21. * a2 * b3 - a3 * b2,
  22. * a3 * b1 - a1 * b3,
  23. * a1 * b2 - a2 * b1
  24. * ]
  25. *
  26. * If one of the input vectors has a dimension greater than 1, the output
  27. * vector will be a 1x3 (2-dimensional) matrix.
  28. *
  29. * Syntax:
  30. *
  31. * math.cross(x, y)
  32. *
  33. * Examples:
  34. *
  35. * math.cross([1, 1, 0], [0, 1, 1]) // Returns [1, -1, 1]
  36. * math.cross([3, -3, 1], [4, 9, 2]) // Returns [-15, -2, 39]
  37. * math.cross([2, 3, 4], [5, 6, 7]) // Returns [-3, 6, -3]
  38. * math.cross([[1, 2, 3]], [[4], [5], [6]]) // Returns [[-3, 6, -3]]
  39. *
  40. * See also:
  41. *
  42. * dot, multiply
  43. *
  44. * @param {Array | Matrix} x First vector
  45. * @param {Array | Matrix} y Second vector
  46. * @return {Array | Matrix} Returns the cross product of `x` and `y`
  47. */
  48. return typed(name, {
  49. 'Matrix, Matrix': function MatrixMatrix(x, y) {
  50. return matrix(_cross(x.toArray(), y.toArray()));
  51. },
  52. 'Matrix, Array': function MatrixArray(x, y) {
  53. return matrix(_cross(x.toArray(), y));
  54. },
  55. 'Array, Matrix': function ArrayMatrix(x, y) {
  56. return matrix(_cross(x, y.toArray()));
  57. },
  58. 'Array, Array': _cross
  59. });
  60. /**
  61. * Calculate the cross product for two arrays
  62. * @param {Array} x First vector
  63. * @param {Array} y Second vector
  64. * @returns {Array} Returns the cross product of x and y
  65. * @private
  66. */
  67. function _cross(x, y) {
  68. var highestDimension = Math.max((0, _array.arraySize)(x).length, (0, _array.arraySize)(y).length);
  69. x = (0, _array.squeeze)(x);
  70. y = (0, _array.squeeze)(y);
  71. var xSize = (0, _array.arraySize)(x);
  72. var ySize = (0, _array.arraySize)(y);
  73. if (xSize.length !== 1 || ySize.length !== 1 || xSize[0] !== 3 || ySize[0] !== 3) {
  74. throw new RangeError('Vectors with length 3 expected ' + '(Size A = [' + xSize.join(', ') + '], B = [' + ySize.join(', ') + '])');
  75. }
  76. var product = [subtract(multiply(x[1], y[2]), multiply(x[2], y[1])), subtract(multiply(x[2], y[0]), multiply(x[0], y[2])), subtract(multiply(x[0], y[1]), multiply(x[1], y[0]))];
  77. if (highestDimension > 1) {
  78. return [product];
  79. } else {
  80. return product;
  81. }
  82. }
  83. });
  84. exports.createCross = createCross;