mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
Renaming stirlingSecondKind to stirlingS2 + changing LaTeX to "\\mathrm{S}\\left(%0%,%1%\\right)"
This commit is contained in:
parent
b889c16b81
commit
ea6bb033eb
@ -12,16 +12,16 @@ module.exports = function (math) {
|
||||
/**
|
||||
* The Stirling numbers of the second kind, counts the number of ways to partition a set of n labelled objects into k nonempty unlabelled subsets.
|
||||
|
||||
* stirlingSecondKind only take integer arguments.
|
||||
* stirlingS2 only take integer arguments.
|
||||
* The following condition must be enforced: k <= n.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* math.stirlingSecondKind(n, k)
|
||||
* math.stirlingS2(n, k)
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* math.stirlingSecondKind(5, 3); //returns 25
|
||||
* math.stirlingS2(5, 3); //returns 25
|
||||
*
|
||||
* If n = k or k = 1, then s(n,k) = 1
|
||||
*
|
||||
@ -30,16 +30,16 @@ module.exports = function (math) {
|
||||
* @return {Number | BigNumber} S(n,k)
|
||||
*/
|
||||
|
||||
math.stirlingSecondKind = function stirlingSecondKind (n, k) {
|
||||
math.stirlingS2 = function stirlingS2 (n, k) {
|
||||
var result = 0;
|
||||
var arity = arguments.length;
|
||||
if (arity != 2) {
|
||||
throw new math.error.ArgumentsError('stirlingSecondKind', arguments.length, 2);
|
||||
throw new math.error.ArgumentsError('stirlingS2', arguments.length, 2);
|
||||
}
|
||||
|
||||
if (isNumber(n) && isNumber(k)) {
|
||||
if (!isInteger(n) || n < 0 || !isInteger(k) || k < 0) {
|
||||
throw new TypeError('Positive integer value expected in function stirlingSecondKind');
|
||||
throw new TypeError('Positive integer value expected in function stirlingS2');
|
||||
}
|
||||
if (k > n) {
|
||||
throw new TypeError('k must be less than or equal to n');
|
||||
@ -58,7 +58,7 @@ module.exports = function (math) {
|
||||
console.log("result: ", result)
|
||||
return result;
|
||||
} else {
|
||||
throw new TypeError('Integer values are expected in stirlingSecondKind')
|
||||
throw new TypeError('Integer values are expected in stirlingS2')
|
||||
}
|
||||
|
||||
// if (n instanceof BigNumber) {
|
||||
@ -67,10 +67,10 @@ module.exports = function (math) {
|
||||
// k = BigNumber.convert(k);
|
||||
|
||||
// if (!(k instanceof BigNumber) || !isPositiveInteger(k)) {
|
||||
// throw new TypeError('Positive integer value expected in function stirlingSecondKind');
|
||||
// throw new TypeError('Positive integer value expected in function stirlingS2');
|
||||
// }
|
||||
// if (k.gt(n)) {
|
||||
// throw new TypeError('k must be less than n in function stirlingSecondKind');
|
||||
// throw new TypeError('k must be less than n in function stirlingS2');
|
||||
// }
|
||||
|
||||
// result = new BigNumber(1);
|
||||
@ -83,7 +83,7 @@ module.exports = function (math) {
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// throw new math.error.UnsupportedTypeError('stirlingSecondKindg', math['typeof'](n));
|
||||
// throw new math.error.UnsupportedTypeError('stirlingS2g', math['typeof'](n));
|
||||
};
|
||||
|
||||
|
||||
@ -269,7 +269,7 @@ function create (config) {
|
||||
require('./function/bitwise/rightLogShift')(math, _config);
|
||||
|
||||
//functions - combinatorics
|
||||
require('./function/combinatorics/stirlingSecondKind')(math, _config);
|
||||
require('./function/combinatorics/stirlingS2')(math, _config);
|
||||
|
||||
// functions - complex
|
||||
require('./function/complex/arg')(math, _config);
|
||||
|
||||
@ -168,7 +168,7 @@ var functions = {
|
||||
're': '\\Re\\left\\lbrace%0%\\right\\rbrace',
|
||||
|
||||
//combinatorics
|
||||
'stirlingSecondKind': '\S{%0%}{%1%}',
|
||||
'stirlingS2': '\\mathrm{S}\\left(%0%,%1%\\right)',
|
||||
|
||||
//construction
|
||||
'bignumber': {
|
||||
|
||||
45
test/function/combinatorics/stirlingS2.test.js
Normal file
45
test/function/combinatorics/stirlingS2.test.js
Normal file
@ -0,0 +1,45 @@
|
||||
var assert = require('assert'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
stirlingS2 = math.stirlingS2;
|
||||
|
||||
describe('stirlingS2', function() {
|
||||
|
||||
it('should calculate the number of ways to partition a set of n objects into k non-empty subsets', function() {
|
||||
assert.equal(stirlingS2(5,3), 25);
|
||||
assert.equal(stirlingS2(0,0), 1);
|
||||
assert.equal(stirlingS2(8,7), 28);
|
||||
});
|
||||
|
||||
// it('should calculate the stirlingS2 of n items taken k at a time with BigNumbers', function(){
|
||||
// assert.deepEqual(stirlingS2(math.bignumber(7), math.bignumber(5)),math.bignumber(140));
|
||||
// assert.deepEqual(stirlingS2(math.bignumber(10), math.bignumber(4)),math.bignumber(34105));
|
||||
// assert.deepEqual(stirlingS2(math.bignumber(8), math.bignumber(6)),math.bignumber(266));
|
||||
// });
|
||||
|
||||
// it('should calculate the combinations of n items taken k at a time with BigNumbers', function() {
|
||||
// assert.deepEqual(combinations(math.bignumber(7), math.bignumber(5)), math.bignumber(21));
|
||||
// assert.deepEqual(combinations(math.bignumber(20), math.bignumber(15)), math.bignumber(15504));
|
||||
// assert.deepEqual(combinations(math.bignumber(63), math.bignumber(7)), math.bignumber(553270671));
|
||||
// assert.deepEqual(combinations(math.bignumber(25), math.bignumber(6)), math.bignumber(177100));
|
||||
// });
|
||||
|
||||
it('should not work with non-integer and negative input', function() {
|
||||
assert.throws(function() {stirlingS2(0.5, 3)}, TypeError);
|
||||
assert.throws(function() {stirlingS2(3, 5)}, TypeError);
|
||||
// assert.throws(function() {stirlingS2(math.bignumber(3), math.bignumber(5))}, TypeError);
|
||||
// assert.throws(function() {stirlingS2(math.bignumber(3.5), math.bignumber(-3))}, TypeError);
|
||||
// assert.throws(function() {stirlingS2(math.bignumber(3.5), 1/3)}, TypeError);
|
||||
});
|
||||
|
||||
it('should not work with the wrong number or type of arguments', function() {
|
||||
assert.throws(function() {stirlingS2(5, 3, 2)});
|
||||
assert.throws(function() {stirlingS2(true, "hello world")});
|
||||
});
|
||||
|
||||
it('should LaTeX stirlingS2', function () {
|
||||
var expression = math.parse('stirlingS2(3,2)');
|
||||
assert.equal(expression.toTex(), '\S{3}{2}');
|
||||
});
|
||||
|
||||
});
|
||||
@ -1,45 +0,0 @@
|
||||
var assert = require('assert'),
|
||||
error = require('../../../lib/error/index'),
|
||||
math = require('../../../index'),
|
||||
stirlingSecondKind = math.stirlingSecondKind;
|
||||
|
||||
describe('stirlingSecondKind', function() {
|
||||
|
||||
it('should calculate the number of ways to partition a set of n objects into k non-empty subsets', function() {
|
||||
assert.equal(stirlingSecondKind(5,3), 25);
|
||||
assert.equal(stirlingSecondKind(0,0), 1);
|
||||
assert.equal(stirlingSecondKind(8,7), 28);
|
||||
});
|
||||
|
||||
// it('should calculate the stirlingSecondKind of n items taken k at a time with BigNumbers', function(){
|
||||
// assert.deepEqual(stirlingSecondKind(math.bignumber(7), math.bignumber(5)),math.bignumber(140));
|
||||
// assert.deepEqual(stirlingSecondKind(math.bignumber(10), math.bignumber(4)),math.bignumber(34105));
|
||||
// assert.deepEqual(stirlingSecondKind(math.bignumber(8), math.bignumber(6)),math.bignumber(266));
|
||||
// });
|
||||
|
||||
// it('should calculate the combinations of n items taken k at a time with BigNumbers', function() {
|
||||
// assert.deepEqual(combinations(math.bignumber(7), math.bignumber(5)), math.bignumber(21));
|
||||
// assert.deepEqual(combinations(math.bignumber(20), math.bignumber(15)), math.bignumber(15504));
|
||||
// assert.deepEqual(combinations(math.bignumber(63), math.bignumber(7)), math.bignumber(553270671));
|
||||
// assert.deepEqual(combinations(math.bignumber(25), math.bignumber(6)), math.bignumber(177100));
|
||||
// });
|
||||
|
||||
it('should not work with non-integer and negative input', function() {
|
||||
assert.throws(function() {stirlingSecondKind(0.5, 3)}, TypeError);
|
||||
assert.throws(function() {stirlingSecondKind(3, 5)}, TypeError);
|
||||
// assert.throws(function() {stirlingSecondKind(math.bignumber(3), math.bignumber(5))}, TypeError);
|
||||
// assert.throws(function() {stirlingSecondKind(math.bignumber(3.5), math.bignumber(-3))}, TypeError);
|
||||
// assert.throws(function() {stirlingSecondKind(math.bignumber(3.5), 1/3)}, TypeError);
|
||||
});
|
||||
|
||||
it('should not work with the wrong number or type of arguments', function() {
|
||||
assert.throws(function() {stirlingSecondKind(5, 3, 2)});
|
||||
assert.throws(function() {stirlingSecondKind(true, "hello world")});
|
||||
});
|
||||
|
||||
it('should LaTeX stirlingSecondKind', function () {
|
||||
var expression = math.parse('stirlingSecondKind(3,2)');
|
||||
assert.equal(expression.toTex(), '\S{3}{2}');
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user