001package conexp.fx.core.math;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.util.Collection;
026
027public final class Math3 {
028
029  public static final double wsum(final double x, final double y, final double p) {
030    return p * x + (1d - p) * y;
031  }
032
033  public static final double round(final double x, final int decimalsAfterDot) {
034    final double f = Math.pow(10d, (double) decimalsAfterDot);
035    return Math.rint(f * x) / f;
036  }
037
038  public static final long factorial(final int n) {
039    return n == 0 ? 1 : n * factorial(n - 1);
040  }
041
042  public static final double modulo(final double number, final double module) {
043    double value = number;
044    while (value < 0)
045      value += module;
046    return value % module;
047  }
048
049  public static final long binomial(final int N, final int K) {
050    final long[][] binomial = new long[N + 1][K + 1];
051    for (int k = 1; k <= K; k++)
052      binomial[0][k] = 0;
053    for (int n = 0; n <= N; n++)
054      binomial[n][0] = 1;
055    for (int n = 1; n <= N; n++)
056      for (int k = 1; k <= K; k++)
057        binomial[n][k] = binomial[n - 1][k - 1] + binomial[n - 1][k];
058    return binomial[N][K];
059  }
060
061  public static double sum(Collection<? extends Number> c) {
062    double s = 0d;
063    for (Number n : c)
064      s += n.doubleValue();
065    return s;
066  }
067
068  public static double product(Collection<? extends Number> c) {
069    double s = 1d;
070    for (Number n : c)
071      s *= n.doubleValue();
072    return s;
073  }
074
075  public static final String formatMillis(final long millis) {
076    if (millis == 0)
077      return "";
078    final long ms = millis % 1000l;
079    final long s = (millis / 1000l) % 60l;
080    final long m = (millis / 60000l) % 60l;
081    final long h = (millis / 3600000l);
082    if (h > 0)
083      return String.format("%02dh %02dmin", h, m);
084    if (m > 0)
085      return String.format("%02dmin %02ds", m, s);
086    if (s > 0)
087      return String.format("%02ds %03dms", s, ms);
088    return String.format("%03dms", ms);
089  }
090
091  public static final String formatNanos(final long nanos) {
092    if (nanos == 0)
093      return "";
094    final long ns = nanos % 1000l;
095    final long µs = (nanos / 1000l) % 1000l;
096    final long ms = (nanos / 1000000l) % 1000l;
097    final long s = (nanos / 1000000000l) % 60l;
098    final long m = (nanos / 60000000000l) % 60l;
099    final long h = (nanos / 3600000000000l);
100    if (h > 0)
101      return String.format("%02dh %02dmin", h, m);
102    if (m > 0)
103      return String.format("%02dmin %02ds", m, s);
104    if (s > 0)
105      return String.format("%02ds %03dms", s, ms);
106    if (ms > 0)
107      return String.format("%03dms %03dµs", ms, µs);
108    if (µs > 0)
109      return String.format("%03dµs %03dns", µs, ns);
110    return String.format("%03dns", ns);
111  }
112}