Skip to content

codegen: implement real Eigen matrix-multiply (@) support #94

Description

@petercorke

Summary

bdsim.codegen has no C++ implementation for Python's @ (matrix multiply) operator -- CppEmitter.expr_BinaryOp's @ case fails loudly at generation time ("matrix multiply (@) has no C++ implementation yet") rather than emitting a call to a matmul() helper that was never actually defined (the previous behavior: silently generated C++ that failed at compile/link time instead of generation time).

This is independent of continuous-time block support (bdsim#93, closed wontfix) -- that issue is specifically about continuous blocks not being pursued at all; this one is about the @ operator itself, which is on the roadmap regardless. @ shows up in sampled/stateless blocks too -- e.g. PROD's matrix branch (prod = prod @ input), reachable today for any PROD block fed matrix inputs.

Proposed fix

Eigen's own Matrix::operator* already performs real matrix multiplication (not elementwise) for two Matrix-typed operands, so a first cut is likely cheap:

// Python: prod @ input  ->
(prod * input)   // when both operands are Eigen::Matrix-typed

Also needed for PROD's / (matrix division -> np.linalg.inv, which maps to Eigen's .inverse() for square matrices) and, if continuous-block support is ever revisited, LTI_SISO-style self.C @ x.

Scope note

Not the same bug as bdsim#93 -- please don't close this alongside that one; they're unrelated except for sharing a symptom.

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

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions