diff --git a/Makefile b/Makefile index b40320e..43c04f8 100644 --- a/Makefile +++ b/Makefile @@ -46,8 +46,9 @@ gyb: $(sources) %.hpp: %.hpp.gyb gyb --line-directive '' -o $@ $< -Sources/BaseMath/BaseMath.swift Sources/BaseMath/BaseVector.swift Sources/CBaseMath/CBaseMath.cpp: mathfuncs.py +Sources/BaseMath/BaseRandom.swift Sources/BaseMath/CBaseRandom.swift Sources/BaseMath/BaseMath.swift Sources/BaseMath/BaseVector.swift Sources/CBaseMath/CBaseMath.cpp: mathfuncs.py cpp_template.py cpp_types.py c2swift.py Sources/CBaseMath/include/CBaseMath.h: Sources/CBaseMath/CBaseMath.cpp +Sources/CBaseMath/include/CBaseRandom.h: Sources/CBaseMath/CBaseRandom.cpp .PHONY: clean clean: diff --git a/Sources/BaseMath/BaseMath.swift b/Sources/BaseMath/BaseMath.swift index 7aa514d..2c73ca0 100644 --- a/Sources/BaseMath/BaseMath.swift +++ b/Sources/BaseMath/BaseMath.swift @@ -9,10 +9,13 @@ extension BinaryFloatingPoint where Self: CVarArg { precedencegroup ExponentiationPrecedence { associativity: right higherThan: MultiplicationPrecedence } infix operator ^^: ExponentiationPrecedence -public protocol SupportsBasicMath:BinaryFloatingPoint { +public extension Numeric { typealias Element=Self typealias PtrT = UnsafePointer typealias MutPtrT = UnsafeMutablePointer +} + +public protocol SupportsBasicMath:BinaryFloatingPoint { init(_ value: Self) init() diff --git a/Sources/BaseMath/BaseMath.swift.gyb b/Sources/BaseMath/BaseMath.swift.gyb index 1430b86..cd603b2 100644 --- a/Sources/BaseMath/BaseMath.swift.gyb +++ b/Sources/BaseMath/BaseMath.swift.gyb @@ -10,10 +10,13 @@ extension BinaryFloatingPoint where Self: CVarArg { precedencegroup ExponentiationPrecedence { associativity: right higherThan: MultiplicationPrecedence } infix operator ^^: ExponentiationPrecedence -public protocol SupportsBasicMath:BinaryFloatingPoint { +public extension Numeric { typealias Element=Self typealias PtrT = UnsafePointer typealias MutPtrT = UnsafeMutablePointer +} + +public protocol SupportsBasicMath:BinaryFloatingPoint { init(_ value: Self) init() diff --git a/Sources/BaseMath/BaseRand.swift b/Sources/BaseMath/BaseRand.swift index 988a321..708da70 100644 --- a/Sources/BaseMath/BaseRand.swift +++ b/Sources/BaseMath/BaseRand.swift @@ -1,18 +1,6 @@ import Foundation import CBaseMath -public class RandGen { - public let ptr:OpaquePointer? - public init() { ptr=RandGen_create() } - deinit { RandGen_destroy(ptr) } - - @usableFromInline static var storeKey:String { return "RandGen" } - @usableFromInline static var stored:RandGen { - if let r = Thread.current.threadDictionary[storeKey] as? RandGen { return r } - return Thread.setToTLS(RandGen(), storeKey) - } -} - public protocol Initable { init() } extension Float:Initable {} @@ -21,14 +9,37 @@ extension Int:Initable {} extension Int32:Initable {} extension Bool:Initable {} -public protocol Distribution:Nullary { - subscript()->Element {get} - subscript(n:Int)->[Element] {get} - func gen_array(_ n:Int)->[Element] - func gen_aligned(_ n:Int)->AlignedStorage - func gen_pointer(_ n:Int)->UnsafeMutableBufferPointer +public protocol CppTypePtr { + func delete() +} + +public class CppType { + public let p:T + init(_ p:T) {self.p=p} + deinit {p.delete()} +} + +public class mt19937:CppType { + public convenience init() { self.init(CBaseMath.mt19937_create()) } + + @usableFromInline static var storeKey:String { get { return "mt19937" } } + @usableFromInline static var stored:mt19937 { get { + if let r = Thread.current.threadDictionary[storeKey] as? mt19937 { return r } + return Thread.setToTLS(mt19937(), storeKey) + }} +} + +public protocol DistributionC:CppTypePtr { + associatedtype Element:SignedNumeric + func call(_ g:mt19937C)->Element } -extension Distribution { + +public class Distribution:CppType,Nullary { + public typealias Element=T.Element + public var g:mt19937 + + init(_ p:T, _ g:mt19937) {self.g=g; super.init(p) } + @inlinable public subscript()->Element { return p.call(g.p) } public subscript(n:Int)->[Element] { return gen_array(n) } public func gen_array(_ n:Int)->[Element] { return [Element].fill(self, n) @@ -41,450 +52,357 @@ extension Distribution { } } -public final class uniform_int_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Int32, _ b:Int32) { ptr=uniform_int_distribution_int_create(a,b); g=g_ } - public convenience init(_ a:Int32, _ b:Int32) { self.init(RandGen.stored, a,b) } - deinit { uniform_int_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return uniform_int_distribution_int_call(ptr, g.ptr) } + +public final class uniform_int_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ a:Int,_ b:Int) { super.init(uniform_int_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Int,_ b:Int) { self.init(mt19937.stored, a,b) } +} +extension Int { + public static func uniform_int_distribution(_ g_:mt19937 , _ a:Int,_ b:Int)->uniform_int_distribution_Int {return uniform_int_distribution_Int(g_, a,b)} + public static func uniform_int_distribution(_ a:Int,_ b:Int)->uniform_int_distribution_Int {return uniform_int_distribution_Int(a,b)} +} + +public final class uniform_int_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ a:Int32,_ b:Int32) { super.init(uniform_int_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Int32,_ b:Int32) { self.init(mt19937.stored, a,b) } } extension Int32 { - public static func uniform_int_distribution(_ g_:RandGen, _ a:Int32, _ b:Int32)->uniform_int_distribution_Int32 {return uniform_int_distribution_Int32(g_, a,b)} - public static func uniform_int_distribution(_ a:Int32, _ b:Int32)->uniform_int_distribution_Int32 {return uniform_int_distribution_Int32(a,b)} + public static func uniform_int_distribution(_ g_:mt19937 , _ a:Int32,_ b:Int32)->uniform_int_distribution_Int32 {return uniform_int_distribution_Int32(g_, a,b)} + public static func uniform_int_distribution(_ a:Int32,_ b:Int32)->uniform_int_distribution_Int32 {return uniform_int_distribution_Int32(a,b)} } -public final class uniform_int_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Int, _ b:Int) { ptr=uniform_int_distribution_long_create(a,b); g=g_ } - public convenience init(_ a:Int, _ b:Int) { self.init(RandGen.stored, a,b) } - deinit { uniform_int_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return uniform_int_distribution_long_call(ptr, g.ptr) } + +public final class binomial_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ t:Int,_ p:Double) { super.init(binomial_distribution_create(t,p, Element.init()), g_) } + public convenience init(_ t:Int,_ p:Double) { self.init(mt19937.stored, t,p) } } extension Int { - public static func uniform_int_distribution(_ g_:RandGen, _ a:Int, _ b:Int)->uniform_int_distribution_Int {return uniform_int_distribution_Int(g_, a,b)} - public static func uniform_int_distribution(_ a:Int, _ b:Int)->uniform_int_distribution_Int {return uniform_int_distribution_Int(a,b)} + public static func binomial_distribution(_ g_:mt19937 , _ t:Int,_ p:Double)->binomial_distribution_Int {return binomial_distribution_Int(g_, t,p)} + public static func binomial_distribution(_ t:Int,_ p:Double)->binomial_distribution_Int {return binomial_distribution_Int(t,p)} } -public final class binomial_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ t:Int32, _ p:Double) { ptr=binomial_distribution_int_create(t,p); g=g_ } - public convenience init(_ t:Int32, _ p:Double) { self.init(RandGen.stored, t,p) } - deinit { binomial_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return binomial_distribution_int_call(ptr, g.ptr) } + +public final class binomial_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ t:Int32,_ p:Double) { super.init(binomial_distribution_create(t,p, Element.init()), g_) } + public convenience init(_ t:Int32,_ p:Double) { self.init(mt19937.stored, t,p) } } extension Int32 { - public static func binomial_distribution(_ g_:RandGen, _ t:Int32, _ p:Double)->binomial_distribution_Int32 {return binomial_distribution_Int32(g_, t,p)} - public static func binomial_distribution(_ t:Int32, _ p:Double)->binomial_distribution_Int32 {return binomial_distribution_Int32(t,p)} + public static func binomial_distribution(_ g_:mt19937 , _ t:Int32,_ p:Double)->binomial_distribution_Int32 {return binomial_distribution_Int32(g_, t,p)} + public static func binomial_distribution(_ t:Int32,_ p:Double)->binomial_distribution_Int32 {return binomial_distribution_Int32(t,p)} } -public final class binomial_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ t:Int, _ p:Double) { ptr=binomial_distribution_long_create(t,p); g=g_ } - public convenience init(_ t:Int, _ p:Double) { self.init(RandGen.stored, t,p) } - deinit { binomial_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return binomial_distribution_long_call(ptr, g.ptr) } + +public final class negative_binomial_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ k:Int,_ p:Double) { super.init(negative_binomial_distribution_create(k,p, Element.init()), g_) } + public convenience init(_ k:Int,_ p:Double) { self.init(mt19937.stored, k,p) } } extension Int { - public static func binomial_distribution(_ g_:RandGen, _ t:Int, _ p:Double)->binomial_distribution_Int {return binomial_distribution_Int(g_, t,p)} - public static func binomial_distribution(_ t:Int, _ p:Double)->binomial_distribution_Int {return binomial_distribution_Int(t,p)} + public static func negative_binomial_distribution(_ g_:mt19937 , _ k:Int,_ p:Double)->negative_binomial_distribution_Int {return negative_binomial_distribution_Int(g_, k,p)} + public static func negative_binomial_distribution(_ k:Int,_ p:Double)->negative_binomial_distribution_Int {return negative_binomial_distribution_Int(k,p)} } -public final class negative_binomial_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ k:Int32, _ p:Double) { ptr=negative_binomial_distribution_int_create(k,p); g=g_ } - public convenience init(_ k:Int32, _ p:Double) { self.init(RandGen.stored, k,p) } - deinit { negative_binomial_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return negative_binomial_distribution_int_call(ptr, g.ptr) } + +public final class negative_binomial_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ k:Int32,_ p:Double) { super.init(negative_binomial_distribution_create(k,p, Element.init()), g_) } + public convenience init(_ k:Int32,_ p:Double) { self.init(mt19937.stored, k,p) } } extension Int32 { - public static func negative_binomial_distribution(_ g_:RandGen, _ k:Int32, _ p:Double)->negative_binomial_distribution_Int32 {return negative_binomial_distribution_Int32(g_, k,p)} - public static func negative_binomial_distribution(_ k:Int32, _ p:Double)->negative_binomial_distribution_Int32 {return negative_binomial_distribution_Int32(k,p)} + public static func negative_binomial_distribution(_ g_:mt19937 , _ k:Int32,_ p:Double)->negative_binomial_distribution_Int32 {return negative_binomial_distribution_Int32(g_, k,p)} + public static func negative_binomial_distribution(_ k:Int32,_ p:Double)->negative_binomial_distribution_Int32 {return negative_binomial_distribution_Int32(k,p)} } -public final class negative_binomial_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ k:Int, _ p:Double) { ptr=negative_binomial_distribution_long_create(k,p); g=g_ } - public convenience init(_ k:Int, _ p:Double) { self.init(RandGen.stored, k,p) } - deinit { negative_binomial_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return negative_binomial_distribution_long_call(ptr, g.ptr) } + +public final class geometric_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ p:Double) { super.init(geometric_distribution_create(p, Element.init()), g_) } + public convenience init(_ p:Double) { self.init(mt19937.stored, p) } } extension Int { - public static func negative_binomial_distribution(_ g_:RandGen, _ k:Int, _ p:Double)->negative_binomial_distribution_Int {return negative_binomial_distribution_Int(g_, k,p)} - public static func negative_binomial_distribution(_ k:Int, _ p:Double)->negative_binomial_distribution_Int {return negative_binomial_distribution_Int(k,p)} + public static func geometric_distribution(_ g_:mt19937 , _ p:Double)->geometric_distribution_Int {return geometric_distribution_Int(g_, p)} + public static func geometric_distribution(_ p:Double)->geometric_distribution_Int {return geometric_distribution_Int(p)} } -public final class geometric_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ p:Double) { ptr=geometric_distribution_int_create(p); g=g_ } - public convenience init(_ p:Double) { self.init(RandGen.stored, p) } - deinit { geometric_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return geometric_distribution_int_call(ptr, g.ptr) } + +public final class geometric_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ p:Double) { super.init(geometric_distribution_create(p, Element.init()), g_) } + public convenience init(_ p:Double) { self.init(mt19937.stored, p) } } extension Int32 { - public static func geometric_distribution(_ g_:RandGen, _ p:Double)->geometric_distribution_Int32 {return geometric_distribution_Int32(g_, p)} + public static func geometric_distribution(_ g_:mt19937 , _ p:Double)->geometric_distribution_Int32 {return geometric_distribution_Int32(g_, p)} public static func geometric_distribution(_ p:Double)->geometric_distribution_Int32 {return geometric_distribution_Int32(p)} } -public final class geometric_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ p:Double) { ptr=geometric_distribution_long_create(p); g=g_ } - public convenience init(_ p:Double) { self.init(RandGen.stored, p) } - deinit { geometric_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return geometric_distribution_long_call(ptr, g.ptr) } + +public final class poisson_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ mean:Double) { super.init(poisson_distribution_create(mean, Element.init()), g_) } + public convenience init(_ mean:Double) { self.init(mt19937.stored, mean) } } extension Int { - public static func geometric_distribution(_ g_:RandGen, _ p:Double)->geometric_distribution_Int {return geometric_distribution_Int(g_, p)} - public static func geometric_distribution(_ p:Double)->geometric_distribution_Int {return geometric_distribution_Int(p)} + public static func poisson_distribution(_ g_:mt19937 , _ mean:Double)->poisson_distribution_Int {return poisson_distribution_Int(g_, mean)} + public static func poisson_distribution(_ mean:Double)->poisson_distribution_Int {return poisson_distribution_Int(mean)} } -public final class poisson_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ mean:Double) { ptr=poisson_distribution_int_create(mean); g=g_ } - public convenience init(_ mean:Double) { self.init(RandGen.stored, mean) } - deinit { poisson_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return poisson_distribution_int_call(ptr, g.ptr) } + +public final class poisson_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ mean:Double) { super.init(poisson_distribution_create(mean, Element.init()), g_) } + public convenience init(_ mean:Double) { self.init(mt19937.stored, mean) } } extension Int32 { - public static func poisson_distribution(_ g_:RandGen, _ mean:Double)->poisson_distribution_Int32 {return poisson_distribution_Int32(g_, mean)} + public static func poisson_distribution(_ g_:mt19937 , _ mean:Double)->poisson_distribution_Int32 {return poisson_distribution_Int32(g_, mean)} public static func poisson_distribution(_ mean:Double)->poisson_distribution_Int32 {return poisson_distribution_Int32(mean)} } -public final class poisson_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ mean:Double) { ptr=poisson_distribution_long_create(mean); g=g_ } - public convenience init(_ mean:Double) { self.init(RandGen.stored, mean) } - deinit { poisson_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return poisson_distribution_long_call(ptr, g.ptr) } + +public final class discrete_distribution_Int:Distribution { + public init(_ g_:mt19937 , _ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer) { super.init(discrete_distribution_create(start,end, Element.init()), g_) } + public convenience init(_ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer) { self.init(mt19937.stored, start,end) } } extension Int { - public static func poisson_distribution(_ g_:RandGen, _ mean:Double)->poisson_distribution_Int {return poisson_distribution_Int(g_, mean)} - public static func poisson_distribution(_ mean:Double)->poisson_distribution_Int {return poisson_distribution_Int(mean)} + public static func discrete_distribution(_ g_:mt19937 , _ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer)->discrete_distribution_Int {return discrete_distribution_Int(g_, start,end)} + public static func discrete_distribution(_ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer)->discrete_distribution_Int {return discrete_distribution_Int(start,end)} } -public final class uniform_real_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Float,_ b:Float) { ptr=uniform_real_distribution_float_create(a,b); g=g_ } - public convenience init(_ a:Float,_ b:Float) { self.init(RandGen.stored, a,b) } - deinit { uniform_real_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return uniform_real_distribution_float_call(ptr, g.ptr) } + +public final class discrete_distribution_Int32:Distribution { + public init(_ g_:mt19937 , _ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer) { super.init(discrete_distribution_create(start,end, Element.init()), g_) } + public convenience init(_ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer) { self.init(mt19937.stored, start,end) } +} +extension Int32 { + public static func discrete_distribution(_ g_:mt19937 , _ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer)->discrete_distribution_Int32 {return discrete_distribution_Int32(g_, start,end)} + public static func discrete_distribution(_ start:UnsafeMutablePointer,_ end:UnsafeMutablePointer)->discrete_distribution_Int32 {return discrete_distribution_Int32(start,end)} +} + +public final class uniform_real_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ a:Float,_ b:Float) { super.init(uniform_real_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Float,_ b:Float) { self.init(mt19937.stored, a,b) } } extension Float { - public static func uniform_real_distribution(_ g_:RandGen, _ a:Float,_ b:Float)->uniform_real_distribution_Float {return uniform_real_distribution_Float(g_, a,b)} + public static func uniform_real_distribution(_ g_:mt19937 , _ a:Float,_ b:Float)->uniform_real_distribution_Float {return uniform_real_distribution_Float(g_, a,b)} public static func uniform_real_distribution(_ a:Float,_ b:Float)->uniform_real_distribution_Float {return uniform_real_distribution_Float(a,b)} } -public final class uniform_real_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Double,_ b:Double) { ptr=uniform_real_distribution_double_create(a,b); g=g_ } - public convenience init(_ a:Double,_ b:Double) { self.init(RandGen.stored, a,b) } - deinit { uniform_real_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return uniform_real_distribution_double_call(ptr, g.ptr) } + +public final class uniform_real_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ a:Double,_ b:Double) { super.init(uniform_real_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Double,_ b:Double) { self.init(mt19937.stored, a,b) } } extension Double { - public static func uniform_real_distribution(_ g_:RandGen, _ a:Double,_ b:Double)->uniform_real_distribution_Double {return uniform_real_distribution_Double(g_, a,b)} + public static func uniform_real_distribution(_ g_:mt19937 , _ a:Double,_ b:Double)->uniform_real_distribution_Double {return uniform_real_distribution_Double(g_, a,b)} public static func uniform_real_distribution(_ a:Double,_ b:Double)->uniform_real_distribution_Double {return uniform_real_distribution_Double(a,b)} } -public final class exponential_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ l:Float) { ptr=exponential_distribution_float_create(l); g=g_ } - public convenience init(_ l:Float) { self.init(RandGen.stored, l) } - deinit { exponential_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return exponential_distribution_float_call(ptr, g.ptr) } + +public final class exponential_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ l:Float) { super.init(exponential_distribution_create(l, Element.init()), g_) } + public convenience init(_ l:Float) { self.init(mt19937.stored, l) } } extension Float { - public static func exponential_distribution(_ g_:RandGen, _ l:Float)->exponential_distribution_Float {return exponential_distribution_Float(g_, l)} + public static func exponential_distribution(_ g_:mt19937 , _ l:Float)->exponential_distribution_Float {return exponential_distribution_Float(g_, l)} public static func exponential_distribution(_ l:Float)->exponential_distribution_Float {return exponential_distribution_Float(l)} } -public final class exponential_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ l:Double) { ptr=exponential_distribution_double_create(l); g=g_ } - public convenience init(_ l:Double) { self.init(RandGen.stored, l) } - deinit { exponential_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return exponential_distribution_double_call(ptr, g.ptr) } + +public final class exponential_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ l:Double) { super.init(exponential_distribution_create(l, Element.init()), g_) } + public convenience init(_ l:Double) { self.init(mt19937.stored, l) } } extension Double { - public static func exponential_distribution(_ g_:RandGen, _ l:Double)->exponential_distribution_Double {return exponential_distribution_Double(g_, l)} + public static func exponential_distribution(_ g_:mt19937 , _ l:Double)->exponential_distribution_Double {return exponential_distribution_Double(g_, l)} public static func exponential_distribution(_ l:Double)->exponential_distribution_Double {return exponential_distribution_Double(l)} } -public final class gamma_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Float,_ b:Float) { ptr=gamma_distribution_float_create(a,b); g=g_ } - public convenience init(_ a:Float,_ b:Float) { self.init(RandGen.stored, a,b) } - deinit { gamma_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return gamma_distribution_float_call(ptr, g.ptr) } + +public final class gamma_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ a:Float,_ b:Float) { super.init(gamma_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Float,_ b:Float) { self.init(mt19937.stored, a,b) } } extension Float { - public static func gamma_distribution(_ g_:RandGen, _ a:Float,_ b:Float)->gamma_distribution_Float {return gamma_distribution_Float(g_, a,b)} + public static func gamma_distribution(_ g_:mt19937 , _ a:Float,_ b:Float)->gamma_distribution_Float {return gamma_distribution_Float(g_, a,b)} public static func gamma_distribution(_ a:Float,_ b:Float)->gamma_distribution_Float {return gamma_distribution_Float(a,b)} } -public final class gamma_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Double,_ b:Double) { ptr=gamma_distribution_double_create(a,b); g=g_ } - public convenience init(_ a:Double,_ b:Double) { self.init(RandGen.stored, a,b) } - deinit { gamma_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return gamma_distribution_double_call(ptr, g.ptr) } + +public final class gamma_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ a:Double,_ b:Double) { super.init(gamma_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Double,_ b:Double) { self.init(mt19937.stored, a,b) } } extension Double { - public static func gamma_distribution(_ g_:RandGen, _ a:Double,_ b:Double)->gamma_distribution_Double {return gamma_distribution_Double(g_, a,b)} + public static func gamma_distribution(_ g_:mt19937 , _ a:Double,_ b:Double)->gamma_distribution_Double {return gamma_distribution_Double(g_, a,b)} public static func gamma_distribution(_ a:Double,_ b:Double)->gamma_distribution_Double {return gamma_distribution_Double(a,b)} } -public final class weibull_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Float,_ b:Float) { ptr=weibull_distribution_float_create(a,b); g=g_ } - public convenience init(_ a:Float,_ b:Float) { self.init(RandGen.stored, a,b) } - deinit { weibull_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return weibull_distribution_float_call(ptr, g.ptr) } + +public final class weibull_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ a:Float,_ b:Float) { super.init(weibull_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Float,_ b:Float) { self.init(mt19937.stored, a,b) } } extension Float { - public static func weibull_distribution(_ g_:RandGen, _ a:Float,_ b:Float)->weibull_distribution_Float {return weibull_distribution_Float(g_, a,b)} + public static func weibull_distribution(_ g_:mt19937 , _ a:Float,_ b:Float)->weibull_distribution_Float {return weibull_distribution_Float(g_, a,b)} public static func weibull_distribution(_ a:Float,_ b:Float)->weibull_distribution_Float {return weibull_distribution_Float(a,b)} } -public final class weibull_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Double,_ b:Double) { ptr=weibull_distribution_double_create(a,b); g=g_ } - public convenience init(_ a:Double,_ b:Double) { self.init(RandGen.stored, a,b) } - deinit { weibull_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return weibull_distribution_double_call(ptr, g.ptr) } + +public final class weibull_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ a:Double,_ b:Double) { super.init(weibull_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Double,_ b:Double) { self.init(mt19937.stored, a,b) } } extension Double { - public static func weibull_distribution(_ g_:RandGen, _ a:Double,_ b:Double)->weibull_distribution_Double {return weibull_distribution_Double(g_, a,b)} + public static func weibull_distribution(_ g_:mt19937 , _ a:Double,_ b:Double)->weibull_distribution_Double {return weibull_distribution_Double(g_, a,b)} public static func weibull_distribution(_ a:Double,_ b:Double)->weibull_distribution_Double {return weibull_distribution_Double(a,b)} } -public final class normal_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ mean:Float,_ stddev:Float) { ptr=normal_distribution_float_create(mean,stddev); g=g_ } - public convenience init(_ mean:Float,_ stddev:Float) { self.init(RandGen.stored, mean,stddev) } - deinit { normal_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return normal_distribution_float_call(ptr, g.ptr) } + +public final class normal_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ mean:Float,_ stddev:Float) { super.init(normal_distribution_create(mean,stddev, Element.init()), g_) } + public convenience init(_ mean:Float,_ stddev:Float) { self.init(mt19937.stored, mean,stddev) } } extension Float { - public static func normal_distribution(_ g_:RandGen, _ mean:Float,_ stddev:Float)->normal_distribution_Float {return normal_distribution_Float(g_, mean,stddev)} + public static func normal_distribution(_ g_:mt19937 , _ mean:Float,_ stddev:Float)->normal_distribution_Float {return normal_distribution_Float(g_, mean,stddev)} public static func normal_distribution(_ mean:Float,_ stddev:Float)->normal_distribution_Float {return normal_distribution_Float(mean,stddev)} } -public final class normal_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ mean:Double,_ stddev:Double) { ptr=normal_distribution_double_create(mean,stddev); g=g_ } - public convenience init(_ mean:Double,_ stddev:Double) { self.init(RandGen.stored, mean,stddev) } - deinit { normal_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return normal_distribution_double_call(ptr, g.ptr) } + +public final class normal_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ mean:Double,_ stddev:Double) { super.init(normal_distribution_create(mean,stddev, Element.init()), g_) } + public convenience init(_ mean:Double,_ stddev:Double) { self.init(mt19937.stored, mean,stddev) } } extension Double { - public static func normal_distribution(_ g_:RandGen, _ mean:Double,_ stddev:Double)->normal_distribution_Double {return normal_distribution_Double(g_, mean,stddev)} + public static func normal_distribution(_ g_:mt19937 , _ mean:Double,_ stddev:Double)->normal_distribution_Double {return normal_distribution_Double(g_, mean,stddev)} public static func normal_distribution(_ mean:Double,_ stddev:Double)->normal_distribution_Double {return normal_distribution_Double(mean,stddev)} } -public final class lognormal_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ m:Float,_ s:Float) { ptr=lognormal_distribution_float_create(m,s); g=g_ } - public convenience init(_ m:Float,_ s:Float) { self.init(RandGen.stored, m,s) } - deinit { lognormal_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return lognormal_distribution_float_call(ptr, g.ptr) } + +public final class lognormal_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ m:Float,_ s:Float) { super.init(lognormal_distribution_create(m,s, Element.init()), g_) } + public convenience init(_ m:Float,_ s:Float) { self.init(mt19937.stored, m,s) } } extension Float { - public static func lognormal_distribution(_ g_:RandGen, _ m:Float,_ s:Float)->lognormal_distribution_Float {return lognormal_distribution_Float(g_, m,s)} + public static func lognormal_distribution(_ g_:mt19937 , _ m:Float,_ s:Float)->lognormal_distribution_Float {return lognormal_distribution_Float(g_, m,s)} public static func lognormal_distribution(_ m:Float,_ s:Float)->lognormal_distribution_Float {return lognormal_distribution_Float(m,s)} } -public final class lognormal_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ m:Double,_ s:Double) { ptr=lognormal_distribution_double_create(m,s); g=g_ } - public convenience init(_ m:Double,_ s:Double) { self.init(RandGen.stored, m,s) } - deinit { lognormal_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return lognormal_distribution_double_call(ptr, g.ptr) } + +public final class lognormal_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ m:Double,_ s:Double) { super.init(lognormal_distribution_create(m,s, Element.init()), g_) } + public convenience init(_ m:Double,_ s:Double) { self.init(mt19937.stored, m,s) } } extension Double { - public static func lognormal_distribution(_ g_:RandGen, _ m:Double,_ s:Double)->lognormal_distribution_Double {return lognormal_distribution_Double(g_, m,s)} + public static func lognormal_distribution(_ g_:mt19937 , _ m:Double,_ s:Double)->lognormal_distribution_Double {return lognormal_distribution_Double(g_, m,s)} public static func lognormal_distribution(_ m:Double,_ s:Double)->lognormal_distribution_Double {return lognormal_distribution_Double(m,s)} } -public final class chi_squared_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ n:Float) { ptr=chi_squared_distribution_float_create(n); g=g_ } - public convenience init(_ n:Float) { self.init(RandGen.stored, n) } - deinit { chi_squared_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return chi_squared_distribution_float_call(ptr, g.ptr) } + +public final class chi_squared_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ n:Float) { super.init(chi_squared_distribution_create(n, Element.init()), g_) } + public convenience init(_ n:Float) { self.init(mt19937.stored, n) } } extension Float { - public static func chi_squared_distribution(_ g_:RandGen, _ n:Float)->chi_squared_distribution_Float {return chi_squared_distribution_Float(g_, n)} + public static func chi_squared_distribution(_ g_:mt19937 , _ n:Float)->chi_squared_distribution_Float {return chi_squared_distribution_Float(g_, n)} public static func chi_squared_distribution(_ n:Float)->chi_squared_distribution_Float {return chi_squared_distribution_Float(n)} } -public final class chi_squared_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ n:Double) { ptr=chi_squared_distribution_double_create(n); g=g_ } - public convenience init(_ n:Double) { self.init(RandGen.stored, n) } - deinit { chi_squared_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return chi_squared_distribution_double_call(ptr, g.ptr) } + +public final class chi_squared_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ n:Double) { super.init(chi_squared_distribution_create(n, Element.init()), g_) } + public convenience init(_ n:Double) { self.init(mt19937.stored, n) } } extension Double { - public static func chi_squared_distribution(_ g_:RandGen, _ n:Double)->chi_squared_distribution_Double {return chi_squared_distribution_Double(g_, n)} + public static func chi_squared_distribution(_ g_:mt19937 , _ n:Double)->chi_squared_distribution_Double {return chi_squared_distribution_Double(g_, n)} public static func chi_squared_distribution(_ n:Double)->chi_squared_distribution_Double {return chi_squared_distribution_Double(n)} } -public final class cauchy_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Float,_ b:Float) { ptr=cauchy_distribution_float_create(a,b); g=g_ } - public convenience init(_ a:Float,_ b:Float) { self.init(RandGen.stored, a,b) } - deinit { cauchy_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return cauchy_distribution_float_call(ptr, g.ptr) } + +public final class cauchy_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ a:Float,_ b:Float) { super.init(cauchy_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Float,_ b:Float) { self.init(mt19937.stored, a,b) } } extension Float { - public static func cauchy_distribution(_ g_:RandGen, _ a:Float,_ b:Float)->cauchy_distribution_Float {return cauchy_distribution_Float(g_, a,b)} + public static func cauchy_distribution(_ g_:mt19937 , _ a:Float,_ b:Float)->cauchy_distribution_Float {return cauchy_distribution_Float(g_, a,b)} public static func cauchy_distribution(_ a:Float,_ b:Float)->cauchy_distribution_Float {return cauchy_distribution_Float(a,b)} } -public final class cauchy_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ a:Double,_ b:Double) { ptr=cauchy_distribution_double_create(a,b); g=g_ } - public convenience init(_ a:Double,_ b:Double) { self.init(RandGen.stored, a,b) } - deinit { cauchy_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return cauchy_distribution_double_call(ptr, g.ptr) } + +public final class cauchy_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ a:Double,_ b:Double) { super.init(cauchy_distribution_create(a,b, Element.init()), g_) } + public convenience init(_ a:Double,_ b:Double) { self.init(mt19937.stored, a,b) } } extension Double { - public static func cauchy_distribution(_ g_:RandGen, _ a:Double,_ b:Double)->cauchy_distribution_Double {return cauchy_distribution_Double(g_, a,b)} + public static func cauchy_distribution(_ g_:mt19937 , _ a:Double,_ b:Double)->cauchy_distribution_Double {return cauchy_distribution_Double(g_, a,b)} public static func cauchy_distribution(_ a:Double,_ b:Double)->cauchy_distribution_Double {return cauchy_distribution_Double(a,b)} } -public final class fisher_f_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ m:Float,_ n:Float) { ptr=fisher_f_distribution_float_create(m,n); g=g_ } - public convenience init(_ m:Float,_ n:Float) { self.init(RandGen.stored, m,n) } - deinit { fisher_f_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return fisher_f_distribution_float_call(ptr, g.ptr) } + +public final class fisher_f_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ m:Float,_ n:Float) { super.init(fisher_f_distribution_create(m,n, Element.init()), g_) } + public convenience init(_ m:Float,_ n:Float) { self.init(mt19937.stored, m,n) } } extension Float { - public static func fisher_f_distribution(_ g_:RandGen, _ m:Float,_ n:Float)->fisher_f_distribution_Float {return fisher_f_distribution_Float(g_, m,n)} + public static func fisher_f_distribution(_ g_:mt19937 , _ m:Float,_ n:Float)->fisher_f_distribution_Float {return fisher_f_distribution_Float(g_, m,n)} public static func fisher_f_distribution(_ m:Float,_ n:Float)->fisher_f_distribution_Float {return fisher_f_distribution_Float(m,n)} } -public final class fisher_f_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ m:Double,_ n:Double) { ptr=fisher_f_distribution_double_create(m,n); g=g_ } - public convenience init(_ m:Double,_ n:Double) { self.init(RandGen.stored, m,n) } - deinit { fisher_f_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return fisher_f_distribution_double_call(ptr, g.ptr) } + +public final class fisher_f_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ m:Double,_ n:Double) { super.init(fisher_f_distribution_create(m,n, Element.init()), g_) } + public convenience init(_ m:Double,_ n:Double) { self.init(mt19937.stored, m,n) } } extension Double { - public static func fisher_f_distribution(_ g_:RandGen, _ m:Double,_ n:Double)->fisher_f_distribution_Double {return fisher_f_distribution_Double(g_, m,n)} + public static func fisher_f_distribution(_ g_:mt19937 , _ m:Double,_ n:Double)->fisher_f_distribution_Double {return fisher_f_distribution_Double(g_, m,n)} public static func fisher_f_distribution(_ m:Double,_ n:Double)->fisher_f_distribution_Double {return fisher_f_distribution_Double(m,n)} } -public final class student_t_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ n:Float) { ptr=student_t_distribution_float_create(n); g=g_ } - public convenience init(_ n:Float) { self.init(RandGen.stored, n) } - deinit { student_t_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return student_t_distribution_float_call(ptr, g.ptr) } + +public final class student_t_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ n:Float) { super.init(student_t_distribution_create(n, Element.init()), g_) } + public convenience init(_ n:Float) { self.init(mt19937.stored, n) } } extension Float { - public static func student_t_distribution(_ g_:RandGen, _ n:Float)->student_t_distribution_Float {return student_t_distribution_Float(g_, n)} + public static func student_t_distribution(_ g_:mt19937 , _ n:Float)->student_t_distribution_Float {return student_t_distribution_Float(g_, n)} public static func student_t_distribution(_ n:Float)->student_t_distribution_Float {return student_t_distribution_Float(n)} } -public final class student_t_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ n:Double) { ptr=student_t_distribution_double_create(n); g=g_ } - public convenience init(_ n:Double) { self.init(RandGen.stored, n) } - deinit { student_t_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return student_t_distribution_double_call(ptr, g.ptr) } + +public final class student_t_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ n:Double) { super.init(student_t_distribution_create(n, Element.init()), g_) } + public convenience init(_ n:Double) { self.init(mt19937.stored, n) } } extension Double { - public static func student_t_distribution(_ g_:RandGen, _ n:Double)->student_t_distribution_Double {return student_t_distribution_Double(g_, n)} + public static func student_t_distribution(_ g_:mt19937 , _ n:Double)->student_t_distribution_Double {return student_t_distribution_Double(g_, n)} public static func student_t_distribution(_ n:Double)->student_t_distribution_Double {return student_t_distribution_Double(n)} } - -public class discrete_distribution_Int:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ d_s:[Double]) { ptr=discrete_distribution_long_create(d_s.p, d_s.p+d_s.count); g=g_; } - public convenience init(_ d_s:[Double]) { self.init(RandGen.stored, d_s) } - deinit { discrete_distribution_long_destroy(ptr) } - @inlinable public subscript()->Int { return discrete_distribution_long_call(ptr, g.ptr) } +public final class piecewise_constant_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { super.init(piecewise_constant_distribution_create(start_i,end_i,start_w, Element.init()), g_) } + public convenience init(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { self.init(mt19937.stored, start_i,end_i,start_w) } } -extension Int { - public static func discrete_distribution(_ g_:RandGen, _ d_s:[Double])->discrete_distribution_Int {return discrete_distribution_Int(g_, d_s)} - public static func discrete_distribution(_ d_s:[Double])->discrete_distribution_Int {return discrete_distribution_Int(d_s)} +extension Float { + public static func piecewise_constant_distribution(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_constant_distribution_Float {return piecewise_constant_distribution_Float(g_, start_i,end_i,start_w)} + public static func piecewise_constant_distribution(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_constant_distribution_Float {return piecewise_constant_distribution_Float(start_i,end_i,start_w)} } - -public class discrete_distribution_Int32:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ d_s:[Double]) { ptr=discrete_distribution_int_create(d_s.p, d_s.p+d_s.count); g=g_; } - public convenience init(_ d_s:[Double]) { self.init(RandGen.stored, d_s) } - deinit { discrete_distribution_int_destroy(ptr) } - @inlinable public subscript()->Int32 { return discrete_distribution_int_call(ptr, g.ptr) } +public final class piecewise_constant_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { super.init(piecewise_constant_distribution_create(start_i,end_i,start_w, Element.init()), g_) } + public convenience init(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { self.init(mt19937.stored, start_i,end_i,start_w) } } -extension Int32 { - public static func discrete_distribution(_ g_:RandGen, _ d_s:[Double])->discrete_distribution_Int32 {return discrete_distribution_Int32(g_, d_s)} - public static func discrete_distribution(_ d_s:[Double])->discrete_distribution_Int32 {return discrete_distribution_Int32(d_s)} +extension Double { + public static func piecewise_constant_distribution(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_constant_distribution_Double {return piecewise_constant_distribution_Double(g_, start_i,end_i,start_w)} + public static func piecewise_constant_distribution(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_constant_distribution_Double {return piecewise_constant_distribution_Double(start_i,end_i,start_w)} } - - -public class piecewise_constant_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double]) { ptr=piecewise_constant_distribution_float_create(i_s.p, i_s.p+i_s.count, w_s.p); g=g_; } - public convenience init(_ i_s:[Double], _ w_s:[Double]) { self.init(RandGen.stored, i_s, w_s) } - deinit { piecewise_constant_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return piecewise_constant_distribution_float_call(ptr, g.ptr) } +public final class piecewise_linear_distribution_Float:Distribution { + public init(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { super.init(piecewise_linear_distribution_create(start_i,end_i,start_w, Element.init()), g_) } + public convenience init(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { self.init(mt19937.stored, start_i,end_i,start_w) } } extension Float { - public static func piecewise_constant_distribution(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Float {return piecewise_constant_distribution_Float(g_, i_s, w_s)} - public static func piecewise_constant_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Float {return piecewise_constant_distribution_Float(i_s, w_s)} + public static func piecewise_linear_distribution(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_linear_distribution_Float {return piecewise_linear_distribution_Float(g_, start_i,end_i,start_w)} + public static func piecewise_linear_distribution(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_linear_distribution_Float {return piecewise_linear_distribution_Float(start_i,end_i,start_w)} } - -public class piecewise_constant_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double]) { ptr=piecewise_constant_distribution_double_create(i_s.p, i_s.p+i_s.count, w_s.p); g=g_; } - public convenience init(_ i_s:[Double], _ w_s:[Double]) { self.init(RandGen.stored, i_s, w_s) } - deinit { piecewise_constant_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return piecewise_constant_distribution_double_call(ptr, g.ptr) } +public final class piecewise_linear_distribution_Double:Distribution { + public init(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { super.init(piecewise_linear_distribution_create(start_i,end_i,start_w, Element.init()), g_) } + public convenience init(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer) { self.init(mt19937.stored, start_i,end_i,start_w) } } extension Double { - public static func piecewise_constant_distribution(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Double {return piecewise_constant_distribution_Double(g_, i_s, w_s)} - public static func piecewise_constant_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Double {return piecewise_constant_distribution_Double(i_s, w_s)} + public static func piecewise_linear_distribution(_ g_:mt19937 , _ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_linear_distribution_Double {return piecewise_linear_distribution_Double(g_, start_i,end_i,start_w)} + public static func piecewise_linear_distribution(_ start_i:UnsafeMutablePointer,_ end_i:UnsafeMutablePointer,_ start_w:UnsafeMutablePointer)->piecewise_linear_distribution_Double {return piecewise_linear_distribution_Double(start_i,end_i,start_w)} } - -public class piecewise_linear_distribution_Float:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double]) { ptr=piecewise_linear_distribution_float_create(i_s.p, i_s.p+i_s.count, w_s.p); g=g_; } - public convenience init(_ i_s:[Double], _ w_s:[Double]) { self.init(RandGen.stored, i_s, w_s) } - deinit { piecewise_linear_distribution_float_destroy(ptr) } - @inlinable public subscript()->Float { return piecewise_linear_distribution_float_call(ptr, g.ptr) } +extension Int { + public static func discrete_distribution(_ d_s:[Double])->discrete_distribution_Int { + return discrete_distribution(d_s.p, d_s.p+d_s.count) + } } -extension Float { - public static func piecewise_linear_distribution(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Float {return piecewise_linear_distribution_Float(g_, i_s, w_s)} - public static func piecewise_linear_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Float {return piecewise_linear_distribution_Float(i_s, w_s)} +extension Int32 { + public static func discrete_distribution(_ d_s:[Double])->discrete_distribution_Int32 { + return discrete_distribution(d_s.p, d_s.p+d_s.count) + } } -public class piecewise_linear_distribution_Double:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double]) { ptr=piecewise_linear_distribution_double_create(i_s.p, i_s.p+i_s.count, w_s.p); g=g_; } - public convenience init(_ i_s:[Double], _ w_s:[Double]) { self.init(RandGen.stored, i_s, w_s) } - deinit { piecewise_linear_distribution_double_destroy(ptr) } - @inlinable public subscript()->Double { return piecewise_linear_distribution_double_call(ptr, g.ptr) } +extension Float { + public static func piecewise_constant_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Float { + return piecewise_constant_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } + public static func piecewise_linear_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Float { + return piecewise_linear_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } } extension Double { - public static func piecewise_linear_distribution(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Double {return piecewise_linear_distribution_Double(g_, i_s, w_s)} - public static func piecewise_linear_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Double {return piecewise_linear_distribution_Double(i_s, w_s)} + public static func piecewise_constant_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_Double { + return piecewise_constant_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } + public static func piecewise_linear_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_Double { + return piecewise_linear_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } } - diff --git a/Sources/BaseMath/BaseRand.swift.gyb b/Sources/BaseMath/BaseRand.swift.gyb index e508385..f2f2675 100644 --- a/Sources/BaseMath/BaseRand.swift.gyb +++ b/Sources/BaseMath/BaseRand.swift.gyb @@ -1,59 +1,44 @@ import Foundation import CBaseMath -public class RandGen { - public let ptr:OpaquePointer? - public init() { ptr=RandGen_create() } - deinit { RandGen_destroy(ptr) } - - @usableFromInline static var storeKey:String { get { return "RandGen" } } - @usableFromInline static var stored:RandGen { get { - if let r = Thread.current.threadDictionary[storeKey] as? RandGen { return r } - return Thread.setToTLS(RandGen(), storeKey) - }} -} - -%{ -int_types = [ - ['uniform_int_distribution', '_ a:#, _ b:#', 'a,b'], - ['binomial_distribution', '_ t:#, _ p:Double', 't,p'], - ['negative_binomial_distribution', '_ k:#, _ p:Double', 'k,p'], - ['geometric_distribution', '_ p:Double', 'p'], - ['poisson_distribution', '_ mean:Double', 'mean'], -] -real_types = [ - ['uniform_real_distribution', '_ a:#,_ b:#', 'a,b'], - ['exponential_distribution', '_ l:#', 'l'], - ['gamma_distribution', '_ a:#,_ b:#', 'a,b'], - ['weibull_distribution', '_ a:#,_ b:#', 'a,b'], - ['normal_distribution', '_ mean:#,_ stddev:#', 'mean,stddev'], - ['lognormal_distribution', '_ m:#,_ s:#', 'm,s'], - ['chi_squared_distribution', '_ n:#', 'n'], - ['cauchy_distribution', '_ a:#,_ b:#', 'a,b'], - ['fisher_f_distribution', '_ m:#,_ n:#', 'm,n'], - ['student_t_distribution', '_ n:#', 'n'], -] -bool_types = [ - #['bernoulli_distribution', '_ p:Double', 'p'], -] -types = [(n,t1,t2,p1.replace('#',t1),p2) for n,p1,p2 in int_types for t1,t2 in [['Int32','int'],['Int','long']]] -types += [(n,t1,t2,p1.replace('#',t1),p2) for n,p1,p2 in real_types for t1,t2 in [['Float','float'],['Double','double']]] -types += [(n,'Bool','bool',p1,p2) for n,p1,p2 in bool_types] -}% +%{ import sys; sys.path.append('../..'); from cpp_template import *; from cpp_types import * }% public protocol Initable { init() } -% for t in "Float Double Int Int32 Bool".split(): +% for t in float_swift+int_swift+['Bool']: extension ${t}:Initable {} % end -public protocol Distribution:Nullary { - subscript()->Element {get} - subscript(n:Int)->[Element] {get} - func gen_array(_ n:Int)->[Element] - func gen_aligned(_ n:Int)->AlignedStorage - func gen_pointer(_ n:Int)->UnsafeMutableBufferPointer +public protocol CppTypePtr { + func delete() +} + +public class CppType { + public let p:T + init(_ p:T) {self.p=p} + deinit {p.delete()} +} + +public class mt19937:CppType { + public convenience init() { self.init(CBaseMath.mt19937_create()) } + + @usableFromInline static var storeKey:String { get { return "mt19937" } } + @usableFromInline static var stored:mt19937 { get { + if let r = Thread.current.threadDictionary[storeKey] as? mt19937 { return r } + return Thread.setToTLS(mt19937(), storeKey) + }} +} + +public protocol DistributionC:CppTypePtr { + associatedtype Element:SignedNumeric + func call(_ g:mt19937C)->Element } -extension Distribution { + +public class Distribution:CppType,Nullary { + public typealias Element=T.Element + public var g:mt19937 + + init(_ p:T, _ g:mt19937) {self.g=g; super.init(p) } + @inlinable public subscript()->Element { return p.call(g.p) } public subscript(n:Int)->[Element] { return gen_array(n) } public func gen_array(_ n:Int)->[Element] { return [Element].fill(self, n) @@ -66,55 +51,49 @@ extension Distribution { } } -% for n,t1,t2,p1,p2 in types: -public final class ${n}_${t1}:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, ${p1}) { ptr=${n}_${t2}_create(${p2}); g=g_ } - public convenience init(${p1}) { self.init(RandGen.stored, ${p2}) } - deinit { ${n}_${t2}_destroy(ptr) } - @inlinable public subscript()->${t1} { return ${n}_${t2}_call(ptr, g.ptr) } +% for o in dist_types: + % for gs,gc in o.generics: +%{ +n = o.typ +p1 = o.pswift.replace('#',gs) +p2 = o.p2 +sep = "," if p1 else "" +}% + +public final class ${n}_${gs}:Distribution<${n}${gc}C> { + public init(_ g_:mt19937 ${sep} ${p1}) { super.init(${n}_create(${p2}, Element.init()), g_) } + public convenience init(${p1}) { self.init(mt19937.stored, ${p2}) } } -extension ${t1} { - public static func ${n}(_ g_:RandGen, ${p1})->${n}_${t1} {return ${n}_${t1}(g_, ${p2})} - public static func ${n}(${p1})->${n}_${t1} {return ${n}_${t1}(${p2})} +% if not gs: +/* +% end +extension ${gs} { + public static func ${n}(_ g_:mt19937 ${sep} ${p1})->${n}_${gs} {return ${n}_${gs}(g_, ${p2})} + public static func ${n}(${p1})->${n}_${gs} {return ${n}_${gs}(${p2})} } +% if not gs: +*/ % end + % end # g +% end # o -%{ n = 'discrete_distribution' }% -% for t1,t2 in [['Int', 'long'], ['Int32', 'int']]: - -public class ${n}_${t1}:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ d_s:[Double]) { ptr=${n}_${t2}_create(d_s.p, d_s.p+d_s.count); g=g_; } - public convenience init(_ d_s:[Double]) { self.init(RandGen.stored, d_s) } - deinit { ${n}_${t2}_destroy(ptr) } - @inlinable public subscript()->${t1} { return ${n}_${t2}_call(ptr, g.ptr) } -} -extension ${t1} { - public static func ${n}(_ g_:RandGen, _ d_s:[Double])->${n}_${t1} {return ${n}_${t1}(g_, d_s)} - public static func ${n}(_ d_s:[Double])->${n}_${t1} {return ${n}_${t1}(d_s)} +% for t in int_swift: +extension ${t} { + public static func discrete_distribution(_ d_s:[Double])->discrete_distribution_${t} { + return discrete_distribution(d_s.p, d_s.p+d_s.count) + } } - % end -% for n in ['piecewise_constant_distribution', 'piecewise_linear_distribution']: - % for t1,t2 in [['Float', 'float'], ['Double', 'double']]: -public class ${n}_${t1}:Distribution { - public let ptr:OpaquePointer? - @usableFromInline let g:RandGen - public init(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double]) { ptr=${n}_${t2}_create(i_s.p, i_s.p+i_s.count, w_s.p); g=g_; } - public convenience init(_ i_s:[Double], _ w_s:[Double]) { self.init(RandGen.stored, i_s, w_s) } - deinit { ${n}_${t2}_destroy(ptr) } - @inlinable public subscript()->${t1} { return ${n}_${t2}_call(ptr, g.ptr) } -} -extension ${t1} { - public static func ${n}(_ g_:RandGen, _ i_s:[Double], _ w_s:[Double])->${n}_${t1} {return ${n}_${t1}(g_, i_s, w_s)} - public static func ${n}(_ i_s:[Double], _ w_s:[Double])->${n}_${t1} {return ${n}_${t1}(i_s, w_s)} +% for t in float_swift: +extension ${t} { + public static func piecewise_constant_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_constant_distribution_${t} { + return piecewise_constant_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } + public static func piecewise_linear_distribution(_ i_s:[Double], _ w_s:[Double])->piecewise_linear_distribution_${t} { + return piecewise_linear_distribution(i_s.p, i_s.p+i_s.count, w_s.p) + } } - - % end % end diff --git a/Sources/BaseMath/CBaseRand.swift b/Sources/BaseMath/CBaseRand.swift new file mode 100644 index 0000000..4f6393e --- /dev/null +++ b/Sources/BaseMath/CBaseRand.swift @@ -0,0 +1,239 @@ +import Foundation +import CBaseMath + + + +extension uniform_int_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension uniform_int_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension binomial_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension binomial_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension negative_binomial_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension negative_binomial_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension geometric_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension geometric_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension poisson_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension poisson_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension discrete_distributionlongC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int { return CBaseMath.call(self,g) } +} + + +extension discrete_distributionintC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Int32 { return CBaseMath.call(self,g) } +} + + +extension uniform_real_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension uniform_real_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension exponential_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension exponential_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension gamma_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension gamma_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension weibull_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension weibull_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension normal_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension normal_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension lognormal_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension lognormal_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension chi_squared_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension chi_squared_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension cauchy_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension cauchy_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension fisher_f_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension fisher_f_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension student_t_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension student_t_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension piecewise_constant_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension piecewise_constant_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension piecewise_linear_distributionfloatC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Float { return CBaseMath.call(self,g) } +} + + +extension piecewise_linear_distributiondoubleC:DistributionC { + public func delete() {destroy(self)} + public func call(_ g:mt19937C)->Double { return CBaseMath.call(self,g) } +} + + +extension mt19937C:CppTypePtr { + public func delete() {destroy(self)} + +} + + +extension knuth_bC:CppTypePtr { + public func delete() {destroy(self)} + +} + + +extension bernoulli_distributionC:CppTypePtr { + public func delete() {destroy(self)} + +} + + diff --git a/Sources/BaseMath/CBaseRand.swift.gyb b/Sources/BaseMath/CBaseRand.swift.gyb new file mode 100644 index 0000000..fc180dc --- /dev/null +++ b/Sources/BaseMath/CBaseRand.swift.gyb @@ -0,0 +1,9 @@ +import Foundation +import CBaseMath + +%{ import sys; sys.path.append('../..'); from cpp_template import *; from cpp_types import * }% + +% for o in all_types: +${o.swift()} +% end + diff --git a/Sources/CBaseMath/CBaseMath.cpp b/Sources/CBaseMath/CBaseMath.cpp index df197b8..8950581 100644 --- a/Sources/CBaseMath/CBaseMath.cpp +++ b/Sources/CBaseMath/CBaseMath.cpp @@ -1,279 +1,10 @@ #include "include/CBaseMath.h" -#include #include using namespace std; -extern "C" { - -struct RandGenC:mt19937 {}; -typedef struct RandGenC RandGenC; -RandGenC* RandGen_create() { return (RandGenC*)(new mt19937(random_device()())); } -void RandGen_destroy(RandGenC* v) { delete(v); } - - - -struct uniform_int_distribution_intC:uniform_int_distribution {}; -typedef struct uniform_int_distribution_intC uniform_int_distribution_intC; -uniform_int_distribution_intC* uniform_int_distribution_int_create(int a,int b) { return (uniform_int_distribution_intC*)(new uniform_int_distribution(a,b)); } -void uniform_int_distribution_int_destroy(uniform_int_distribution_intC* v) { delete(v); } -int uniform_int_distribution_int_call(uniform_int_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct uniform_int_distribution_longC:uniform_int_distribution {}; -typedef struct uniform_int_distribution_longC uniform_int_distribution_longC; -uniform_int_distribution_longC* uniform_int_distribution_long_create(long a,long b) { return (uniform_int_distribution_longC*)(new uniform_int_distribution(a,b)); } -void uniform_int_distribution_long_destroy(uniform_int_distribution_longC* v) { delete(v); } -long uniform_int_distribution_long_call(uniform_int_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct binomial_distribution_intC:binomial_distribution {}; -typedef struct binomial_distribution_intC binomial_distribution_intC; -binomial_distribution_intC* binomial_distribution_int_create(int t, double p) { return (binomial_distribution_intC*)(new binomial_distribution(t,p)); } -void binomial_distribution_int_destroy(binomial_distribution_intC* v) { delete(v); } -int binomial_distribution_int_call(binomial_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct binomial_distribution_longC:binomial_distribution {}; -typedef struct binomial_distribution_longC binomial_distribution_longC; -binomial_distribution_longC* binomial_distribution_long_create(long t, double p) { return (binomial_distribution_longC*)(new binomial_distribution(t,p)); } -void binomial_distribution_long_destroy(binomial_distribution_longC* v) { delete(v); } -long binomial_distribution_long_call(binomial_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct negative_binomial_distribution_intC:negative_binomial_distribution {}; -typedef struct negative_binomial_distribution_intC negative_binomial_distribution_intC; -negative_binomial_distribution_intC* negative_binomial_distribution_int_create(int k, double p) { return (negative_binomial_distribution_intC*)(new negative_binomial_distribution(k,p)); } -void negative_binomial_distribution_int_destroy(negative_binomial_distribution_intC* v) { delete(v); } -int negative_binomial_distribution_int_call(negative_binomial_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct negative_binomial_distribution_longC:negative_binomial_distribution {}; -typedef struct negative_binomial_distribution_longC negative_binomial_distribution_longC; -negative_binomial_distribution_longC* negative_binomial_distribution_long_create(long k, double p) { return (negative_binomial_distribution_longC*)(new negative_binomial_distribution(k,p)); } -void negative_binomial_distribution_long_destroy(negative_binomial_distribution_longC* v) { delete(v); } -long negative_binomial_distribution_long_call(negative_binomial_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct geometric_distribution_intC:geometric_distribution {}; -typedef struct geometric_distribution_intC geometric_distribution_intC; -geometric_distribution_intC* geometric_distribution_int_create(double p) { return (geometric_distribution_intC*)(new geometric_distribution(p)); } -void geometric_distribution_int_destroy(geometric_distribution_intC* v) { delete(v); } -int geometric_distribution_int_call(geometric_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct geometric_distribution_longC:geometric_distribution {}; -typedef struct geometric_distribution_longC geometric_distribution_longC; -geometric_distribution_longC* geometric_distribution_long_create(double p) { return (geometric_distribution_longC*)(new geometric_distribution(p)); } -void geometric_distribution_long_destroy(geometric_distribution_longC* v) { delete(v); } -long geometric_distribution_long_call(geometric_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct poisson_distribution_intC:poisson_distribution {}; -typedef struct poisson_distribution_intC poisson_distribution_intC; -poisson_distribution_intC* poisson_distribution_int_create(double mean) { return (poisson_distribution_intC*)(new poisson_distribution(mean)); } -void poisson_distribution_int_destroy(poisson_distribution_intC* v) { delete(v); } -int poisson_distribution_int_call(poisson_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct poisson_distribution_longC:poisson_distribution {}; -typedef struct poisson_distribution_longC poisson_distribution_longC; -poisson_distribution_longC* poisson_distribution_long_create(double mean) { return (poisson_distribution_longC*)(new poisson_distribution(mean)); } -void poisson_distribution_long_destroy(poisson_distribution_longC* v) { delete(v); } -long poisson_distribution_long_call(poisson_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct discrete_distribution_intC:discrete_distribution {}; -typedef struct discrete_distribution_intC discrete_distribution_intC; -discrete_distribution_intC* discrete_distribution_int_create(double* start, double* end) { return (discrete_distribution_intC*)(new discrete_distribution(start,end)); } -void discrete_distribution_int_destroy(discrete_distribution_intC* v) { delete(v); } -int discrete_distribution_int_call(discrete_distribution_intC* p, RandGenC* g) {return (*p)(*g);} - - -struct discrete_distribution_longC:discrete_distribution {}; -typedef struct discrete_distribution_longC discrete_distribution_longC; -discrete_distribution_longC* discrete_distribution_long_create(double* start, double* end) { return (discrete_distribution_longC*)(new discrete_distribution(start,end)); } -void discrete_distribution_long_destroy(discrete_distribution_longC* v) { delete(v); } -long discrete_distribution_long_call(discrete_distribution_longC* p, RandGenC* g) {return (*p)(*g);} - - -struct uniform_real_distribution_floatC:uniform_real_distribution {}; -typedef struct uniform_real_distribution_floatC uniform_real_distribution_floatC; -uniform_real_distribution_floatC* uniform_real_distribution_float_create(float a,float b) { return (uniform_real_distribution_floatC*)(new uniform_real_distribution(a,b)); } -void uniform_real_distribution_float_destroy(uniform_real_distribution_floatC* v) { delete(v); } -float uniform_real_distribution_float_call(uniform_real_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct uniform_real_distribution_doubleC:uniform_real_distribution {}; -typedef struct uniform_real_distribution_doubleC uniform_real_distribution_doubleC; -uniform_real_distribution_doubleC* uniform_real_distribution_double_create(double a,double b) { return (uniform_real_distribution_doubleC*)(new uniform_real_distribution(a,b)); } -void uniform_real_distribution_double_destroy(uniform_real_distribution_doubleC* v) { delete(v); } -double uniform_real_distribution_double_call(uniform_real_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct exponential_distribution_floatC:exponential_distribution {}; -typedef struct exponential_distribution_floatC exponential_distribution_floatC; -exponential_distribution_floatC* exponential_distribution_float_create(float l) { return (exponential_distribution_floatC*)(new exponential_distribution(l)); } -void exponential_distribution_float_destroy(exponential_distribution_floatC* v) { delete(v); } -float exponential_distribution_float_call(exponential_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct exponential_distribution_doubleC:exponential_distribution {}; -typedef struct exponential_distribution_doubleC exponential_distribution_doubleC; -exponential_distribution_doubleC* exponential_distribution_double_create(double l) { return (exponential_distribution_doubleC*)(new exponential_distribution(l)); } -void exponential_distribution_double_destroy(exponential_distribution_doubleC* v) { delete(v); } -double exponential_distribution_double_call(exponential_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct gamma_distribution_floatC:gamma_distribution {}; -typedef struct gamma_distribution_floatC gamma_distribution_floatC; -gamma_distribution_floatC* gamma_distribution_float_create(float a,float b) { return (gamma_distribution_floatC*)(new gamma_distribution(a,b)); } -void gamma_distribution_float_destroy(gamma_distribution_floatC* v) { delete(v); } -float gamma_distribution_float_call(gamma_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct gamma_distribution_doubleC:gamma_distribution {}; -typedef struct gamma_distribution_doubleC gamma_distribution_doubleC; -gamma_distribution_doubleC* gamma_distribution_double_create(double a,double b) { return (gamma_distribution_doubleC*)(new gamma_distribution(a,b)); } -void gamma_distribution_double_destroy(gamma_distribution_doubleC* v) { delete(v); } -double gamma_distribution_double_call(gamma_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct weibull_distribution_floatC:weibull_distribution {}; -typedef struct weibull_distribution_floatC weibull_distribution_floatC; -weibull_distribution_floatC* weibull_distribution_float_create(float a,float b) { return (weibull_distribution_floatC*)(new weibull_distribution(a,b)); } -void weibull_distribution_float_destroy(weibull_distribution_floatC* v) { delete(v); } -float weibull_distribution_float_call(weibull_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct weibull_distribution_doubleC:weibull_distribution {}; -typedef struct weibull_distribution_doubleC weibull_distribution_doubleC; -weibull_distribution_doubleC* weibull_distribution_double_create(double a,double b) { return (weibull_distribution_doubleC*)(new weibull_distribution(a,b)); } -void weibull_distribution_double_destroy(weibull_distribution_doubleC* v) { delete(v); } -double weibull_distribution_double_call(weibull_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct normal_distribution_floatC:normal_distribution {}; -typedef struct normal_distribution_floatC normal_distribution_floatC; -normal_distribution_floatC* normal_distribution_float_create(float mean,float stddev) { return (normal_distribution_floatC*)(new normal_distribution(mean,stddev)); } -void normal_distribution_float_destroy(normal_distribution_floatC* v) { delete(v); } -float normal_distribution_float_call(normal_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct normal_distribution_doubleC:normal_distribution {}; -typedef struct normal_distribution_doubleC normal_distribution_doubleC; -normal_distribution_doubleC* normal_distribution_double_create(double mean,double stddev) { return (normal_distribution_doubleC*)(new normal_distribution(mean,stddev)); } -void normal_distribution_double_destroy(normal_distribution_doubleC* v) { delete(v); } -double normal_distribution_double_call(normal_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct lognormal_distribution_floatC:lognormal_distribution {}; -typedef struct lognormal_distribution_floatC lognormal_distribution_floatC; -lognormal_distribution_floatC* lognormal_distribution_float_create(float m,float s) { return (lognormal_distribution_floatC*)(new lognormal_distribution(m,s)); } -void lognormal_distribution_float_destroy(lognormal_distribution_floatC* v) { delete(v); } -float lognormal_distribution_float_call(lognormal_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct lognormal_distribution_doubleC:lognormal_distribution {}; -typedef struct lognormal_distribution_doubleC lognormal_distribution_doubleC; -lognormal_distribution_doubleC* lognormal_distribution_double_create(double m,double s) { return (lognormal_distribution_doubleC*)(new lognormal_distribution(m,s)); } -void lognormal_distribution_double_destroy(lognormal_distribution_doubleC* v) { delete(v); } -double lognormal_distribution_double_call(lognormal_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct chi_squared_distribution_floatC:chi_squared_distribution {}; -typedef struct chi_squared_distribution_floatC chi_squared_distribution_floatC; -chi_squared_distribution_floatC* chi_squared_distribution_float_create(float n) { return (chi_squared_distribution_floatC*)(new chi_squared_distribution(n)); } -void chi_squared_distribution_float_destroy(chi_squared_distribution_floatC* v) { delete(v); } -float chi_squared_distribution_float_call(chi_squared_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct chi_squared_distribution_doubleC:chi_squared_distribution {}; -typedef struct chi_squared_distribution_doubleC chi_squared_distribution_doubleC; -chi_squared_distribution_doubleC* chi_squared_distribution_double_create(double n) { return (chi_squared_distribution_doubleC*)(new chi_squared_distribution(n)); } -void chi_squared_distribution_double_destroy(chi_squared_distribution_doubleC* v) { delete(v); } -double chi_squared_distribution_double_call(chi_squared_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct cauchy_distribution_floatC:cauchy_distribution {}; -typedef struct cauchy_distribution_floatC cauchy_distribution_floatC; -cauchy_distribution_floatC* cauchy_distribution_float_create(float a,float b) { return (cauchy_distribution_floatC*)(new cauchy_distribution(a,b)); } -void cauchy_distribution_float_destroy(cauchy_distribution_floatC* v) { delete(v); } -float cauchy_distribution_float_call(cauchy_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct cauchy_distribution_doubleC:cauchy_distribution {}; -typedef struct cauchy_distribution_doubleC cauchy_distribution_doubleC; -cauchy_distribution_doubleC* cauchy_distribution_double_create(double a,double b) { return (cauchy_distribution_doubleC*)(new cauchy_distribution(a,b)); } -void cauchy_distribution_double_destroy(cauchy_distribution_doubleC* v) { delete(v); } -double cauchy_distribution_double_call(cauchy_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct fisher_f_distribution_floatC:fisher_f_distribution {}; -typedef struct fisher_f_distribution_floatC fisher_f_distribution_floatC; -fisher_f_distribution_floatC* fisher_f_distribution_float_create(float m,float n) { return (fisher_f_distribution_floatC*)(new fisher_f_distribution(m,n)); } -void fisher_f_distribution_float_destroy(fisher_f_distribution_floatC* v) { delete(v); } -float fisher_f_distribution_float_call(fisher_f_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct fisher_f_distribution_doubleC:fisher_f_distribution {}; -typedef struct fisher_f_distribution_doubleC fisher_f_distribution_doubleC; -fisher_f_distribution_doubleC* fisher_f_distribution_double_create(double m,double n) { return (fisher_f_distribution_doubleC*)(new fisher_f_distribution(m,n)); } -void fisher_f_distribution_double_destroy(fisher_f_distribution_doubleC* v) { delete(v); } -double fisher_f_distribution_double_call(fisher_f_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct student_t_distribution_floatC:student_t_distribution {}; -typedef struct student_t_distribution_floatC student_t_distribution_floatC; -student_t_distribution_floatC* student_t_distribution_float_create(float n) { return (student_t_distribution_floatC*)(new student_t_distribution(n)); } -void student_t_distribution_float_destroy(student_t_distribution_floatC* v) { delete(v); } -float student_t_distribution_float_call(student_t_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct student_t_distribution_doubleC:student_t_distribution {}; -typedef struct student_t_distribution_doubleC student_t_distribution_doubleC; -student_t_distribution_doubleC* student_t_distribution_double_create(double n) { return (student_t_distribution_doubleC*)(new student_t_distribution(n)); } -void student_t_distribution_double_destroy(student_t_distribution_doubleC* v) { delete(v); } -double student_t_distribution_double_call(student_t_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct piecewise_constant_distribution_floatC:piecewise_constant_distribution {}; -typedef struct piecewise_constant_distribution_floatC piecewise_constant_distribution_floatC; -piecewise_constant_distribution_floatC* piecewise_constant_distribution_float_create(double* start_i, double* end_i, double* start_w) { return (piecewise_constant_distribution_floatC*)(new piecewise_constant_distribution(start_i,end_i,start_w)); } -void piecewise_constant_distribution_float_destroy(piecewise_constant_distribution_floatC* v) { delete(v); } -float piecewise_constant_distribution_float_call(piecewise_constant_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct piecewise_constant_distribution_doubleC:piecewise_constant_distribution {}; -typedef struct piecewise_constant_distribution_doubleC piecewise_constant_distribution_doubleC; -piecewise_constant_distribution_doubleC* piecewise_constant_distribution_double_create(double* start_i, double* end_i, double* start_w) { return (piecewise_constant_distribution_doubleC*)(new piecewise_constant_distribution(start_i,end_i,start_w)); } -void piecewise_constant_distribution_double_destroy(piecewise_constant_distribution_doubleC* v) { delete(v); } -double piecewise_constant_distribution_double_call(piecewise_constant_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct piecewise_linear_distribution_floatC:piecewise_linear_distribution {}; -typedef struct piecewise_linear_distribution_floatC piecewise_linear_distribution_floatC; -piecewise_linear_distribution_floatC* piecewise_linear_distribution_float_create(double* start_i, double* end_i, double* start_w) { return (piecewise_linear_distribution_floatC*)(new piecewise_linear_distribution(start_i,end_i,start_w)); } -void piecewise_linear_distribution_float_destroy(piecewise_linear_distribution_floatC* v) { delete(v); } -float piecewise_linear_distribution_float_call(piecewise_linear_distribution_floatC* p, RandGenC* g) {return (*p)(*g);} - - -struct piecewise_linear_distribution_doubleC:piecewise_linear_distribution {}; -typedef struct piecewise_linear_distribution_doubleC piecewise_linear_distribution_doubleC; -piecewise_linear_distribution_doubleC* piecewise_linear_distribution_double_create(double* start_i, double* end_i, double* start_w) { return (piecewise_linear_distribution_doubleC*)(new piecewise_linear_distribution(start_i,end_i,start_w)); } -void piecewise_linear_distribution_double_destroy(piecewise_linear_distribution_doubleC* v) { delete(v); } -double piecewise_linear_distribution_double_call(piecewise_linear_distribution_doubleC* p, RandGenC* g) {return (*p)(*g);} - - -struct bernoulli_distribution_boolC:bernoulli_distribution {}; -typedef struct bernoulli_distribution_boolC bernoulli_distribution_boolC; -bernoulli_distribution_boolC* bernoulli_distribution_bool_create(double p) { return (bernoulli_distribution_boolC*)(new bernoulli_distribution(p)); } -void bernoulli_distribution_bool_destroy(bernoulli_distribution_boolC* v) { delete(v); } -bool bernoulli_distribution_bool_call(bernoulli_distribution_boolC* p, RandGenC* g) {return (*p)(*g);} - - void sm_add_float(const float* __restrict__ pSrc, const float val, float* __restrict__ pDst, const int len) { for (int i=0; i #include using namespace std; -extern "C" { - -struct RandGenC:mt19937 {}; -typedef struct RandGenC RandGenC; -RandGenC* RandGen_create() { return (RandGenC*)(new mt19937(random_device()())); } -void RandGen_destroy(RandGenC* v) { delete(v); } - -%{ -int_types = [ - ['uniform_int_distribution', '# a,# b', 'a,b'], - ['binomial_distribution', '# t, double p', 't,p'], - ['negative_binomial_distribution', '# k, double p', 'k,p'], - ['geometric_distribution', 'double p', 'p'], - ['poisson_distribution', 'double mean', 'mean'], - ['discrete_distribution', 'double* start, double* end','start,end'], -] -real_types = [ - ['uniform_real_distribution', '# a,# b', 'a,b'], - ['exponential_distribution', '# l', 'l'], - ['gamma_distribution', '# a,# b', 'a,b'], - ['weibull_distribution', '# a,# b', 'a,b'], - ['normal_distribution', '# mean,# stddev', 'mean,stddev'], - ['lognormal_distribution', '# m,# s', 'm,s'], - ['chi_squared_distribution', '# n', 'n'], - ['cauchy_distribution', '# a,# b', 'a,b'], - ['fisher_f_distribution', '# m,# n', 'm,n'], - ['student_t_distribution', '# n', 'n'], - ['piecewise_constant_distribution', 'double* start_i, double* end_i, double* start_w','start_i,end_i,start_w'], - ['piecewise_linear_distribution', 'double* start_i, double* end_i, double* start_w','start_i,end_i,start_w'], -] -bool_types = [ - ['bernoulli_distribution', 'double p', 'p'], -] -types = [(n,t,p1.replace('#',t),p2) for n,p1,p2 in int_types for t in ['int','long']] -types += [(n,t,p1.replace('#',t),p2) for n,p1,p2 in real_types for t in ['float','double']] -types += [(n,'bool',p1,p2) for n,p1,p2 in bool_types] -}% - -% for n,t,p1,p2 in types: -%{ suf = '' if t=='bool' else f'<{t}>' }% - -struct ${n}_${t}C:${n}${suf} {}; -typedef struct ${n}_${t}C ${n}_${t}C; -${n}_${t}C* ${n}_${t}_create(${p1}) { return (${n}_${t}C*)(new ${n}${suf}(${p2})); } -void ${n}_${t}_destroy(${n}_${t}C* v) { delete(v); } -${t} ${n}_${t}_call(${n}_${t}C* p, RandGenC* g) {return (*p)(*g);} - -% end # n,t - % for t,s in zip(ctypes,['f','']): void sm_add_${t}(const ${t}* __restrict__ pSrc, const ${t} val, ${t}* __restrict__ pDst, const int len) { @@ -95,5 +45,3 @@ void sm_${f}_${t}(const ${t}* __restrict__ pSrc, ${t}* __restrict__ pDst, const % end # t -} - diff --git a/Sources/CBaseMath/CDistribution.cpp b/Sources/CBaseMath/CDistribution.cpp new file mode 100644 index 0000000..85301f9 --- /dev/null +++ b/Sources/CBaseMath/CDistribution.cpp @@ -0,0 +1,357 @@ +#include "include/CDistribution.h" + + +#include +#include + +using namespace std; + + +struct _inner_mt19937:mt19937 {}; +mt19937C mt19937_create() { + mt19937C r = {(_inner_mt19937*) new mt19937(random_device()())}; + return r; +} +OVERLOADABLE void destroy(mt19937C v) { delete(v.p); } + + +struct _inner_knuth_b:knuth_b {}; +knuth_bC knuth_b_create() { + knuth_bC r = {(_inner_knuth_b*) new knuth_b(random_device()())}; + return r; +} +OVERLOADABLE void destroy(knuth_bC v) { delete(v.p); } + + +struct _inner_bernoulli_distribution:bernoulli_distribution {}; +bernoulli_distributionC bernoulli_distribution_create(double p) { + bernoulli_distributionC r = {(_inner_bernoulli_distribution*) new bernoulli_distribution(p)}; + return r; +} +OVERLOADABLE void destroy(bernoulli_distributionC v) { delete(v.p); } + + +struct _inner_uniform_int_distributionint:uniform_int_distribution {}; +OVERLOADABLE uniform_int_distributionintC uniform_int_distribution_create(int a,int b, int _) { + uniform_int_distributionintC r = {(_inner_uniform_int_distributionint*) new uniform_int_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(uniform_int_distributionintC v) { delete(v.p); } + + +struct _inner_uniform_int_distributionlong:uniform_int_distribution {}; +OVERLOADABLE uniform_int_distributionlongC uniform_int_distribution_create(long a,long b, long _) { + uniform_int_distributionlongC r = {(_inner_uniform_int_distributionlong*) new uniform_int_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(uniform_int_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(uniform_int_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(uniform_int_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_binomial_distributionint:binomial_distribution {}; +OVERLOADABLE binomial_distributionintC binomial_distribution_create(int t, double p, int _) { + binomial_distributionintC r = {(_inner_binomial_distributionint*) new binomial_distribution(t,p)}; + return r; +} +OVERLOADABLE void destroy(binomial_distributionintC v) { delete(v.p); } + + +struct _inner_binomial_distributionlong:binomial_distribution {}; +OVERLOADABLE binomial_distributionlongC binomial_distribution_create(long t, double p, long _) { + binomial_distributionlongC r = {(_inner_binomial_distributionlong*) new binomial_distribution(t,p)}; + return r; +} +OVERLOADABLE void destroy(binomial_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(binomial_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(binomial_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_negative_binomial_distributionint:negative_binomial_distribution {}; +OVERLOADABLE negative_binomial_distributionintC negative_binomial_distribution_create(int k, double p, int _) { + negative_binomial_distributionintC r = {(_inner_negative_binomial_distributionint*) new negative_binomial_distribution(k,p)}; + return r; +} +OVERLOADABLE void destroy(negative_binomial_distributionintC v) { delete(v.p); } + + +struct _inner_negative_binomial_distributionlong:negative_binomial_distribution {}; +OVERLOADABLE negative_binomial_distributionlongC negative_binomial_distribution_create(long k, double p, long _) { + negative_binomial_distributionlongC r = {(_inner_negative_binomial_distributionlong*) new negative_binomial_distribution(k,p)}; + return r; +} +OVERLOADABLE void destroy(negative_binomial_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(negative_binomial_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(negative_binomial_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_geometric_distributionint:geometric_distribution {}; +OVERLOADABLE geometric_distributionintC geometric_distribution_create(double p, int _) { + geometric_distributionintC r = {(_inner_geometric_distributionint*) new geometric_distribution(p)}; + return r; +} +OVERLOADABLE void destroy(geometric_distributionintC v) { delete(v.p); } + + +struct _inner_geometric_distributionlong:geometric_distribution {}; +OVERLOADABLE geometric_distributionlongC geometric_distribution_create(double p, long _) { + geometric_distributionlongC r = {(_inner_geometric_distributionlong*) new geometric_distribution(p)}; + return r; +} +OVERLOADABLE void destroy(geometric_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(geometric_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(geometric_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_poisson_distributionint:poisson_distribution {}; +OVERLOADABLE poisson_distributionintC poisson_distribution_create(double mean, int _) { + poisson_distributionintC r = {(_inner_poisson_distributionint*) new poisson_distribution(mean)}; + return r; +} +OVERLOADABLE void destroy(poisson_distributionintC v) { delete(v.p); } + + +struct _inner_poisson_distributionlong:poisson_distribution {}; +OVERLOADABLE poisson_distributionlongC poisson_distribution_create(double mean, long _) { + poisson_distributionlongC r = {(_inner_poisson_distributionlong*) new poisson_distribution(mean)}; + return r; +} +OVERLOADABLE void destroy(poisson_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(poisson_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(poisson_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_discrete_distributionint:discrete_distribution {}; +OVERLOADABLE discrete_distributionintC discrete_distribution_create(double* start, double* end, int _) { + discrete_distributionintC r = {(_inner_discrete_distributionint*) new discrete_distribution(start,end)}; + return r; +} +OVERLOADABLE void destroy(discrete_distributionintC v) { delete(v.p); } + + +struct _inner_discrete_distributionlong:discrete_distribution {}; +OVERLOADABLE discrete_distributionlongC discrete_distribution_create(double* start, double* end, long _) { + discrete_distributionlongC r = {(_inner_discrete_distributionlong*) new discrete_distribution(start,end)}; + return r; +} +OVERLOADABLE void destroy(discrete_distributionlongC v) { delete(v.p); } + +OVERLOADABLE int call(discrete_distributionintC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE long call(discrete_distributionlongC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_uniform_real_distributionfloat:uniform_real_distribution {}; +OVERLOADABLE uniform_real_distributionfloatC uniform_real_distribution_create(float a,float b, float _) { + uniform_real_distributionfloatC r = {(_inner_uniform_real_distributionfloat*) new uniform_real_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(uniform_real_distributionfloatC v) { delete(v.p); } + + +struct _inner_uniform_real_distributiondouble:uniform_real_distribution {}; +OVERLOADABLE uniform_real_distributiondoubleC uniform_real_distribution_create(double a,double b, double _) { + uniform_real_distributiondoubleC r = {(_inner_uniform_real_distributiondouble*) new uniform_real_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(uniform_real_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(uniform_real_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(uniform_real_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_exponential_distributionfloat:exponential_distribution {}; +OVERLOADABLE exponential_distributionfloatC exponential_distribution_create(float l, float _) { + exponential_distributionfloatC r = {(_inner_exponential_distributionfloat*) new exponential_distribution(l)}; + return r; +} +OVERLOADABLE void destroy(exponential_distributionfloatC v) { delete(v.p); } + + +struct _inner_exponential_distributiondouble:exponential_distribution {}; +OVERLOADABLE exponential_distributiondoubleC exponential_distribution_create(double l, double _) { + exponential_distributiondoubleC r = {(_inner_exponential_distributiondouble*) new exponential_distribution(l)}; + return r; +} +OVERLOADABLE void destroy(exponential_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(exponential_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(exponential_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_gamma_distributionfloat:gamma_distribution {}; +OVERLOADABLE gamma_distributionfloatC gamma_distribution_create(float a,float b, float _) { + gamma_distributionfloatC r = {(_inner_gamma_distributionfloat*) new gamma_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(gamma_distributionfloatC v) { delete(v.p); } + + +struct _inner_gamma_distributiondouble:gamma_distribution {}; +OVERLOADABLE gamma_distributiondoubleC gamma_distribution_create(double a,double b, double _) { + gamma_distributiondoubleC r = {(_inner_gamma_distributiondouble*) new gamma_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(gamma_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(gamma_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(gamma_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_weibull_distributionfloat:weibull_distribution {}; +OVERLOADABLE weibull_distributionfloatC weibull_distribution_create(float a,float b, float _) { + weibull_distributionfloatC r = {(_inner_weibull_distributionfloat*) new weibull_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(weibull_distributionfloatC v) { delete(v.p); } + + +struct _inner_weibull_distributiondouble:weibull_distribution {}; +OVERLOADABLE weibull_distributiondoubleC weibull_distribution_create(double a,double b, double _) { + weibull_distributiondoubleC r = {(_inner_weibull_distributiondouble*) new weibull_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(weibull_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(weibull_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(weibull_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_normal_distributionfloat:normal_distribution {}; +OVERLOADABLE normal_distributionfloatC normal_distribution_create(float mean,float stddev, float _) { + normal_distributionfloatC r = {(_inner_normal_distributionfloat*) new normal_distribution(mean,stddev)}; + return r; +} +OVERLOADABLE void destroy(normal_distributionfloatC v) { delete(v.p); } + + +struct _inner_normal_distributiondouble:normal_distribution {}; +OVERLOADABLE normal_distributiondoubleC normal_distribution_create(double mean,double stddev, double _) { + normal_distributiondoubleC r = {(_inner_normal_distributiondouble*) new normal_distribution(mean,stddev)}; + return r; +} +OVERLOADABLE void destroy(normal_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(normal_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(normal_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_lognormal_distributionfloat:lognormal_distribution {}; +OVERLOADABLE lognormal_distributionfloatC lognormal_distribution_create(float m,float s, float _) { + lognormal_distributionfloatC r = {(_inner_lognormal_distributionfloat*) new lognormal_distribution(m,s)}; + return r; +} +OVERLOADABLE void destroy(lognormal_distributionfloatC v) { delete(v.p); } + + +struct _inner_lognormal_distributiondouble:lognormal_distribution {}; +OVERLOADABLE lognormal_distributiondoubleC lognormal_distribution_create(double m,double s, double _) { + lognormal_distributiondoubleC r = {(_inner_lognormal_distributiondouble*) new lognormal_distribution(m,s)}; + return r; +} +OVERLOADABLE void destroy(lognormal_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(lognormal_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(lognormal_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_chi_squared_distributionfloat:chi_squared_distribution {}; +OVERLOADABLE chi_squared_distributionfloatC chi_squared_distribution_create(float n, float _) { + chi_squared_distributionfloatC r = {(_inner_chi_squared_distributionfloat*) new chi_squared_distribution(n)}; + return r; +} +OVERLOADABLE void destroy(chi_squared_distributionfloatC v) { delete(v.p); } + + +struct _inner_chi_squared_distributiondouble:chi_squared_distribution {}; +OVERLOADABLE chi_squared_distributiondoubleC chi_squared_distribution_create(double n, double _) { + chi_squared_distributiondoubleC r = {(_inner_chi_squared_distributiondouble*) new chi_squared_distribution(n)}; + return r; +} +OVERLOADABLE void destroy(chi_squared_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(chi_squared_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(chi_squared_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_cauchy_distributionfloat:cauchy_distribution {}; +OVERLOADABLE cauchy_distributionfloatC cauchy_distribution_create(float a,float b, float _) { + cauchy_distributionfloatC r = {(_inner_cauchy_distributionfloat*) new cauchy_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(cauchy_distributionfloatC v) { delete(v.p); } + + +struct _inner_cauchy_distributiondouble:cauchy_distribution {}; +OVERLOADABLE cauchy_distributiondoubleC cauchy_distribution_create(double a,double b, double _) { + cauchy_distributiondoubleC r = {(_inner_cauchy_distributiondouble*) new cauchy_distribution(a,b)}; + return r; +} +OVERLOADABLE void destroy(cauchy_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(cauchy_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(cauchy_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_fisher_f_distributionfloat:fisher_f_distribution {}; +OVERLOADABLE fisher_f_distributionfloatC fisher_f_distribution_create(float m,float n, float _) { + fisher_f_distributionfloatC r = {(_inner_fisher_f_distributionfloat*) new fisher_f_distribution(m,n)}; + return r; +} +OVERLOADABLE void destroy(fisher_f_distributionfloatC v) { delete(v.p); } + + +struct _inner_fisher_f_distributiondouble:fisher_f_distribution {}; +OVERLOADABLE fisher_f_distributiondoubleC fisher_f_distribution_create(double m,double n, double _) { + fisher_f_distributiondoubleC r = {(_inner_fisher_f_distributiondouble*) new fisher_f_distribution(m,n)}; + return r; +} +OVERLOADABLE void destroy(fisher_f_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(fisher_f_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(fisher_f_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_student_t_distributionfloat:student_t_distribution {}; +OVERLOADABLE student_t_distributionfloatC student_t_distribution_create(float n, float _) { + student_t_distributionfloatC r = {(_inner_student_t_distributionfloat*) new student_t_distribution(n)}; + return r; +} +OVERLOADABLE void destroy(student_t_distributionfloatC v) { delete(v.p); } + + +struct _inner_student_t_distributiondouble:student_t_distribution {}; +OVERLOADABLE student_t_distributiondoubleC student_t_distribution_create(double n, double _) { + student_t_distributiondoubleC r = {(_inner_student_t_distributiondouble*) new student_t_distribution(n)}; + return r; +} +OVERLOADABLE void destroy(student_t_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(student_t_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(student_t_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_piecewise_constant_distributionfloat:piecewise_constant_distribution {}; +OVERLOADABLE piecewise_constant_distributionfloatC piecewise_constant_distribution_create(double* start_i, double* end_i, double* start_w, float _) { + piecewise_constant_distributionfloatC r = {(_inner_piecewise_constant_distributionfloat*) new piecewise_constant_distribution(start_i,end_i,start_w)}; + return r; +} +OVERLOADABLE void destroy(piecewise_constant_distributionfloatC v) { delete(v.p); } + + +struct _inner_piecewise_constant_distributiondouble:piecewise_constant_distribution {}; +OVERLOADABLE piecewise_constant_distributiondoubleC piecewise_constant_distribution_create(double* start_i, double* end_i, double* start_w, double _) { + piecewise_constant_distributiondoubleC r = {(_inner_piecewise_constant_distributiondouble*) new piecewise_constant_distribution(start_i,end_i,start_w)}; + return r; +} +OVERLOADABLE void destroy(piecewise_constant_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(piecewise_constant_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(piecewise_constant_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + +struct _inner_piecewise_linear_distributionfloat:piecewise_linear_distribution {}; +OVERLOADABLE piecewise_linear_distributionfloatC piecewise_linear_distribution_create(double* start_i, double* end_i, double* start_w, float _) { + piecewise_linear_distributionfloatC r = {(_inner_piecewise_linear_distributionfloat*) new piecewise_linear_distribution(start_i,end_i,start_w)}; + return r; +} +OVERLOADABLE void destroy(piecewise_linear_distributionfloatC v) { delete(v.p); } + + +struct _inner_piecewise_linear_distributiondouble:piecewise_linear_distribution {}; +OVERLOADABLE piecewise_linear_distributiondoubleC piecewise_linear_distribution_create(double* start_i, double* end_i, double* start_w, double _) { + piecewise_linear_distributiondoubleC r = {(_inner_piecewise_linear_distributiondouble*) new piecewise_linear_distribution(start_i,end_i,start_w)}; + return r; +} +OVERLOADABLE void destroy(piecewise_linear_distributiondoubleC v) { delete(v.p); } + +OVERLOADABLE float call(piecewise_linear_distributionfloatC p,mt19937C g) {return p.p->operator()(*g.p);} +OVERLOADABLE double call(piecewise_linear_distributiondoubleC p,mt19937C g) {return p.p->operator()(*g.p);} + diff --git a/Sources/CBaseMath/CDistribution.cpp.gyb b/Sources/CBaseMath/CDistribution.cpp.gyb new file mode 100644 index 0000000..50be003 --- /dev/null +++ b/Sources/CBaseMath/CDistribution.cpp.gyb @@ -0,0 +1,19 @@ +#include "include/CDistribution.h" + +%{ +import sys; sys.path.append('../..'); +from cpp_template import * +from cpp_types import * +}% + +#include +#include + +using namespace std; + +% for t in gen_types+dist_types: +${t.cpp_impl()} +% end +% for t in dist_types: +% end + diff --git a/Sources/CBaseMath/include/CBaseMath.h b/Sources/CBaseMath/include/CBaseMath.h index 2af0366..c78a6c6 100644 --- a/Sources/CBaseMath/include/CBaseMath.h +++ b/Sources/CBaseMath/include/CBaseMath.h @@ -3,197 +3,10 @@ extern "C" { #endif +#define OVERLOADABLE __attribute__((overloadable)) #include +#include "CDistribution.h" -typedef struct RandGenC RandGenC; - -RandGenC* RandGen_create() ; -void RandGen_destroy(RandGenC* v) ; -typedef struct uniform_int_distribution_intC uniform_int_distribution_intC; - -uniform_int_distribution_intC* uniform_int_distribution_int_create(int a,int b) ; -void uniform_int_distribution_int_destroy(uniform_int_distribution_intC* v) ; -int uniform_int_distribution_int_call(uniform_int_distribution_intC* p, RandGenC* g) ; -typedef struct uniform_int_distribution_longC uniform_int_distribution_longC; - -uniform_int_distribution_longC* uniform_int_distribution_long_create(long a,long b) ; -void uniform_int_distribution_long_destroy(uniform_int_distribution_longC* v) ; -long uniform_int_distribution_long_call(uniform_int_distribution_longC* p, RandGenC* g) ; -typedef struct binomial_distribution_intC binomial_distribution_intC; - -binomial_distribution_intC* binomial_distribution_int_create(int t, double p) ; -void binomial_distribution_int_destroy(binomial_distribution_intC* v) ; -int binomial_distribution_int_call(binomial_distribution_intC* p, RandGenC* g) ; -typedef struct binomial_distribution_longC binomial_distribution_longC; - -binomial_distribution_longC* binomial_distribution_long_create(long t, double p) ; -void binomial_distribution_long_destroy(binomial_distribution_longC* v) ; -long binomial_distribution_long_call(binomial_distribution_longC* p, RandGenC* g) ; -typedef struct negative_binomial_distribution_intC negative_binomial_distribution_intC; - -negative_binomial_distribution_intC* negative_binomial_distribution_int_create(int k, double p) ; -void negative_binomial_distribution_int_destroy(negative_binomial_distribution_intC* v) ; -int negative_binomial_distribution_int_call(negative_binomial_distribution_intC* p, RandGenC* g) ; -typedef struct negative_binomial_distribution_longC negative_binomial_distribution_longC; - -negative_binomial_distribution_longC* negative_binomial_distribution_long_create(long k, double p) ; -void negative_binomial_distribution_long_destroy(negative_binomial_distribution_longC* v) ; -long negative_binomial_distribution_long_call(negative_binomial_distribution_longC* p, RandGenC* g) ; -typedef struct geometric_distribution_intC geometric_distribution_intC; - -geometric_distribution_intC* geometric_distribution_int_create(double p) ; -void geometric_distribution_int_destroy(geometric_distribution_intC* v) ; -int geometric_distribution_int_call(geometric_distribution_intC* p, RandGenC* g) ; -typedef struct geometric_distribution_longC geometric_distribution_longC; - -geometric_distribution_longC* geometric_distribution_long_create(double p) ; -void geometric_distribution_long_destroy(geometric_distribution_longC* v) ; -long geometric_distribution_long_call(geometric_distribution_longC* p, RandGenC* g) ; -typedef struct poisson_distribution_intC poisson_distribution_intC; - -poisson_distribution_intC* poisson_distribution_int_create(double mean) ; -void poisson_distribution_int_destroy(poisson_distribution_intC* v) ; -int poisson_distribution_int_call(poisson_distribution_intC* p, RandGenC* g) ; -typedef struct poisson_distribution_longC poisson_distribution_longC; - -poisson_distribution_longC* poisson_distribution_long_create(double mean) ; -void poisson_distribution_long_destroy(poisson_distribution_longC* v) ; -long poisson_distribution_long_call(poisson_distribution_longC* p, RandGenC* g) ; -typedef struct discrete_distribution_intC discrete_distribution_intC; - -discrete_distribution_intC* discrete_distribution_int_create(double* start, double* end) ; -void discrete_distribution_int_destroy(discrete_distribution_intC* v) ; -int discrete_distribution_int_call(discrete_distribution_intC* p, RandGenC* g) ; -typedef struct discrete_distribution_longC discrete_distribution_longC; - -discrete_distribution_longC* discrete_distribution_long_create(double* start, double* end) ; -void discrete_distribution_long_destroy(discrete_distribution_longC* v) ; -long discrete_distribution_long_call(discrete_distribution_longC* p, RandGenC* g) ; -typedef struct uniform_real_distribution_floatC uniform_real_distribution_floatC; - -uniform_real_distribution_floatC* uniform_real_distribution_float_create(float a,float b) ; -void uniform_real_distribution_float_destroy(uniform_real_distribution_floatC* v) ; -float uniform_real_distribution_float_call(uniform_real_distribution_floatC* p, RandGenC* g) ; -typedef struct uniform_real_distribution_doubleC uniform_real_distribution_doubleC; - -uniform_real_distribution_doubleC* uniform_real_distribution_double_create(double a,double b) ; -void uniform_real_distribution_double_destroy(uniform_real_distribution_doubleC* v) ; -double uniform_real_distribution_double_call(uniform_real_distribution_doubleC* p, RandGenC* g) ; -typedef struct exponential_distribution_floatC exponential_distribution_floatC; - -exponential_distribution_floatC* exponential_distribution_float_create(float l) ; -void exponential_distribution_float_destroy(exponential_distribution_floatC* v) ; -float exponential_distribution_float_call(exponential_distribution_floatC* p, RandGenC* g) ; -typedef struct exponential_distribution_doubleC exponential_distribution_doubleC; - -exponential_distribution_doubleC* exponential_distribution_double_create(double l) ; -void exponential_distribution_double_destroy(exponential_distribution_doubleC* v) ; -double exponential_distribution_double_call(exponential_distribution_doubleC* p, RandGenC* g) ; -typedef struct gamma_distribution_floatC gamma_distribution_floatC; - -gamma_distribution_floatC* gamma_distribution_float_create(float a,float b) ; -void gamma_distribution_float_destroy(gamma_distribution_floatC* v) ; -float gamma_distribution_float_call(gamma_distribution_floatC* p, RandGenC* g) ; -typedef struct gamma_distribution_doubleC gamma_distribution_doubleC; - -gamma_distribution_doubleC* gamma_distribution_double_create(double a,double b) ; -void gamma_distribution_double_destroy(gamma_distribution_doubleC* v) ; -double gamma_distribution_double_call(gamma_distribution_doubleC* p, RandGenC* g) ; -typedef struct weibull_distribution_floatC weibull_distribution_floatC; - -weibull_distribution_floatC* weibull_distribution_float_create(float a,float b) ; -void weibull_distribution_float_destroy(weibull_distribution_floatC* v) ; -float weibull_distribution_float_call(weibull_distribution_floatC* p, RandGenC* g) ; -typedef struct weibull_distribution_doubleC weibull_distribution_doubleC; - -weibull_distribution_doubleC* weibull_distribution_double_create(double a,double b) ; -void weibull_distribution_double_destroy(weibull_distribution_doubleC* v) ; -double weibull_distribution_double_call(weibull_distribution_doubleC* p, RandGenC* g) ; -typedef struct normal_distribution_floatC normal_distribution_floatC; - -normal_distribution_floatC* normal_distribution_float_create(float mean,float stddev) ; -void normal_distribution_float_destroy(normal_distribution_floatC* v) ; -float normal_distribution_float_call(normal_distribution_floatC* p, RandGenC* g) ; -typedef struct normal_distribution_doubleC normal_distribution_doubleC; - -normal_distribution_doubleC* normal_distribution_double_create(double mean,double stddev) ; -void normal_distribution_double_destroy(normal_distribution_doubleC* v) ; -double normal_distribution_double_call(normal_distribution_doubleC* p, RandGenC* g) ; -typedef struct lognormal_distribution_floatC lognormal_distribution_floatC; - -lognormal_distribution_floatC* lognormal_distribution_float_create(float m,float s) ; -void lognormal_distribution_float_destroy(lognormal_distribution_floatC* v) ; -float lognormal_distribution_float_call(lognormal_distribution_floatC* p, RandGenC* g) ; -typedef struct lognormal_distribution_doubleC lognormal_distribution_doubleC; - -lognormal_distribution_doubleC* lognormal_distribution_double_create(double m,double s) ; -void lognormal_distribution_double_destroy(lognormal_distribution_doubleC* v) ; -double lognormal_distribution_double_call(lognormal_distribution_doubleC* p, RandGenC* g) ; -typedef struct chi_squared_distribution_floatC chi_squared_distribution_floatC; - -chi_squared_distribution_floatC* chi_squared_distribution_float_create(float n) ; -void chi_squared_distribution_float_destroy(chi_squared_distribution_floatC* v) ; -float chi_squared_distribution_float_call(chi_squared_distribution_floatC* p, RandGenC* g) ; -typedef struct chi_squared_distribution_doubleC chi_squared_distribution_doubleC; - -chi_squared_distribution_doubleC* chi_squared_distribution_double_create(double n) ; -void chi_squared_distribution_double_destroy(chi_squared_distribution_doubleC* v) ; -double chi_squared_distribution_double_call(chi_squared_distribution_doubleC* p, RandGenC* g) ; -typedef struct cauchy_distribution_floatC cauchy_distribution_floatC; - -cauchy_distribution_floatC* cauchy_distribution_float_create(float a,float b) ; -void cauchy_distribution_float_destroy(cauchy_distribution_floatC* v) ; -float cauchy_distribution_float_call(cauchy_distribution_floatC* p, RandGenC* g) ; -typedef struct cauchy_distribution_doubleC cauchy_distribution_doubleC; - -cauchy_distribution_doubleC* cauchy_distribution_double_create(double a,double b) ; -void cauchy_distribution_double_destroy(cauchy_distribution_doubleC* v) ; -double cauchy_distribution_double_call(cauchy_distribution_doubleC* p, RandGenC* g) ; -typedef struct fisher_f_distribution_floatC fisher_f_distribution_floatC; - -fisher_f_distribution_floatC* fisher_f_distribution_float_create(float m,float n) ; -void fisher_f_distribution_float_destroy(fisher_f_distribution_floatC* v) ; -float fisher_f_distribution_float_call(fisher_f_distribution_floatC* p, RandGenC* g) ; -typedef struct fisher_f_distribution_doubleC fisher_f_distribution_doubleC; - -fisher_f_distribution_doubleC* fisher_f_distribution_double_create(double m,double n) ; -void fisher_f_distribution_double_destroy(fisher_f_distribution_doubleC* v) ; -double fisher_f_distribution_double_call(fisher_f_distribution_doubleC* p, RandGenC* g) ; -typedef struct student_t_distribution_floatC student_t_distribution_floatC; - -student_t_distribution_floatC* student_t_distribution_float_create(float n) ; -void student_t_distribution_float_destroy(student_t_distribution_floatC* v) ; -float student_t_distribution_float_call(student_t_distribution_floatC* p, RandGenC* g) ; -typedef struct student_t_distribution_doubleC student_t_distribution_doubleC; - -student_t_distribution_doubleC* student_t_distribution_double_create(double n) ; -void student_t_distribution_double_destroy(student_t_distribution_doubleC* v) ; -double student_t_distribution_double_call(student_t_distribution_doubleC* p, RandGenC* g) ; -typedef struct piecewise_constant_distribution_floatC piecewise_constant_distribution_floatC; - -piecewise_constant_distribution_floatC* piecewise_constant_distribution_float_create(double* start_i, double* end_i, double* start_w) ; -void piecewise_constant_distribution_float_destroy(piecewise_constant_distribution_floatC* v) ; -float piecewise_constant_distribution_float_call(piecewise_constant_distribution_floatC* p, RandGenC* g) ; -typedef struct piecewise_constant_distribution_doubleC piecewise_constant_distribution_doubleC; - -piecewise_constant_distribution_doubleC* piecewise_constant_distribution_double_create(double* start_i, double* end_i, double* start_w) ; -void piecewise_constant_distribution_double_destroy(piecewise_constant_distribution_doubleC* v) ; -double piecewise_constant_distribution_double_call(piecewise_constant_distribution_doubleC* p, RandGenC* g) ; -typedef struct piecewise_linear_distribution_floatC piecewise_linear_distribution_floatC; - -piecewise_linear_distribution_floatC* piecewise_linear_distribution_float_create(double* start_i, double* end_i, double* start_w) ; -void piecewise_linear_distribution_float_destroy(piecewise_linear_distribution_floatC* v) ; -float piecewise_linear_distribution_float_call(piecewise_linear_distribution_floatC* p, RandGenC* g) ; -typedef struct piecewise_linear_distribution_doubleC piecewise_linear_distribution_doubleC; - -piecewise_linear_distribution_doubleC* piecewise_linear_distribution_double_create(double* start_i, double* end_i, double* start_w) ; -void piecewise_linear_distribution_double_destroy(piecewise_linear_distribution_doubleC* v) ; -double piecewise_linear_distribution_double_call(piecewise_linear_distribution_doubleC* p, RandGenC* g) ; -typedef struct bernoulli_distribution_boolC bernoulli_distribution_boolC; - -bernoulli_distribution_boolC* bernoulli_distribution_bool_create(double p) ; -void bernoulli_distribution_bool_destroy(bernoulli_distribution_boolC* v) ; -bool bernoulli_distribution_bool_call(bernoulli_distribution_boolC* p, RandGenC* g) ; void sm_add_float(const float* __restrict__ pSrc, const float val, float* __restrict__ pDst, const int len) ; float smSum_float(const float* __restrict__ pSrc, const int len) ; float smSum_sqr_float(const float* __restrict__ pSrc, const int len) ; diff --git a/Sources/CBaseMath/include/CBaseMath.h.gyb b/Sources/CBaseMath/include/CBaseMath.h.gyb index 31e2c5d..87af43c 100644 --- a/Sources/CBaseMath/include/CBaseMath.h.gyb +++ b/Sources/CBaseMath/include/CBaseMath.h.gyb @@ -1,22 +1,14 @@ -%{ - import re - lines = [] - for l in open('../CBaseMath.cpp').readlines(): - s = re.search(r'^typedef', l) - if s: lines.append(l) - s = re.search(r'^(\w[^"]+){', l) - if s and not l.startswith('struct'): lines.append(s.group(1)+';') -}% +%{ import sys; sys.path.append('../../..'); from cpp_template import * }% #ifdef __cplusplus extern "C" { #endif +#define OVERLOADABLE __attribute__((overloadable)) #include +#include "CDistribution.h" -% for l in lines: -${l} -% end +${ make_header(__file__) } #ifdef __cplusplus } diff --git a/Sources/CBaseMath/include/CDistribution.h b/Sources/CBaseMath/include/CDistribution.h new file mode 100644 index 0000000..76f7fb6 --- /dev/null +++ b/Sources/CBaseMath/include/CDistribution.h @@ -0,0 +1,323 @@ + +#ifdef __cplusplus +extern "C" { +#endif + +#define OVERLOADABLE __attribute__((overloadable)) +#include + + +typedef struct _inner_mt19937 _inner_mt19937; +typedef struct mt19937C mt19937C; +struct mt19937C {_inner_mt19937* p;}; + + +typedef struct _inner_knuth_b _inner_knuth_b; +typedef struct knuth_bC knuth_bC; +struct knuth_bC {_inner_knuth_b* p;}; + + +typedef struct _inner_bernoulli_distribution _inner_bernoulli_distribution; +typedef struct bernoulli_distributionC bernoulli_distributionC; +struct bernoulli_distributionC {_inner_bernoulli_distribution* p;}; + + +typedef struct _inner_uniform_int_distributionint _inner_uniform_int_distributionint; +typedef struct uniform_int_distributionintC uniform_int_distributionintC; +struct uniform_int_distributionintC {_inner_uniform_int_distributionint* p;}; + + +typedef struct _inner_uniform_int_distributionlong _inner_uniform_int_distributionlong; +typedef struct uniform_int_distributionlongC uniform_int_distributionlongC; +struct uniform_int_distributionlongC {_inner_uniform_int_distributionlong* p;}; + + +typedef struct _inner_binomial_distributionint _inner_binomial_distributionint; +typedef struct binomial_distributionintC binomial_distributionintC; +struct binomial_distributionintC {_inner_binomial_distributionint* p;}; + + +typedef struct _inner_binomial_distributionlong _inner_binomial_distributionlong; +typedef struct binomial_distributionlongC binomial_distributionlongC; +struct binomial_distributionlongC {_inner_binomial_distributionlong* p;}; + + +typedef struct _inner_negative_binomial_distributionint _inner_negative_binomial_distributionint; +typedef struct negative_binomial_distributionintC negative_binomial_distributionintC; +struct negative_binomial_distributionintC {_inner_negative_binomial_distributionint* p;}; + + +typedef struct _inner_negative_binomial_distributionlong _inner_negative_binomial_distributionlong; +typedef struct negative_binomial_distributionlongC negative_binomial_distributionlongC; +struct negative_binomial_distributionlongC {_inner_negative_binomial_distributionlong* p;}; + + +typedef struct _inner_geometric_distributionint _inner_geometric_distributionint; +typedef struct geometric_distributionintC geometric_distributionintC; +struct geometric_distributionintC {_inner_geometric_distributionint* p;}; + + +typedef struct _inner_geometric_distributionlong _inner_geometric_distributionlong; +typedef struct geometric_distributionlongC geometric_distributionlongC; +struct geometric_distributionlongC {_inner_geometric_distributionlong* p;}; + + +typedef struct _inner_poisson_distributionint _inner_poisson_distributionint; +typedef struct poisson_distributionintC poisson_distributionintC; +struct poisson_distributionintC {_inner_poisson_distributionint* p;}; + + +typedef struct _inner_poisson_distributionlong _inner_poisson_distributionlong; +typedef struct poisson_distributionlongC poisson_distributionlongC; +struct poisson_distributionlongC {_inner_poisson_distributionlong* p;}; + + +typedef struct _inner_discrete_distributionint _inner_discrete_distributionint; +typedef struct discrete_distributionintC discrete_distributionintC; +struct discrete_distributionintC {_inner_discrete_distributionint* p;}; + + +typedef struct _inner_discrete_distributionlong _inner_discrete_distributionlong; +typedef struct discrete_distributionlongC discrete_distributionlongC; +struct discrete_distributionlongC {_inner_discrete_distributionlong* p;}; + + +typedef struct _inner_uniform_real_distributionfloat _inner_uniform_real_distributionfloat; +typedef struct uniform_real_distributionfloatC uniform_real_distributionfloatC; +struct uniform_real_distributionfloatC {_inner_uniform_real_distributionfloat* p;}; + + +typedef struct _inner_uniform_real_distributiondouble _inner_uniform_real_distributiondouble; +typedef struct uniform_real_distributiondoubleC uniform_real_distributiondoubleC; +struct uniform_real_distributiondoubleC {_inner_uniform_real_distributiondouble* p;}; + + +typedef struct _inner_exponential_distributionfloat _inner_exponential_distributionfloat; +typedef struct exponential_distributionfloatC exponential_distributionfloatC; +struct exponential_distributionfloatC {_inner_exponential_distributionfloat* p;}; + + +typedef struct _inner_exponential_distributiondouble _inner_exponential_distributiondouble; +typedef struct exponential_distributiondoubleC exponential_distributiondoubleC; +struct exponential_distributiondoubleC {_inner_exponential_distributiondouble* p;}; + + +typedef struct _inner_gamma_distributionfloat _inner_gamma_distributionfloat; +typedef struct gamma_distributionfloatC gamma_distributionfloatC; +struct gamma_distributionfloatC {_inner_gamma_distributionfloat* p;}; + + +typedef struct _inner_gamma_distributiondouble _inner_gamma_distributiondouble; +typedef struct gamma_distributiondoubleC gamma_distributiondoubleC; +struct gamma_distributiondoubleC {_inner_gamma_distributiondouble* p;}; + + +typedef struct _inner_weibull_distributionfloat _inner_weibull_distributionfloat; +typedef struct weibull_distributionfloatC weibull_distributionfloatC; +struct weibull_distributionfloatC {_inner_weibull_distributionfloat* p;}; + + +typedef struct _inner_weibull_distributiondouble _inner_weibull_distributiondouble; +typedef struct weibull_distributiondoubleC weibull_distributiondoubleC; +struct weibull_distributiondoubleC {_inner_weibull_distributiondouble* p;}; + + +typedef struct _inner_normal_distributionfloat _inner_normal_distributionfloat; +typedef struct normal_distributionfloatC normal_distributionfloatC; +struct normal_distributionfloatC {_inner_normal_distributionfloat* p;}; + + +typedef struct _inner_normal_distributiondouble _inner_normal_distributiondouble; +typedef struct normal_distributiondoubleC normal_distributiondoubleC; +struct normal_distributiondoubleC {_inner_normal_distributiondouble* p;}; + + +typedef struct _inner_lognormal_distributionfloat _inner_lognormal_distributionfloat; +typedef struct lognormal_distributionfloatC lognormal_distributionfloatC; +struct lognormal_distributionfloatC {_inner_lognormal_distributionfloat* p;}; + + +typedef struct _inner_lognormal_distributiondouble _inner_lognormal_distributiondouble; +typedef struct lognormal_distributiondoubleC lognormal_distributiondoubleC; +struct lognormal_distributiondoubleC {_inner_lognormal_distributiondouble* p;}; + + +typedef struct _inner_chi_squared_distributionfloat _inner_chi_squared_distributionfloat; +typedef struct chi_squared_distributionfloatC chi_squared_distributionfloatC; +struct chi_squared_distributionfloatC {_inner_chi_squared_distributionfloat* p;}; + + +typedef struct _inner_chi_squared_distributiondouble _inner_chi_squared_distributiondouble; +typedef struct chi_squared_distributiondoubleC chi_squared_distributiondoubleC; +struct chi_squared_distributiondoubleC {_inner_chi_squared_distributiondouble* p;}; + + +typedef struct _inner_cauchy_distributionfloat _inner_cauchy_distributionfloat; +typedef struct cauchy_distributionfloatC cauchy_distributionfloatC; +struct cauchy_distributionfloatC {_inner_cauchy_distributionfloat* p;}; + + +typedef struct _inner_cauchy_distributiondouble _inner_cauchy_distributiondouble; +typedef struct cauchy_distributiondoubleC cauchy_distributiondoubleC; +struct cauchy_distributiondoubleC {_inner_cauchy_distributiondouble* p;}; + + +typedef struct _inner_fisher_f_distributionfloat _inner_fisher_f_distributionfloat; +typedef struct fisher_f_distributionfloatC fisher_f_distributionfloatC; +struct fisher_f_distributionfloatC {_inner_fisher_f_distributionfloat* p;}; + + +typedef struct _inner_fisher_f_distributiondouble _inner_fisher_f_distributiondouble; +typedef struct fisher_f_distributiondoubleC fisher_f_distributiondoubleC; +struct fisher_f_distributiondoubleC {_inner_fisher_f_distributiondouble* p;}; + + +typedef struct _inner_student_t_distributionfloat _inner_student_t_distributionfloat; +typedef struct student_t_distributionfloatC student_t_distributionfloatC; +struct student_t_distributionfloatC {_inner_student_t_distributionfloat* p;}; + + +typedef struct _inner_student_t_distributiondouble _inner_student_t_distributiondouble; +typedef struct student_t_distributiondoubleC student_t_distributiondoubleC; +struct student_t_distributiondoubleC {_inner_student_t_distributiondouble* p;}; + + +typedef struct _inner_piecewise_constant_distributionfloat _inner_piecewise_constant_distributionfloat; +typedef struct piecewise_constant_distributionfloatC piecewise_constant_distributionfloatC; +struct piecewise_constant_distributionfloatC {_inner_piecewise_constant_distributionfloat* p;}; + + +typedef struct _inner_piecewise_constant_distributiondouble _inner_piecewise_constant_distributiondouble; +typedef struct piecewise_constant_distributiondoubleC piecewise_constant_distributiondoubleC; +struct piecewise_constant_distributiondoubleC {_inner_piecewise_constant_distributiondouble* p;}; + + +typedef struct _inner_piecewise_linear_distributionfloat _inner_piecewise_linear_distributionfloat; +typedef struct piecewise_linear_distributionfloatC piecewise_linear_distributionfloatC; +struct piecewise_linear_distributionfloatC {_inner_piecewise_linear_distributionfloat* p;}; + + +typedef struct _inner_piecewise_linear_distributiondouble _inner_piecewise_linear_distributiondouble; +typedef struct piecewise_linear_distributiondoubleC piecewise_linear_distributiondoubleC; +struct piecewise_linear_distributiondoubleC {_inner_piecewise_linear_distributiondouble* p;}; + + +mt19937C mt19937_create() ; +OVERLOADABLE void destroy(mt19937C v) ; +knuth_bC knuth_b_create() ; +OVERLOADABLE void destroy(knuth_bC v) ; +bernoulli_distributionC bernoulli_distribution_create(double p) ; +OVERLOADABLE void destroy(bernoulli_distributionC v) ; +OVERLOADABLE uniform_int_distributionintC uniform_int_distribution_create(int a,int b, int _) ; +OVERLOADABLE void destroy(uniform_int_distributionintC v) ; +OVERLOADABLE uniform_int_distributionlongC uniform_int_distribution_create(long a,long b, long _) ; +OVERLOADABLE void destroy(uniform_int_distributionlongC v) ; +OVERLOADABLE binomial_distributionintC binomial_distribution_create(int t, double p, int _) ; +OVERLOADABLE void destroy(binomial_distributionintC v) ; +OVERLOADABLE binomial_distributionlongC binomial_distribution_create(long t, double p, long _) ; +OVERLOADABLE void destroy(binomial_distributionlongC v) ; +OVERLOADABLE negative_binomial_distributionintC negative_binomial_distribution_create(int k, double p, int _) ; +OVERLOADABLE void destroy(negative_binomial_distributionintC v) ; +OVERLOADABLE negative_binomial_distributionlongC negative_binomial_distribution_create(long k, double p, long _) ; +OVERLOADABLE void destroy(negative_binomial_distributionlongC v) ; +OVERLOADABLE geometric_distributionintC geometric_distribution_create(double p, int _) ; +OVERLOADABLE void destroy(geometric_distributionintC v) ; +OVERLOADABLE geometric_distributionlongC geometric_distribution_create(double p, long _) ; +OVERLOADABLE void destroy(geometric_distributionlongC v) ; +OVERLOADABLE poisson_distributionintC poisson_distribution_create(double mean, int _) ; +OVERLOADABLE void destroy(poisson_distributionintC v) ; +OVERLOADABLE poisson_distributionlongC poisson_distribution_create(double mean, long _) ; +OVERLOADABLE void destroy(poisson_distributionlongC v) ; +OVERLOADABLE discrete_distributionintC discrete_distribution_create(double* start, double* end, int _) ; +OVERLOADABLE void destroy(discrete_distributionintC v) ; +OVERLOADABLE discrete_distributionlongC discrete_distribution_create(double* start, double* end, long _) ; +OVERLOADABLE void destroy(discrete_distributionlongC v) ; +OVERLOADABLE uniform_real_distributionfloatC uniform_real_distribution_create(float a,float b, float _) ; +OVERLOADABLE void destroy(uniform_real_distributionfloatC v) ; +OVERLOADABLE uniform_real_distributiondoubleC uniform_real_distribution_create(double a,double b, double _) ; +OVERLOADABLE void destroy(uniform_real_distributiondoubleC v) ; +OVERLOADABLE exponential_distributionfloatC exponential_distribution_create(float l, float _) ; +OVERLOADABLE void destroy(exponential_distributionfloatC v) ; +OVERLOADABLE exponential_distributiondoubleC exponential_distribution_create(double l, double _) ; +OVERLOADABLE void destroy(exponential_distributiondoubleC v) ; +OVERLOADABLE gamma_distributionfloatC gamma_distribution_create(float a,float b, float _) ; +OVERLOADABLE void destroy(gamma_distributionfloatC v) ; +OVERLOADABLE gamma_distributiondoubleC gamma_distribution_create(double a,double b, double _) ; +OVERLOADABLE void destroy(gamma_distributiondoubleC v) ; +OVERLOADABLE weibull_distributionfloatC weibull_distribution_create(float a,float b, float _) ; +OVERLOADABLE void destroy(weibull_distributionfloatC v) ; +OVERLOADABLE weibull_distributiondoubleC weibull_distribution_create(double a,double b, double _) ; +OVERLOADABLE void destroy(weibull_distributiondoubleC v) ; +OVERLOADABLE normal_distributionfloatC normal_distribution_create(float mean,float stddev, float _) ; +OVERLOADABLE void destroy(normal_distributionfloatC v) ; +OVERLOADABLE normal_distributiondoubleC normal_distribution_create(double mean,double stddev, double _) ; +OVERLOADABLE void destroy(normal_distributiondoubleC v) ; +OVERLOADABLE lognormal_distributionfloatC lognormal_distribution_create(float m,float s, float _) ; +OVERLOADABLE void destroy(lognormal_distributionfloatC v) ; +OVERLOADABLE lognormal_distributiondoubleC lognormal_distribution_create(double m,double s, double _) ; +OVERLOADABLE void destroy(lognormal_distributiondoubleC v) ; +OVERLOADABLE chi_squared_distributionfloatC chi_squared_distribution_create(float n, float _) ; +OVERLOADABLE void destroy(chi_squared_distributionfloatC v) ; +OVERLOADABLE chi_squared_distributiondoubleC chi_squared_distribution_create(double n, double _) ; +OVERLOADABLE void destroy(chi_squared_distributiondoubleC v) ; +OVERLOADABLE cauchy_distributionfloatC cauchy_distribution_create(float a,float b, float _) ; +OVERLOADABLE void destroy(cauchy_distributionfloatC v) ; +OVERLOADABLE cauchy_distributiondoubleC cauchy_distribution_create(double a,double b, double _) ; +OVERLOADABLE void destroy(cauchy_distributiondoubleC v) ; +OVERLOADABLE fisher_f_distributionfloatC fisher_f_distribution_create(float m,float n, float _) ; +OVERLOADABLE void destroy(fisher_f_distributionfloatC v) ; +OVERLOADABLE fisher_f_distributiondoubleC fisher_f_distribution_create(double m,double n, double _) ; +OVERLOADABLE void destroy(fisher_f_distributiondoubleC v) ; +OVERLOADABLE student_t_distributionfloatC student_t_distribution_create(float n, float _) ; +OVERLOADABLE void destroy(student_t_distributionfloatC v) ; +OVERLOADABLE student_t_distributiondoubleC student_t_distribution_create(double n, double _) ; +OVERLOADABLE void destroy(student_t_distributiondoubleC v) ; +OVERLOADABLE piecewise_constant_distributionfloatC piecewise_constant_distribution_create(double* start_i, double* end_i, double* start_w, float _) ; +OVERLOADABLE void destroy(piecewise_constant_distributionfloatC v) ; +OVERLOADABLE piecewise_constant_distributiondoubleC piecewise_constant_distribution_create(double* start_i, double* end_i, double* start_w, double _) ; +OVERLOADABLE void destroy(piecewise_constant_distributiondoubleC v) ; +OVERLOADABLE piecewise_linear_distributionfloatC piecewise_linear_distribution_create(double* start_i, double* end_i, double* start_w, float _) ; +OVERLOADABLE void destroy(piecewise_linear_distributionfloatC v) ; +OVERLOADABLE piecewise_linear_distributiondoubleC piecewise_linear_distribution_create(double* start_i, double* end_i, double* start_w, double _) ; +OVERLOADABLE void destroy(piecewise_linear_distributiondoubleC v) ; +OVERLOADABLE int call(uniform_int_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(uniform_int_distributionlongC p,mt19937C g) ; +OVERLOADABLE int call(binomial_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(binomial_distributionlongC p,mt19937C g) ; +OVERLOADABLE int call(negative_binomial_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(negative_binomial_distributionlongC p,mt19937C g) ; +OVERLOADABLE int call(geometric_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(geometric_distributionlongC p,mt19937C g) ; +OVERLOADABLE int call(poisson_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(poisson_distributionlongC p,mt19937C g) ; +OVERLOADABLE int call(discrete_distributionintC p,mt19937C g) ; +OVERLOADABLE long call(discrete_distributionlongC p,mt19937C g) ; +OVERLOADABLE float call(uniform_real_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(uniform_real_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(exponential_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(exponential_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(gamma_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(gamma_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(weibull_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(weibull_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(normal_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(normal_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(lognormal_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(lognormal_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(chi_squared_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(chi_squared_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(cauchy_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(cauchy_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(fisher_f_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(fisher_f_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(student_t_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(student_t_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(piecewise_constant_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(piecewise_constant_distributiondoubleC p,mt19937C g) ; +OVERLOADABLE float call(piecewise_linear_distributionfloatC p,mt19937C g) ; +OVERLOADABLE double call(piecewise_linear_distributiondoubleC p,mt19937C g) ; + +#ifdef __cplusplus +} +#endif + diff --git a/Sources/CBaseMath/include/CDistribution.h.gyb b/Sources/CBaseMath/include/CDistribution.h.gyb new file mode 100644 index 0000000..700b32b --- /dev/null +++ b/Sources/CBaseMath/include/CDistribution.h.gyb @@ -0,0 +1,23 @@ +%{ +import sys; sys.path.append('../../..'); +from cpp_template import * +from cpp_types import * +}% + +#ifdef __cplusplus +extern "C" { +#endif + +#define OVERLOADABLE __attribute__((overloadable)) +#include + +% for t in gen_types+dist_types: +${t.cpp_decl()} +% end + +${ make_header(__file__) } + +#ifdef __cplusplus +} +#endif + diff --git a/c2swift.py b/c2swift.py new file mode 100644 index 0000000..9345739 --- /dev/null +++ b/c2swift.py @@ -0,0 +1,51 @@ +from pdb import set_trace +import re + +def lower1(s): return s[:1].lower() + s[1:] + +float_swift = ['Float', 'Double'] +float_c = ['float', 'double'] +int_swift = ['Int', 'Int32'] +int_c = ['long', 'int'] +float_types = list(zip(float_swift,float_c)) +int_types = list(zip(int_swift,int_c)) + +type_replace = {'double':'Double', 'float':'Float', 'int':'Int32', 'long':'Int', + 'void':'Void', '#':'#', '':''} +type_replace_rev = {v:k for k,v in type_replace.items()} + +def word(name): return fr'(?P<{name}>[\w#]+)' +param_re = re.compile(fr'(?Pconst *)?{word("t")} *(?P\*?) *{word("name")}(?P *\[\])?') + +def parse_type(s): + m = param_re.search(s) + if not m: raise Exception(f"Failed to parse: {s}") + name = m.group('name') + is_ptr = m.group('ptr') or m.group('arr') + t = m.group('t') + t = type_replace.get(t,t) + if is_ptr: + if t=='Void': t = 'UnsafeRawPointer' + else: + p_type ='UnsafePointer' if m.group('const') else 'UnsafeMutablePointer' + t = f"{p_type}<{t}>" + return (name,t) + +def parse_types(s): + if not s: return {} + s = re.split(r',\s*', s) + return dict([parse_type(o) for o in s]) + +def test_parse() : + in1 = "const long n,const float a[], double r[], const int N, const float *ex, # f" + ex1 = dict([("n","Int"), ("a","UnsafePointer"), ("r","UnsafeMutablePointer"), ("N","Int32"), + ("ex","UnsafePointer"), ("f","#")]) + + res = parse_types(in1) + for r,exp in zip(res.items(),ex1.items()): + assert r == exp, f'{r}\n{exp}' + + print("done") + +if __name__=='__main__': test_parse() + diff --git a/cpp_template.py b/cpp_template.py new file mode 100644 index 0000000..2351d3b --- /dev/null +++ b/cpp_template.py @@ -0,0 +1,97 @@ +import re,pathlib,os +from pdb import set_trace +from c2swift import * + +def make_header(fn): + fn = os.path.basename(fn).split('.')[0] + lines = [] + for l in open(f"../{fn}.cpp").readlines(): + if '//internal' in l: continue + s = re.search(r'^typedef', l) + if s: lines.append(l) + s = re.search(r'^(\w.*?){', l) + if s and not l.startswith('struct'): lines.append(s.group(1)+';') + return "\n".join(lines) + +class cpp: + def __init__(self, typ, p1, p2=None, gens=None, module='MISSING'): + if not gens: gens=[''] + self.typ,self.p1,self.p2,self.gens,self.module = typ,p1,p2,gens,module + self.ps = parse_types(p1) + self.pswift = ",".join([f"_ {n}:{t}" for n,t in self.ps.items()]) + if self.p2 is None: self.p2 = ",".join(self.ps.keys()) + self.funcs = [] + self.swift_type = 'CppTypePtr' + + def cpp_impl_(self, g): + suf = f'<{g}>' if g else '' + xargs = f', {g} _' if g else '' + over = 'OVERLOADABLE ' if g else '' + t,p1,p2 = self.typ,self.p1,self.p2 + return f""" +struct _inner_{t}{g}:{t}{suf} {{}}; +{over}{t}{g}C {t}_create({p1}{xargs}) {{ + {t}{g}C r = {{(_inner_{t}{g}*) new {t}{suf}({p2})}}; + return r; +}} +OVERLOADABLE void destroy({t}{g}C v) {{ delete(v.p); }} +""".replace('#', g) + + def cpp_impl(self): + res = [self.cpp_impl_(g) for g in self.gens] + for func in self.funcs: res += self.cpp_func(*func) + return '\n'.join(res) + + def cpp_decl_(self,g): + t = self.typ + return f""" +typedef struct _inner_{t}{g} _inner_{t}{g}; +typedef struct {t}{g}C {t}{g}C; +struct {t}{g}C {{_inner_{t}{g}* p;}}; +""" + + @property + def generics(self): + return [(o,type_replace_rev.get(o,o)) for o in self.gens] + + def cpp_decl(self): + res = [self.cpp_decl_(g) for g in self.gens] + return '\n'.join(res) + + def cpp_func_(self,f,a1,a2,g): + over = 'OVERLOADABLE ' if g else '' + return f"{over}{f}({self.typ}{g}C p{a1}) {{return p.p->{a2};}}".replace('#', g) + + def cpp_func(self,f,a1,a2=None): + if a1: a1=','+a1 + res = [self.cpp_func_(f,a1,a2,g) for g in self.gens] + return res + + #o.add_func('# call', 'mt19937C g', 'operator()(*g.p)') + def add_func(self, *func): self.funcs.append(func) + + def swift_func_(self,f,a1, a2, g): + ps = parse_types(a1) + p1 = ','.join([f"_ {k}:{v}" for k,v in ps.items()]) + p2 = ",".join(ps.keys()) + f,ret = list(parse_types(f).items())[0] + if ret=='#': ret=g + if p2: p2=','+p2 + return f"public func {f}({p1})->{ret} {{ return {self.module}.{f}(self{p2}) }}" + #public func call(_ g:mt19937C)->{g} {{ return CBaseMath.call(self, g) }} + + def swift_(self, g): + g2 = type_replace_rev[g] + funcs = [self.swift_func_(*func,g) for func in self.funcs] + funcs = '\n'.join(funcs) + return f""" +extension {self.typ}{g2}C:{self.swift_type} {{ + public func delete() {{destroy(self)}} + {funcs} +}} +""" + + def swift(self): + res = [self.swift_(g) for g in self.gens] + return '\n'.join(res) + diff --git a/cpp_types.py b/cpp_types.py new file mode 100644 index 0000000..77b81ad --- /dev/null +++ b/cpp_types.py @@ -0,0 +1,39 @@ +from cpp_template import * + +gen_types = [ + cpp("mt19937", "", "random_device()()"), + cpp("knuth_b", "", "random_device()()"), + cpp("bernoulli_distribution", "double p"), +] +int_dist = [ + cpp("uniform_int_distribution", "# a,# b"), + cpp("binomial_distribution", "# t, double p"), + cpp("negative_binomial_distribution", "# k, double p"), + cpp("geometric_distribution", "double p"), + cpp("poisson_distribution", "double mean"), + cpp("discrete_distribution", "double* start, double* end"), +] +for o in int_dist: o.gens = int_swift +real_dist = [ + cpp("uniform_real_distribution", "# a,# b"), + cpp("exponential_distribution", "# l"), + cpp("gamma_distribution", "# a,# b"), + cpp("weibull_distribution", "# a,# b"), + cpp("normal_distribution", "# mean,# stddev"), + cpp("lognormal_distribution", "# m,# s"), + cpp("chi_squared_distribution", "# n"), + cpp("cauchy_distribution", "# a,# b"), + cpp("fisher_f_distribution", "# m,# n"), + cpp("student_t_distribution", "# n"), + cpp("piecewise_constant_distribution", "double* start_i, double* end_i, double* start_w"), + cpp("piecewise_linear_distribution", "double* start_i, double* end_i, double* start_w"), +] +for o in real_dist: o.gens = float_swift +dist_types = int_dist+real_dist +all_types = dist_types+gen_types +for o in all_types: o.module = 'CBaseMath' + +for o in dist_types: + o.swift_type = 'DistributionC' + o.add_func('# call', 'mt19937C g', 'operator()(*g.p)') +