Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functions for generating uniform/normal distributed randomness #154

Merged
merged 10 commits into from
Oct 29, 2019
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ opt_in_rules:
- empty_count
- explicit_enum_raw_value
- explicit_init
- extension_access_modifier
- fatal_error_message
- file_header
- first_where
Expand Down
2 changes: 1 addition & 1 deletion Sources/Surge/Arithmetic/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func powInPlace<X: UnsafeMutableMemoryAccessible>(_ lhs: inout X, _ rhs: Double)
return powInPlace(&lhs, rhs)
}

//// MARK: - Square
// MARK: - Square

public func sq<L: UnsafeMemoryAccessible>(_ lhs: L) -> [Float] where L.Element == Float {
return withArray(from: lhs) { sqInPlace(&$0) }
Expand Down
128 changes: 128 additions & 0 deletions Sources/Surge/Linear Algebra/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,135 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF

return matrix
}
}

// MARK: - Initialization: Randomized

extension Matrix where Scalar == Float {
public static func randUniform(
rows: Int,
columns: Int,
range: ClosedRange<Float> = 0.0...1.0
) -> Matrix {
var generator = SystemRandomNumberGenerator()
return self.randUniform(
rows: rows,
columns: columns,
range: range,
using: &generator
)
}

public static func randUniform<T>(
rows: Int,
columns: Int,
range: ClosedRange<Float> = 0.0...1.0,
using generator: inout T
) -> Matrix where T: RandomNumberGenerator {
let grid = Surge.randUniform(
count: rows * columns,
range: range,
using: &generator
)
return Matrix(rows: rows, columns: columns, grid: grid)
}

public static func randNormal(
rows: Int,
columns: Int,
mu: Float = 0.0,
sigma: Float = 1.0
) -> Matrix {
var generator = SystemRandomNumberGenerator()
return self.randNormal(
rows: rows,
columns: columns,
mu: mu,
sigma: sigma,
using: &generator
)
}

public static func randNormal<T>(
rows: Int,
columns: Int,
mu: Float = 0.0,
sigma: Float = 1.0,
using generator: inout T
) -> Matrix where T: RandomNumberGenerator {
let grid = Surge.randNormal(
count: rows * columns,
mu: mu,
sigma: sigma,
using: &generator
)
return Matrix(rows: rows, columns: columns, grid: grid)
}
}

extension Matrix where Scalar == Double {
public static func randUniform(
rows: Int,
columns: Int,
range: ClosedRange<Double> = 0.0...1.0
) -> Matrix {
var generator = SystemRandomNumberGenerator()
return self.randUniform(
rows: rows,
columns: columns,
range: range,
using: &generator
)
}

public static func randUniform<T>(
rows: Int,
columns: Int,
range: ClosedRange<Double> = 0.0...1.0,
using generator: inout T
) -> Matrix where T: RandomNumberGenerator {
let grid = Surge.randUniform(
count: rows * columns,
range: range,
using: &generator
)
return Matrix(rows: rows, columns: columns, grid: grid)
}

public static func randNormal(
rows: Int,
columns: Int,
mu: Double = 0.0,
sigma: Double = 1.0
) -> Matrix {
var generator = SystemRandomNumberGenerator()
return self.randNormal(
rows: rows,
columns: columns,
mu: mu,
sigma: sigma,
using: &generator
)
}

public static func randNormal<T>(
rows: Int,
columns: Int,
mu: Double = 0.0,
sigma: Double = 1.0,
using generator: inout T
) -> Matrix where T: RandomNumberGenerator {
let grid = Surge.randNormal(
count: rows * columns,
mu: mu,
sigma: sigma,
using: &generator
)
return Matrix(rows: rows, columns: columns, grid: grid)
}
}

extension Matrix {
// MARK: - Subscript

public subscript(row: Int, column: Int) -> Scalar {
Expand Down
82 changes: 82 additions & 0 deletions Sources/Surge/Linear Algebra/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,93 @@ public struct Vector<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
} else {
scalars = Array(contents)
}
self.init(scalars: scalars)
}

public init(scalars: [Scalar]) {
self.dimensions = scalars.count
self.scalars = scalars
}
}

// MARK: - Initialization: Randomized

extension Vector where Scalar == Float {
public static func randUniform(
count: Int,
range: ClosedRange<Float> = 0.0...1.0
) -> Vector {
var generator = SystemRandomNumberGenerator()
return self.randUniform(count: count, range: range, using: &generator)
}

public static func randUniform<T>(
count: Int,
range: ClosedRange<Float> = 0.0...1.0,
using generator: inout T
) -> Vector where T: RandomNumberGenerator {
let scalars = Surge.randUniform(count: count, range: range, using: &generator)
return Vector(scalars: scalars)
}

public static func randNormal(
count: Int,
mu: Float = 0.0,
sigma: Float = 1.0
) -> Vector {
var generator = SystemRandomNumberGenerator()
return self.randNormal(count: count, mu: mu, sigma: sigma, using: &generator)
}

public static func randNormal<T>(
count: Int,
mu: Float = 0.0,
sigma: Float = 1.0,
using generator: inout T
) -> Vector where T: RandomNumberGenerator {
let scalars = Surge.randNormal(count: count, mu: mu, sigma: sigma, using: &generator)
return Vector(scalars: scalars)
}
}

extension Vector where Scalar == Double {
public static func randUniform(
count: Int,
range: ClosedRange<Double> = 0.0...1.0
) -> Vector {
var generator = SystemRandomNumberGenerator()
return self.randUniform(count: count, range: range, using: &generator)
}

public static func randUniform<T>(
count: Int,
range: ClosedRange<Double> = 0.0...1.0,
using generator: inout T
) -> Vector where T: RandomNumberGenerator {
let scalars = Surge.randUniform(count: count, range: range, using: &generator)
return Vector(scalars: scalars)
}

public static func randNormal(
count: Int,
mu: Double = 0.0,
sigma: Double = 1.0
) -> Vector {
var generator = SystemRandomNumberGenerator()
return self.randNormal(count: count, mu: mu, sigma: sigma, using: &generator)
}

public static func randNormal<T>(
count: Int,
mu: Double = 0.0,
sigma: Double = 1.0,
using generator: inout T
) -> Vector where T: RandomNumberGenerator {
let scalars = Surge.randNormal(count: count, mu: mu, sigma: sigma, using: &generator)
return Vector(scalars: scalars)
}
}

// MARK: - ExpressibleByArrayLiteral

extension Vector: ExpressibleByArrayLiteral {
Expand Down
Loading