From 7a4fe23465531bbb073c4768d4ed56c5af618130 Mon Sep 17 00:00:00 2001 From: Martin Donk Date: Mon, 30 Dec 2019 16:03:02 -0600 Subject: [PATCH 1/4] Revisiting roots --- Algebra.js | 2 +- nerdamer.core.js | 67 ++++++++++++++++++++++++++++++++++++----------- spec/core.spec.js | 2 +- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/Algebra.js b/Algebra.js index 42dfc181..ef2d67c5 100644 --- a/Algebra.js +++ b/Algebra.js @@ -3924,4 +3924,4 @@ if((typeof module) !== 'undefined') { } ]); nerdamer.api(); -})(); +})(); \ No newline at end of file diff --git a/nerdamer.core.js b/nerdamer.core.js index adc58908..095148cb 100644 --- a/nerdamer.core.js +++ b/nerdamer.core.js @@ -1133,7 +1133,10 @@ var nerdamer = (function (imports) { //Big ========================================================================== var Big = { cos: function (x) { - return new Symbol(bigDec.cos(x.toString())); + return new Symbol(bigDec.cos(x.multiplier.toDecimal())); + }, + sin: function (x) { + return new Symbol(bigDec.sin(x.multiplier.toDecimal())); } }; //Math2 ======================================================================== @@ -5467,7 +5470,8 @@ var nerdamer = (function (imports) { 'intersects': [intersects, 2], 'is_subset': [is_subset, 2], //system support - 'print': [print, -1] + 'print': [print, -1], + 'nroots': [nroots, 1] }; //error handler @@ -6945,7 +6949,38 @@ var nerdamer = (function (imports) { function degrees(symbol) { return _.parse(format('({0})*180/pi', symbol)); } - + + function nroots(symbol) { + var a, b; + if(symbol.group === FN && symbol.fname === '') { + a = Symbol.unwrapPARENS(_.parse(symbol).toLinear()); + b = _.parse(symbol.power); + } + if(symbol.group === P) { + a = _.parse(symbol.value); + b = _.parse(symbol.power); + } + + if(a && b && a.group === N && b.group === N) { + var _roots = []; + var r = _.parse(a).abs().toString(); + //https://en.wikipedia.org/wiki/De_Moivre%27s_formula + var x = arg(a).toString(); + var n = b.multiplier.den.toString(); + var p = b.multiplier.num.toString(); + + var formula = "(({0})^({1})*(cos({3})+({2})*sin({3})))^({4})"; + for(var i=0; i Date: Mon, 30 Dec 2019 16:58:23 -0600 Subject: [PATCH 2/4] Update to nroots --- nerdamer.core.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/nerdamer.core.js b/nerdamer.core.js index 095148cb..f67d1f60 100644 --- a/nerdamer.core.js +++ b/nerdamer.core.js @@ -6956,13 +6956,14 @@ var nerdamer = (function (imports) { a = Symbol.unwrapPARENS(_.parse(symbol).toLinear()); b = _.parse(symbol.power); } - if(symbol.group === P) { + else if(symbol.group === P) { a = _.parse(symbol.value); b = _.parse(symbol.power); } if(a && b && a.group === N && b.group === N) { var _roots = []; + var parts = Symbol.toPolarFormArray(symbol); var r = _.parse(a).abs().toString(); //https://en.wikipedia.org/wiki/De_Moivre%27s_formula var x = arg(a).toString(); @@ -6976,9 +6977,23 @@ var nerdamer = (function (imports) { } return Vector.fromArray(_roots); } + else if(symbol.isConstant(true)) { + var sign = symbol.sign(); + var x = evaluate(symbol.abs()); + var root = _.sqrt(x); + + var _roots = [root.clone(), root.negate()]; + + if(sign < 0) + _roots = _roots.map(function(x) { + return _.multiply(x, Symbol.imaginary()); + }); + } else { - return Vector.fromArray([_.parse(symbol)]); + _roots = [_.parse(symbol)]; } + + return Vector.fromArray(_roots); } /** @@ -11534,3 +11549,7 @@ var nerdamer = (function (imports) { if ((typeof module) !== 'undefined') { module.exports = nerdamer; }; + + +var x = nerdamer('nroots(4+2i)'); +console.log(x.text()); \ No newline at end of file From 2b4e4a851afabd3d0a94ae77bcbe260d5a57430e Mon Sep 17 00:00:00 2001 From: Martin Donk Date: Mon, 30 Dec 2019 22:08:40 -0600 Subject: [PATCH 3/4] roots in progress --- Algebra.js | 4 ++ nerdamer.core.js | 144 +++++++++++++++++++++++++++-------------------- 2 files changed, 88 insertions(+), 60 deletions(-) diff --git a/Algebra.js b/Algebra.js index ef2d67c5..8efb79a0 100644 --- a/Algebra.js +++ b/Algebra.js @@ -1787,6 +1787,10 @@ if((typeof module) !== 'undefined') { } }, roots: function(symbol) { + + if(symbol.isConstant(true, true)) { + return core.Utils.nroots(symbol); + } var roots = __.proots(symbol).map(function(x) { return _.parse(x); }); diff --git a/nerdamer.core.js b/nerdamer.core.js index f67d1f60..4aaabc59 100644 --- a/nerdamer.core.js +++ b/nerdamer.core.js @@ -5,7 +5,7 @@ * Source : https://github.com/jiggzson/nerdamer */ -/* global trig, trigh, Infinity, define, arguments2Array, NaN */ +/* global trig, trigh, Infinity, define, arguments2Array, NaN, evaluate */ //externals ==================================================================== /* BigInterger.js v1.6.40 https://github.com/peterolson/BigInteger.js/blob/master/LICENSE */ //var nerdamerBigInt = typeof nerdamerBigInt !== 'undefined' ? nerdamerBigInt : require("big-integer"); @@ -709,7 +709,61 @@ var nerdamer = (function (imports) { return retval; }; + /** + * Gets nth roots of a number + * @param {Symbol} symbol + * @returns {Vector} + */ + var nroots = function(symbol) { + var a, b; + + if(symbol.group === FN && symbol.fname === '') { + a = Symbol.unwrapPARENS(_.parse(symbol).toLinear()); + b = _.parse(symbol.power); + } + else if(symbol.group === P) { + a = _.parse(symbol.value); + b = _.parse(symbol.power); + } + if(a && b && (a.group === N || a.group === CP) && b.group === N) { + var _roots = []; + var parts = Symbol.toPolarFormArray(evaluate(symbol)); + + var r = parts[0]; + + //https://en.wikipedia.org/wiki/De_Moivre%27s_formula + var x = _.arg(a); + var n = b.multiplier.den.toString(); + var p = b.multiplier.num.toString(); + + var formula = '(({0})^({1})*(cos({3})+({2})*sin({3})))^({4})'; + + for(var i=0; i Date: Tue, 31 Dec 2019 10:35:33 -0600 Subject: [PATCH 4/4] Update roots --- nerdamer.core.js | 92 ++++++++++++++++++++++++++++++++++++-------- spec/algebra.spec.js | 5 +++ 2 files changed, 82 insertions(+), 15 deletions(-) diff --git a/nerdamer.core.js b/nerdamer.core.js index 4aaabc59..e331d151 100644 --- a/nerdamer.core.js +++ b/nerdamer.core.js @@ -5,7 +5,7 @@ * Source : https://github.com/jiggzson/nerdamer */ -/* global trig, trigh, Infinity, define, arguments2Array, NaN, evaluate */ +/* global trig, trigh, Infinity, define, arguments2Array, NaN */ //externals ==================================================================== /* BigInterger.js v1.6.40 https://github.com/peterolson/BigInteger.js/blob/master/LICENSE */ //var nerdamerBigInt = typeof nerdamerBigInt !== 'undefined' ? nerdamerBigInt : require("big-integer"); @@ -709,7 +709,7 @@ var nerdamer = (function (imports) { return retval; }; - /** + /** * Gets nth roots of a number * @param {Symbol} symbol * @returns {Vector} @@ -726,11 +726,13 @@ var nerdamer = (function (imports) { b = _.parse(symbol.power); } - if(a && b && (a.group === N || a.group === CP) && b.group === N) { + if(a && b && (a.group === N) && b.group === N && a.multiplier.isNegative()) { var _roots = []; + var parts = Symbol.toPolarFormArray(evaluate(symbol)); - var r = parts[0]; + + //var r = _.parse(a).abs().toString(); //https://en.wikipedia.org/wiki/De_Moivre%27s_formula var x = _.arg(a); @@ -756,6 +758,7 @@ var nerdamer = (function (imports) { _roots = _roots.map(function(x) { return _.multiply(x, Symbol.imaginary()); }); + } else { _roots = [_.parse(symbol)]; @@ -763,7 +766,7 @@ var nerdamer = (function (imports) { return Vector.fromArray(_roots); }; - + /** * Sorts and array given 2 parameters * @param {String} a @@ -3174,6 +3177,7 @@ var nerdamer = (function (imports) { Symbol.toPolarFormArray = function (symbol) { var re, im, r, theta; re = symbol.realpart(); + im = symbol.imagpart(); r = Symbol.hyp(re, im); theta = re.equals(0) ? _.parse('pi/2') : _.trig.atan(_.divide(im, re)); return [r, theta]; @@ -7008,6 +7012,52 @@ var nerdamer = (function (imports) { return _.parse(format('({0})*180/pi', symbol)); } + function nroots(symbol) { + var a, b; + if(symbol.group === FN && symbol.fname === '') { + a = Symbol.unwrapPARENS(_.parse(symbol).toLinear()); + b = _.parse(symbol.power); + } + else if(symbol.group === P) { + a = _.parse(symbol.value); + b = _.parse(symbol.power); + } + + if(a && b && a.group === N && b.group === N) { + var _roots = []; + var parts = Symbol.toPolarFormArray(symbol); + var r = _.parse(a).abs().toString(); + //https://en.wikipedia.org/wiki/De_Moivre%27s_formula + var x = arg(a).toString(); + var n = b.multiplier.den.toString(); + var p = b.multiplier.num.toString(); + + var formula = "(({0})^({1})*(cos({3})+({2})*sin({3})))^({4})"; + for(var i=0; i