Add membership function and continuous set basic defs
This commit is contained in:
parent
3fdfb14381
commit
186d94ff17
|
@ -0,0 +1,27 @@
|
|||
public struct ContinuousFuzzySet: FuzzySet {
|
||||
public typealias Element = Double
|
||||
|
||||
private let membershipFunction: MembershipFunction
|
||||
|
||||
public init(membershipFunction: MembershipFunction) {
|
||||
self.membershipFunction = membershipFunction
|
||||
}
|
||||
|
||||
public init(membershipFunction: @escaping (Element) -> Grade) {
|
||||
self.membershipFunction = MembershipFunction(membershipFunction)
|
||||
}
|
||||
|
||||
public func grade(forElement element: Element) -> Grade {
|
||||
membershipFunction(element)
|
||||
}
|
||||
}
|
||||
|
||||
public extension ContinuousFuzzySet {
|
||||
static var empty: Self {
|
||||
.init(membershipFunction: .zero)
|
||||
}
|
||||
|
||||
static var universe: Self {
|
||||
.init(membershipFunction: .one)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
public struct MembershipFunction {
|
||||
|
||||
private let function: (Double) -> Double
|
||||
|
||||
public init(_ function: @escaping (Double) -> Grade) {
|
||||
self.function = function
|
||||
}
|
||||
|
||||
public func callAsFunction(_ x: Double) -> Double {
|
||||
function(x)
|
||||
}
|
||||
}
|
||||
|
||||
/// Common membership functions
|
||||
public extension MembershipFunction {
|
||||
static var zero: Self {
|
||||
.init { _ in 0.0 }
|
||||
}
|
||||
|
||||
static var one: Self {
|
||||
.init { _ in 1.0 }
|
||||
}
|
||||
|
||||
static func fuzzySingleton(_ a: Double) -> Self {
|
||||
.init { $0 == a ? 1.0 : 0.0 }
|
||||
}
|
||||
|
||||
// TODO: Add more
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import XCTest
|
||||
import FuzzyKit
|
||||
|
||||
final class ContinuousFuzzySetTests: XCTestCase {
|
||||
// TODO
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import XCTest
|
||||
import FuzzyKit
|
||||
|
||||
final class MembershipFunctionTests: XCTestCase {
|
||||
func test_isCallable() throws {
|
||||
let sut = MembershipFunction.one
|
||||
let x = 1.0
|
||||
|
||||
let result = sut(x)
|
||||
|
||||
XCTAssertEqual(result, x)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue