/*
* MittagLefflerFunction.java
*
* Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
*
* This file is part of BEAST.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package dr.math;
import cern.jet.stat.Gamma;
/**
* @author Marc Suchard
*
* Evaluates numerical approximations to the Mittag-Leffler function E_{\alpha}(-x^{\alpha}).
* This function forms a basis for many solutions to fractional differential equations (i.e., Levy flights)
*
* Diethelm K, Ford NJ, Freed AD, Luchko Y (2005) Algorithms for the fractional calculus: a selection
* of numerical methods. Comput. Methods Appl. Mech. Engrg. 194, 742-773.
*
* In my hands, the approximation from DKKL is poor for \alpha < 0.1 in the truncated series span of x
*
*/
public class MittagLefflerFunction {
private static final double EPS = 10E-15;
private static final boolean DEBUG = false;
public MittagLefflerFunction(double alpha) {
this.alpha = alpha;
this.beta = 1;
}
public MittagLefflerFunction(double alpha, double beta) {
this.alpha = alpha;
this.beta = beta;
}
public static void main(String arg[]) {
System.out.println("Comparing power-series expansion to Pade approximation of");
System.out.println("E_{\\alpha}(-x^{\\alpha})");
int lenAlpha = 20;
int lenX = 1000;
double startAlpha = 0.05;
double endAlpha = 0.999;
double startX = 0.001;
double endX = 20;
double incAlpha = (endAlpha - startAlpha) / (lenAlpha);
double incX = (endX - startX) / (lenX);
double[][] padeResults = new double[lenAlpha][lenX];
double[][] powsResults = new double[lenAlpha][lenX];
long start, end;
double alpha;
double x;
start = System.nanoTime();
alpha = startAlpha;
for(int i=0; i 1E-1) {
System.err.println("Big difference at ("+i+","+j+") = "+delta);
}
}
}
MSE /= (lenAlpha*lenX);
System.out.println("MSE = "+MSE);
}
public double evaluate(double x) {
return evaluatePowerSeries(x, alpha, beta);
}
public static double evaluatePowerSeries(double x, double alpha, double beta) {
return evaluatePowerSeries(x, alpha, beta, EPS);
}
public static double evaluatePowerSeries(double x, double alpha) {
return evaluatePowerSeries(x,alpha,1.0,EPS);
}
public static double evaluatePowerSeries(double x, double alpha, double beta, double eps) {
final double z = -Math.pow(x,alpha);
double E = 0;
double summand = 1;
int k = 0;
double XpowK = 1;
while (Math.abs(summand) >= eps) {
summand = XpowK / gamma(beta + alpha * k);
XpowK *= z;
k++;
E += summand;
}
if (DEBUG) {
System.err.println("Loops = " + k);
}
return E;
}
public static double evaluatePade(double x, double alpha) {
return evaluatePade(x, 0, alpha);
}
public static double evaluatePade(double x, double y, double alpha) {
if (alpha < 0.01 || alpha > 1) {
System.err.println("alpha = "+alpha);
throw new RuntimeException("Mittag-Leffler function only implemented for 0.01 <= alpha <= 1");
}
if (y != 0) {
throw new RuntimeException("Mittag-Leffler function only implemented for real arguments");
}
double E;
if (alpha == 1) { // Returns standard exponential function
return Math.exp(-Math.pow(x, alpha));
}
if (x < 0) {
throw new RuntimeException("Mittag-Leffler function only defined for positive quantities");
} else if (x <= 0.1) {
E = 0;
final double z = -Math.pow(x, alpha);
for (int k = 0; k <= 4; k++) {
E += Math.pow(z, k) / gamma(1 + alpha * k);
}
} else if (x < 15) {
// Do a linear interpolation between grid pts.
// Pade approximation grid pts { 0.01, 0.02, ..., 0.99 }
final double rescaledAlpha = alpha * 100.0;
final int end = (int) rescaledAlpha;
final int start = end - 1;
double delta = rescaledAlpha - end;
E = padeApproximation(x, start);
if (delta > 0) {
E *= (1 - delta);
if (end < 99) { // Only 98 values.
E += padeApproximation(x, end) * delta;
} else {
E += Math.exp(x) * delta;
}
}
} else {
E = 0;
final double z = -Math.pow(x, -alpha);
for (int k = 1; k <= 4; k++) {
E -= Math.pow(z, k) / gamma(1 - alpha * k);
}
}
return E;
}
private static double gamma(double z) {
// To extend the gamma function to negative (non-integer) numbers, apply the relationship
// \Gamma(z) = \frac{ \Gamma(z+n) }{ z(z+1)\cdots(z+n-1),
// by choosing n such that z+n is positive
if (z > 0) {
return Gamma.gamma(z);
}
int n = ((int) -z) + 1;
double factor = 1;
for(int i=0; i