Merge pull request #958 from nosqlbench/nosqlbench-957-statistics

Replace embedded commons3 statistics with commons-statistics 1.0
This commit is contained in:
Jonathan Shook 2023-01-27 14:11:10 -06:00 committed by GitHub
commit 8d02d7829e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
675 changed files with 215 additions and 128792 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,7 @@ package io.nosqlbench.cqlgen.transformers;
import io.nosqlbench.cqlgen.core.CGKeyspaceStats; import io.nosqlbench.cqlgen.core.CGKeyspaceStats;
import io.nosqlbench.cqlgen.core.CGSchemaStats; import io.nosqlbench.cqlgen.core.CGSchemaStats;
import io.nosqlbench.cqlgen.core.CGTableStats; import io.nosqlbench.cqlgen.core.CGTableStats;
import org.apache.commons.math4.util.Pair; import org.apache.commons.math4.legacy.core.Pair;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,22 @@ import static org.assertj.core.api.Assertions.assertThat;
public class GraalJsEvaluatorTest { public class GraalJsEvaluatorTest {
// TODO:
/**
* This evaluator may benefit from being compiled. Presently it give a warning:
* <pre>{@code
* [To redirect Truffle log output to a file use one of the following options:
* * '--log.file=<path>' if the option is passed using a guest language launcher.
* * '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher.
* * Configure logging using the polyglot embedding API.]
* [engine] WARNING: The polyglot context is using an implementation that does not support runtime compilation.
* The guest application code will therefore be executed in interpreted mode only.
* Execution only in interpreted mode will strongly impact the guest application performance.
* For more information on using GraalVM see https://www.graalvm.org/java/quickstart/.
* To disable this warning the '--engine.WarnInterpreterOnly=false' option or use the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property.
* }</pre>
*/
@Test @Test
public void testBasicOperations() { public void testBasicOperations() {
GraalJsEvaluator<Long> ne = new GraalJsEvaluator<>(Long.class); GraalJsEvaluator<Long> ne = new GraalJsEvaluator<>(Long.class);

View File

@ -134,6 +134,32 @@
<version>1.10.0</version> <version>1.10.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-core</artifactId>
<version>4.0-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy</artifactId>
<version>4.0-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy-core</artifactId>
<version>4.0-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy-exception</artifactId>
<version>4.0-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-statistics-distribution</artifactId>
<version>1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId> <artifactId>jmh-core</artifactId>
@ -295,12 +321,6 @@
<version>2.12.2</version> <version>2.12.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId> <artifactId>commons-csv</artifactId>

View File

@ -60,6 +60,28 @@
<artifactId>javapoet</artifactId> <artifactId>javapoet</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math4-legacy-exception</artifactId>
<version>4.0-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-statistics-distribution</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -63,10 +63,6 @@
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.elega9t</groupId> <groupId>com.elega9t</groupId>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.BetaDistribution;
@ThreadSafeMapper @ThreadSafeMapper
public class Beta extends IntToDoubleContinuousCurve { public class Beta extends IntToDoubleContinuousCurve {
public Beta(double alpha, double beta, String... mods) { public Beta(double alpha, double beta, String... mods) {
super(new BetaDistribution(alpha, beta), mods); super(BetaDistribution.of(alpha, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.CauchyDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Cauchy extends IntToDoubleContinuousCurve { public class Cauchy extends IntToDoubleContinuousCurve {
public Cauchy(double median, double scale, String... mods) { public Cauchy(double median, double scale, String... mods) {
super(new CauchyDistribution(median, scale), mods); super(CauchyDistribution.of(median, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.ChiSquaredDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class ChiSquared extends IntToDoubleContinuousCurve { public class ChiSquared extends IntToDoubleContinuousCurve {
public ChiSquared(double degreesOfFreedom, String... mods) { public ChiSquared(double degreesOfFreedom, String... mods) {
super(new ChiSquaredDistribution(degreesOfFreedom), mods); super(ChiSquaredDistribution.of(degreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,7 @@ package io.nosqlbench.virtdata.library.curves4.continuous.int_double;
import io.nosqlbench.virtdata.api.annotations.Categories; import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.apache.commons.statistics.distribution.ConstantContinuousDistribution; import org.apache.commons.math4.legacy.distribution.EmpiricalDistribution;
/** /**
* Always yields the same value. * Always yields the same value.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.ConstantContinuousDistribution
@Categories({Category.distributions}) @Categories({Category.distributions})
public class ConstantContinuous extends IntToDoubleContinuousCurve { public class ConstantContinuous extends IntToDoubleContinuousCurve {
public ConstantContinuous(double value, String... mods) { public ConstantContinuous(double value, String... mods) {
super(new ConstantContinuousDistribution(value), mods); super(EmpiricalDistribution.from(1, new double[]{value}), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,7 @@ import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example; import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.apache.commons.math4.distribution.EnumeratedRealDistribution; import org.apache.commons.math4.legacy.distribution.EnumeratedRealDistribution;
/** /**
* Creates a probability density given the values and optional weights provided, in "value:weight value:weight ..." form. * Creates a probability density given the values and optional weights provided, in "value:weight value:weight ..." form.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.ExponentialDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Exponential extends IntToDoubleContinuousCurve { public class Exponential extends IntToDoubleContinuousCurve {
public Exponential(double mean, String... mods) { public Exponential(double mean, String... mods) {
super(new ExponentialDistribution(mean), mods); super(ExponentialDistribution.of(mean), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,6 @@ import org.apache.commons.statistics.distribution.FDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class F extends IntToDoubleContinuousCurve { public class F extends IntToDoubleContinuousCurve {
public F(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, String... mods) { public F(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, String... mods) {
super(new FDistribution(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom), mods); super(FDistribution.of(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.GammaDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Gamma extends IntToDoubleContinuousCurve { public class Gamma extends IntToDoubleContinuousCurve {
public Gamma(double shape, double scale, String... mods) { public Gamma(double shape, double scale, String... mods) {
super(new GammaDistribution(shape, scale), mods); super(GammaDistribution.of(shape, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.GumbelDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Gumbel extends IntToDoubleContinuousCurve { public class Gumbel extends IntToDoubleContinuousCurve {
public Gumbel(double mu, double beta, String... mods) { public Gumbel(double mu, double beta, String... mods) {
super(new GumbelDistribution(mu, beta), mods); super(GumbelDistribution.of(mu, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.LaplaceDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Laplace extends IntToDoubleContinuousCurve { public class Laplace extends IntToDoubleContinuousCurve {
public Laplace(double mu, double beta, String... mods) { public Laplace(double mu, double beta, String... mods) {
super(new LaplaceDistribution(mu, beta), mods); super(LaplaceDistribution.of(mu, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,6 @@ import org.apache.commons.statistics.distribution.LevyDistribution;
*/ */
@ThreadSafeMapper @ThreadSafeMapper
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Levy extends IntToDoubleContinuousCurve { public Levy(double mu, double c, String... mods) { super(new LevyDistribution(mu,c), mods); public class Levy extends IntToDoubleContinuousCurve { public Levy(double mu, double c, String... mods) { super(LevyDistribution.of(mu,c), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.LogNormalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class LogNormal extends IntToDoubleContinuousCurve { public class LogNormal extends IntToDoubleContinuousCurve {
public LogNormal(double scale, double shape, String... mods) { public LogNormal(double scale, double shape, String... mods) {
super(new LogNormalDistribution(scale, shape), mods); super(LogNormalDistribution.of(scale, shape), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.LogisticDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Logistic extends IntToDoubleContinuousCurve { public class Logistic extends IntToDoubleContinuousCurve {
public Logistic(double mu, double scale, String... mods) { public Logistic(double mu, double scale, String... mods) {
super(new LogisticDistribution(mu, scale), mods); super(LogisticDistribution.of(mu, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.NakagamiDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Nakagami extends IntToDoubleContinuousCurve { public class Nakagami extends IntToDoubleContinuousCurve {
public Nakagami(double mu, double omega, String... mods) { public Nakagami(double mu, double omega, String... mods) {
super(new NakagamiDistribution(mu, omega), mods); super(NakagamiDistribution.of(mu, omega), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.NormalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Normal extends IntToDoubleContinuousCurve { public class Normal extends IntToDoubleContinuousCurve {
public Normal(double mean, double sd, String... mods) { public Normal(double mean, double sd, String... mods) {
super(new NormalDistribution(mean, sd), mods); super(NormalDistribution.of(mean, sd), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.ParetoDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pareto extends IntToDoubleContinuousCurve { public class Pareto extends IntToDoubleContinuousCurve {
public Pareto(double scale, double shape, String... mods) { public Pareto(double scale, double shape, String... mods) {
super(new ParetoDistribution(scale, shape), mods); super(ParetoDistribution.of(scale, shape), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.TDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class T extends IntToDoubleContinuousCurve { public class T extends IntToDoubleContinuousCurve {
public T(double degreesOfFreedom, String... mods) { public T(double degreesOfFreedom, String... mods) {
super(new TDistribution(degreesOfFreedom), mods); super(TDistribution.of(degreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.TriangularDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Triangular extends IntToDoubleContinuousCurve { public class Triangular extends IntToDoubleContinuousCurve {
public Triangular(double a, double c, double b, String... mods) { public Triangular(double a, double c, double b, String... mods) {
super(new TriangularDistribution(a,c,b), mods); super(TriangularDistribution.of(a,c,b), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends IntToDoubleContinuousCurve { public class Uniform extends IntToDoubleContinuousCurve {
public Uniform(double lower, double upper, String... mods) { public Uniform(double lower, double upper, String... mods) {
super(new UniformContinuousDistribution(lower, upper), mods); super(UniformContinuousDistribution.of(lower, upper), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,6 @@ import org.apache.commons.statistics.distribution.WeibullDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Weibull extends IntToDoubleContinuousCurve { public class Weibull extends IntToDoubleContinuousCurve {
public Weibull(double alpha, double beta, String... mods) { public Weibull(double alpha, double beta, String... mods) {
super(new WeibullDistribution(alpha, beta), mods); super(WeibullDistribution.of(alpha, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.BetaDistribution;
@ThreadSafeMapper @ThreadSafeMapper
public class Beta extends LongToDoubleContinuousCurve { public class Beta extends LongToDoubleContinuousCurve {
public Beta(double alpha, double beta, String... mods) { public Beta(double alpha, double beta, String... mods) {
super(new BetaDistribution(alpha, beta), mods); super(BetaDistribution.of(alpha, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.CauchyDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Cauchy extends LongToDoubleContinuousCurve { public class Cauchy extends LongToDoubleContinuousCurve {
public Cauchy(double median, double scale, String... mods) { public Cauchy(double median, double scale, String... mods) {
super(new CauchyDistribution(median, scale), mods); super(CauchyDistribution.of(median, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ChiSquaredDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class ChiSquared extends LongToDoubleContinuousCurve { public class ChiSquared extends LongToDoubleContinuousCurve {
public ChiSquared(double degreesOfFreedom, String... mods) { public ChiSquared(double degreesOfFreedom, String... mods) {
super(new ChiSquaredDistribution(degreesOfFreedom), mods); super(ChiSquaredDistribution.of(degreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,12 +19,12 @@ package io.nosqlbench.virtdata.library.curves4.continuous.long_double;
import io.nosqlbench.virtdata.api.annotations.Categories; import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.apache.commons.statistics.distribution.ConstantContinuousDistribution; import org.apache.commons.math4.legacy.distribution.EmpiricalDistribution;
@ThreadSafeMapper @ThreadSafeMapper
@Categories({Category.distributions}) @Categories({Category.distributions})
public class ConstantContinuous extends LongToDoubleContinuousCurve { public class ConstantContinuous extends LongToDoubleContinuousCurve {
public ConstantContinuous(double value, String... mods) { public ConstantContinuous(double value, String... mods) {
super(new ConstantContinuousDistribution(value), mods); super(EmpiricalDistribution.from(1,new double[]{value}), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,7 @@ import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example; import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.apache.commons.math4.distribution.EnumeratedRealDistribution; import org.apache.commons.math4.legacy.distribution.EnumeratedRealDistribution;
@ThreadSafeMapper @ThreadSafeMapper
@Categories({Category.distributions}) @Categories({Category.distributions})

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ExponentialDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Exponential extends LongToDoubleContinuousCurve { public class Exponential extends LongToDoubleContinuousCurve {
public Exponential(double mean, String... mods) { public Exponential(double mean, String... mods) {
super(new ExponentialDistribution(mean), mods); super(ExponentialDistribution.of(mean), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.FDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class F extends LongToDoubleContinuousCurve { public class F extends LongToDoubleContinuousCurve {
public F(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, String... mods) { public F(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, String... mods) {
super(new FDistribution(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom), mods); super(FDistribution.of(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.GammaDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Gamma extends LongToDoubleContinuousCurve { public class Gamma extends LongToDoubleContinuousCurve {
public Gamma(double shape, double scale, String... mods) { public Gamma(double shape, double scale, String... mods) {
super(new GammaDistribution(shape, scale), mods); super(GammaDistribution.of(shape, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,10 +21,11 @@ import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.apache.commons.statistics.distribution.GumbelDistribution; import org.apache.commons.statistics.distribution.GumbelDistribution;
@ThreadSafeMapper @ThreadSafeMapper
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Gumbel extends LongToDoubleContinuousCurve { public class Gumbel extends LongToDoubleContinuousCurve {
public Gumbel(double mu, double beta, String... mods) { public Gumbel(double mu, double beta, String... mods) {
super(new GumbelDistribution(mu, beta), mods); super(GumbelDistribution.of(mu, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.LaplaceDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Laplace extends LongToDoubleContinuousCurve { public class Laplace extends LongToDoubleContinuousCurve {
public Laplace(double mu, double beta, String... mods) { public Laplace(double mu, double beta, String... mods) {
super(new LaplaceDistribution(mu, beta), mods); super(LaplaceDistribution.of(mu, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,6 @@ import org.apache.commons.statistics.distribution.LevyDistribution;
@ThreadSafeMapper @ThreadSafeMapper
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Levy extends LongToDoubleContinuousCurve { public Levy(double mu, double c, String... mods) { super(new LevyDistribution(mu,c), mods); public class Levy extends LongToDoubleContinuousCurve { public Levy(double mu, double c, String... mods) { super(LevyDistribution.of(mu,c), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.LogNormalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class LogNormal extends LongToDoubleContinuousCurve { public class LogNormal extends LongToDoubleContinuousCurve {
public LogNormal(double scale, double shape, String... mods) { public LogNormal(double scale, double shape, String... mods) {
super(new LogNormalDistribution(scale, shape), mods); super(LogNormalDistribution.of(scale, shape), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.LogisticDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Logistic extends LongToDoubleContinuousCurve { public class Logistic extends LongToDoubleContinuousCurve {
public Logistic(double mu, double scale, String... mods) { public Logistic(double mu, double scale, String... mods) {
super(new LogisticDistribution(mu, scale), mods); super(LogisticDistribution.of(mu, scale), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.NakagamiDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Nakagami extends LongToDoubleContinuousCurve { public class Nakagami extends LongToDoubleContinuousCurve {
public Nakagami(double mu, double omega, String... mods) { public Nakagami(double mu, double omega, String... mods) {
super(new NakagamiDistribution(mu, omega), mods); super(NakagamiDistribution.of(mu, omega), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.NormalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Normal extends LongToDoubleContinuousCurve { public class Normal extends LongToDoubleContinuousCurve {
public Normal(double mean, double sd, String... mods) { public Normal(double mean, double sd, String... mods) {
super(new NormalDistribution(mean, sd), mods); super(NormalDistribution.of(mean, sd), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ParetoDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pareto extends LongToDoubleContinuousCurve { public class Pareto extends LongToDoubleContinuousCurve {
public Pareto(double scale, double shape, String... mods) { public Pareto(double scale, double shape, String... mods) {
super(new ParetoDistribution(scale, shape), mods); super(ParetoDistribution.of(scale, shape), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.TDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class T extends LongToDoubleContinuousCurve { public class T extends LongToDoubleContinuousCurve {
public T(double degreesOfFreedom, String... mods) { public T(double degreesOfFreedom, String... mods) {
super(new TDistribution(degreesOfFreedom), mods); super(TDistribution.of(degreesOfFreedom), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.TriangularDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Triangular extends LongToDoubleContinuousCurve { public class Triangular extends LongToDoubleContinuousCurve {
public Triangular(double a, double c, double b, String... mods) { public Triangular(double a, double c, double b, String... mods) {
super(new TriangularDistribution(a,c,b), mods); super(TriangularDistribution.of(a,c,b), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends LongToDoubleContinuousCurve { public class Uniform extends LongToDoubleContinuousCurve {
public Uniform(double lower, double upper, String... mods) { public Uniform(double lower, double upper, String... mods) {
super(new UniformContinuousDistribution(lower, upper), mods); super(UniformContinuousDistribution.of(lower, upper), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.WeibullDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Weibull extends LongToDoubleContinuousCurve { public class Weibull extends LongToDoubleContinuousCurve {
public Weibull(double alpha, double beta, String... mods) { public Weibull(double alpha, double beta, String... mods) {
super(new WeibullDistribution(alpha, beta), mods); super(WeibullDistribution.of(alpha, beta), mods);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.BinomialDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Binomial extends IntToIntDiscreteCurve { public class Binomial extends IntToIntDiscreteCurve {
public Binomial(int trials, double p, String... modslist) { public Binomial(int trials, double p, String... modslist) {
super(new BinomialDistribution(trials, p), modslist); super(BinomialDistribution.of(trials, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.GeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Geometric extends IntToIntDiscreteCurve { public class Geometric extends IntToIntDiscreteCurve {
public Geometric(double p, String... modslist) { public Geometric(double p, String... modslist) {
super(new GeometricDistribution(p), modslist); super(GeometricDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.HypergeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Hypergeometric extends IntToIntDiscreteCurve { public class Hypergeometric extends IntToIntDiscreteCurve {
public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) { public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) {
super(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize), modslist); super(HypergeometricDistribution.of(populationSize, numberOfSuccesses, sampleSize), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.PascalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pascal extends IntToIntDiscreteCurve { public class Pascal extends IntToIntDiscreteCurve {
public Pascal(int r, double p, String... modslist) { public Pascal(int r, double p, String... modslist) {
super(new PascalDistribution(r, p), modslist); super(PascalDistribution.of(r, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.PoissonDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Poisson extends IntToIntDiscreteCurve { public class Poisson extends IntToIntDiscreteCurve {
public Poisson(double p, String... modslist) { public Poisson(double p, String... modslist) {
super(new PoissonDistribution(p), modslist); super(PoissonDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends IntToIntDiscreteCurve { public class Uniform extends IntToIntDiscreteCurve {
public Uniform(int lower, int upper, String... modslist) { public Uniform(int lower, int upper, String... modslist) {
super(new UniformDiscreteDistribution(lower, upper), modslist); super(UniformDiscreteDistribution.of(lower, upper), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,6 @@ import org.apache.commons.statistics.distribution.ZipfDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Zipf extends IntToIntDiscreteCurve { public class Zipf extends IntToIntDiscreteCurve {
public Zipf(int numberOfElements, double exponent, String... modslist) { public Zipf(int numberOfElements, double exponent, String... modslist) {
super(new ZipfDistribution(numberOfElements, exponent), modslist); super(ZipfDistribution.of(numberOfElements, exponent), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.BinomialDistribution;
@ThreadSafeMapper @ThreadSafeMapper
public class Binomial extends IntToLongDiscreteCurve { public class Binomial extends IntToLongDiscreteCurve {
public Binomial(int trials, double p, String... modslist) { public Binomial(int trials, double p, String... modslist) {
super(new BinomialDistribution(trials, p), modslist); super(BinomialDistribution.of(trials, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.GeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Geometric extends IntToLongDiscreteCurve { public class Geometric extends IntToLongDiscreteCurve {
public Geometric(double p, String... modslist) { public Geometric(double p, String... modslist) {
super(new GeometricDistribution(p), modslist); super(GeometricDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.HypergeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Hypergeometric extends IntToLongDiscreteCurve { public class Hypergeometric extends IntToLongDiscreteCurve {
public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) { public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) {
super(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize), modslist); super(HypergeometricDistribution.of(populationSize, numberOfSuccesses, sampleSize), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PascalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pascal extends IntToLongDiscreteCurve { public class Pascal extends IntToLongDiscreteCurve {
public Pascal(int r, double p, String... modslist) { public Pascal(int r, double p, String... modslist) {
super(new PascalDistribution(r, p), modslist); super(PascalDistribution.of(r, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PoissonDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Poisson extends IntToLongDiscreteCurve { public class Poisson extends IntToLongDiscreteCurve {
public Poisson(double p, String... modslist) { public Poisson(double p, String... modslist) {
super(new PoissonDistribution(p), modslist); super(PoissonDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends IntToLongDiscreteCurve { public class Uniform extends IntToLongDiscreteCurve {
public Uniform(int lower, int upper, String... modslist) { public Uniform(int lower, int upper, String... modslist) {
super(new UniformDiscreteDistribution(lower, upper), modslist); super(UniformDiscreteDistribution.of(lower, upper), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ZipfDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Zipf extends IntToLongDiscreteCurve { public class Zipf extends IntToLongDiscreteCurve {
public Zipf(int numberOfElements, double exponent, String... modslist) { public Zipf(int numberOfElements, double exponent, String... modslist) {
super(new ZipfDistribution(numberOfElements, exponent), modslist); super(ZipfDistribution.of(numberOfElements, exponent), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.BinomialDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Binomial extends LongToIntDiscreteCurve { public class Binomial extends LongToIntDiscreteCurve {
public Binomial(int trials, double p, String... modslist) { public Binomial(int trials, double p, String... modslist) {
super(new BinomialDistribution(trials, p), modslist); super(BinomialDistribution.of(trials, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.GeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Geometric extends LongToIntDiscreteCurve { public class Geometric extends LongToIntDiscreteCurve {
public Geometric(double p, String... modslist) { public Geometric(double p, String... modslist) {
super(new GeometricDistribution(p), modslist); super(GeometricDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.HypergeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Hypergeometric extends LongToIntDiscreteCurve { public class Hypergeometric extends LongToIntDiscreteCurve {
public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) { public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) {
super(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize), modslist); super(HypergeometricDistribution.of(populationSize, numberOfSuccesses, sampleSize), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PascalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pascal extends LongToIntDiscreteCurve { public class Pascal extends LongToIntDiscreteCurve {
public Pascal(int r, double p, String... modslist) { public Pascal(int r, double p, String... modslist) {
super(new PascalDistribution(r, p), modslist); super(PascalDistribution.of(r, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PoissonDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Poisson extends LongToIntDiscreteCurve { public class Poisson extends LongToIntDiscreteCurve {
public Poisson(double p, String... modslist) { public Poisson(double p, String... modslist) {
super(new PoissonDistribution(p), modslist); super(PoissonDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends LongToIntDiscreteCurve { public class Uniform extends LongToIntDiscreteCurve {
public Uniform(int lower, int upper, String... modslist) { public Uniform(int lower, int upper, String... modslist) {
super(new UniformDiscreteDistribution(lower, upper), modslist); super(UniformDiscreteDistribution.of(lower, upper), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ZipfDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Zipf extends LongToIntDiscreteCurve { public class Zipf extends LongToIntDiscreteCurve {
public Zipf(int numberOfElements, double exponent, String... modslist) { public Zipf(int numberOfElements, double exponent, String... modslist) {
super(new ZipfDistribution(numberOfElements, exponent), modslist); super(ZipfDistribution.of(numberOfElements, exponent), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.BinomialDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Binomial extends LongToLongDiscreteCurve { public class Binomial extends LongToLongDiscreteCurve {
public Binomial(int trials, double p, String... modslist) { public Binomial(int trials, double p, String... modslist) {
super(new BinomialDistribution(trials, p), modslist); super(BinomialDistribution.of(trials, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.GeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Geometric extends LongToLongDiscreteCurve { public class Geometric extends LongToLongDiscreteCurve {
public Geometric(double p, String... modslist) { public Geometric(double p, String... modslist) {
super(new GeometricDistribution(p), modslist); super(GeometricDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.HypergeometricDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Hypergeometric extends LongToLongDiscreteCurve { public class Hypergeometric extends LongToLongDiscreteCurve {
public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) { public Hypergeometric(int populationSize, int numberOfSuccesses, int sampleSize, String... modslist) {
super(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize), modslist); super(HypergeometricDistribution.of(populationSize, numberOfSuccesses, sampleSize), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PascalDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Pascal extends LongToLongDiscreteCurve { public class Pascal extends LongToLongDiscreteCurve {
public Pascal(int r, double p, String... modslist) { public Pascal(int r, double p, String... modslist) {
super(new PascalDistribution(r, p), modslist); super(PascalDistribution.of(r, p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.PoissonDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Poisson extends LongToLongDiscreteCurve { public class Poisson extends LongToLongDiscreteCurve {
public Poisson(double p, String... modslist) { public Poisson(double p, String... modslist) {
super(new PoissonDistribution(p), modslist); super(PoissonDistribution.of(p), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Uniform extends LongToLongDiscreteCurve { public class Uniform extends LongToLongDiscreteCurve {
public Uniform(int lower, int upper, String... modslist) { public Uniform(int lower, int upper, String... modslist) {
super(new UniformDiscreteDistribution(lower, upper), modslist); super(UniformDiscreteDistribution.of(lower, upper), modslist);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,6 @@ import org.apache.commons.statistics.distribution.ZipfDistribution;
@Categories({Category.distributions}) @Categories({Category.distributions})
public class Zipf extends LongToLongDiscreteCurve { public class Zipf extends LongToLongDiscreteCurve {
public Zipf(int numberOfElements, double exponent, String... modslist) { public Zipf(int numberOfElements, double exponent, String... modslist) {
super(new ZipfDistribution(numberOfElements, exponent), modslist); super(ZipfDistribution.of(numberOfElements, exponent), modslist);
} }
} }

View File

@ -1,58 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4;
/**
* Interface representing a <a href="http://mathworld.wolfram.com/Field.html">field</a>.
* <p>
* Classes implementing this interface will often be singletons.
* </p>
* @param <T> the type of the field elements
* @see FieldElement
* @since 2.0
*/
public interface Field<T> {
/** Get the additive identity of the field.
* <p>
* The additive identity is the element e<sub>0</sub> of the field such that
* for all elements a of the field, the equalities a + e<sub>0</sub> =
* e<sub>0</sub> + a = a hold.
* </p>
* @return additive identity of the field
*/
T getZero();
/** Get the multiplicative identity of the field.
* <p>
* The multiplicative identity is the element e<sub>1</sub> of the field such that
* for all elements a of the field, the equalities a &times; e<sub>1</sub> =
* e<sub>1</sub> &times; a = a hold.
* </p>
* @return multiplicative identity of the field
*/
T getOne();
/**
* Returns the runtime class of the FieldElement.
*
* @return The {@code Class} object that represents the runtime
* class of this object.
*/
Class<? extends FieldElement<T>> getRuntimeClass();
}

View File

@ -1,85 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4;
import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.NullArgumentException;
/**
* Interface representing <a href="http://mathworld.wolfram.com/Field.html">field</a> elements.
* @param <T> the type of the field elements
* @see Field
* @since 2.0
*/
public interface FieldElement<T> {
/** Compute this + a.
* @param a element to add
* @return a new element representing this + a
* @throws NullArgumentException if {@code a} is {@code null}.
*/
T add(T a) throws NullArgumentException;
/** Compute this - a.
* @param a element to subtract
* @return a new element representing this - a
* @throws NullArgumentException if {@code a} is {@code null}.
*/
T subtract(T a) throws NullArgumentException;
/**
* Returns the additive inverse of {@code this} element.
* @return the opposite of {@code this}.
*/
T negate();
/** Compute n &times; this. Multiplication by an integer number is defined
* as the following sum
* n &times; this = &sum;<sub>i=1</sub><sup>n</sup> this.
* @param n Number of times {@code this} must be added to itself.
* @return A new element representing n &times; this.
*/
T multiply(int n);
/** Compute this &times; a.
* @param a element to multiply
* @return a new element representing this &times; a
* @throws NullArgumentException if {@code a} is {@code null}.
*/
T multiply(T a) throws NullArgumentException;
/** Compute this &divide; a.
* @param a element to divide by
* @return a new element representing this &divide; a
* @throws NullArgumentException if {@code a} is {@code null}.
* @throws MathArithmeticException if {@code a} is zero
*/
T divide(T a) throws NullArgumentException, MathArithmeticException;
/**
* Returns the multiplicative inverse of {@code this} element.
* @return the inverse of {@code this}.
* @throws MathArithmeticException if {@code this} is zero
*/
T reciprocal() throws MathArithmeticException;
/** Get the {@link Field} to which the instance belongs.
* @return {@link Field} to which the instance belongs
*/
Field<T> getField();
}

View File

@ -1,402 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4;
import org.apache.commons.math4.exception.DimensionMismatchException;
/**
* Interface representing a <a href="http://mathworld.wolfram.com/RealNumber.html">real</a>
* <a href="http://mathworld.wolfram.com/Field.html">field</a>.
* @param <T> the type of the field elements
* @see FieldElement
* @since 3.2
*/
public interface RealFieldElement<T> extends FieldElement<T> {
/** Get the real value of the number.
* @return real value
*/
double getReal();
/** '+' operator.
* @param a right hand side parameter of the operator
* @return this+a
*/
T add(double a);
/** '-' operator.
* @param a right hand side parameter of the operator
* @return this-a
*/
T subtract(double a);
/** '&times;' operator.
* @param a right hand side parameter of the operator
* @return this&times;a
*/
T multiply(double a);
/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;a
*/
T divide(double a);
/** IEEE remainder operator.
* @param a right hand side parameter of the operator
* @return this - n &times; a where n is the closest integer to this/a
* (the even integer is chosen for n if this/a is halfway between two integers)
*/
T remainder(double a);
/** IEEE remainder operator.
* @param a right hand side parameter of the operator
* @return this - n &times; a where n is the closest integer to this/a
* (the even integer is chosen for n if this/a is halfway between two integers)
* @exception DimensionMismatchException if number of free parameters or orders are inconsistent
*/
T remainder(T a)
throws DimensionMismatchException;
/** absolute value.
* @return abs(this)
*/
T abs();
/** Get the smallest whole number larger than instance.
* @return ceil(this)
*/
T ceil();
/** Get the largest whole number smaller than instance.
* @return floor(this)
*/
T floor();
/** Get the whole number that is the nearest to the instance, or the even one if x is exactly half way between two integers.
* @return a double number r such that r is an integer r - 0.5 &le; this &le; r + 0.5
*/
T rint();
/** Get the closest long to instance value.
* @return closest long to {@link #getReal()}
*/
long round();
/** Compute the signum of the instance.
* The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
* @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
*/
T signum();
/**
* Returns the instance with the sign of the argument.
* A NaN {@code sign} argument is treated as positive.
*
* @param sign the sign for the returned value
* @return the instance with the same sign as the {@code sign} argument
*/
T copySign(T sign);
/**
* Returns the instance with the sign of the argument.
* A NaN {@code sign} argument is treated as positive.
*
* @param sign the sign for the returned value
* @return the instance with the same sign as the {@code sign} argument
*/
T copySign(double sign);
/**
* Multiply the instance by a power of 2.
* @param n power of 2
* @return this &times; 2<sup>n</sup>
*/
T scalb(int n);
/**
* Returns the hypotenuse of a triangle with sides {@code this} and {@code y}
* - sqrt(<i>this</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
* avoiding intermediate overflow or underflow.
*
* <ul>
* <li> If either argument is infinite, then the result is positive infinity.</li>
* <li> else, if either argument is NaN then the result is NaN.</li>
* </ul>
*
* @param y a value
* @return sqrt(<i>this</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
* @exception DimensionMismatchException if number of free parameters or orders are inconsistent
*/
T hypot(T y)
throws DimensionMismatchException;
/** {@inheritDoc} */
@Override
T reciprocal();
/** Square root.
* @return square root of the instance
*/
T sqrt();
/** Cubic root.
* @return cubic root of the instance
*/
T cbrt();
/** N<sup>th</sup> root.
* @param n order of the root
* @return n<sup>th</sup> root of the instance
*/
T rootN(int n);
/** Power operation.
* @param p power to apply
* @return this<sup>p</sup>
*/
T pow(double p);
/** Integer power operation.
* @param n power to apply
* @return this<sup>n</sup>
*/
T pow(int n);
/** Power operation.
* @param e exponent
* @return this<sup>e</sup>
* @exception DimensionMismatchException if number of free parameters or orders are inconsistent
*/
T pow(T e)
throws DimensionMismatchException;
/** Exponential.
* @return exponential of the instance
*/
T exp();
/** Exponential minus 1.
* @return exponential minus one of the instance
*/
T expm1();
/** Natural logarithm.
* @return logarithm of the instance
*/
T log();
/** Shifted natural logarithm.
* @return logarithm of one plus the instance
*/
T log1p();
/** Base 10 logarithm.
* @return base 10 logarithm of the instance
* @since 4.0
*/
T log10();
/** Cosine operation.
* @return cos(this)
*/
T cos();
/** Sine operation.
* @return sin(this)
*/
T sin();
/** Tangent operation.
* @return tan(this)
*/
T tan();
/** Arc cosine operation.
* @return acos(this)
*/
T acos();
/** Arc sine operation.
* @return asin(this)
*/
T asin();
/** Arc tangent operation.
* @return atan(this)
*/
T atan();
/** Two arguments arc tangent operation.
* @param x second argument of the arc tangent
* @return atan2(this, x)
* @exception DimensionMismatchException if number of free parameters or orders are inconsistent
*/
T atan2(T x)
throws DimensionMismatchException;
/** Hyperbolic cosine operation.
* @return cosh(this)
*/
T cosh();
/** Hyperbolic sine operation.
* @return sinh(this)
*/
T sinh();
/** Hyperbolic tangent operation.
* @return tanh(this)
*/
T tanh();
/** Inverse hyperbolic cosine operation.
* @return acosh(this)
*/
T acosh();
/** Inverse hyperbolic sine operation.
* @return asin(this)
*/
T asinh();
/** Inverse hyperbolic tangent operation.
* @return atanh(this)
*/
T atanh();
/**
* Compute a linear combination.
* @param a Factors.
* @param b Factors.
* @return <code>&Sigma;<sub>i</sub> a<sub>i</sub> b<sub>i</sub></code>.
* @throws DimensionMismatchException if arrays dimensions don't match
* @since 3.2
*/
T linearCombination(T[] a, T[] b)
throws DimensionMismatchException;
/**
* Compute a linear combination.
* @param a Factors.
* @param b Factors.
* @return <code>&Sigma;<sub>i</sub> a<sub>i</sub> b<sub>i</sub></code>.
* @throws DimensionMismatchException if arrays dimensions don't match
* @since 3.2
*/
T linearCombination(double[] a, T[] b)
throws DimensionMismatchException;
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub>
* @see #linearCombination(Object, Object, Object, Object, Object, Object)
* @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
* @since 3.2
*/
T linearCombination(T a1, T b1, T a2, T b2);
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub>
* @see #linearCombination(double, Object, double, Object, double, Object)
* @see #linearCombination(double, Object, double, Object, double, Object, double, Object)
* @since 3.2
*/
T linearCombination(double a1, T b1, double a2, T b2);
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @param a3 first factor of the third term
* @param b3 second factor of the third term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
* @see #linearCombination(Object, Object, Object, Object)
* @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
* @since 3.2
*/
T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3);
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @param a3 first factor of the third term
* @param b3 second factor of the third term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
* @see #linearCombination(double, Object, double, Object)
* @see #linearCombination(double, Object, double, Object, double, Object, double, Object)
* @since 3.2
*/
T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3);
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @param a3 first factor of the third term
* @param b3 second factor of the third term
* @param a4 first factor of the third term
* @param b4 second factor of the third term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
* a<sub>4</sub>&times;b<sub>4</sub>
* @see #linearCombination(Object, Object, Object, Object)
* @see #linearCombination(Object, Object, Object, Object, Object, Object)
* @since 3.2
*/
T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3, T a4, T b4);
/**
* Compute a linear combination.
* @param a1 first factor of the first term
* @param b1 second factor of the first term
* @param a2 first factor of the second term
* @param b2 second factor of the second term
* @param a3 first factor of the third term
* @param b3 second factor of the third term
* @param a4 first factor of the third term
* @param b4 second factor of the third term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
* a<sub>4</sub>&times;b<sub>4</sub>
* @see #linearCombination(double, Object, double, Object)
* @see #linearCombination(double, Object, double, Object, double, Object)
* @since 3.2
*/
T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3, double a4, T b4);
}

View File

@ -1,35 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a bivariate real function.
*
* @since 2.1
*/
public interface BivariateFunction {
/**
* Compute the value for the function.
*
* @param x Abscissa for which the function value should be computed.
* @param y Ordinate for which the function value should be computed.
* @return the value.
*/
double value(double x, double y);
}

View File

@ -1,544 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
import org.apache.commons.numbers.arrays.LinearCombination;
import org.apache.commons.math4.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math4.analysis.differentiation.MultivariateDifferentiableFunction;
import org.apache.commons.math4.analysis.differentiation.UnivariateDifferentiableFunction;
import org.apache.commons.math4.analysis.function.Identity;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
/**
* Utilities for manipulating function objects.
*
* @since 3.0
*/
public class FunctionUtils {
/**
* Class only contains static methods.
*/
private FunctionUtils() {}
/**
* Composes functions.
* <p>
* The functions in the argument list are composed sequentially, in the
* given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).</p>
*
* @param f List of functions.
* @return the composite function.
*/
public static UnivariateFunction compose(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
double r = x;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
};
}
/**
* Composes functions.
* <p>
* The functions in the argument list are composed sequentially, in the
* given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).</p>
*
* @param f List of functions.
* @return the composite function.
* @since 3.1
*/
public static UnivariateDifferentiableFunction compose(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double t) {
double r = t;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
/** {@inheritDoc} */
@Override
public DerivativeStructure value(final DerivativeStructure t) {
DerivativeStructure r = t;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
};
}
/**
* Adds functions.
*
* @param f List of functions.
* @return a function that computes the sum of the functions.
*/
public static UnivariateFunction add(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r += f[i].value(x);
}
return r;
}
};
}
/**
* Adds functions.
*
* @param f List of functions.
* @return a function that computes the sum of the functions.
* @since 3.1
*/
public static UnivariateDifferentiableFunction add(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double t) {
double r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r += f[i].value(t);
}
return r;
}
/** {@inheritDoc}
* @throws DimensionMismatchException if functions are not consistent with each other
*/
@Override
public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
DerivativeStructure r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r = r.add(f[i].value(t));
}
return r;
}
};
}
/**
* Multiplies functions.
*
* @param f List of functions.
* @return a function that computes the product of the functions.
*/
public static UnivariateFunction multiply(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r *= f[i].value(x);
}
return r;
}
};
}
/**
* Multiplies functions.
*
* @param f List of functions.
* @return a function that computes the product of the functions.
* @since 3.1
*/
public static UnivariateDifferentiableFunction multiply(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double t) {
double r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r *= f[i].value(t);
}
return r;
}
/** {@inheritDoc} */
@Override
public DerivativeStructure value(final DerivativeStructure t) {
DerivativeStructure r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r = r.multiply(f[i].value(t));
}
return r;
}
};
}
/**
* Returns the univariate function
* {@code h(x) = combiner(f(x), g(x)).}
*
* @param combiner Combiner function.
* @param f Function.
* @param g Function.
* @return the composite function.
*/
public static UnivariateFunction combine(final BivariateFunction combiner,
final UnivariateFunction f,
final UnivariateFunction g) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
return combiner.value(f.value(x), g.value(x));
}
};
}
/**
* Returns a MultivariateFunction h(x[]) defined by <pre> <code>
* h(x[]) = combiner(...combiner(combiner(initialValue,f(x[0])),f(x[1]))...),f(x[x.length-1]))
* </code></pre>
*
* @param combiner Combiner function.
* @param f Function.
* @param initialValue Initial value.
* @return a collector function.
*/
public static MultivariateFunction collector(final BivariateFunction combiner,
final UnivariateFunction f,
final double initialValue) {
return new MultivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double[] point) {
double result = combiner.value(initialValue, f.value(point[0]));
for (int i = 1; i < point.length; i++) {
result = combiner.value(result, f.value(point[i]));
}
return result;
}
};
}
/**
* Returns a MultivariateFunction h(x[]) defined by <pre> <code>
* h(x[]) = combiner(...combiner(combiner(initialValue,x[0]),x[1])...),x[x.length-1])
* </code></pre>
*
* @param combiner Combiner function.
* @param initialValue Initial value.
* @return a collector function.
*/
public static MultivariateFunction collector(final BivariateFunction combiner,
final double initialValue) {
return collector(combiner, new Identity(), initialValue);
}
/**
* Creates a unary function by fixing the first argument of a binary function.
*
* @param f Binary function.
* @param fixed value to which the first argument of {@code f} is set.
* @return the unary function h(x) = f(fixed, x)
*/
public static UnivariateFunction fix1stArgument(final BivariateFunction f,
final double fixed) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
return f.value(fixed, x);
}
};
}
/**
* Creates a unary function by fixing the second argument of a binary function.
*
* @param f Binary function.
* @param fixed value to which the second argument of {@code f} is set.
* @return the unary function h(x) = f(x, fixed)
*/
public static UnivariateFunction fix2ndArgument(final BivariateFunction f,
final double fixed) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(double x) {
return f.value(x, fixed);
}
};
}
/**
* Samples the specified univariate real function on the specified interval.
* <p>
* The interval is divided equally into {@code n} sections and sample points
* are taken from {@code min} to {@code max - (max - min) / n}; therefore
* {@code f} is not sampled at the upper bound {@code max}.</p>
*
* @param f Function to be sampled
* @param min Lower bound of the interval (included).
* @param max Upper bound of the interval (excluded).
* @param n Number of sample points.
* @return the array of samples.
* @throws NumberIsTooLargeException if the lower bound {@code min} is
* greater than, or equal to the upper bound {@code max}.
* @throws NotStrictlyPositiveException if the number of sample points
* {@code n} is negative.
*/
public static double[] sample(UnivariateFunction f, double min, double max, int n)
throws NumberIsTooLargeException, NotStrictlyPositiveException {
if (n <= 0) {
throw new NotStrictlyPositiveException(
LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES,
Integer.valueOf(n));
}
if (min >= max) {
throw new NumberIsTooLargeException(min, max, false);
}
final double[] s = new double[n];
final double h = (max - min) / n;
for (int i = 0; i < n; i++) {
s[i] = f.value(min + i * h);
}
return s;
}
/** Convert regular functions to {@link UnivariateDifferentiableFunction}.
* <p>
* This method handle the case with one free parameter and several derivatives.
* For the case with several free parameters and only first order derivatives,
* see {@link #toDifferentiable(MultivariateFunction, MultivariateVectorFunction)}.
* There are no direct support for intermediate cases, with several free parameters
* and order 2 or more derivatives, as is would be difficult to specify all the
* cross derivatives.
* </p>
* <p>
* Note that the derivatives are expected to be computed only with respect to the
* raw parameter x of the base function, i.e. they are df/dx, df<sup>2</sup>/dx<sup>2</sup>, ...
* Even if the built function is later used in a composition like f(sin(t)), the provided
* derivatives should <em>not</em> apply the composition with sine and its derivatives by
* themselves. The composition will be done automatically here and the result will properly
* contain f(sin(t)), df(sin(t))/dt, df<sup>2</sup>(sin(t))/dt<sup>2</sup> despite the
* provided derivatives functions know nothing about the sine function.
* </p>
* @param f base function f(x)
* @param derivatives derivatives of the base function, in increasing differentiation order
* @return a differentiable function with value and all specified derivatives
* @see #toDifferentiable(MultivariateFunction, MultivariateVectorFunction)
* @see #derivative(UnivariateDifferentiableFunction, int)
*/
public static UnivariateDifferentiableFunction toDifferentiable(final UnivariateFunction f,
final UnivariateFunction ... derivatives) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double x) {
return f.value(x);
}
/** {@inheritDoc} */
@Override
public DerivativeStructure value(final DerivativeStructure x) {
if (x.getOrder() > derivatives.length) {
throw new NumberIsTooLargeException(x.getOrder(), derivatives.length, true);
}
final double[] packed = new double[x.getOrder() + 1];
packed[0] = f.value(x.getValue());
for (int i = 0; i < x.getOrder(); ++i) {
packed[i + 1] = derivatives[i].value(x.getValue());
}
return x.compose(packed);
}
};
}
/** Convert regular functions to {@link MultivariateDifferentiableFunction}.
* <p>
* This method handle the case with several free parameters and only first order derivatives.
* For the case with one free parameter and several derivatives,
* see {@link #toDifferentiable(UnivariateFunction, UnivariateFunction...)}.
* There are no direct support for intermediate cases, with several free parameters
* and order 2 or more derivatives, as is would be difficult to specify all the
* cross derivatives.
* </p>
* <p>
* Note that the gradient is expected to be computed only with respect to the
* raw parameter x of the base function, i.e. it is df/dx<sub>1</sub>, df/dx<sub>2</sub>, ...
* Even if the built function is later used in a composition like f(sin(t), cos(t)), the provided
* gradient should <em>not</em> apply the composition with sine or cosine and their derivative by
* itself. The composition will be done automatically here and the result will properly
* contain f(sin(t), cos(t)), df(sin(t), cos(t))/dt despite the provided derivatives functions
* know nothing about the sine or cosine functions.
* </p>
* @param f base function f(x)
* @param gradient gradient of the base function
* @return a differentiable function with value and gradient
* @see #toDifferentiable(UnivariateFunction, UnivariateFunction...)
* @see #derivative(MultivariateDifferentiableFunction, int[])
*/
public static MultivariateDifferentiableFunction toDifferentiable(final MultivariateFunction f,
final MultivariateVectorFunction gradient) {
return new MultivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double[] point) {
return f.value(point);
}
/** {@inheritDoc} */
@Override
public DerivativeStructure value(final DerivativeStructure[] point) {
// set up the input parameters
final double[] dPoint = new double[point.length];
for (int i = 0; i < point.length; ++i) {
dPoint[i] = point[i].getValue();
if (point[i].getOrder() > 1) {
throw new NumberIsTooLargeException(point[i].getOrder(), 1, true);
}
}
// evaluate regular functions
final double v = f.value(dPoint);
final double[] dv = gradient.value(dPoint);
if (dv.length != point.length) {
// the gradient function is inconsistent
throw new DimensionMismatchException(dv.length, point.length);
}
// build the combined derivative
final int parameters = point[0].getFreeParameters();
final double[] partials = new double[point.length];
final double[] packed = new double[parameters + 1];
packed[0] = v;
final int orders[] = new int[parameters];
for (int i = 0; i < parameters; ++i) {
// we differentiate once with respect to parameter i
orders[i] = 1;
for (int j = 0; j < point.length; ++j) {
partials[j] = point[j].getPartialDerivative(orders);
}
orders[i] = 0;
// compose partial derivatives
packed[i + 1] = LinearCombination.value(dv, partials);
}
return new DerivativeStructure(parameters, 1, packed);
}
};
}
/** Convert an {@link UnivariateDifferentiableFunction} to an
* {@link UnivariateFunction} computing n<sup>th</sup> order derivative.
* <p>
* This converter is only a convenience method. Beware computing only one derivative does
* not save any computation as the original function will really be called under the hood.
* The derivative will be extracted from the full {@link DerivativeStructure} result.
* </p>
* @param f original function, with value and all its derivatives
* @param order of the derivative to extract
* @return function computing the derivative at required order
* @see #derivative(MultivariateDifferentiableFunction, int[])
* @see #toDifferentiable(UnivariateFunction, UnivariateFunction...)
*/
public static UnivariateFunction derivative(final UnivariateDifferentiableFunction f, final int order) {
return new UnivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(final double x) {
final DerivativeStructure dsX = new DerivativeStructure(1, order, 0, x);
return f.value(dsX).getPartialDerivative(order);
}
};
}
/** Convert an {@link MultivariateDifferentiableFunction} to an
* {@link MultivariateFunction} computing n<sup>th</sup> order derivative.
* <p>
* This converter is only a convenience method. Beware computing only one derivative does
* not save any computation as the original function will really be called under the hood.
* The derivative will be extracted from the full {@link DerivativeStructure} result.
* </p>
* @param f original function, with value and all its derivatives
* @param orders of the derivative to extract, for each free parameters
* @return function computing the derivative at required order
* @see #derivative(UnivariateDifferentiableFunction, int)
* @see #toDifferentiable(MultivariateFunction, MultivariateVectorFunction)
*/
public static MultivariateFunction derivative(final MultivariateDifferentiableFunction f, final int[] orders) {
return new MultivariateFunction() {
/** {@inheritDoc} */
@Override
public double value(final double[] point) {
// the maximum differentiation order is the sum of all orders
int sumOrders = 0;
for (final int order : orders) {
sumOrders += order;
}
// set up the input parameters
final DerivativeStructure[] dsPoint = new DerivativeStructure[point.length];
for (int i = 0; i < point.length; ++i) {
dsPoint[i] = new DerivativeStructure(point.length, sumOrders, i, point[i]);
}
return f.value(dsPoint).getPartialDerivative(orders);
}
};
}
}

View File

@ -1,42 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a multivariate real function.
*
* @since 2.0
*/
public interface MultivariateFunction {
/**
* Compute the value for the function at the given point.
*
* @param point Point at which the function must be evaluated.
* @return the function value for the given point.
* @throws org.apache.commons.math4.exception.DimensionMismatchException
* if the parameter's dimension is wrong for the function being evaluated.
* @throws org.apache.commons.math4.exception.MathIllegalArgumentException
* when the activated method itself can ascertain that preconditions,
* specified in the API expressed at the level of the activated method,
* have been violated. In the vast majority of cases where Commons Math
* throws this exception, it is the result of argument checking of actual
* parameters immediately passed to a method.
*/
double value(double[] point);
}

View File

@ -1,35 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a multivariate matrix function.
* @since 2.0
*/
public interface MultivariateMatrixFunction {
/**
* Compute the value for the function at the given point.
* @param point point at which the function must be evaluated
* @return function value for the given point
* @exception IllegalArgumentException if point's dimension is wrong
*/
double[][] value(double[] point)
throws IllegalArgumentException;
}

View File

@ -1,35 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a multivariate vectorial function.
* @since 2.0
*/
public interface MultivariateVectorFunction {
/**
* Compute the value for the function at the given point.
* @param point point at which the function must be evaluated
* @return function value for the given point
* @exception IllegalArgumentException if point's dimension is wrong
*/
double[] value(double[] point)
throws IllegalArgumentException;
}

View File

@ -1,44 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a real function that depends on one independent
* variable plus some extra parameters.
*
* @since 3.0
*/
public interface ParametricUnivariateFunction {
/**
* Compute the value of the function.
*
* @param x Point for which the function value should be computed.
* @param parameters Function parameters.
* @return the value.
*/
double value(double x, double... parameters);
/**
* Compute the gradient of the function with respect to its parameters.
*
* @param x Point for which the function value should be computed.
* @param parameters Function parameters.
* @return the value.
*/
double[] gradient(double x, double... parameters);
}

View File

@ -1,86 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
import org.apache.commons.math4.RealFieldElement;
/**
* An interface representing a univariate real function.
* <p>
* When a <em>user-defined</em> function encounters an error during
* evaluation, the {@link #value(RealFieldElement) value} method should throw a
* <em>user-defined</em> unchecked exception.</p>
* <p>
* The following code excerpt shows the recommended way to do that using
* a root solver as an example, but the same construct is applicable to
* ODE integrators or optimizers.
*
* <pre>
* private static class LocalException extends RuntimeException {
* // The x value that caused the problem.
* private final SomeFieldType x;
*
* public LocalException(SomeFieldType x) {
* this.x = x;
* }
*
* public double getX() {
* return x;
* }
* }
*
* private static class MyFunction implements FieldUnivariateFunction&lt;SomeFieldType&gt; {
* public SomeFieldType value(SomeFieldType x) {
* SomeFieldType y = hugeFormula(x);
* if (somethingBadHappens) {
* throw new LocalException(x);
* }
* return y;
* }
* }
*
* public void compute() {
* try {
* solver.solve(maxEval, new MyFunction(a, b, c), min, max);
* } catch (LocalException le) {
* // Retrieve the x value.
* }
* }
* </pre>
*<p>
* As shown, the exception is local to the user's code and it is guaranteed
* that Apache Commons Math will not catch it.</p>
*
* @param <T> the type of the field elements
* @since 3.6
* @see UnivariateFunction
*/
public interface RealFieldUnivariateFunction<T extends RealFieldElement<T>> {
/**
* Compute the value of the function.
*
* @param x Point at which the function value should be computed.
* @return the value of the function.
* @throws IllegalArgumentException when the activated method itself can
* ascertain that a precondition, specified in the API expressed at the
* level of the activated method, has been violated.
* When Commons Math throws an {@code IllegalArgumentException}, it is
* usually the consequence of checking the actual parameters passed to
* the method.
*/
T value(T x);
}

View File

@ -1,35 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a trivariate real function.
*
* @since 2.2
*/
public interface TrivariateFunction {
/**
* Compute the value for the function.
*
* @param x x-coordinate for which the function value should be computed.
* @param y y-coordinate for which the function value should be computed.
* @param z z-coordinate for which the function value should be computed.
* @return the value.
*/
double value(double x, double y, double z);
}

View File

@ -1,81 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a univariate real function.
* <p>
* When a <em>user-defined</em> function encounters an error during
* evaluation, the {@link #value(double) value} method should throw a
* <em>user-defined</em> unchecked exception.</p>
* <p>
* The following code excerpt shows the recommended way to do that using
* a root solver as an example, but the same construct is applicable to
* ODE integrators or optimizers.</p>
*
* <pre>
* private static class LocalException extends RuntimeException {
* // The x value that caused the problem.
* private final double x;
*
* public LocalException(double x) {
* this.x = x;
* }
*
* public double getX() {
* return x;
* }
* }
*
* private static class MyFunction implements UnivariateFunction {
* public double value(double x) {
* double y = hugeFormula(x);
* if (somethingBadHappens) {
* throw new LocalException(x);
* }
* return y;
* }
* }
*
* public void compute() {
* try {
* solver.solve(maxEval, new MyFunction(a, b, c), min, max);
* } catch (LocalException le) {
* // Retrieve the x value.
* }
* }
* </pre>
*
* As shown, the exception is local to the user's code and it is guaranteed
* that Apache Commons Math will not catch it.
*
*/
public interface UnivariateFunction {
/**
* Compute the value of the function.
*
* @param x Point at which the function value should be computed.
* @return the value of the function.
* @throws IllegalArgumentException when the activated method itself can
* ascertain that a precondition, specified in the API expressed at the
* level of the activated method, has been violated.
* When Commons Math throws an {@code IllegalArgumentException}, it is
* usually the consequence of checking the actual parameters passed to
* the method.
*/
double value(double x);
}

View File

@ -1,33 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a univariate matrix function.
*
* @since 2.0
*/
public interface UnivariateMatrixFunction {
/**
* Compute the value for the function.
* @param x the point for which the function value should be computed
* @return the value
*/
double[][] value(double x);
}

View File

@ -1,33 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis;
/**
* An interface representing a univariate vectorial function.
*
* @since 2.0
*/
public interface UnivariateVectorFunction {
/**
* Compute the value for the function.
* @param x the point for which the function value should be computed
* @return the value
*/
double[] value(double x);
}

View File

@ -1,393 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import java.io.Serializable;
import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.analysis.UnivariateMatrixFunction;
import org.apache.commons.math4.analysis.UnivariateVectorFunction;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NotPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.util.FastMath;
/** Univariate functions differentiator using finite differences.
* <p>
* This class creates some wrapper objects around regular
* {@link UnivariateFunction univariate functions} (or {@link
* UnivariateVectorFunction univariate vector functions} or {@link
* UnivariateMatrixFunction univariate matrix functions}). These
* wrapper objects compute derivatives in addition to function
* values.
* </p>
* <p>
* The wrapper objects work by calling the underlying function on
* a sampling grid around the current point and performing polynomial
* interpolation. A finite differences scheme with n points is
* theoretically able to compute derivatives up to order n-1, but
* it is generally better to have a slight margin. The step size must
* also be small enough in order for the polynomial approximation to
* be good in the current point neighborhood, but it should not be too
* small because numerical instability appears quickly (there are several
* differences of close points). Choosing the number of points and
* the step size is highly problem dependent.
* </p>
* <p>
* As an example of good and bad settings, lets consider the quintic
* polynomial function {@code f(x) = (x-1)*(x-0.5)*x*(x+0.5)*(x+1)}.
* Since it is a polynomial, finite differences with at least 6 points
* should theoretically recover the exact same polynomial and hence
* compute accurate derivatives for any order. However, due to numerical
* errors, we get the following results for a 7 points finite differences
* for abscissae in the [-10, 10] range:
* <ul>
* <li>step size = 0.25, second order derivative error about 9.97e-10</li>
* <li>step size = 0.25, fourth order derivative error about 5.43e-8</li>
* <li>step size = 1.0e-6, second order derivative error about 148</li>
* <li>step size = 1.0e-6, fourth order derivative error about 6.35e+14</li>
* </ul>
* <p>
* This example shows that the small step size is really bad, even simply
* for second order derivative!</p>
*
* @since 3.1
*/
public class FiniteDifferencesDifferentiator
implements UnivariateFunctionDifferentiator, UnivariateVectorFunctionDifferentiator,
UnivariateMatrixFunctionDifferentiator, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20120917L;
/** Number of points to use. */
private final int nbPoints;
/** Step size. */
private final double stepSize;
/** Half sample span. */
private final double halfSampleSpan;
/** Lower bound for independent variable. */
private final double tMin;
/** Upper bound for independent variable. */
private final double tMax;
/**
* Build a differentiator with number of points and step size when independent variable is unbounded.
* <p>
* Beware that wrong settings for the finite differences differentiator
* can lead to highly unstable and inaccurate results, especially for
* high derivation orders. Using very small step sizes is often a
* <em>bad</em> idea.
* </p>
* @param nbPoints number of points to use
* @param stepSize step size (gap between each point)
* @exception NotPositiveException if {@code stepsize <= 0} (note that
* {@link NotPositiveException} extends {@link NumberIsTooSmallException})
* @exception NumberIsTooSmallException {@code nbPoint <= 1}
*/
public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize)
throws NotPositiveException, NumberIsTooSmallException {
this(nbPoints, stepSize, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
/**
* Build a differentiator with number of points and step size when independent variable is bounded.
* <p>
* When the independent variable is bounded (tLower &lt; t &lt; tUpper), the sampling
* points used for differentiation will be adapted to ensure the constraint holds
* even near the boundaries. This means the sample will not be centered anymore in
* these cases. At an extreme case, computing derivatives exactly at the lower bound
* will lead the sample to be entirely on the right side of the derivation point.
* </p>
* <p>
* Note that the boundaries are considered to be excluded for function evaluation.
* </p>
* <p>
* Beware that wrong settings for the finite differences differentiator
* can lead to highly unstable and inaccurate results, especially for
* high derivation orders. Using very small step sizes is often a
* <em>bad</em> idea.
* </p>
* @param nbPoints number of points to use
* @param stepSize step size (gap between each point)
* @param tLower lower bound for independent variable (may be {@code Double.NEGATIVE_INFINITY}
* if there are no lower bounds)
* @param tUpper upper bound for independent variable (may be {@code Double.POSITIVE_INFINITY}
* if there are no upper bounds)
* @exception NotPositiveException if {@code stepsize <= 0} (note that
* {@link NotPositiveException} extends {@link NumberIsTooSmallException})
* @exception NumberIsTooSmallException {@code nbPoint <= 1}
* @exception NumberIsTooLargeException {@code stepSize * (nbPoints - 1) >= tUpper - tLower}
*/
public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize,
final double tLower, final double tUpper)
throws NotPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
if (nbPoints <= 1) {
throw new NumberIsTooSmallException(stepSize, 1, false);
}
this.nbPoints = nbPoints;
if (stepSize <= 0) {
throw new NotPositiveException(stepSize);
}
this.stepSize = stepSize;
halfSampleSpan = 0.5 * stepSize * (nbPoints - 1);
if (2 * halfSampleSpan >= tUpper - tLower) {
throw new NumberIsTooLargeException(2 * halfSampleSpan, tUpper - tLower, false);
}
final double safety = FastMath.ulp(halfSampleSpan);
this.tMin = tLower + halfSampleSpan + safety;
this.tMax = tUpper - halfSampleSpan - safety;
}
/**
* Get the number of points to use.
* @return number of points to use
*/
public int getNbPoints() {
return nbPoints;
}
/**
* Get the step size.
* @return step size
*/
public double getStepSize() {
return stepSize;
}
/**
* Evaluate derivatives from a sample.
* <p>
* Evaluation is done using divided differences.
* </p>
* @param t evaluation abscissa value and derivatives
* @param t0 first sample point abscissa
* @param y function values sample {@code y[i] = f(t[i]) = f(t0 + i * stepSize)}
* @return value and derivatives at {@code t}
* @exception NumberIsTooLargeException if the requested derivation order
* is larger or equal to the number of points
*/
private DerivativeStructure evaluate(final DerivativeStructure t, final double t0,
final double[] y)
throws NumberIsTooLargeException {
// create divided differences diagonal arrays
final double[] top = new double[nbPoints];
final double[] bottom = new double[nbPoints];
for (int i = 0; i < nbPoints; ++i) {
// update the bottom diagonal of the divided differences array
bottom[i] = y[i];
for (int j = 1; j <= i; ++j) {
bottom[i - j] = (bottom[i - j + 1] - bottom[i - j]) / (j * stepSize);
}
// update the top diagonal of the divided differences array
top[i] = bottom[0];
}
// evaluate interpolation polynomial (represented by top diagonal) at t
final int order = t.getOrder();
final int parameters = t.getFreeParameters();
final double[] derivatives = t.getAllDerivatives();
final double dt0 = t.getValue() - t0;
DerivativeStructure interpolation = new DerivativeStructure(parameters, order, 0.0);
DerivativeStructure monomial = null;
for (int i = 0; i < nbPoints; ++i) {
if (i == 0) {
// start with monomial(t) = 1
monomial = new DerivativeStructure(parameters, order, 1.0);
} else {
// monomial(t) = (t - t0) * (t - t1) * ... * (t - t(i-1))
derivatives[0] = dt0 - (i - 1) * stepSize;
final DerivativeStructure deltaX = new DerivativeStructure(parameters, order, derivatives);
monomial = monomial.multiply(deltaX);
}
interpolation = interpolation.add(monomial.multiply(top[i]));
}
return interpolation;
}
/** {@inheritDoc}
* <p>The returned object cannot compute derivatives to arbitrary orders. The
* value function will throw a {@link NumberIsTooLargeException} if the requested
* derivation order is larger or equal to the number of points.
* </p>
*/
@Override
public UnivariateDifferentiableFunction differentiate(final UnivariateFunction function) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
@Override
public double value(final double x) throws MathIllegalArgumentException {
return function.value(x);
}
/** {@inheritDoc} */
@Override
public DerivativeStructure value(final DerivativeStructure t)
throws MathIllegalArgumentException {
// check we can achieve the requested derivation order with the sample
if (t.getOrder() >= nbPoints) {
throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
}
// compute sample position, trying to be centered if possible
final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;
// compute sample points
final double[] y = new double[nbPoints];
for (int i = 0; i < nbPoints; ++i) {
y[i] = function.value(t0 + i * stepSize);
}
// evaluate derivatives
return evaluate(t, t0, y);
}
};
}
/** {@inheritDoc}
* <p>The returned object cannot compute derivatives to arbitrary orders. The
* value function will throw a {@link NumberIsTooLargeException} if the requested
* derivation order is larger or equal to the number of points.
* </p>
*/
@Override
public UnivariateDifferentiableVectorFunction differentiate(final UnivariateVectorFunction function) {
return new UnivariateDifferentiableVectorFunction() {
/** {@inheritDoc} */
@Override
public double[]value(final double x) throws MathIllegalArgumentException {
return function.value(x);
}
/** {@inheritDoc} */
@Override
public DerivativeStructure[] value(final DerivativeStructure t)
throws MathIllegalArgumentException {
// check we can achieve the requested derivation order with the sample
if (t.getOrder() >= nbPoints) {
throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
}
// compute sample position, trying to be centered if possible
final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;
// compute sample points
double[][] y = null;
for (int i = 0; i < nbPoints; ++i) {
final double[] v = function.value(t0 + i * stepSize);
if (i == 0) {
y = new double[v.length][nbPoints];
}
for (int j = 0; j < v.length; ++j) {
y[j][i] = v[j];
}
}
// evaluate derivatives
final DerivativeStructure[] value = new DerivativeStructure[y.length];
for (int j = 0; j < value.length; ++j) {
value[j] = evaluate(t, t0, y[j]);
}
return value;
}
};
}
/** {@inheritDoc}
* <p>The returned object cannot compute derivatives to arbitrary orders. The
* value function will throw a {@link NumberIsTooLargeException} if the requested
* derivation order is larger or equal to the number of points.
* </p>
*/
@Override
public UnivariateDifferentiableMatrixFunction differentiate(final UnivariateMatrixFunction function) {
return new UnivariateDifferentiableMatrixFunction() {
/** {@inheritDoc} */
@Override
public double[][] value(final double x) throws MathIllegalArgumentException {
return function.value(x);
}
/** {@inheritDoc} */
@Override
public DerivativeStructure[][] value(final DerivativeStructure t)
throws MathIllegalArgumentException {
// check we can achieve the requested derivation order with the sample
if (t.getOrder() >= nbPoints) {
throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
}
// compute sample position, trying to be centered if possible
final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;
// compute sample points
double[][][] y = null;
for (int i = 0; i < nbPoints; ++i) {
final double[][] v = function.value(t0 + i * stepSize);
if (i == 0) {
y = new double[v.length][v[0].length][nbPoints];
}
for (int j = 0; j < v.length; ++j) {
for (int k = 0; k < v[j].length; ++k) {
y[j][k][i] = v[j][k];
}
}
}
// evaluate derivatives
final DerivativeStructure[][] value = new DerivativeStructure[y.length][y[0].length];
for (int j = 0; j < value.length; ++j) {
for (int k = 0; k < y[j].length; ++k) {
value[j][k] = evaluate(t, t0, y[j][k]);
}
}
return value;
}
};
}
}

View File

@ -1,66 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.MultivariateVectorFunction;
/** Class representing the gradient of a multivariate function.
* <p>
* The vectorial components of the function represent the derivatives
* with respect to each function parameters.
* </p>
* @since 3.1
*/
public class GradientFunction implements MultivariateVectorFunction {
/** Underlying real-valued function. */
private final MultivariateDifferentiableFunction f;
/** Simple constructor.
* @param f underlying real-valued function
*/
public GradientFunction(final MultivariateDifferentiableFunction f) {
this.f = f;
}
/** {@inheritDoc} */
@Override
public double[] value(double[] point) {
// set up parameters
final DerivativeStructure[] dsX = new DerivativeStructure[point.length];
for (int i = 0; i < point.length; ++i) {
dsX[i] = new DerivativeStructure(point.length, 1, i, point[i]);
}
// compute the derivatives
final DerivativeStructure dsY = f.value(dsX);
// extract the gradient
final double[] y = new double[point.length];
final int[] orders = new int[point.length];
for (int i = 0; i < point.length; ++i) {
orders[i] = 1;
y[i] = dsY.getPartialDerivative(orders);
orders[i] = 0;
}
return y;
}
}

View File

@ -1,70 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.MultivariateMatrixFunction;
/** Class representing the Jacobian of a multivariate vector function.
* <p>
* The rows iterate on the model functions while the columns iterate on the parameters; thus,
* the numbers of rows is equal to the dimension of the underlying function vector
* value and the number of columns is equal to the number of free parameters of
* the underlying function.
* </p>
* @since 3.1
*/
public class JacobianFunction implements MultivariateMatrixFunction {
/** Underlying vector-valued function. */
private final MultivariateDifferentiableVectorFunction f;
/** Simple constructor.
* @param f underlying vector-valued function
*/
public JacobianFunction(final MultivariateDifferentiableVectorFunction f) {
this.f = f;
}
/** {@inheritDoc} */
@Override
public double[][] value(double[] point) {
// set up parameters
final DerivativeStructure[] dsX = new DerivativeStructure[point.length];
for (int i = 0; i < point.length; ++i) {
dsX[i] = new DerivativeStructure(point.length, 1, i, point[i]);
}
// compute the derivatives
final DerivativeStructure[] dsY = f.value(dsX);
// extract the Jacobian
final double[][] y = new double[dsY.length][point.length];
final int[] orders = new int[point.length];
for (int i = 0; i < dsY.length; ++i) {
for (int j = 0; j < point.length; ++j) {
orders[j] = 1;
y[i][j] = dsY[i].getPartialDerivative(orders);
orders[j] = 0;
}
}
return y;
}
}

View File

@ -1,42 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.MultivariateFunction;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
/**
* Extension of {@link MultivariateFunction} representing a
* multivariate differentiable real function.
* @since 3.1
*/
public interface MultivariateDifferentiableFunction extends MultivariateFunction {
/**
* Compute the value for the function at the given point.
*
* @param point Point at which the function must be evaluated.
* @return the function value for the given point.
* @exception MathIllegalArgumentException if {@code point} does not
* satisfy the function's constraints (wrong dimension, argument out of bound,
* or unsupported derivative order for example)
*/
DerivativeStructure value(DerivativeStructure[] point)
throws MathIllegalArgumentException;
}

View File

@ -1,43 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.MultivariateVectorFunction;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
/**
* Extension of {@link MultivariateVectorFunction} representing a
* multivariate differentiable vectorial function.
* @since 3.1
*/
public interface MultivariateDifferentiableVectorFunction
extends MultivariateVectorFunction {
/**
* Compute the value for the function at the given point.
* @param point point at which the function must be evaluated
* @return function value for the given point
* @exception MathIllegalArgumentException if {@code point} does not
* satisfy the function's constraints (wrong dimension, argument out of bound,
* or unsupported derivative order for example)
*/
DerivativeStructure[] value(DerivativeStructure[] point)
throws MathIllegalArgumentException;
}

View File

@ -1,937 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.numbers.arrays.LinearCombination;
import org.apache.commons.numbers.core.Precision;
import org.apache.commons.math4.Field;
import org.apache.commons.math4.FieldElement;
import org.apache.commons.math4.RealFieldElement;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathUtils;
/**
* First derivative computation with large number of variables.
* <p>
* This class plays a similar role to {@link DerivativeStructure}, with
* a focus on efficiency when dealing with large number of independent variables
* and most computation depend only on a few of them, and when only first derivative
* is desired. When these conditions are met, this class should be much faster than
* {@link DerivativeStructure} and use less memory.
* </p>
*
* @since 3.3
*/
public class SparseGradient implements RealFieldElement<SparseGradient>, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20131025L;
/** Value of the calculation. */
private double value;
/** Stored derivative, each key representing a different independent variable. */
private final Map<Integer, Double> derivatives;
/** Internal constructor.
* @param value value of the function
* @param derivatives derivatives map, a deep copy will be performed,
* so the map given here will remain safe from changes in the new instance,
* may be null to create an empty derivatives map, i.e. a constant value
*/
private SparseGradient(final double value, final Map<Integer, Double> derivatives) {
this.value = value;
this.derivatives = new HashMap<>();
if (derivatives != null) {
this.derivatives.putAll(derivatives);
}
}
/** Internal constructor.
* @param value value of the function
* @param scale scaling factor to apply to all derivatives
* @param derivatives derivatives map, a deep copy will be performed,
* so the map given here will remain safe from changes in the new instance,
* may be null to create an empty derivatives map, i.e. a constant value
*/
private SparseGradient(final double value, final double scale,
final Map<Integer, Double> derivatives) {
this.value = value;
this.derivatives = new HashMap<>();
if (derivatives != null) {
for (final Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
this.derivatives.put(entry.getKey(), scale * entry.getValue());
}
}
}
/** Factory method creating a constant.
* @param value value of the constant
* @return a new instance
*/
public static SparseGradient createConstant(final double value) {
return new SparseGradient(value, Collections.<Integer, Double> emptyMap());
}
/** Factory method creating an independent variable.
* @param idx index of the variable
* @param value value of the variable
* @return a new instance
*/
public static SparseGradient createVariable(final int idx, final double value) {
return new SparseGradient(value, Collections.singletonMap(idx, 1.0));
}
/**
* Find the number of variables.
* @return number of variables
*/
public int numVars() {
return derivatives.size();
}
/**
* Get the derivative with respect to a particular index variable.
*
* @param index index to differentiate with.
* @return derivative with respect to a particular index variable
*/
public double getDerivative(final int index) {
final Double out = derivatives.get(index);
return (out == null) ? 0.0 : out;
}
/**
* Get the value of the function.
* @return value of the function.
*/
public double getValue() {
return value;
}
/** {@inheritDoc} */
@Override
public double getReal() {
return value;
}
/** {@inheritDoc} */
@Override
public SparseGradient add(final SparseGradient a) {
final SparseGradient out = new SparseGradient(value + a.value, derivatives);
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, entry.getValue());
} else {
out.derivatives.put(id, old + entry.getValue());
}
}
return out;
}
/**
* Add in place.
* <p>
* This method is designed to be faster when used multiple times in a loop.
* </p>
* <p>
* The instance is changed here, in order to not change the
* instance the {@link #add(SparseGradient)} method should
* be used.
* </p>
* @param a instance to add
*/
public void addInPlace(final SparseGradient a) {
value += a.value;
for (final Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = derivatives.get(id);
if (old == null) {
derivatives.put(id, entry.getValue());
} else {
derivatives.put(id, old + entry.getValue());
}
}
}
/** {@inheritDoc} */
@Override
public SparseGradient add(final double c) {
final SparseGradient out = new SparseGradient(value + c, derivatives);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient subtract(final SparseGradient a) {
final SparseGradient out = new SparseGradient(value - a.value, derivatives);
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, -entry.getValue());
} else {
out.derivatives.put(id, old - entry.getValue());
}
}
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient subtract(double c) {
return new SparseGradient(value - c, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient multiply(final SparseGradient a) {
final SparseGradient out =
new SparseGradient(value * a.value, Collections.<Integer, Double> emptyMap());
// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
out.derivatives.put(entry.getKey(), a.value * entry.getValue());
}
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, value * entry.getValue());
} else {
out.derivatives.put(id, old + value * entry.getValue());
}
}
return out;
}
/**
* Multiply in place.
* <p>
* This method is designed to be faster when used multiple times in a loop.
* </p>
* <p>
* The instance is changed here, in order to not change the
* instance the {@link #add(SparseGradient)} method should
* be used.
* </p>
* @param a instance to multiply
*/
public void multiplyInPlace(final SparseGradient a) {
// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
derivatives.put(entry.getKey(), a.value * entry.getValue());
}
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = derivatives.get(id);
if (old == null) {
derivatives.put(id, value * entry.getValue());
} else {
derivatives.put(id, old + value * entry.getValue());
}
}
value *= a.value;
}
/** {@inheritDoc} */
@Override
public SparseGradient multiply(final double c) {
return new SparseGradient(value * c, c, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient multiply(final int n) {
return new SparseGradient(value * n, n, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient divide(final SparseGradient a) {
final SparseGradient out = new SparseGradient(value / a.value, Collections.<Integer, Double> emptyMap());
// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
out.derivatives.put(entry.getKey(), entry.getValue() / a.value);
}
for (Map.Entry<Integer, Double> entry : a.derivatives.entrySet()) {
final int id = entry.getKey();
final Double old = out.derivatives.get(id);
if (old == null) {
out.derivatives.put(id, -out.value / a.value * entry.getValue());
} else {
out.derivatives.put(id, old - out.value / a.value * entry.getValue());
}
}
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient divide(final double c) {
return new SparseGradient(value / c, 1.0 / c, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient negate() {
return new SparseGradient(-value, -1.0, derivatives);
}
/** {@inheritDoc} */
@Override
public Field<SparseGradient> getField() {
return new Field<SparseGradient>() {
/** {@inheritDoc} */
@Override
public SparseGradient getZero() {
return createConstant(0);
}
/** {@inheritDoc} */
@Override
public SparseGradient getOne() {
return createConstant(1);
}
/** {@inheritDoc} */
@Override
public Class<? extends FieldElement<SparseGradient>> getRuntimeClass() {
return SparseGradient.class;
}
};
}
/** {@inheritDoc} */
@Override
public SparseGradient remainder(final double a) {
return new SparseGradient(FastMath.IEEEremainder(value, a), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient remainder(final SparseGradient a) {
// compute k such that lhs % rhs = lhs - k rhs
final double rem = FastMath.IEEEremainder(value, a.value);
final double k = FastMath.rint((value - rem) / a.value);
return subtract(a.multiply(k));
}
/** {@inheritDoc} */
@Override
public SparseGradient abs() {
if (Double.doubleToLongBits(value) < 0) {
// we use the bits representation to also handle -0.0
return negate();
} else {
return this;
}
}
/** {@inheritDoc} */
@Override
public SparseGradient ceil() {
return createConstant(FastMath.ceil(value));
}
/** {@inheritDoc} */
@Override
public SparseGradient floor() {
return createConstant(FastMath.floor(value));
}
/** {@inheritDoc} */
@Override
public SparseGradient rint() {
return createConstant(FastMath.rint(value));
}
/** {@inheritDoc} */
@Override
public long round() {
return FastMath.round(value);
}
/** {@inheritDoc} */
@Override
public SparseGradient signum() {
return createConstant(FastMath.signum(value));
}
/** {@inheritDoc} */
@Override
public SparseGradient copySign(final SparseGradient sign) {
final long m = Double.doubleToLongBits(value);
final long s = Double.doubleToLongBits(sign.value);
if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
return this;
}
return negate(); // flip sign
}
/** {@inheritDoc} */
@Override
public SparseGradient copySign(final double sign) {
final long m = Double.doubleToLongBits(value);
final long s = Double.doubleToLongBits(sign);
if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
return this;
}
return negate(); // flip sign
}
/** {@inheritDoc} */
@Override
public SparseGradient scalb(final int n) {
final SparseGradient out = new SparseGradient(FastMath.scalb(value, n), Collections.<Integer, Double> emptyMap());
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
out.derivatives.put(entry.getKey(), FastMath.scalb(entry.getValue(), n));
}
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient hypot(final SparseGradient y) {
if (Double.isInfinite(value) || Double.isInfinite(y.value)) {
return createConstant(Double.POSITIVE_INFINITY);
} else if (Double.isNaN(value) || Double.isNaN(y.value)) {
return createConstant(Double.NaN);
} else {
final int expX = FastMath.getExponent(value);
final int expY = FastMath.getExponent(y.value);
if (expX > expY + 27) {
// y is negligible with respect to x
return abs();
} else if (expY > expX + 27) {
// x is negligible with respect to y
return y.abs();
} else {
// find an intermediate scale to avoid both overflow and underflow
final int middleExp = (expX + expY) / 2;
// scale parameters without losing precision
final SparseGradient scaledX = scalb(-middleExp);
final SparseGradient scaledY = y.scalb(-middleExp);
// compute scaled hypotenuse
final SparseGradient scaledH =
scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt();
// remove scaling
return scaledH.scalb(middleExp);
}
}
}
/**
* Returns the hypotenuse of a triangle with sides {@code x} and {@code y}
* - sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
* avoiding intermediate overflow or underflow.
*
* <ul>
* <li> If either argument is infinite, then the result is positive infinity.</li>
* <li> else, if either argument is NaN then the result is NaN.</li>
* </ul>
*
* @param x a value
* @param y a value
* @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
*/
public static SparseGradient hypot(final SparseGradient x, final SparseGradient y) {
return x.hypot(y);
}
/** {@inheritDoc} */
@Override
public SparseGradient reciprocal() {
return new SparseGradient(1.0 / value, -1.0 / (value * value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient sqrt() {
final double sqrt = FastMath.sqrt(value);
return new SparseGradient(sqrt, 0.5 / sqrt, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient cbrt() {
final double cbrt = FastMath.cbrt(value);
return new SparseGradient(cbrt, 1.0 / (3 * cbrt * cbrt), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient rootN(final int n) {
if (n == 2) {
return sqrt();
} else if (n == 3) {
return cbrt();
} else {
final double root = FastMath.pow(value, 1.0 / n);
return new SparseGradient(root, 1.0 / (n * FastMath.pow(root, n - 1)), derivatives);
}
}
/** {@inheritDoc} */
@Override
public SparseGradient pow(final double p) {
return new SparseGradient(FastMath.pow(value, p), p * FastMath.pow(value, p - 1), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient pow(final int n) {
if (n == 0) {
return getField().getOne();
} else {
final double valueNm1 = FastMath.pow(value, n - 1);
return new SparseGradient(value * valueNm1, n * valueNm1, derivatives);
}
}
/** {@inheritDoc} */
@Override
public SparseGradient pow(final SparseGradient e) {
return log().multiply(e).exp();
}
/** Compute a<sup>x</sup> where a is a double and x a {@link SparseGradient}
* @param a number to exponentiate
* @param x power to apply
* @return a<sup>x</sup>
*/
public static SparseGradient pow(final double a, final SparseGradient x) {
if (a == 0) {
if (x.value == 0) {
return x.compose(1.0, Double.NEGATIVE_INFINITY);
} else if (x.value < 0) {
return x.compose(Double.NaN, Double.NaN);
} else {
return x.getField().getZero();
}
} else {
final double ax = FastMath.pow(a, x.value);
return new SparseGradient(ax, ax * FastMath.log(a), x.derivatives);
}
}
/** {@inheritDoc} */
@Override
public SparseGradient exp() {
final double e = FastMath.exp(value);
return new SparseGradient(e, e, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient expm1() {
return new SparseGradient(FastMath.expm1(value), FastMath.exp(value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient log() {
return new SparseGradient(FastMath.log(value), 1.0 / value, derivatives);
}
/** Base 10 logarithm.
* @return base 10 logarithm of the instance
*/
@Override
public SparseGradient log10() {
return new SparseGradient(FastMath.log10(value), 1.0 / (FastMath.log(10.0) * value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient log1p() {
return new SparseGradient(FastMath.log1p(value), 1.0 / (1.0 + value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient cos() {
return new SparseGradient(FastMath.cos(value), -FastMath.sin(value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient sin() {
return new SparseGradient(FastMath.sin(value), FastMath.cos(value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient tan() {
final double t = FastMath.tan(value);
return new SparseGradient(t, 1 + t * t, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient acos() {
return new SparseGradient(FastMath.acos(value), -1.0 / FastMath.sqrt(1 - value * value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient asin() {
return new SparseGradient(FastMath.asin(value), 1.0 / FastMath.sqrt(1 - value * value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient atan() {
return new SparseGradient(FastMath.atan(value), 1.0 / (1 + value * value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient atan2(final SparseGradient x) {
// compute r = sqrt(x^2+y^2)
final SparseGradient r = multiply(this).add(x.multiply(x)).sqrt();
final SparseGradient a;
if (x.value >= 0) {
// compute atan2(y, x) = 2 atan(y / (r + x))
a = divide(r.add(x)).atan().multiply(2);
} else {
// compute atan2(y, x) = +/- pi - 2 atan(y / (r - x))
final SparseGradient tmp = divide(r.subtract(x)).atan().multiply(-2);
a = tmp.add(tmp.value <= 0 ? -FastMath.PI : FastMath.PI);
}
// fix value to take special cases (+0/+0, +0/-0, -0/+0, -0/-0, +/-infinity) correctly
a.value = FastMath.atan2(value, x.value);
return a;
}
/** Two arguments arc tangent operation.
* @param y first argument of the arc tangent
* @param x second argument of the arc tangent
* @return atan2(y, x)
*/
public static SparseGradient atan2(final SparseGradient y, final SparseGradient x) {
return y.atan2(x);
}
/** {@inheritDoc} */
@Override
public SparseGradient cosh() {
return new SparseGradient(FastMath.cosh(value), FastMath.sinh(value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient sinh() {
return new SparseGradient(FastMath.sinh(value), FastMath.cosh(value), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient tanh() {
final double t = FastMath.tanh(value);
return new SparseGradient(t, 1 - t * t, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient acosh() {
return new SparseGradient(FastMath.acosh(value), 1.0 / FastMath.sqrt(value * value - 1.0), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient asinh() {
return new SparseGradient(FastMath.asinh(value), 1.0 / FastMath.sqrt(value * value + 1.0), derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient atanh() {
return new SparseGradient(FastMath.atanh(value), 1.0 / (1.0 - value * value), derivatives);
}
/** Convert radians to degrees, with error of less than 0.5 ULP
* @return instance converted into degrees
*/
public SparseGradient toDegrees() {
return new SparseGradient(FastMath.toDegrees(value), FastMath.toDegrees(1.0), derivatives);
}
/** Convert degrees to radians, with error of less than 0.5 ULP
* @return instance converted into radians
*/
public SparseGradient toRadians() {
return new SparseGradient(FastMath.toRadians(value), FastMath.toRadians(1.0), derivatives);
}
/** Evaluate Taylor expansion of a sparse gradient.
* @param delta parameters offsets (&Delta;x, &Delta;y, ...)
* @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
*/
public double taylor(final double ... delta) {
double y = value;
for (int i = 0; i < delta.length; ++i) {
y += delta[i] * getDerivative(i);
}
return y;
}
/** Compute composition of the instance by a univariate function.
* @param f0 value of the function at (i.e. f({@link #getValue()}))
* @param f1 first derivative of the function at
* the current point (i.e. f'({@link #getValue()}))
* @return f(this)
*/
public SparseGradient compose(final double f0, final double f1) {
return new SparseGradient(f0, f1, derivatives);
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final SparseGradient[] a,
final SparseGradient[] b)
throws DimensionMismatchException {
// compute a simple value, with all partial derivatives
SparseGradient out = a[0].getField().getZero();
for (int i = 0; i < a.length; ++i) {
out = out.add(a[i].multiply(b[i]));
}
// recompute an accurate value, taking care of cancellations
final double[] aDouble = new double[a.length];
for (int i = 0; i < a.length; ++i) {
aDouble[i] = a[i].getValue();
}
final double[] bDouble = new double[b.length];
for (int i = 0; i < b.length; ++i) {
bDouble[i] = b[i].getValue();
}
out.value = LinearCombination.value(aDouble, bDouble);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final double[] a, final SparseGradient[] b) {
// compute a simple value, with all partial derivatives
SparseGradient out = b[0].getField().getZero();
for (int i = 0; i < a.length; ++i) {
out = out.add(b[i].multiply(a[i]));
}
// recompute an accurate value, taking care of cancellations
final double[] bDouble = new double[b.length];
for (int i = 0; i < b.length; ++i) {
bDouble[i] = b[i].getValue();
}
out.value = LinearCombination.value(a, bDouble);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
final SparseGradient a2, final SparseGradient b2) {
// compute a simple value, with all partial derivatives
SparseGradient out = a1.multiply(b1).add(a2.multiply(b2));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1.value, b1.value, a2.value, b2.value);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final double a1, final SparseGradient b1,
final double a2, final SparseGradient b2) {
// compute a simple value, with all partial derivatives
SparseGradient out = b1.multiply(a1).add(b2.multiply(a2));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1, b1.value, a2, b2.value);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
final SparseGradient a2, final SparseGradient b2,
final SparseGradient a3, final SparseGradient b3) {
// compute a simple value, with all partial derivatives
SparseGradient out = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1.value, b1.value,
a2.value, b2.value,
a3.value, b3.value);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final double a1, final SparseGradient b1,
final double a2, final SparseGradient b2,
final double a3, final SparseGradient b3) {
// compute a simple value, with all partial derivatives
SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1, b1.value,
a2, b2.value,
a3, b3.value);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final SparseGradient a1, final SparseGradient b1,
final SparseGradient a2, final SparseGradient b2,
final SparseGradient a3, final SparseGradient b3,
final SparseGradient a4, final SparseGradient b4) {
// compute a simple value, with all partial derivatives
SparseGradient out = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1.value, b1.value,
a2.value, b2.value,
a3.value, b3.value,
a4.value, b4.value);
return out;
}
/** {@inheritDoc} */
@Override
public SparseGradient linearCombination(final double a1, final SparseGradient b1,
final double a2, final SparseGradient b2,
final double a3, final SparseGradient b3,
final double a4, final SparseGradient b4) {
// compute a simple value, with all partial derivatives
SparseGradient out = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4));
// recompute an accurate value, taking care of cancellations
out.value = LinearCombination.value(a1, b1.value,
a2, b2.value,
a3, b3.value,
a4, b4.value);
return out;
}
/**
* Test for the equality of two sparse gradients.
* <p>
* Sparse gradients are considered equal if they have the same value
* and the same derivatives.
* </p>
* @param other Object to test for equality to this
* @return true if two sparse gradients are equal
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof SparseGradient) {
final SparseGradient rhs = (SparseGradient)other;
if (!Precision.equals(value, rhs.value, 1)) {
return false;
}
if (derivatives.size() != rhs.derivatives.size()) {
return false;
}
for (final Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
if (!rhs.derivatives.containsKey(entry.getKey())) {
return false;
}
if (!Precision.equals(entry.getValue(), rhs.derivatives.get(entry.getKey()), 1)) {
return false;
}
}
return true;
}
return false;
}
/**
* Get a hashCode for the derivative structure.
* @return a hash code value for this object
* @since 3.2
*/
@Override
public int hashCode() {
return 743 + 809 * MathUtils.hash(value) + 167 * derivatives.hashCode();
}
}

View File

@ -1,43 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.UnivariateFunction;
import org.apache.commons.math4.exception.DimensionMismatchException;
/** Interface for univariate functions derivatives.
* <p>This interface represents a simple function which computes
* both the value and the first derivative of a mathematical function.
* The derivative is computed with respect to the input variable.</p>
* @see UnivariateDifferentiableFunction
* @see UnivariateFunctionDifferentiator
* @since 3.1
*/
public interface UnivariateDifferentiableFunction extends UnivariateFunction {
/** Simple mathematical function.
* <p>{@link UnivariateDifferentiableFunction} classes compute both the
* value and the first derivative of the function.</p>
* @param t function input value
* @return function result
* @exception DimensionMismatchException if t is inconsistent with the
* function's free parameters or order
*/
DerivativeStructure value(DerivativeStructure t)
throws DimensionMismatchException;
}

View File

@ -1,40 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.UnivariateMatrixFunction;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
/**
* Extension of {@link UnivariateMatrixFunction} representing a univariate differentiable matrix function.
*
* @since 3.1
*/
public interface UnivariateDifferentiableMatrixFunction
extends UnivariateMatrixFunction {
/**
* Compute the value for the function.
* @param x the point for which the function value should be computed
* @return the value
* @exception MathIllegalArgumentException if {@code x} does not
* satisfy the function's constraints (argument out of bound, or unsupported
* derivative order for example)
*/
DerivativeStructure[][] value(DerivativeStructure x) throws MathIllegalArgumentException;
}

View File

@ -1,40 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.UnivariateVectorFunction;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
/**
* Extension of {@link UnivariateVectorFunction} representing a univariate differentiable vectorial function.
*
* @since 3.1
*/
public interface UnivariateDifferentiableVectorFunction
extends UnivariateVectorFunction {
/**
* Compute the value for the function.
* @param x the point for which the function value should be computed
* @return the value
* @exception MathIllegalArgumentException if {@code x} does not
* satisfy the function's constraints (argument out of bound, or unsupported
* derivative order for example)
*/
DerivativeStructure[] value(DerivativeStructure x) throws MathIllegalArgumentException;
}

View File

@ -1,33 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.UnivariateFunction;
/** Interface defining the function differentiation operation.
* @since 3.1
*/
public interface UnivariateFunctionDifferentiator {
/** Create an implementation of a {@link UnivariateDifferentiableFunction
* differential} from a regular {@link UnivariateFunction function}.
* @param function function to differentiate
* @return differential function
*/
UnivariateDifferentiableFunction differentiate(UnivariateFunction function);
}

View File

@ -1,33 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.analysis.differentiation;
import org.apache.commons.math4.analysis.UnivariateMatrixFunction;
/** Interface defining the function differentiation operation.
* @since 3.1
*/
public interface UnivariateMatrixFunctionDifferentiator {
/** Create an implementation of a {@link UnivariateDifferentiableMatrixFunction
* differential} from a regular {@link UnivariateMatrixFunction matrix function}.
* @param function function to differentiate
* @return differential function
*/
UnivariateDifferentiableMatrixFunction differentiate(UnivariateMatrixFunction function);
}

Some files were not shown because too many files have changed in this diff Show More