fix(emulator): vectorize calcSpatialCov.matrix, removing O(n^3) redundant loop#3988
Closed
anshul23102 wants to merge 1 commit into
Closed
fix(emulator): vectorize calcSpatialCov.matrix, removing O(n^3) redundant loop#3988anshul23102 wants to merge 1 commit into
anshul23102 wants to merge 1 commit into
Conversation
The inner `for (j in seq_len(nl))` loop was entirely redundant: it
overwrote `H[i, ]` with the same vector on every iteration, doing
nl times more work than necessary. The outer loop over rows was also
unnecessary because R's matrix arithmetic applies the operation to
all elements at once.
Replace both loops with a single vectorized expression:
tau * exp(-psi * d)
This gives identical results in O(n^2) instead of O(n^3). Add tests
covering correctness, dimensions, symmetry, and agreement with the
already-vectorized calcSpatialCov.list implementation.
Fixes PecanProject#3964.
Member
|
Is this a duplicate of #3969? |
Contributor
Author
|
Yes, this is a duplicate of #3969 - I missed that PR when I opened this. Closing in favor of that one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3964.
calcSpatialCov.matrix()contained a nested loop that was doing far more work than needed:The inner
jloop was completely redundant: on every iteration it overwroteH[i, ]with the same value, repeating the same assignmentnltimes. The outeriloop was also unnecessary since R applies arithmetic operations to entire matrices at once.The fix replaces both loops with a single vectorized expression:
This produces identical output in O(n^2) instead of O(n^3). The already-vectorized
calcSpatialCov.listsibling method was the model for this approach.Regarding the second bug mentioned in #3964 (brittle unnamed
run.idindo.callinsensitivity.R): bothdo.callcalls at lines 309 and 470 already passrun.id = run.idas a named argument, so no change is needed there.Changes
modules/emulator/R/calcSpatialCov.matrix.R: replace nested loops with one vectorized expressionmodules/emulator/tests/testthat/test-calcSpatialCov.R: new test file covering correctness, output dimensions, diagonal values, symmetry, and agreement withcalcSpatialCov.listmodules/emulator/NEWS.md: changelog entryTest plan
test-calcSpatialCov.RpasscalcSpatialCov.matrix()matchescalcSpatialCov.list()for a single-component distance matrix