Skip to content

Commit

Permalink
Improve LerchPhi
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Nov 9, 2024
1 parent 8e47671 commit 5b8c4bb
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.hipparchus.exception.MathIllegalArgumentException;
import org.hipparchus.exception.MathIllegalStateException;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.basic.OperationSystem;
import org.matheclipse.core.builtin.functions.BesselJS;
import org.matheclipse.core.builtin.functions.GammaJS;
import org.matheclipse.core.builtin.functions.ZetaJS;
Expand Down Expand Up @@ -1489,24 +1488,95 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
IExpr s = ast.arg2();
IExpr a = ast.arg3();
if (s.isZero()) {
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/02/01/0007/
return F.Power(F.Subtract(F.C1, z), F.CN1);
}

if (a.isZero()) {
return F.PolyLog(s, z);
if (z.isZero()) {
if (s.isOne() && a.isZero()) {
// LerchPhi(0,1,0)
return F.C0;
}
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/03/01/0002/
// (a^2)^(-s/2)
return F.Power(F.Power(a, 2), s.isOne() ? F.CN1D2 : F.Times(F.CN1D2, s));
}
if (a.isOne()) {
// PolyLog(s, z)/z
return F.Times(F.PolyLog(s, z), F.Power(z, F.CN1));
if (z.isMinusOne()) {
if (s.isOne() && a.isZero()) {
// LerchPhi(-1,1,0)
// -Log(2)
return F.Negate(F.Log(F.C2));
}
if (s.isNumEqualInteger(F.C2) && a.isRationalValue(F.C1D2)) {
// LerchPhi(-1,2,1/2)
// 4*Catalan
return F.Times(F.C4, F.Catalan);
}
if (a.isOne()) {
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/05/01/
// (1-2^(1-s))*Zeta(s)
return F.Times(F.Subtract(F.C1, F.Power(F.C2, F.Subtract(F.C1, s))), F.Zeta(s));
}
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/03/01/0001/
// Zeta(s,a/2)/2^s-Zeta(s,1/2*(1+a))/2^s
return F.Plus(F.Times(F.Power(F.Power(F.C2, s), F.CN1), F.Zeta(s, F.Times(F.C1D2, a))),
F.Times(F.CN1, F.Power(F.Power(F.C2, s), F.CN1),
F.Zeta(s, F.Times(F.C1D2, F.Plus(F.C1, a)))));
}
if (z.isOne()) {
if (s.isOne() && a.isNumber()) {
return F.CInfinity;
}
if (s.isNumEqualInteger(F.C2) && a.isOne()) {
// LerchPhi(1, 2, 1)
// Pi^2/6
return F.Times(F.QQ(1L, 6L), F.Sqr(F.Pi));
}


if (a.isOne()) {
IExpr re = s.re();
if (re.isReal() && ((IReal) re).isGT(F.C1)) {
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/05/01/0001/
return F.Zeta(s);
}
}
} else if (z.isNumEqualInteger(F.C2)) {
if (s.isOne() && a.isZero()) {
// LerchPhi(2, 1, 0)
// -I*Pi
F.Times(F.CNI, F.Pi);
}
} else if (z.equals(F.CC(1, 2, -1, 2))) {
if (s.isNumEqualInteger(F.C2) && a.isOne()) {
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/02/01/0003/
// LerchPhi(1/2-1/2*I, 2, 1)
// (1+I)*PolyLog(2,1/2-I/2)
return F.Times(F.Plus(F.C1, F.CI), F.PolyLog(F.C2, F.CC(1, 2, -1, 2)));
}

}

if (z.isZero()) {
if (s.isOne()) {
return F.Power(F.Power(a, 2), F.CN1D2);
int n = a.toIntDefault();
if (n != Integer.MIN_VALUE) {
IExpr polyLog = engine.evaluate(F.PolyLog(s, z));
if (n <= 0) {
n = -n;
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/01/01/0002/
// z^n*(PolyLog(s,z)+Sum(1/(z^k*k^s),{k,1,n}))
return F.Times(F.Power(z, n), //
F.Plus(polyLog, //
F.sum(k -> F.Power(F.Times(F.Power(z, k), F.Power(k, s)), F.CN1), 1, n)));
} else {
// https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/03/01/01/01/0009/
// (PolyLog(s,z)-Sum(z^k/k^s,{k,1,-1+n}))/z^n
return F.Times(F.Power(z, -n), //
F.Subtract(polyLog, //
F.sum(k -> F.Times(F.Power(F.Power(k, s), F.CN1), F.Power(z, k)), 1, n - 1)));
}
// (a^2)^(-s/2)
return F.Power(F.Power(a, F.C2), F.Times(F.CN1D2, s));
}


return F.NIL;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.matheclipse.core.eval.exception;

/** Exception which will be thrown, if the Config.MAX_AST_SIZE limit was exceeded. */
/** Exception which will be thrown, if the {@link Config#MAX_AST_SIZE} limit was exceeded. */
public class ASTElementLimitExceeded extends LimitException {

private static final long serialVersionUID = 8925451277545397036L;
Expand All @@ -27,7 +27,8 @@ public String getMessage() {
}

/**
* Throws exception which will be thrown, if the Config.MAX_AST_SIZE limit was exceeded.
* Throws a new <code>ASTElementLimitExceeded</code> exception, if the {@link Config#MAX_AST_SIZE}
* limit was exceeded.
*
* <p>
* Usage:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.matheclipse.core.eval.exception;

/** Exception which will be thrown, if the Config.MAX_AST_SIZE limit was exceeded. */
import org.matheclipse.core.basic.Config;

/** Exception which will be thrown, if the {@link Config#MAX_BIT_LENGTH} was exceeded. */
public class BigIntegerLimitExceeded extends LimitException {

private static final long serialVersionUID = 8925451277545397036L;
Expand All @@ -11,27 +13,27 @@ public BigIntegerLimitExceeded(final long limit) {
fLimit = limit;
}

/**
* Set the exceeded limit to <code>(long)rowDimension*(long)columnDimension</code>.
*
* @param rowDimension
* @param columnDimension
*/
public BigIntegerLimitExceeded(int rowDimension, int columnDimension) {
fLimit = rowDimension * (long) columnDimension;
}

@Override
public String getMessage() {
return "BigInteger bit length " + fLimit + " exceeded";
}

/**
* Throws a new <code>BigIntegerLimitExceeded</code> exception, if the
* {@link Config#MAX_BIT_LENGTH} limit was exceeded.
*
* <p>
* Usage:
*
* <pre>
* if (((IInteger) number).bitLength() > Config.MAX_BIT_LENGTH / 100) {
* BigIntegerLimitExceeded.throwIt(Config.MAX_BIT_LENGTH / 100);
* }
* </pre>
*
* @param limit
*/
public static void throwIt(final long limit) {
// HeapContext.enter();
// try {
throw new BigIntegerLimitExceeded(limit); // .copy());
// } finally {
// HeapContext.exit();
// }
throw new BigIntegerLimitExceeded(limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10085,7 +10085,7 @@ public static IExpr intSum(final Function<IInteger, IExpr> function, final int f
number = number.plus((INumber) temp);
if (number instanceof IInteger //
&& ((IInteger) number).bitLength() > Config.MAX_BIT_LENGTH / 100) {
BigIntegerLimitExceeded.throwIt(Config.MAX_BIT_LENGTH / 100);
BigIntegerLimitExceeded.throwIt(((IInteger) number).bitLength());
}
} else {
numberOfLeaves += temp.leafCount() + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public static void initialize() {
rules = LaplaceTransformRules.RULES;
rules = LegendrePRules.RULES;
rules = LegendreQRules.RULES;
rules = LerchPhiRules.RULES;
rules = LimitRules.RULES;
rules = LogRules.RULES;
rules = MatrixDRules.RULES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DerivativeRules {
* <li>index 0 - number of equal rules in <code>RULES</code></li>
* </ul>
*/
final public static int[] SIZES = { 132, 0 };
final public static int[] SIZES = { 134, 0 };

final public static IAST RULES = List(
IInit(Derivative, SIZES),
Expand Down Expand Up @@ -439,6 +439,12 @@ public class DerivativeRules {
Times(CN1D2,Log(C2Pi)), true),
// Zeta'(-1)=1/12-Log(Glaisher)
ISet($($(Derivative(C1),Zeta),CN1),
Subtract(QQ(1L,12L),Log(Glaisher)), true)
Subtract(QQ(1L,12L),Log(Glaisher)), true),
// Derivative(1,0,0)[LerchPhi]=(LerchPhi(1,-1+#2,#3)-LerchPhi(#1,#2,#3)*#3)/#1&
ISet($(Derivative(C1,C0,C0),LerchPhi),
Function(Times(Power(Slot1,CN1),Plus(LerchPhi(C1,Plus(CN1,Slot2),Slot(C3)),Times(CN1,LerchPhi(Slot1,Slot2,Slot(C3)),Slot(C3))))), true),
// Derivative(0,0,1)[LerchPhi]=-LerchPhi(#1,1+#2,#3)*#2&
ISet($(Derivative(C0,C0,C1),LerchPhi),
Function(Times(CN1,LerchPhi(Slot1,Plus(C1,Slot2),Slot(C3)),Slot2)), true)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ public class FunctionExpandRules {
// PolyLog(2,1/2*(-1-Sqrt(5)))=(-1)*1/10*Pi^2-ArcCsch(2)^2
Set(PolyLog(C2,Times(C1D2,Subtract(CN1,CSqrt5))),
Subtract(Times(QQ(-1L,10L),Sqr(Pi)),Sqr(ArcCsch(C2)))),
// PolyLog(2,1/2-I*1/2)=-I*Catalan+Pi^2/48-(I*1/4*Pi-Log(2)/2)^2/2
Set(PolyLog(C2,CC(1L,2L,-1L,2L)),
Plus(Times(CNI,Catalan),Times(QQ(1L,48L),Sqr(Pi)),Times(CN1D2,Sqr(Plus(Times(CC(0L,1L,1L,4L),Pi),Times(CN1D2,Log(C2))))))),
// Abs(x_)^y_Integer:=x^y/;EvenQ(y)&&x∈Reals
SetDelayed(Power(Abs(x_),$p(y, Integer)),
Condition(Power(x,y),And(EvenQ(y),Element(x,Reals)))),
Expand Down

This file was deleted.

5 changes: 4 additions & 1 deletion symja_android_library/rules/DerivativeRules.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
Derivative(0,1)[StruveL] = (1/2)*(StruveL(#-1, #2) + StruveL(#+1, #2) + (#2/2)^#/(Sqrt(Pi)*Gamma(#+3/2))) &,

Derivative(1)[Zeta][0] = (-1/2)*Log(2*Pi),
Derivative(1)[Zeta][-1] = 1/12-Log(Glaisher)
Derivative(1)[Zeta][-1] = 1/12-Log(Glaisher),

Derivative(1,0,0)[LerchPhi] = (LerchPhi(1,-1+#2,#3)-LerchPhi(#,#2,#3)*#3)/# &,
Derivative(0,0,1)[LerchPhi] = (-LerchPhi(#, 1 + #2, #3))*#2 &

}
3 changes: 2 additions & 1 deletion symja_android_library/rules/FunctionExpandRules.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@
PolyLog(2,(-1+Sqrt(5))/2) = Pi^2/10 - ArcCsch(2)^2,
PolyLog(2,(1-Sqrt(5))/2) = -(Pi^2/10) + ArcCsch(2)^2 + (1/2)*(Pi^2/15 - ArcCsch(2)^2) ,
PolyLog(2,(-1-Sqrt(5))/2) = -(Pi^2/10) - ArcCsch(2)^2,

PolyLog(2,Complex(1/2,-1/2)) = -I*Catalan+Pi^2/48-(I*1/4*Pi-Log(2)/2)^2/2,

Power(Abs(x_),y_Integer) := x^y
/; EvenQ(y) && Element(x,Reals),
Power(Abs(x_),y_Integer) := With({a=Quotient(y,2)},(Im(x)^2+Re(x)^2)^a)
Expand Down
13 changes: 0 additions & 13 deletions symja_android_library/rules/LerchPhiRules.m

This file was deleted.

0 comments on commit 5b8c4bb

Please sign in to comment.