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