Skip to content

Commit

Permalink
Implement count based on size (#2092)
Browse files Browse the repository at this point in the history
* Implement count based on size

Closes #2085

* Improve count implementation

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
  • Loading branch information
Josef37 and josdejong authored Jan 31, 2021
1 parent 7d6fd8c commit bf0eedf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export { createOr } from './function/logical/or.js'
export { createXor } from './function/logical/xor.js'
export { createConcat } from './function/matrix/concat.js'
export { createColumn } from './function/matrix/column.js'
export { createCount } from './function/matrix/count.js'
export { createCross } from './function/matrix/cross.js'
export { createDiag } from './function/matrix/diag.js'
export { createFilter } from './function/matrix/filter.js'
Expand Down
37 changes: 37 additions & 0 deletions src/function/matrix/count.js
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))
}
})
})
2 changes: 1 addition & 1 deletion src/function/matrix/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createSize = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*
* See also:
*
* resize, squeeze, subset
* count, resize, squeeze, subset
*
* @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix
* @return {Array | Matrix} A vector with size of `x`.
Expand Down
40 changes: 40 additions & 0 deletions test/unit-tests/function/matrix/count.test.js
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/)
})
})

0 comments on commit bf0eedf

Please sign in to comment.