Re: [julia-users] C++-like operator() for Julia's composite types
First, are you looking for anonymous functions? http://docs.julialang.org/en/latest/manual/functions/#anonymous-functions Second, I suspect Grid.jl already does exactly what you're asking re interpolation. https://github.com/timholy/Grid.jl --Tim On Tuesday, February 25, 2014 02:34:11 AM Marek Gagolewski wrote: Dear all, Is it possible in Julia to define a C++-like operator() for its struct-like composite types (defined with typeend)? Thanks to such a method, one may treat an object as a function. Here's a GitHub gist illustrating what I'm trying to achieve: https://gist.github.com/gagolews/9206364 In other words, I'd like to create a function which has some associated (deep copied) objects with it, by calling new_function = create_function(data) and such that new_function does not rely on dataanymore (at least from its caller/user perspective). I tried to play with Julia's macros, but I'm quite sure it's not the case here. My inspiration is R's approxfun() which returns a function object that interpolates (linearly) a given set of points in 2D. I'm trying to develop a similar tool in Julia, but I cannot move on because of this issue. (x - seq(0, 1, length.out=10)) [1] 0.000 0.111 0.222 0.333 0.444 0.556 0.667 [8] 0.778 0.889 1.000 (y - x^2) [1] 0. 0.01234568 0.04938272 0. 0.19753086 0.30864198 [7] 0. 0.60493827 0.79012346 1. f - approxfun(x, y) f(0.35) [1] 0.1240741 y - sqrt(x) f(0.35) # no change (y is stored within f) [1] 0.1240741 # more precisely it is a new environment (hash table) # ASSOCIATED with the function ls(envir=environment(f)) [1] f method x y yleft yright f # one source code, but operates on different data function (v) .approxfun(x, y, v, method, yleft, yright, f) bytecode: 0x2ff5fb8 environment: 0x320a7d0 Anybody? Best regards, Marek Gagolewski http://gagolewski.rexamine.com
Re: [julia-users] C++-like operator() for Julia's composite types
On Tuesday, February 25, 2014 4:24:48 PM UTC+1, Stefan Karpinski wrote: We've discussed it, but at this point function call syntax is not overloadable. Grid.jl https://github.com/timholy/Grid.jl overloads the indexing operator for this purpose instead. Many thanks thanks for your answer. Maybe some day then. At least now I know that I should think of some solution myself. :) Anyway, an overloaded () operator would be much more convenient than [] in my case - there would be a single way for accessing a method, f(), where f is either a `function` object or a 'struct-type' object.
Re: [julia-users] C++-like operator() for Julia's composite types
On Tuesday, February 25, 2014 5:16:13 PM UTC+1, Tim Holy wrote: First, are you looking for anonymous functions? http://docs.julialang.org/en/latest/manual/functions/#anonymous-functions Second, I suspect Grid.jl already does exactly what you're asking re interpolation. https://github.com/timholy/Grid.jl Thanks Tim, but I think that's not the case. I'd like to create a function that returns an interpolating function (independent of the objects which were used to create it). I don't think it's directly possible in Grid (except for a []-like hack that Stefan mentioned). It only partially suits my needs, unfortunately. Among similar functions in R that obey this property I find approxfun, splinefun, and ecdf (each one aiming at some kind of point interpolation)
Re: [julia-users] C++-like operator() for Julia's composite types
this is kinda obvious, so i suspect it's not what you want, but just in case... function make_interp(x, y) function interp(x2) @assert x2 x[1] x2 x[end] i = 1 while x[i] x2; i += 1; end (x[i] - x2) * y[i-1] + (x2 - x[i-1]) * y[i] end end x = [1,2,3,4,5] y = [x2^2 for x2 in x] my_interp = make_interp(x, y) println(my_interp(2.1)) # prints 4.5 andrew On Tuesday, 25 February 2014 17:17:24 UTC-3, Marek Gagolewski wrote: On Tuesday, February 25, 2014 5:16:13 PM UTC+1, Tim Holy wrote: First, are you looking for anonymous functions? http://docs.julialang.org/en/latest/manual/functions/#anonymous-functions Second, I suspect Grid.jl already does exactly what you're asking re interpolation. https://github.com/timholy/Grid.jl Thanks Tim, but I think that's not the case. I'd like to create a function that returns an interpolating function (independent of the objects which were used to create it). I don't think it's directly possible in Grid (except for a []-like hack that Stefan mentioned). It only partially suits my needs, unfortunately. Among similar functions in R that obey this property I find approxfun, splinefun, and ecdf (each one aiming at some kind of point interpolation)
Re: [julia-users] C++-like operator() for Julia's composite types
oh, and i forgot to normalize by the x delta. On Tuesday, 25 February 2014 17:47:16 UTC-3, andrew cooke wrote: this is kinda obvious, so i suspect it's not what you want, but just in case... function make_interp(x, y) function interp(x2) @assert x2 x[1] x2 x[end] i = 1 while x[i] x2; i += 1; end (x[i] - x2) * y[i-1] + (x2 - x[i-1]) * y[i] end end x = [1,2,3,4,5] y = [x2^2 for x2 in x] my_interp = make_interp(x, y) println(my_interp(2.1)) # prints 4.5 andrew On Tuesday, 25 February 2014 17:17:24 UTC-3, Marek Gagolewski wrote: On Tuesday, February 25, 2014 5:16:13 PM UTC+1, Tim Holy wrote: First, are you looking for anonymous functions? http://docs.julialang.org/en/latest/manual/functions/#anonymous-functions Second, I suspect Grid.jl already does exactly what you're asking re interpolation. https://github.com/timholy/Grid.jl Thanks Tim, but I think that's not the case. I'd like to create a function that returns an interpolating function (independent of the objects which were used to create it). I don't think it's directly possible in Grid (except for a []-like hack that Stefan mentioned). It only partially suits my needs, unfortunately. Among similar functions in R that obey this property I find approxfun, splinefun, and ecdf (each one aiming at some kind of point interpolation)
Re: [julia-users] C++-like operator() for Julia's composite types
Dear Andrew, Nope, unfortunately it's not what I am trying to achieve. The code you've kindly (let's forget the interpolation task, what I really meant is some programming construct, so it's OK with no delta_x) submitted gives: my_interp = make_interp(x, y) println(my_interp(2.1)) # prints 4.5 y[2] = 5 println(my_interp(2.1)) # prints 5.4 and I also would like to get 4.5 in the second case. All the best, Marek
Re: [julia-users] C++-like operator() for Julia's composite types
Does this give you what you want: function make_interp(x, y) y = copy(y) function interp(x2) @assert x2 x[1] x2 x[end] i = 1 while x[i] x2; i += 1; end (x[i] - x2) * y[i-1] + (x2 - x[i-1]) * y[i] end end x = [1,2,3,4,5] y = [x2^2 for x2 in x] my_interp = make_interp(x, y) println(my_interp(2.1)) # prints 4.5 y[2] = 5 println(my_interp(2.1)) # prints 4.5 On Tue, Feb 25, 2014 at 4:01 PM, Marek Gagolewski m.gagolew...@gmail.com wrote: Dear Andrew, Nope, unfortunately it's not what I am trying to achieve. The code you've kindly (let's forget the interpolation task, what I really meant is some programming construct, so it's OK with no delta_x) submitted gives: my_interp = make_interp(x, y) println(my_interp(2.1)) # prints 4.5 y[2] = 5 println(my_interp(2.1)) # prints 5.4 and I also would like to get 4.5 in the second case. All the best, Marek
Re: [julia-users] C++-like operator() for Julia's composite types
On Tuesday, February 25, 2014 10:27:47 PM UTC+1, tshort wrote: Does this give you what you want: [...] Thanks very much, this is exactly the solution I needed. :) All the best Marek