Skip to content

Commit

Permalink
Added sqrt to DoubleArray
Browse files Browse the repository at this point in the history
  • Loading branch information
heySourabh committed Jan 25, 2024
1 parent 0fc3095 commit 4d7d4c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/util/DoubleArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static double[] sqr(double[] array) {
return apply(array, e -> e * e);
}

public static double[] sqrt(double[] array) {return apply(array, Math::sqrt);}

public static double[] apply(double[] a1, double[] a2, DoubleBinaryOperator function) {
double[] result = new double[a1.length];

Expand Down
16 changes: 14 additions & 2 deletions test/main/util/DoubleArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

import static main.util.TestHelper.assertThrows;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class DoubleArrayTest {

Expand Down Expand Up @@ -126,4 +126,16 @@ public void dot_product_of_two_vectors() {

assertEquals(dotProduct, DoubleArray.dot(v1, v2), 1e-15);
}

@Test
public void test_sqr() {
double[] v = {5, 10, 1, 3, 2, 1.2};
assertArrayEquals(new double[]{25, 100, 1, 9, 4, 1.44}, DoubleArray.sqr(v), 1e-15);
}

@Test
public void test_sqrt() {
double[] v = {25, 100, 9, 4};
assertArrayEquals(new double[]{5, 10, 3, 2}, DoubleArray.sqrt(v), 1e-15);
}
}

0 comments on commit 4d7d4c4

Please sign in to comment.