/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program 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.1 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.eval.traintest.metrics;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Definitions of different discount functions.
 */
public final class Discounts {
    private Discounts() {}

    private static final Pattern LOG_PAT = Pattern.compile("log(?:\\((\\d+)\\))?", Pattern.CASE_INSENSITIVE);
    private static final Pattern EXP_PAT = Pattern.compile("exp\\((\\d+)\\)", Pattern.CASE_INSENSITIVE);

    /**
     * Create a log-base-2 discount.
     * @return The discount.
     */
    public static LogDiscount log2() {
        return new LogDiscount(2);
    }

    /**
     * Create a new logarithmic discount.
     * @param base The log base.
     * @return The discount.
     */
    public static LogDiscount log(double base) {
        return new LogDiscount(base);
    }

    /**
     * Create a new exponential (half-life) discount.
     * @param hl The half-life of the decay function.
     * @return The discount.
     */
    public static ExponentialDiscount exp(double hl) {
        return new ExponentialDiscount(hl);
    }

    /**
     * Parse a discount expression from a string.
     * @param disc The discount string.
     * @return The discount.
     */
    public static Discount parse(String disc) {
        if (disc.toLowerCase().equals("log2")) {
            return log2();
        }

        Matcher m = LOG_PAT.matcher(disc);
        if (m.matches()) {
            String grp = m.group(1);
            double base = grp != null ? Double.parseDouble(grp) : 2;
            return new LogDiscount(base);
        }

        m = EXP_PAT.matcher(disc);
        if (m.matches()) {
            double hl = Double.parseDouble(m.group(1));
            return new ExponentialDiscount(hl);
        }

        throw new IllegalArgumentException("invalid discount specification " + disc);
    }
}

@@ -1475,176 +1475,562 @@
 unt.
-%0A     * @return The discount.%0A     */%0A    public static LogDiscount log2() %7B%0A        return new LogDiscount(2);%0A    %7D%0A%0A    /**%0A     * Create a new logarithmic discount.
+  The discount function is:%0A     *%0A     * %5C%5C%5B%5C%5Cmathrm%7Bdisc%7D(i) =%0A     * %5C%5Cbegin%7Bcases%7D%0A     * 1 & i %5C%5Cle 2 %5C%5C%5C%5C%0A     * (%5C%5Cmathrm%7Blog%7D_%7B2%7D i)%5E%7B-1%7D & %5C%5Cmathrm%7Belse%7D%0A     * %5C%5Cend%7Bcases%7D %5C%5C%5D%0A     *%0A     * @return The discount.%0A     */%0A    public static LogDiscount log2() %7B%0A        return new LogDiscount(2);%0A    %7D%0A%0A    /**%0A     * Create a new logarithmic discount.  The discount function is:%0A     *%0A     * %5C%5C%5B%5C%5Cmathrm%7Bdisc%7D(i) =%0A     * %5C%5Cbegin%7Bcases%7D%0A     * 1 & i %5C%5Cle b %5C%5C%5C%5C%0A     * (%5C%5Cmathrm%7Blog%7D_%7Bb%7D i)%5E%7B-1%7D & %5C%5Cmathrm%7Belse%7D%0A     * %5C%5Cend%7Bcases%7D %5C%5C%5D%0A     *
 %0A   
@@ -2057,16 +2057,20 @@
 log base
+ $b$
 .%0A     *
@@ -2251,24 +2251,145 @@
 e) discount.
+  The discount function is:%0A     *%0A     * %5C%5C%5B%5C%5Cmathrm%7Bdisc%7D(i) = %5C%5Cleft(2%5E%7B%5C%5Cfrac%7Bi-1%7D%7B%5C%5Calpha-1%7D%7D%5C%5Cright)%5E%7B-1%7D%5C%5C%5D%0A     *
 %0A     * @par
@@ -2407,16 +2407,26 @@
 alf-life
+ $%5C%5Calpha$
  of the 

Document the evaluator discount formulas