diff --git a/Sources/PlatoCore/Functions/Native/DefaultNativeFunctionHandler.swift b/Sources/PlatoCore/Functions/Native/DefaultNativeFunctionHandler.swift index f782cba..9ae481b 100644 --- a/Sources/PlatoCore/Functions/Native/DefaultNativeFunctionHandler.swift +++ b/Sources/PlatoCore/Functions/Native/DefaultNativeFunctionHandler.swift @@ -24,18 +24,24 @@ public struct DefaultNativeFunctionHandler: NativeFunctionHandling { case RandomFunc.name: return try RandomFunc.handle(parameters: parameters) // Math - case LogFunc.name: - return try LogFunc.handle(parameters: parameters) - case Log2Func.name: - return try Log2Func.handle(parameters: parameters) - case Log10Func.name: - return try Log10Func.handle(parameters: parameters) case PowFunc.name: return try PowFunc.handle(parameters: parameters) case SqrtFunc.name: return try SqrtFunc.handle(parameters: parameters) case ExpFunc.name: return try ExpFunc.handle(parameters: parameters) + // Log + case LogFunc.name: + return try LogFunc.handle(parameters: parameters) + case Log2Func.name: + return try Log2Func.handle(parameters: parameters) + case Log10Func.name: + return try Log10Func.handle(parameters: parameters) + // Gamma + case GammaFunc.name: + return try GammaFunc.handle(parameters: parameters) + case LogGammaFunc.name: + return try LogGammaFunc.handle(parameters: parameters) // Sin case SinFunc.name: return try SinFunc.handle(parameters: parameters) diff --git a/Sources/PlatoCore/Functions/Native/Math/ExpFunc.swift b/Sources/PlatoCore/Functions/Native/Math/ExpFunc.swift index fd7370d..ee0a334 100644 --- a/Sources/PlatoCore/Functions/Native/Math/ExpFunc.swift +++ b/Sources/PlatoCore/Functions/Native/Math/ExpFunc.swift @@ -18,7 +18,6 @@ public struct ExpFunc: FunctionResultHandling { guard x.type.isNumber else { throw FunctionError.typeError(parameterType: x.type, expectedType: .number) } - return Value(float: exp(x.asFloat)) } } diff --git a/Sources/PlatoCore/Functions/Native/Math/GammaFunc.swift b/Sources/PlatoCore/Functions/Native/Math/GammaFunc.swift new file mode 100644 index 0000000..5c5d5f1 --- /dev/null +++ b/Sources/PlatoCore/Functions/Native/Math/GammaFunc.swift @@ -0,0 +1,23 @@ +// +// GammaFunc.swift +// +// +// Created by Pedro Ésli Vieira do Nascimento on 19/03/24. +// + +import Darwin + +public struct GammaFunc: FunctionResultHandling { + public static let name: String = "gamma" + + public static func handle(parameters: [CallParameter]) throws -> Value { + guard !parameters.isEmpty else { throw FunctionError.missingArgument(parameter: "x") } + guard parameters.count == 1 else { throw FunctionError.extraArgument } + + let x = parameters[0].value + + guard x.type.isNumber else { throw FunctionError.typeError(parameterType: x.type, expectedType: .number) } + + return Value(float: tgamma(x.asFloat)) + } +} diff --git a/Sources/PlatoCore/Functions/Native/Math/LogGammaFunc.swift b/Sources/PlatoCore/Functions/Native/Math/LogGammaFunc.swift new file mode 100644 index 0000000..5230808 --- /dev/null +++ b/Sources/PlatoCore/Functions/Native/Math/LogGammaFunc.swift @@ -0,0 +1,22 @@ +// +// LogGammaFunc.swift +// +// +// Created by Pedro Ésli Vieira do Nascimento on 19/03/24. +// + +import Darwin + +public struct LogGammaFunc: FunctionResultHandling { + public static let name: String = "logGamma" + + public static func handle(parameters: [CallParameter]) throws -> Value { + guard !parameters.isEmpty else { throw FunctionError.missingArgument(parameter: "x") } + guard parameters.count == 1 else { throw FunctionError.extraArgument } + + let x = parameters[0].value + + guard x.type.isNumber else { throw FunctionError.typeError(parameterType: x.type, expectedType: .number) } + return Value(float: lgamma(x.asFloat).0) + } +} diff --git a/Tests/PlatoTests/PlatoTests.swift b/Tests/PlatoTests/PlatoTests.swift index 7a90fab..cc0ce82 100644 --- a/Tests/PlatoTests/PlatoTests.swift +++ b/Tests/PlatoTests/PlatoTests.swift @@ -13,7 +13,7 @@ final class PlatoTests: XCTestCase { func testGeneral() { let code = """ - 6 + cos(90) 2 "321pedro" """