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

Piecewise functions #655

Open
dpsanders opened this issue May 26, 2024 · 5 comments
Open

Piecewise functions #655

dpsanders opened this issue May 26, 2024 · 5 comments

Comments

@dpsanders
Copy link
Member

dpsanders commented May 26, 2024

Here's a possible implementation for piecewise functions:

using IntervalArithmetic
using IntervalArithmetic.Symbols # to use the constructor `..`

# represents a piecewise 1D function
struct Piecewise{T <: Tuple}
    pieces::T 
end

Piecewise(pairs...) = Piecewise(pairs)

function (piecewise::Piecewise)(X::Interval)
    return reduce(hull, f(intersect_interval(X, region)) for (region, f)  piecewise.pieces)
end

f = Piecewise(
        0..3 => x -> x + interval(1),
        3..6 => identity,
        6..Inf => x -> x + interval(2)
)

f(2..5)

Then Heaviside should be definable with

H = Piecewise(-Inf..0 => zero, 0..Inf => one)

but that is not working for me right now 🤔 E.g. it gives

julia> H(1..1)
[0.0, 1.0]_trv

whereas it should give [1, 1].

@dpsanders
Copy link
Member Author

Ah that's because

julia> zero(emptyinterval())
[0.0, 0.0]_com

I have an idea for how to make a default 0 value.

@dpsanders
Copy link
Member Author

dpsanders commented May 26, 2024

This version works:

function (piecewise::Piecewise)(X::Interval)
    return reduce(hull, begin
                            val = intersect_interval(X, region)
                            isempty_interval(val) ? val : f(val)
                        end
                    for (region, f)  piecewise.pieces)
end

H = Piecewise(-Inf..0 => zero, 0..Inf => one)

@dpsanders
Copy link
Member Author

It would probably be worth having a PiecewiseConstant too.

@dpsanders
Copy link
Member Author

We also need to add the decoration of course.

@OlivierHnt
Copy link
Member

OlivierHnt commented May 26, 2024

This issue seems to supersede #653 and #654 no?

Also, with such a feature, implementing the Heaviside function would be even simpler than it already is so I am not sure it would warrant being defined inside the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants