-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Complex numbers get_real(), get_imag(), to_complex #2554
Conversation
…4.1 (tags/RELEASE_600/final)
…nto complex-numbers
Steve's fix for apply_scalar_unary
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.
cool lgtm! @rok-cesnovar I know you wanted to think about the names here, are you and @bob-carpenter cool with get_real()
and get_imag()
imo a little wordy but they're fine
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.
Lots of comments given this is your first PR. It shouldn't be hard to fix and I'm happy to sit down and work through it in more detail with you.
stan/math/prim/fun.hpp
Outdated
@@ -270,7 +271,6 @@ | |||
#include <stan/math/prim/fun/read_corr_matrix.hpp> | |||
#include <stan/math/prim/fun/read_cov_L.hpp> | |||
#include <stan/math/prim/fun/read_cov_matrix.hpp> | |||
#include <stan/math/prim/fun/real.hpp> |
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.
Don't remove real()
and imag()
. We won't use those functions by that name in Stan, but we need it to support programs written against the C++ standard library for our autodiff.
You can just have get_real()
and get_imag()
delegate to real()
and imag()
for implementation, but the generic form you have below is probably easier.
stan/math/prim/fun/to_complex.hpp
Outdated
* | ||
* Used for type casting from int to double for real component. | ||
* Otherwise arrays of complex don't work with ints. | ||
*/ |
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.
The following template shouldn't be necessary given the signature I suggested with promotion.
stan/math/prim/fun/to_complex.hpp
Outdated
} | ||
|
||
/** | ||
* Return a complex type from a integer real part and an imaginary part. |
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.
The return is a complex value, not a complex type. And the arguments are a real part and imaginary part, but both are optional, which should be documented along with default values. For example,
@param[in] re real component (default = 0)
@param[in] im imaginary component (default = 0)
You can see I also included the specification that both of these are input arguments (not things set by the program).
stan/math/prim/fun/get_real.hpp
Outdated
@@ -14,8 +14,8 @@ namespace math { | |||
* @param[in] z argument |
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.
"argument" is not sufficient doc. This should say something like "value whose real component is extracted" or something like that.
stan/math/prim/fun/get_real.hpp
Outdated
@@ -14,8 +14,8 @@ namespace math { | |||
* @param[in] z argument | |||
* @return real part of argument |
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.
[optional] it's nice to stay consistent in terminology. I'd suggest choosing "part" or "component" and sticking to it throughout the doc.
stan/math/prim/fun/to_complex.hpp
Outdated
* Return a complex type from a real part and an imaginary part. | ||
* Default values for both parts is 0. | ||
* | ||
* @tparam T type of real and |
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.
These don't need "and" (though that is how you'd write mathematics). And it should be "type of real component". The "type of real" would be double
or float
.
stan/math/prim/fun/to_complex.hpp
Outdated
* @tparam T type of real and | ||
* @param re real element | ||
* @param im imaginary element | ||
* @return Complex type |
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.
The return is a value, not a type.
stan/math/prim/fun/to_complex.hpp
Outdated
* @param im imaginary element | ||
* @return Complex type | ||
* | ||
* Used for type casting from int to double for real component. |
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.
Doc should be written actively in terms of what the function does, not how it's used (that can come later if it's not clear). So the doc for this would be the same as the doc or the previous example, only we shouldn't actually need this second function.
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
…nto complex-numbers
…nto complex-numbers
…4.1 (tags/RELEASE_600/final)
…nickdidio/math into complex-numbers"" This reverts commit dc46d7e.
…nto complex-numbers
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
…nto complex-numbers
…4.1 (tags/RELEASE_600/final)
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.
This is good to go when it passes tests.
So the tests for this are going to fail because it relies on Steve's apply-scalar-unary fix at #2549. I've tested both PR's together and it works. Not sure what to do about this, but it should work as soon as Steve's PR goes through. |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
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.
Thanks for fixing.
Summary
Adding support for complex scalar functions get_real(), get_imag() and to_complex().
get_real() takes in a complex number and outputs its real component, get_imag() does the same but for the imaginary component.
to_complex() takes in either 0, 1, or 2 ints or doubles and returns a complex number with its real and imaginary component set to the inputs, and any unfilled components set to 0.
Tests
Test files are available in test/unit/math/mix/fun and are named get_real_test.hpp, get_imag_test.hpp and to_complex_test.hpp
Tests can be ran with:
./runTests.py test/unit/math/mix/fun/get_real_test.hpp
./runTests.py test/unit/math/mix/fun/get_imag_test.hpp
./runTests.py test/unit/math/mix/fun/to_complex_test.hpp
Side Effects
None
Release notes
Added complex scalar functions get_real(), get_imag(), and to_complex.
Checklist
Copyright holder: Nicholas DiDio
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested