Repository navigation
Conversation
The function only swapped the sub- and super-diagonal columns. In the (sub, diag, super) row layout A.T[i,i-1] = A[i-1,i] and A.T[i,i+1] = A[i+1,i], so the diagonals must also shift by one row. The result was wrong whenever the matrix is not symmetric. Add a test comparing against the dense transpose.
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.
transpose_tridiagonal_matrixinfinancepy/utils/math.pytransposes a tridiagonal matrix stored as three columns (rowiholdsA[i,i-1],A[i,i],A[i,i+1]). It swapped the sub- and super-diagonal columns but did not shift them by one row, which a transpose needs (A.T[i,i-1] = A[i-1,i]andA.T[i,i+1] = A[i+1,i]). The result is wrong for any matrix that is not symmetric.Real result for the 4x4 matrix with rows
[0,2,3],[4,5,6],[7,8,9],[10,11,0], expanded to a dense matrix, onmasterand with this change:The fix keeps the diagonal and fills the sub-diagonal from the previous row's super-diagonal, and the super-diagonal from the next row's sub-diagonal.
Test:
test_transpose_tridiagonal_matrixintests/unit/test_model_finite_difference.pycompares the result with the transpose of the dense matrix; it fails onmasterand passes now. The only caller I found isfd_roll_forwardsinfinancepy/models/finite_difference.py(and I did not find anything callingfd_roll_forwards), so if that function is considered dead code this is a fix to a public helper only. I did not run the whole unit suite myself: the worker that prepared this saw two unrelated failures intest_inflation_swap.pythat fail the same way on untouched upstream, and I did not repeat that.math.pyuses CRLF line endings and I kept them.