Skip to content

engine: RANK ranks the whole array, not each row as VECTOR RANK does #1026

Open

Description

@bpowers

Simlin's RANK is Vensim's VECTOR RANK under the XMILE name, and it does not match Vensim on multi-dimensional arrays or on direction values other than 1.

Example

test/test-models/tests/vector_order/ has real Vensim DSS 7.3.4 output.
revenue RANK2A[company,Region] = VECTOR RANK(revenue2A[company,Region], 1) over a 5 x 3 array gives values 1..3: each company row is ranked over its three Region values (the last dimension).
Simlin ranks all 15 elements together, giving 1..15.
For [[31, 11, 21], [32, 12, 22]], Vensim's rule gives [3, 1, 2, 3, 1, 2]; Simlin gives [5, 1, 3, 6, 2, 4].
Nothing tests this: that model does not import (it reads a spreadsheet range), and the RANK tests are all 1-D.

Direction: vensim.com fn_vector_rank says any direction > 0 ranks ascending; Simlin ranks ascending only when the rounded direction equals 1.
Checked against the documentation only, not a Vensim run.

Separately, XMILE 1.0 section 3.7.1.3 defines RANK(A, n[, B]) as something else: the scalar index of the n-th smallest element (Stella's help agrees).
An XMILE model using spec RANK compiles in Simlin and computes an array of ranks instead.

Why it matters

No model in test/ uses either function outside test fixtures, so this is low priority, but the per-row and direction rules are cheap to match against known ground truth.

Fix

  1. Rank within each row of the last dimension, in both the VM and wasm backends; vm_vector_sort_order.rs already implements that rule for VECTOR SORT ORDER from the same Vensim file.
  2. Treat direction > 0 as ascending.
  3. Document in the engine CLAUDE.md that RANK means Vensim's VECTOR RANK, not the XMILE function.

Alternative for the naming: bind Vensim's function to the name VECTOR RANK and either implement spec RANK or reject it with a clear error.
That touches the MDL reader and writer and the LTM reducer tables.

Also unsupported: the spec's two-argument MAX(A, 0) / MIN(A, 0) reducing a whole array; MAX(MAX(A), 0) works.

Pointers

  • src/simlin-engine/src/vm.rs: Opcode::Rank; src/simlin-engine/src/wasmgen/vector.rs: emit_rank
  • src/simlin-engine/src/vm_vector_sort_order.rs: the per-row rule to copy
  • src/simlin-engine/src/mdl/xmile_compat.rs: "vector rank" => "RANK"

Activity

  1. added
    compat
    engine
    on Aug 25, 2026
  2. bpowers commented on Aug 26, 2026

    @bpowers
    OwnerAuthor

    Addendum to section 1: VECTOR RANK ranks within the innermost row; Opcode::Rank ranks the whole view

    Found while reviewing the Phase 1 signature-table tests (builtin_signature_tests::rank_accepts_the_cross_dimension_operand_vector_sort_order_accepts, branch compiler-unification). Beyond binding Vensim semantics to the XMILE name, Simlin's RANK does not match Vensim's VECTOR RANK on a multi-row operand.

    Evidence: test/test-models/tests/vector_order/output.tab

    Vensim DSS for Windows 7.3.4 output, per the directory's README. test_vector_order.mdl beside it declares Region: A, B, C, company: (company1-company5) -> scompany, product: X, Y, and eight VECTOR RANK equations. The ones with a multi-row operand, read from the Time = 0 row of output.tab:

    • revenue RANK2A[company,Region] = VECTOR RANK(revenue2A[company,Region], 1), operand 5 x 3. Columns revenue RANK2A[company1,A], [company1,B], [company1,C] read 1, 2, 3; [company2,A..C] read 2, 1, 3; [company3,A..C] read 3, 2, 1; [company4,A..C] read 2, 3, 1; [company5,A..C] read 2, 3, 1. Every value is in 1..3: each company row is ranked over its three Region values. A whole-view rank would range over 1..15.
    • revenue RANK2B[Region,scompany] = VECTOR RANK(revenue2B[Region,company], 1), operand 3 x 5. [A,sort1..sort5] read 1, 4, 3, 2, 5; [B,sort1..sort5] read 3, 2, 1, 5, 4; [C,sort1..sort5] read 5, 4, 1, 2, 3. Each Region row is a permutation of 1..5.
    • revenue RANK3[Region,product,scompany] = VECTOR RANK(revenue3[Region,product,company], 1), operand 3 x 2 x 5. [A,X,sort1..sort5] read 1, 4, 3, 2, 5 and [A,Y,sort1..sort5] the same (the model loads the same spreadsheet column into the X and Y rows of revenue3); [B,X,sort1..sort5] read 3, 2, 1, 5, 4; [C,X,sort1..sort5] read 5, 4, 1, 2, 3. Each (Region, product) row is a permutation of 1..5.
    • The direction-0 variants have the same per-row shape reversed, e.g. revenue RANK neg2A[company1,A..C] read 3, 2, 1.
    • The 1-D revenue RANK[company] = VECTOR RANK(revenue[company], 1) reads 1, 4, 5, 2, 3: one row, where per-row and whole-view ranking coincide.

    RANK2A is the discriminating case. Its left-hand side carries the operand's own subscripts ([company,Region]), so no axis is singled out by a differing subscript, and Vensim still ranks along the last-declared dimension (Region). The rule these eight equations agree on: the innermost (last-declared) dimension of the operand is the ranked axis, and every assignment of the outer dimensions is an independent row. That is the rule src/simlin-engine/src/vm_vector_sort_order.rs already implements for VECTOR SORT ORDER from the same file (SORT ORDER2A[sort1..sort3,A] and friends are per-row 0-based permutations), with the rustdoc there citing the fixture for the 0-based convention.

    What Simlin does

    Opcode::Rank (src/simlin-engine/src/vm.rs, the Opcode::Rank { write_temp_id } arm) collects every element of the view into one list, sorts it once, and writes rank + 1 at each element's original position: whole-view ranking. src/simlin-engine/src/wasmgen/vector.rs (emit_rank) mirrors it. On the RANK2A operand that ranks all 15 elements together, so Simlin's revenue RANK2A would range over 1..15 where Vensim's ranges over 1..3 per row. This is reasoned from the opcode, not from a run: vector_order is not in the tests/integration/simulate.rs corpus, so nothing exercises it today, and the existing array_tests::rank_tests rows are all 1-D, where the two rules agree.

    The signature-table test pins the current whole-view value for its 2 x 3 operand [[31, 11, 21], [32, 12, 22]] as [5, 1, 3, 6, 2, 4] and names this issue; the Vensim per-row result would be [3, 1, 2, 3, 1, 2]. That test is about admission parity between RANK and VECTOR SORT ORDER (what the signature table decides), not about the value.

    Consequence for the resolution

    Whichever way section 1 is resolved (rename to VECTOR RANK, or keep RANK Vensim-flavoured), the Vensim-semantics implementation also needs the per-innermost-row rule in both backends, with vector_sort_order as the template: iterate the view in row-major order, rank within each block of inner = |last dim| elements, write rank + 1 at the in-row position. Adding vector_order to the integration corpus would pin it against the fixture.

  3. changed the title [-]engine: RANK, two-arg MAX/MIN, and n-ary MEAN diverge from XMILE v1.0 section 3.7.1.3 without documentation[/-] [+]engine: RANK ranks the whole array, not each row as VECTOR RANK does[/+] on Oct 12, 2026
  4. added
    bug
    on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompatengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions