Skip to content

Fix RecombineMatrix for asymmetric boundary conditions#116

Merged
jipolanco merged 1 commit into
jipolanco:masterfrom
fintzij:fix/asymmetric-bc-recombine-matrix
Feb 16, 2026
Merged

Fix RecombineMatrix for asymmetric boundary conditions#116
jipolanco merged 1 commit into
jipolanco:masterfrom
fintzij:fix/asymmetric-bc-recombine-matrix

Conversation

@fintzij

@fintzij fintzij commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Two bugs in RecombineMatrix (src/Recombinations/matrices.jl) that manifest when the left and right boundary conditions differ (cₗ ≠ cᵣ). Both are one-line fixes; symmetric BCs (cₗ = cᵣ) are unaffected.

Bug 1: getindex — wrong row offset for right-boundary block

# Before (line 446):
ii = i - h - cr    # BUG: uses right constraint count

# After:
ii = i - h - cl    # FIX: uses left constraint count

The right-block row index into the lower-right submatrix Lᵣ should be i - h - cₗ (matching what 3-arg mul! computes), not i - h - cᵣ. When cₗ ≠ cᵣ, getindex returns incorrect values, which corrupts:

  • Matrix{T}(R) (dense conversion)
  • replace_in_print_matrix (display)
  • evaluate and evaluate_all in bases.jl (basis function evaluation)

Derivation: The first row of Lᵣ corresponds to parent B-spline row N - nᵣ - cᵣ + 1. Substituting N = M + cₗ + cᵣ and h = M - nᵣ:

ii = i - (N - nᵣ - cᵣ + 1) + 1
   = i - (M + cₗ + cᵣ) + nᵣ + cᵣ
   = i - (M - nᵣ) - cₗ
   = i - h - cₗ       ← correct

Bug 2: 5-arg mul! — wrong SVector size for right-boundary block

# Before (line 501):
y[js] = α * lr * view(x, (h+1):M) + β * SVector{n1}(view(y, js))
#                                             n1 = nl + cl (LEFT corner size)

# After:
n2 = nr + cr
y[js] = α * lr * view(x, (h+1):M) + β * SVector{n2}(view(y, js))
#                                             n2 = nr + cr (RIGHT corner size)

The right-boundary range js has length nr + cr, but the code wrapped it in SVector{n1} where n1 = nl + cl. When nl + cl ≠ nr + cr, this throws DimensionMismatch.

Why existing tests don't catch this

test_recombine_matrix explicitly asserts bcleft === bcright and nl == nr && cl == cr. The only asymmetric test ("Hybrid BCs") tests spline evaluation but never tests getindex, mul!, or matrix conversion.

Tests added

New "Asymmetric BC RecombineMatrix" test set covering 6 asymmetric BC combinations × 2 spline orders (4, 6). For each case:

  • Matrix{Float64}(R) matches ground-truth from 3-arg mul!
  • Every R[i,j] entry is verified individually
  • 5-arg mul!(y, R, x, α, β) runs without error and produces correct results
Minimal working example (click to expand)
using BSplineKit, LinearAlgebra

# Cubic B-spline basis with 8 functions
B = BSplineBasis(BSplineOrder(4), 0.0:0.2:1.0)

# Asymmetric BCs: Free left (cₗ=0), Neumann right (cᵣ=1)
R = RecombineMatrix(B, (), (Derivative(1),))  # size 8×7

x = ones(7)

# 3-arg mul! is correct:
y_mul = zeros(8); mul!(y_mul, R, x)

# Matrix conversion uses getindex — had the bug:
R_dense = Matrix{Float64}(R)
y_idx = R_dense * x

println("Match? ", y_mul  y_idx)
# Before fix: false (R[7,7] returns 0 instead of 1)
# After fix:  true

# 5-arg mul! — had DimensionMismatch for nl+cl ≠ nr+cr:
y5 = ones(8)
mul!(y5, R, x, 2.0, 3.0)  # Before fix: ERROR; After fix: works

@codecov

codecov Bot commented Feb 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.18%. Comparing base (401af69) to head (4676d9b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #116   +/-   ##
=======================================
  Coverage   95.17%   95.18%           
=======================================
  Files          36       36           
  Lines        2344     2346    +2     
=======================================
+ Hits         2231     2233    +2     
  Misses        113      113           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Fix two bugs in RecombineMatrix (src/Recombinations/matrices.jl) that
manifest when the left and right boundary conditions differ (cl != cr).

Bug 1: getindex - wrong row offset for right-boundary block
  The right-block row index was computed as ii = i - h - cr, but the
  correct offset uses the left constraint count: ii = i - h - cl.
  This caused Matrix(R) and any R[i,j] call to return wrong values,
  while 3-arg mul!(y, R, x) remained correct. Also affected downstream
  methods: replace_in_print_matrix, evaluate, and evaluate_all.

Bug 2: 5-arg mul! - wrong SVector size for right-boundary block
  The right-boundary block used SVector{n1} where n1 = nl + cl (the
  LEFT corner size), but the right corner has nr + cr elements.
  When nl + cl != nr + cr, this threw DimensionMismatch.
  Fix: use n2 = nr + cr for the right-boundary SVector.

Both are one-line fixes. Symmetric BCs (cl = cr) are unaffected.

Add tests covering asymmetric BC cases for getindex, Matrix conversion,
and 5-arg mul! across multiple spline orders and BC combinations.
@fintzij fintzij force-pushed the fix/asymmetric-bc-recombine-matrix branch from 5a73ba4 to 4676d9b Compare February 14, 2026 02:07
@jipolanco

Copy link
Copy Markdown
Owner

This looks great! Thanks for catching the issue and fixing it.

@jipolanco jipolanco merged commit f413767 into jipolanco:master Feb 16, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants