Fix norm and add test of it

This commit is contained in:
Iaroslav Postovalov 2020-08-11 15:06:53 +07:00
parent 8ab8864227
commit 837150b5eb
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 8 additions and 2 deletions

View File

@ -153,7 +153,7 @@ object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex> {
*/
operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this)
override fun norm(arg: Complex): Complex = arg.conjugate * arg
override fun norm(arg: Complex): Complex = sqrt(arg.conjugate * arg)
override fun symbol(value: String): Complex = if (value == "i") i else super.symbol(value)
}

View File

@ -1,6 +1,7 @@
package scientifik.kmath.operations
import kotlin.math.*
import kotlin.math.PI
import kotlin.math.abs
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@ -64,4 +65,9 @@ internal class ComplexFieldTest {
ComplexField { i * 8 }.let { it.im.toInt() to it.re.toInt() },
ComplexField { Complex(2, 2) pow 2 }.let { it.im.toInt() to it.re.toInt() })
}
@Test
fun testNorm() {
assertEquals(2.toComplex(), ComplexField { norm(2 * i) })
}
}