Skip to content

Matrix inputs: preserve c(...) and r(...) model-answer types - #1847

Merged
sangwinc merged 8 commits into
maths:iss1848from
adamant-pwn:codex/vector-grid-model-answer
Aug 25, 2026
Merged

Matrix inputs: preserve c(...) and r(...) model-answer types#1847
sangwinc merged 8 commits into
maths:iss1848from
adamant-pwn:codex/vector-grid-model-answer

Conversation

@adamant-pwn

@adamant-pwn adamant-pwn commented Aug 22, 2026

Copy link
Copy Markdown

Summary

STACK's fixed and variable-size matrix inputs currently reconstruct every response as matrix(...). A column vector, a row vector, and a one-column or one-row matrix therefore lose the distinction already represented by STACK's c(...), r(...), and matrix(...) forms.

This extends the c(...)/r(...) input convention discussed in #109 to both structured matrix inputs.

This patch makes the instantiated model answer the single source of truth:

  • a matrix(...) model answer keeps the existing input and response unchanged;
  • a c(...) or r(...) model answer makes either input reconstruct the same vector form;
  • the fixed grid infers the corresponding N-by-1 or 1-by-N shape;
  • the variable-size input keeps its existing variable length while preserving the model-answer type;
  • no new input option or author-supplied dimension is required; and
  • rendered HTML exposes data-stack-input-value-type="c" or "r", while the API reports casValueType: matrix|c|r for both input types.

This deliberately does not infer semantics from dimensions: matrix([1],[2]) remains a matrix, while c(1,2) remains a column vector.

Authoring example

stack_linear_algebra_declare(true);
v: c(1, 2, 3);

Choose either matrix input and use v as its model answer. The fixed grid creates one box per component; the variable-size input keeps its existing free-size textarea. Both return c(...). Use vec_convert(ans1) in a PRT when an answer test or calculation requires a matrix.

The same pattern works for a row vector whose model answer is r(...).

Why retain the structured inputs?

An algebraic input already accepts c(1,-2,1), but it requires students to type the function name, commas and balanced parentheses in one field. The fixed grid exposes the expected dimension and gives each component its own box; the variable-size input keeps the lighter whitespace notation without fixing the dimension. This patch preserves both interactions without discarding the CAS type.

Compatibility

Existing matrix(...) model answers render and validate as before. The patch adds no question setting, language string or migration step. Questions opt into the behavior by using STACK's existing c(...) or r(...) value as the model answer.

The HTML inputs continue to use the existing question-level matrixparens option. stack_linear_algebra_declare(true) now sends c(...) and r(...) through stack_matrix_disp(...), so their TeX output respects the same option instead of hardcoding bmatrix. #1848 remains open for the separate question of independent matrix and vector settings.

Verification

  • Added focused coverage for fixed and variable-size C/R serialisation, response reconstruction, HTML/API metadata, validation, nested entries and forbidden generated constructors.
  • php -l passes for both changed PHP classes and their focused test files in the current STACK API image.
  • End-to-end tests in an isolated current STACK API container passed for both input types and both vector forms. The inputs reconstructed c(...) and r(...), preserved them in validation and API metadata, and formatted column- and row-vector sample solutions vertically and horizontally respectively.
  • Direct Maxima tests passed for [, (, {, | and no delimiters. An integrated API test confirmed that fixed and variable-size C/R inputs use the same round HTML and TeX delimiters when matrixparens is (.
  • The same tests passed with c/r listed in forbidWords: the generated top-level constructor is allowed, while the original rules still apply to each student-entered cell.

@adamant-pwn
adamant-pwn marked this pull request as ready for review August 22, 2026 18:50
@sangwinc
sangwinc changed the base branch from master to dev August 24, 2026 11:30
@sangwinc

Copy link
Copy Markdown
Member

Thanks @adamant-pwn that's a very helpful suggestion, and not one I'd thought of myself.

  1. I've changed the base to "dev" as we typically stage new features for the next release there. (See https://docs.stack-assessment.org/en/Developer/Releasing/#0-code-on-github )
  2. I think this is a real improvement, thank you, and it only breaks back-compatibility where a teacher has been somewhat inconsistent!
  3. Thanks for remembering the API.

I always thought of r(...) and c(...) as "sugar" for special matrix geometry, and therefore don't see the need to do this. I think, however, your motivation is captured in #1848 where you want different matrix parentheses. In this context, what you're asking for here is very sensible. We'd need the matrix inputs validation display to match the bracket types of the structure.

@LukeLongworth do you have any thoughts or comments, I know you've made a lot of contributions to the linear algebra libraries in STACK.

@adamant-pwn thank you so much for adding in test cases, that's super helpful and very welcome indeed. I think you'll also need the same for the matrix of variable size? https://github.com/maths/moodle-qtype_stack/blob/master/stack/input/varmatrix/varmatrix.class.php

Comment thread api/README.md Outdated
- `syntaxHintType`: If the Syntax hint should be displayed as a placeholder, or as initial value. Supported for the types `algebraic, numerical, string, units`
- `options`: Key-Value Object containing the options for choice like input types. Supportet for the types `checkbox, dropdown, radio`
- `matrixbrackets`: The desired matrix bracket style. One of `matrixroundbrackets`, `matrixsquarebrackets`, `matrixbarbrackets`, `matrixnobrackets`. Supported for the types `matrix` and `varmatrix`
- `casValueType`: The Maxima constructor returned by a fixed matrix grid: `matrix`, `c`, or `r`. Supported for the `matrix` type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EJMFarrow could you review the proposed modifications to the API please?

@adamant-pwn
adamant-pwn force-pushed the codex/vector-grid-model-answer branch from 59ab850 to 3372043 Compare August 24, 2026 12:42
@adamant-pwn adamant-pwn changed the title Matrix input: preserve c(...) and r(...) model-answer types Matrix inputs: preserve c(...) and r(...) model-answer types Aug 24, 2026
@adamant-pwn

Copy link
Copy Markdown
Author

Thanks! I've added the same handling for the variable-size matrix input and rebased onto dev. Both inputs now use the instantiated model-answer type rather than inferring it from the dimensions.

I also included the small parenthesis fix discussed in #1848: c and r now use stack_matrix_disp(), so validation and the HTML input follow the same existing matrixparens setting. #1848 can stay focused on whether matrices and vectors need independent settings. I'll wait for Edmund and Luke's comments before changing that API further.

@EJMFarrow

Copy link
Copy Markdown
Collaborator

API changes are minimal and look good, @sangwinc . Thanks, @adamant-pwn ! (Almost everyone will be using the pre-rendered inputs but great to have the info there for anyone who needs it.) CSS changes have also been added to the mobile CSS - lovely.

@adamant-pwn
adamant-pwn force-pushed the codex/vector-grid-model-answer branch from e1036a6 to 31d9cf0 Compare August 24, 2026 14:04
@LukeLongworth

Copy link
Copy Markdown
Collaborator

Hi! Thanks for this input, I think it's great. I'll be very transparent and say that this was always a rather hacked-together solution. My programming experience is limited and this worked for my particular use-case so I didn't look further! Integrating these functions more naturally into the broader STACK ecosystem sounds great.

@sangwinc

Copy link
Copy Markdown
Member

Thanks @LukeLongworth if you have any capacity to test/comment on these developments then I'd very much appreciate your continued input.

@sangwinc
sangwinc changed the base branch from dev to iss1848 August 25, 2026 09:43
@sangwinc
sangwinc merged commit 82c369f into maths:iss1848 Aug 25, 2026
adamant-pwn added a commit to adamant-pwn/moodle-qtype_stack that referenced this pull request Aug 25, 2026
The iss1848 integration branch now includes PR maths#1847, which overlaps the matrix and variable-matrix renderers, API documentation, and tests changed here. Resolve those overlaps by retaining c()/r() CAS value-type support together with the curly-bracket renderer and its API value. This keeps PR maths#1849 conflict-free without rewriting its published history.
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.

4 participants