Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement count based on size #2092

Merged
merged 3 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/)
})
})