Skip to content

Commit eac2185

Browse files
committed
Continued tests
1 parent 7719ef0 commit eac2185

File tree

13 files changed

+320
-33
lines changed

13 files changed

+320
-33
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*.scala]
2+
indent_style = space
3+
indent_size = 2
4+
5+
[*.java]
6+
indent_style = space
7+
indent_size = 4

build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ lazy val root = (project in file(".")).settings(
1010

1111
// Scala test dependency
1212
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test
13+
14+
// Junit test dependency
15+
libraryDependencies ++= Seq(
16+
"junit" % "junit" % "4.13.2" % Test,
17+
"com.novocode" % "junit-interface" % "0.11" % Test
18+
)

src/main/scala/io/github/hexagonnico/cmplxlib/Complex.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ case class Complex(a: Double, b: Double) extends Double2 {
238238
*
239239
* @return The multiplicative inverse of this complex number
240240
*/
241-
def reciprocal: Complex = this.conjugate / (this.a * this.a + this.b * b)
241+
def inverse: Complex = this.conjugate / (this.a * this.a + this.b * b)
242242

243243
/**
244244
* Returns the result of the division between this complex number and the given one.
245245
*
246246
* @param z The complex number by which this one is divided
247247
* @return The result of the division between this complex number and the given one
248248
*/
249-
def /(z: Complex): Complex = this * z.reciprocal
249+
def /(z: Complex): Complex = this * z.inverse
250250

251251
/**
252252
* Returns the result of the division between this complex number and the given one.
@@ -394,7 +394,7 @@ object Complex {
394394
*/
395395
def sqrt(z: Complex): Complex = {
396396
val abs = z.abs
397-
0.5 * math.sqrt(2.0) * Complex(math.sqrt(abs + z.a), math.signum(z.b) * math.sqrt(abs - z.a))
397+
Complex(math.sqrt((abs + z.a) / 2.0), math.signum(z.b) * math.sqrt((abs - z.a) / 2.0))
398398
}
399399

400400
/**
@@ -458,6 +458,6 @@ object Complex {
458458
* @param z The complex number by which this one is divided
459459
* @return The result of the division of this real number by the given complex number
460460
*/
461-
def /(z: Complex): Complex = r * z.reciprocal
461+
def /(z: Complex): Complex = r * z.inverse
462462
}
463463
}

src/main/scala/io/github/hexagonnico/cmplxlib/vector/ComplexVector.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ abstract class ComplexVector[V <: ComplexVector[V]] extends VecAbstract[V] {
7070
* @param z The complex number by which the vector is divided
7171
* @return The result of the division of this vector by the given complex number
7272
*/
73-
def /(z: Complex): V = this * z.reciprocal
73+
def /(z: Complex): V = this * z.inverse
7474

7575
/**
7676
* Returns the result of the division of this vector by the given complex number.
@@ -133,15 +133,15 @@ abstract class ComplexVector[V <: ComplexVector[V]] extends VecAbstract[V] {
133133
*
134134
* @return A vector whose components are the multiplicative inverse of this vector's components
135135
*/
136-
def reciprocal: V
136+
def inverse: V
137137

138138
/**
139139
* Returns the component-wise division between this vector and the given one.
140140
*
141141
* @param v The second operand of the division
142142
* @return The component-wise division between this vector and the given one
143143
*/
144-
def /(v: V): V = this * v.reciprocal
144+
def /(v: V): V = this * v.inverse
145145

146146
/**
147147
* Returns the component-wise division between this vector and the given one.

src/main/scala/io/github/hexagonnico/cmplxlib/vector/Vec2c.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ case class Vec2c(x: Complex, y: Complex) extends ComplexVector[Vec2c] {
148148
*
149149
* @return A vector whose components are the multiplicative inverse of this vectors components
150150
*/
151-
override def reciprocal: Vec2c = Vec2c(this.x.reciprocal, this.y.reciprocal)
151+
override def inverse: Vec2c = Vec2c(this.x.inverse, this.y.inverse)
152152

153153
/**
154154
* Returns the component-wise division between this vector and the given scalars.

src/main/scala/io/github/hexagonnico/cmplxlib/vector/Vec3c.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ case class Vec3c(x: Complex, y: Complex, z: Complex) extends ComplexVector[Vec3c
156156
*
157157
* @return A vector whose components are the multiplicative inverse of this vector's components
158158
*/
159-
override def reciprocal: Vec3c = Vec3c(this.x.reciprocal, this.y.reciprocal, this.z.reciprocal)
159+
override def inverse: Vec3c = Vec3c(this.x.inverse, this.y.inverse, this.z.inverse)
160160

161161
/**
162162
* Returns the component-wise division between this vector and the given scalars.

src/main/scala/io/github/hexagonnico/cmplxlib/vector/Vec4c.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ case class Vec4c(x: Complex, y: Complex, z: Complex, w: Complex) extends Complex
164164
*
165165
* @return A vector whose components are the multiplicative inverse of this vector's components
166166
*/
167-
override def reciprocal: Vec4c = Vec4c(this.x.reciprocal, this.y.reciprocal, this.z.reciprocal, this.w.reciprocal)
167+
override def inverse: Vec4c = Vec4c(this.x.inverse, this.y.inverse, this.z.inverse, this.w.inverse)
168168

169169
/**
170170
* Returns the component-wise division between this vector and the given scalars.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.github.hexagonnico.cmplxlib;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class TestComplex {
7+
8+
@Test
9+
public void testComplexPlusTwoValues() {
10+
Complex z = new Complex(2.0, 1.0);
11+
Complex res = z.plus(1.5, 2.0);
12+
Assert.assertEquals(new Complex(3.5, 3.0), res);
13+
}
14+
15+
@Test
16+
public void testComplexPlusReal() {
17+
Complex z = new Complex(2.0, 1.0);
18+
Complex res = z.plus(1.5);
19+
Assert.assertEquals(new Complex(3.5, 1.0), res);
20+
}
21+
22+
@Test
23+
public void testComplexSum() {
24+
Complex z = new Complex(2.0, 1.0);
25+
Complex w = new Complex(1.5, 2.0);
26+
Assert.assertEquals(new Complex(3.5, 3.0), z.plus(w));
27+
}
28+
29+
@Test
30+
public void testNegated() {
31+
Complex z = new Complex(2.0, 1.0);
32+
Assert.assertEquals(new Complex(-2.0, -1.0), z.negated());
33+
}
34+
35+
@Test
36+
public void testComplexMinusTwoValues() {
37+
Complex z = new Complex(2.0, 1.0);
38+
Complex res = z.minus(1.5, 2.0);
39+
Assert.assertEquals(new Complex(0.5, -1.0), res);
40+
}
41+
42+
@Test
43+
public void testComplexMinusReal() {
44+
Complex z = new Complex(2.0, 1.0);
45+
Complex res = z.minus(1.5);
46+
Assert.assertEquals(new Complex(0.5, 1.0), res);
47+
}
48+
49+
@Test
50+
public void testComplexSubtraction() {
51+
Complex z = new Complex(2.0, 1.0);
52+
Complex w = new Complex(1.5, 2.0);
53+
Assert.assertEquals(new Complex(0.5, -1.0), z.minus(w));
54+
}
55+
56+
@Test
57+
public void testComplexTimesReal() {
58+
Complex z = new Complex(2.0, 1.0);
59+
Complex res = z.multiply(1.5);
60+
Assert.assertEquals(new Complex(3.0, 1.5), res);
61+
}
62+
63+
@Test
64+
public void testComplexTimesTwoValues() {
65+
Complex z = new Complex(2.0, 1.0);
66+
Complex res = z.multiply(1.5, 2.0);
67+
Assert.assertEquals(new Complex(1.0, 5.5), res);
68+
}
69+
70+
@Test
71+
public void testComplexProduct() {
72+
Complex z = new Complex(2.0, 1.0);
73+
Complex w = new Complex(1.5, 2.0);
74+
Assert.assertEquals(new Complex(1.0, 5.5), z.multiply(w));
75+
}
76+
77+
@Test
78+
public void testComplexDividedByReal() {
79+
Complex z = new Complex(2.0, 1.0);
80+
Complex res = z.divide(2.0);
81+
Assert.assertEquals(new Complex(1.0, 0.5), res);
82+
}
83+
84+
@Test
85+
public void testComplexDivision() {
86+
Complex z = new Complex(3.0, 2.0);
87+
Complex w = new Complex(2.0, 2.0);
88+
Assert.assertEquals(new Complex(1.25, -0.25), z.divide(w));
89+
}
90+
91+
@Test
92+
public void testComplexDividedByTwoValues() {
93+
Complex z = new Complex(3.0, 2.0);
94+
Complex res = z.divide(2.0, 2.0);
95+
Assert.assertEquals(new Complex(1.25, -0.25), res);
96+
}
97+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.github.hexagonnico.cmplxlib.vector;
2+
3+
import io.github.hexagonnico.cmplxlib.Complex;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class TestVec2c {
8+
9+
@Test
10+
public void testVectorPlusValues() {
11+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
12+
Vec2c res = vec.plus(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
13+
Assert.assertEquals(new Vec2c(new Complex(3.0, 4.5), new Complex(4.5, 1.5)), res);
14+
}
15+
16+
@Test
17+
public void testVectorSum() {
18+
Vec2c a = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
19+
Vec2c b = new Vec2c(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
20+
Assert.assertEquals(new Vec2c(new Complex(3.0, 4.5), new Complex(4.5, 1.5)), a.plus(b));
21+
}
22+
23+
@Test
24+
public void testNegated() {
25+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
26+
Assert.assertEquals(new Vec2c(new Complex(-1.0, -2.0), new Complex(-1.5, -1.0)), vec.negated());
27+
}
28+
29+
@Test
30+
public void testVectorMinusValues() {
31+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
32+
Vec2c res = vec.minus(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
33+
Assert.assertEquals(new Vec2c(new Complex(-1.0, -0.5), new Complex(-1.5, 0.5)), res);
34+
}
35+
36+
@Test
37+
public void testVectorSubtraction() {
38+
Vec2c a = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
39+
Vec2c b = new Vec2c(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
40+
Assert.assertEquals(new Vec2c(new Complex(-1.0, -0.5), new Complex(-1.5, 0.5)), a.minus(b));
41+
}
42+
43+
@Test
44+
public void testMultiplyByReal() {
45+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
46+
Vec2c res = vec.multiply(1.5);
47+
Assert.assertEquals(new Vec2c(new Complex(1.5, 3.0), new Complex(2.25, 1.5)), res);
48+
}
49+
50+
@Test
51+
public void testMultiplyByComplex() {
52+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
53+
Vec2c res = vec.multiply(new Complex(1.5, 1.0));
54+
Assert.assertEquals(new Vec2c(new Complex(-0.5, 4.0), new Complex(1.25, 3.0)), res);
55+
}
56+
57+
@Test
58+
public void testDividedByReal() {
59+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
60+
Vec2c res = vec.divide(2.0);
61+
Assert.assertEquals(new Vec2c(new Complex(0.5, 1.0), new Complex(0.75, 0.5)), res);
62+
}
63+
64+
@Test
65+
public void testDividedByComplex() {
66+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
67+
Vec2c res = vec.divide(new Complex(1.0, 1.0));
68+
Assert.assertEquals(new Vec2c(new Complex(3.0 / 2.0, 0.5), new Complex(1.25, -0.25)), res);
69+
}
70+
71+
@Test
72+
public void testComponentWiseMultiplicationValues() {
73+
Vec2c vec = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
74+
Vec2c res = vec.multiply(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
75+
Assert.assertEquals(new Vec2c(new Complex(-3.0, 6.5), new Complex(4.0, 3.75)), res);
76+
}
77+
78+
@Test
79+
public void testComponentWiseMultiplication() {
80+
Vec2c a = new Vec2c(new Complex(1.0, 2.0), new Complex(1.5, 1.0));
81+
Vec2c b = new Vec2c(new Complex(2.0, 2.5), new Complex(3.0, 0.5));
82+
Assert.assertEquals(new Vec2c(new Complex(-3.0, 6.5), new Complex(4.0, 3.75)), a.multiply(b));
83+
}
84+
85+
@Test
86+
public void testComponentWiseDivision() {
87+
Vec2c a = new Vec2c(new Complex(8.0, 6.0), new Complex(6.0, 8.0));
88+
Vec2c b = new Vec2c(new Complex(2.0, 2.0), new Complex(4.0, 2.0));
89+
Assert.assertEquals(new Vec2c(new Complex(3.5, -0.5), new Complex(2.0, 1.0)), a.divide(b));
90+
}
91+
92+
@Test
93+
public void testComponentWiseDivisionValues() {
94+
Vec2c vec = new Vec2c(new Complex(8.0, 6.0), new Complex(6.0, 8.0));
95+
Vec2c res = vec.divide(new Complex(2.0, 2.0), new Complex(4.0, 2.0));
96+
Assert.assertEquals(new Vec2c(new Complex(3.5, -0.5), new Complex(2.0, 1.0)), res);
97+
}
98+
}

src/test/scala/io/github/hexagonnico/cmplxlib/ComplexSuite.scala

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.hexagonnico.cmplxlib
22

3+
import org.scalactic.Tolerance.convertNumericToPlusOrMinusWrapper
34
import org.scalatest.funsuite.AnyFunSuite
45

56
class ComplexSuite extends AnyFunSuite {
@@ -97,9 +98,9 @@ class ComplexSuite extends AnyFunSuite {
9798
assert(z.abs == math.sqrt(2.0))
9899
}
99100

100-
test("Reciprocal of a complex number") {
101+
test("Multiplicative inverse of a complex number") {
101102
val z = Complex(2.0, 1.0)
102-
assert(z.reciprocal == Complex(0.4, -0.2))
103+
assert(z.inverse == Complex(0.4, -0.2))
103104
}
104105

105106
test("Complex number divided by a complex number") {
@@ -146,4 +147,28 @@ class ComplexSuite extends AnyFunSuite {
146147
test("Zero to string") {
147148
assert(Complex.Zero.toString == "0.0")
148149
}
150+
151+
test("Sine of a complex number") {
152+
val z = Complex.sin(Complex(math.Pi, 1.0))
153+
assert(z.real === 0.0 +- 1e-9)
154+
assert(z.imaginary === (1.0 - math.E * math.E) / (2.0 * math.E) +- 1e-9)
155+
}
156+
157+
test("Cosine of a complex number") {
158+
val z = Complex.cos(Complex.I)
159+
assert(z.real === (math.E * math.E + 1.0) / (2.0 * math.E) +- 1e-9)
160+
assert(z.imaginary === 0.0 +- 1e-9)
161+
}
162+
163+
test("Inverse sine of a complex number") {
164+
val z = Complex.asin(Complex(0.0, (1.0 - math.E * math.E) / (2.0 * math.E)))
165+
assert(z.real === 0.0 +- 1e-9)
166+
assert(z.imaginary === -1.0 +- 1e-9)
167+
}
168+
169+
test("Inverse cosine of a complex number") {
170+
val z = Complex.acos(Complex(1.0 / math.sqrt(2.0), 0.0))
171+
assert(z.real === math.Pi / 4.0 +- 1e-9)
172+
assert(z.imaginary === 0.0 +- 1e-9)
173+
}
149174
}

0 commit comments

Comments
 (0)