Skip to content

Commit

Permalink
fix and tests for issue 43
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmein-ts committed Mar 19, 2024
1 parent fc88d5d commit 4543e91
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
19 changes: 13 additions & 6 deletions Solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ if((typeof module) !== 'undefined') {
else if(rhs.group === FN || rhs.group === S || rhs.group === PL) {
return [rhs, lhs];
}
return [rhs, lhs];
},
sqrtSolve: function (symbol, v) {
var sqrts = new Symbol(0);
Expand Down Expand Up @@ -1303,7 +1304,7 @@ if((typeof module) !== 'undefined') {
return null;
}
// can handle only abs at beginning
if (eqns.LHS.group !== FN && eqns.RHS.group !== FN) {
if ((eqns.LHS.group !== FN || false)&& eqns.RHS.group !== FN) {
return null;
}
// we have exactly one abs. kill it and make two cases
Expand All @@ -1313,7 +1314,7 @@ if((typeof module) !== 'undefined') {
const resultplus = solve(eqplus, solve_for, null, depth, fn);
const resultminus = solve(eqminus, solve_for, null, depth, fn);

return [resultplus, resultminus];
return [resultminus, resultplus];
}
/*
*
Expand Down Expand Up @@ -1743,7 +1744,8 @@ if((typeof module) !== 'undefined') {
}

// sort by numerical value to be ready for uniquefy filter
solutions.sort((a,b)=>{
solutions
.sort((a,b)=>{
const sa = a.text("decimals");
const sb = b.text("decimals");
const xa = Number(sa);
Expand All @@ -1758,10 +1760,15 @@ if((typeof module) !== 'undefined') {
return xa-xb;
});

// round to 15 digits
solutions = solutions
.map((a)=>(!a.isConstant()?a:new Symbol(Number(Number(a).toPrecision(15)))));

// uniquefy to epsilon
// console.log("solutions: "+solutions);
solutions = solutions.filter((x,i,a)=>{
x = Number(x);
solutions = solutions
.filter((x,i,a)=>{
x = Number(Number(x).toPrecision(15));
let x2 = Number(a[i-1]);
// console.log(" x: "+x)
if (i === 0 || isNaN(x) || isNaN(x2)) {
Expand Down Expand Up @@ -1924,7 +1931,7 @@ if((typeof module) !== 'undefined') {
}
}
catch(error) {
;
console.log("error "+error);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion all.min.js

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions spec/solve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Solve', function () {
expect(nerdamer('solve(-5*sqrt(14)x-14x^2*sqrt(83)-10x=0,x)').toString()).toEqual('[(-5/14)*(2+sqrt(14))*sqrt(83)^(-1),0]');
expect(nerdamer('solve(8*x^3-26x^2+3x+9,x)').toString()).toEqual('[3/4,-1/2,3]');
expect(nerdamer('solve(a*x^2+b*x+c, x)').toString()).toEqual('[(1/2)*(-b+sqrt(-4*a*c+b^2))*a^(-1),(1/2)*(-b-sqrt(-4*a*c+b^2))*a^(-1)]');
expect(nerdamer('solve(sqrt(x^3)+sqrt(x^2)-sqrt(x)=0,x)').toString()).toEqual('[0,78202389238903801/240831735646702201]');
expect(nerdamer('solve(sqrt(x^3)+sqrt(x^2)-sqrt(x)=0,x)').toString()).toEqual('[0,9854668106203761/30348392770825801]');
expect(nerdamer('solve(x^3-10x^2+31x-30,x)').toString()).toEqual('[3,5,2]');
expect(nerdamer('solve(sqrt(x)+sqrt(2x+1)=5,x)').toString()).toEqual('[4]');
expect(nerdamer('solve(x=2/(3-x),x)').toString()).toEqual('[1,2]');
Expand Down Expand Up @@ -87,10 +87,10 @@ describe('Solve', function () {
expect(nerdamer('(y+((x)^(2)))=9').solveFor('x').toString() ).toEqual('sqrt(-y+9),-sqrt(-y+9)');
expect(nerdamer('solve(c*((((((4*x)+1))*d))/(5))*f=((a)/(b)), x)').toString()).toEqual('[(-1/4)*(-5*a+b*c*d*f)*(b*c*d*f)^(-1)]');
expect(nerdamer('c*((((((4*x)+1))*d))/(5))*f=((a)/(b))').solveFor('x').toString()).toEqual('(1/4)*(-b*c*d*f+5*a)*(b*c*d*f)^(-1)');
expect(nerdamer('solve(1000+200*x=100*2^(x/2), x)').toString()).toEqual('[-246320546/50179663,605937277/62026537]');
expect(nerdamer('solve(x=100*2^((1/400)*(-1000+x)), x)').toString()).toEqual('[332944835/18248037,2393608973/810349]');
expect(nerdamer('solve(1000+200*x=100*2^(x/2), x)').toString()).toEqual('[-89048725/18140732,145224097/14865809]');
expect(nerdamer('solve(x=100*2^((1/400)*(-1000+x)), x)').toString()).toEqual('[332944835/18248037,37066322021/12548690]');
expect(nerdamer('solve(m=(34141034228515471851/614756350000000000000000000)*(3631430813109509/100837351+51955423*log(5+m)), m)')
.toString()).toEqual('[-246320546/50179663,260402392/26655991]');
.toString()).toEqual('[-89048725/18140732,145224097/14865809]');
expect(nerdamer('(5-3y)/(5+y)=(1-9y)/(3y-7)').solveFor("y").toString()).toEqual('1/2');
// np issue #26
// expect(nerdamer("solve(h=1/2*(9.81)*m*s^(-2)*t^2, t)").evaluate().text()).toEqual("[0.4515236409857309*s*sqrt(h)*sqrt(m)^(-1),-0.4515236409857309*s*sqrt(h)*sqrt(m)^(-1)]");
Expand Down Expand Up @@ -158,7 +158,16 @@ describe('Solve', function () {
.toEqual('[-0.0305810397553517*baseunit_m^(-1)*baseunit_s*sqrt(218)*sqrt(baseunit_m)*sqrt(h),'+
'0.0305810397553517*baseunit_m^(-1)*baseunit_s*sqrt(218)*sqrt(baseunit_m)*sqrt(h)]');
expect(nerdamer('solve(abs(a-b)=0, a)').toString()).toEqual('[b]');
expect(nerdamer('solve(abs(a-b)=5, a)').toString()).toEqual('[-(-5-b),-5+b]');
expect(nerdamer('solve(abs(a-b)=5, a)').toString()).toEqual('[-5+b,-(-5-b)]');
// issue #43
expect(nerdamer('solve(x-6/x - 1 = 0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve(x^2-x-6=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^2=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^x=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^(x-4)=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^(x+4)=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^(x^2)=0,x)').toString()).toEqual('[-2,3]');
expect(nerdamer('solve((x^2-x-6)*e^(x^2)=0,x)').toString()).toEqual('[-2,3]');
});
});

Expand Down
21 changes: 20 additions & 1 deletion spec/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,26 @@ let x;
// console.log(x);
// x = nerdamer("(a/((a+1)*(a+b)))/((1/(a+1))+(1/(a+b)))").simplify().text();
// console.log(x);
x = nerdamer('solve(sqrt(x^3)+sqrt(x^2)-sqrt(x)=0,x)').toString();

x=nerdamer('solve(x-6/x - 1 = 0,x)').toString();
console.log(x);
x=nerdamer('solve(x^2-x-6=0,x)').toString();
console.log(x);
x=nerdamer('solve((x^2-x-6)*e^2=0,x)').toString();
console.log(x);

x=nerdamer('solve((x^2-x-6)*e^x=0,x)').toString();
console.log(x);
x=nerdamer('solve((x^2-x-6)*e^x=0,x)').toString();
console.log(x);

x=nerdamer('solve((x^2-x-6)*e^(x-4)=0,x)').toString();
console.log(x);
x=nerdamer('solve((x^2-x-6)*e^(x+4)=0,x)').toString();
console.log(x);
x=nerdamer('solve((x^2-x-6)*e^(x^2)=0,x)').toString();
console.log(x);
x=nerdamer('solve((x^2-x-6)*e^(x^2)=0,x)').toString();
console.log(x);

// nerdamer.setVar("x","-sqrt(-1+z)");
Expand Down

0 comments on commit 4543e91

Please sign in to comment.