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

Iterated and inverse functions syntax #43

Closed
jariji opened this issue Jan 31, 2024 · 7 comments
Closed

Iterated and inverse functions syntax #43

jariji opened this issue Jan 31, 2024 · 7 comments

Comments

@jariji
Copy link
Contributor

jariji commented Jan 31, 2024

Using the syntax idea from JuliaLang/julia#39042, I wonder what you think about this syntax for iterated functions. This style is used in array languages.

using InverseFunctions, Test
struct IteratedFunction{F}
    f::F
    n::Int
end
((;f,n)::IteratedFunction)(x) =
    if n == 0
        x
    elseif n > 0
        foldl((y,f)->f(y), [f for _ in 1:n]; init=x);
    else # if n < 0
        (inverse(f) ̂ (-n))(x)
    end

f ̂ n = IteratedFunction(f,n)

@test (exp̂0)(3) == 3
@test (exp̂1)(1)  2.718281828459045
@test (exp̂2)(1)  15.154262241479262
@test (exp̂3)(1)  3.814279104760214e6
@test (exp̂(-1))(ℯ)  1.0
@test (exp̂(-2))(ℯ)  0
@oschulz
Copy link
Collaborator

oschulz commented Jan 31, 2024

@jariji, I have support for iterated function in FunctionChains, and FunctionChains supports InverseFunctions (and ChangesOfVariables as well):

julia> using FunctionChains, InverseFunctions, FillArrays

julia> f = setinverse(x -> x + 1, x -> x - 1)

julia> g = fchain(Fill(f, 10))

julia> g(3)
13

julia> inverse(g)(13)
3

It would certainly make sense to add a shortcut/equivalent for fchain(Fill(f, 10)) to FunctionChains (ideally without depending on FillArrays, which is rather heavy) - would you like to move this issue there?

About using f ∘̂ n is the operator - hm maybe, @devmotion, what do you think?

Supporting f ∘̂ -n in a type-stable fashion could get tricky though.

@devmotion
Copy link
Member

I'm sorry, it's not clear to me how this syntax request is related to InverseFunctions.

Generally, I like the operator proposal - but since it's taken from the Julia PR I think (similar to other proposals in base Julia) if desired it should be tried in a separate dedicated package (similar to the tensor syntax in e.g. https://github.com/JuliaMath/TensorCore.jl) and not in InverseFunctions (InverseFunctions could then eg define inverse in an extension for such a package).

@oschulz
Copy link
Collaborator

oschulz commented Jan 31, 2024

I'm sorry, it's not clear to me how this syntax request is related to InverseFunctions.

Indeed, it should (for example) go into FunctionChains (see above). I was just wondering about the choice of operator - sorry, that was a bit off-topic given my remarks before.

I'll comment on the Julia PR.

@jariji
Copy link
Contributor Author

jariji commented Feb 21, 2024

It would certainly make sense to add a shortcut/equivalent for fchain(Fill(f, 10)) to FunctionChains (ideally without depending on FillArrays, which is rather heavy) - would you like to move this issue there?

Yes please.

@oschulz
Copy link
Collaborator

oschulz commented Feb 24, 2024

Ok, I'll get on that in FunctionChains then @jariji .

@oschulz
Copy link
Collaborator

oschulz commented Feb 28, 2024

Using Iterators.repeated should make this fairly easy without needing FillArrays.

@oschulz
Copy link
Collaborator

oschulz commented Feb 28, 2024

Closing this here in favor of oschulz/FunctionChains.jl#13 .

@oschulz oschulz closed this as completed Feb 28, 2024
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

3 participants