hyperformula-dc
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
637 lines (561 loc) • 20.8 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import "core-js/modules/es.math.cosh.js";
import "core-js/modules/es.math.sinh.js";
import "core-js/modules/es.object.get-prototype-of.js";
import "core-js/modules/es.reflect.construct.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.symbol.description.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.symbol.iterator.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.array.slice.js";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.array.from.js";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* @license
* Copyright (c) 2021 Handsoncode. All rights reserved.
*/
import { CellError, ErrorType } from '../../Cell';
import { ErrorMessage } from '../../error-message';
import { coerceComplexToString } from '../ArithmeticHelper';
import { ArgumentTypes, FunctionPlugin } from './FunctionPlugin';
export var ComplexPlugin = /*#__PURE__*/function (_FunctionPlugin) {
_inherits(ComplexPlugin, _FunctionPlugin);
var _super = _createSuper(ComplexPlugin);
function ComplexPlugin() {
_classCallCheck(this, ComplexPlugin);
return _super.apply(this, arguments);
}
_createClass(ComplexPlugin, [{
key: "complex",
value: function complex(ast, state) {
return this.runFunction(ast.args, state, this.metadata('COMPLEX'), function (re, im, unit) {
if (unit !== 'i' && unit !== 'j') {
return new CellError(ErrorType.VALUE, ErrorMessage.ShouldBeIorJ);
}
return coerceComplexToString([re, im], unit);
});
}
}, {
key: "imabs",
value: function imabs(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMABS'), abs);
}
}, {
key: "imaginary",
value: function imaginary(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMAGINARY'), function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
re = _ref2[0],
im = _ref2[1];
return im;
});
}
}, {
key: "imreal",
value: function imreal(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMREAL'), function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
re = _ref4[0],
im = _ref4[1];
return re;
});
}
}, {
key: "imargument",
value: function imargument(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMARGUMENT'), function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
re = _ref6[0],
im = _ref6[1];
if (re === 0 && im === 0) {
return new CellError(ErrorType.DIV_BY_ZERO);
}
return Math.atan2(im, re);
});
}
}, {
key: "imconjugate",
value: function imconjugate(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCONJUGATE'), function (_ref7) {
var _ref8 = _slicedToArray(_ref7, 2),
re = _ref8[0],
im = _ref8[1];
return coerceComplexToString([re, -im]);
});
}
}, {
key: "imcos",
value: function imcos(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCOS'), function (arg) {
return coerceComplexToString(cos(arg));
});
}
}, {
key: "imcosh",
value: function imcosh(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCOSH'), function (arg) {
return coerceComplexToString(cosh(arg));
});
}
}, {
key: "imcot",
value: function imcot(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCOT'), function (arg) {
return coerceComplexToString(div(cos(arg), sin(arg)));
});
}
}, {
key: "imcsc",
value: function imcsc(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCSC'), function (arg) {
return coerceComplexToString(div([1, 0], sin(arg)));
});
}
}, {
key: "imcsch",
value: function imcsch(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMCSCH'), function (arg) {
return coerceComplexToString(div([1, 0], sinh(arg)));
});
}
}, {
key: "imsec",
value: function imsec(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSEC'), function (arg) {
return coerceComplexToString(div([1, 0], cos(arg)));
});
}
}, {
key: "imsech",
value: function imsech(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSECH'), function (arg) {
return coerceComplexToString(div([1, 0], cosh(arg)));
});
}
}, {
key: "imsin",
value: function imsin(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSIN'), function (arg) {
return coerceComplexToString(sin(arg));
});
}
}, {
key: "imsinh",
value: function imsinh(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSINH'), function (arg) {
return coerceComplexToString(sinh(arg));
});
}
}, {
key: "imtan",
value: function imtan(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMTAN'), function (arg) {
return coerceComplexToString(div(sin(arg), cos(arg)));
});
}
}, {
key: "imdiv",
value: function imdiv(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMDIV'), function (arg1, arg2) {
return coerceComplexToString(div(arg1, arg2));
});
}
}, {
key: "improduct",
value: function improduct(ast, state) {
var _this = this;
return this.runFunction(ast.args, state, this.metadata('IMPRODUCT'), function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var coerced = _this.arithmeticHelper.coerceComplexExactRanges(args);
if (coerced instanceof CellError) {
return coerced;
}
var prod = [1, 0];
var _iterator = _createForOfIteratorHelper(coerced),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var val = _step.value;
prod = mul(prod, val);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return coerceComplexToString(prod);
});
}
}, {
key: "imsum",
value: function imsum(ast, state) {
var _this2 = this;
return this.runFunction(ast.args, state, this.metadata('IMSUM'), function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var coerced = _this2.arithmeticHelper.coerceComplexExactRanges(args);
if (coerced instanceof CellError) {
return coerced;
}
var sum = [0, 0];
var _iterator2 = _createForOfIteratorHelper(coerced),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var val = _step2.value;
sum = add(sum, val);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return coerceComplexToString(sum);
});
}
}, {
key: "imsub",
value: function imsub(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSUB'), function (arg1, arg2) {
return coerceComplexToString(sub(arg1, arg2));
});
}
}, {
key: "imexp",
value: function imexp(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMEXP'), function (arg) {
return coerceComplexToString(exp(arg));
});
}
}, {
key: "imln",
value: function imln(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMLN'), function (arg) {
return coerceComplexToString(ln(arg));
});
}
}, {
key: "imlog10",
value: function imlog10(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMLOG10'), function (arg) {
var _ln = ln(arg),
_ln2 = _slicedToArray(_ln, 2),
re = _ln2[0],
im = _ln2[1];
var c = Math.log(10);
return coerceComplexToString([re / c, im / c]);
});
}
}, {
key: "imlog2",
value: function imlog2(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMLOG2'), function (arg) {
var _ln3 = ln(arg),
_ln4 = _slicedToArray(_ln3, 2),
re = _ln4[0],
im = _ln4[1];
var c = Math.log(2);
return coerceComplexToString([re / c, im / c]);
});
}
}, {
key: "impower",
value: function impower(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMPOWER'), function (arg, n) {
return coerceComplexToString(power(arg, n));
});
}
}, {
key: "imsqrt",
value: function imsqrt(ast, state) {
return this.runFunction(ast.args, state, this.metadata('IMSQRT'), function (arg) {
return coerceComplexToString(power(arg, 0.5));
});
}
}]);
return ComplexPlugin;
}(FunctionPlugin);
ComplexPlugin.implementedFunctions = {
'COMPLEX': {
method: 'complex',
parameters: [{
argumentType: ArgumentTypes.NUMBER
}, {
argumentType: ArgumentTypes.NUMBER
}, {
argumentType: ArgumentTypes.STRING,
defaultValue: 'i'
}]
},
'IMABS': {
method: 'imabs',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMAGINARY': {
method: 'imaginary',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMREAL': {
method: 'imreal',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMARGUMENT': {
method: 'imargument',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCONJUGATE': {
method: 'imconjugate',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCOS': {
method: 'imcos',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCOSH': {
method: 'imcosh',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCOT': {
method: 'imcot',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCSC': {
method: 'imcsc',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMCSCH': {
method: 'imcsch',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMSEC': {
method: 'imsec',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMSECH': {
method: 'imsech',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMSIN': {
method: 'imsin',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMSINH': {
method: 'imsinh',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMTAN': {
method: 'imtan',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMDIV': {
method: 'imdiv',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}, {
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMPRODUCT': {
method: 'improduct',
parameters: [{
argumentType: ArgumentTypes.ANY
}],
repeatLastArgs: 1
},
'IMSUM': {
method: 'imsum',
parameters: [{
argumentType: ArgumentTypes.ANY
}],
repeatLastArgs: 1
},
'IMSUB': {
method: 'imsub',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}, {
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMEXP': {
method: 'imexp',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMLN': {
method: 'imln',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMLOG10': {
method: 'imlog10',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMLOG2': {
method: 'imlog2',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
},
'IMPOWER': {
method: 'impower',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}, {
argumentType: ArgumentTypes.NUMBER
}]
},
'IMSQRT': {
method: 'imsqrt',
parameters: [{
argumentType: ArgumentTypes.COMPLEX
}]
}
};
function add(_ref9, _ref10) {
var _ref11 = _slicedToArray(_ref9, 2),
re1 = _ref11[0],
im1 = _ref11[1];
var _ref12 = _slicedToArray(_ref10, 2),
re2 = _ref12[0],
im2 = _ref12[1];
return [re1 + re2, im1 + im2];
}
function sub(_ref13, _ref14) {
var _ref15 = _slicedToArray(_ref13, 2),
re1 = _ref15[0],
im1 = _ref15[1];
var _ref16 = _slicedToArray(_ref14, 2),
re2 = _ref16[0],
im2 = _ref16[1];
return [re1 - re2, im1 - im2];
}
function mul(_ref17, _ref18) {
var _ref19 = _slicedToArray(_ref17, 2),
re1 = _ref19[0],
im1 = _ref19[1];
var _ref20 = _slicedToArray(_ref18, 2),
re2 = _ref20[0],
im2 = _ref20[1];
return [re1 * re2 - im1 * im2, re1 * im2 + re2 * im1];
}
function div(_ref21, _ref22) {
var _ref23 = _slicedToArray(_ref21, 2),
re1 = _ref23[0],
im1 = _ref23[1];
var _ref24 = _slicedToArray(_ref22, 2),
re2 = _ref24[0],
im2 = _ref24[1];
var denom = Math.pow(re2, 2) + Math.pow(im2, 2);
var _mul = mul([re1, im1], [re2, -im2]),
_mul2 = _slicedToArray(_mul, 2),
nomRe = _mul2[0],
nomIm = _mul2[1];
return [nomRe / denom, nomIm / denom];
}
function cos(_ref25) {
var _ref26 = _slicedToArray(_ref25, 2),
re = _ref26[0],
im = _ref26[1];
return [Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)];
}
function cosh(_ref27) {
var _ref28 = _slicedToArray(_ref27, 2),
re = _ref28[0],
im = _ref28[1];
return [Math.cosh(re) * Math.cos(im), Math.sinh(re) * Math.sin(im)];
}
function sin(_ref29) {
var _ref30 = _slicedToArray(_ref29, 2),
re = _ref30[0],
im = _ref30[1];
return [Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)];
}
function sinh(_ref31) {
var _ref32 = _slicedToArray(_ref31, 2),
re = _ref32[0],
im = _ref32[1];
return [Math.sinh(re) * Math.cos(im), Math.cosh(re) * Math.sin(im)];
}
function exp(_ref33) {
var _ref34 = _slicedToArray(_ref33, 2),
re = _ref34[0],
im = _ref34[1];
return [Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)];
}
function abs(_ref35) {
var _ref36 = _slicedToArray(_ref35, 2),
re = _ref36[0],
im = _ref36[1];
return Math.sqrt(re * re + im * im);
}
function ln(_ref37) {
var _ref38 = _slicedToArray(_ref37, 2),
re = _ref38[0],
im = _ref38[1];
return [Math.log(abs([re, im])), Math.atan2(im, re)];
}
function power(arg, n) {
var _ln5 = ln(arg),
_ln6 = _slicedToArray(_ln5, 2),
re = _ln6[0],
im = _ln6[1];
return exp([n * re, n * im]);
}