-
Notifications
You must be signed in to change notification settings - Fork 166
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
Dual will not work correctly for complex numbers #297
Comments
I'll try have a look at implementing the changes myself, and will open a pull request if I succeed |
Hi @petar-andrejic , thanks for initiating this discussion. I don't deal with complex numbers and I'm currently very busy with my main project (Reaktoro - https://reaktoro.org) which uses |
So digging a bit further, the main issue is that the current implementation only makes sense for holomorphic complex functions, since otherwise one has to consider wirtinger derivatives (which would have to be a new api specifically for complex numbers and as such probably out of scope since you're busy with other things). The current implementation works fine as long as one uses only holomorphic functions, but things like real, imag, abs and conj are not holomorphic, and ideally they would somehow not be implemented and refuse to compile for Dual<complex, complex>, I'm not sure what a clean way of doing that would be though. As it currently stands the implementation should still be fine for std::complex, i.e. using std::complex to take the derivative of complex valued functions of a real variable. However, that doesn't work for me since the compiler doesn't find an appropriate template, e.g.
the above won't compile since it can't find an appropriate template for cos and sin |
As it stands the following overloads seem to be problematic, and restrict the type to work only for real types:
This contradicts the info given in the tutorial, which suggests using
Dual<std::complex<double>,std::complex<double>>
A simple example of how it goes wrong:
The output should be ideally
or similar
But instead we get
which matches what we can see the hardcoded methods are actually doing. Abs2 is doing a similarly problematic thing by returning the number times itself no matter what type we are passing in.
As it stands, it should be fine to use std::real, std::imag, not so sure about std::conj. The first two work correctly for real types anyway, while std::conj on a double returns a complex which could be a problem.
For abs2 and abs, this would have to be changed as well, abs2 to work correctly even for complex types, and in the apply method for abs binary comparisons are used, which will not work for complex numbers
The text was updated successfully, but these errors were encountered: