Skip to content

Commit 7d57c57

Browse files
authored
Added quadratic mean (#7315)
* Added quadratic mean Added a quadratic mean of the given numbers, sqrt ((n1^2+n2^2+...+nk^2)/k). * Added tests for quadratic mean * Corrected quadratic mean * Added comment to quadratic mean * Corrected quadratic mean tests * Replaced sqrt by pow * Error fixed * Extra whitespace removed * Extra whitespace removed * Removed extra white space * Removed extra white space
1 parent 24c2bea commit 7d57c57

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/main/java/com/thealgorithms/maths/Means.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ public static Double harmonic(final Iterable<Double> numbers) {
107107
return size / sumOfReciprocals;
108108
}
109109

110+
/**
111+
* Computes the quadratic mean (root mean square) of the given numbers.
112+
* <p>
113+
* The quadratic mean is calculated as: √[(x₁^2 × x₂^2 × ... × xₙ^2)/n]
114+
* </p>
115+
* <p>
116+
* Example: For numbers [1, 7], the quadratic mean is √[(1^2+7^2)/2] = √25 = 5.0
117+
* </p>
118+
*
119+
* @param numbers the input numbers (must not be empty)
120+
* @return the quadratic mean of the input numbers
121+
* @throws IllegalArgumentException if the input is empty
122+
* @see <a href="https://en.wikipedia.org/wiki/Root_mean_square">Quadratic
123+
* Mean</a>
124+
*/
125+
public static Double quadratic(final Iterable<Double> numbers) {
126+
checkIfNotEmpty(numbers);
127+
double sumOfSquares = StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + y * y);
128+
int size = IterableUtils.size(numbers);
129+
return Math.pow(sumOfSquares / size, 0.5);
130+
}
131+
110132
/**
111133
* Validates that the input iterable is not empty.
112134
*

src/test/java/com/thealgorithms/maths/MeansTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,53 @@ void testHarmonicMeanWithLinkedList() {
172172
assertEquals(expected, Means.harmonic(numbers), EPSILON);
173173
}
174174

175+
// ========== Quadratic Mean Tests ==========
176+
177+
@Test
178+
void testQuadraticMeanThrowsExceptionForEmptyList() {
179+
List<Double> numbers = new ArrayList<>();
180+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Means.quadratic(numbers));
181+
assertTrue(exception.getMessage().contains("Empty list"));
182+
}
183+
184+
@Test
185+
void testQuadraticMeanSingleNumber() {
186+
LinkedHashSet<Double> numbers = new LinkedHashSet<>(Arrays.asList(2.5));
187+
assertEquals(2.5, Means.quadratic(numbers), EPSILON);
188+
}
189+
190+
@Test
191+
void testQuadraticMeanTwoNumbers() {
192+
List<Double> numbers = Arrays.asList(1.0, 7.0);
193+
assertEquals(5.0, Means.quadratic(numbers), EPSILON);
194+
}
195+
196+
@Test
197+
void testQuadraticMeanMultipleNumbers() {
198+
Vector<Double> numbers = new Vector<>(Arrays.asList(1.0, 2.5, 3.0, 7.5, 10.0));
199+
double expected = Math.sqrt(34.5);
200+
assertEquals(expected, Means.quadratic(numbers), EPSILON);
201+
}
202+
203+
@Test
204+
void testQuadraticMeanThreeNumbers() {
205+
List<Double> numbers = Arrays.asList(3.0, 6.0, 9.0);
206+
double expected = Math.sqrt(42.0);
207+
assertEquals(expected, Means.quadratic(numbers), EPSILON);
208+
}
209+
210+
@Test
211+
void testQuadraticMeanIdenticalNumbers() {
212+
List<Double> numbers = Arrays.asList(5.0, 5.0, 5.0);
213+
assertEquals(5.0, Means.quadratic(numbers), EPSILON);
214+
}
215+
216+
@Test
217+
void testQuadraticMeanWithLinkedList() {
218+
LinkedList<Double> numbers = new LinkedList<>(Arrays.asList(1.0, 5.0, 11.0));
219+
assertEquals(7.0, Means.quadratic(numbers), EPSILON);
220+
}
221+
175222
// ========== Additional Edge Case Tests ==========
176223

177224
@Test
@@ -198,21 +245,25 @@ void testAllMeansConsistencyForIdenticalValues() {
198245
double arithmetic = Means.arithmetic(numbers);
199246
double geometric = Means.geometric(numbers);
200247
double harmonic = Means.harmonic(numbers);
248+
double quadratic = Means.quadratic(numbers);
201249

202250
assertEquals(7.5, arithmetic, EPSILON);
203251
assertEquals(7.5, geometric, EPSILON);
204252
assertEquals(7.5, harmonic, EPSILON);
253+
assertEquals(7.5, quadratic, EPSILON);
205254
}
206255

207256
@Test
208257
void testMeansRelationship() {
209-
// For positive numbers, harmonic mean ≤ geometric mean ≤ arithmetic mean
258+
// For positive numbers, harmonic mean ≤ geometric mean ≤ arithmetic mean ≤ quadratic mean
210259
List<Double> numbers = Arrays.asList(2.0, 4.0, 8.0);
211260
double arithmetic = Means.arithmetic(numbers);
212261
double geometric = Means.geometric(numbers);
213262
double harmonic = Means.harmonic(numbers);
263+
double quadratic = Means.quadratic(numbers);
214264

215265
assertTrue(harmonic <= geometric, "Harmonic mean should be ≤ geometric mean");
216266
assertTrue(geometric <= arithmetic, "Geometric mean should be ≤ arithmetic mean");
267+
assertTrue(arithmetic <= quadratic, "Arithmetic mean should be ≤ quadratic mean");
217268
}
218269
}

0 commit comments

Comments
 (0)