From 9e17ef53dfa8e76c154aed7236bba55f2f3b651d Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Sun, 31 Mar 2024 00:29:19 -0300 Subject: [PATCH] refactor: Class and test restructuring, publish new version 1.1.6 --- packages/typescript/.gitignore | 3 + packages/typescript/.npmignore | 3 + packages/typescript/README.md | 2 +- packages/typescript/dist/Main.js | 578 +++++++++++++ .../typescript/dist/arithmeticOp/index.js | 18 - .../generateCriticalPointInterval/index.js | 10 - packages/typescript/dist/loganmatic.js | 577 +------------ .../dist/randomNumberBetween/index.js | 7 - packages/typescript/package.json | 86 +- packages/typescript/src/ExtendedsTests.ts | 31 + packages/typescript/src/Main.ts | 793 +++++++++++++++++ packages/typescript/src/arithmeticOp/index.ts | 24 - .../generateCriticalPointInterval/index.ts | 8 - packages/typescript/src/loganmatic.ts | 798 +----------------- .../src/randomNumberBetween/index.ts | 4 - .../typescript/tests/arithmeticOp.test.ts | 36 +- packages/typescript/tests/equations.test.ts | 18 +- packages/typescript/types/Main.d.ts | 171 ++++ .../typescript/types/arithmeticOp/index.d.ts | 6 - .../generateCriticalPointInterval/index.d.ts | 1 - packages/typescript/types/loganmatic.d.ts | 182 +--- .../types/randomNumberBetween/index.d.ts | 1 - 22 files changed, 1668 insertions(+), 1689 deletions(-) create mode 100644 packages/typescript/dist/Main.js delete mode 100644 packages/typescript/dist/generateCriticalPointInterval/index.js delete mode 100644 packages/typescript/dist/randomNumberBetween/index.js create mode 100644 packages/typescript/src/ExtendedsTests.ts create mode 100644 packages/typescript/src/Main.ts delete mode 100644 packages/typescript/src/generateCriticalPointInterval/index.ts delete mode 100644 packages/typescript/src/randomNumberBetween/index.ts create mode 100644 packages/typescript/types/Main.d.ts delete mode 100644 packages/typescript/types/generateCriticalPointInterval/index.d.ts delete mode 100644 packages/typescript/types/randomNumberBetween/index.d.ts diff --git a/packages/typescript/.gitignore b/packages/typescript/.gitignore index 25fbf5a..99a9c7a 100644 --- a/packages/typescript/.gitignore +++ b/packages/typescript/.gitignore @@ -1,2 +1,5 @@ node_modules/ coverage/ +ExtendedsTests.js +ExtendedsTests.d.ts +dist/types/ diff --git a/packages/typescript/.npmignore b/packages/typescript/.npmignore index 3fb0036..b542ba4 100644 --- a/packages/typescript/.npmignore +++ b/packages/typescript/.npmignore @@ -12,3 +12,6 @@ tests/ coverage/ jest.config.js tsconfig.types.json +ExtendedsTests.js +ExtendedsTests.d.ts +dist/types/ diff --git a/packages/typescript/README.md b/packages/typescript/README.md index 44ebf57..5bb2300 100644 --- a/packages/typescript/README.md +++ b/packages/typescript/README.md @@ -4,7 +4,7 @@ Now calculate the third degree root...

- Buy Me A Coffee + Buy Me A Coffee

diff --git a/packages/typescript/dist/Main.js b/packages/typescript/dist/Main.js new file mode 100644 index 0000000..ad84c82 --- /dev/null +++ b/packages/typescript/dist/Main.js @@ -0,0 +1,578 @@ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var arithmeticOp_1 = __importDefault(require("./arithmeticOp")); +var piValue = "3.1415926535897932384626433832795"; +var Calculator = (function (_super) { + __extends(Calculator, _super); + function Calculator() { + var _this = _super.call(this) || this; + _this.EulerNumber = _this.createEulerNumber(); + _this.Pi = parseFloat(piValue); + return _this; + } + Calculator.prototype.generateCriticalPointInterval = function (min, max) { + return this.randomNumberBetween(min, max); + }; + Calculator.prototype.createEulerNumber = function (n) { + if (n === void 0) { n = 9999; } + var sumStart = 0; + var emptyValue = 0; + for (var k = sumStart; k < n + 1; k++) { + var series = Math.pow(1, k) / this.factorial(k); + emptyValue += series; + } + return emptyValue; + }; + Calculator.prototype.newtonMethod = function (valueA, valueB, valueC, valueD, checkedYes) { + var derivedValueA = valueA * 3; + var derivedValueB = valueB * 2; + var derivedValueC = valueC * 1; + var delta = Math.pow(derivedValueB, 2) - 4 * derivedValueA * derivedValueC; + var answer1 = (-derivedValueB + Math.pow(delta, (1 / 2))) / (2 * derivedValueA); + var answer2 = (-derivedValueB - Math.pow(delta, (1 / 2))) / (2 * derivedValueA); + var criticalPoint1 = answer1 * 1000; + var criticalPoint2 = answer2 * 1000; + if (answer1 < 0 && answer2 < 0) { + criticalPoint1 = criticalPoint2 * -1; + } + else if (answer1 > 0 && answer2 > 0) { + criticalPoint1 = criticalPoint2 * -1; + } + var criticalPoint3; + if (answer1 > answer2) { + if (Number(answer1) - Number(answer2) > 1) { + criticalPoint3 = this.generateCriticalPointInterval(Math.ceil(Number(answer2) + 0.2), Number(answer1)); + } + else { + criticalPoint3 = this.generateCriticalPointInterval(Number(answer2) + 0.2, Number(answer1)); + } + } + else { + if (Number(answer2) - Number(answer1) > 1) { + criticalPoint3 = this.generateCriticalPointInterval(Math.ceil(Number(answer1) + 0.2), Number(answer2)); + } + else { + criticalPoint3 = this.generateCriticalPointInterval(Number(answer1) + 0.2, Number(answer2)); + } + } + if (delta < 0) { + criticalPoint1 = -10000; + criticalPoint2 = 9000; + } + var firstRootCritical = []; + var iterations = 100000; + for (var index = 0; index < iterations; index++) { + criticalPoint1 = + criticalPoint1 - + (Number(valueA) * Math.pow(criticalPoint1, 3) + + Number(valueB) * Math.pow(criticalPoint1, 2) + + Number(valueC) * criticalPoint1 + + Number(valueD)) / + (Number(derivedValueA) * Math.pow(criticalPoint1, 2) + + Number(derivedValueB) * criticalPoint1 + + Number(derivedValueC)); + var functionValue1 = (Number(valueA) * Math.pow(criticalPoint1, 3) + + Number(valueB) * Math.pow(criticalPoint1, 2) + + Number(valueC) * criticalPoint1 + + Number(valueD)).toFixed(10); + criticalPoint2 = + criticalPoint2 - + (Number(valueA) * Math.pow(criticalPoint2, 3) + + Number(valueB) * Math.pow(criticalPoint2, 2) + + Number(valueC) * criticalPoint2 + + Number(valueD)) / + (Number(derivedValueA) * Math.pow(criticalPoint2, 2) + + Number(derivedValueB) * criticalPoint2 + + Number(derivedValueC)); + var functionValue2 = (Number(valueA) * Math.pow(criticalPoint2, 3) + + Number(valueB) * Math.pow(criticalPoint2, 2) + + Number(valueC) * criticalPoint2 + + Number(valueD)).toFixed(10); + criticalPoint3 = + criticalPoint3 - + (Number(valueA) * Math.pow(criticalPoint3, 3) + + Number(valueB) * Math.pow(criticalPoint3, 2) + + Number(valueC) * criticalPoint3 + + Number(valueD)) / + (Number(derivedValueA) * Math.pow(criticalPoint3, 2) + + Number(derivedValueB) * criticalPoint3 + + Number(derivedValueC)); + if (parseFloat(functionValue1) === 0.0 && + parseFloat(functionValue2) === 0.0) { + firstRootCritical.push(criticalPoint1, criticalPoint2, criticalPoint3); + break; + } + } + if (firstRootCritical[0].toFixed(7) == firstRootCritical[1].toFixed(7)) { + if (checkedYes) { + return { + value: [firstRootCritical[0]], + msg: "It has only 1 real root in X = ".concat(firstRootCritical[0].toFixed(4)), + }; + } + else { + return { + value: [firstRootCritical[0]], + msg: "It has only 1 real root in X = ".concat(firstRootCritical[0]), + }; + } + } + else if (firstRootCritical[0].toFixed(4) == firstRootCritical[2].toFixed(4)) { + this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } + else if (firstRootCritical[1].toFixed(4) == firstRootCritical[2].toFixed(4)) { + this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } + else { + if (checkedYes) { + return { + value: [ + firstRootCritical[0], + firstRootCritical[1], + firstRootCritical[2], + ], + msg: "X1 \u2245 ".concat(firstRootCritical[0].toFixed(4), ", X2 \u2245 ").concat(firstRootCritical[1].toFixed(4), ", X3 \u2245 ").concat(firstRootCritical[2].toFixed(4)), + }; + } + else { + return { + value: [ + firstRootCritical[0], + firstRootCritical[1], + firstRootCritical[2], + ], + msg: "X1 \u2245 ".concat(firstRootCritical[0], ", X2 \u2245 ").concat(firstRootCritical[1], ", X3 \u2245 ").concat(firstRootCritical[2]), + }; + } + } + return { + value: [firstRootCritical[0]], + msg: "X1 = ".concat(firstRootCritical[0]), + }; + }; + Calculator.prototype.ruffiniDevice = function (valueA, valueB, valueC, valueD, roots, checkedYes) { + var first = valueA * roots[0]; + var secondCoefficient = Number(first) + Number(valueB); + var second = secondCoefficient * roots[0]; + var thirdCoefficient = Number(second) + Number(valueC); + var third = thirdCoefficient * roots[0]; + var fourthCoefficient = Number(third) + Number(valueD); + if (fourthCoefficient == 0) { + var delta = Math.pow(secondCoefficient, 2) - 4 * valueA * thirdCoefficient; + if (delta < 0) { + return { + value: [0], + msg: "It has only 1 real root in X = ".concat(roots[0]), + }; + } + else { + var answer1 = (-secondCoefficient + Math.pow(delta, (1 / 2))) / (2 * valueA); + var answer2 = (-secondCoefficient - Math.pow(delta, (1 / 2))) / (2 * valueA); + if (delta === 0) { + if (answer1 == roots[0]) { + return { + value: [0], + msg: "The value of X1 = 0 | X1 = X2 = X3", + }; + } + else { + if (checkedYes) { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1.toFixed(2), " | X2 = X3"), + }; + } + else { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X2 = X3"), + }; + } + } + } + else { + if (answer1 == roots[0]) { + if (checkedYes) { + return { + value: [roots[0], answer2], + msg: "The value of X1 = ".concat(roots[0], " and X2 = ").concat(answer2.toFixed(2), " | X1 = X3"), + }; + } + else { + return { + value: [roots[0], answer2], + msg: "The value of X1 = ".concat(roots[0], " and X2 = ").concat(answer2, " | X1 = X3"), + }; + } + } + else if (answer2 == roots[0]) { + if (checkedYes) { + return { + value: [roots[0], answer1], + msg: "The value of X1 = ".concat(roots[0], " and X2 it's the same as: ").concat(answer1.toFixed(2), " | X1 = X3"), + }; + } + else { + return { + value: [roots[0], answer1], + msg: "The value of X1 = ".concat(roots[0], " and X2 it's the same as: ").concat(answer1, " | X1 = X3"), + }; + } + } + else { + if (checkedYes) { + return { + value: [roots[0], answer1, answer2], + msg: "The value of X1 = ".concat(roots[0], ", X2 it's the same as: ").concat(answer1.toFixed(2), " and The value of X3 it's the same as: ").concat(answer2.toFixed(2)), + }; + } + else { + return { + value: [roots[0], answer1, answer2], + msg: "The value of X1 = ".concat(roots[0], ", X2 it's the same as: ").concat(answer1, " and The value of X3 it's the same as: ").concat(answer2), + }; + } + } + } + } + } + else if (fourthCoefficient != 0) { + return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } + else { + return { + value: null, + msg: "Vish, I don't know what happened HEHEHE", + }; + } + }; + Calculator.prototype.absoluteValue = function (number) { + if (number < 0) { + return -number; + } + else { + return number; + } + }; + Calculator.prototype.factorial = function (valueToCalculate) { + if (valueToCalculate === 0) { + return 1; + } + var result = 1; + for (var i = 1; i <= valueToCalculate; i++) { + result *= i; + } + return result; + }; + Calculator.prototype.squareRoot = function (valueToCalculate) { + return Math.pow(valueToCalculate, (1 / 2)); + }; + Calculator.prototype.cubicRoot = function (valueToCalculate) { + var convertToPositive = this.absoluteValue(valueToCalculate); + var result = Math.pow(convertToPositive, (1 / 3)); + if (valueToCalculate < 0) { + return result * -1; + } + else { + return result; + } + }; + Calculator.prototype.factor = function (valueToCalculate) { + if (typeof valueToCalculate !== "number") { + return console.log("This is not an integer"); + } + var factoredNumbers = []; + for (var y = 2; y < valueToCalculate; y++) { + while (valueToCalculate % y === 0) { + valueToCalculate /= y; + factoredNumbers.push(y); + } + } + if (factoredNumbers.length === 0) { + factoredNumbers.push(valueToCalculate); + } + return factoredNumbers; + }; + Calculator.prototype.sine = function (valueToCalculate) { + var n; + if (this.absoluteValue(valueToCalculate) > 5 && + this.absoluteValue(valueToCalculate) <= 30) { + n = 100; + } + else if (this.absoluteValue(valueToCalculate) > 30) { + n = 40; + } + else { + n = 200; + } + var startSum = 0; + var emptyValue = 0; + for (var k = startSum; k < n + 1; k++) { + var series = (Math.pow((-1), k) * Math.pow(valueToCalculate, (2 * k + 1))) / + this.factorial(2 * k + 1); + emptyValue += series; + } + if (this.absoluteValue(emptyValue) < 0.00000001) { + return 0; + } + else { + return emptyValue; + } + }; + Calculator.prototype.cosine = function (valueToCalculate) { + var n; + if (this.absoluteValue(valueToCalculate) > 5 && + this.absoluteValue(valueToCalculate) <= 30) { + n = 100; + } + else if (this.absoluteValue(valueToCalculate) > 30) { + n = 40; + } + else { + n = 200; + } + var startSum = 0; + var emptyValue = 0; + for (var k = startSum; k < n + 1; k++) { + var series = (Math.pow((-1), k) * Math.pow(valueToCalculate, (2 * k))) / this.factorial(2 * k); + emptyValue += series; + } + if (this.absoluteValue(emptyValue) < 0.00000001) { + return 0; + } + else { + return emptyValue; + } + }; + Calculator.prototype.gcd = function (valuesToCalculate) { + var mdcValue = valuesToCalculate[0]; + for (var i = 1; i < valuesToCalculate.length; i++) { + var a = mdcValue; + var b = valuesToCalculate[i]; + var rest = void 0; + while (b !== 0) { + rest = a % b; + a = b; + b = rest; + } + mdcValue = a; + } + return this.absoluteValue(mdcValue); + }; + Calculator.prototype.lcm = function (valuesToCalculate) { + var mmcValue = valuesToCalculate[0]; + for (var i = 1; i < valuesToCalculate.length; i++) { + var a = mmcValue; + var b = valuesToCalculate[i]; + var rest = void 0; + var mdcValue = a; + while (b !== 0) { + rest = a % b; + a = b; + b = rest; + } + mmcValue = (mdcValue * valuesToCalculate[i]) / a; + } + return this.absoluteValue(mmcValue); + }; + Calculator.prototype.randomNumberBetween = function (min, max) { + var timestamp = Date.now(); + return min + (timestamp % (max - min + 1)); + }; + Calculator.prototype.linearEquation = function (a, b) { + var numberA = Number(a); + var numberB = Number(b); + if (numberA === 0) { + return { + value: null, + msg: "The value of 'a' cannot be 0", + }; + } + var root = -numberB / numberA; + return { + value: root, + msg: "The value of x is the same as: ".concat(root), + }; + }; + Calculator.prototype.quadraticEquation = function (a, b, c) { + var numberA = Number(a); + var numberB = Number(b); + var numberC = Number(c); + if (Math.pow(numberB, 2) - 4 * numberA * numberC < 0) + return { + value: null, + msg: "The equation does not have real roots", + }; + if (numberA === 0 && numberB === 0) { + return { + value: null, + msg: "The values of 'a' and 'b' cannot be 0 at the same time", + }; + } + else { + var root1 = (-numberB + this.squareRoot(Math.pow(b, 2) - 4 * numberA * numberC)) / + (2 * numberA); + var root2 = (-numberB - this.squareRoot(Math.pow(b, 2) - 4 * numberA * numberC)) / + (2 * numberA); + if (root1 === root2) { + return { + value: [root1], + msg: "It has only 1 real root in X = ".concat(root1), + }; + } + else { + return { + value: [root1, root2], + msg: "The value of X1 = ".concat(root1, " and X2 = ").concat(root2), + }; + } + } + }; + Calculator.prototype.cubicEquation = function (a, b, c, d, approximate) { + if (a === void 0) { a = 0; } + if (b === void 0) { b = 0; } + if (c === void 0) { c = 0; } + if (d === void 0) { d = 0; } + if (approximate === void 0) { approximate = false; } + var checkedYes = approximate; + var valueA = Number(a); + var valueB = Number(b); + var valueC = Number(c); + var valueD = Number(d); + if (valueD == 0) { + var x1 = 0; + var delta = Math.pow(valueB, 2) - 4 * valueA * valueC; + if (delta < 0) { + return { + value: [0], + msg: "It has only 1 real root in X = 0", + }; + } + else { + var answer1 = (-valueB + Math.pow(delta, (1 / 2))) / (2 * valueA); + var answer2 = (-valueB - Math.pow(delta, (1 / 2))) / (2 * valueA); + if (delta === 0) { + if (answer1 == x1) { + return { + value: [0], + msg: "The value of X1 = 0 | X1 = X2 = X3", + }; + } + else { + if (checkedYes) { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1.toFixed(2), " | X2 = X3"), + }; + } + else { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X2 = X3"), + }; + } + } + } + else { + if (answer1 == x1) { + if (checkedYes) { + return { + value: [0, answer2], + msg: "The value of X1 = 0 and X2 = ".concat(answer2.toFixed(2), " | X1 = X3"), + }; + } + else { + return { + value: [0, answer2], + msg: "The value of X1 = 0 and X2 = ".concat(answer2, " | X1 = X3"), + }; + } + } + else if (answer2 == x1) { + if (checkedYes) { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 = ".concat(answer1.toFixed(2), " | X1 = X3"), + }; + } + else { + return { + value: [0, answer1], + msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X1 = X3"), + }; + } + } + else { + if (checkedYes) { + return { + value: [0, answer1, answer2], + msg: "The value of X1 = 0, X2 it's the same as: ".concat(answer1.toFixed(2), " and The value of X3 it's the same as: ").concat(answer2.toFixed(2)), + }; + } + else { + return { + value: [0, answer1, answer2], + msg: "The value of X1 = 0, X2 it's the same as: ".concat(answer1, " and The value of X3 it's the same as: ").concat(answer2), + }; + } + } + } + } + } + else { + var possibleRoots = []; + if (valueD > 0) { + for (var index = 1; index < Number(valueD) + 1; index++) { + var isInteger = valueD % index; + if (isInteger == 0) { + possibleRoots.push(index); + possibleRoots.push(-index); + } + } + } + else { + for (var index = -1; index > Number(valueD) - 1; index--) { + var isInteger = valueD % index; + if (isInteger == 0) { + possibleRoots.push(index); + possibleRoots.push(-index); + } + } + } + var roots_1 = []; + possibleRoots.forEach(function (test) { + var primeiraRaiz = Number(valueA) * Math.pow(test, 3) + + Number(valueB) * Math.pow(test, 2) + + Number(valueC) * test + + Number(valueD); + if (primeiraRaiz == 0) { + roots_1.push(test); + } + }); + if (roots_1.length === 0) { + return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } + return this.ruffiniDevice(valueA, valueB, valueC, valueD, roots_1, checkedYes); + } + }; + return Calculator; +}(arithmeticOp_1.default)); +exports.default = Calculator; diff --git a/packages/typescript/dist/arithmeticOp/index.js b/packages/typescript/dist/arithmeticOp/index.js index 0e167b7..9415577 100644 --- a/packages/typescript/dist/arithmeticOp/index.js +++ b/packages/typescript/dist/arithmeticOp/index.js @@ -45,24 +45,6 @@ var ArithmeticOp = (function () { } return result; }; - ArithmeticOp.prototype.getSum = function () { - return this.sum; - }; - ArithmeticOp.prototype.getSub = function () { - return this.sub; - }; - ArithmeticOp.prototype.getMul = function () { - return this.mul; - }; - ArithmeticOp.prototype.getDiv = function () { - return this.div; - }; - ArithmeticOp.prototype.getMod = function () { - return this.mod; - }; - ArithmeticOp.prototype.getPower = function () { - return this.power; - }; return ArithmeticOp; }()); exports.default = ArithmeticOp; diff --git a/packages/typescript/dist/generateCriticalPointInterval/index.js b/packages/typescript/dist/generateCriticalPointInterval/index.js deleted file mode 100644 index 048392a..0000000 --- a/packages/typescript/dist/generateCriticalPointInterval/index.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -var randomNumberBetween_1 = __importDefault(require("../randomNumberBetween")); -function generateCriticalPointInterval(min, max) { - return (0, randomNumberBetween_1.default)(min, max); -} -exports.default = generateCriticalPointInterval; diff --git a/packages/typescript/dist/loganmatic.js b/packages/typescript/dist/loganmatic.js index 752da61..afa6884 100644 --- a/packages/typescript/dist/loganmatic.js +++ b/packages/typescript/dist/loganmatic.js @@ -1,578 +1,7 @@ "use strict"; -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; -var arithmeticOp_1 = __importDefault(require("./arithmeticOp")); -var generateCriticalPointInterval_1 = __importDefault(require("./generateCriticalPointInterval")); -var randomNumberBetween_1 = __importDefault(require("./randomNumberBetween")); -var piValue = "3.1415926535897932384626433832795"; -var Calculator = (function (_super) { - __extends(Calculator, _super); - function Calculator() { - var _this = _super.call(this) || this; - _this.EulerNumber = _this.createEulerNumber(); - _this.Pi = parseFloat(piValue); - return _this; - } - Calculator.prototype.createEulerNumber = function (n) { - if (n === void 0) { n = 9999; } - var sumStart = 0; - var emptyValue = 0; - for (var k = sumStart; k < n + 1; k++) { - var series = Math.pow(1, k) / this.factorial(k); - emptyValue += series; - } - return emptyValue; - }; - Calculator.prototype.newtonMethod = function (valueA, valueB, valueC, valueD, checkedYes) { - var derivedValueA = valueA * 3; - var derivedValueB = valueB * 2; - var derivedValueC = valueC * 1; - var delta = Math.pow(derivedValueB, 2) - 4 * derivedValueA * derivedValueC; - var answer1 = (-derivedValueB + Math.pow(delta, (1 / 2))) / (2 * derivedValueA); - var answer2 = (-derivedValueB - Math.pow(delta, (1 / 2))) / (2 * derivedValueA); - var criticalPoint1 = answer1 * 1000; - var criticalPoint2 = answer2 * 1000; - if (answer1 < 0 && answer2 < 0) { - criticalPoint1 = criticalPoint2 * -1; - } - else if (answer1 > 0 && answer2 > 0) { - criticalPoint1 = criticalPoint2 * -1; - } - var criticalPoint3; - if (answer1 > answer2) { - if (Number(answer1) - Number(answer2) > 1) { - criticalPoint3 = (0, generateCriticalPointInterval_1.default)(Math.ceil(Number(answer2) + 0.2), Number(answer1)); - } - else { - criticalPoint3 = (0, generateCriticalPointInterval_1.default)(Number(answer2) + 0.2, Number(answer1)); - } - } - else { - if (Number(answer2) - Number(answer1) > 1) { - criticalPoint3 = (0, generateCriticalPointInterval_1.default)(Math.ceil(Number(answer1) + 0.2), Number(answer2)); - } - else { - criticalPoint3 = (0, generateCriticalPointInterval_1.default)(Number(answer1) + 0.2, Number(answer2)); - } - } - if (delta < 0) { - criticalPoint1 = -10000; - criticalPoint2 = 9000; - } - var firstRootCritical = []; - var iterations = 100000; - for (var index = 0; index < iterations; index++) { - criticalPoint1 = - criticalPoint1 - - (Number(valueA) * Math.pow(criticalPoint1, 3) + - Number(valueB) * Math.pow(criticalPoint1, 2) + - Number(valueC) * criticalPoint1 + - Number(valueD)) / - (Number(derivedValueA) * Math.pow(criticalPoint1, 2) + - Number(derivedValueB) * criticalPoint1 + - Number(derivedValueC)); - var functionValue1 = (Number(valueA) * Math.pow(criticalPoint1, 3) + - Number(valueB) * Math.pow(criticalPoint1, 2) + - Number(valueC) * criticalPoint1 + - Number(valueD)).toFixed(10); - criticalPoint2 = - criticalPoint2 - - (Number(valueA) * Math.pow(criticalPoint2, 3) + - Number(valueB) * Math.pow(criticalPoint2, 2) + - Number(valueC) * criticalPoint2 + - Number(valueD)) / - (Number(derivedValueA) * Math.pow(criticalPoint2, 2) + - Number(derivedValueB) * criticalPoint2 + - Number(derivedValueC)); - var functionValue2 = (Number(valueA) * Math.pow(criticalPoint2, 3) + - Number(valueB) * Math.pow(criticalPoint2, 2) + - Number(valueC) * criticalPoint2 + - Number(valueD)).toFixed(10); - criticalPoint3 = - criticalPoint3 - - (Number(valueA) * Math.pow(criticalPoint3, 3) + - Number(valueB) * Math.pow(criticalPoint3, 2) + - Number(valueC) * criticalPoint3 + - Number(valueD)) / - (Number(derivedValueA) * Math.pow(criticalPoint3, 2) + - Number(derivedValueB) * criticalPoint3 + - Number(derivedValueC)); - if (parseFloat(functionValue1) === 0.0 && - parseFloat(functionValue2) === 0.0) { - firstRootCritical.push(criticalPoint1, criticalPoint2, criticalPoint3); - break; - } - } - if (firstRootCritical[0].toFixed(7) == firstRootCritical[1].toFixed(7)) { - if (checkedYes) { - return { - value: [firstRootCritical[0]], - msg: "It has only 1 real root in X = ".concat(firstRootCritical[0].toFixed(4)), - }; - } - else { - return { - value: [firstRootCritical[0]], - msg: "It has only 1 real root in X = ".concat(firstRootCritical[0]), - }; - } - } - else if (firstRootCritical[0].toFixed(4) == firstRootCritical[2].toFixed(4)) { - this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } - else if (firstRootCritical[1].toFixed(4) == firstRootCritical[2].toFixed(4)) { - this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } - else { - if (checkedYes) { - return { - value: [ - firstRootCritical[0], - firstRootCritical[1], - firstRootCritical[2], - ], - msg: "X1 \u2245 ".concat(firstRootCritical[0].toFixed(4), ", X2 \u2245 ").concat(firstRootCritical[1].toFixed(4), ", X3 \u2245 ").concat(firstRootCritical[2].toFixed(4)), - }; - } - else { - return { - value: [ - firstRootCritical[0], - firstRootCritical[1], - firstRootCritical[2], - ], - msg: "X1 \u2245 ".concat(firstRootCritical[0], ", X2 \u2245 ").concat(firstRootCritical[1], ", X3 \u2245 ").concat(firstRootCritical[2]), - }; - } - } - return { - value: [firstRootCritical[0]], - msg: "X1 = ".concat(firstRootCritical[0]), - }; - }; - Calculator.prototype.ruffiniDevice = function (valueA, valueB, valueC, valueD, raizes, checkedYes) { - var first = valueA * raizes[0]; - var secondCoefficient = Number(first) + Number(valueB); - var second = secondCoefficient * raizes[0]; - var thirdCoefficient = Number(second) + Number(valueC); - var third = thirdCoefficient * raizes[0]; - var fourthCoefficient = Number(third) + Number(valueD); - if (fourthCoefficient == 0) { - var delta = Math.pow(secondCoefficient, 2) - 4 * valueA * thirdCoefficient; - if (delta < 0) { - return { - value: [0], - msg: "It has only 1 real root in X = ".concat(raizes[0]), - }; - } - else { - var answer1 = (-secondCoefficient + Math.pow(delta, (1 / 2))) / (2 * valueA); - var answer2 = (-secondCoefficient - Math.pow(delta, (1 / 2))) / (2 * valueA); - if (delta === 0) { - if (answer1 == raizes[0]) { - return { - value: [0], - msg: "The value of X1 = 0 | X1 = X2 = X3", - }; - } - else { - if (checkedYes) { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1.toFixed(2), " | X2 = X3"), - }; - } - else { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X2 = X3"), - }; - } - } - } - else { - if (answer1 == raizes[0]) { - if (checkedYes) { - return { - value: [raizes[0], answer2], - msg: "The value of X1 = ".concat(raizes[0], " and X2 = ").concat(answer2.toFixed(2), " | X1 = X3"), - }; - } - else { - return { - value: [raizes[0], answer2], - msg: "The value of X1 = ".concat(raizes[0], " and X2 = ").concat(answer2, " | X1 = X3"), - }; - } - } - else if (answer2 == raizes[0]) { - if (checkedYes) { - return { - value: [raizes[0], answer1], - msg: "The value of X1 = ".concat(raizes[0], " and X2 it's the same as: ").concat(answer1.toFixed(2), " | X1 = X3"), - }; - } - else { - return { - value: [raizes[0], answer1], - msg: "The value of X1 = ".concat(raizes[0], " and X2 it's the same as: ").concat(answer1, " | X1 = X3"), - }; - } - } - else { - if (checkedYes) { - return { - value: [raizes[0], answer1, answer2], - msg: "The value of X1 = ".concat(raizes[0], ", X2 it's the same as: ").concat(answer1.toFixed(2), " and The value of X3 it's the same as: ").concat(answer2.toFixed(2)), - }; - } - else { - return { - value: [raizes[0], answer1, answer2], - msg: "The value of X1 = ".concat(raizes[0], ", X2 it's the same as: ").concat(answer1, " and The value of X3 it's the same as: ").concat(answer2), - }; - } - } - } - } - } - else if (fourthCoefficient != 0) { - return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } - else { - return { - value: null, - msg: "Vish, I don't know what happened HEHEHE", - }; - } - }; - Calculator.prototype.absoluteValue = function (number) { - if (number < 0) { - return -number; - } - else { - return number; - } - }; - Calculator.prototype.factorial = function (valueToCalculate) { - if (valueToCalculate === 0) { - return 1; - } - var result = 1; - for (var i = 1; i <= valueToCalculate; i++) { - result *= i; - } - return result; - }; - Calculator.prototype.squareRoot = function (valueToCalculate) { - return Math.pow(valueToCalculate, (1 / 2)); - }; - Calculator.prototype.cubicRoot = function (valueToCalculate) { - var convertToPositive = this.absoluteValue(valueToCalculate); - var result = Math.pow(convertToPositive, (1 / 3)); - if (valueToCalculate < 0) { - return result * -1; - } - else { - return result; - } - }; - Calculator.prototype.factor = function (valueToCalculate) { - if (typeof valueToCalculate !== "number") { - return console.log("This is not an integer"); - } - var factoredNumbers = []; - for (var y = 2; y < valueToCalculate; y++) { - while (valueToCalculate % y === 0) { - valueToCalculate /= y; - factoredNumbers.push(y); - } - } - if (factoredNumbers.length === 0) { - factoredNumbers.push(valueToCalculate); - } - return factoredNumbers; - }; - Calculator.prototype.sine = function (valueToCalculate) { - var n; - if (this.absoluteValue(valueToCalculate) > 5 && - this.absoluteValue(valueToCalculate) <= 30) { - n = 100; - } - else if (this.absoluteValue(valueToCalculate) > 30) { - n = 40; - } - else { - n = 200; - } - var startSum = 0; - var emptyValue = 0; - for (var k = startSum; k < n + 1; k++) { - var series = (Math.pow((-1), k) * Math.pow(valueToCalculate, (2 * k + 1))) / - this.factorial(2 * k + 1); - emptyValue += series; - } - if (this.absoluteValue(emptyValue) < 0.00000001) { - return 0; - } - else { - return emptyValue; - } - }; - Calculator.prototype.cosine = function (valueToCalculate) { - var n; - if (this.absoluteValue(valueToCalculate) > 5 && - this.absoluteValue(valueToCalculate) <= 30) { - n = 100; - } - else if (this.absoluteValue(valueToCalculate) > 30) { - n = 40; - } - else { - n = 200; - } - var startSum = 0; - var emptyValue = 0; - for (var k = startSum; k < n + 1; k++) { - var series = (Math.pow((-1), k) * Math.pow(valueToCalculate, (2 * k))) / this.factorial(2 * k); - emptyValue += series; - } - if (this.absoluteValue(emptyValue) < 0.00000001) { - return 0; - } - else { - return emptyValue; - } - }; - Calculator.prototype.gcd = function (valuesToCalculate) { - var mdcValue = valuesToCalculate[0]; - for (var i = 1; i < valuesToCalculate.length; i++) { - var a = mdcValue; - var b = valuesToCalculate[i]; - var rest = void 0; - while (b !== 0) { - rest = a % b; - a = b; - b = rest; - } - mdcValue = a; - } - return this.absoluteValue(mdcValue); - }; - Calculator.prototype.lcm = function (valuesToCalculate) { - var mmcValue = valuesToCalculate[0]; - for (var i = 1; i < valuesToCalculate.length; i++) { - var a = mmcValue; - var b = valuesToCalculate[i]; - var rest = void 0; - var mdcValue = a; - while (b !== 0) { - rest = a % b; - a = b; - b = rest; - } - mmcValue = (mdcValue * valuesToCalculate[i]) / a; - } - return this.absoluteValue(mmcValue); - }; - Calculator.prototype.randomNumberBetween = function (min, max) { - return (0, randomNumberBetween_1.default)(min, max); - }; - Calculator.prototype.linearEquation = function (a, b) { - var numberA = Number(a); - var numberB = Number(b); - if (numberA === 0) { - return { - value: null, - msg: "The value of 'a' cannot be 0", - }; - } - var root = -numberB / numberA; - return { - value: root, - msg: "The value of x is the same as: ".concat(root), - }; - }; - Calculator.prototype.quadraticEquation = function (a, b, c) { - var numberA = Number(a); - var numberB = Number(b); - var numberC = Number(c); - if (Math.pow(numberB, 2) - 4 * numberA * numberC < 0) - return { - value: null, - msg: "The equation does not have real roots", - }; - if (numberA === 0 && numberB === 0) { - return { - value: null, - msg: "The values of 'a' and 'b' cannot be 0 at the same time", - }; - } - else { - var root1 = (-numberB + this.squareRoot(Math.pow(b, 2) - 4 * numberA * numberC)) / - (2 * numberA); - var root2 = (-numberB - this.squareRoot(Math.pow(b, 2) - 4 * numberA * numberC)) / - (2 * numberA); - if (root1 === root2) { - return { - value: [root1], - msg: "It has only 1 real root in X = ".concat(root1), - }; - } - else { - return { - value: [root1, root2], - msg: "The value of X1 = ".concat(root1, " and X2 = ").concat(root2), - }; - } - } - }; - Calculator.prototype.cubicEquation = function (a, b, c, d, approximate) { - if (a === void 0) { a = 0; } - if (b === void 0) { b = 0; } - if (c === void 0) { c = 0; } - if (d === void 0) { d = 0; } - if (approximate === void 0) { approximate = false; } - var checkedYes = approximate; - var valueA = Number(a); - var valueB = Number(b); - var valueC = Number(c); - var valueD = Number(d); - if (valueD == 0) { - var x1 = 0; - var delta = Math.pow(valueB, 2) - 4 * valueA * valueC; - if (delta < 0) { - return { - value: [0], - msg: "It has only 1 real root in X = 0", - }; - } - else { - var answer1 = (-valueB + Math.pow(delta, (1 / 2))) / (2 * valueA); - var answer2 = (-valueB - Math.pow(delta, (1 / 2))) / (2 * valueA); - if (delta === 0) { - if (answer1 == x1) { - return { - value: [0], - msg: "The value of X1 = 0 | X1 = X2 = X3", - }; - } - else { - if (checkedYes) { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1.toFixed(2), " | X2 = X3"), - }; - } - else { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X2 = X3"), - }; - } - } - } - else { - if (answer1 == x1) { - if (checkedYes) { - return { - value: [0, answer2], - msg: "The value of X1 = 0 and X2 = ".concat(answer2.toFixed(2), " | X1 = X3"), - }; - } - else { - return { - value: [0, answer2], - msg: "The value of X1 = 0 and X2 = ".concat(answer2, " | X1 = X3"), - }; - } - } - else if (answer2 == x1) { - if (checkedYes) { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 = ".concat(answer1.toFixed(2), " | X1 = X3"), - }; - } - else { - return { - value: [0, answer1], - msg: "The value of X1 = 0 and X2 is equal to: ".concat(answer1, " | X1 = X3"), - }; - } - } - else { - if (checkedYes) { - return { - value: [0, answer1, answer2], - msg: "The value of X1 = 0, X2 it's the same as: ".concat(answer1.toFixed(2), " and The value of X3 it's the same as: ").concat(answer2.toFixed(2)), - }; - } - else { - return { - value: [0, answer1, answer2], - msg: "The value of X1 = 0, X2 it's the same as: ".concat(answer1, " and The value of X3 it's the same as: ").concat(answer2), - }; - } - } - } - } - } - else { - var possibleRoots = []; - if (valueD > 0) { - for (var index = 1; index < Number(valueD) + 1; index++) { - var isInteger = valueD % index; - if (isInteger == 0) { - possibleRoots.push(index); - possibleRoots.push(-index); - } - } - } - else { - for (var index = -1; index > Number(valueD) - 1; index--) { - var isInteger = valueD % index; - if (isInteger == 0) { - possibleRoots.push(index); - possibleRoots.push(-index); - } - } - } - var raizes_1 = []; - possibleRoots.forEach(function (test) { - var primeiraRaiz = Number(valueA) * Math.pow(test, 3) + - Number(valueB) * Math.pow(test, 2) + - Number(valueC) * test + - Number(valueD); - if (primeiraRaiz == 0) { - raizes_1.push(test); - } - }); - if (raizes_1.length === 0) { - return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } - return this.ruffiniDevice(valueA, valueB, valueC, valueD, raizes_1, checkedYes); - } - }; - Calculator.prototype.getRufinhoDevice = function () { - return this.ruffiniDevice; - }; - return Calculator; -}(arithmeticOp_1.default)); -module.exports = new Calculator(); +var Main_1 = __importDefault(require("./Main")); +var calculator = new Main_1.default(); +module.exports = calculator; diff --git a/packages/typescript/dist/randomNumberBetween/index.js b/packages/typescript/dist/randomNumberBetween/index.js deleted file mode 100644 index 415145d..0000000 --- a/packages/typescript/dist/randomNumberBetween/index.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -function randomNumberBetween(min, max) { - var timestamp = Date.now(); - return min + (timestamp % (max - min + 1)); -} -exports.default = randomNumberBetween; diff --git a/packages/typescript/package.json b/packages/typescript/package.json index 641f9bc..34be8c4 100644 --- a/packages/typescript/package.json +++ b/packages/typescript/package.json @@ -1,41 +1,51 @@ { - "name": "loganmatic", - "version": "1.1.4", - "description": "Biblioteca de matematica com algumas funções para facilitar calculos", - "publisher": "Gabriel Logan", - "main": "./dist/loganmatic.js", - "types": "./types/loganmatic.d.ts", - "scripts": { - "test": "npx jest --coverage --verbose", + "name": "loganmatic", + "version": "1.1.6", + "description": "Math library with some functions to facilitate calculations", + "publisher": "Gabriel Logan", + "main": "./dist/loganmatic.js", + "types": "./types/loganmatic.d.ts", + "scripts": { + "test": "npx jest --coverage --verbose", "test:watch": "npx jest --watch", - "build": "npx tsc", - "build:types": "tsc -p tsconfig.types.json" - }, - "keywords": [ - "matemática", - "cálculo", - "funções", - "biblioteca" - ], - "author": { - "name": "Gabriel Logan", - "url": "https://github.com/gabriel-logan/Math_Lib/" - }, - "bugs": { - "url": "https://github.com/gabriel-logan/Math_Lib//issues" - }, - "homepage": "https://github.com/gabriel-logan/Math_Lib/#readme", - "license": "ISC", - "devDependencies": { - "@types/jest": "^29.5.12", - "@typescript-eslint/eslint-plugin": "^7.4.0", - "@typescript-eslint/parser": "^7.4.0", - "eslint": "^8.57.0", - "eslint-config-prettier": "^9.1.0", - "eslint-plugin-prettier": "^5.1.3", - "jest": "^29.7.0", - "prettier": "^3.2.5", - "ts-jest": "^29.1.2", - "typescript": "^5.4.3" - } + "build": "npx tsc", + "build:types": "tsc -p tsconfig.types.json" + }, + "keywords": [ + "math", + "calculation", + "functions", + "library", + "mathematics", + "algebra", + "geometry", + "trigonometry", + "matematica", + "calculo", + "funcoes", + "biblioteca", + "equations", + "equacoes" + ], + "author": { + "name": "Gabriel Logan", + "url": "https://github.com/gabriel-logan/Math_Lib/" + }, + "bugs": { + "url": "https://github.com/gabriel-logan/Math_Lib/issues" + }, + "homepage": "https://gabriel-logan.github.io/Math_Lib/typescript", + "license": "ISC", + "devDependencies": { + "@types/jest": "^29.5.12", + "@typescript-eslint/eslint-plugin": "^7.4.0", + "@typescript-eslint/parser": "^7.4.0", + "eslint": "^8.57.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.1.3", + "jest": "^29.7.0", + "prettier": "^3.2.5", + "ts-jest": "^29.1.2", + "typescript": "^5.4.3" + } } diff --git a/packages/typescript/src/ExtendedsTests.ts b/packages/typescript/src/ExtendedsTests.ts new file mode 100644 index 0000000..11b15ba --- /dev/null +++ b/packages/typescript/src/ExtendedsTests.ts @@ -0,0 +1,31 @@ +import Calculator from "./Main"; + +export default class ExtendedsTests extends Calculator { + public get getRufinhoDevice() { + return this.ruffiniDevice; + } + + public get getSum() { + return this.sum; + } + + public get getSub() { + return this.sub; + } + + public get getMul() { + return this.mul; + } + + public get getDiv() { + return this.div; + } + + public get getMod() { + return this.mod; + } + + public get getPower() { + return this.power; + } +} diff --git a/packages/typescript/src/Main.ts b/packages/typescript/src/Main.ts new file mode 100644 index 0000000..dde77e2 --- /dev/null +++ b/packages/typescript/src/Main.ts @@ -0,0 +1,793 @@ +// Class that represents a calculator with some basic mathematical functions + +import ArithmeticOp from "./arithmeticOp"; + +import { + ReturnTypesForEquation, + ReturnTypesForEquation2upDegree, +} from "./types/loganmatic"; + +const piValue: string = "3.1415926535897932384626433832795"; + +/** + * @author - Gabriel Logan + * @description - Program created as a math library in English + * @example - import Mathematics from "loganmatic" + * console.log(Mathematics.Pi) + */ +export default class Calculator extends ArithmeticOp { + EulerNumber: number; // Declare EulerNumber property + Pi: number; // Declare Pi property + + constructor() { + super(); + this.EulerNumber = this.createEulerNumber(); + this.Pi = parseFloat(piValue); + } + + private generateCriticalPointInterval(min: number, max: number) { + return this.randomNumberBetween(min, max); + } + + /** + * Method to calculate the absoluteValue value of a number + * @param n - Precision of the euler number + * @example Mathematics.createEulerNumber(99999); will create an euler number with 99999 sums, be careful + * this can freeze your pc, very large numbers result in delay to find the value + * @return - Euler Number + */ + protected createEulerNumber(n = 9999): number { + const sumStart = 0; + let emptyValue = 0; + + for (let k = sumStart; k < n + 1; k++) { + const series = 1 ** k / this.factorial(k); + emptyValue += series; + } + return emptyValue; + } + + /** + * @param valueA + * @param valueB + * @param valueC + * @param valueD + * @param checkedYes + */ + protected newtonMethod( + valueA: number, + valueB: number, + valueC: number, + valueD: number, + checkedYes: boolean, + ) { + const derivedValueA = valueA * 3; + const derivedValueB = valueB * 2; + const derivedValueC = valueC * 1; + + const delta = derivedValueB ** 2 - 4 * derivedValueA * derivedValueC; + + const answer1 = (-derivedValueB + delta ** (1 / 2)) / (2 * derivedValueA); + const answer2 = (-derivedValueB - delta ** (1 / 2)) / (2 * derivedValueA); + + let criticalPoint1 = answer1 * 1000; + let criticalPoint2 = answer2 * 1000; + + if (answer1 < 0 && answer2 < 0) { + criticalPoint1 = criticalPoint2 * -1; + } else if (answer1 > 0 && answer2 > 0) { + criticalPoint1 = criticalPoint2 * -1; + } + + let criticalPoint3: number; + + if (answer1 > answer2) { + if (Number(answer1) - Number(answer2) > 1) { + criticalPoint3 = this.generateCriticalPointInterval( + Math.ceil(Number(answer2) + 0.2), + Number(answer1), + ); + } else { + criticalPoint3 = this.generateCriticalPointInterval( + Number(answer2) + 0.2, + Number(answer1), + ); + } + } else { + if (Number(answer2) - Number(answer1) > 1) { + criticalPoint3 = this.generateCriticalPointInterval( + Math.ceil(Number(answer1) + 0.2), + Number(answer2), + ); + } else { + criticalPoint3 = this.generateCriticalPointInterval( + Number(answer1) + 0.2, + Number(answer2), + ); + } + } + + if (delta < 0) { + criticalPoint1 = -10000; + criticalPoint2 = 9000; + } + + const firstRootCritical = []; + + const iterations = 100000; + + for (let index = 0; index < iterations; index++) { + criticalPoint1 = + criticalPoint1 - + (Number(valueA) * criticalPoint1 ** 3 + + Number(valueB) * criticalPoint1 ** 2 + + Number(valueC) * criticalPoint1 + + Number(valueD)) / + (Number(derivedValueA) * criticalPoint1 ** 2 + + Number(derivedValueB) * criticalPoint1 + + Number(derivedValueC)); + + const functionValue1 = ( + Number(valueA) * criticalPoint1 ** 3 + + Number(valueB) * criticalPoint1 ** 2 + + Number(valueC) * criticalPoint1 + + Number(valueD) + ).toFixed(10); + + criticalPoint2 = + criticalPoint2 - + (Number(valueA) * criticalPoint2 ** 3 + + Number(valueB) * criticalPoint2 ** 2 + + Number(valueC) * criticalPoint2 + + Number(valueD)) / + (Number(derivedValueA) * criticalPoint2 ** 2 + + Number(derivedValueB) * criticalPoint2 + + Number(derivedValueC)); + + const functionValue2 = ( + Number(valueA) * criticalPoint2 ** 3 + + Number(valueB) * criticalPoint2 ** 2 + + Number(valueC) * criticalPoint2 + + Number(valueD) + ).toFixed(10); + + criticalPoint3 = + criticalPoint3 - + (Number(valueA) * criticalPoint3 ** 3 + + Number(valueB) * criticalPoint3 ** 2 + + Number(valueC) * criticalPoint3 + + Number(valueD)) / + (Number(derivedValueA) * criticalPoint3 ** 2 + + Number(derivedValueB) * criticalPoint3 + + Number(derivedValueC)); + + // const valueOfFuncao3 = ((Number(valueA)*((criticalPoint3)**3)) + (Number(valueB)*((criticalPoint3)**2)) + (Number(valueC)*(criticalPoint3)) + Number (valueD)).toFixed(10); + + if ( + parseFloat(functionValue1) === 0.0 && + parseFloat(functionValue2) === 0.0 + ) { + firstRootCritical.push(criticalPoint1, criticalPoint2, criticalPoint3); + break; + } + } + + if (firstRootCritical[0].toFixed(7) == firstRootCritical[1].toFixed(7)) { + if (checkedYes) { + return { + value: [firstRootCritical[0]], + msg: `It has only 1 real root in X = ${firstRootCritical[0].toFixed(4)}`, + }; + } else { + return { + value: [firstRootCritical[0]], + msg: `It has only 1 real root in X = ${firstRootCritical[0]}`, + }; + } + } else if ( + firstRootCritical[0].toFixed(4) == firstRootCritical[2].toFixed(4) + ) { + this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } else if ( + firstRootCritical[1].toFixed(4) == firstRootCritical[2].toFixed(4) + ) { + this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } else { + if (checkedYes) { + return { + value: [ + firstRootCritical[0], + firstRootCritical[1], + firstRootCritical[2], + ], + msg: `X1 ≅ ${firstRootCritical[0].toFixed(4)}, X2 ≅ ${firstRootCritical[1].toFixed(4)}, X3 ≅ ${firstRootCritical[2].toFixed(4)}`, + }; + } else { + return { + value: [ + firstRootCritical[0], + firstRootCritical[1], + firstRootCritical[2], + ], + msg: `X1 ≅ ${firstRootCritical[0]}, X2 ≅ ${firstRootCritical[1]}, X3 ≅ ${firstRootCritical[2]}`, + }; + } + } + + return { + value: [firstRootCritical[0]], + msg: `X1 = ${firstRootCritical[0]}`, + }; + } + + /** + * @param valueA + * @param valueB + * @param valueC + * @param valueD + * @param roots + * @param checkedYes + */ + protected ruffiniDevice( + valueA: number, + valueB: number, + valueC: number, + valueD: number, + roots: number[], + checkedYes: boolean, + ) { + const first = valueA * roots[0]; + + const secondCoefficient = Number(first) + Number(valueB); + + const second = secondCoefficient * roots[0]; + + const thirdCoefficient = Number(second) + Number(valueC); + + const third = thirdCoefficient * roots[0]; + + const fourthCoefficient = Number(third) + Number(valueD); + + if (fourthCoefficient == 0) { + const delta = secondCoefficient ** 2 - 4 * valueA * thirdCoefficient; + + if (delta < 0) { + return { + value: [0], + msg: `It has only 1 real root in X = ${roots[0]}`, + }; + } else { + const answer1 = (-secondCoefficient + delta ** (1 / 2)) / (2 * valueA); + const answer2 = (-secondCoefficient - delta ** (1 / 2)) / (2 * valueA); + + if (delta === 0) { + if (answer1 == roots[0]) { + return { + value: [0], + msg: "The value of X1 = 0 | X1 = X2 = X3", + }; + } else { + if (checkedYes) { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 is equal to: ${answer1.toFixed(2)} | X2 = X3`, + }; + } else { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X2 = X3`, + }; + } + } + } else { + if (answer1 == roots[0]) { + if (checkedYes) { + return { + value: [roots[0], answer2], + msg: `The value of X1 = ${roots[0]} and X2 = ${answer2.toFixed(2)} | X1 = X3`, + }; + } else { + return { + value: [roots[0], answer2], + msg: `The value of X1 = ${roots[0]} and X2 = ${answer2} | X1 = X3`, + }; + } + } else if (answer2 == roots[0]) { + if (checkedYes) { + return { + value: [roots[0], answer1], + msg: `The value of X1 = ${roots[0]} and X2 it's the same as: ${answer1.toFixed(2)} | X1 = X3`, + }; + } else { + return { + value: [roots[0], answer1], + msg: `The value of X1 = ${roots[0]} and X2 it's the same as: ${answer1} | X1 = X3`, + }; + } + } else { + if (checkedYes) { + return { + value: [roots[0], answer1, answer2], + msg: `The value of X1 = ${roots[0]}, X2 it's the same as: ${answer1.toFixed(2)} and The value of X3 it's the same as: ${answer2.toFixed(2)}`, + }; + } else { + return { + value: [roots[0], answer1, answer2], + msg: `The value of X1 = ${roots[0]}, X2 it's the same as: ${answer1} and The value of X3 it's the same as: ${answer2}`, + }; + } + } + } + } + } else if (fourthCoefficient != 0) { + return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } else { + return { + value: null, + msg: "Vish, I don't know what happened HEHEHE", + }; + } + } + + // ABOVE PRIVATE FUNCTIONS + + /** + * Method for calculating the absoluteValue value of a number + * @param number - The number to calculate the module + * @example Matematics.absoluteValue(-4) + * @return - The result = 4 + */ + absoluteValue(number: number): number { + if (number < 0) { + return -number; // Returns the negative value as positive + } else { + return number; // Returns the positive value as it is + } + } + + /** + * Method to calculate the factorial of a number + * @param valueToCalculate - The number to calculate the factorial + * @example Matematics.factorial(4) + * @return - The result of the factorial which is 24 + */ + factorial(valueToCalculate: number): number { + // If the value is zero, the factorial is 1 + if (valueToCalculate === 0) { + return 1; + } + + let result = 1; + // Iterates over the numbers less than or equal to the value to calculate the factorial + for (let i = 1; i <= valueToCalculate; i++) { + result *= i; + } + + // Returns the result of the factorial + return result; + } + + /** + * Method to calculate the square root of a number + * @param valueToCalculate - The number to calculate the square root + * @example Matematics.squareRoot(9) + * @return - The result of the square root = 3 + */ + squareRoot(valueToCalculate: number): number { + return valueToCalculate ** (1 / 2); + } + + /** + * Method to calculate the cubic root of a number + * @param valueToCalculate - The number to calculate the cubic root + * @example Matematics.cubicRoot(8) + * @return - The result of the cubic root = 2 + */ + cubicRoot(valueToCalculate: number): number { + const convertToPositive = this.absoluteValue(valueToCalculate); + const result = convertToPositive ** (1 / 3); + if (valueToCalculate < 0) { + return result * -1; + } else { + return result; + } + } + + /** + * Method to factor a number + * @param valueToCalculate - The number to be factored + * @example Matematics.factor(100) + * @return - An array with the factors of the number [2, 2, 5, 5] + */ + factor(valueToCalculate: number): void | number[] { + // If the value is not a number, returns an error message + if (typeof valueToCalculate !== "number") { + return console.log("This is not an integer"); + } + + const factoredNumbers = []; + + // Finds the factors of the number + for (let y = 2; y < valueToCalculate; y++) { + while (valueToCalculate % y === 0) { + valueToCalculate /= y; + factoredNumbers.push(y); + } + } + + // If the array is empty, it means that the number is prime and it is added to the array + if (factoredNumbers.length === 0) { + factoredNumbers.push(valueToCalculate); + } + + return factoredNumbers; + } + + /** + * Method to calculate the sine of a number + * @param valueToCalculate - The number to calculate the sine + * @example Matematics.sine(Matematics.Pi) + * @return - The result = 0 because the sine of pi(180 degrees) = 0 + */ + sine(valueToCalculate: number): number { + // Value of the variable X if it exists + let n: number; + if ( + this.absoluteValue(valueToCalculate) > 5 && + this.absoluteValue(valueToCalculate) <= 30 + ) { + n = 100; // Value An for partial sum + } else if (this.absoluteValue(valueToCalculate) > 30) { + n = 40; // Value An for partial sum + } else { + n = 200; // Value An for partial sum + } + const startSum = 0; // Initial value of the sum + let emptyValue = 0; + for (let k = startSum; k < n + 1; k++) { + const series = + ((-1) ** k * valueToCalculate ** (2 * k + 1)) / + this.factorial(2 * k + 1); + emptyValue += series; // Adds the series according to the value of n + } + + if (this.absoluteValue(emptyValue) < 0.00000001) { + return 0; + } else { + return emptyValue; // Returns the partial sum of the series + } + } + + /** + * Method to calculate the cosine of a number + * @param valueToCalculate - The number to calculate the cosine + * @example Matematics.cosine(Matematics.Pi) + * @return - The result = 0 because the cosine of pi(180 degrees) = 0 + */ + cosine(valueToCalculate: number): number { + // Value of the variable X if it exists + let n: number; + if ( + this.absoluteValue(valueToCalculate) > 5 && + this.absoluteValue(valueToCalculate) <= 30 + ) { + n = 100; // Value An for partial sum + } else if (this.absoluteValue(valueToCalculate) > 30) { + n = 40; // Value An for partial sum + } else { + n = 200; // Value An for partial sum + } + const startSum = 0; // Initial value of the sum + let emptyValue = 0; + for (let k = startSum; k < n + 1; k++) { + const series = + ((-1) ** k * valueToCalculate ** (2 * k)) / this.factorial(2 * k); + emptyValue += series; // Adds the series according to the value of n + } + + if (this.absoluteValue(emptyValue) < 0.00000001) { + return 0; + } else { + return emptyValue; // Returns the partial sum of the series + } + } + + /** + * Method to calculate the gcd + * @param valuesToCalculate - The numbers to calculate the gcd + * @example Matematics.gcd([4,8]) + * @return - The result = 4 because the gcd of 4 and 8 = 4 + */ + gcd(valuesToCalculate: number[]): number { + let mdcValue = valuesToCalculate[0]; + for (let i = 1; i < valuesToCalculate.length; i++) { + let a = mdcValue; + let b = valuesToCalculate[i]; + let rest: number; + while (b !== 0) { + rest = a % b; + a = b; + b = rest; + } + mdcValue = a; + } + return this.absoluteValue(mdcValue); + } + + /** + * Method to calculate the lcm + * @param valuesToCalculate - The numbers to calculate the lcm + * @example Matematics.lcm([4,8]) + * @return - The result = 8 because the lcm of 4 and 8 = 8 + */ + lcm(valuesToCalculate: number[]): number { + let mmcValue = valuesToCalculate[0]; + for (let i = 1; i < valuesToCalculate.length; i++) { + let a = mmcValue; + let b = valuesToCalculate[i]; + let rest: number; + const mdcValue = a; + while (b !== 0) { + rest = a % b; + a = b; + b = rest; + } + mmcValue = (mdcValue * valuesToCalculate[i]) / a; + } + return this.absoluteValue(mmcValue); + } + + /** + * Method to generate a random number between two values + * @param min - The minimum value + * @param max - The maximum value + * @example Matematics.randomNumberBetween(1, 10) + * @return - A random number between 1 and 10 + */ + randomNumberBetween(min: number, max: number): number { + const timestamp = Date.now(); + return min + (timestamp % (max - min + 1)); + } + + /** + * Method to calculate the root of a first degree polynomial + * @param a + * @param b + * @example Mathematics.linearEquation(a, b) + * + * a = term that accompanies the (x) + * and b = independent term + * + * EX: ax + b = 0 or 2x + 3 = 0 | a=2 and b=3 + * + * Mathematics.linearEquation(2, 3) + * @return - The result = x = -3/2 = -1.5 + */ + linearEquation(a: number, b: number): ReturnTypesForEquation { + const numberA = Number(a); + const numberB = Number(b); + + if (numberA === 0) { + return { + value: null, + msg: "The value of 'a' cannot be 0", + }; + } + + const root = -numberB / numberA; + return { + value: root, + msg: `The value of x is the same as: ${root}`, + }; + } + + /** + * Method to calculate the root of a first-degree polynomial + * @param a + * @param b + * @param c + * @example Mathematics.quadraticEquation(a, b, c) + * + * a = coefficient of (x^2) + * b = coefficient of (x) + * c = constant term + * + * EX: a(x^2) + b(x) + c = 0 + * + * 1(x^2) + 2(x) - 3 = 0 | a = 1, b = 2, c = -3 + * + * Mathematics.quadraticEquation(1, 2, -3) + * @return - The result = [1, -3] + */ + quadraticEquation( + a: number, + b: number, + c: number, + ): ReturnTypesForEquation2upDegree { + const numberA = Number(a); + const numberB = Number(b); + const numberC = Number(c); + + if (numberB ** 2 - 4 * numberA * numberC < 0) + return { + value: null, + msg: "The equation does not have real roots", + }; + + if (numberA === 0 && numberB === 0) { + return { + value: null, + msg: "The values of 'a' and 'b' cannot be 0 at the same time", + }; + } else { + const root1 = + (-numberB + this.squareRoot(b ** 2 - 4 * numberA * numberC)) / + (2 * numberA); + const root2 = + (-numberB - this.squareRoot(b ** 2 - 4 * numberA * numberC)) / + (2 * numberA); + + if (root1 === root2) { + return { + value: [root1], + msg: `It has only 1 real root in X = ${root1}`, + }; + } else { + return { + value: [root1, root2], + msg: `The value of X1 = ${root1} and X2 = ${root2}`, + }; + } + } + } + + /** + * Method to calculate the root of a third-degree polynomial + * @param a + * @param b + * @param c + * @param d + * @param [approximate=false] + * @example Mathematics.cubicEquation(a, b, c, d) + * + * a = coefficient of (x^3) + * b = coefficient of (x^2) + * c = coefficient of (x) + * d = constant term + * + * Mathematics.cubicEquation(1, 2, -3, 5) + * @return - It has only 1 real root in X = -3.344171229347796 + */ + cubicEquation( + a = 0, + b = 0, + c = 0, + d = 0, + approximate = false, + ): ReturnTypesForEquation2upDegree { + const checkedYes = approximate; + const valueA = Number(a); + const valueB = Number(b); + const valueC = Number(c); + const valueD = Number(d); + + if (valueD == 0) { + const x1 = 0; + const delta = valueB ** 2 - 4 * valueA * valueC; + + if (delta < 0) { + return { + value: [0], + msg: "It has only 1 real root in X = 0", + }; + } else { + const answer1 = (-valueB + delta ** (1 / 2)) / (2 * valueA); + const answer2 = (-valueB - delta ** (1 / 2)) / (2 * valueA); + + if (delta === 0) { + if (answer1 == x1) { + return { + value: [0], + msg: "The value of X1 = 0 | X1 = X2 = X3", + }; + } else { + if (checkedYes) { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 is equal to: ${answer1.toFixed(2)} | X2 = X3`, + }; + } else { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X2 = X3`, + }; + } + } + } else { + if (answer1 == x1) { + if (checkedYes) { + return { + value: [0, answer2], + msg: `The value of X1 = 0 and X2 = ${answer2.toFixed(2)} | X1 = X3`, + }; + } else { + return { + value: [0, answer2], + msg: `The value of X1 = 0 and X2 = ${answer2} | X1 = X3`, + }; + } + } else if (answer2 == x1) { + if (checkedYes) { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 = ${answer1.toFixed(2)} | X1 = X3`, + }; + } else { + return { + value: [0, answer1], + msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X1 = X3`, + }; + } + } else { + if (checkedYes) { + return { + value: [0, answer1, answer2], + msg: `The value of X1 = 0, X2 it's the same as: ${answer1.toFixed(2)} and The value of X3 it's the same as: ${answer2.toFixed(2)}`, + }; + } else { + return { + value: [0, answer1, answer2], + msg: `The value of X1 = 0, X2 it's the same as: ${answer1} and The value of X3 it's the same as: ${answer2}`, + }; + } + } + } + } + } else { + const possibleRoots = []; + + if (valueD > 0) { + for (let index = 1; index < Number(valueD) + 1; index++) { + const isInteger = valueD % index; + if (isInteger == 0) { + possibleRoots.push(index); + possibleRoots.push(-index); + } + } + } else { + for (let index = -1; index > Number(valueD) - 1; index--) { + const isInteger = valueD % index; + if (isInteger == 0) { + possibleRoots.push(index); + possibleRoots.push(-index); + } + } + } + const roots: number[] = []; + + possibleRoots.forEach((test) => { + const primeiraRaiz = + Number(valueA) * test ** 3 + + Number(valueB) * test ** 2 + + Number(valueC) * test + + Number(valueD); + if (primeiraRaiz == 0) { + roots.push(test); + } + }); + + if (roots.length === 0) { + return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); + } + + return this.ruffiniDevice( + valueA, + valueB, + valueC, + valueD, + roots, + checkedYes, + ); + } + } +} diff --git a/packages/typescript/src/arithmeticOp/index.ts b/packages/typescript/src/arithmeticOp/index.ts index b71217a..76a2697 100644 --- a/packages/typescript/src/arithmeticOp/index.ts +++ b/packages/typescript/src/arithmeticOp/index.ts @@ -60,28 +60,4 @@ export default class ArithmeticOp { } return result; } - - public getSum() { - return this.sum; - } - - public getSub() { - return this.sub; - } - - public getMul() { - return this.mul; - } - - public getDiv() { - return this.div; - } - - public getMod() { - return this.mod; - } - - public getPower() { - return this.power; - } } diff --git a/packages/typescript/src/generateCriticalPointInterval/index.ts b/packages/typescript/src/generateCriticalPointInterval/index.ts deleted file mode 100644 index a239851..0000000 --- a/packages/typescript/src/generateCriticalPointInterval/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import randomNumberBetween from "../randomNumberBetween"; - -export default function generateCriticalPointInterval( - min: number, - max: number, -) { - return randomNumberBetween(min, max); -} diff --git a/packages/typescript/src/loganmatic.ts b/packages/typescript/src/loganmatic.ts index 5ef87a7..a1c0db2 100644 --- a/packages/typescript/src/loganmatic.ts +++ b/packages/typescript/src/loganmatic.ts @@ -1,797 +1,5 @@ -// Class that represents a calculator with some basic mathematical functions +import Calculator from "./Main"; -import ArithmeticOp from "./arithmeticOp"; -import generateCriticalPointInterval from "./generateCriticalPointInterval"; -import randomNumberBetween from "./randomNumberBetween"; -import { - ReturnTypesForEquation, - ReturnTypesForEquation2upDegree, -} from "./types/loganmatic"; +const calculator = new Calculator(); -const piValue: string = "3.1415926535897932384626433832795"; - -/** - * @author - Gabriel Logan - * @description - Program created as a math library in English - * @example - import Mathematics from "loganmatic" - * console.log(Mathematics.Pi) - */ -class Calculator extends ArithmeticOp { - EulerNumber: number; // Declare EulerNumber property - Pi: number; // Declare Pi property - - constructor() { - super(); - this.EulerNumber = this.createEulerNumber(); - this.Pi = parseFloat(piValue); - } - - /** - * Method to calculate the absoluteValue value of a number - * @param n - Precision of the euler number - * @example Mathematics.createEulerNumber(99999); will create an euler number with 99999 sums, be careful - * this can freeze your pc, very large numbers result in delay to find the value - * @return - Euler Number - */ - protected createEulerNumber(n = 9999): number { - const sumStart = 0; - let emptyValue = 0; - - for (let k = sumStart; k < n + 1; k++) { - const series = 1 ** k / this.factorial(k); - emptyValue += series; - } - return emptyValue; - } - - /** - * @param valueA - * @param valueB - * @param valueC - * @param valueD - * @param checkedYes - */ - protected newtonMethod( - valueA: number, - valueB: number, - valueC: number, - valueD: number, - checkedYes: boolean, - ) { - const derivedValueA = valueA * 3; - const derivedValueB = valueB * 2; - const derivedValueC = valueC * 1; - - const delta = derivedValueB ** 2 - 4 * derivedValueA * derivedValueC; - - const answer1 = (-derivedValueB + delta ** (1 / 2)) / (2 * derivedValueA); - const answer2 = (-derivedValueB - delta ** (1 / 2)) / (2 * derivedValueA); - - let criticalPoint1 = answer1 * 1000; - let criticalPoint2 = answer2 * 1000; - - if (answer1 < 0 && answer2 < 0) { - criticalPoint1 = criticalPoint2 * -1; - } else if (answer1 > 0 && answer2 > 0) { - criticalPoint1 = criticalPoint2 * -1; - } - - let criticalPoint3: number; - - if (answer1 > answer2) { - if (Number(answer1) - Number(answer2) > 1) { - criticalPoint3 = generateCriticalPointInterval( - Math.ceil(Number(answer2) + 0.2), - Number(answer1), - ); - } else { - criticalPoint3 = generateCriticalPointInterval( - Number(answer2) + 0.2, - Number(answer1), - ); - } - } else { - if (Number(answer2) - Number(answer1) > 1) { - criticalPoint3 = generateCriticalPointInterval( - Math.ceil(Number(answer1) + 0.2), - Number(answer2), - ); - } else { - criticalPoint3 = generateCriticalPointInterval( - Number(answer1) + 0.2, - Number(answer2), - ); - } - } - - if (delta < 0) { - criticalPoint1 = -10000; - criticalPoint2 = 9000; - } - - const firstRootCritical = []; - - const iterations = 100000; - - for (let index = 0; index < iterations; index++) { - criticalPoint1 = - criticalPoint1 - - (Number(valueA) * criticalPoint1 ** 3 + - Number(valueB) * criticalPoint1 ** 2 + - Number(valueC) * criticalPoint1 + - Number(valueD)) / - (Number(derivedValueA) * criticalPoint1 ** 2 + - Number(derivedValueB) * criticalPoint1 + - Number(derivedValueC)); - - const functionValue1 = ( - Number(valueA) * criticalPoint1 ** 3 + - Number(valueB) * criticalPoint1 ** 2 + - Number(valueC) * criticalPoint1 + - Number(valueD) - ).toFixed(10); - - criticalPoint2 = - criticalPoint2 - - (Number(valueA) * criticalPoint2 ** 3 + - Number(valueB) * criticalPoint2 ** 2 + - Number(valueC) * criticalPoint2 + - Number(valueD)) / - (Number(derivedValueA) * criticalPoint2 ** 2 + - Number(derivedValueB) * criticalPoint2 + - Number(derivedValueC)); - - const functionValue2 = ( - Number(valueA) * criticalPoint2 ** 3 + - Number(valueB) * criticalPoint2 ** 2 + - Number(valueC) * criticalPoint2 + - Number(valueD) - ).toFixed(10); - - criticalPoint3 = - criticalPoint3 - - (Number(valueA) * criticalPoint3 ** 3 + - Number(valueB) * criticalPoint3 ** 2 + - Number(valueC) * criticalPoint3 + - Number(valueD)) / - (Number(derivedValueA) * criticalPoint3 ** 2 + - Number(derivedValueB) * criticalPoint3 + - Number(derivedValueC)); - - // const valueOfFuncao3 = ((Number(valueA)*((criticalPoint3)**3)) + (Number(valueB)*((criticalPoint3)**2)) + (Number(valueC)*(criticalPoint3)) + Number (valueD)).toFixed(10); - - if ( - parseFloat(functionValue1) === 0.0 && - parseFloat(functionValue2) === 0.0 - ) { - firstRootCritical.push(criticalPoint1, criticalPoint2, criticalPoint3); - break; - } - } - - if (firstRootCritical[0].toFixed(7) == firstRootCritical[1].toFixed(7)) { - if (checkedYes) { - return { - value: [firstRootCritical[0]], - msg: `It has only 1 real root in X = ${firstRootCritical[0].toFixed(4)}`, - }; - } else { - return { - value: [firstRootCritical[0]], - msg: `It has only 1 real root in X = ${firstRootCritical[0]}`, - }; - } - } else if ( - firstRootCritical[0].toFixed(4) == firstRootCritical[2].toFixed(4) - ) { - this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } else if ( - firstRootCritical[1].toFixed(4) == firstRootCritical[2].toFixed(4) - ) { - this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } else { - if (checkedYes) { - return { - value: [ - firstRootCritical[0], - firstRootCritical[1], - firstRootCritical[2], - ], - msg: `X1 ≅ ${firstRootCritical[0].toFixed(4)}, X2 ≅ ${firstRootCritical[1].toFixed(4)}, X3 ≅ ${firstRootCritical[2].toFixed(4)}`, - }; - } else { - return { - value: [ - firstRootCritical[0], - firstRootCritical[1], - firstRootCritical[2], - ], - msg: `X1 ≅ ${firstRootCritical[0]}, X2 ≅ ${firstRootCritical[1]}, X3 ≅ ${firstRootCritical[2]}`, - }; - } - } - - return { - value: [firstRootCritical[0]], - msg: `X1 = ${firstRootCritical[0]}`, - }; - } - - /** - * @param valueA - * @param valueB - * @param valueC - * @param valueD - * @param raizes - * @param checkedYes - */ - protected ruffiniDevice( - valueA: number, - valueB: number, - valueC: number, - valueD: number, - raizes: number[], - checkedYes: boolean, - ) { - const first = valueA * raizes[0]; - - const secondCoefficient = Number(first) + Number(valueB); - - const second = secondCoefficient * raizes[0]; - - const thirdCoefficient = Number(second) + Number(valueC); - - const third = thirdCoefficient * raizes[0]; - - const fourthCoefficient = Number(third) + Number(valueD); - - if (fourthCoefficient == 0) { - const delta = secondCoefficient ** 2 - 4 * valueA * thirdCoefficient; - - if (delta < 0) { - return { - value: [0], - msg: `It has only 1 real root in X = ${raizes[0]}`, - }; - } else { - const answer1 = (-secondCoefficient + delta ** (1 / 2)) / (2 * valueA); - const answer2 = (-secondCoefficient - delta ** (1 / 2)) / (2 * valueA); - - if (delta === 0) { - if (answer1 == raizes[0]) { - return { - value: [0], - msg: "The value of X1 = 0 | X1 = X2 = X3", - }; - } else { - if (checkedYes) { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 is equal to: ${answer1.toFixed(2)} | X2 = X3`, - }; - } else { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X2 = X3`, - }; - } - } - } else { - if (answer1 == raizes[0]) { - if (checkedYes) { - return { - value: [raizes[0], answer2], - msg: `The value of X1 = ${raizes[0]} and X2 = ${answer2.toFixed(2)} | X1 = X3`, - }; - } else { - return { - value: [raizes[0], answer2], - msg: `The value of X1 = ${raizes[0]} and X2 = ${answer2} | X1 = X3`, - }; - } - } else if (answer2 == raizes[0]) { - if (checkedYes) { - return { - value: [raizes[0], answer1], - msg: `The value of X1 = ${raizes[0]} and X2 it's the same as: ${answer1.toFixed(2)} | X1 = X3`, - }; - } else { - return { - value: [raizes[0], answer1], - msg: `The value of X1 = ${raizes[0]} and X2 it's the same as: ${answer1} | X1 = X3`, - }; - } - } else { - if (checkedYes) { - return { - value: [raizes[0], answer1, answer2], - msg: `The value of X1 = ${raizes[0]}, X2 it's the same as: ${answer1.toFixed(2)} and The value of X3 it's the same as: ${answer2.toFixed(2)}`, - }; - } else { - return { - value: [raizes[0], answer1, answer2], - msg: `The value of X1 = ${raizes[0]}, X2 it's the same as: ${answer1} and The value of X3 it's the same as: ${answer2}`, - }; - } - } - } - } - } else if (fourthCoefficient != 0) { - return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } else { - return { - value: null, - msg: "Vish, I don't know what happened HEHEHE", - }; - } - } - - // ABOVE PRIVATE FUNCTIONS - - /** - * Method for calculating the absoluteValue value of a number - * @param number - The number to calculate the module - * @example Matematics.absoluteValue(-4) - * @return - The result = 4 - */ - absoluteValue(number: number): number { - if (number < 0) { - return -number; // Returns the negative value as positive - } else { - return number; // Returns the positive value as it is - } - } - - /** - * Method to calculate the factorial of a number - * @param valueToCalculate - The number to calculate the factorial - * @example Matematics.factorial(4) - * @return - The result of the factorial which is 24 - */ - factorial(valueToCalculate: number): number { - // If the value is zero, the factorial is 1 - if (valueToCalculate === 0) { - return 1; - } - - let result = 1; - // Iterates over the numbers less than or equal to the value to calculate the factorial - for (let i = 1; i <= valueToCalculate; i++) { - result *= i; - } - - // Returns the result of the factorial - return result; - } - - /** - * Method to calculate the square root of a number - * @param valueToCalculate - The number to calculate the square root - * @example Matematics.squareRoot(9) - * @return - The result of the square root = 3 - */ - squareRoot(valueToCalculate: number): number { - return valueToCalculate ** (1 / 2); - } - - /** - * Method to calculate the cubic root of a number - * @param valueToCalculate - The number to calculate the cubic root - * @example Matematics.cubicRoot(8) - * @return - The result of the cubic root = 2 - */ - cubicRoot(valueToCalculate: number): number { - const convertToPositive = this.absoluteValue(valueToCalculate); - const result = convertToPositive ** (1 / 3); - if (valueToCalculate < 0) { - return result * -1; - } else { - return result; - } - } - - /** - * Method to factor a number - * @param valueToCalculate - The number to be factored - * @example Matematics.factor(100) - * @return - An array with the factors of the number [2, 2, 5, 5] - */ - factor(valueToCalculate: number): void | number[] { - // If the value is not a number, returns an error message - if (typeof valueToCalculate !== "number") { - return console.log("This is not an integer"); - } - - const factoredNumbers = []; - - // Finds the factors of the number - for (let y = 2; y < valueToCalculate; y++) { - while (valueToCalculate % y === 0) { - valueToCalculate /= y; - factoredNumbers.push(y); - } - } - - // If the array is empty, it means that the number is prime and it is added to the array - if (factoredNumbers.length === 0) { - factoredNumbers.push(valueToCalculate); - } - - return factoredNumbers; - } - - /** - * Method to calculate the sine of a number - * @param valueToCalculate - The number to calculate the sine - * @example Matematics.sine(Matematics.Pi) - * @return - The result = 0 because the sine of pi(180 degrees) = 0 - */ - sine(valueToCalculate: number): number { - // Value of the variable X if it exists - let n: number; - if ( - this.absoluteValue(valueToCalculate) > 5 && - this.absoluteValue(valueToCalculate) <= 30 - ) { - n = 100; // Value An for partial sum - } else if (this.absoluteValue(valueToCalculate) > 30) { - n = 40; // Value An for partial sum - } else { - n = 200; // Value An for partial sum - } - const startSum = 0; // Initial value of the sum - let emptyValue = 0; - for (let k = startSum; k < n + 1; k++) { - const series = - ((-1) ** k * valueToCalculate ** (2 * k + 1)) / - this.factorial(2 * k + 1); - emptyValue += series; // Adds the series according to the value of n - } - - if (this.absoluteValue(emptyValue) < 0.00000001) { - return 0; - } else { - return emptyValue; // Returns the partial sum of the series - } - } - - /** - * Method to calculate the cosine of a number - * @param valueToCalculate - The number to calculate the cosine - * @example Matematics.cosine(Matematics.Pi) - * @return - The result = 0 because the cosine of pi(180 degrees) = 0 - */ - cosine(valueToCalculate: number): number { - // Value of the variable X if it exists - let n: number; - if ( - this.absoluteValue(valueToCalculate) > 5 && - this.absoluteValue(valueToCalculate) <= 30 - ) { - n = 100; // Value An for partial sum - } else if (this.absoluteValue(valueToCalculate) > 30) { - n = 40; // Value An for partial sum - } else { - n = 200; // Value An for partial sum - } - const startSum = 0; // Initial value of the sum - let emptyValue = 0; - for (let k = startSum; k < n + 1; k++) { - const series = - ((-1) ** k * valueToCalculate ** (2 * k)) / this.factorial(2 * k); - emptyValue += series; // Adds the series according to the value of n - } - - if (this.absoluteValue(emptyValue) < 0.00000001) { - return 0; - } else { - return emptyValue; // Returns the partial sum of the series - } - } - - /** - * Method to calculate the gcd - * @param valuesToCalculate - The numbers to calculate the gcd - * @example Matematics.gcd([4,8]) - * @return - The result = 4 because the gcd of 4 and 8 = 4 - */ - gcd(valuesToCalculate: number[]): number { - let mdcValue = valuesToCalculate[0]; - for (let i = 1; i < valuesToCalculate.length; i++) { - let a = mdcValue; - let b = valuesToCalculate[i]; - let rest: number; - while (b !== 0) { - rest = a % b; - a = b; - b = rest; - } - mdcValue = a; - } - return this.absoluteValue(mdcValue); - } - - /** - * Method to calculate the lcm - * @param valuesToCalculate - The numbers to calculate the lcm - * @example Matematics.lcm([4,8]) - * @return - The result = 8 because the lcm of 4 and 8 = 8 - */ - lcm(valuesToCalculate: number[]): number { - let mmcValue = valuesToCalculate[0]; - for (let i = 1; i < valuesToCalculate.length; i++) { - let a = mmcValue; - let b = valuesToCalculate[i]; - let rest: number; - const mdcValue = a; - while (b !== 0) { - rest = a % b; - a = b; - b = rest; - } - mmcValue = (mdcValue * valuesToCalculate[i]) / a; - } - return this.absoluteValue(mmcValue); - } - - /** - * Method to generate a random number between two values - * @param min - The minimum value - * @param max - The maximum value - * @example Matematics.randomNumberBetween(1, 10) - * @return - A random number between 1 and 10 - */ - randomNumberBetween(min: number, max: number): number { - return randomNumberBetween(min, max); - } - - /** - * Method to calculate the root of a first degree polynomial - * @param a - * @param b - * @example Mathematics.linearEquation(a, b) - * - * a = term that accompanies the (x) - * and b = independent term - * - * EX: ax + b = 0 or 2x + 3 = 0 | a=2 and b=3 - * - * Mathematics.linearEquation(2, 3) - * @return - The result = x = -3/2 = -1.5 - */ - linearEquation(a: number, b: number): ReturnTypesForEquation { - const numberA = Number(a); - const numberB = Number(b); - - if (numberA === 0) { - return { - value: null, - msg: "The value of 'a' cannot be 0", - }; - } - - const root = -numberB / numberA; - return { - value: root, - msg: `The value of x is the same as: ${root}`, - }; - } - - /** - * Method to calculate the root of a first-degree polynomial - * @param a - * @param b - * @param c - * @example Mathematics.quadraticEquation(a, b, c) - * - * a = coefficient of (x^2) - * b = coefficient of (x) - * c = constant term - * - * EX: a(x^2) + b(x) + c = 0 - * - * 1(x^2) + 2(x) - 3 = 0 | a = 1, b = 2, c = -3 - * - * Mathematics.quadraticEquation(1, 2, -3) - * @return - The result = [1, -3] - */ - quadraticEquation( - a: number, - b: number, - c: number, - ): ReturnTypesForEquation2upDegree { - const numberA = Number(a); - const numberB = Number(b); - const numberC = Number(c); - - if (numberB ** 2 - 4 * numberA * numberC < 0) - return { - value: null, - msg: "The equation does not have real roots", - }; - - if (numberA === 0 && numberB === 0) { - return { - value: null, - msg: "The values of 'a' and 'b' cannot be 0 at the same time", - }; - } else { - const root1 = - (-numberB + this.squareRoot(b ** 2 - 4 * numberA * numberC)) / - (2 * numberA); - const root2 = - (-numberB - this.squareRoot(b ** 2 - 4 * numberA * numberC)) / - (2 * numberA); - - if (root1 === root2) { - return { - value: [root1], - msg: `It has only 1 real root in X = ${root1}`, - }; - } else { - return { - value: [root1, root2], - msg: `The value of X1 = ${root1} and X2 = ${root2}`, - }; - } - } - } - - /** - * Method to calculate the root of a third-degree polynomial - * @param a - * @param b - * @param c - * @param d - * @param [approximate=false] - * @example Mathematics.cubicEquation(a, b, c, d) - * - * a = coefficient of (x^3) - * b = coefficient of (x^2) - * c = coefficient of (x) - * d = constant term - * - * Mathematics.cubicEquation(1, 2, -3, 5) - * @return - It has only 1 real root in X = -3.344171229347796 - */ - cubicEquation( - a = 0, - b = 0, - c = 0, - d = 0, - approximate = false, - ): ReturnTypesForEquation2upDegree { - const checkedYes = approximate; - const valueA = Number(a); - const valueB = Number(b); - const valueC = Number(c); - const valueD = Number(d); - - if (valueD == 0) { - const x1 = 0; - const delta = valueB ** 2 - 4 * valueA * valueC; - - if (delta < 0) { - return { - value: [0], - msg: "It has only 1 real root in X = 0", - }; - } else { - const answer1 = (-valueB + delta ** (1 / 2)) / (2 * valueA); - const answer2 = (-valueB - delta ** (1 / 2)) / (2 * valueA); - - if (delta === 0) { - if (answer1 == x1) { - return { - value: [0], - msg: "The value of X1 = 0 | X1 = X2 = X3", - }; - } else { - if (checkedYes) { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 is equal to: ${answer1.toFixed(2)} | X2 = X3`, - }; - } else { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X2 = X3`, - }; - } - } - } else { - if (answer1 == x1) { - if (checkedYes) { - return { - value: [0, answer2], - msg: `The value of X1 = 0 and X2 = ${answer2.toFixed(2)} | X1 = X3`, - }; - } else { - return { - value: [0, answer2], - msg: `The value of X1 = 0 and X2 = ${answer2} | X1 = X3`, - }; - } - } else if (answer2 == x1) { - if (checkedYes) { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 = ${answer1.toFixed(2)} | X1 = X3`, - }; - } else { - return { - value: [0, answer1], - msg: `The value of X1 = 0 and X2 is equal to: ${answer1} | X1 = X3`, - }; - } - } else { - if (checkedYes) { - return { - value: [0, answer1, answer2], - msg: `The value of X1 = 0, X2 it's the same as: ${answer1.toFixed(2)} and The value of X3 it's the same as: ${answer2.toFixed(2)}`, - }; - } else { - return { - value: [0, answer1, answer2], - msg: `The value of X1 = 0, X2 it's the same as: ${answer1} and The value of X3 it's the same as: ${answer2}`, - }; - } - } - } - } - } else { - const possibleRoots = []; - - if (valueD > 0) { - for (let index = 1; index < Number(valueD) + 1; index++) { - const isInteger = valueD % index; - if (isInteger == 0) { - possibleRoots.push(index); - possibleRoots.push(-index); - } - } - } else { - for (let index = -1; index > Number(valueD) - 1; index--) { - const isInteger = valueD % index; - if (isInteger == 0) { - possibleRoots.push(index); - possibleRoots.push(-index); - } - } - } - const raizes: number[] = []; - - possibleRoots.forEach((test) => { - const primeiraRaiz = - Number(valueA) * test ** 3 + - Number(valueB) * test ** 2 + - Number(valueC) * test + - Number(valueD); - if (primeiraRaiz == 0) { - raizes.push(test); - } - }); - - if (raizes.length === 0) { - return this.newtonMethod(valueA, valueB, valueC, valueD, checkedYes); - } - - return this.ruffiniDevice( - valueA, - valueB, - valueC, - valueD, - raizes, - checkedYes, - ); - } - } - - public getRufinhoDevice() { - return this.ruffiniDevice; - } -} - -// Create an instance of the Calculator class -// Exports the instance of the Calculator class -export = new Calculator(); +export = calculator; diff --git a/packages/typescript/src/randomNumberBetween/index.ts b/packages/typescript/src/randomNumberBetween/index.ts deleted file mode 100644 index 695c7f2..0000000 --- a/packages/typescript/src/randomNumberBetween/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export default function randomNumberBetween(min: number, max: number) { - const timestamp = Date.now(); - return min + (timestamp % (max - min + 1)); -} diff --git a/packages/typescript/tests/arithmeticOp.test.ts b/packages/typescript/tests/arithmeticOp.test.ts index 494ada7..e5ea563 100644 --- a/packages/typescript/tests/arithmeticOp.test.ts +++ b/packages/typescript/tests/arithmeticOp.test.ts @@ -1,54 +1,54 @@ -import ArithmeticOp from "../src/arithmeticOp/index"; +import ExtendedsTest from "../src/ExtendedsTests"; describe("ArithmeticOp", () => { - let arithmeticOp: ArithmeticOp; + let arithmeticOp: ExtendedsTest; beforeEach(() => { - arithmeticOp = new ArithmeticOp(); + arithmeticOp = new ExtendedsTest(); }); describe("sum", () => { it("should calculate the sum of the given numbers", () => { - expect(arithmeticOp.getSum()(1, 2, 3)).toBe(6); - expect(arithmeticOp.getSum()(10, 20, 30, 40)).toBe(100); + expect(arithmeticOp.getSum(1, 2, 3)).toBe(6); + expect(arithmeticOp.getSum(10, 20, 30, 40)).toBe(100); }); }); describe("sub", () => { it("should calculate the difference between the given numbers", () => { - expect(arithmeticOp.getSub()(5, 2)).toBe(3); - expect(arithmeticOp.getSub()(10, 5, 3)).toBe(2); + expect(arithmeticOp.getSub(5, 2)).toBe(3); + expect(arithmeticOp.getSub(10, 5, 3)).toBe(2); }); }); describe("mul", () => { it("should calculate the product of the given numbers", () => { - expect(arithmeticOp.getMul()(2, 3)).toBe(6); - expect(arithmeticOp.getMul()(5, 10, 2)).toBe(100); + expect(arithmeticOp.getMul(2, 3)).toBe(6); + expect(arithmeticOp.getMul(5, 10, 2)).toBe(100); }); }); describe("div", () => { it("should calculate the division of the given numbers", () => { - expect(arithmeticOp.getDiv()(10, 2)).toBe(5); - expect(arithmeticOp.getDiv()(100, 5, 2)).toBe(10); + expect(arithmeticOp.getDiv(10, 2)).toBe(5); + expect(arithmeticOp.getDiv(100, 5, 2)).toBe(10); }); }); describe("mod", () => { it("should calculate the modulo of the given numbers", () => { - expect(arithmeticOp.getMod()(10, 3)).toBe(1); - expect(arithmeticOp.getMod()(100, 5, 3)).toBe(0); - expect(arithmeticOp.getMod()(100, 54, 6)).toBe(4); + expect(arithmeticOp.getMod(10, 3)).toBe(1); + expect(arithmeticOp.getMod(100, 5, 3)).toBe(0); + expect(arithmeticOp.getMod(100, 54, 6)).toBe(4); }); }); describe("power", () => { it("should calculate the power of the given base and exponent", () => { - expect(arithmeticOp.getPower()(2, 3)).toBe(8); - expect(arithmeticOp.getPower()(5, 3)).toBe(125); - expect(arithmeticOp.getPower()(-10, 2)).toBe(100); - expect(arithmeticOp.getPower()(-2, 3)).toBe(-8); + expect(arithmeticOp.getPower(2, 3)).toBe(8); + expect(arithmeticOp.getPower(5, 3)).toBe(125); + expect(arithmeticOp.getPower(-10, 2)).toBe(100); + expect(arithmeticOp.getPower(-2, 3)).toBe(-8); }); }); }); diff --git a/packages/typescript/tests/equations.test.ts b/packages/typescript/tests/equations.test.ts index 901a1af..a9381f2 100644 --- a/packages/typescript/tests/equations.test.ts +++ b/packages/typescript/tests/equations.test.ts @@ -1,25 +1,33 @@ -import Mathematics from "../src/loganmatic"; -import generateCriticalPointInterval from "../src/generateCriticalPointInterval/"; +import ExtendedsTest from "../src/ExtendedsTests"; describe("Calculadora equations", () => { // loganmatic.test.ts - const rufineMethod = Mathematics.getRufinhoDevice(); + const Mathematics = new ExtendedsTest(); test("getRufinhoDevice returns the expected result", () => { // Substitua 'arg1', 'arg2', etc. pelos argumentos reais que getRufinhoDevice aceita - const result = rufineMethod(1, -6, 11, -6, [1, 3, 2], false); + const result = Mathematics.getRufinhoDevice( + 1, + -6, + 11, + -6, + [1, 3, 2], + false, + ); // Substitua 'expectedResult' pelo resultado que você espera de getRufinhoDevice expect(result.value).toEqual([1, 3, 2]); }); - test("generateCriticalPointInterval returns a value within the expected range", () => { + /** + * test("generateCriticalPointInterval returns a value within the expected range", () => { const min = 1; const max = 10; const result = generateCriticalPointInterval(min, max); expect(result).toBeGreaterThanOrEqual(min); expect(result).toBeLessThanOrEqual(max); }); + */ test("should correctly calculate the root of a linear equation", () => { const equationRoot = Mathematics.linearEquation(2, 3); diff --git a/packages/typescript/types/Main.d.ts b/packages/typescript/types/Main.d.ts new file mode 100644 index 0000000..f41630c --- /dev/null +++ b/packages/typescript/types/Main.d.ts @@ -0,0 +1,171 @@ +import ArithmeticOp from "./arithmeticOp"; +import { ReturnTypesForEquation, ReturnTypesForEquation2upDegree } from "./types/loganmatic"; +/** + * @author - Gabriel Logan + * @description - Program created as a math library in English + * @example - import Mathematics from "loganmatic" + * console.log(Mathematics.Pi) + */ +export default class Calculator extends ArithmeticOp { + EulerNumber: number; + Pi: number; + constructor(); + private generateCriticalPointInterval; + /** + * Method to calculate the absoluteValue value of a number + * @param n - Precision of the euler number + * @example Mathematics.createEulerNumber(99999); will create an euler number with 99999 sums, be careful + * this can freeze your pc, very large numbers result in delay to find the value + * @return - Euler Number + */ + protected createEulerNumber(n?: number): number; + /** + * @param valueA + * @param valueB + * @param valueC + * @param valueD + * @param checkedYes + */ + protected newtonMethod(valueA: number, valueB: number, valueC: number, valueD: number, checkedYes: boolean): { + value: number[]; + msg: string; + }; + /** + * @param valueA + * @param valueB + * @param valueC + * @param valueD + * @param roots + * @param checkedYes + */ + protected ruffiniDevice(valueA: number, valueB: number, valueC: number, valueD: number, roots: number[], checkedYes: boolean): { + value: number[]; + msg: string; + } | { + value: null; + msg: string; + }; + /** + * Method for calculating the absoluteValue value of a number + * @param number - The number to calculate the module + * @example Matematics.absoluteValue(-4) + * @return - The result = 4 + */ + absoluteValue(number: number): number; + /** + * Method to calculate the factorial of a number + * @param valueToCalculate - The number to calculate the factorial + * @example Matematics.factorial(4) + * @return - The result of the factorial which is 24 + */ + factorial(valueToCalculate: number): number; + /** + * Method to calculate the square root of a number + * @param valueToCalculate - The number to calculate the square root + * @example Matematics.squareRoot(9) + * @return - The result of the square root = 3 + */ + squareRoot(valueToCalculate: number): number; + /** + * Method to calculate the cubic root of a number + * @param valueToCalculate - The number to calculate the cubic root + * @example Matematics.cubicRoot(8) + * @return - The result of the cubic root = 2 + */ + cubicRoot(valueToCalculate: number): number; + /** + * Method to factor a number + * @param valueToCalculate - The number to be factored + * @example Matematics.factor(100) + * @return - An array with the factors of the number [2, 2, 5, 5] + */ + factor(valueToCalculate: number): void | number[]; + /** + * Method to calculate the sine of a number + * @param valueToCalculate - The number to calculate the sine + * @example Matematics.sine(Matematics.Pi) + * @return - The result = 0 because the sine of pi(180 degrees) = 0 + */ + sine(valueToCalculate: number): number; + /** + * Method to calculate the cosine of a number + * @param valueToCalculate - The number to calculate the cosine + * @example Matematics.cosine(Matematics.Pi) + * @return - The result = 0 because the cosine of pi(180 degrees) = 0 + */ + cosine(valueToCalculate: number): number; + /** + * Method to calculate the gcd + * @param valuesToCalculate - The numbers to calculate the gcd + * @example Matematics.gcd([4,8]) + * @return - The result = 4 because the gcd of 4 and 8 = 4 + */ + gcd(valuesToCalculate: number[]): number; + /** + * Method to calculate the lcm + * @param valuesToCalculate - The numbers to calculate the lcm + * @example Matematics.lcm([4,8]) + * @return - The result = 8 because the lcm of 4 and 8 = 8 + */ + lcm(valuesToCalculate: number[]): number; + /** + * Method to generate a random number between two values + * @param min - The minimum value + * @param max - The maximum value + * @example Matematics.randomNumberBetween(1, 10) + * @return - A random number between 1 and 10 + */ + randomNumberBetween(min: number, max: number): number; + /** + * Method to calculate the root of a first degree polynomial + * @param a + * @param b + * @example Mathematics.linearEquation(a, b) + * + * a = term that accompanies the (x) + * and b = independent term + * + * EX: ax + b = 0 or 2x + 3 = 0 | a=2 and b=3 + * + * Mathematics.linearEquation(2, 3) + * @return - The result = x = -3/2 = -1.5 + */ + linearEquation(a: number, b: number): ReturnTypesForEquation; + /** + * Method to calculate the root of a first-degree polynomial + * @param a + * @param b + * @param c + * @example Mathematics.quadraticEquation(a, b, c) + * + * a = coefficient of (x^2) + * b = coefficient of (x) + * c = constant term + * + * EX: a(x^2) + b(x) + c = 0 + * + * 1(x^2) + 2(x) - 3 = 0 | a = 1, b = 2, c = -3 + * + * Mathematics.quadraticEquation(1, 2, -3) + * @return - The result = [1, -3] + */ + quadraticEquation(a: number, b: number, c: number): ReturnTypesForEquation2upDegree; + /** + * Method to calculate the root of a third-degree polynomial + * @param a + * @param b + * @param c + * @param d + * @param [approximate=false] + * @example Mathematics.cubicEquation(a, b, c, d) + * + * a = coefficient of (x^3) + * b = coefficient of (x^2) + * c = coefficient of (x) + * d = constant term + * + * Mathematics.cubicEquation(1, 2, -3, 5) + * @return - It has only 1 real root in X = -3.344171229347796 + */ + cubicEquation(a?: number, b?: number, c?: number, d?: number, approximate?: boolean): ReturnTypesForEquation2upDegree; +} diff --git a/packages/typescript/types/arithmeticOp/index.d.ts b/packages/typescript/types/arithmeticOp/index.d.ts index d5f0926..8c3c461 100644 --- a/packages/typescript/types/arithmeticOp/index.d.ts +++ b/packages/typescript/types/arithmeticOp/index.d.ts @@ -39,10 +39,4 @@ export default class ArithmeticOp { * @returns The power of the base and exponent. */ protected power(base: number, exponent: number): number; - getSum(): (...numbers: number[]) => number; - getSub(): (...numbers: number[]) => number; - getMul(): (...numbers: number[]) => number; - getDiv(): (...numbers: number[]) => number; - getMod(): (...numbers: number[]) => number; - getPower(): (base: number, exponent: number) => number; } diff --git a/packages/typescript/types/generateCriticalPointInterval/index.d.ts b/packages/typescript/types/generateCriticalPointInterval/index.d.ts deleted file mode 100644 index c24e35d..0000000 --- a/packages/typescript/types/generateCriticalPointInterval/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export default function generateCriticalPointInterval(min: number, max: number): number; diff --git a/packages/typescript/types/loganmatic.d.ts b/packages/typescript/types/loganmatic.d.ts index 070c3b5..24c369c 100644 --- a/packages/typescript/types/loganmatic.d.ts +++ b/packages/typescript/types/loganmatic.d.ts @@ -1,179 +1,3 @@ -import ArithmeticOp from "./arithmeticOp"; -import { ReturnTypesForEquation, ReturnTypesForEquation2upDegree } from "./types/loganmatic"; -/** - * @author - Gabriel Logan - * @description - Program created as a math library in English - * @example - import Mathematics from "loganmatic" - * console.log(Mathematics.Pi) - */ -declare class Calculator extends ArithmeticOp { - EulerNumber: number; - Pi: number; - constructor(); - /** - * Method to calculate the absoluteValue value of a number - * @param n - Precision of the euler number - * @example Mathematics.createEulerNumber(99999); will create an euler number with 99999 sums, be careful - * this can freeze your pc, very large numbers result in delay to find the value - * @return - Euler Number - */ - protected createEulerNumber(n?: number): number; - /** - * @param valueA - * @param valueB - * @param valueC - * @param valueD - * @param checkedYes - */ - protected newtonMethod(valueA: number, valueB: number, valueC: number, valueD: number, checkedYes: boolean): { - value: number[]; - msg: string; - }; - /** - * @param valueA - * @param valueB - * @param valueC - * @param valueD - * @param raizes - * @param checkedYes - */ - protected ruffiniDevice(valueA: number, valueB: number, valueC: number, valueD: number, raizes: number[], checkedYes: boolean): { - value: number[]; - msg: string; - } | { - value: null; - msg: string; - }; - /** - * Method for calculating the absoluteValue value of a number - * @param number - The number to calculate the module - * @example Matematics.absoluteValue(-4) - * @return - The result = 4 - */ - absoluteValue(number: number): number; - /** - * Method to calculate the factorial of a number - * @param valueToCalculate - The number to calculate the factorial - * @example Matematics.factorial(4) - * @return - The result of the factorial which is 24 - */ - factorial(valueToCalculate: number): number; - /** - * Method to calculate the square root of a number - * @param valueToCalculate - The number to calculate the square root - * @example Matematics.squareRoot(9) - * @return - The result of the square root = 3 - */ - squareRoot(valueToCalculate: number): number; - /** - * Method to calculate the cubic root of a number - * @param valueToCalculate - The number to calculate the cubic root - * @example Matematics.cubicRoot(8) - * @return - The result of the cubic root = 2 - */ - cubicRoot(valueToCalculate: number): number; - /** - * Method to factor a number - * @param valueToCalculate - The number to be factored - * @example Matematics.factor(100) - * @return - An array with the factors of the number [2, 2, 5, 5] - */ - factor(valueToCalculate: number): void | number[]; - /** - * Method to calculate the sine of a number - * @param valueToCalculate - The number to calculate the sine - * @example Matematics.sine(Matematics.Pi) - * @return - The result = 0 because the sine of pi(180 degrees) = 0 - */ - sine(valueToCalculate: number): number; - /** - * Method to calculate the cosine of a number - * @param valueToCalculate - The number to calculate the cosine - * @example Matematics.cosine(Matematics.Pi) - * @return - The result = 0 because the cosine of pi(180 degrees) = 0 - */ - cosine(valueToCalculate: number): number; - /** - * Method to calculate the gcd - * @param valuesToCalculate - The numbers to calculate the gcd - * @example Matematics.gcd([4,8]) - * @return - The result = 4 because the gcd of 4 and 8 = 4 - */ - gcd(valuesToCalculate: number[]): number; - /** - * Method to calculate the lcm - * @param valuesToCalculate - The numbers to calculate the lcm - * @example Matematics.lcm([4,8]) - * @return - The result = 8 because the lcm of 4 and 8 = 8 - */ - lcm(valuesToCalculate: number[]): number; - /** - * Method to generate a random number between two values - * @param min - The minimum value - * @param max - The maximum value - * @example Matematics.randomNumberBetween(1, 10) - * @return - A random number between 1 and 10 - */ - randomNumberBetween(min: number, max: number): number; - /** - * Method to calculate the root of a first degree polynomial - * @param a - * @param b - * @example Mathematics.linearEquation(a, b) - * - * a = term that accompanies the (x) - * and b = independent term - * - * EX: ax + b = 0 or 2x + 3 = 0 | a=2 and b=3 - * - * Mathematics.linearEquation(2, 3) - * @return - The result = x = -3/2 = -1.5 - */ - linearEquation(a: number, b: number): ReturnTypesForEquation; - /** - * Method to calculate the root of a first-degree polynomial - * @param a - * @param b - * @param c - * @example Mathematics.quadraticEquation(a, b, c) - * - * a = coefficient of (x^2) - * b = coefficient of (x) - * c = constant term - * - * EX: a(x^2) + b(x) + c = 0 - * - * 1(x^2) + 2(x) - 3 = 0 | a = 1, b = 2, c = -3 - * - * Mathematics.quadraticEquation(1, 2, -3) - * @return - The result = [1, -3] - */ - quadraticEquation(a: number, b: number, c: number): ReturnTypesForEquation2upDegree; - /** - * Method to calculate the root of a third-degree polynomial - * @param a - * @param b - * @param c - * @param d - * @param [approximate=false] - * @example Mathematics.cubicEquation(a, b, c, d) - * - * a = coefficient of (x^3) - * b = coefficient of (x^2) - * c = coefficient of (x) - * d = constant term - * - * Mathematics.cubicEquation(1, 2, -3, 5) - * @return - It has only 1 real root in X = -3.344171229347796 - */ - cubicEquation(a?: number, b?: number, c?: number, d?: number, approximate?: boolean): ReturnTypesForEquation2upDegree; - getRufinhoDevice(): (valueA: number, valueB: number, valueC: number, valueD: number, raizes: number[], checkedYes: boolean) => { - value: number[]; - msg: string; - } | { - value: null; - msg: string; - }; -} -declare const _default: Calculator; -export = _default; +import Calculator from "./Main"; +declare const calculator: Calculator; +export = calculator; diff --git a/packages/typescript/types/randomNumberBetween/index.d.ts b/packages/typescript/types/randomNumberBetween/index.d.ts deleted file mode 100644 index aa22c36..0000000 --- a/packages/typescript/types/randomNumberBetween/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export default function randomNumberBetween(min: number, max: number): number;