nerdamer
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -16,3 +16,2 @@ /* | ||
//export nerdamer | ||
module.exports = nerdamer; | ||
module.exports = nerdamer; |
10
Extra.js
@@ -160,2 +160,3 @@ /* | ||
den = symbol.getDenom().toUnitMultiplier(); | ||
//TODO: Make it so factor doesn't destroy pi | ||
@@ -171,3 +172,3 @@ //num = core.Algebra.Factor.factor(symbol.getNum()); | ||
den_p = new core.Frac(1); | ||
//convert s to a string | ||
@@ -193,5 +194,6 @@ s = s_.toString(); | ||
// 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 | ||
@@ -579,2 +581,2 @@ finalize(); | ||
nerdamer.api(); | ||
}()); | ||
}()); |
@@ -9,3 +9,3 @@ { | ||
"license": "MIT", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"homepage": "http://nerdamer.com/", | ||
@@ -12,0 +12,0 @@ "directory": { |
55
Solve.js
@@ -67,2 +67,4 @@ /* | ||
core.Settings.FILTER_SOLUTIONS = true; | ||
//the maximum number of recursive calls | ||
core.Settings.MAX_SOLVE_DEPTH = 10; | ||
@@ -104,6 +106,13 @@ core.Symbol.prototype.hasTrig = function () { | ||
}, | ||
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; | ||
@@ -299,3 +308,3 @@ }, | ||
*/ | ||
toLHS: function (eqn) { | ||
toLHS: function (eqn, expand) { | ||
if(isSymbol(eqn)) | ||
@@ -310,3 +319,3 @@ return eqn; | ||
} | ||
return eqn.toLHS(); | ||
return eqn.toLHS(expand); | ||
}, | ||
@@ -750,3 +759,3 @@ getSystemVariables: function(eqns) { | ||
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 | ||
@@ -943,3 +952,9 @@ points.push(Math.floor(start / 2)); //half way from zero might be a good start | ||
*/ | ||
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 | ||
@@ -1026,8 +1041,13 @@ if (eqns instanceof Equation) { | ||
} | ||
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 | ||
@@ -1168,3 +1188,3 @@ //polynomial (including rationals).If it is then we use the Jenkins-Traubb algorithm. | ||
for(var x in factors.factors) { | ||
add_to_result(solve(factors.factors[x])); | ||
add_to_result(solve(factors.factors[x], solve_for)); | ||
} | ||
@@ -1195,2 +1215,6 @@ } | ||
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- | ||
@@ -1309,3 +1333,8 @@ if (deg === 1) | ||
} | ||
} | ||
if(solutions.length === 0) { | ||
//try factoring | ||
add_to_result(solve(factored, solve_for, solutions, depth)); | ||
} | ||
} | ||
@@ -1344,3 +1373,3 @@ } | ||
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(); | ||
@@ -1347,0 +1376,0 @@ add_to_result(solve(eq, solve_for)); |
@@ -758,5 +758,5 @@ /* global expect */ | ||
it('should calculate nth roots correctly', function() { | ||
expect(nerdamer('roots((-1)^(1/5))').evaluate().text()).toEqual('[0.5877852522924731*i+0.8090169943749475,-0.3090169943749474+0.9510565162951536*i,-1+1.2246467991473532e-16*i,-0.30901699437494756-0.9510565162951535*i,-0.5877852522924734*i+0.8090169943749473]'); | ||
expect(nerdamer('roots((2)^(1/3))').evaluate().text()).toEqual('[1.1224620483093812,-1.1224620483093812]'); | ||
expect(nerdamer('roots((-1)^(1/5))').evaluate().text()).toEqual('[0.5877852522924731*i+0.809016994374947,-0.309016994374947+0.9510565162951536*i,-1+1e-16*i,-0.309016994374948-0.9510565162951536*i,-0.5877852522924734*i+0.809016994374947]'); | ||
expect(nerdamer('roots((2)^(1/3))').evaluate().text()).toEqual('[1.122462048309381,-1.122462048309381]'); | ||
}); | ||
}); |
@@ -6,2 +6,3 @@ /* global expect */ | ||
var nerdamer = require('../nerdamer.core.js'); | ||
var round = nerdamer.getCore().Utils.round; | ||
@@ -202,7 +203,7 @@ describe('calculus', function () { | ||
given: 'defint(cos(x),1,2,x)', | ||
expected: '0.06782644201778515' | ||
expected: '0.067826442018' | ||
}, | ||
{ | ||
given: 'defint(cos(x),1,2,x)', | ||
expected: '0.06782644201778515' | ||
given: 'defint(cos(x)^3*x^2-1,-1,9)', | ||
expected: '8.543016466395' | ||
}, | ||
@@ -232,2 +233,10 @@ { | ||
expected: '-1' | ||
}, | ||
{ | ||
given: 'defint((x^2-3)/(-x^3+9x+1), 1, 3, x)', | ||
expected: '0.732408192445406585' | ||
}, | ||
{ | ||
given: 'defint(x*(x-5)^(1/2),5,8)', | ||
expected: '23.555890982936999348' | ||
} | ||
@@ -241,3 +250,3 @@ ]; | ||
// then | ||
expect(parsed.text()).toEqual(testCases[i].expected); | ||
expect(round(parsed.text(), 14)).toEqual(round(testCases[i].expected), 14); | ||
} | ||
@@ -315,3 +324,3 @@ }); | ||
given: 'integrate(asin(a*x), x)', | ||
expected: 'a^(-1)*sqrt(-(a*x)^2+1)+asin(a*x)*x' | ||
expected: 'a^(-1)*sqrt(-a^2*x^2+1)+asin(a*x)*x' | ||
}, | ||
@@ -318,0 +327,0 @@ { |
@@ -201,3 +201,7 @@ /* global expect */ | ||
given: 'solve((5*x^4-2)/(x+1)/(x^2-1),x)', | ||
expected: '[(-316684236/398209345)*i,(316684236/398209345)*i,-72425485/91070226,72425485/91070226]' | ||
expected: '[72425485/91070226,-72425485/91070226,(316684236/398209345)*i,(-316684236/398209345)*i]' | ||
}, | ||
{ | ||
given: 'solve(0=(x^(2)-2)/(e^(x)-1), x)', | ||
expected: '[-sqrt(2),sqrt(2)]' | ||
} | ||
@@ -277,5 +281,9 @@ ]; | ||
it('should solve functions with factorials', function() { | ||
expect(nerdamer('solve(x!-x^2,x)').text()).toEqual('[-2.2003917826105948,-4.010232827899529,-2.938361683501947,1,1.0000000000000009,1.0000000000000007,3.5623822853908957,3.5623822853908966,0.9999999999999998,1.0000000000000002]'); | ||
expect(nerdamer('solve(x!-x^2,x)').text('decimals', 20)).toEqual('[-2.200391782610595,-4.010232827899529,-2.938361683501947,1,1.000000000000001,1.000000000000001,3.562382285390896,3.562382285390897,0.9999999999999910,1.000000000000000]'); | ||
}); | ||
it('should solve for variables other than x', function() { | ||
expect(nerdamer('solve(2*a^(2)+4*a*6=128, a)').toString()).toEqual('[4,-16]'); | ||
}); | ||
xit('should solve factors', function() { | ||
@@ -282,0 +290,0 @@ expect(nerdamer('solve((x-1)*(-a*c-a*x+c*x+x^2),x)').text()).toEqual('[1,-c,a]'); |
@@ -32,3 +32,3 @@ /* global expect */ | ||
var parse = function(e, subs) { | ||
var r = nerdamer(e, subs).evaluate().text('decimal'); | ||
var r = nerdamer(e, subs).evaluate().text('decimals'); | ||
if(!isNaN(r)) | ||
@@ -35,0 +35,0 @@ r = Number(r); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1266841
24978