|
735 | 735 | return (Dual{T}(sd, cd * π * partials(d)), Dual{T}(cd, -sd * π * partials(d))) |
736 | 736 | end |
737 | 737 |
|
| 738 | +# LinearAlgebra.givensAlgorithm # |
| 739 | +#-------------------------------# |
| 740 | + |
| 741 | +# This definition ensures that we match `LinearAlgebra.givensAlgorithm` |
| 742 | +# for non-dual numbers (i.e., `ForwardDiff.Dual` with zero partials) |
| 743 | +# `LinearAlgebra.givensAlgorithm` is derived from LAPACK's dlartg |
| 744 | +# which is [documented](https://netlib.org/lapack/explore-html/da/dd3/group__lartg_ga86f8f877eaea0386cdc2c3c175d9ea88.html) to return |
| 745 | +# three values c, s, u for two arguments x and y with |
| 746 | +# u = sgn(x) sqrt(x^2 + y^2) |
| 747 | +# c = x/u |
| 748 | +# s = y/u |
| 749 | +# The function is discontinuous in u at x=0 |
| 750 | +@define_binary_dual_op( |
| 751 | + LinearAlgebra.givensAlgorithm, |
| 752 | + begin |
| 753 | + vx, vy = value(x), value(y) |
| 754 | + c, s, u = LinearAlgebra.givensAlgorithm(vx, vy) |
| 755 | + ∂c∂x = s^2 / u |
| 756 | + ∂c∂y = ∂s∂x = -(c * s / u) |
| 757 | + ∂s∂y = c^2 / u |
| 758 | + ∂x = partials(x) |
| 759 | + ∂y = partials(y) |
| 760 | + ∂c = _mul_partials(∂x, ∂y, ∂c∂x, ∂c∂y) |
| 761 | + ∂s = _mul_partials(∂x, ∂y, ∂s∂x, ∂s∂y) |
| 762 | + ∂u = _mul_partials(∂x, ∂y, c, s) |
| 763 | + return Dual{Txy}(c, ∂c), Dual{Txy}(s, ∂s), Dual{Txy}(u, ∂u) |
| 764 | + end, |
| 765 | + begin |
| 766 | + vx = value(x) |
| 767 | + c, s, u = LinearAlgebra.givensAlgorithm(vx, y) |
| 768 | + ∂c∂x = s^2 / u |
| 769 | + ∂s∂x = -(c * s / u) |
| 770 | + ∂x = partials(x) |
| 771 | + ∂c = ∂c∂x * ∂x |
| 772 | + ∂s = ∂s∂x * ∂x |
| 773 | + ∂u = c * ∂x |
| 774 | + return Dual{Tx}(c, ∂c), Dual{Tx}(s, ∂s), Dual{Tx}(u, ∂u) |
| 775 | + end, |
| 776 | + begin |
| 777 | + vy = value(y) |
| 778 | + c, s, u = LinearAlgebra.givensAlgorithm(x, vy) |
| 779 | + ∂c∂y = -(c * s / u) |
| 780 | + ∂s∂y = c^2 / u |
| 781 | + ∂y = partials(y) |
| 782 | + ∂c = ∂c∂y * ∂y |
| 783 | + ∂s = ∂s∂y * ∂y |
| 784 | + ∂u = s * ∂y |
| 785 | + return Dual{Ty}(c, ∂c), Dual{Ty}(s, ∂s), Dual{Ty}(u, ∂u) |
| 786 | + end, |
| 787 | +) |
| 788 | + |
738 | 789 | # Symmetric eigvals # |
739 | 790 | #-------------------# |
740 | 791 |
|
|
0 commit comments