@@ -986,44 +986,6 @@ public protocol Differentiable {
986
986
/// equivalent to exponential map, which moves `self` on the geodesic
987
987
/// surface along the given tangent vector.
988
988
mutating func move (along direction : TangentVector)
989
-
990
- /// A closure that produces a zero tangent vector and does not capture `self`.
991
- ///
992
- /// In some cases, the zero tangent vector of `self` is equal to
993
- /// `TangentVector.zero`. In other cases, the zero tangent vector depends on
994
- /// information in `self`, such as shape for an n-dimensional array type.
995
- /// For differentiable programming, it is more memory-efficient to define a
996
- /// custom `zeroTangentVectorInitializer` property which returns a closure
997
- /// that captures and uses only the necessary information to create a zero
998
- /// tangent vector. For example:
999
- ///
1000
- /// ```swift
1001
- /// struct Vector {
1002
- /// var scalars: [Float]
1003
- /// var count: Int { scalars.count }
1004
- /// init(repeating repeatedElement: Float, count: Int) { ... }
1005
- /// }
1006
- ///
1007
- /// extension Vector: Differentiable {
1008
- /// typealias TangentVector = Vector
1009
- ///
1010
- /// @noDerivative
1011
- /// var zeroTangentVectorInitializer: () -> TangentVector {
1012
- /// let count = self.count
1013
- /// return { TangentVector(repeating: 0, count: count) }
1014
- /// }
1015
- /// }
1016
- /// ```
1017
- ///
1018
- @noDerivative
1019
- var zeroTangentVectorInitializer: () -> TangentVector { get }
1020
- }
1021
-
1022
- extension Differentiable {
1023
- /// A tangent vector such that `move(along: zeroTangentVector)` will not modify
1024
- /// `self`.
1025
- @noDerivative
1026
- var zeroTangentVector: TangentVector { zeroTangentVectorInitializer () }
1027
989
}
1028
990
```
1029
991
@@ -1092,13 +1054,6 @@ public extension Differentiable where Self == TangentVector {
1092
1054
}
1093
1055
```
1094
1056
1095
- The ` zeroTangentVector ` property returns a tangent vector such that calling
1096
- ` move(along:) ` on the vector will not modify ` self ` . A zero tangent vector is
1097
- often used in the initialization of mathematical optimization, where tangent
1098
- vectors are initially zero and modified iteratively. This property may be
1099
- different from ` TangentVector.zero ` because some tangent vectors depend on
1100
- instance properties of ` self ` , e.g. the ` count ` property in ` Array ` .
1101
-
1102
1057
#### ` Differentiable ` conformances
1103
1058
1104
1059
Conforming a type to ` Differentiable ` tells Swift that changes in values of this
@@ -1146,13 +1101,6 @@ extension Array: Differentiable where Element: Differentiable {
1146
1101
self [i].move (along : Element .TangentVector (direction.elements [i]))
1147
1102
}
1148
1103
}
1149
-
1150
- @noDerivative
1151
- public var zeroTangentVectorInitializer: () -> TangentVector {
1152
- { [zeroInits = map (\.zeroTangentVectorInitializer )] in
1153
- TangentVector (zeroInits.map { $0 () })
1154
- }
1155
- }
1156
1104
}
1157
1105
1158
1106
// struct Dictionary<Key: Hashable, Value>
@@ -1173,14 +1121,6 @@ extension Dictionary: Differentiable where Value: Differentiable {
1173
1121
self [i].move (along : Value .TangentVector (direction.elements [i]))
1174
1122
}
1175
1123
}
1176
-
1177
- @noDerivative
1178
- public var zeroTangentVectorInitializer: () -> TangentVector {
1179
- { [keys = self .keys ] in
1180
- let pairs = zip (keys, sequence (first : .zero , next : {$0 }))
1181
- return TangentVector (Dictionary (uniqueKeysWithValues : pairs))
1182
- }
1183
- }
1184
1124
}
1185
1125
1186
1126
// enum Optional<Wrapped>
@@ -1199,18 +1139,6 @@ extension Optional: Differentiable where Wrapped: Differentiable {
1199
1139
self ? .move (along : value)
1200
1140
}
1201
1141
}
1202
-
1203
- @noDerivative
1204
- public var zeroTangentVectorInitializer: () -> TangentVector {
1205
- switch self {
1206
- case nil :
1207
- return { TangentVector (nil ) }
1208
- case let x? :
1209
- return { [zeroTanInit = x.zeroTangentVectorInitializer ] in
1210
- TangentVector (zeroTanInit ())
1211
- }
1212
- }
1213
- }
1214
1142
}
1215
1143
```
1216
1144
@@ -1261,13 +1189,12 @@ product manifold of the manifolds each differentiable variable's type
1261
1189
represents. Differentiable variables' types are required to conform to
1262
1190
` Differentiable ` because the synthesized implementation needs to access each
1263
1191
differentiable variable's type's ` TangentVector ` associated type and invoke each
1264
- differentiable variable's implementation of ` move(along:) ` and
1265
- ` zeroTangentVectorInitializer ` . Because the synthesized implementation needs to
1266
- invoke ` move(along:) ` on each differentiable variable, the differentiable
1267
- variables must have a ` move(along:) ` which satisfies the protocol requirement
1268
- and can be invoked on the property. That is, the property must be either a
1269
- variable (` var ` ) or a constant (` let ` ) with a non-` mutating ` implementation of
1270
- the ` move(along:) ` protocol requirement.
1192
+ differentiable variable's implementation of ` move(along:) ` . Because the
1193
+ synthesized implementation needs to invoke ` move(along:) ` on each differentiable
1194
+ variable, the differentiable variables must have a ` move(along:) ` which satisfies the
1195
+ protocol requirement and can be invoked on the property. That is, the property
1196
+ must be either a variable (` var ` ) or a constant (` let ` ) with a non-` mutating `
1197
+ implementation of the ` move(along:) ` protocol requirement.
1271
1198
1272
1199
The synthesized ` TangentVector ` has the same effective access level as the
1273
1200
original type declaration. Properties in the synthesized ` TangentVector ` have
@@ -1282,12 +1209,6 @@ example, synthesized `TangentVector`s always adopt the `AdditiveArithmetic` and
1282
1209
The synthesized ` move(along:) ` method calls ` move(along:) ` for each pair of a
1283
1210
differentiable variable and its corresponding property in ` TangentVector ` .
1284
1211
1285
- The synthesized ` zeroTangentVectorInitializer ` property returns a closure that
1286
- captures and calls each stored property's ` zeroTangentVectorInitializer `
1287
- closure. When memberwise derivation is not possible (e.g. for custom
1288
- user-defined ` TangentVector ` types), ` zeroTangentVectorInitializer ` is
1289
- synthesized as a ` { TangentVector.zero } ` closure.
1290
-
1291
1212
``` swift
1292
1213
struct Foo <T : Differentiable , U : Differentiable >: @memberwise Differentiable {
1293
1214
// `x` and `y` are the "differentiable variables".
@@ -1306,13 +1227,6 @@ struct Foo<T: Differentiable, U: Differentiable>: @memberwise Differentiable {
1306
1227
// x.move(along: direction.x)
1307
1228
// y.move(along: direction.y)
1308
1229
// }
1309
- //
1310
- // var zeroTangentVectorInitializer: () -> TangentVector {
1311
- // { [xTanInit = x.zeroTangentVectorInitializer,
1312
- // yTanInit = y.zeroTangentVectorInitializer] in
1313
- // TangentVector(x: xTanInit(), y: yTanInit())
1314
- // }
1315
- // }
1316
1230
}
1317
1231
```
1318
1232
@@ -1404,14 +1318,6 @@ struct Point<T: Real>: @memberwise Differentiable, @memberwise AdditiveArithmeti
1404
1318
// The compiler synthesizes:
1405
1319
//
1406
1320
// typealias TangentVector = Self
1407
- //
1408
- // @noDerivative
1409
- // var zeroTangentVectorInitializer: () -> TangentVector {
1410
- // { [xTanInit = x.zeroTangentVectorInitializer,
1411
- // yTanInit = y.zeroTangentVectorInitializer] in
1412
- // TangentVector(x: xTanInit(), y: yTanInit())
1413
- // }
1414
- // }
1415
1321
}
1416
1322
```
1417
1323
0 commit comments