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