Skip to content

VM: SUM over transposed array view returns NaN #422

Closed

Description

@bpowers

Description

SUM(matrix') (SUM applied to a transposed 2D array view) returns NaN in the VM instead of computing the correct sum.

For a 2x3 matrix with values [[1,2,3],[4,5,6]], SUM(matrix') should return 21 (the sum of all elements is invariant under transposition), but the VM produces NaN.

A workaround exists using explicit dimension position syntax: SUM(matrix[@2, @1]) correctly returns 21. This suggests the issue is specifically in how the VM handles the transpose operator (') when feeding into an array reduction opcode.

Why it matters

  • Correctness: Users writing SUM(transposed_view) get silently wrong results (NaN propagates through downstream equations)
  • Usability: The workaround ([@2, @1] syntax) is non-obvious and fragile if dimensions change

Component(s) affected

  • src/simlin-engine/src/vm.rs (array reduction opcodes with transposed views)
  • src/simlin-engine/src/compiler/codegen.rs (view stack setup for transposed arrays feeding into reductions)

Current test coverage

The test transpose_sum in src/simlin-engine/src/array_tests.rs (line 571) documents this limitation with a TODO comment, asserting the current NaN behavior.

Possible approaches

  1. Investigate how the view stack represents transposed arrays and whether ArraySum (and other reduction opcodes) correctly iterate over the transposed view's elements
  2. The dimension-position workaround ([@2, @1]) works, suggesting the reduction opcode itself is fine but the transpose view is not being materialized correctly for reduction

Context

Identified during the interpreter-removal migration (branch remove-interpreter). This is a pre-existing VM limitation that was previously masked because earlier tests only ran through the AST interpreter.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions