Commit b7b9053f authored by kosheo's avatar kosheo

Add nCr and nPr aliases to combinations and permutations functions respectively. Closes #1.

parent 86bb37c7
......@@ -22,6 +22,8 @@
else
return number.toString();
};
this._registerAliases();
};
/**
......@@ -170,4 +172,42 @@
return (typeof value !== 'undefined' && typeof value !== 'function');
};
/**
* Register aliases for MathJS functions
*/
clc.Calculator.prototype._registerAliases = function ()
{
var self = this;
// nCr(n, k) -> combinations(n, k)
if (!this._mathJs.nCr)
{
var nCr = this._mathJs.typed('nCr', {
'BigNumber, BigNumber': function (n, k)
{
return self._mathJs.combinations(n, k);
}
});
nCr.toTex = self._mathJs.combinations.toTex;
this._mathJs.import({ 'nCr': nCr });
}
// nPr(n) -> permutations(n)
// nPr(n, k) -> permutations(n, k)
if (!this._mathJs.nPr)
{
var nPr = this._mathJs.typed('nPr', {
'BigNumber': function (n)
{
return self._mathJs.permutations(n);
},
'BigNumber, BigNumber': function (n, k)
{
return self._mathJs.permutations(n, k);
}
});
this._mathJs.import({ 'nPr': nPr });
}
};
}(window.clc = window.clc || {}));
......@@ -113,6 +113,14 @@
'hex', 'bin'
];
/**
* Function aliases.
*/
clc.ALIASES =
[
'nCr', 'nPr'
];
/**
* List of keywords that will be autocompleted.
*/
......@@ -157,5 +165,5 @@
// Clear terminal
'clear'
].concat(clc.CLCALC_FUNCTIONS);
].concat(clc.CLCALC_FUNCTIONS, clc.ALIASES);
}(window.clc = window.clc || {}));
......@@ -7,7 +7,7 @@
clc.SyntaxHighlighter = function ()
{
this._constants = [].concat(clc.MATH_JS_CONSTANTS, [ 'true', 'false' ]);
this._keywords = [].concat(clc.MATH_JS_FUNCTIONS, clc.CLCALC_FUNCTIONS);
this._keywords = [].concat(clc.MATH_JS_FUNCTIONS, clc.CLCALC_FUNCTIONS, clc.ALIASES);
this._re = {
'identifier': new RegExp('([0-9]*)([A-Za-z_$][A-Za-z0-9_$]*)', 'g'),
......
......@@ -75,6 +75,8 @@ block content
a(href = '#function-plot') Function Plot
li
a(href = '#precision') Precision
li
a(href = '#aliases') Aliases
li
a(href = '#about') About
div.col-md-9.help-content
......@@ -661,6 +663,23 @@ block content
{ 'expression': 'abs(30!)', 'value': '265252859812191058636308480000000' },
{ 'expression': 'abs(30! + 0i)', 'value': '2.6525285981219107e+32' })
// Precision
h1(id = 'aliases') Aliases
p For convenience, Cl Calc introduces some function aliases to built-in MathJS functions:
table.table.table-striped
thead
tr
th Alias
th MathJS Function
tbody
tr
td.code nCr
td.code combinations
tr
td.code nPr
td.code permutations
// About
h1(id = 'about') About
......
describe ('Aliases', function ()
{
beforeEach(function() { jasmine.addCustomEqualityTester(texIgnoringResultEqualityTester); });
it ('Should process nCr exactly the same as combinations', function ()
{
var calc = createCalculatorInstance();
expect(calc.evaluate('nCr(0, 0)').result).toEqual(createResult('1'));
expect(calc.evaluate('nCr(5, 3)').result).toEqual(createResult('10'));
expect(calc.evaluate('nCr(7, 2)').tex).toEqual('\\binom{7}{2}');
expect(calc.evaluate('combinations(7, 2)').tex).toEqual('\\binom{7}{2}');
expect(function () { calc.evaluate('nCr(1, 2)'); }).toThrowError(TypeError);
expect(function () { calc.evaluate('nCr(1)'); }).toThrowError(TypeError);
expect(function () { calc.evaluate('nCr()'); }).toThrowError(TypeError);
});
it ('Should process nPr exactly the same as permutations', function ()
{
var calc = createCalculatorInstance();
expect(calc.evaluate('nPr(0, 0)').result).toEqual(createResult('1'));
expect(calc.evaluate('nPr(5, 3)').result).toEqual(createResult('60'));
expect(calc.evaluate('nPr(10)').result).toEqual(createResult('3628800'));
expect(calc.evaluate('nPr(7, 2)').tex).toEqual('\\mathrm{nPr}\\left(7,2\\right)');
expect(calc.evaluate('permutations(7, 2)').tex).toEqual('\\mathrm{permutations}\\left(7,2\\right)');
expect(function () { calc.evaluate('nPr(1, 2)'); }).toThrowError(TypeError);
expect(function () { calc.evaluate('nPr()'); }).toThrowError(TypeError);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment