simple-squiggle

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

setMultiplicity.js (1803B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createSetMultiplicity = void 0;
      7 
      8 var _array = require("../../utils/array.js");
      9 
     10 var _factory = require("../../utils/factory.js");
     11 
     12 var name = 'setMultiplicity';
     13 var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index'];
     14 var createSetMultiplicity = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     15   var typed = _ref.typed,
     16       size = _ref.size,
     17       subset = _ref.subset,
     18       compareNatural = _ref.compareNatural,
     19       Index = _ref.Index;
     20 
     21   /**
     22    * Count the multiplicity of an element in a multiset.
     23    * A multi-dimension array will be converted to a single-dimension array before the operation.
     24    *
     25    * Syntax:
     26    *
     27    *    math.setMultiplicity(element, set)
     28    *
     29    * Examples:
     30    *
     31    *    math.setMultiplicity(1, [1, 2, 2, 4])    // returns 1
     32    *    math.setMultiplicity(2, [1, 2, 2, 4])    // returns 2
     33    *
     34    * See also:
     35    *
     36    *    setDistinct, setSize
     37    *
     38    * @param {number | BigNumber | Fraction | Complex} e  An element in the multiset
     39    * @param {Array | Matrix}     a  A multiset
     40    * @return {number}            The number of how many times the multiset contains the element
     41    */
     42   return typed(name, {
     43     'number | BigNumber | Fraction | Complex, Array | Matrix': function numberBigNumberFractionComplexArrayMatrix(e, a) {
     44       if (subset(size(a), new Index(0)) === 0) {
     45         // if empty, return 0
     46         return 0;
     47       }
     48 
     49       var b = (0, _array.flatten)(Array.isArray(a) ? a : a.toArray());
     50       var count = 0;
     51 
     52       for (var i = 0; i < b.length; i++) {
     53         if (compareNatural(b[i], e) === 0) {
     54           count++;
     55         }
     56       }
     57 
     58       return count;
     59     }
     60   });
     61 });
     62 exports.createSetMultiplicity = createSetMultiplicity;