Skip to content

Defines derivatives for remaining tgmath math functions. #27559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion stdlib/public/Platform/tgmath.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,27 @@ func _vjpExp(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (value, { v in value * v })
}

@usableFromInline
func _vjpExp2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
let value = exp2(x)
return (value, { v in v * ${T}(M_LN2) * value })
}

@usableFromInline
func _vjpLog(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (log(x), { v in v / x })
}

@usableFromInline
func _vjpLog10(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (log10(x), { v in v * ${T}(M_LOG10E) / x})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (log10(x), { v in v * ${T}(M_LOG10E) / x})
return (log10(x), { v in v * ${T}(M_LOG10E) / x })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 6b1ebf2

}

@usableFromInline
func _vjpLog2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (log2(x), { v in v / (${T}(M_LN2) * x)})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent by 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on c92e34b

}

@usableFromInline
func _vjpSin(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (sin(x), { v in v * cos(x) })
Expand All @@ -122,6 +138,72 @@ func _vjpTan(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
let value = tan(x)
return (value, { v in v * (1 + value * value) })
}

@usableFromInline
func _vjpAsin(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (asin(x), { v in v / sqrt(1 - x * x) })
}

@usableFromInline
func _vjpAcos(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (acos(x), { v in -v / sqrt(1 - x * x) })
}

@usableFromInline
func _vjpAtan(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (atan(x), { v in v / (1 + x * x) })
}

@usableFromInline
func _vjpSinh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (sinh(x), { v in v * cosh(x) })
}

@usableFromInline
func _vjpCosh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (cosh(x), { v in v * sinh(x) })
}

@usableFromInline
func _vjpTanh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
let value = tanh(x)
return (value, { v in v * (1 - value * value) })
}

@usableFromInline
func _vjpAsinh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (asinh(x), { v in v / sqrt(1 + x * x) })
}

@usableFromInline
func _vjpAcosh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (acosh(x), { v in v / sqrt(x * x - 1) })
}

@usableFromInline
func _vjpAtanh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (atanh(x), { v in v / (1 - x * x) })
}

@usableFromInline
func _vjpExpm1(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (expm1(x), { v in exp(x) * v })
}

@usableFromInline
func _vjpLog1p(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (log1p(x), { v in v / ( x + 1) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (log1p(x), { v in v / ( x + 1) })
return (log1p(x), { v in v / (x + 1) })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 6b1ebf2

}

@usableFromInline
func _vjpErf(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (erf(x), { v in v * ${T}(M_2_SQRTPI) * exp(-x * x) })
}

@usableFromInline
func _vjpErfc(_ x: ${T}) -> (${T}, (${T}) -> ${T}) {
return (erfc(x), { v in v * -${T}(M_2_SQRTPI) * exp(-x * x) })
}
% if T == 'Float80':
#endif
% end
Expand Down Expand Up @@ -201,7 +283,14 @@ UnaryIntrinsicFunctions = [
]

# SWIFT_ENABLE_TENSORFLOW
HasVJP = ["exp", "log", "tan", "cos", "sin"]
HasVJP = [
'acos', 'asin', 'atan', 'tan',
'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh',
'expm1',
'log1p',
'erf', 'erfc',
'cos', 'sin', 'exp', 'exp2', 'log', 'log10', 'log2'
]

def AllFloatTypes():
for bits in allFloatBits:
Expand Down
18 changes: 17 additions & 1 deletion test/stdlib/tgmath.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,26 @@ MathTests.test("${T}") {
% for T in ['Float', 'Float80']:
MathTests.test("gradient_${T}") {
expectEqualWithTolerance(7.3890560989306502274, gradient(at: 2.0 as ${T}, in: exp), ulps:16)
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps:16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps:16)
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps: 16)

Same for all other occurrences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on c92e34b

expectEqualWithTolerance(7.3890560989306502274, gradient(at: 2.0 as ${T}, in: expm1), ulps:16)
expectEqualWithTolerance(0.5, gradient(at: 2.0 as ${T}, in: log), ulps:16)
expectEqualWithTolerance(0.21714724095162590833, gradient(at: 2.0 as ${T}, in: log10), ulps:16)
expectEqualWithTolerance(0.7213475204444817278, gradient(at: 2.0 as ${T}, in: log2), ulps:16)
expectEqualWithTolerance(0.33333333333333333334, gradient(at: 2.0 as ${T}, in: log1p), ulps:16)
expectEqualWithTolerance(5.774399204041917612, gradient(at: 2.0 as ${T}, in: tan), ulps:16)
expectEqualWithTolerance(-0.416146836547142387, gradient(at: 2.0 as ${T}, in: sin), ulps:16)
expectEqualWithTolerance(-0.9092974268256816954, gradient(at: 2.0 as ${T}, in: cos), ulps:16)
expectEqualWithTolerance(-0.416146836547142387, gradient(at: 2.0 as ${T}, in: sin), ulps:16)
expectEqualWithTolerance(1.154700538379251529, gradient(at: 0.5 as ${T}, in: asin), ulps:16)
expectEqualWithTolerance(-1.154700538379251529, gradient(at: 0.5 as ${T}, in: acos), ulps:16)
expectEqualWithTolerance(0.8, gradient(at: 0.5 as ${T}, in: atan), ulps:16)
expectEqualWithTolerance(3.7621956910836314597, gradient(at: 2.0 as ${T}, in: sinh), ulps:16)
expectEqualWithTolerance(3.6268604078470187677, gradient(at: 2.0 as ${T}, in: cosh), ulps:16)
expectEqualWithTolerance(0.07065082485316446565, gradient(at: 2.0 as ${T}, in: tanh), ulps:16)
expectEqualWithTolerance(0.44721359549995793928, gradient(at: 2.0 as ${T}, in: asinh), ulps:16)
expectEqualWithTolerance(0.5773502691896257645, gradient(at: 2.0 as ${T}, in: acosh), ulps:16)
expectEqualWithTolerance(1.3333333333333333334, gradient(at: 0.5 as ${T}, in: atanh), ulps:16)
expectEqualWithTolerance(0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erf), ulps:16)
expectEqualWithTolerance(-0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erfc), ulps:16)
}
%end

Expand Down