From 4b9992aca16601e4f98c5801c22521f50961d05f Mon Sep 17 00:00:00 2001 From: Martin Donk Date: Tue, 31 Dec 2019 10:35:33 -0600 Subject: [PATCH] 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