Skip to content

Commit

Permalink
WIP #930 iterative functions for TrigExpand
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Mar 10, 2024
1 parent e9188ef commit 286f66d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.matheclipse.core.expression.ImplementationStatus;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IASTAppendable;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.IInteger;
import org.matheclipse.core.visit.VisitorPlusTimesPowerReplaceAll;
Expand Down Expand Up @@ -109,8 +108,12 @@ private IExpr expandPlus(IAST ast, IAST plusAST) {
return expandCoshSinhPlus(plusAST, true);
} else if (ast.isAST(S.Csch, 2)) {
return expandCschPlus(plusAST, 1);
} else if (ast.isAST(S.Coth, 2)) {
// Cosh(x) / Sinh(x)
return F.Divide(expandCoshSinhPlus(plusAST, true), expandCoshSinhPlus(plusAST, false));
} else if (ast.isTanh()) {
return expandTanhPlus(plusAST, 1);
// Sinh(x) / Cosh(x)
return F.Divide(expandCoshSinhPlus(plusAST, false), expandCoshSinhPlus(plusAST, true));
}
return F.NIL;
}
Expand Down Expand Up @@ -298,28 +301,6 @@ private static IExpr expandSechPlus(IAST plusAST, int startPosition) {
F.Power(F.Plus(F.Times(F.Cosh(b), F.Cosh(a)), F.Times(F.Sinh(a), F.Sinh(b))), F.CN1)));
}

/**
* <code>Tanh(a+b+c+...)</code>
*
* @param plusAST
* @param startPosition
* @return
*/
private static IExpr expandTanhPlus(IAST plusAST, int startPosition) {
IASTAppendable result = F.TimesAlloc(2);
IExpr lhs = plusAST.get(startPosition);
if (startPosition == plusAST.size() - 2) {
// (Tanh(x)+Tanh(y)) / (1+Tanh(x)*Tanh(y))
IExpr rhs = plusAST.get(startPosition + 1);
result.append(Plus(F.Tanh(lhs), F.Tanh(rhs)));
result.append(F.Power(Plus(F.C1, Times(F.Tanh(lhs), F.Tanh(rhs))), F.CN1));
} else {
result.append(Plus(F.Tanh(lhs), expandTanhPlus(plusAST, startPosition + 1)));
result.append(F.Power(
Plus(F.C1, Times(F.Tanh(lhs), expandTanhPlus(plusAST, startPosition + 1))), F.CN1));
}
return result;
}
}

public TrigExpand() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25704,12 +25704,18 @@ public void testTrigExpand() {
check("TrigExpand(Sinh(a+b+c))", //
"Cosh(b)*Cosh(c)*Sinh(a)+Cosh(a)*Cosh(c)*Sinh(b)+Cosh(a)*Cosh(b)*Sinh(c)+Sinh(a)*Sinh(b)*Sinh(c)");

check("TrigExpand(Coth(a+b))", //
"(Cosh(a)*Cosh(b))/(Cosh(b)*Sinh(a)+Cosh(a)*Sinh(b))+" //
+ "(Sinh(a)*Sinh(b))/(Cosh(b)*Sinh(a)+Cosh(a)*Sinh(b))");

check("TrigExpand(Tanh(a+b))", //
"Tanh(a)/(1+Tanh(a)*Tanh(b))+Tanh(b)/(1+Tanh(a)*Tanh(b))");
"(Cosh(b)*Sinh(a))/(Cosh(a)*Cosh(b)+Sinh(a)*Sinh(b))+" //
+ "(Cosh(a)*Sinh(b))/(Cosh(a)*Cosh(b)+Sinh(a)*Sinh(b))");
check("TrigExpand(Tanh(a+b+c))", //
"Tanh(a)/(1+(Tanh(a)*Tanh(b))/(1+Tanh(b)*Tanh(c))+(Tanh(a)*Tanh(c))/(1+Tanh(b)*Tanh(c)))+Tanh(b)/((\n"
+ "1+Tanh(b)*Tanh(c))*(1+(Tanh(a)*Tanh(b))/(1+Tanh(b)*Tanh(c))+(Tanh(a)*Tanh(c))/(1+Tanh(b)*Tanh(c))))+Tanh(c)/((\n"
+ "1+Tanh(b)*Tanh(c))*(1+(Tanh(a)*Tanh(b))/(1+Tanh(b)*Tanh(c))+(Tanh(a)*Tanh(c))/(1+Tanh(b)*Tanh(c))))");
"(Cosh(b)*Cosh(c)*Sinh(a))/(Cosh(a)*Cosh(b)*Cosh(c)+Cosh(c)*Sinh(a)*Sinh(b)+Cosh(b)*Sinh(a)*Sinh(c)+Cosh(a)*Sinh(b)*Sinh(c))+"//
+ "(Cosh(a)*Cosh(c)*Sinh(b))/(Cosh(a)*Cosh(b)*Cosh(c)+Cosh(c)*Sinh(a)*Sinh(b)+Cosh(b)*Sinh(a)*Sinh(c)+Cosh(a)*Sinh(b)*Sinh(c))+"//
+ "(Cosh(a)*Cosh(b)*Sinh(c))/(Cosh(a)*Cosh(b)*Cosh(c)+Cosh(c)*Sinh(a)*Sinh(b)+Cosh(b)*Sinh(a)*Sinh(c)+Cosh(a)*Sinh(b)*Sinh(c))+"//
+ "(Sinh(a)*Sinh(b)*Sinh(c))/(Cosh(a)*Cosh(b)*Cosh(c)+Cosh(c)*Sinh(a)*Sinh(b)+Cosh(b)*Sinh(a)*Sinh(c)+Cosh(a)*Sinh(b)*Sinh(c))");

check("TrigExpand(Csc(a+b+c))", //
"1/(Cos(b)*Cos(c)*Sin(a)+Cos(a)*Cos(c)*Sin(b)+Cos(a)*Cos(b)*Sin(c)-Sin(a)*Sin(b)*Sin(c))");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public void testTrigSimplifyTR10() {

tr10 = TrigSimplifyFu.tr10(F.Sin(F.Plus(F.a, F.b, F.c)));
assertEquals(tr10.toString(), //
"Sin(a)*Cos(b)*Cos(c)+Sin(a)*-Sin(b)*Sin(c)+Cos(a)*Cos(c)*Sin(b)+Cos(a)*Cos(b)*Sin(c)");
"Cos(c)*Cos(b)*Sin(a)+Cos(c)*Cos(a)*Sin(b)+Cos(a)*Cos(b)*Sin(c)+-Sin(a)*Sin(b)*Sin(c)");
}

@Test
Expand Down

0 comments on commit 286f66d

Please sign in to comment.