|
| 1 | +struct RealFunctionsDerivativesGenerator { |
| 2 | + static func realFunctionsDerivativesExtension(type: String, floatingPointType: String) -> String { |
| 3 | + """ |
| 4 | + #if canImport(_Differentiation) |
| 5 | + import _Differentiation |
| 6 | + import RealModule |
| 7 | + |
| 8 | + // MARK: ElementaryFunctions derivatives |
| 9 | + extension \(type) { |
| 10 | + @derivative(of: exp) |
| 11 | + public static func _vjpExp(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 12 | + let value = exp(x) |
| 13 | + return (value: value, pullback: { v in v * value }) |
| 14 | + } |
| 15 | + |
| 16 | + @derivative(of: expMinusOne) |
| 17 | + public static func _vjpExpMinusOne(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 18 | + return (value: expMinusOne(x), pullback: { v in v * exp(x) }) |
| 19 | + } |
| 20 | + |
| 21 | + @derivative(of: cosh) |
| 22 | + public static func _vjpCosh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 23 | + (value: cosh(x), pullback: { v in sinh(x) }) |
| 24 | + } |
| 25 | + |
| 26 | + @derivative(of: sinh) |
| 27 | + public static func _vjpSinh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 28 | + (value: sinh(x), pullback: { v in cosh(x) }) |
| 29 | + } |
| 30 | + |
| 31 | + @derivative(of: tanh) |
| 32 | + public static func _vjpTanh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 33 | + ( |
| 34 | + value: tanh(x), |
| 35 | + pullback: { v in |
| 36 | + let coshx = cosh(x) |
| 37 | + return v / (coshx * coshx) |
| 38 | + } |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + @derivative(of: cos) |
| 43 | + public static func _vjpCos(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 44 | + (value: cos(x), pullback: { v in -v * sin(x) }) |
| 45 | + } |
| 46 | + |
| 47 | + @derivative(of: sin) |
| 48 | + public static func _vjpSin(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 49 | + (value: sin(x), pullback: { v in v * cos(x) }) |
| 50 | + } |
| 51 | + |
| 52 | + @derivative(of: tan) |
| 53 | + public static func _vjpTan(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 54 | + ( |
| 55 | + value: tan(x), |
| 56 | + pullback: { v in |
| 57 | + let cosx = cos(x) |
| 58 | + return v / (cosx * cosx) |
| 59 | + } |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + @derivative(of: log(_:)) |
| 64 | + public static func _vjpLog(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 65 | + (value: log(x), pullback: { v in v / x }) |
| 66 | + } |
| 67 | + |
| 68 | + @derivative(of: acosh) |
| 69 | + public static func _vjpAcosh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 70 | + // only valid for x > 1 |
| 71 | + return (value: acosh(x), pullback: { v in v / sqrt(x * x - 1) }) |
| 72 | + } |
| 73 | + |
| 74 | + @derivative(of: asinh) |
| 75 | + public static func _vjpAsinh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 76 | + (value: asinh(x), pullback: { v in v / sqrt(x * x + 1) }) |
| 77 | + } |
| 78 | + |
| 79 | + @derivative(of: atanh) |
| 80 | + public static func _vjpAtanh(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 81 | + (value: atanh(x), pullback: { v in v / (1 - x * x) }) |
| 82 | + } |
| 83 | + |
| 84 | + @derivative(of: acos) |
| 85 | + public static func _vjpAcos(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 86 | + (value: acos(x), pullback: { v in -v / (1 - x * x) }) |
| 87 | + } |
| 88 | + |
| 89 | + @derivative(of: asin) |
| 90 | + public static func _vjpAsin(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 91 | + (value: asin(x), pullback: { v in v / (1 - x * x) }) |
| 92 | + } |
| 93 | + |
| 94 | + @derivative(of: atan) |
| 95 | + public static func _vjpAtan(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 96 | + (value: atan(x), pullback: { v in v / (x * x + 1) }) |
| 97 | + } |
| 98 | + |
| 99 | + @derivative(of: log(onePlus:)) |
| 100 | + public static func _vjpLog(onePlus x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 101 | + (value: log(onePlus: x), pullback: { v in v / (1 + x) }) |
| 102 | + } |
| 103 | + |
| 104 | + @derivative(of: pow) |
| 105 | + public static func _vjpPow(_ x: \(type), _ y: \(type)) -> (value: \(type), pullback: (\(type)) -> (\(type), \(type))) { |
| 106 | + let value = pow(x, y) |
| 107 | + // pullback wrt y is not defined for (x < 0) and (x = 0, y = 0) |
| 108 | + return (value: value, pullback: { v in (v * y * pow(x, y - 1), v * value * log(x)) }) |
| 109 | + } |
| 110 | + |
| 111 | + @derivative(of: pow) |
| 112 | + public static func _vjpPow(_ x: \(type), _ n: Int) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 113 | + (value: pow(x, n), pullback: { v in v * \(floatingPointType)(n) * pow(x, n - 1) }) |
| 114 | + } |
| 115 | + |
| 116 | + @derivative(of: sqrt) |
| 117 | + public static func _vjpSqrt(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 118 | + let value = sqrt(x) |
| 119 | + return (value: value, pullback: { v in v / (2 * value) }) |
| 120 | + } |
| 121 | + |
| 122 | + @derivative(of: root) |
| 123 | + public static func _vjpRoot(_ x: \(type), _ n: Int) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 124 | + let value = root(x, n) |
| 125 | + return (value: value, pullback: { v in v * value / (x * \(floatingPointType)(n)) }) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // MARK: RealFunctions derivatives |
| 130 | + extension \(type) { |
| 131 | + @derivative(of: erf) |
| 132 | + public static func _vjpErf(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 133 | + (value: erf(x), pullback: { v in 2 * exp(-x * x) / .sqrt(\(floatingPointType).pi) }) |
| 134 | + } |
| 135 | + |
| 136 | + @derivative(of: erfc) |
| 137 | + public static func _vjpErfc(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 138 | + (value: erfc(x), pullback: { v in -2 * exp(-x * x) / .sqrt(\(floatingPointType).pi) }) |
| 139 | + } |
| 140 | + |
| 141 | + @derivative(of: exp2) |
| 142 | + public static func _vjpExp2(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 143 | + let value = exp2(x) |
| 144 | + return (value, { v in v * value * .log(2) }) |
| 145 | + } |
| 146 | + |
| 147 | + @derivative(of: exp10) |
| 148 | + public static func _vjpExp10(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 149 | + let value = exp10(x) |
| 150 | + return (value, { v in v * value * .log(10) }) |
| 151 | + } |
| 152 | + |
| 153 | + @derivative(of: gamma) |
| 154 | + public static func _vjpGamma(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 155 | + fatalError("unimplemented") |
| 156 | + } |
| 157 | + |
| 158 | + @derivative(of: log2) |
| 159 | + public static func _vjpLog2(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 160 | + (value: log2(x), pullback: { v in v / (.log(2) * x) }) |
| 161 | + } |
| 162 | + |
| 163 | + @derivative(of: log10) |
| 164 | + public static func _vjpLog10(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 165 | + (value: log10(x), pullback: { v in v / (.log(10) * x) }) |
| 166 | + } |
| 167 | + |
| 168 | + @derivative(of: logGamma) |
| 169 | + public static func _vjpLogGamma(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 170 | + fatalError("unimplemented") |
| 171 | + } |
| 172 | + |
| 173 | + @derivative(of: atan2) |
| 174 | + public static func _vjpAtan2(y: \(type), x: \(type)) -> (value: \(type), pullback: (\(type)) -> (\(type), \(type))) { |
| 175 | + ( |
| 176 | + value: atan2(y: y, x: x), |
| 177 | + pullback: { v in |
| 178 | + let c = x * x + y * y |
| 179 | + return (v * x / c, -v * y / c) |
| 180 | + } |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | + @derivative(of: hypot) |
| 185 | + public static func _vjpHypot(_ x: \(type), _ y: \(type)) -> (value: \(type), pullback: (\(type)) -> (\(type), \(type))) { |
| 186 | + ( |
| 187 | + value: hypot(x, y), |
| 188 | + pullback: { v in |
| 189 | + let c = sqrt(x * x + y * y) |
| 190 | + return (v * x / c, v * y / c) |
| 191 | + } |
| 192 | + ) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // MARK: FloatingPoint functions derivatives |
| 197 | + extension \(type) { |
| 198 | + @derivative(of: abs) |
| 199 | + public static func _vjpAbs(_ x: \(type)) -> (value: \(type), pullback: (\(type)) -> \(type)) { |
| 200 | + \({ |
| 201 | + if type == floatingPointType { |
| 202 | + "x < 0 ? (value: -x, pullback: { v in .zero - v }) : (value: x, pullback: { v in v })" |
| 203 | + } else { |
| 204 | + "(value: abs(x), pullback: { v in v.replacing(with: -v, where: x .< .zero) })" |
| 205 | + } |
| 206 | + }()) |
| 207 | + } |
| 208 | + } |
| 209 | + #endif |
| 210 | + """ |
| 211 | + } |
| 212 | +} |
0 commit comments