-
-
Notifications
You must be signed in to change notification settings - Fork 93
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 vals_c #1032
Complex numbers vals_c #1032
Conversation
… into complex-numbers2
…4.1 (tags/RELEASE_600/final)
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
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.
This looks great. The requested changes are all minor.
src/cmdstan/io/json/json_data.hpp
Outdated
@@ -119,6 +120,43 @@ class json_data : public stan::io::var_context { | |||
return empty_vec_r_; | |||
} | |||
|
|||
/** | |||
* Read out a vector of complex numbers from the specifed name. | |||
* @param name Name of Variable. |
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 needs @return
doc.
It should also have some light doc on the presumed input format or a pointer to where it's described. Otherwise, the code's too cryptic.
src/cmdstan/io/json/json_data.hpp
Outdated
offset *= dim_r[i]; | ||
} | ||
for (int i = 0; i < vec_c.size(); ++i) { | ||
vec_c[i] = std::complex<double>{static_cast<double>(val_r[i]), |
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.
Why does val_r[i]
need to be cast here? Isn't it already a double
value? And even if it's not a double value, calling the std::complex<double>
constructor should automatically cast it to double
because of the template specification.
const std::vector<size_t> &expected_dims) { | ||
EXPECT_EQ(true, (jdata.contains_r(name) || jdata.contains_i(name))); | ||
std::vector<size_t> dims = jdata.dims_r(name); | ||
if (dims.size() == 1) { |
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.
You don't need this first conditional. pop_back()
will be equivalent to clear()
if there is only a single element in the container. You generally don't want to branch for this kind of thing as branching is very inefficient when there's branch-point misprediction.
for (size_t i = 0; i < dims.size(); i++) | ||
EXPECT_EQ(expected_dims[i], dims[i]); | ||
std::vector<std::complex<double>> vals = jdata.vals_c(name); | ||
; |
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.
Remove the stray semicolon. It's a valid empty statement, but it's not doing anything.
@@ -144,6 +177,18 @@ TEST(ioJson, jsonData_real_array_1D) { | |||
test_real_var(jdata, txt, "foo", expected_vals, expected_dims); | |||
} | |||
|
|||
TEST(ioJson, jsonData_complex_array_1D) { |
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.
[question]
Is there a test that we get the right error message if the variable is not found?
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.
We test if the variable name exists in test_complex_var
EXPECT_EQ(true, (jdata.contains_r(name) || jdata.contains_i(name)));
…4.1 (tags/RELEASE_600/final)
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.
Looks good. Thanks for updating.
Submisison Checklist
./runCmdStanTests.py src/test
Summary:
Allow for intake of complex numbers in Stan. This PR mainly adds a new function called vals_c which allow for intake of complex numbers in a Stan program.
Documentation
All documentation for complex number is available at stan-dev/docs#378.
How to Verify:
Added more tests in /src/test/interface/io/json/json_data_test.cpp
Run tests:
./runCmdStanTests.py src/test
Side Effects:
None
Copyright and Licensing
Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company): Nicholas DiDio
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses: