File: forward_mode_inout.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (161 lines) | stat: -rw-r--r-- 4,107 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-forward-mode-differentiation)
// REQUIRES: executable_test

import StdlibUnittest
import DifferentiationUnittest

var ForwardModeInoutTests = TestSuite("ForwardModeInoutDifferentiation")

//===----------------------------------------------------------------------===//
// Inout tests.
//===----------------------------------------------------------------------===//

import DifferentiationUnittest
import StdlibUnittest

ForwardModeInoutTests.test("Float.+=") {
  func mutatingAddWrapper(_ x: Float, _ y: Float) -> Float {
    var result: Float = x
    result += y
    return result
  }
  expectEqual(20, differential(at: 4, 5, of: mutatingAddWrapper)(10, 10))
}

ForwardModeInoutTests.test("Float.-=") {
  func mutatingSubtractWrapper(_ x: Float, _ y: Float) -> Float {
    var result: Float = x
    result -= y
    return result
  }
  expectEqual(0, differential(at: 4, 5, of: mutatingSubtractWrapper)(10, 10))
  expectEqual(10, differential(at: 4, 5, of: mutatingSubtractWrapper)(20, 10))
}

ForwardModeInoutTests.test("Float.*=") {
  func mutatingMultiplyWrapper(_ x: Float, _ y: Float) -> Float {
    var result: Float = x
    result *= y
    return result
  }
  expectEqual(22, differential(at: 4, 5, of: mutatingMultiplyWrapper)(2, 3))
}

ForwardModeInoutTests.test("Float./=") {
  func mutatingDivideWrapper(_ x: Float, _ y: Float) -> Float {
    var result: Float = x
    result /= y
    return result
  }
  expectEqual(-1, differential(at: 2, 3, of: mutatingDivideWrapper)(3, 9))
}

// Simplest possible `inout` parameter differentiation.
ForwardModeInoutTests.test("InoutIdentity") {
  // Semantically, an empty function with an `inout` parameter is an identity
  // function.
  func inoutIdentity(_ x: inout Float) {}

  func identity(_ x: Float) -> Float {
    var result = x
    inoutIdentity(&result)
    return result
  }
  expectEqual(1, derivative(at: 10, of: identity))
  expectEqual(10, differential(at: 10, of: identity)(10))

  func inoutIdentityGeneric<T: Differentiable>(_ x: inout T) {}

  func identityGeneric<T: Differentiable>(_ x: T) -> T {
    var result: T = x
    inoutIdentityGeneric(&result)
    return result
  }
  expectEqual(1, derivative(at: 10.0, of: identityGeneric))
  expectEqual(10, differential(at: 10.0, of: identityGeneric)(10))
}

ForwardModeInoutTests.test("MultipleInoutParams") {
  func swap<T: Differentiable>(_ x: inout T, _ y: inout T) {
    let z = x
    x = y
    y = z
  }

  func first<T: Differentiable>(_ x: T, _ y: T) -> T {
    var p1 = x
    var p2 = y
    swap(&p1, &p2)
    return p2
  }
  expectEqual(1, differential(at: 1, 1, of: first)(1, 2))

  func second<T: Differentiable>(_ x: T, _ y: T) -> T {
    var p1 = x
    var p2 = y
    swap(&p1, &p2)
    return p1
  }
  expectEqual(2, differential(at: 1, 1, of: second)(1, 2))
}

ForwardModeInoutTests.test("StructMutatingMethod") {
  struct Mut: Differentiable {
    var x: Float

    mutating func add(_ y: Float) {
      self.x += y
    }

    mutating func addMut(_ m: Mut) {
      self.x += m.x
    }
  }

  func identity(_ y: Float) -> Float {
    var mut = Mut(x: 0.0)
    mut.add(y)
    return mut.x
  }
  expectEqual(1, derivative(at: 1, of: identity))

  func identity2(_ y: Float) -> Float {
    var mut = Mut(x: 0.0)
    let mut2 = Mut(x: y)
    mut.addMut(mut2)
    return mut.x
  }
  expectEqual(1, derivative(at: 1, of: identity2))

  func double(_ y: Float) -> Float {
    var mut = Mut(x: y)
    mut.add(y)
    return mut.x
  }
  expectEqual(2, derivative(at: 1, of: double))

  func double2(_ y: Float) -> Float {
    var mut = Mut(x: y)
    let mut2 = Mut(x: y)
    mut.addMut(mut2)
    return mut.x
  }
  expectEqual(2, derivative(at: 1, of: double2))

  func square(_ y: Float) -> Float {
    var mut = Mut(x: 0.0)
    mut.add(y * y)
    return mut.x
  }
  expectEqual(6, derivative(at: 3, of: square))

  func square2(_ y: Float) -> Float {
    var mut = Mut(x: 0.0)
    let mut2 = Mut(x: y * y)
    mut.addMut(mut2)
    return mut.x
  }
  expectEqual(6, derivative(at: 3, of: square2))
}

runAllTests()