added complext number support

This commit is contained in:
sergeych committed 2026-03-28 17:44:43 +03:00
1 parent 418b1ae2b6
commit cd007050a8
6 files changed
+424 -16

No files matched your search

@@ -27,6 +27,7 @@ import net.sergeych.lyng.bytecode.CmdVm
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.stdlib_included.complexLyng
import net.sergeych.lyng.stdlib_included.decimalLyng
import net.sergeych.lyng.stdlib_included.observableLyng
import net.sergeych.lyng.stdlib_included.operatorsLyng
@@ -615,12 +616,91 @@ class Script(
type = type("lyng.Real")
)
getOrCreateNamespace("Math").apply {
fun ensureFn(name: String, fn: suspend ScopeFacade.() -> Obj) {
if (members.containsKey(name)) return
addFn(name, code = fn)
}
addConstDoc(
name = "PI",
value = pi,
doc = "The mathematical constant pi (π) in the Math namespace.",
type = type("lyng.Real")
)
ensureFn("floor") {
val x = args.firstAndOnly()
if (x is ObjInt) x else ObjReal(floor(x.toDouble()))
}
ensureFn("ceil") {
val x = args.firstAndOnly()
if (x is ObjInt) x else ObjReal(ceil(x.toDouble()))
}
ensureFn("round") {
val x = args.firstAndOnly()
if (x is ObjInt) x else ObjReal(round(x.toDouble()))
}
ensureFn("sin") {
ObjReal(sin(args.firstAndOnly().toDouble()))
}
ensureFn("cos") {
ObjReal(cos(args.firstAndOnly().toDouble()))
}
ensureFn("tan") {
ObjReal(tan(args.firstAndOnly().toDouble()))
}
ensureFn("asin") {
ObjReal(asin(args.firstAndOnly().toDouble()))
}
ensureFn("acos") {
ObjReal(acos(args.firstAndOnly().toDouble()))
}
ensureFn("atan") {
ObjReal(atan(args.firstAndOnly().toDouble()))
}
ensureFn("sinh") {
ObjReal(sinh(args.firstAndOnly().toDouble()))
}
ensureFn("cosh") {
ObjReal(cosh(args.firstAndOnly().toDouble()))
}
ensureFn("tanh") {
ObjReal(tanh(args.firstAndOnly().toDouble()))
}
ensureFn("asinh") {
ObjReal(asinh(args.firstAndOnly().toDouble()))
}
ensureFn("acosh") {
ObjReal(acosh(args.firstAndOnly().toDouble()))
}
ensureFn("atanh") {
ObjReal(atanh(args.firstAndOnly().toDouble()))
}
ensureFn("exp") {
ObjReal(exp(args.firstAndOnly().toDouble()))
}
ensureFn("ln") {
ObjReal(ln(args.firstAndOnly().toDouble()))
}
ensureFn("log10") {
ObjReal(log10(args.firstAndOnly().toDouble()))
}
ensureFn("log2") {
ObjReal(log2(args.firstAndOnly().toDouble()))
}
ensureFn("pow") {
requireExactCount(2)
ObjReal(
(args[0].toDouble()).pow(args[1].toDouble())
)
}
ensureFn("sqrt") {
ObjReal(
sqrt(args.firstAndOnly().toDouble())
)
}
ensureFn("abs") {
val x = args.firstAndOnly()
if (x is ObjInt) ObjInt(x.value.absoluteValue) else ObjReal(x.toDouble().absoluteValue)
}
}
}
@@ -759,6 +839,9 @@ class Script(
module.eval(Source("lyng.decimal", decimalLyng))
ObjBigDecimalSupport.bindTo(module)
}
addPackage("lyng.complex") { module ->
module.eval(Source("lyng.complex", complexLyng))
}
addPackage("lyng.buffer") {
it.addConstDoc(
name = "Buffer",
@@ -0,0 +1,76 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
class ComplexModuleTest {
@Test
fun testComplexArithmeticAndInterop() = runTest {
val scope = Script.newScope()
scope.eval(
"""
import lyng.complex
assertEquals(Complex(0.0, 2.0), 2.i)
assertEquals(Complex(2.0, 0.0), 2.re)
assertEquals(Complex(1.0, 2.0), 1 + 2.i)
assertEquals(Complex(1.5, 2.0), 1.5 + 2.i)
assertEquals(Complex(3.0, 2.0), 1 + Complex(2.0, 2.0))
val product: Complex = Complex(1.0, 2.0) * Complex(3.0, -1.0)
assertEquals(5.0, product.re)
assertEquals(5.0, product.im)
val quotient: Complex = Complex(5.0, 5.0) / Complex(3.0, -1.0)
assertEquals(1.0, quotient.re)
assertEquals(2.0, quotient.im)
assertEquals(Complex(1.0, -2.0), Complex(1.0, 2.0).conjugate)
""".trimIndent()
)
}
@Test
fun testComplexMemberMathFunctions() = runTest {
val scope = Script.newScope()
scope.eval(
"""
import lyng.complex
val eps = 1e-9
val eipi = Complex.imaginary(π).exp()
assert(abs(eipi.re + 1.0) < eps)
assert(abs(eipi.im) < eps)
val iy: Complex = 2.i
val sinIy = iy.sin()
assert(abs(sinIy.re) < eps)
assert(abs(sinIy.im - sinh(2.0)) < eps)
val minusOne: Complex = (-1).re
val root = minusOne.sqrt()
assert(abs(root.re) < eps)
assert(abs(root.im - 1.0) < eps)
val unitLeft: Complex = cis(π)
assert(abs(unitLeft.re + 1.0) < eps)
assert(abs(unitLeft.im) < eps)
""".trimIndent()
)
}
}