-
Notifications
You must be signed in to change notification settings - Fork 55
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
Fix dim problem with arma::field inputs #352
Conversation
So heres an Overview of the progress so far. Consider this Cpp Function:
Creating a simple 1d list of matrices works as expected:
You may wonder why I set It also works for 2d lists:
The question remains about how to deal with the 3D case. If we do not find a suitable solution we could fall back to the current implementation of flattening out the field (and maybe return a warning about that?). |
NB: I have NOT YET looked at your most recent change. Consider this semi-related Played a bit more with creating // [[Rcpp::export]]
Rcpp::List fieldtest1() {
field<cube> zap(1,2,1);
zap.print("zap");
field<mat> out(2, 2);
out.print("out");
field<vec> boo(2);
boo.print("boo");
return Rcpp::List::create(Rcpp::Named("fc") = zap,
Rcpp::Named("fm") = out,
Rcpp::Named("fv") = boo);
} The above works, but using, say, There are three tentative // To return arma::vec or arma::rowvec as R vector (i.e. dimensionless),
// one of the following macro can be defined before including
// RcppArmadillo.h. "ANYVEC" applys for both col- and row-vec.
//#define RCPP_ARMADILLO_RETURN_COLVEC_AS_VECTOR
//#define RCPP_ARMADILLO_RETURN_ROWVEC_AS_VECTOR
//#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR We could see if altering their value helps. |
Thanks again for your comments. I just pushed some additional changes and now it works. Previously I missed out on RcppArmadilloWrap.h but adding I wrote some lines of demo code here. |
That would be sweet! I was tied up somewhere else (there was for once a bug in the RC for (Rcpp)Armadillo, plus other "stuff") but this is excellent new. I think we can add ample unit tests and illustrations as this matures. |
I see that CI reports failing tests. This affects test_rcpparmadillo.R. At first glance, those are field/dim related and may need to be adjusted.
|
Yes, maybe comment the failing tests out 'for now' and add a big fat |
Regarding the user test, I think that's because the list matrix is now being considered as an array instead of a matrix under the https://github.com/RcppCore/RcppArmadillo/runs/4097501712?check_suite_focus=true#step:5:110 call| expect_equal(res[[4]][[2]], matrix(letters[1:4], ncol = 2, nrow = 2))
diff| Attributes: < Component "dim": Numeric: lengths (2, 3) differ >
diff| target is matrix, current is array
Error: 2 out of 259 tests failed
In R 4.0 higher, this object is considered to inherit both: Quickly checking through an example gives: A = matrix(1, ncol = 2, nrow = 3)
B = matrix(4, ncol = 5, nrow = 6)
listmatrix = matrix(list( ), 2, 2)
listmatrix[1, 1] = list(A)
listmatrix[1, 2] = list(B)
listmatrix[2, 1] = list(A)
listmatrix[2, 2] = list(B)
listmatrix
# [,1] [,2]
# [1,] numeric,6 numeric,30
# [2,] numeric,6 numeric,30
typeof(listmatrix)
[1] "list"
class(listmatrix)
[1] "matrix" "array" # Pass into C++
recovered_listmatrix = arma_field_exporter(listmatrix)
# Check list recovery
class(recovered_listmatrix)
[1] "array"
typeof(recovered_listmatrix)
[1] "list" I think when I penned #263, I had expected the |
Correct but shouldn't it have bitten us then for all that time too? (Maybe I am asleep, and tickling the bug needs our changes hence we only see it now...) In any event I think there is reasonably little use of (Quick check) Ok, proving myself wrong with one query. GH mirror of CRAN has 100+ hits for |
@BerriJ : Ok, we are all caught up at CRAN and with Armadillo upstream. Can you please rebase to |
I did. I also squashed my commits since the first 2 were just fiddling around with it. Now it's one commit with all of my changes.
Maybe we can do a reverse dependency check and look how bad it is? I suspect that most people won't be affected by this change even if they use arma::fields. I think about the following:
|
Yup. I have the machinery for it, and fresh baselines from the three tests I ran for RcppArmadillo 0.10.7.3.0 its predecessor release candidates. Stay tuned! |
So the test run and the result has been comitted as usual with the summary in the table below. That is a lot of shrapnel. We cannot merge the PR as is. I have been thinking about this while it was running and the results were accumulating towards this outcome: could you possibly rejig the fix in the PR such that it is opt-in? In other words set up a That way you get to opt into new and corrected
|
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.
Nice work also to re-enable the tests/
Thank you. So I simply defined 2 variables: However, I could not test it. I tried simply running those functions: test.cpp:
after executing devtools::load_all(). Probably my setup does not catch that I hope I got it. Ci looks good at least 👍 . Also, it would be very interesting to make 2 additional reverse dep checks to see which change is more problematic. Thanks so far, Dirk. |
We should be able to add something. One approach is simply ... another cpp source file with the New reverse depends run started and now past the first former failure so fingers crossed ... |
Odd to see |
Absolutely not. The rev.dep box is a plain standard Linux machine with an Intel CPU. Always has been. What happens in other run on other machines under other OSs is, well, something else. It is a regular error likely caused by the
|
Last rev.dep was of course again very clean, see its commit So I think we're ready, modulo maybe reflecting on the previous comment. Do you want to fold that in? |
I would propose to merge as is. The main functionality of exporting and importing fields works fine and always returning fields with 3 dimensions is something most people can easily live with I think. One could think about adding an additional flag for dropping dimensions (something like drop=TRUE in R). But I would propose to postpone this for now since I'm quite busy. |
Hi @BerriJ sorry for this taking a moment but we may as well get it right. I just committed (to master) a file |
@eddelbuettel agreed. The proposed change is still a bit problematic. There shouldn't be a reason for why |
Did you maybe miss executing a git push? I can't find the commit you refer to. Looked here. |
I did indeed, sorry for that and thanks for the heads-up. Sometimes I think the 'sequence' is muscle memory in |
I'm pretty shure I got cIRT::direct_sum() working now. The problem was that it crashed at If dims exist it will set the field size according to dims, else it will just make a 1D field with length of the list (basically the old behavior). I still have to look into your file @eddelbuettel . |
I just added the two > str(a3)
List of 12
$ : num[0 , 0 ]
$ : num[0 , 0 ]
$ : num [1:2, 1:3] -1.024 1.534 -0.894 -0.49 0.597 ...
$ : num[0 , 0 ]
$ : num[0 , 0 ]
$ : num [1:2, 1:4] 0.0648 -0.2241 1.5294 -0.4769 0.3206 ...
$ : num[0 , 0 ]
$ : num [1:3, 1:2] 1.4157 1.6768 -0.0524 -0.1988 -1.3985 ...
$ : num[0 , 0 ]
$ : num[0 , 0 ]
$ : num[0 , 0 ]
$ : num[0 , 0 ]
- attr(*, "dim")= int [1:3] 2 3 2
>
> class(a3)
[1] "array"
> dim(a3)
[1] 2 3 2
> The indexing is still ... awkward but I am also not that familiar with arrays where we have more than two dimensions. |
Sorry for adding another commit. I realized that I had left So as far as I see we are done now? Do you want to run the rev dep checks another time? |
Yes I think we're there, or very close. The #ifdef protection should mean to harm over at the CRAN side and those who want it (you ;-) ) can opt into it now. We can then slowly and patiently email those with failing packages and suggest they turn the |
Oh, the one thing I forgot: as I made one change to |
This the dims of arma::field when specified as input or output. This implementation works with 1d, 2d and 3d fields.
This also fixes a problem occuring with cIRT::direct_sum()
I did, no problem. :-) |
Thanks for your patience and diligence on this! |
Attempts to fix #351
This correctly passes the dims of nested lists when arma::field is specified as input (so field from R to Cpp).
This implementation works with 1d and 2d fields. The generalization to 3d seems very straightforward. However, just adding a third loop and extending the dims results in an error at runtime: "product [x] do not match the length of object [x]".
This problem with 3d arrays most probably also affects "FieldImporter" (so field from C++ to R) since the following function fails with the same error: