Skip to content

Commit 635b9fe

Browse files
committed
Clean up tests and add docs related to WyRand
1 parent 68988c6 commit 635b9fe

6 files changed

Lines changed: 40 additions & 61 deletions

File tree

Sources/RandomModule/Random.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
Protocol for random vector.
3-
Random vector protocol and init.
2+
Random vector protocol using WyRand.
43
*/
54

65
import VectorModule
@@ -35,6 +34,18 @@ extension Double: Random {
3534
}
3635

3736
extension Vector {
37+
38+
/// Create a vector of random values from a uniform distrubtion over [0, 1).
39+
/// ```swift
40+
/// let vec = Vector<Double>.random(size: 5)
41+
/// ```
42+
/// This uses the WyRand pseudorandom number generator (PRNG) to generate the random values in the vector.
43+
/// Values are a uniform distribution over 0 to 1 excluding 1 which is denoted as [0, 1).
44+
/// Provide a seed input value to create a reproducible random vector.
45+
/// - Parameters:
46+
/// - size: Size of the vector.
47+
/// - seed: Seed for the random number generator.
48+
/// - Returns: Vector of random values.
3849
public static func random(size: Int, seed: UInt64? = nil) -> Vector where Scalar: Random {
3950
Scalar.random(size: size, seed: seed)
4051
}

Sources/RandomModule/WyRand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ https://lemire.me/blog/2019/03/19/the-fastest-conventional-random-number-generat
1111
import Foundation
1212

1313
public struct WyRand: RandomNumberGenerator {
14-
private var state : UInt64
14+
private var state: UInt64
1515

1616
public init(seed: UInt64? = nil) {
1717
state = seed ?? UInt64(abs(UUID().hashValue))

Sources/VectorModule/Trigonometry.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,39 @@ extension Float: Trigonometry {
3737
vForce.tan(a.buffer, result: &result.buffer)
3838
return result
3939
}
40-
40+
4141
public static func asin(_ a: Vector<Float>) -> Vector<Float> {
4242
var result = Vector(like: a)
4343
vForce.asin(a.buffer, result: &result.buffer)
4444
return result
4545
}
46-
46+
4747
public static func acos(_ a: Vector<Float>) -> Vector<Float> {
4848
var result = Vector(like: a)
4949
vForce.acos(a.buffer, result: &result.buffer)
5050
return result
5151
}
52-
52+
5353
public static func atan(_ a: Vector<Float>) -> Vector<Float> {
5454
var result = Vector(like: a)
5555
vForce.atan(a.buffer, result: &result.buffer)
5656
return result
5757
}
58-
58+
5959
public static func csc(_ a: Vector<Float>) -> Vector<Float> {
6060
var result = Vector(like: a)
6161
vForce.sin(a.buffer, result: &result.buffer)
6262
vForce.reciprocal(result.buffer, result: &result.buffer)
6363
return result
6464
}
65-
65+
6666
public static func sec(_ a: Vector<Float>) -> Vector<Float> {
6767
var result = Vector(like: a)
6868
vForce.cos(a.buffer, result: &result.buffer)
6969
vForce.reciprocal(result.buffer, result: &result.buffer)
7070
return result
7171
}
72-
72+
7373
public static func cot(_ a: Vector<Float>) -> Vector<Float> {
7474
var result = Vector(like: a)
7575
vForce.tan(a.buffer, result: &result.buffer)
@@ -98,39 +98,39 @@ extension Double: Trigonometry {
9898
vForce.tan(a.buffer, result: &result.buffer)
9999
return result
100100
}
101-
101+
102102
public static func asin(_ a: Vector<Double>) -> Vector<Double> {
103103
var result = Vector(like: a)
104104
vForce.asin(a.buffer, result: &result.buffer)
105105
return result
106106
}
107-
107+
108108
public static func acos(_ a: Vector<Double>) -> Vector<Double> {
109109
var result = Vector(like: a)
110110
vForce.acos(a.buffer, result: &result.buffer)
111111
return result
112112
}
113-
113+
114114
public static func atan(_ a: Vector<Double>) -> Vector<Double> {
115115
var result = Vector(like: a)
116116
vForce.atan(a.buffer, result: &result.buffer)
117117
return result
118118
}
119-
119+
120120
public static func csc(_ a: Vector<Double>) -> Vector<Double> {
121121
var result = Vector(like: a)
122122
vForce.sin(a.buffer, result: &result.buffer)
123123
vForce.reciprocal(result.buffer, result: &result.buffer)
124124
return result
125125
}
126-
126+
127127
public static func sec(_ a: Vector<Double>) -> Vector<Double> {
128128
var result = Vector(like: a)
129129
vForce.cos(a.buffer, result: &result.buffer)
130130
vForce.reciprocal(result.buffer, result: &result.buffer)
131131
return result
132132
}
133-
133+
134134
public static func cot(_ a: Vector<Double>) -> Vector<Double> {
135135
var result = Vector(like: a)
136136
vForce.tan(a.buffer, result: &result.buffer)
@@ -174,7 +174,8 @@ public func tan<Scalar>(_ vec: Vector<Scalar>) -> Vector<Scalar> where Scalar: T
174174

175175
/// Calculate the arcsine of each element in a vector.
176176
///
177-
/// Elements in the vector should be within the domain -1 ≤ x ≤ 1 otherwise results may be nan. Results are returned on the closed interval -π / 2 ≤ y ≤ π / 2.
177+
/// Elements in the vector should be within the domain -1 ≤ x ≤ 1 otherwise results may be nan.
178+
/// Results are returned on the closed interval -π / 2 ≤ y ≤ π / 2.
178179
/// ```swift
179180
/// let vec = Vector([-1, -0.5, 0, 0.5, 1])
180181
/// let result = asin(vec)

Tests/RandomDistTests.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.

Tests/RandomTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ struct RandomTests {
3333
let d = Vector<Double>.random(size: 4, seed: 12345)
3434
#expect(d == Vector<Double>([0.7132790974854359, 0.9815675033121781, 0.7952823641045631, 0.25364800714420743]))
3535
}
36+
37+
@Test func randomDist() {
38+
let a = Vector<Float>.randomDistribution(size: 4)
39+
let b = Vector<Double>.randomDistribution(size: 5)
40+
#expect(a[0] <= 1.0)
41+
#expect(b[0] <= 1.0)
42+
}
3643
}

Tests/TrigonometryTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ struct TrigonometryTests {
99

1010
@Test func vectorTrig() {
1111
let a = Vector<Float>([1, 2, 3, 4]) // single precision
12-
12+
1313
#expect(sin(a).isApproximatelyEqual(to: [0.841471, 0.909297, 0.14112, -0.756802]))
1414
#expect(cos(a).isApproximatelyEqual(to: [0.540302, -0.416147, -0.989992, -0.653644]))
1515
#expect(tan(a).isApproximatelyEqual(to: [1.55741, -2.18504, -0.142547, 1.15782], relativeTolerance: 1e-4))
16-
16+
1717
#expect(csc(a).isApproximatelyEqual(to: [1.18839511, 1.09975017, 7.0861674, -1.32134871]))
1818
#expect(sec(a).isApproximatelyEqual(to: [1.85081572, -2.40299796, -1.01010867, -1.52988566]))
1919
#expect(cot(a).isApproximatelyEqual(to: [0.64209262, -0.45765755, -7.01525255, 0.86369115]))
2020

2121
let b = Vector([1, 2, 3, 4.0]) // double precision
22-
22+
2323
#expect(sin(b).isApproximatelyEqual(to: [0.841471, 0.909297, 0.14112, -0.756802], relativeTolerance: 1e-4))
2424
#expect(cos(b).isApproximatelyEqual(to: [0.540302, -0.416147, -0.989992, -0.6536], relativeTolerance: 1e-4))
2525
#expect(tan(b).isApproximatelyEqual(to: [1.55741, -2.18504, -0.142547, 1.15782], relativeTolerance: 1e-4))
26-
26+
2727
#expect(csc(b).isApproximatelyEqual(to: [1.18839511, 1.09975017, 7.0861674, -1.32134871]))
2828
#expect(sec(b).isApproximatelyEqual(to: [1.85081572, -2.40299796, -1.01010867, -1.52988566]))
2929
#expect(cot(b).isApproximatelyEqual(to: [0.64209262, -0.45765755, -7.01525255, 0.86369115]))
3030
}
31-
31+
3232
@Test func vectorArcTrig() {
3333
let a = Vector<Float>([-1, -0.5, 0, 0.5, 1]) // single precision
3434
#expect(asin(a) == [-1.57079633, -0.52359878, 0.0, 0.52359878, 1.57079633])

0 commit comments

Comments
 (0)