-
Notifications
You must be signed in to change notification settings - Fork 62
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
Add realdot
and imagdot
from ChainRules
#474
Conversation
* Fix indentation * Test \ on complex inputs * Test ^ on complex inputs * Test identity on complex inputs * Test muladd on complex inputs * Test binary functions on complex inputs * Test functions on complex inputs * Release type constraint on exp * Add _realconjtimes * Use _realconjtimes in abs/abs2 rules * Add complex rule for hypot * Add generic rule for adjoint * Add generic rule for real * Add generic rule for imag * Add complex rule for hypot * Add rules/tests for Complex * Test frule for identity * Add missing angle test * Make inline just in case * Unify abs rules * Introduce _imagconjtimes utility function * Unify angle rules * Unify sign rules * Multiply by correct variable * Fix argument order * Bump ChainRulesTestUtils version number * Restrict to Complex * Use muladd * Update src/rulesets/Base/fastmath_able.jl Co-authored-by: willtebbutt <[email protected]> Co-authored-by: willtebbutt <[email protected]>
* rename DoesNotExist * rename Composite * bump version and compat * rename Zero * remove typos * reexport deprecated types manually
Pages = ["rule_definition_tools.jl"] | ||
Pages = [ | ||
"rule_definition_tools.jl", | ||
"utils.jl", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe they should be moved to src/rule_definition_tools.jl
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That file is full of metaprogramming stuff.
It doesn't need this.
Files called utils.jl
are always a mistake.
I think something like complex_math.jl
as it currently stands,
or additional_mathematical_functions.jl
if we think we will need more of them later.
Codecov Report
@@ Coverage Diff @@
## master #474 +/- ##
==========================================
+ Coverage 92.89% 93.00% +0.11%
==========================================
Files 15 16 +1
Lines 816 829 +13
==========================================
+ Hits 758 771 +13
Misses 58 58
Continue to review full report at Codecov.
|
I am not sure if this is worth adding to our API surface? idk I could be convinced that this is a crucial tool needed to define rules for nonholomorphic functions. |
Oh I didn't notice this was ChainRulesCore. Then yes, in that case, I do not know if this is worth adding here, in the spirit of keeping this package as-simple-as-possible. |
I'm not fully convinced either, that's why I asked in the SpecialFunctions PR 😄 I assumed it could be useful since it came up in these simple cases in SpecialFunctions and hence I assumed that it would likely pop up in other derivatives of non-holomorphic functions as well. I came across I still think it would be a bit annoying to have to copy |
realconjtimes
and imagconjtimes
from ChainRulesrealdot
and imagdot
from ChainRules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not yet done thinking about this
i am not sure how much my opinion on this is worth, as i'm unlikely to be a user of the code added, but fwiw... I think adding these particular methods to CRC seems fine to me, in a maybe rough ideas of what “a high-bar” means here:
(But as i say, perhaps as someone not often actually writing rules, this is setting the bar too high for adding more helpers functions) |
I agree with this
I think as of right now now it doesn't quite meet the bar. |
Just came across https://github.com/JuliaStats/StatsBase.jl/blob/0a179531f14d0140356fcf1f1d55bd0240030cf2/src/scalarstats.jl#L259-L261, so I found another use case for Maybe the example indicates though that it could be useful to add them to base? |
src/utils.jl
Outdated
@inline realdot(x::Real, y::Complex) = x * real(y) | ||
@inline realdot(x::Complex, y::Real) = real(x) * y | ||
@inline realdot(x::Real, y::Real) = x * y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be safe to generalize the cases, and then the last one is redundant since real(::Real)
is a no-op
@inline realdot(x::Real, y::Complex) = x * real(y) | |
@inline realdot(x::Complex, y::Real) = real(x) * y | |
@inline realdot(x::Real, y::Real) = x * y | |
@inline realdot(x::Real, y::Number) = x * real(y) | |
@inline realdot(x::Number, y::Real) = real(x) * y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this introduce an ambiguity, when both are real?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right.
I don't think these have been shown to be useful enough to be in base Julia, but it might be worth putting them in their own ultra-lightweight package. Then we can point to that package in our documentation, and package developers can depend on it if they find it useful. Simultaneously resolves the concerns with putting it here while still making the functionality available. |
Co-authored-by: Seth Axen <[email protected]>
The only problem with moving them to a separate package seems to be that |
Yeah, that's right. I'm thinking both
While |
I put together a package and added only |
Cool, we can add that as a dep for ChainRules.jl and SpecialFunctions.jl? |
It's not registered yet (and I'd like @sethaxen to approve the linked PR) but that's the plan 🙂 |
I initiated the registration process: JuliaRegistries/General#47024 |
Closed by JuliaDiff/ChainRules.jl#542. |
This PR adds
realconjtimes
realdot
andimagconjtimes
imagdot
, currently defined inChainRules
as_realconjtimes
and_imagconjtimes
. These optimizations ofreal(conj(x) * y)
, or more generallyreal(dot(x, y))
, andimag(conj(x) * y)
/imag(dot(x, y))
are useful more generally and could be used e.g. in JuliaMath/SpecialFunctions.jl#350.I extracted
src/rulesets/Base/utils.jl
(and its history) from ChainRules, renamed the functions, and added some tests and documentation.