Repository navigation
Description
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
- Rank within each row of the last dimension, in both the VM and wasm backends;
vm_vector_sort_order.rsalready implements that rule forVECTOR SORT ORDERfrom the same Vensim file. - Treat direction > 0 as ascending.
- Document in the engine
CLAUDE.mdthatRANKmeans Vensim'sVECTOR 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_ranksrc/simlin-engine/src/vm_vector_sort_order.rs: the per-row rule to copysrc/simlin-engine/src/mdl/xmile_compat.rs:"vector rank" => "RANK"
Activity
bpowers commentedon Aug 26, 2026
Addendum to section 1:
VECTOR RANKranks within the innermost row;Opcode::Rankranks the whole viewFound while reviewing the Phase 1 signature-table tests (
builtin_signature_tests::rank_accepts_the_cross_dimension_operand_vector_sort_order_accepts, branchcompiler-unification). Beyond binding Vensim semantics to the XMILE name, Simlin'sRANKdoes not match Vensim'sVECTOR RANKon a multi-row operand.Evidence:
test/test-models/tests/vector_order/output.tabVensim DSS for Windows 7.3.4 output, per the directory's README.
test_vector_order.mdlbeside it declaresRegion: A, B, C,company: (company1-company5) -> scompany,product: X, Y, and eightVECTOR RANKequations. The ones with a multi-row operand, read from theTime = 0row ofoutput.tab:revenue RANK2A[company,Region] = VECTOR RANK(revenue2A[company,Region], 1), operand 5 x 3. Columnsrevenue RANK2A[company1,A],[company1,B],[company1,C]read1, 2, 3;[company2,A..C]read2, 1, 3;[company3,A..C]read3, 2, 1;[company4,A..C]read2, 3, 1;[company5,A..C]read2, 3, 1. Every value is in 1..3: eachcompanyrow is ranked over its threeRegionvalues. 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]read1, 4, 3, 2, 5;[B,sort1..sort5]read3, 2, 1, 5, 4;[C,sort1..sort5]read5, 4, 1, 2, 3. EachRegionrow 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]read1, 4, 3, 2, 5and[A,Y,sort1..sort5]the same (the model loads the same spreadsheet column into the X and Y rows ofrevenue3);[B,X,sort1..sort5]read3, 2, 1, 5, 4;[C,X,sort1..sort5]read5, 4, 1, 2, 3. Each(Region, product)row is a permutation of 1..5.revenue RANK neg2A[company1,A..C]read3, 2, 1.revenue RANK[company] = VECTOR RANK(revenue[company], 1)reads1, 4, 5, 2, 3: one row, where per-row and whole-view ranking coincide.RANK2Ais 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 rulesrc/simlin-engine/src/vm_vector_sort_order.rsalready implements forVECTOR SORT ORDERfrom 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, theOpcode::Rank { write_temp_id }arm) collects every element of the view into one list, sorts it once, and writesrank + 1at each element's original position: whole-view ranking.src/simlin-engine/src/wasmgen/vector.rs(emit_rank) mirrors it. On theRANK2Aoperand that ranks all 15 elements together, so Simlin'srevenue RANK2Awould range over 1..15 where Vensim's ranges over 1..3 per row. This is reasoned from the opcode, not from a run:vector_orderis not in thetests/integration/simulate.rscorpus, so nothing exercises it today, and the existingarray_tests::rank_testsrows 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 betweenRANKandVECTOR 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 keepRANKVensim-flavoured), the Vensim-semantics implementation also needs the per-innermost-row rule in both backends, withvector_sort_orderas the template: iterate the view in row-major order, rank within each block ofinner = |last dim|elements, writerank + 1at the in-row position. Addingvector_orderto the integration corpus would pin it against the fixture.[-]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[/+]