Skip to content

Commit

Permalink
feat: gamma functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroesli committed Mar 19, 2024
1 parent 173123f commit 06ce20b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion Sources/PlatoCore/Functions/Native/Math/ExpFunc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
23 changes: 23 additions & 0 deletions Sources/PlatoCore/Functions/Native/Math/GammaFunc.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
22 changes: 22 additions & 0 deletions Sources/PlatoCore/Functions/Native/Math/LogGammaFunc.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion Tests/PlatoTests/PlatoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class PlatoTests: XCTestCase {

func testGeneral() {
let code = """
6
cos(90)
2
"321pedro"
"""
Expand Down

0 comments on commit 06ce20b

Please sign in to comment.