simple-squiggle

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

cross.js (2937B)


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