simple-squiggle

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

setUnion.js (1532B)


      1 import { flatten } from '../../utils/array.js';
      2 import { factory } from '../../utils/factory.js';
      3 var name = 'setUnion';
      4 var dependencies = ['typed', 'size', 'concat', 'subset', 'setIntersect', 'setSymDifference', 'Index'];
      5 export var createSetUnion = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     size,
      9     concat,
     10     subset,
     11     setIntersect,
     12     setSymDifference,
     13     Index
     14   } = _ref;
     15 
     16   /**
     17    * Create the union of two (multi)sets.
     18    * Multi-dimension arrays will be converted to single-dimension arrays before the operation.
     19    *
     20    * Syntax:
     21    *
     22    *    math.setUnion(set1, set2)
     23    *
     24    * Examples:
     25    *
     26    *    math.setUnion([1, 2, 3, 4], [3, 4, 5, 6])            // returns [1, 2, 3, 4, 5, 6]
     27    *    math.setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]])    // returns [1, 2, 3, 4, 5, 6]
     28    *
     29    * See also:
     30    *
     31    *    setIntersect, setDifference
     32    *
     33    * @param {Array | Matrix}    a1  A (multi)set
     34    * @param {Array | Matrix}    a2  A (multi)set
     35    * @return {Array | Matrix}    The union of two (multi)sets
     36    */
     37   return typed(name, {
     38     'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) {
     39       if (subset(size(a1), new Index(0)) === 0) {
     40         // if any of them is empty, return the other one
     41         return flatten(a2);
     42       } else if (subset(size(a2), new Index(0)) === 0) {
     43         return flatten(a1);
     44       }
     45 
     46       var b1 = flatten(a1);
     47       var b2 = flatten(a2);
     48       return concat(setSymDifference(b1, b2), setIntersect(b1, b2));
     49     }
     50   });
     51 });