Skip to content

Add cap-to-weight order contract#95

Merged
Arcadia-1 merged 1 commit into
Arcadia-1:mainfrom
chenzc24:codex/cdacwgt-order-contract
Jul 8, 2026
Merged

Add cap-to-weight order contract#95
Arcadia-1 merged 1 commit into
Arcadia-1:mainfrom
chenzc24:codex/cdacwgt-order-contract

Conversation

@chenzc24

@chenzc24 chenzc24 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR makes the cap-to-weight ordering contract explicit without changing the existing Python default.

convert_cap_to_weight historically follows the MATLAB legacy cap2weight convention:

input:  [LSB ... MSB]
output: [LSB ... MSB]

MATLAB's newer cdacwgt interface uses the opposite convention:

input:  [MSB ... LSB]
output: [MSB ... LSB]

This PR adds explicit order controls:

convert_cap_to_weight(..., input_order=lsb_to_msb, output_order=None)
  • input_order=lsb_to_msb keeps the existing Python / cap2weight behavior.
  • input_order=msb_to_lsb accepts MATLAB cdacwgt-ordered inputs.
  • output_order=None defaults to the same order as input_order.
  • output_order=lsb_to_msb | msb_to_lsb can be selected independently.

Why

The recursive capacitance calculation is already the same algorithm. The mismatch is an API contract issue: callers can easily pass a MATLAB cdacwgt array into Python and silently get weights in the wrong bit order. Making the order explicit removes that ambiguity while preserving backward compatibility.

Validation

Current MATLAB cdacwgt reference for the segmented bridge case:

cd = [4 2 1 4 2 1];
cb = [0 4 0 8/7 0 0];
cp = [0 0 0 0 0 1];
[w, ct] = cdacwgt(cd, cb, cp);

Current MATLAB returns:

w  = [0.6666666666666666 0.1666666666666667 0.08333333333333333 0.04166666666666666 0.02083333333333333 0.01041666666666667]
ct = 6

Python with input_order=msb_to_lsb, output_order=msb_to_lsb now returns the same values.

Default behavior remains unchanged:

convert_cap_to_weight([1, 2, 4, 8], ...)
weights = [1, 2, 4, 8] / 15
c_total = 15

Tests

uv run --with pytest pytest tests/unit/test_cap2weight.py -q
uv run --with pytest pytest tests/integration/test_user_guide_skill_examples.py -q

I intentionally did not update bundled skill docs in this PR to avoid overlapping with the currently open documentation PRs. A follow-up docs-only PR can add the new order options after those land.

@Arcadia-1
Arcadia-1 merged commit 28b4a67 into Arcadia-1:main Jul 8, 2026
1 check passed
@chenzc24
chenzc24 deleted the codex/cdacwgt-order-contract branch July 9, 2026 06:07
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