Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
roots in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jiggzson committed Dec 31, 2019
1 parent 76b1aad commit 2b4e4a8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 60 deletions.
4 changes: 4 additions & 0 deletions Algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
144 changes: 84 additions & 60 deletions nerdamer.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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<n; i++) {
var t = evaluate(_.parse(format("(({0})+2*pi*({1}))/({2})", x, i, n))).multiplier.toDecimal();
_roots.push(evaluate(_.parse(format(formula, r, n, Settings.IMAGINARY, t, p))));
}
return Vector.fromArray(_roots);
}
else if(symbol.isConstant(true, 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 {
_roots = [_.parse(symbol)];
}

return Vector.fromArray(_roots);
};

/**
* Sorts and array given 2 parameters
* @param {String} a
Expand Down Expand Up @@ -3112,17 +3166,14 @@ var nerdamer = (function (imports) {
return symbol;
};
Symbol.hyp = function (a, b) {
if (a.equals(0))
return b.clone();
if (b.equals(0))
return a.clone();
a = a || new Symbol(0);
b = b || new Symbol(0);
return _.sqrt(_.add(_.pow(a.clone(), new Symbol(2)), _.pow(b.clone(), new Symbol(2))));
};
//converts to polar form array
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];
Expand Down Expand Up @@ -3515,9 +3566,17 @@ var nerdamer = (function (imports) {
isSQRT: function () {
return this.fname === SQRT;
},
isConstant: function (check_all) {
isConstant: function (check_all, check_symbols) {
if(check_symbols && this.group === CB) {
for(var x in this.symbols) {
if(this.symbols[x].isConstant(true))
return true;
}
}

if (check_all === 'all' && (this.isPi() || this.isE()))
return true;

if (check_all && this.group === FN) {
for (var i = 0; i < this.args.length; i++) {
if (!this.args[i].isConstant(check_all))
Expand Down Expand Up @@ -5470,8 +5529,7 @@ var nerdamer = (function (imports) {
'intersects': [intersects, 2],
'is_subset': [is_subset, 2],
//system support
'print': [print, -1],
'nroots': [nroots, 1]
'print': [print, -1]
};

//error handler
Expand Down Expand Up @@ -6950,52 +7008,6 @@ 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<n; i++) {
var t = evaluate(_.parse(format("(({0})+2*pi*({1}))/({2})", x, i, n))).multiplier.toDecimal();
_roots.push(evaluate(_.parse(format(formula, r, n, Settings.IMAGINARY, t, p))));
}
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 {
_roots = [_.parse(symbol)];
}

return Vector.fromArray(_roots);
}

/**
* The square root function
* @param {Symbol} symbol
Expand Down Expand Up @@ -7256,8 +7268,18 @@ var nerdamer = (function (imports) {
function arg(symbol) {
var re = symbol.realpart();
var im = symbol.imagpart();
if (re.isConstant() && im.isConstant())
if (re.isConstant() && im.isConstant()) {
// if(im.equals(0) && re.equals(-1)) {
// return _.parse('pi');
// }
// else if(im.equals(1) && re.equals(0)) {
// return _.parse('pi/2');
// }
// else if(im.equals(1) && re.equals(1)) {
// return _.parse('pi/4');
// }
return new Symbol(Math.atan2(im, re));
}
return _.symfunction('atan2', [im, re]);
}

Expand Down Expand Up @@ -8059,6 +8081,11 @@ var nerdamer = (function (imports) {
this.sqrt = sqrt;
this.log = log;
this.nthroot = nthroot;
this.arg = arg;
this.conjugate = conjugate;
this.imagpart = imagpart;
this.realpart = realpart;


//Parser.methods ===============================================================
this.addPreprocessor = function (name, action, order, shift_cells) {
Expand Down Expand Up @@ -11024,6 +11051,7 @@ var nerdamer = (function (imports) {
isVariableSymbol: isVariableSymbol,
isVector: isVector,
keys: keys,
nroots: nroots,
remove: remove,
reserveNames: reserveNames,
range: range,
Expand Down Expand Up @@ -11549,7 +11577,3 @@ var nerdamer = (function (imports) {
if ((typeof module) !== 'undefined') {
module.exports = nerdamer;
};


var x = nerdamer('nroots(4+2i)');
console.log(x.text());

0 comments on commit 2b4e4a8

Please sign in to comment.