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

1D comprehensions with multiple variables #4867

Closed
kmsquire opened this issue Nov 20, 2013 · 11 comments
Closed

1D comprehensions with multiple variables #4867

kmsquire opened this issue Nov 20, 2013 · 11 comments

Comments

@kmsquire
Copy link
Member

I just tried to do

julia> [(i,j) for i=1:5, j=i:5]
ERROR: i not defined
 in anonymous at no file

Unfortunately, this produces an error that i is undefined, since this produces a 2D array in Julia.

Related to discussion in #550, especially #550 (comment), but there, the proposal is to force 1D array creation in the presence of an if clause (which makes sense).

I'm not sure there's a good solution here, mainly because I don't know how popular/common the 2D comprehension syntax is.

@JeffBezanson
Copy link
Member

I think the if clause solution is as reasonable as it's going to get. Reopen if a different idea occurs to you.

@RaulDurand
Copy link

What about the use of two for (a la Python ) in order to use the first variable?

L = [ i+j for i=1:10 for j=i:10 if (i+j)%2>0 ]    # returns a 1D array

See my comment #550 (comment)

@JeffBezanson JeffBezanson reopened this Nov 16, 2014
@JeffBezanson
Copy link
Member

Yeah that's a good idea.

@StefanKarpinski
Copy link
Member

I'm not super keen on the double for thing – largely because the double for seems to me like the version that should produce a 2-d array. I proposed at some point that the presence of an if always flatten.

@nalimilan
Copy link
Member

if i >= j could be useful if it created symmetric matrices. But TBH I'm not sure the complexity that would add is worth it.

@daviddelaat
Copy link
Contributor

If the if syntax is implemented by growing an array (which is probably necessary for when there are a large number of iterations and the if statement is almost always false), then I think we need some other notation for getting the 1-d result.

I also think the if true syntax feels a bit hacky (and for me nested 1-d comprehensions are much more common than conditions in comprehensions).

I like @RaulDurand proposal, but the other way around is also possible: double for to get a matrix and comma's to vectorize.

@prcastro
Copy link
Contributor

I like @daviddelaat proposal, feels much more natural than 2 fors for 1-D array.

@StefanKarpinski
Copy link
Member

That would also break all existing code that uses multidimensional arrays.

@JeffBezanson
Copy link
Member

Actually I'd like for (i,j) in product(x,y) for flattened iteration over
multiple things.

@garborg
Copy link
Contributor

garborg commented Nov 22, 2014

+1 for product

@RaulDurand
Copy link

May be I am wrong but product may not work when the second range depends on the first.
In the example below there is no way to use product because the second iteration is done in different sets of 'children':

children = Child[]
for parent in parents
    for child in parent.children
          push!(children, child)
    end
end

On the other hand, trying to make a direct translation into a comprehension we could have:

children = [ child for parent in parents for child in parent.children ]

or even, including an if clause:

children = [ child for parent in parents for child in parent.children if child.age<12 ]

When I work with finite elements, usually I have to do:

all_dofs = Dof[]
for elem in elems
    for node in elem.nodes
        for dof in node.dofs
            push!(all_dofs, dof)
        end
    end
end

A similar translation into a comprehension leads to:

all_dofs = [ dof for elem in elems for node in elem.nodes for dof in node.dofs ]

The use of multiple for seems natural in the above 1d examples and I don't think we need to associate the number of for with the number of dimensions.

IMHO, I think that the use of commas are ok for multidimensional arrays since it is similar to the use of commas for indexing (for example A[i,j] or B[i,j,k]) and the array sizes in each dimension are known a priori.

This was referenced Jan 26, 2016
JeffBezanson added a commit that referenced this issue Jun 27, 2016
this also uses the new lowering for typed comprehensions, allowing all
comprehensions on unknown-length iterables (fixes #1457)
JeffBezanson added a commit that referenced this issue Jun 28, 2016
this also uses the new lowering for typed comprehensions, allowing all
comprehensions on unknown-length iterables (fixes #1457)
JeffBezanson added a commit that referenced this issue Jun 29, 2016
this also uses the new lowering for typed comprehensions, allowing all
comprehensions on unknown-length iterables (fixes #1457)
JeffBezanson added a commit that referenced this issue Jul 6, 2016
this also uses the new lowering for typed comprehensions, allowing all
comprehensions on unknown-length iterables (fixes #1457)
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
JuliaLang#4867)

this also uses the new lowering for typed comprehensions, allowing all
comprehensions on unknown-length iterables (fixes JuliaLang#1457)
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

9 participants