Skip to content

Commit e9188ef

Browse files
committed
WIP #930 iterative function for TrigExpand of Cosh / Sinh(a+b+c)
1 parent 7de2624 commit e9188ef

File tree

2 files changed

+49
-95
lines changed

2 files changed

+49
-95
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/reflection/system/TrigExpand.java

+34-95
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public IExpr apply(IExpr ast) {
7373
}
7474

7575
private static IExpr distributeTimes(IExpr expr) {
76-
if (expr.isTimes()) {
77-
return Algebra.distributeTimes(expr);
78-
}
79-
return expr;
76+
return Algebra.distributeTimes(expr);
8077
}
8178

8279
/**
@@ -88,21 +85,22 @@ private static IExpr distributeTimes(IExpr expr) {
8885
*/
8986
private IExpr expandPlus(IAST ast, IAST plusAST) {
9087
if (ast.isSin()) {
91-
return expandSinPlus(plusAST, 1);
88+
return expandCosSinPlus(plusAST, false);
9289
} else if (ast.isCos()) {
93-
return expandCosPlus(plusAST, 1);
90+
return expandCosSinPlus(plusAST, true);
91+
// return expandCosPlus(plusAST, 1);
9492
} else if (ast.isAST(S.Cot, 2)) {
9593
// Cos(x) / Sin(x)
96-
return F.Divide(expandCosPlus(plusAST, 1), expandSinPlus(plusAST, 1));
94+
return F.Divide(expandCosSinPlus(plusAST, true), expandCosSinPlus(plusAST, false));
9795
} else if (ast.isTan()) {
9896
// Sin(x) / Cos(x)
99-
return F.Divide(expandSinPlus(plusAST, 1), expandCosPlus(plusAST, 1));
97+
return F.Divide(expandCosSinPlus(plusAST, false), expandCosSinPlus(plusAST, true));
10098
} else if (ast.isAST(S.Csc, 2)) {
10199
// 1 / Sin(x)
102-
return expandSinPlus(plusAST, 1).inverse();
100+
return expandCosSinPlus(plusAST, false).inverse();
103101
} else if (ast.isAST(S.Sec, 2)) {
104102
// 1 / Cos(x)
105-
return expandCosPlus(plusAST, 1).inverse();
103+
return expandCosSinPlus(plusAST, true).inverse();
106104
} else if (ast.isAST(S.Sech, 2)) {
107105
return expandSechPlus(plusAST, 1);
108106
} else if (ast.isSinh()) {
@@ -145,7 +143,7 @@ private IExpr expandTimes(IAST ast, IAST timesAST) {
145143
return expandSinTimes(n, theta).inverse();
146144
} else if (ast.isAST(S.Sec, 2)) {
147145
// 1 / Cos(x)
148-
return expandCosTimes(n, theta).inverse();
146+
return expandCosTimes(n, theta).inverse();
149147
} else if (ast.isSinh()) {
150148
int nInt = n.toInt();
151149
// return expandSinhPlus(F.constantArray(F.Plus, theta, nInt), 1);
@@ -209,71 +207,7 @@ private static IExpr expandSinTimes(IInteger n, IExpr theta) {
209207
}
210208

211209
/**
212-
* <code>Sin(a+b+c+...)</code>
213-
*
214-
* @param plusAST
215-
* @param startPosition
216-
* @return
217-
*/
218-
private static IExpr expandSinPlus(IAST plusAST, int startPosition) {
219-
IASTAppendable result = F.PlusAlloc(2);
220-
IExpr lhs = plusAST.get(startPosition);
221-
if (startPosition == plusAST.size() - 2) {
222-
IExpr rhs = plusAST.get(startPosition + 1);
223-
result.append(Times(Sin(lhs), Cos(rhs)));
224-
result.append(Times(Cos(lhs), Sin(rhs)));
225-
} else {
226-
result.append(Times(Sin(lhs), expandCosPlus(plusAST, startPosition + 1)));
227-
result.append(Times(Cos(lhs), expandSinPlus(plusAST, startPosition + 1)));
228-
}
229-
return result;
230-
}
231-
232-
/**
233-
* <code>Sinh(a+b+c+...)</code>
234-
*
235-
* @param plusAST
236-
* @param startPosition
237-
* @return
238-
*/
239-
// private static IExpr expandSinhPlus(IAST plusAST, int startPosition) {
240-
// IASTAppendable result = F.PlusAlloc(2);
241-
// IExpr lhs = plusAST.get(startPosition);
242-
// if (startPosition == plusAST.size() - 2) {
243-
// // Sinh(x)*Cosh(y) + Cosh(x)*Sinh(y)
244-
// IExpr rhs = plusAST.get(startPosition + 1);
245-
// result.append(Times(F.Sinh(lhs), F.Cosh(rhs)));
246-
// result.append(Times(F.Cosh(lhs), F.Sinh(rhs)));
247-
// } else {
248-
// result.append(Times(F.Sinh(lhs), expandCoshPlus(plusAST, startPosition + 1)));
249-
// result.append(Times(F.Cosh(lhs), expandSinhPlus(plusAST, startPosition + 1)));
250-
// }
251-
// return result;
252-
// }
253-
254-
/**
255-
* <code>Sin(a+b+c+...)</code>
256-
*
257-
* @param plusAST
258-
* @param startPosition
259-
* @return
260-
*/
261-
private static IExpr expandCosPlus(IAST plusAST, int startPosition) {
262-
IASTAppendable result = F.PlusAlloc(2);
263-
IExpr lhs = plusAST.get(startPosition);
264-
if (startPosition == plusAST.size() - 2) {
265-
IExpr rhs = plusAST.get(startPosition + 1);
266-
result.append(Times(Cos(lhs), Cos(rhs)));
267-
result.append(Times(CN1, Sin(lhs), Sin(rhs)));
268-
} else {
269-
result.append(Times(Cos(lhs), expandCosPlus(plusAST, startPosition + 1)));
270-
result.append(Times(CN1, Sin(lhs), expandSinPlus(plusAST, startPosition + 1)));
271-
}
272-
return result;
273-
}
274-
275-
/**
276-
* <code>if (coshResult==true) then Cosh(a+b+c+...) else Sinh(a+b+c+...)</code>
210+
* <code>if (coshResult==true) then TrigExand(Cosh(a+b+c+...)) else TrigExand(Sinh(a+b+c+...))</code>
277211
*
278212
* @param plusAST
279213
* @param coshResult
@@ -288,34 +222,39 @@ private static IExpr expandCoshSinhPlus(IAST plusAST, boolean coshResult) {
288222
IExpr sinhPlus = F.Plus(Times(F.Sinh(lhs), F.Cosh(rhs)), Times(F.Cosh(lhs), F.Sinh(rhs)));
289223
for (int i = 3; i < plusAST.size(); i++) {
290224
lhs = plusAST.get(i);
291-
IExpr coshTemp = F.Plus(Times(F.Cosh(lhs), coshPlus), Times(F.Sinh(lhs), sinhPlus));
292-
IExpr sinhTemp = F.Plus(Times(F.Sinh(lhs), coshPlus), Times(F.Cosh(lhs), sinhPlus));
225+
IExpr coshTemp = F.Plus(distributeTimes(Times(F.Cosh(lhs), coshPlus)),
226+
distributeTimes(Times(F.Sinh(lhs), sinhPlus)));
227+
IExpr sinhTemp = F.Plus(distributeTimes(Times(F.Sinh(lhs), coshPlus)),
228+
distributeTimes(Times(F.Cosh(lhs), sinhPlus)));
293229
coshPlus = coshTemp;
294230
sinhPlus = sinhTemp;
295231
}
296232
return coshResult ? coshPlus : sinhPlus;
297233
}
234+
298235
/**
299-
* <code>Cosh(a+b+c+...)</code>
300-
*
236+
* <code>if (cosResult==true) then TrigExand(Cos(a+b+c+...)) else TrigExand(Sin(a+b+c+...))</code>
237+
*
301238
* @param plusAST
302-
* @param startPosition
239+
* @param cosResult
303240
* @return
304241
*/
305-
// private static IExpr expandCoshPlus(IAST plusAST, int startPosition) {
306-
// IASTAppendable result = F.PlusAlloc(2);
307-
// IExpr lhs = plusAST.get(startPosition);
308-
// if (startPosition == plusAST.size() - 2) {
309-
// // Cosh(x)*Cosh(y) + Sinh(x)*Sinh(y)
310-
// IExpr rhs = plusAST.get(startPosition + 1);
311-
// result.append(Times(F.Cosh(lhs), F.Cosh(rhs)));
312-
// result.append(Times(F.Sinh(lhs), F.Sinh(rhs)));
313-
// } else {
314-
// result.append(Times(F.Cosh(lhs), expandCoshPlus(plusAST, startPosition + 1)));
315-
// result.append(Times(F.Sinh(lhs), expandSinhPlus(plusAST, startPosition + 1)));
316-
// }
317-
// return result;
318-
// }
242+
private static IExpr expandCosSinPlus(IAST plusAST, boolean cosResult) {
243+
IExpr lhs = plusAST.arg1();
244+
IExpr rhs = plusAST.arg2();
245+
IExpr cosPlus = F.Plus(Times(Cos(lhs), Cos(rhs)), Times(CN1, Sin(lhs), Sin(rhs)));
246+
IExpr sinPlus = F.Plus(Times(Sin(lhs), Cos(rhs)), Times(Cos(lhs), Sin(rhs)));
247+
for (int i = 3; i < plusAST.size(); i++) {
248+
lhs = plusAST.get(i);
249+
IExpr cosTemp = F.Plus(distributeTimes(Times(F.Cos(lhs), cosPlus)),
250+
distributeTimes(Times(CN1, Sin(lhs), sinPlus)));
251+
IExpr sinTemp = F.Plus(distributeTimes(Times(F.Sin(lhs), cosPlus)),
252+
distributeTimes(Times(F.Cos(lhs), sinPlus)));
253+
cosPlus = cosTemp;
254+
sinPlus = sinTemp;
255+
}
256+
return cosResult ? cosPlus : sinPlus;
257+
}
319258

320259
/**
321260
* <code>Csch(a+b+c+...)</code>

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java

+15
Original file line numberDiff line numberDiff line change
@@ -25657,6 +25657,21 @@ public void testTrigExpand() {
2565725657
"Cosh(a)*Cosh(b)*Cosh(c)+Cosh(c)*Sinh(a)*Sinh(b)+Cosh(b)*Sinh(a)*Sinh(c)+Cosh(a)*Sinh(b)*Sinh(c)");
2565825658
check("TrigExpand(Cosh(a+b+c+d))", //
2565925659
"Cosh(a)*Cosh(b)*Cosh(c)*Cosh(d)+Cosh(c)*Cosh(d)*Sinh(a)*Sinh(b)+Cosh(b)*Cosh(d)*Sinh(a)*Sinh(c)+Cosh(a)*Cosh(d)*Sinh(b)*Sinh(c)+Cosh(b)*Cosh(c)*Sinh(a)*Sinh(d)+Cosh(a)*Cosh(c)*Sinh(b)*Sinh(d)+Cosh(a)*Cosh(b)*Sinh(c)*Sinh(d)+Sinh(a)*Sinh(b)*Sinh(c)*Sinh(d)");
25660+
// issue #930
25661+
check("TrigExpand(Sin(a+b))", //
25662+
"Cos(b)*Sin(a)+Cos(a)*Sin(b)");
25663+
check("TrigExpand(Sin(a+b+c))", //
25664+
"Cos(b)*Cos(c)*Sin(a)+Cos(a)*Cos(c)*Sin(b)+Cos(a)*Cos(b)*Sin(c)-Sin(a)*Sin(b)*Sin(c)");
25665+
check("TrigExpand(Sin(a+b+c+d))", //
25666+
"Cos(b)*Cos(c)*Cos(d)*Sin(a)+Cos(a)*Cos(c)*Cos(d)*Sin(b)+Cos(a)*Cos(b)*Cos(d)*Sin(c)-Cos(d)*Sin(a)*Sin(b)*Sin(c)+Cos(a)*Cos(b)*Cos(c)*Sin(d)-Cos(c)*Sin(a)*Sin(b)*Sin(d)-Cos(b)*Sin(a)*Sin(c)*Sin(d)-Cos(a)*Sin(b)*Sin(c)*Sin(d)");
25667+
// issue #930
25668+
check("TrigExpand(Cos(a+b))", //
25669+
"Cos(a)*Cos(b)-Sin(a)*Sin(b)");
25670+
check("TrigExpand(Cos(a+b+c))", //
25671+
"Cos(a)*Cos(b)*Cos(c)-Cos(c)*Sin(a)*Sin(b)-Cos(b)*Sin(a)*Sin(c)-Cos(a)*Sin(b)*Sin(c)");
25672+
check("TrigExpand(Cos(a+b+c+d))", //
25673+
"Cos(a)*Cos(b)*Cos(c)*Cos(d)-Cos(c)*Cos(d)*Sin(a)*Sin(b)-Cos(b)*Cos(d)*Sin(a)*Sin(c)-Cos(a)*Cos(d)*Sin(b)*Sin(c)-Cos(b)*Cos(c)*Sin(a)*Sin(d)-Cos(a)*Cos(c)*Sin(b)*Sin(d)-Cos(a)*Cos(b)*Sin(c)*Sin(d)+Sin(a)*Sin(b)*Sin(c)*Sin(d)");
25674+
2566025675

2566125676
check("TrigExpand( Csch(2*x) )", //
2566225677
"1/2*Csch(x)*Sech(x)");

0 commit comments

Comments
 (0)