Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloads for Base functions needed for tracing through matrix exponential from ExponentialUtilities #55

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ for TT in (:JacobianTracer, :ConnectivityTracer, :HessianTracer)
@eval Base.similar(::Array, ::Type{$TT{S}}, dims::Dims{N}) where {N,S} =
zeros($TT{S}, dims)
end

Base.real(sct::SparseConnectivityTracer.AbstractTracer) = sct
Base.float(sct::SparseConnectivityTracer.AbstractTracer) = sct
Base.max(x::SparseConnectivityTracer.AbstractTracer, y::SparseConnectivityTracer.AbstractTracer) = x
Base.floatmin(x::Type{<:SparseConnectivityTracer.AbstractTracer}) = Base.floatmin(Float32)
Base.eps(x::Type{<:SparseConnectivityTracer.AbstractTracer}) = Base.eps(Float32)
Comment on lines +35 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should already exist:

# Functions returning constant output
# that only depends on the input type.
# For the purpose of operator overloading,
# these are kept separate from ops_1_to_1_z.
ops_1_to_1_const = (
:zero, :one,
:eps,
:typemin, :typemax,
:floatmin, :floatmax, :maxintfloat,
)

Did you encounter any errors on these functions?

Base.isinf(::SparseConnectivityTracer.AbstractTracer) = false
Base.isless(x::SparseConnectivityTracer.AbstractTracer, ::SparseConnectivityTracer.AbstractTracer) = true
Base.isless(::SparseConnectivityTracer.AbstractTracer, ::Float64) = true
Comment on lines +38 to +39
Copy link
Owner

@adrhill adrhill May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparisons like max, isless and isequal will need primal value information, otherwise we always hit the first branch of any comparison.
To give you an example, the function f(x, y) = isless(x, y) ? x : y would always return the same Jacobian pattern [1 0].

Any global pattern we return should be a conservative estimate of the sparsity over the entire input domain.
In case of the example above, this would be [1 1] (not sparse).

We will add functionality for local sparsity detection soon. I opened #56 for the purpose of tracking.

Base.isfinite(::SparseConnectivityTracer.AbstractTracer) = true
Base.conj(x::SparseConnectivityTracer.AbstractTracer) = x