simple-squiggle

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

cross.js (2667B)


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