Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/emulator/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* Added keywords and bug reporting URL to DESCRIPTION. No other files changed.

## Fixed

* `calcSpatialCov.matrix()`: removed a redundant nested loop that recomputed each matrix row `n` times unnecessarily, reducing complexity from O(n^3) to O(n^2) (#3964).



# PEcAn.emulator 1.8.1
Expand Down
10 changes: 1 addition & 9 deletions modules/emulator/R/calcSpatialCov.matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,5 @@
##' @author Michael Dietze
##' @export
calcSpatialCov.matrix <- function(d, psi, tau, ...) {
nl <- nrow(d)
H <- matrix(0, nl, nl)
for (i in seq_len(nl)) {
# for(j in 1:nl){ H[i,j] <- tau*exp(-psi*d[i,j]) }
for (j in seq_len(nl)) {
H[i, ] <- tau * exp(-psi * d[i, ])
}
}
return(H)
tau * exp(-psi * d)
}
44 changes: 44 additions & 0 deletions modules/emulator/tests/testthat/test-calcSpatialCov.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
test_that("calcSpatialCov.matrix returns correct exponential covariance", {
d <- matrix(c(0, 1, 2,
1, 0, 1,
2, 1, 0), nrow = 3)
psi <- 0.5
tau <- 2.0

result <- calcSpatialCov.matrix(d, psi = psi, tau = tau)

expected <- tau * exp(-psi * d)
expect_equal(result, expected)
})

test_that("calcSpatialCov.matrix returns a matrix of the same dimensions as d", {
d <- matrix(runif(25), nrow = 5)
result <- calcSpatialCov.matrix(d, psi = 1, tau = 1)
expect_equal(dim(result), dim(d))
})

test_that("calcSpatialCov.matrix diagonal is tau when d has zeros on diagonal", {
n <- 4
d <- matrix(1, n, n)
diag(d) <- 0
tau <- 3.0
result <- calcSpatialCov.matrix(d, psi = 1, tau = tau)
expect_equal(diag(result), rep(tau, n))
})

test_that("calcSpatialCov.matrix is symmetric when d is symmetric", {
d <- as.matrix(dist(1:5))
result <- calcSpatialCov.matrix(d, psi = 0.3, tau = 1.5)
expect_equal(result, t(result))
})

test_that("calcSpatialCov.list and calcSpatialCov.matrix agree on single-component case", {
d <- as.matrix(dist(1:4))
psi <- 0.4
tau <- 2.0

result_matrix <- calcSpatialCov.matrix(d, psi = psi, tau = tau)
result_list <- calcSpatialCov.list(list(d), psi = psi, tau = tau)

expect_equal(result_matrix, result_list)
})
Loading