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

Commit

Permalink
Merge pull request #532 from jiggzson/dev
Browse files Browse the repository at this point in the history
v. 1.1.3
  • Loading branch information
jiggzson authored Mar 29, 2020
2 parents 06f5535 + 5e49cdc commit 0fa149e
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 111 deletions.
10 changes: 7 additions & 3 deletions Algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,11 @@ if((typeof module) !== 'undefined') {

for(var i=0, l=factors.length; i<l; i++) {
var f = factors[i];

//don't wrap group S or FN
var factor = f.power.equals(1)?
var factor = f.power.equals(1) && f.fname !== '' /* don't wrap it twice */?
_.symfunction(core.PARENTHESIS, [f]) : f;

factored = _.multiply(factored, factor);
}
if(factored.fname === '')
Expand Down Expand Up @@ -2133,6 +2135,7 @@ if((typeof module) !== 'undefined') {
factor: function(symbol, factors) {
var _symbol = _.parse(symbol);
var retval = __.Factor._factor(_symbol, factors);

if(retval.equals(symbol)) {
return retval;
}
Expand Down Expand Up @@ -2215,6 +2218,7 @@ if((typeof module) !== 'undefined') {
}
else if(symbol.group === S && symbol.isSimple())
return symbol;

//expand the symbol to get it in a predictable form. If this step
//is skipped some factors are missed.
if(symbol.group === CP) {
Expand Down Expand Up @@ -2455,7 +2459,6 @@ if((typeof module) !== 'undefined') {
* @returns {Symbol}
*/
coeffFactor: function(symbol, factors) {

if(symbol.isComposite()) {
var gcd = core.Math2.QGCD.apply(null, symbol.coeffs());
if(!gcd.equals(1)) {
Expand All @@ -2467,6 +2470,7 @@ if((typeof module) !== 'undefined') {
}
else x.multiplier = x.multiplier.divide(gcd);
});

}
symbol.updateHash();
if(factors)
Expand Down Expand Up @@ -3928,4 +3932,4 @@ if((typeof module) !== 'undefined') {
}
]);
nerdamer.api();
})();
})();
50 changes: 42 additions & 8 deletions Calculus.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {

return _.multiply(retval, m);
};

Symbol.prototype.hasTrig = function() {
if(this.isConstant(true) || this.group === S)
return false;
if(this.fname && (core.Utils.in_trig(this.fname) || core.Utils.in_inverse_trig(this.fname)))
return true;
if(this.symbols) {
for(var x in this.symbols)
if(this.symbols[x].hasTrig())
return true;
}
return false;
};

core.Expression.prototype.hasIntegral = function() {
return this.symbol.hasIntegral();
Expand Down Expand Up @@ -355,10 +368,17 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
if(core.Utils.isVector(symbol)) {
var vector = new core.Vector([]);
symbol.each(function(x) {
vector.elements.push(__.diff(x, wrt));
vector.elements.push(__.diff(x, wrt, nth));
});
return vector;
}
else if(core.Utils.isMatrix(symbol)) {
var matrix = new core.Matrix();
symbol.each(function(x, j, i) {
matrix.set(i, j, __.diff(x, wrt, nth));
});
return matrix;
}

var d = isSymbol(wrt) ? wrt.text() : wrt;
//the nth derivative
Expand Down Expand Up @@ -649,7 +669,8 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
integration: {
u_substitution: function(symbols, dx) {
function try_combo(a, b, f) {
var q = f ? f(a, b) : _.divide(a.clone(), __.diff(b, dx));
var d = __.diff(b, dx);
var q = f ? f(a, b) : _.divide(a.clone(), d);
if(!q.contains(dx, true))
return q;
return null;
Expand Down Expand Up @@ -728,8 +749,9 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
}
else if(a.isComposite() || b.isComposite()) {
var f = function(a, b) {
var d = __.diff(b, dx);
var A = core.Algebra.Factor.factor(a),
B = core.Algebra.Factor.factor(__.diff(b, dx));
B = core.Algebra.Factor.factor(d);
var q = _.divide(A, B);
return q;
};
Expand Down Expand Up @@ -764,10 +786,17 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
//TODO: This whole thing needs to be rolled into one but for now I'll leave it as two separate parts
if(!isSymbol(dx))
dx = _.parse(dx);

var result, partial_fractions;
result = new Symbol(0);
partial_fractions = core.Algebra.PartFrac.partfrac(input, dx);

if(partial_fractions.group === CB && partial_fractions.isLinear()) {
//perform a quick check to make sure that all partial fractions are linear
partial_fractions.each(function(x) {
if(!x.isLinear())
__.integration.stop();
})
partial_fractions.each(function(x) {
result = _.add(result, __.integrate(x, dx, depth, opt));
});
Expand Down Expand Up @@ -1471,7 +1500,6 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
fn2 = sym2.fname;
//reset the symbol minus the coeff
symbol = _.multiply(sym1.clone(), sym2.clone());

if(g1 === FN && g2 === FN) {
if(fn1 === LOG || fn2 === LOG) {
retval = __.integration.by_parts(symbol.clone(), dx, depth, opt);
Expand Down Expand Up @@ -1692,6 +1720,9 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
t.multiplier = sym1.multiplier;
symbol = _.divide(sym2, t);
}
else {
symbol = _.expand(symbol);
}
retval = __.integration.partial_fraction(symbol, dx, depth);
}
else if(g1 === CP && g2 === S) {
Expand Down Expand Up @@ -1983,12 +2014,15 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
};

var vars = core.Utils.variables(symbol),
integral = __.integrate(symbol, dx),
retval;

hasTrig = symbol.hasTrig();
var retval, integral;
if(vars.length === 1)
dx = vars[0];
if(!integral.hasIntegral()) {
if(!hasTrig) {
integral = __.integrate(symbol, dx);
}

if(!hasTrig && !integral.hasIntegral()) {
var upper = {},
lower = {},
a, b;
Expand Down
10 changes: 6 additions & 4 deletions Extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ if ((typeof module) !== 'undefined') {
//get the numerator and denominator
num = symbol.getNum();
den = symbol.getDenom().toUnitMultiplier();

//TODO: Make it so factor doesn't destroy pi
//num = core.Algebra.Factor.factor(symbol.getNum());
//den = core.Algebra.Factor.factor(symbol.getDenom().invert(null, true));
Expand All @@ -167,7 +168,7 @@ if ((typeof module) !== 'undefined') {
}
else
den_p = new core.Frac(1);

//convert s to a string
s = s_.toString();
//split up the denominator if in the form ax+b
Expand All @@ -188,9 +189,10 @@ if ((typeof module) !== 'undefined') {
}
else if (den.group === CP && den_p.equals(1)) {
// a/(b*s-c) -> ae^(-bt)
if (f.x.isLinear() && !num.contains(s)) {
if (f.x.isLinear() && !num.contains(s)) {console.log(f.a.toString(), f.b.toString())
t = _.divide(t, f.a.clone());
retval = _.pow(new Symbol('e'), _.multiply(t, f.b.negate()));
retval = _.parse(format('(({0})^({3}-1)*e^(-(({2})*({0}))/({1})))/(({3}-1)!*({1})^({3}))', t, f.a, f.b, den_p))
// retval = _.pow(new Symbol('e'), _.multiply(t, f.b.negate()));
//wrap it up
finalize();
}
Expand Down Expand Up @@ -573,4 +575,4 @@ if ((typeof module) !== 'undefined') {

//link registered functions externally
nerdamer.api();
}());
}());
55 changes: 42 additions & 13 deletions Solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ if ((typeof module) !== 'undefined') {
core.Settings.SOLUTION_PROXIMITY = 1e-14;
//Indicate wheter to filter the solutions are not
core.Settings.FILTER_SOLUTIONS = true;
//the maximum number of recursive calls
core.Settings.MAX_SOLVE_DEPTH = 10;

core.Symbol.prototype.hasTrig = function () {
return this.containsFunction(['cos', 'sin', 'tan', 'cot', 'csc', 'sec']);
Expand Down Expand Up @@ -100,10 +102,17 @@ if ((typeof module) !== 'undefined') {
text: function (option) {
return this.LHS.text(option) + '=' + this.RHS.text(option);
},
toLHS: function () {
var eqn = this.removeDenom();
toLHS: function (expand) {
expand = typeof expand === 'undefined' ? true : false;
var eqn;
if(!expand) {
eqn = this.clone();
}
else {
eqn = this.removeDenom();
}
var _t = _.subtract(eqn.LHS, eqn.RHS);
var retval = _.expand(_t);
var retval = expand ? _.expand(_t) : _t;
return retval;
},
removeDenom: function () {
Expand Down Expand Up @@ -294,7 +303,7 @@ if ((typeof module) !== 'undefined') {
* @param {Equation|String} eqn
* @returns {Symbol}
*/
toLHS: function (eqn) {
toLHS: function (eqn, expand) {
if(isSymbol(eqn))
return eqn;
//If it's an equation then call its toLHS function instead
Expand All @@ -304,7 +313,7 @@ if ((typeof module) !== 'undefined') {
es[1] = es[1] || '0';
eqn = new Equation(_.parse(es[0]), _.parse(es[1]));
}
return eqn.toLHS();
return eqn.toLHS(expand);
},
getSystemVariables: function(eqns) {
vars = variables(eqns[0], null, null, true);
Expand Down Expand Up @@ -743,7 +752,7 @@ if ((typeof module) !== 'undefined') {
last = f(start),
last_sign = last / Math.abs(last),
rside = core.Settings.ROOTS_PER_SIDE, // the max number of roots on right side
lside = rside * 2 + 1; // the max number of roots on left side
lside = rside; // the max number of roots on left side
// check around the starting point
points.push(Math.floor(start / 2)); //half way from zero might be a good start
points.push(Math.abs(start)); //|f(0)| could be a good start
Expand Down Expand Up @@ -935,7 +944,13 @@ if ((typeof module) !== 'undefined') {
* @param {type} solve_for
* @returns {Array}
*/
var solve = function (eqns, solve_for, solutions) {
var solve = function (eqns, solve_for, solutions, depth) {
depth = depth || 0;

if(depth++ > Settings.MAX_SOLVE_DEPTH) {
return solutions;
}

//make preparations if it's an Equation
if (eqns instanceof Equation) {
//if it's zero then we're done
Expand Down Expand Up @@ -1017,12 +1032,17 @@ if ((typeof module) !== 'undefined') {
if(eqns.group === FN && eqns.fname === 'sqrt') {
eqns = _.pow(Symbol.unwrapSQRT(eqns), new Symbol(2));
}

var eq = core.Utils.isSymbol(eqns) ? eqns : __.toLHS(eqns),
//pass in false to not expand equations such as (x+y)^5.
//It suffices to solve for the numerator since there's no value in the denominator which yields a zero for the function
var eq = (core.Utils.isSymbol(eqns) ? eqns : __.toLHS(eqns, false)).getNum(),
vars = core.Utils.variables(eq), //get a list of all the variables
numvars = vars.length;//how many variables are we dealing with


//it sufficient to solve (x+y) if eq is (x+y)^n since 0^n
if(core.Utils.isInt(eq.power) && eq.power > 0) {
eq = _.parse(eq).toLinear();
}

//if we're dealing with a single variable then we first check if it's a
//polynomial (including rationals).If it is then we use the Jenkins-Traubb algorithm.
//Don't waste time
Expand Down Expand Up @@ -1158,7 +1178,7 @@ if ((typeof module) !== 'undefined') {
//if the equation has more than one symbolic factor then solve those individually
if(factors.getNumberSymbolics() > 1) {
for(var x in factors.factors) {
add_to_result(solve(factors.factors[x]));
add_to_result(solve(factors.factors[x], solve_for));
}
}
else {
Expand All @@ -1184,6 +1204,10 @@ if ((typeof module) !== 'undefined') {
}

if (!was_calculated) {
eqns = _.parse(eqns);
if(eqns instanceof core.Equation)
eqns = eqns.toLHS();

//we can solve algebraically for degrees 1, 2, 3. The remainder we switch to Jenkins-
if (deg === 1)
add_to_result(_.divide(coeffs[0], coeffs[1].negate()));
Expand Down Expand Up @@ -1297,7 +1321,12 @@ if ((typeof module) !== 'undefined') {
if (solutions.length === 0)
add_to_result(__.divideAndConquer(eq, solve_for));
}
}

if(solutions.length === 0) {
//try factoring
add_to_result(solve(factored, solve_for, solutions, depth));
}
}

}
catch (e) { /*something went wrong. EXITING*/
Expand Down Expand Up @@ -1331,7 +1360,7 @@ if ((typeof module) !== 'undefined') {
//check if x is by itself
var x = parts[1];
if(x.group === S) {
rhs = _.divide(_.subtract(_.pow(new Symbol('e'), _.divide(rhs, _.parse(lhs.multiplier))), parts[3]), parts[0]);
rhs = _.divide(_.subtract(_.pow(lhs.args.length > 1 ? lhs.args[1] : new Symbol('e'), _.divide(rhs, _.parse(lhs.multiplier))), parts[3]), parts[0]);
var eq = new Equation(x, rhs).toLHS();
add_to_result(solve(eq, solve_for));
}
Expand Down
3 changes: 1 addition & 2 deletions all.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ require('./Solve.js');
require('./Extra.js');

//export nerdamer
module.exports = nerdamer;

module.exports = nerdamer;
Loading

0 comments on commit 0fa149e

Please sign in to comment.