Skip to content
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

i.cca: use 0 based array indexing #3239

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

marisn
Copy link
Contributor

@marisn marisn commented Nov 15, 2023

When ccmath library was introduced in 49b8066 most of arrays were moved to 0 based indexing but some locations got missed. Most likely it is the source of a crash reported a few years a go: https://trac.osgeo.org/grass/ticket/2297

With this fix I'm able to run the code without a segfault, but I haven't figured out how to get any output and thus no tests are provided.

When ccmath library was introduced in 49b8066
most of arrays were moved to 0 based indexing but some locations
got missed. Most likely it is the source of a crash reported a few years
a go: https://trac.osgeo.org/grass/ticket/2297
@marisn marisn added the C Related code is in C label Nov 15, 2023
@neteler neteler added the bug Something isn't working label Nov 15, 2023
@neteler neteler added this to the 8.4.0 milestone Nov 15, 2023
@neteler neteler added the backport to 8.3 PR needs to be backported to release branch 8.3 label Nov 15, 2023
@marisn marisn marked this pull request as draft November 16, 2023 18:37
@marisn
Copy link
Contributor Author

marisn commented Nov 16, 2023

Although this PR fixes segfault, the code does not produce correct output (in comparison to pre- 49b8066 version). Unless the testcase passes, this PR does not improve on the current state.

cov[i][j][k] = cov[i][k][j] = sigs.sig[i - 1].var[j - 1][k - 1];
for (j = 0; j < bands; j++) {
mu[i][j] = sigs.sig[i].mean[j];
for (k = 0; k < j; k++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try

Suggested change
for (k = 0; k < j; k++) {
for (k = 0; k <= j; k++) {

because j is not the upper bound of k to be excluded but to be included, also with zero-based indexing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting my mistake!

Still even with this being fixed, output is not identical to the old version. Should it be identical? Suggestions how to test?

Copy link
Contributor

@metzm metzm Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions in stats.c assume zero-based indices, thus I would expect different results in the sense that results with your PR (zero based indices) are correct whereas previous results (one based indices) were not correct, particularly because the indexing to cov[][][] did not change.

You would need some independent, trusted alternative for Canonical components analysis in order to decide which results are correct. Regarding the code, your PR seems like a bugfix, previous results are probably not correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarification, cov has been indexed previously in main.c as cov[i][j][k] which was wrong for one-based i, j, k because zero-based i, j, k are used in stats.c. With your PR, cov is consistently indexed with zero-based i, j, k, that could explain the differences.

@wenzeslaus wenzeslaus added the errata Include in errata (fixes wrongly computed results) label Jan 5, 2024
@wenzeslaus wenzeslaus modified the milestones: 8.4.0, Future Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport to 8.3 PR needs to be backported to release branch 8.3 bug Something isn't working C Related code is in C errata Include in errata (fixes wrongly computed results)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants