Skip to content

Feature: Support tuple and NamedTuple outputs #310

Description

@bdrhill

Problem

Functions returning tuples or NamedTuples fail with MethodError because to_array in src/trace_functions.jl doesn't handle these types.

MWE

using SparseConnectivityTracer

detector = TracerSparsityDetector()

# Tuple output fails
f_tuple(x) = (x[1]^2, x[2]^2)
jacobian_sparsity(f_tuple, [1.0, 2.0], detector)
# MethodError: no method matching to_array(::Tuple{GradientTracer...})

# NamedTuple output fails  
f_nt(x) = (a=x[1]^2, b=x[2]^2)
jacobian_sparsity(f_nt, [1.0, 2.0], detector)
# MethodError: no method matching to_array(::NamedTuple...)

Expected behavior

These should work by collecting the tuple elements into an array:

to_array(x::Tuple) = collect(x)
to_array(x::NamedTuple) = collect(values(x))

Related

This is related to #298 (Better unpacking of output types) which mentions Rational/Complex types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent 🤖Agentically generated issuenot plannedThere are no plans to work on this feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions