Equation.js/dist/operators.js

154 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-04-20 09:28:11 +00:00
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
2020-06-09 13:58:42 +00:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _index = require('./index');
2020-06-09 13:58:42 +00:00
var _index2 = _interopRequireDefault(_index);
2015-04-20 09:28:11 +00:00
/*
* Operators and Functions
* fn: function used to evaluate value
* format: the format using which arguments are parsed:
* 0 indicates an argument and 1 indicates the operator
* e.g: factorial is 01, add is 010, like 2!, 2+2
* precedence: determines which operators should be evaluated first
* the lower the value, the higher the precedence
* hasExpression: determines if any of the operator arguments are an expression
* This way, arguments will not be solved by equation and instead
* you have to call solve on each argument yourself.
* You get the arguments as parsed arrays sigma(0, 5, 2@) becomes
* sigma(0, 5, [2, '*', '@']). See sigma operator for reference
2015-04-20 09:28:11 +00:00
*/
exports['default'] = {
'^': {
fn: function fn(a, b) {
return Math.pow(a, b);
},
format: '010',
precedence: 0
},
'*': {
fn: function fn(a, b) {
return a * b;
},
format: '010',
precedence: 1
},
'/': {
fn: function fn(a, b) {
return a / b;
},
format: '010',
precedence: 1
},
'%': {
fn: function fn(a, b) {
return a % b;
},
format: '010',
precedence: 1
},
'\\': {
fn: function fn(a, b) {
return Math.floor(a / b);
},
format: '010',
precedence: 1
},
'+': {
fn: function fn(a, b) {
return a + b;
},
format: '010',
precedence: 2
},
'-': {
fn: function fn(a, b) {
return a - b;
},
format: '010',
precedence: 2
},
'!': {
fn: function fn(a) {
var sum = 1;
2020-06-09 13:58:42 +00:00
for (var i = 1; i <= a; ++i) {
2015-04-20 09:28:11 +00:00
sum *= i;
}
return sum;
},
format: '01',
precedence: 2
},
2020-06-09 13:58:42 +00:00
'log': {
2015-04-20 09:28:11 +00:00
fn: Math.log,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'ln': {
2015-04-20 09:28:11 +00:00
fn: Math.log,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'lg': {
2015-04-20 09:28:11 +00:00
fn: function fn(a) {
return Math.log(a) / Math.log(2);
},
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'sin': {
2015-04-20 09:28:11 +00:00
fn: Math.sin,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'cos': {
2015-04-20 09:28:11 +00:00
fn: Math.cos,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'tan': {
2015-04-20 09:28:11 +00:00
fn: Math.tan,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'cot': {
2015-04-20 09:28:11 +00:00
fn: Math.cot,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'round': {
fn: Math.round,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'floor': {
fn: Math.floor,
format: '10',
precedence: -1
},
2020-06-09 13:58:42 +00:00
'sigma': {
fn: function fn(from, to, expression) {
var expr = expression.join('').replace(/,/g, '');
var regex = new RegExp(ITERATOR_SIGN, 'g');
var sum = 0;
for (var i = from; i <= to; i++) {
2020-06-09 13:58:42 +00:00
sum += _index2['default'].solve(expr.replace(regex, i));
}
return sum;
},
format: '1000',
hasExpression: true,
precedence: -1
2015-04-20 09:28:11 +00:00
}
};
var ITERATOR_SIGN = '@';
2020-06-09 13:58:42 +00:00
module.exports = exports['default'];