Skip to content

Bug: ifelse with constant branches fails with MethodError #314

Description

@bdrhill

Problem

ifelse(tracer_condition, constant1, constant2) fails when both branches are constants (not tracers), because output_union(::Float64, ::Float64) is not defined.

MWE

using SparseConnectivityTracer

detector = TracerSparsityDetector()

# This fails - both branches are constants
f(x) = [ifelse(x[1] > 0, 1.0, 0.0) * x[2]]
jacobian_sparsity(f, [1.0, 2.0], detector)
# MethodError: no method matching output_union(::Float64, ::Float64)

# This works - at least one branch is a tracer
g(x) = [ifelse(x[1] > 0, x[1], x[2])]
jacobian_sparsity(g, [1.0, 2.0], detector)
# OK: nnz=2

Expected behavior

When both branches are constants, ifelse should return a constant (zero sparsity pattern), not error.

Fix

Add method in src/overloads/ifelse_global.jl:

output_union(x::Real, y::Real) = x  # or some neutral value, since no tracers

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent 🤖Agentically generated issuebugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions