Skip to content

Commit

Permalink
Fixed Incorrect Trig Derivative bug #31
Browse files Browse the repository at this point in the history
  • Loading branch information
65001 committed Dec 26, 2019
1 parent 6c65031 commit 8449f00
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
Binary file modified AbMath/AbMath.dll
Binary file not shown.
58 changes: 30 additions & 28 deletions AbMath/Calculator/AST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,32 @@ private void GenerateExponentSimplifications()

private void GenerateTrigSimplifications()
{
//TODO:
//[f(x) * cos(x)]/[g(x) * sin(x)] -> [f(x) * cot(x)]/g(x)
//[f(x) * sin(x)]/cos(x) -> f(x) * tan(x)
//sin(x)/[f(x) * cos(x)] -> tan(x)/f(x)
//[f(x) * sin(x)]/[g(x) * cos(x)] -> [f(x) * tan(x)]/g(x)

//TODO: [1 + tan(f(x))^2] -> sec(f(x))^2
//TODO: [cot(f(x))^2 + 1] -> csc(f(x))^2

//These will probably violate domain constraints ?
//TODO: sec(x)^2 - tan(x)^2 = 1
//TODO: cot(x)^2 + 1 = csc(x)^2
//TODO: csc(x)^2 - cot(x)^2 = 1

//TODO: Double Angle
//[cos(x)^2 - sin(x)^2] = cos(2x)
//1 - 2sin(x)^2 = cos(2x)
//2cos(x)^2 - 1 = cos(2x)
//2sin(x)cos(x) = sin(2x)
//[2tan(x)]/1 - tan(x)^2] = tan(2x)

//TODO: Power Reducing
//[1 - cos(2x)]/2 = sin(x)^2
//[1 + cos(2x)]/2 = cos(x)^2
//[1 - cos(2x)]/[1 + cos(2x)] = tan(x)^2


}

Expand Down Expand Up @@ -637,32 +663,6 @@ private void Simplify(RPN.Node node, SimplificationMode mode)
RPN.Node division = new RPN.Node(new[] { node[0, 1], cot }, new RPN.Token("/", 2, RPN.Type.Operator));
Assign(node, division);
}
//TODO:
//[f(x) * cos(x)]/[g(x) * sin(x)] -> [f(x) * cot(x)]/g(x)

//[f(x) * sin(x)]/cos(x) -> f(x) * tan(x)
//sin(x)/[f(x) * cos(x)] -> tan(x)/f(x)
//[f(x) * sin(x)]/[g(x) * cos(x)] -> [f(x) * tan(x)]/g(x)

//TODO: [1 + tan(f(x))^2] -> sec(f(x))^2
//TODO: [cot(f(x))^2 + 1] -> csc(f(x))^2

//These will probably violate domain constraints ?
//TODO: sec(x)^2 - tan(x)^2 = 1
//TODO: cot(x)^2 + 1 = csc(x)^2
//TODO: csc(x)^2 - cot(x)^2 = 1

//TODO: Double Angle
//[cos(x)^2 - sin(x)^2] = cos(2x)
//1 - 2sin(x)^2 = cos(2x)
//2cos(x)^2 - 1 = cos(2x)
//2sin(x)cos(x) = sin(2x)
//[2tan(x)]/1 - tan(x)^2] = tan(2x)

//TODO: Power Reducing
//[1 - cos(2x)]/2 = sin(x)^2
//[1 + cos(2x)]/2 = cos(x)^2
//[1 - cos(2x)]/[1 + cos(2x)] = tan(x)^2
}
else if (mode == SimplificationMode.Swap)
{
Expand Down Expand Up @@ -1943,8 +1943,9 @@ private void Derive(RPN.Node foo, RPN.Node variable)
RPN.Node subtraction = new RPN.Node(new[] { new RPN.Node(1), exponent },
new RPN.Token("-", 2, RPN.Type.Operator));
RPN.Node sqrt = new RPN.Node(new[] { subtraction }, new RPN.Token("sqrt", 1, RPN.Type.Function));
RPN.Node abs = new RPN.Node(new[] { body.Clone() }, new RPN.Token("abs", 1, RPN.Type.Function));
RPN.Node denominator =
new RPN.Node(new[] { sqrt, Clone(body) }, new RPN.Token("*", 2, RPN.Type.Operator));
new RPN.Node(new[] { sqrt, abs }, new RPN.Token("*", 2, RPN.Type.Operator));

RPN.Node division = new RPN.Node(new[] { denominator, bodyDerive },
new RPN.Token("/", 2, RPN.Type.Operator));
Expand Down Expand Up @@ -1975,8 +1976,9 @@ private void Derive(RPN.Node foo, RPN.Node variable)
RPN.Node subtraction = new RPN.Node(new[] { new RPN.Node(1), exponent },
new RPN.Token("-", 2, RPN.Type.Operator));
RPN.Node sqrt = new RPN.Node(new[] { subtraction }, new RPN.Token("sqrt", 1, RPN.Type.Function));
RPN.Node abs = new RPN.Node(new[] { body.Clone() }, new RPN.Token("abs", 1, RPN.Type.Function));
RPN.Node denominator =
new RPN.Node(new[] { sqrt, Clone(body) }, new RPN.Token("*", 2, RPN.Type.Operator));
new RPN.Node(new[] { sqrt, abs }, new RPN.Token("*", 2, RPN.Type.Operator));
RPN.Node multiplication = new RPN.Node(new[] { new RPN.Node(-1), bodyDerive },
new RPN.Token("*", 2, RPN.Type.Operator));
RPN.Node division = new RPN.Node(new[] { denominator, multiplication },
Expand Down
8 changes: 4 additions & 4 deletions Unit Tester/Apportionment/Calculator/Derivative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,20 @@ public void ArcCot()
public void ArcSec()
{
RPN test = new RPN("derivative( arcsec(x), x)").Compute();
Assert.AreEqual("1 x x 2 ^ 1 - sqrt * /", test.Polish.Print());
Assert.AreEqual("1 x abs x 2 ^ 1 - sqrt * /", test.Polish.Print());

test.SetEquation("derivative( arcsec(x^2), x)").Compute();
Assert.AreEqual("2 x * x 2 ^ x 4 ^ 1 - sqrt * /", test.Polish.Print());
Assert.AreEqual("2 x * x 2 ^ abs x 4 ^ 1 - sqrt * /", test.Polish.Print());
}

[Test]
public void ArcCsc()
{
RPN test = new RPN("derivative( arccsc(x), x)").Compute();
Assert.AreEqual("-1 x x 2 ^ 1 - sqrt * /", test.Polish.Print());
Assert.AreEqual("-1 x abs x 2 ^ 1 - sqrt * /", test.Polish.Print());

test.SetEquation("derivative( arccsc(x^2), x)").Compute();
Assert.AreEqual("-2 x * x 2 ^ x 4 ^ 1 - sqrt * /", test.Polish.Print());
Assert.AreEqual("-2 x * x 2 ^ abs x 4 ^ 1 - sqrt * /", test.Polish.Print());
}

[Test]
Expand Down

0 comments on commit 8449f00

Please sign in to comment.