Skip to content

Commit

Permalink
fix the issue with sign of a and b in a*x^n==b equations
Browse files Browse the repository at this point in the history
Task #140 - solve gives wrong answers for some polynomial equations
  • Loading branch information
grzegorzmazur committed Oct 13, 2015
1 parent 4466691 commit 1bd8c69
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions scripts/univar.rep/code.ys
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,10 @@ Rule("PSolve", 1, 1, IsUniVar(uni) And Degree(uni) > 1 And Coef(uni, 0 .. Degree

// Special case: a * x^n + b, solution generated by roots of unity
Rule("PSolve", 1, 1, IsUniVar(uni) And Degree(uni) > 1 And ConstantTerm(uni) != 0 And Coef(uni, 1 .. Degree(uni) - 1) = ZeroVector(Degree(uni) - 1)) [
Local(n, k);
Local(n, k, phi0);
n := Degree(uni);
(-ConstantTerm(uni))^(1/n) / Abs(LeadingCoef(uni))^(1/n) * MapSingle({{k}, Complex(Cos(2 * k * Pi / n), Sin(2 * k * Pi / n))}, 0 .. n - 1);
phi0 := If (Sign(LeadingCoef(uni)) * Sign(ConstantTerm(uni)) < 0, 0, Pi);
(Abs(ConstantTerm(uni) / LeadingCoef(uni)))^(1/n) * MapSingle({{k}, Complex(Cos((phi0 + 2 * k * Pi) / n), Sin((phi0 + 2 * k * Pi) / n))}, 0 .. n - 1);
];

Rule("PSolve", 1, 10, IsUniVar(uni) And Degree(uni) = 2)
Expand Down
12 changes: 12 additions & 0 deletions tests/solve.yts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ VerifySolve(Solve(x^5 == 1, x),
{ x == 1, x == Exp(2/5*I*Pi), x == Exp(4/5*I*Pi),
x == Exp(-2/5*I*Pi), x == Exp(-4/5*I*Pi)});

VerifySolve(Solve(x^3 == 1, x),
{x==1,x==Complex((-1)/2,Sqrt(3/4)),x==Complex((-1)/2,-Sqrt(3/4))});

VerifySolve(Solve(x^3 == -1, x),
{x==Complex(1/2,Sqrt(3/4)),x==(-1),x==Complex(1/2,-Sqrt(3/4))});

VerifySolve(Solve(-x^3 == 1, x),
{x==Complex(1/2,Sqrt(3/4)),x==(-1),x==Complex(1/2,-Sqrt(3/4))});

VerifySolve(Solve(-x^3 == -1, x),
{x==1,x==Complex((-1)/2,Sqrt(3/4)),x==Complex((-1)/2,-Sqrt(3/4))});

VerifySolve(Solve(Sqrt(x) == 1, x), { x == 1 });
VerifySolve(Solve(Sqrt(x) == -1, x), { });
VerifySolve(Solve(Sqrt(x) == I, x), { x == -1 });
Expand Down

0 comments on commit 1bd8c69

Please sign in to comment.