fix(trade): keep dimnames on the all-zero .balance_matrix() path - #765
Open
lbm364dl wants to merge 1 commit into
Open
fix(trade): keep dimnames on the all-zero .balance_matrix() path#765lbm364dl wants to merge 1 commit into
lbm364dl wants to merge 1 commit into
Conversation
.balance_matrix() short-circuits when balanced exports and imports are all zero, and returned matrix(0, nrow = n, ncol = n) with no dimnames, while the normal return path copies dimnames(trade_matrix). The documented contract for the bilateral trade matrices is name indexing (m["A", "B"]), so the zero path quietly broke it; it was benign only because both consumers short-circuit on zero content before indexing. Set dimnames on the zero path the same way the normal path does, and pin the contract with a test. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
What was wrong
.balance_matrix()(R/bilateral_trade.R) short-circuits when the balancedexport and import targets are all zero:
That return carries no dimnames, while the normal return path builds its result
with
dimnames = dimnames(trade_matrix). The documented contract for thesematrices (
R/bilateral_trade.R:26) is name indexing — "the value atm["A", "B"]is the trade in tonnes" — so the zero path quietly violated it.It is benign today only because both consumers short-circuit on zero content
before indexing, which is exactly the accident worth pinning.
Classification: mechanical — a contract/crash fix with no defensible
alternative and no numeric effect (the returned values are all zero either
way).
What changed
dimnames = dimnames(trade_matrix), the same way thenormal path does.
tests/testthat/test_bilateral_trade.R:.balance_matrix keeps dimnames when all trade is zero— feeds an all-zerogroup with named dimensions through and asserts both
dimnames(result)andresult["1", "2"].The pre-existing
.balance_matrix returns zero matrix when all trade is zerotest passes an unnamed input matrix, so it keeps passing unchanged.
Verification
Failing before the fix (source change stashed, new test present):
Passing after the fix:
Also ran
air format .(no further changes to other files) andlintr::lint()on both changed files — no lints. No roxygen changed, so no
document()runwas needed; no
NEWS.mdentry, since no published value changes.Closes #235.
🤖 Generated with Claude Code