Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/com/thealgorithms/maths/Means.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ public static Double harmonic(final Iterable<Double> numbers) {
return size / sumOfReciprocals;
}

/**
* Computes the quadratic mean (root mean square) of the given numbers.
* <p>
* The quadratic mean is calculated as: √[(x₁^2 × x₂^2 × ... × xₙ^2)/n]
* </p>
* <p>
* Example: For numbers [1, 7], the quadratic mean is √[(1^2+7^2)/2] = √25 = 5.0
* </p>
*
* @param numbers the input numbers (must not be empty)
* @return the quadratic mean of the input numbers
* @throws IllegalArgumentException if the input is empty
* @see <a href="https://en.wikipedia.org/wiki/Root_mean_square">Quadratic
* Mean</a>
*/
public static Double quadratic(final Iterable<Double> numbers) {
checkIfNotEmpty(numbers);
double sumOfSquares = StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + y * y);
int size = IterableUtils.size(numbers);
return Math.pow(sumOfSquares / size, 0.5);
}

/**
* Validates that the input iterable is not empty.
*
Expand Down
53 changes: 52 additions & 1 deletion src/test/java/com/thealgorithms/maths/MeansTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,53 @@ void testHarmonicMeanWithLinkedList() {
assertEquals(expected, Means.harmonic(numbers), EPSILON);
}

// ========== Quadratic Mean Tests ==========

@Test
void testQuadraticMeanThrowsExceptionForEmptyList() {
List<Double> numbers = new ArrayList<>();
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Means.quadratic(numbers));
assertTrue(exception.getMessage().contains("Empty list"));
}

@Test
void testQuadraticMeanSingleNumber() {
LinkedHashSet<Double> numbers = new LinkedHashSet<>(Arrays.asList(2.5));
assertEquals(2.5, Means.quadratic(numbers), EPSILON);
}

@Test
void testQuadraticMeanTwoNumbers() {
List<Double> numbers = Arrays.asList(1.0, 7.0);
assertEquals(5.0, Means.quadratic(numbers), EPSILON);
}

@Test
void testQuadraticMeanMultipleNumbers() {
Vector<Double> numbers = new Vector<>(Arrays.asList(1.0, 2.5, 3.0, 7.5, 10.0));
double expected = Math.sqrt(34.5);
assertEquals(expected, Means.quadratic(numbers), EPSILON);
}

@Test
void testQuadraticMeanThreeNumbers() {
List<Double> numbers = Arrays.asList(3.0, 6.0, 9.0);
double expected = Math.sqrt(42.0);
assertEquals(expected, Means.quadratic(numbers), EPSILON);
}

@Test
void testQuadraticMeanIdenticalNumbers() {
List<Double> numbers = Arrays.asList(5.0, 5.0, 5.0);
assertEquals(5.0, Means.quadratic(numbers), EPSILON);
}

@Test
void testQuadraticMeanWithLinkedList() {
LinkedList<Double> numbers = new LinkedList<>(Arrays.asList(1.0, 5.0, 11.0));
assertEquals(7.0, Means.quadratic(numbers), EPSILON);
}

// ========== Additional Edge Case Tests ==========

@Test
Expand All @@ -198,21 +245,25 @@ void testAllMeansConsistencyForIdenticalValues() {
double arithmetic = Means.arithmetic(numbers);
double geometric = Means.geometric(numbers);
double harmonic = Means.harmonic(numbers);
double quadratic = Means.quadratic(numbers);

assertEquals(7.5, arithmetic, EPSILON);
assertEquals(7.5, geometric, EPSILON);
assertEquals(7.5, harmonic, EPSILON);
assertEquals(7.5, quadratic, EPSILON);
}

@Test
void testMeansRelationship() {
// For positive numbers, harmonic mean ≤ geometric mean ≤ arithmetic mean
// For positive numbers, harmonic mean ≤ geometric mean ≤ arithmetic mean ≤ quadratic mean
List<Double> numbers = Arrays.asList(2.0, 4.0, 8.0);
double arithmetic = Means.arithmetic(numbers);
double geometric = Means.geometric(numbers);
double harmonic = Means.harmonic(numbers);
double quadratic = Means.quadratic(numbers);

assertTrue(harmonic <= geometric, "Harmonic mean should be ≤ geometric mean");
assertTrue(geometric <= arithmetic, "Geometric mean should be ≤ arithmetic mean");
assertTrue(arithmetic <= quadratic, "Arithmetic mean should be ≤ quadratic mean");
}
}
Loading