Skip to content

feat: add 1D array truncation, 2D matrix rendering, and numpy parameter support#257

Open
eng-ak wants to merge 1 commit into
connorferster:mainfrom
eng-ak:feature/array-truncation
Open

feat: add 1D array truncation, 2D matrix rendering, and numpy parameter support#257
eng-ak wants to merge 1 commit into
connorferster:mainfrom
eng-ak:feature/array-truncation

Conversation

@eng-ak

@eng-ak eng-ak commented May 5, 2026

Copy link
Copy Markdown

Summary

This PR adds support for rendering numpy arrays and matrices in handcalcs, along with a few bug fixes to handle edge cases when numpy types appear in parameter lines.

  • Added render_array for 1D lists/arrays with configurable truncation via array_truncate_threshold and array_truncate_end config options
  • Added render_matrix for 2D matrices using LaTeX matrix environments (matrix_environment config, defaults to bmatrix; also supports pmatrix, vmatrix, etc.)
  • 2D truncation can be controlled independently for rows and columns using a scalar or (row, col) tuple, with \vdots, \cdots, and \ddots at the truncation corner
  • Added set_truncation() as a public helper that accepts both int and tuple without the type restrictions of set_option
  • Column (nx1) and row (1xn) vectors are handled automatically

Bug fixes

  • Fixed convert_parameter to extract raw Python values before swap_symbolic_calcs, preventing crashes when numpy arrays appear as ParameterLine values
  • Fixed latex_repr to check for sympy objects before arrays, ensuring sympy.Matrix renders correctly rather than being passed to render_matrix

Test plan

  • Run the new test_array_matrix.py suite (112 tests covering all new functions, truncation edge cases, numpy, pint units, convert_parameter guard, and set_truncation API)
  • Confirm existing tests still pass with no regressions

…er support

New features:
- render_array: 1D list/array rendering with configurable truncation
  (array_truncate_threshold, array_truncate_end config options)
- render_matrix: 2D matrix rendering using LaTeX matrix environments
  (matrix_environment config, default bmatrix; supports pmatrix, vmatrix, etc.)
- 2D truncation: rows and columns independently controlled via scalar or
  (row, col) tuple -- uses \vdots, \cdots, \ddots at the truncation corner
- set_truncation(): public helper accepting both int and tuple without
  set_option type restrictions
- Vectors: column (nx1) and row (1xn) shapes handled automatically

Bug fixes:
- convert_parameter: extract raw Python values before swap_symbolic_calcs
  to prevent crashes when numpy arrays appear as ParameterLine values
- latex_repr: check for sympy objects before arrays so sympy.Matrix
  is rendered correctly instead of falling into render_matrix

Tests:
- test_array_matrix.py: 112-test suite covering all new functions,
  truncation edge cases, numpy, pint units, convert_parameter guard,
  and set_truncation API
@eng-ak

eng-ak commented May 11, 2026

Copy link
Copy Markdown
Author

Hi @connorferster,

I noticed that many users have requested support for NumPy arrays, so I wanted to share my fork of handcalcs with array support for 1D arrays and matrix support for 2D arrays. I also implemented truncation for arrays, which may be useful for large arrays when you only want to output part of the matrix as an example calculation. Please feel free to review this feature and let me know if you would like me to modify the code. please see the demo file below to get an idea of how it works

array_matrix_demo.ipynb

Best regards,

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.

1 participant