Giter Club home page Giter Club logo

Comments (4)

phipsgabler avatar phipsgabler commented on June 1, 2024 1
julia> foldl(index; init = a) do x, index
         x[index...]
       end
2-element Array{Float64,1}:
 0.32155828770721984
 0.14675284701717906

Now that's one ingenious fold! But yeah, I'll put a PR replacing the strings by @vinds on my todo list, then. Although I fear that there's some code depending on the string indices in peculiar ways...

from dynamicppl.jl.

devmotion avatar devmotion commented on June 1, 2024

I would be happy if one could come up with a better alternative, actually I've thought about this a bit as well.

Would an equivalent of Tuple{Vararg{Union{Int, Colon}}} suffice?

To me it seemed the main issue would be nested indexing, e.g., it would not be sufficient for the third example here:

- `x[1] ~ Normal()` will generate a `VarName` with `sym == :x` and `indexing == "[1]"`.
- `x[:,1] ~ MvNormal(zeros(2))` will generate a `VarName` with `sym == :x` and
`indexing == "[Colon(),1]"`.
- `x[:,1][2] ~ Normal()` will generate a `VarName` with `sym == :x` and
`indexing == "[Colon(),1][2]"`.

But actually, this example seems a bit weird - if x is supposed to be a matrix of real numbers, one should just write x[2, 1] instead. However, there might be a use case for matrices of vectors where one would want to write something like x[1, 2][3]?

from dynamicppl.jl.

phipsgabler avatar phipsgabler commented on June 1, 2024

To me it seemed the main issue would be nested indexing.

It seems @vinds already handles that and returns a tuple of Tuple{Vararg{Union{Int, Colon}}}. However, it is used exactly nowhere in DPPL or Turing. @mohamed82008, have you inteded to use it in the future?

Perhaps we can also convert VarName to the following, to have an intermediate layer of abstraction:

struct VarName{name, I, T}
    indices::T

    VarName(name::Symbol, indices::Vararg{<:Index, I}) where {I} = 
        new{name, I, typeof(indices)}(indices)
end

struct Index{N, T}
    locations::T

    Index(locations::Vararg{<:Union{Colon, Int}, N}) where {N} = 
        new{N, typeof(locations)}(locations)
end

But actually, this example seems a bit weird - if x is supposed to be a matrix of real numbers, one should just write x[2, 1] instead. However, there might be a use case for matrices of vectors where one would want to write something like x[1, 2][3]?

Maybe in nonparametric models?

One more thing: are arbitrary slices allowed, like x[2:4][begin+2:end-2]?

from dynamicppl.jl.

devmotion avatar devmotion commented on June 1, 2024

It seems @vinds already handles that and returns a tuple of Tuple{Vararg{Union{Int, Colon}}}.

Ah, I did not know about @vinds. In general, every iterable structure would be nice (but I guess a tuple is the most reasonable one here) since then one could perform, e.g.,

julia> a = [rand(2) for i in 1:5, j in 1:4]
5×4 Array{Array{Float64,1},2}:
 [0.530985, 0.453194]    [0.321558, 0.146753]  [0.0247715, 0.334534]  [0.0981353, 0.713722]
 [0.521073, 0.817136]    [0.972983, 0.438349]  [0.0885043, 0.715975]  [0.165333, 0.667311] 
 [0.0977404, 0.0194948]  [0.913825, 0.584694]  [0.227551, 0.477571]   [0.524443, 0.0958542]
 [0.583755, 0.328472]    [0.259529, 0.397364]  [0.917507, 0.169614]   [0.0989215, 0.176417]
 [0.930105, 0.362702]    [0.262971, 0.38255]   [0.54622, 0.692841]    [0.317475, 0.106472] 

julia> index = ((1, Colon()), (2,))
((1, Colon()), (2,))

julia> foldl(index; init = a) do x, index
         x[index...]
       end
2-element Array{Float64,1}:
 0.32155828770721984
 0.14675284701717906

without having to work with strings.

from dynamicppl.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.