Skip to content

Commit

Permalink
Google Java Format
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jun 26, 2024
1 parent d9008b8 commit 3fb50ca
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 85 deletions.
5 changes: 3 additions & 2 deletions src/main/java/LA/ComplexNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ public ComplexNumber realScalarMultiply(double scalar) {
* @return The result
*/
public ComplexNumber divide(ComplexNumber z) {
return this.multiply(z.multiplicativeInverse().orElseThrow(() -> new IllegalArgumentException("Illegal division")));
return this.multiply(
z.multiplicativeInverse()
.orElseThrow(() -> new IllegalArgumentException("Illegal division")));
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/LA/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public static Vector addVectors(Vector v1, Vector v2) {
int size = v1.size();
ComplexNumber[] result = new ComplexNumber[size];

for (int i=0; i < size; i++) {
result[i] = v1.getComponent(i+1).add(v2.getComponent(i+1));
for (int i = 0; i < size; i++) {
result[i] = v1.getComponent(i + 1).add(v2.getComponent(i + 1));
}

return new Vector(result);
Expand Down
170 changes: 95 additions & 75 deletions src/test/java/LA/ComplexNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void whenInstantiatedInPolarForm_thenHasCorrectRectangularCoords() {
assertEquals(0, Math.round(z6.realPart()));
assertEquals(-1, z6.imaginaryPart(), 0.01);
}

@Test
public void whenInstantiatedInRectangularForm_thenHasCorrectArgument() {
ComplexNumber z1 = ComplexNumber.rectangular(1, 1);
Expand Down Expand Up @@ -72,117 +73,132 @@ public void testProductOfComplexNumbers() {
assertEquals(10, Math.round(ComplexNumber.product(z1, z3).imaginaryPart()));
assertEquals(0, Math.round(ComplexNumber.product(z1, z2, ComplexNumber.ZERO, z3).realPart()));
assertEquals(
0, Math.round(ComplexNumber.product(z1, z2, ComplexNumber.ZERO, z3).imaginaryPart()));
0, Math.round(ComplexNumber.product(z1, z2, ComplexNumber.ZERO, z3).imaginaryPart()));
}

@Test
public void addComplexNumbers() {
assertEquals(ComplexNumber.rectangular(5,10), ComplexNumber.rectangular(3,9)
.add(ComplexNumber.rectangular(2,1)));
assertEquals(ComplexNumber.rectangular(5,10), ComplexNumber.rectangular(9,9)
.add(ComplexNumber.rectangular(-4,1)));
assertEquals(ComplexNumber.rectangular(5,10), ComplexNumber.ADDITIVE_IDENTITY
.add(ComplexNumber.rectangular(5,10)));
assertEquals(ComplexNumber.rectangular(6,12), ComplexNumber.ADDITIVE_IDENTITY
.add(ComplexNumber.rectangular(1,2))
.add(ComplexNumber.rectangular(2,4))
.add(ComplexNumber.rectangular(3,6)));
assertEquals(
ComplexNumber.rectangular(5, 10),
ComplexNumber.rectangular(3, 9).add(ComplexNumber.rectangular(2, 1)));
assertEquals(
ComplexNumber.rectangular(5, 10),
ComplexNumber.rectangular(9, 9).add(ComplexNumber.rectangular(-4, 1)));
assertEquals(
ComplexNumber.rectangular(5, 10),
ComplexNumber.ADDITIVE_IDENTITY.add(ComplexNumber.rectangular(5, 10)));
assertEquals(
ComplexNumber.rectangular(6, 12),
ComplexNumber.ADDITIVE_IDENTITY
.add(ComplexNumber.rectangular(1, 2))
.add(ComplexNumber.rectangular(2, 4))
.add(ComplexNumber.rectangular(3, 6)));
}

@Test
public void subtractComplexNumbers() {
assertEquals(ComplexNumber.rectangular(1,8), ComplexNumber.rectangular(3,9)
.subtract(ComplexNumber.rectangular(2,1)));
assertEquals(ComplexNumber.rectangular(13,8), ComplexNumber.rectangular(9,9)
.subtract(ComplexNumber.rectangular(-4,1)));
assertEquals(ComplexNumber.rectangular(-5,-10), ComplexNumber.ADDITIVE_IDENTITY
.subtract(ComplexNumber.rectangular(5,10)));
assertEquals(ComplexNumber.rectangular(-6,-12), ComplexNumber.ADDITIVE_IDENTITY
.subtract(ComplexNumber.rectangular(1,2))
.subtract(ComplexNumber.rectangular(2,4))
.subtract(ComplexNumber.rectangular(3,6)));
assertEquals(
ComplexNumber.rectangular(1, 8),
ComplexNumber.rectangular(3, 9).subtract(ComplexNumber.rectangular(2, 1)));
assertEquals(
ComplexNumber.rectangular(13, 8),
ComplexNumber.rectangular(9, 9).subtract(ComplexNumber.rectangular(-4, 1)));
assertEquals(
ComplexNumber.rectangular(-5, -10),
ComplexNumber.ADDITIVE_IDENTITY.subtract(ComplexNumber.rectangular(5, 10)));
assertEquals(
ComplexNumber.rectangular(-6, -12),
ComplexNumber.ADDITIVE_IDENTITY
.subtract(ComplexNumber.rectangular(1, 2))
.subtract(ComplexNumber.rectangular(2, 4))
.subtract(ComplexNumber.rectangular(3, 6)));

// Mix of both
assertEquals(ComplexNumber.rectangular(-2,-4), ComplexNumber.ADDITIVE_IDENTITY
.subtract(ComplexNumber.rectangular(1,2))
.add(ComplexNumber.rectangular(2,4))
.subtract(ComplexNumber.rectangular(3,6)));
assertEquals(
ComplexNumber.rectangular(-2, -4),
ComplexNumber.ADDITIVE_IDENTITY
.subtract(ComplexNumber.rectangular(1, 2))
.add(ComplexNumber.rectangular(2, 4))
.subtract(ComplexNumber.rectangular(3, 6)));
}

@Test
public void testMultiplyComplexNumbers() {
// Test Case 1: (1 + 8i) * (3 + 9i)
assertEquals(ComplexNumber.rectangular(-69, 33),
ComplexNumber.rectangular(1, 8)
.multiply(ComplexNumber.rectangular(3, 9)));
assertEquals(
ComplexNumber.rectangular(-69, 33),
ComplexNumber.rectangular(1, 8).multiply(ComplexNumber.rectangular(3, 9)));

// Test Case 2: (9 + 9i) * (-4 + 1i)
assertEquals(ComplexNumber.rectangular(-45, -27),
ComplexNumber.rectangular(9, 9)
.multiply(ComplexNumber.rectangular(-4, 1)));
assertEquals(
ComplexNumber.rectangular(-45, -27),
ComplexNumber.rectangular(9, 9).multiply(ComplexNumber.rectangular(-4, 1)));

// Test Case 3: 0 * (5 + 10i)
assertEquals(ComplexNumber.ADDITIVE_IDENTITY,
ComplexNumber.ADDITIVE_IDENTITY
.multiply(ComplexNumber.rectangular(5, 10)));
assertEquals(
ComplexNumber.ADDITIVE_IDENTITY,
ComplexNumber.ADDITIVE_IDENTITY.multiply(ComplexNumber.rectangular(5, 10)));

// Test Case 4: (1 + 2i) * (2 + 4i) * (3 + 6i)
assertEquals(ComplexNumber.rectangular(-66, -12),
ComplexNumber.rectangular(1, 2)
.multiply(ComplexNumber.rectangular(2, 4))
.multiply(ComplexNumber.rectangular(3, 6)));
assertEquals(
ComplexNumber.rectangular(-66, -12),
ComplexNumber.rectangular(1, 2)
.multiply(ComplexNumber.rectangular(2, 4))
.multiply(ComplexNumber.rectangular(3, 6)));
}

@Test
public void testRealScalarMultiply() {
// Test Case 1: (1 + 8i) * 2
assertEquals(ComplexNumber.rectangular(2, 16),
ComplexNumber.rectangular(1, 8)
.realScalarMultiply(2));
assertEquals(
ComplexNumber.rectangular(2, 16), ComplexNumber.rectangular(1, 8).realScalarMultiply(2));

// Test Case 2: (9 + 9i) * -1
assertEquals(ComplexNumber.rectangular(-9, -9),
ComplexNumber.rectangular(9, 9)
.realScalarMultiply(-1));
assertEquals(
ComplexNumber.rectangular(-9, -9),
ComplexNumber.rectangular(9, 9).realScalarMultiply(-1));

// Test Case 3: (5 + 10i) * 0
assertEquals(ComplexNumber.ADDITIVE_IDENTITY,
ComplexNumber.rectangular(5, 10)
.realScalarMultiply(0));
assertEquals(
ComplexNumber.ADDITIVE_IDENTITY, ComplexNumber.rectangular(5, 10).realScalarMultiply(0));

// Test Case 4: (1 + 2i) * 3
assertEquals(ComplexNumber.rectangular(3, 6),
ComplexNumber.rectangular(1, 2)
.realScalarMultiply(3));
assertEquals(
ComplexNumber.rectangular(3, 6), ComplexNumber.rectangular(1, 2).realScalarMultiply(3));
}

@Test
public void testDivide() {
// Test Case 1: (1 + 8i) / (3 + 9i)
assertEquals(ComplexNumber.rectangular(5.0/6, 1.0/6),
ComplexNumber.rectangular(1, 8)
.divide(ComplexNumber.rectangular(3, 9)));
assertEquals(
ComplexNumber.rectangular(5.0 / 6, 1.0 / 6),
ComplexNumber.rectangular(1, 8).divide(ComplexNumber.rectangular(3, 9)));

// Test Case 2: (5 + 10i) / (1 + 0i)
assertEquals(ComplexNumber.rectangular(5, 10),
ComplexNumber.rectangular(5, 10)
.divide(ComplexNumber.rectangular(1, 0)));
assertEquals(
ComplexNumber.rectangular(5, 10),
ComplexNumber.rectangular(5, 10).divide(ComplexNumber.rectangular(1, 0)));

// Test Case 3: (1 + 2i) / (2 + 4i)
assertEquals(ComplexNumber.rectangular(0.5, 0),
ComplexNumber.rectangular(1, 2)
.divide(ComplexNumber.rectangular(2, 4)));
assertEquals(
ComplexNumber.rectangular(0.5, 0),
ComplexNumber.rectangular(1, 2).divide(ComplexNumber.rectangular(2, 4)));

// Test Case 4: (4 + 2i) / (2 + 1i)
assertEquals(ComplexNumber.rectangular(2, 0),
ComplexNumber.rectangular(4, 2)
.divide(ComplexNumber.rectangular(2, 1)));
assertEquals(
ComplexNumber.rectangular(2, 0),
ComplexNumber.rectangular(4, 2).divide(ComplexNumber.rectangular(2, 1)));

// Test Case 5: (10 + 5i) / (2 + 1i)
assertEquals(ComplexNumber.rectangular(5, 0),
ComplexNumber.rectangular(10, 5)
.divide(ComplexNumber.rectangular(2, 1)));
assertEquals(
ComplexNumber.rectangular(5, 0),
ComplexNumber.rectangular(10, 5).divide(ComplexNumber.rectangular(2, 1)));

assertThrows(IllegalArgumentException.class, () -> ComplexNumber.real(1).divide(ComplexNumber.ADDITIVE_IDENTITY));
assertThrows(
IllegalArgumentException.class,
() -> ComplexNumber.real(1).divide(ComplexNumber.ADDITIVE_IDENTITY));
}

@Test
public void testSumOfComplexNumbers() {
var z1 = ComplexNumber.rectangular(1, 2);
Expand All @@ -191,7 +207,8 @@ public void testSumOfComplexNumbers() {
assert (ComplexNumber.rectangular(6, 9).equals(ComplexNumber.sum(z1, z2, z3)));
assert (ComplexNumber.rectangular(3, 5).equals(ComplexNumber.sum(z1, z2)));
assert (ComplexNumber.rectangular(4, 6).equals(ComplexNumber.sum(z1, z3)));
assert (ComplexNumber.rectangular(4, 6).equals(ComplexNumber.sum(z1, z3, ComplexNumber.ZERO)));
assert (ComplexNumber.rectangular(4, 6)
.equals(ComplexNumber.sum(z1, z3, ComplexNumber.ZERO)));
assert (ComplexNumber.ZERO.equals(ComplexNumber.sum(ComplexNumber.ZERO)));
}
}
Expand All @@ -210,15 +227,16 @@ public void testSqrtOfComplexNumbers() {
assert (ComplexNumber.rectangular(1, 2).equals(ComplexNumber.rectangular(-3, 4).sqrt()));
assert (ComplexNumber.rectangular(0, 3).equals(ComplexNumber.rectangular(-9, 0).sqrt()));
assert (ComplexNumber.rectangular(Math.sqrt(2), 0))
.equals((ComplexNumber.rectangular(2, 0).sqrt()));
.equals((ComplexNumber.rectangular(2, 0).sqrt()));
assert (ComplexNumber.rectangular(8, -6).equals(ComplexNumber.rectangular(28, -96).sqrt()));
assert (ComplexNumber.rectangular(24, 63)
.equals(ComplexNumber.rectangular(-3393, 3024).sqrt()));
.equals(ComplexNumber.rectangular(-3393, 3024).sqrt()));
assert (ComplexNumber.rectangular(0.24, 0.5)
.equals(ComplexNumber.rectangular(-0.1924, 0.24).sqrt()));
.equals(ComplexNumber.rectangular(-0.1924, 0.24).sqrt()));
// Note tried a test with -2-7i but this will never happen when returning +ve sqrt
assert (ComplexNumber.rectangular(2, 7).equals(ComplexNumber.rectangular(-45, 28).sqrt()));
}

@SuppressWarnings("EqualsBetweenInconvertibleTypes")
@Test
public void testEquals() {
Expand All @@ -228,7 +246,8 @@ public void testEquals() {
assert (b.equals(ComplexNumber.rectangular(4.6, 1)));
assert (!b.equals(new ArrayList<String>()));

// Problem with rounding when comparing - these 2 are identical but doesn't match due to way of implementation
// Problem with rounding when comparing - these 2 are identical but doesn't match due to way of
// implementation
assertEquals(3, Math.round(ComplexNumber.polar(5, Math.atan(4.0 / 3.0)).realPart()));
assertEquals(4, Math.round(ComplexNumber.polar(5, Math.atan(4.0 / 3.0)).imaginaryPart()));
}
Expand All @@ -248,7 +267,8 @@ void multiplicativeInverse_complexNumber() {
ComplexNumber z = ComplexNumber.rectangular(1, 2); // Complex number 1 + 2i
ComplexNumber result = z.multiplicativeInverse().orElseThrow();

assertEquals(ComplexNumber.rectangular(0.2, -0.4), result); // Expected: 1/(1+2i) = (1/5) - (2/5)i
assertEquals(
ComplexNumber.rectangular(0.2, -0.4), result); // Expected: 1/(1+2i) = (1/5) - (2/5)i
}

@Test
Expand All @@ -263,9 +283,9 @@ void multiplicativeInverse_largeNumbers() {
ComplexNumber z = ComplexNumber.rectangular(100, -200); // Complex number 100 - 200i
ComplexNumber result = z.multiplicativeInverse().orElseThrow();

assertEquals(ComplexNumber.rectangular(0.002, 0.004), result); // Expected: 1/(100-200i) = 0.002 + 0.004i
assertEquals(
ComplexNumber.rectangular(0.002, 0.004),
result); // Expected: 1/(100-200i) = 0.002 + 0.004i
}
}


}
20 changes: 14 additions & 6 deletions src/test/java/LA/VectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void givenTwoComplexVectors_thenReturnTheirStandardComplexInnerProduct() {

assertEquals(ComplexNumber.rectangular(37, 39), Vector.innerProduct(v1, v2));
}

@Test
void givenTwoRealVectors_thenReturnTheirDotProduct1() {
var v1 = new Vector(ComplexNumber.rectangular(2, 0), ComplexNumber.rectangular(6, 0));
Expand Down Expand Up @@ -105,24 +106,28 @@ class Add {
void addVectors_realVectors_sumOfRealParts() {
Vector v1 = new Vector(ComplexNumber.rectangular(2, 0), ComplexNumber.rectangular(6, 0));
Vector v2 = new Vector(ComplexNumber.rectangular(4, 0), ComplexNumber.rectangular(1, 0));
Vector expected = new Vector(ComplexNumber.rectangular(6, 0), ComplexNumber.rectangular(7, 0));
Vector expected =
new Vector(ComplexNumber.rectangular(6, 0), ComplexNumber.rectangular(7, 0));
assertEquals(expected, Vector.addVectors(v1, v2));
}

@Test
void addVectors_complexVectors_sumOfComplexParts() {
Vector v1 = new Vector(ComplexNumber.rectangular(1, 2), ComplexNumber.rectangular(3, 4));
Vector v2 = new Vector(ComplexNumber.rectangular(5, 6), ComplexNumber.rectangular(7, 8));
Vector expected = new Vector(ComplexNumber.rectangular(6, 8), ComplexNumber.rectangular(10, 12));
Vector expected =
new Vector(ComplexNumber.rectangular(6, 8), ComplexNumber.rectangular(10, 12));
assertEquals(expected, Vector.addVectors(v1, v2));
}

@Test
void addVectors_vectorAndZeroVector_originalVector() {
Vector v1 = new Vector(ComplexNumber.rectangular(1, 2), ComplexNumber.rectangular(3, 4));
Vector zeroVector = new Vector(ComplexNumber.rectangular(0, 0), ComplexNumber.rectangular(0, 0));
Vector zeroVector =
new Vector(ComplexNumber.rectangular(0, 0), ComplexNumber.rectangular(0, 0));
assertEquals(v1, Vector.addVectors(v1, zeroVector));
}

@Test
void addVectors_shouldHandleVectorWithNegativeComponents() {
Vector v1 = new Vector(ComplexNumber.rectangular(-2, -3), ComplexNumber.rectangular(-4, -5));
Expand All @@ -143,8 +148,10 @@ void addVectors_shouldHandleVectorsWithDifferentSizes() {

@Test
void addVectors_shouldHandleLargeComplexNumbers() {
Vector v1 = new Vector(ComplexNumber.rectangular(1000, 2000), ComplexNumber.rectangular(-3000, 4000));
Vector v2 = new Vector(ComplexNumber.rectangular(5000, -6000), ComplexNumber.rectangular(7000, 8000));
Vector v1 =
new Vector(ComplexNumber.rectangular(1000, 2000), ComplexNumber.rectangular(-3000, 4000));
Vector v2 =
new Vector(ComplexNumber.rectangular(5000, -6000), ComplexNumber.rectangular(7000, 8000));
Vector result = Vector.addVectors(v1, v2);

assertEquals(ComplexNumber.rectangular(6000, -4000), result.getComponent(1));
Expand All @@ -154,7 +161,8 @@ void addVectors_shouldHandleLargeComplexNumbers() {
@Test
void addVectors_shouldHandleVectorAdditionWithZeroVectors() {
Vector v1 = new Vector(ComplexNumber.rectangular(1, 2), ComplexNumber.rectangular(3, 4));
Vector zeroVector = new Vector(ComplexNumber.rectangular(0, 0), ComplexNumber.rectangular(0, 0));
Vector zeroVector =
new Vector(ComplexNumber.rectangular(0, 0), ComplexNumber.rectangular(0, 0));
Vector result1 = Vector.addVectors(v1, zeroVector);
Vector result2 = Vector.addVectors(zeroVector, v1); // Test commutativity

Expand Down

0 comments on commit 3fb50ca

Please sign in to comment.