@@ -8,7 +8,11 @@ import _Differentiation
88// https://bugs.swift.org/browse/TF-1193
99
1010extension  Dictionary :  @retroactive   Differentiable  where  Value:  Differentiable  { 
11+     /// Tangent Vectors for each key-value pair
1112    public  typealias  TangentVector  =  [ Key :  Value . TangentVector ] 
13+ 
14+     /// Shift the dictionary by the given ``TangentVector``
15+     /// All keys in the given shift vectar must exist in the dictionary
1216    public  mutating  func  move( by direction:  TangentVector )  { 
1317        for  (componentKey,  componentDirection)  in  direction { 
1418            func  fatalMissingComponent( )  ->  Value  { 
@@ -18,6 +22,7 @@ extension Dictionary: @retroactive Differentiable where Value: Differentiable {
1822        } 
1923    } 
2024
25+     /// Create a zero tangent vector for every key
2126    public  var  zeroTangentVectorInitializer :  ( )  ->  TangentVector  { 
2227        let  listOfKeys  =  keys  // capturing only what's needed, not the entire self, in order to not waste memory
2328        func  initializer( )  ->  Self . TangentVector  { 
@@ -29,14 +34,17 @@ extension Dictionary: @retroactive Differentiable where Value: Differentiable {
2934
3035/// Implements the `AdditiveArithmetic` requirements.
3136extension  Dictionary :  @retroactive   AdditiveArithmetic  where  Value:  AdditiveArithmetic  { 
37+     /// Combine two dictionaries by adding their keys
3238    public  static  func  +  ( _ lhs:  Self ,  _ rhs:  Self )  ->  Self  { 
3339        lhs. merging ( rhs,  uniquingKeysWith:  + ) 
3440    } 
3541
42+     /// Combine two dictionaries by subtracting their keys
3643    public  static  func  -  ( _ lhs:  Self ,  _ rhs:  Self )  ->  Self  { 
3744        lhs. merging ( rhs. mapValues  {  . zero -  $0 } ,  uniquingKeysWith:  + ) 
3845    } 
3946
47+     /// Return an empty dictionary
4048    public  static  var  zero :  Self  {  [ : ]  } 
4149} 
4250
0 commit comments