mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add different types of Mean (#4339)
This commit is contained in:
5
pom.xml
5
pom.xml
@ -52,6 +52,11 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
54
src/main/java/com/thealgorithms/maths/Means.java
Normal file
54
src/main/java/com/thealgorithms/maths/Means.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.util.stream.StreamSupport;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Mean
|
||||
* <p>
|
||||
* by: Punit Patel
|
||||
*/
|
||||
public final class Means {
|
||||
|
||||
private Means() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief computes the [Arithmetic Mean](https://en.wikipedia.org/wiki/Arithmetic_mean) of the input
|
||||
* @param numbers the input numbers
|
||||
* @throws IllegalArgumentException empty input
|
||||
* @return the arithmetic mean of the input numbers
|
||||
*/
|
||||
public static Double arithmetic(final Iterable<Double> numbers) {
|
||||
checkIfNotEmpty(numbers);
|
||||
return StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x + y).get() / IterableUtils.size(numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief computes the [Geometric Mean](https://en.wikipedia.org/wiki/Geometric_mean) of the input
|
||||
* @param numbers the input numbers
|
||||
* @throws IllegalArgumentException empty input
|
||||
* @return the geometric mean of the input numbers
|
||||
*/
|
||||
public static Double geometric(final Iterable<Double> numbers) {
|
||||
checkIfNotEmpty(numbers);
|
||||
return Math.pow(StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x * y).get(), 1d / IterableUtils.size(numbers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief computes the [Harmonic Mean](https://en.wikipedia.org/wiki/Harmonic_mean) of the input
|
||||
* @param numbers the input numbers
|
||||
* @throws IllegalArgumentException empty input
|
||||
* @return the harmonic mean of the input numbers
|
||||
*/
|
||||
public static Double harmonic(final Iterable<Double> numbers) {
|
||||
checkIfNotEmpty(numbers);
|
||||
return IterableUtils.size(numbers) / StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + 1d / y);
|
||||
}
|
||||
|
||||
private static void checkIfNotEmpty(final Iterable<Double> numbers) {
|
||||
if (!numbers.iterator().hasNext()) {
|
||||
throw new IllegalArgumentException("Emtpy list given for Mean computation.");
|
||||
}
|
||||
}
|
||||
}
|
71
src/test/java/com/thealgorithms/maths/MeansTest.java
Normal file
71
src/test/java/com/thealgorithms/maths/MeansTest.java
Normal file
@ -0,0 +1,71 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.assertj.core.util.Sets;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MeansTest {
|
||||
|
||||
@Test
|
||||
void arithmeticMeanZeroNumbers() throws IllegalArgumentException {
|
||||
List<Double> numbers = new ArrayList<>();
|
||||
assertThrows(IllegalArgumentException.class, () -> Means.arithmetic(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void geometricMeanZeroNumbers() throws IllegalArgumentException {
|
||||
List<Double> numbers = new ArrayList<>();
|
||||
assertThrows(IllegalArgumentException.class, () -> Means.geometric(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void harmonicMeanZeroNumbers() throws IllegalArgumentException {
|
||||
List<Double> numbers = new ArrayList<>();
|
||||
assertThrows(IllegalArgumentException.class, () -> Means.harmonic(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void arithmeticMeanSingleNumber() {
|
||||
List<Double> numbers = Lists.newArrayList(2.5);
|
||||
assertEquals(2.5, Means.arithmetic(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void geometricMeanSingleNumber() {
|
||||
Set<Double> numbers = Sets.newHashSet(Lists.newArrayList(2.5));
|
||||
assertEquals(2.5, Means.geometric(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void harmonicMeanSingleNumber() {
|
||||
LinkedHashSet<Double> numbers = Sets.newLinkedHashSet(2.5);
|
||||
assertEquals(2.5, Means.harmonic(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void arithmeticMeanMultipleNumbers() {
|
||||
Set<Double> numbers = Sets.newTreeSet(1d, 2.5, 83.3, 25.9999, 46.0001, 74.7, 74.5);
|
||||
assertEquals(44, Means.arithmetic(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void geometricMeanMultipleNumbers() {
|
||||
LinkedList<Double> numbers = new LinkedList<>() {};
|
||||
numbers.addAll(Lists.newArrayList(1d, 2d, 3d, 4d, 5d, 6d, 1.25));
|
||||
assertEquals(2.6426195539300585, Means.geometric(numbers));
|
||||
}
|
||||
|
||||
@Test
|
||||
void harmonicMeanMultipleNumbers() {
|
||||
Vector<Double> numbers = new Vector<>(Lists.newArrayList(1d, 2.5, 83.3, 25.9999, 46.0001, 74.7, 74.5));
|
||||
assertEquals(4.6697322801074135, Means.harmonic(numbers));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user