@@ -317,42 +317,53 @@ ArrayAutoDiffTests.test("ExpressibleByArrayLiteralIndirect") {
317
317
}
318
318
319
319
ArrayAutoDiffTests . test ( " Array.+ " ) {
320
- struct TwoArrays : Differentiable {
321
- var a : [ Float ]
322
- var b : [ Float ]
320
+ func sumFirstThreeConcatenating ( _ a : [ Float ] , _ b : [ Float ] ) -> Float {
321
+ let c = a + b
322
+ return c [ 0 ] + c [ 1 ] + c [ 2 ]
323
323
}
324
324
325
- func sumFirstThreeConcatenated( _ arrs: TwoArrays ) -> Float {
326
- let c = arrs. a + arrs. b
325
+ expectEqual (
326
+ ( . init( [ 1 , 1 ] ) , . init( [ 1 , 0 ] ) ) ,
327
+ gradient ( at: [ 0 , 0 ] , [ 0 , 0 ] , in: sumFirstThreeConcatenating) )
328
+ expectEqual (
329
+ ( . init( [ 1 , 1 , 1 , 0 ] ) , . init( [ 0 , 0 ] ) ) ,
330
+ gradient ( at: [ 0 , 0 , 0 , 0 ] , [ 0 , 0 ] , in: sumFirstThreeConcatenating) )
331
+ expectEqual (
332
+ ( . init( [ ] ) , . init( [ 1 , 1 , 1 , 0 ] ) ) ,
333
+ gradient ( at: [ ] , [ 0 , 0 , 0 , 0 ] , in: sumFirstThreeConcatenating) )
334
+
335
+ func identity( _ array: [ Float ] ) -> [ Float ] {
336
+ var results : [ Float ] = [ ]
337
+ for i in withoutDerivative ( at: array. indices) {
338
+ results = results + [ array [ i] ]
339
+ }
340
+ return results
341
+ }
342
+ let v = FloatArrayTan ( [ 4 , - 5 , 6 ] )
343
+ expectEqual ( v, pullback ( at: [ 1 , 2 , 3 ] , in: identity) ( v) )
344
+ }
345
+
346
+ ArrayAutoDiffTests . test ( " Array.+= " ) {
347
+ func sumFirstThreeConcatenating( _ a: [ Float ] , _ b: [ Float ] ) -> Float {
348
+ var c = a
349
+ c += b
327
350
return c [ 0 ] + c[ 1 ] + c[ 2 ]
328
351
}
329
352
330
353
expectEqual (
331
- TwoArrays . TangentVector (
332
- a: FloatArrayTan ( [ 1 , 1 ] ) ,
333
- b: FloatArrayTan ( [ 1 , 0 ] ) ) ,
334
- gradient (
335
- at: TwoArrays ( a: [ 0 , 0 ] , b: [ 0 , 0 ] ) ,
336
- in: sumFirstThreeConcatenated) )
354
+ ( . init( [ 1 , 1 ] ) , . init( [ 1 , 0 ] ) ) ,
355
+ gradient ( at: [ 0 , 0 ] , [ 0 , 0 ] , in: sumFirstThreeConcatenating) )
337
356
expectEqual (
338
- TwoArrays . TangentVector (
339
- a: FloatArrayTan ( [ 1 , 1 , 1 , 0 ] ) ,
340
- b: FloatArrayTan ( [ 0 , 0 ] ) ) ,
341
- gradient (
342
- at: TwoArrays ( a: [ 0 , 0 , 0 , 0 ] , b: [ 0 , 0 ] ) ,
343
- in: sumFirstThreeConcatenated) )
357
+ ( . init( [ 1 , 1 , 1 , 0 ] ) , . init( [ 0 , 0 ] ) ) ,
358
+ gradient ( at: [ 0 , 0 , 0 , 0 ] , [ 0 , 0 ] , in: sumFirstThreeConcatenating) )
344
359
expectEqual (
345
- TwoArrays . TangentVector (
346
- a: FloatArrayTan ( [ ] ) ,
347
- b: FloatArrayTan ( [ 1 , 1 , 1 , 0 ] ) ) ,
348
- gradient (
349
- at: TwoArrays ( a: [ ] , b: [ 0 , 0 , 0 , 0 ] ) ,
350
- in: sumFirstThreeConcatenated) )
360
+ ( . init( [ ] ) , . init( [ 1 , 1 , 1 , 0 ] ) ) ,
361
+ gradient ( at: [ ] , [ 0 , 0 , 0 , 0 ] , in: sumFirstThreeConcatenating) )
351
362
352
363
func identity( _ array: [ Float ] ) -> [ Float ] {
353
364
var results : [ Float ] = [ ]
354
365
for i in withoutDerivative ( at: array. indices) {
355
- results = results + [ array [ i] ]
366
+ results += [ array [ i] ]
356
367
}
357
368
return results
358
369
}
0 commit comments