-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement count based on size (#2092)
* Implement count based on size Closes #2085 * Improve count implementation Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { factory } from '../../utils/factory.js' | ||
|
||
const name = 'count' | ||
const dependencies = ['typed', 'size', 'prod'] | ||
|
||
export const createCount = /* #__PURE__ */ factory(name, dependencies, ({ typed, size, prod }) => { | ||
/** | ||
* Count the number of elements of a matrix, array or string. | ||
* | ||
* Syntax: | ||
* | ||
* math.count(x) | ||
* | ||
* Examples: | ||
* | ||
* math.count('hello world') // returns 11 | ||
* const A = [[1, 2, 3], [4, 5, 6]] | ||
* math.count(A) // returns 6 | ||
* math.count(math.range(1,6)) // returns 5 | ||
* | ||
* See also: | ||
* | ||
* size | ||
* | ||
* @param {string | Array | Matrix} x A matrix or string | ||
* @return {number} An integer with the elements in `x`. | ||
*/ | ||
return typed(name, { | ||
string: function (x) { | ||
return x.length | ||
}, | ||
|
||
'Matrix | Array': function (x) { | ||
return prod(size(x)) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import assert from 'assert' | ||
import math from '../../../../src/defaultInstance.js' | ||
|
||
const { count, matrix, sparse } = math | ||
|
||
describe('count', function () { | ||
it('should count arrays', function () { | ||
assert.strictEqual(count([[1, 2, 3], [4, 5, 6]]), 6) | ||
assert.strictEqual(count([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), 8) | ||
assert.strictEqual(count([]), 0) | ||
assert.strictEqual(count([[]]), 0) | ||
}) | ||
|
||
it('should count dense matrices', function () { | ||
assert.strictEqual(count(matrix([[1, 2, 3], [4, 5, 6]])), 6) | ||
assert.strictEqual(count(matrix([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])), 8) | ||
assert.strictEqual(count(matrix([])), 0) | ||
assert.strictEqual(count(matrix([[]])), 0) | ||
}) | ||
|
||
it('should count sparse matrices', function () { | ||
assert.strictEqual(count(sparse([[1, 2, 3], [4, 5, 6]])), 6) | ||
assert.strictEqual(count(sparse([])), 0) | ||
assert.strictEqual(count(sparse([[]])), 0) | ||
}) | ||
|
||
it('should count strings', function () { | ||
assert.strictEqual(count('123456'), 6) | ||
assert.strictEqual(count(''), 0) | ||
}) | ||
|
||
it('should throw an error if called with an invalid number of arguments', function () { | ||
assert.throws(function () { count() }, /TypeError: Too few arguments/) | ||
assert.throws(function () { count(1, 2) }, /TypeError: Too many arguments/) | ||
}) | ||
|
||
it('should throw an error if called with invalid type of arguments', function () { | ||
assert.throws(function () { count(new Date()) }, /TypeError: Unexpected type of argument/) | ||
}) | ||
}) |