-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdifferentiable_protocol.swift
45 lines (41 loc) · 1.16 KB
/
differentiable_protocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// RUN: %target-typecheck-verify-swift
import _Differentiation
// Test `Differentiable` protocol conformances.
struct FloatWrapper {
var value: Float
}
extension FloatWrapper: AdditiveArithmetic {
static var zero: Self {
FloatWrapper(value: Float.zero)
}
static func + (lhs: Self, rhs: Self) -> Self {
return FloatWrapper(value: lhs.value + rhs.value)
}
static func - (lhs: Self, rhs: Self) -> Self {
return FloatWrapper(value: lhs.value + rhs.value)
}
}
extension FloatWrapper: Differentiable {
public typealias TangentVector = Self
}
struct Wrapper<T> {
var value: T
}
extension Wrapper: Equatable where T: Equatable {}
extension Wrapper: AdditiveArithmetic where T: AdditiveArithmetic {
static var zero: Self {
Wrapper(value: T.zero)
}
static func + (lhs: Self, rhs: Self) -> Self {
return Wrapper(value: lhs.value + rhs.value)
}
static func - (lhs: Self, rhs: Self) -> Self {
return Wrapper(value: lhs.value + rhs.value)
}
}
extension Wrapper: Differentiable where T: Differentiable {
typealias TangentVector = Wrapper<T.TangentVector>
mutating func move(by offset: TangentVector) {
value.move(by: offset.value)
}
}