[julia-users] Re: Unexpected type behaviour in arithmetic operations

2014-02-28 Thread David P. Sanders


El viernes, 28 de febrero de 2014 07:49:09 UTC-6, andrew cooke escribió:

 the current behaviour is explained at 
 http://julia.readthedocs.org/en/latest/manual/conversion-and-promotion/


But I don't find it evident from that document that Int32+Int32 gives Int64 
on a 64-bit machine!
 



 defining
   Base.promote_rule(::Type{Int32}, ::Type{Int32}) = Int32
 doesn't help either, and i'm not sure why.

 andrew


 On Friday, 28 February 2014 10:46:16 UTC-3, Tobias Knopp wrote:

 There was quite some discussion on this topic in 
 https://groups.google.com/forum/?fromgroups=#!searchin/julia-users/Int32/julia-users/Rte_I6htLRc/VJG5DWVcZbQJ
 I had the feeling that we almost convinced Stefan :-)

 The good thing is that Array{Int32} operations do not promote to 
 Array{Int64}. But I still vote for promoting Int32 + Int32 to Int32.



[julia-users] Interesting behaviour in IntSet

2014-02-28 Thread David P. Sanders

I am investigating possible data structures for an application.
Here is an interesting behaviour in IntSet, which is no doubt to do with 
the implementation.
Maybe it should just throw an exception if someone tries to add a really 
large integer like this!


julia s = IntSet()
IntSet()

julia push!(s, 10)
IntSet(10)

julia sizeof(s)
24

julia push!(s, 100)
IntSet(10, 100)

julia sizeof(s)
24

julia push!(s, 1000)
IntSet(10, 100, 1000)

julia push!(s, 1)
IntSet(10, 100, 1000, 1)

julia push!(s, 10)
IntSet(10, 100, 1000, 1, 10)

julia sizeof(s)
24

julia push!(s, 100)
IntSet(10, 100, 1000, 1, 10, 1410065408, 
1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 
1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 
1410065408, 1410065408, 1410065408, 1410065408^CEvaluation succeeded, but 
an error occurred while showing value of type IntSet:
ERROR: interrupt
 in show at intset.jl:172
 in anonymous at show.jl:973
 in showlimited at show.jl:972
 in writemime at repl.jl:2
 in display at multimedia.jl:117
 in display at multimedia.jl:119
 in display at multimedia.jl:151


[julia-users] Re: Interesting behaviour in IntSet

2014-02-28 Thread David P. Sanders


El viernes, 28 de febrero de 2014 08:41:37 UTC-6, Ivar Nesje escribió:

 The documentation states very clear that 
 IntSethttp://docs.julialang.org/en/latest/stdlib/base/#Base.IntSet should 
 only be used for dense collections, and that 
 Sethttp://docs.julialang.org/en/latest/stdlib/base/#Base.Set, 
 should be used for sparse collections.


Agreed. Of course, this was just a toy example to test the limits of IntSet.
In the real application that I am working towards, I want to think about 
systems of size at least 10^5 x 10^5. Mapping pairs (x,y) in this system to 
a single number gives up to 10^10,
which is what I was testing.

 


 Construct a sorted set of the integers generated by the given iterable 
 object, or an empty set. Implemented as a bit string, and therefore 
 designed for dense integer sets. If the set will be sparse (for example 
 holding a single very large integer), use Set instead.


 Do you happen to know a nice limit to how much memory IntSet should be 
 allowed to use?


In this case, it seems to be using more memory than I have available on my 
machine (4GB on my laptop).
I guess my point is that normally I would expect that to give me an 
out-of-memory error, rather than enter an infinite loop producing garbage.



 On my laptop 100 MB would be more than I can afford


Not sure what you mean by that -- doesn't it rather depend on the 
application? If I am doing a heavy computation on my laptop over night, I 
am happy for it to use all available memory.

 

 , but that would make IntSet unusable for bigger calculations on bigger 
 systems, so it should be no smaller than 10 GB.


I have another machine with a lot of memory (128 GB), so I certainly do not 
want to impose an arbitrary restriction.


David.
 


 Ivar

 kl. 15:16:33 UTC+1 fredag 28. februar 2014 skrev David P. Sanders følgende:


 I am investigating possible data structures for an application.
 Here is an interesting behaviour in IntSet, which is no doubt to do 
 with the implementation.
 Maybe it should just throw an exception if someone tries to add a really 
 large integer like this!


 julia s = IntSet()
 IntSet()

 julia push!(s, 10)
 IntSet(10)

 julia sizeof(s)
 24

 julia push!(s, 100)
 IntSet(10, 100)

 julia sizeof(s)
 24

 julia push!(s, 1000)
 IntSet(10, 100, 1000)

 julia push!(s, 1)
 IntSet(10, 100, 1000, 1)

 julia push!(s, 10)
 IntSet(10, 100, 1000, 1, 10)

 julia sizeof(s)
 24

 julia push!(s, 100)
 IntSet(10, 100, 1000, 1, 10, 1410065408, 
 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 
 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 1410065408, 
 1410065408, 1410065408, 1410065408, 1410065408^CEvaluation succeeded, but 
 an error occurred while showing value of type IntSet:
 ERROR: interrupt
  in show at intset.jl:172
  in anonymous at show.jl:973
  in showlimited at show.jl:972
  in writemime at repl.jl:2
  in display at multimedia.jl:117
  in display at multimedia.jl:119
  in display at multimedia.jl:151



[julia-users] Re: Iteration over a Set where elements may be deleted in the process -- bug?

2014-02-28 Thread David P. Sanders


El viernes, 28 de febrero de 2014 16:41:20 UTC-6, Pierre-Yves Gérardy 
escribió:

 To realize what you want to do, you can add the elements to be removed to 
 another set.

 On each iteration, verify that the current element isn't already in the 
 set of elements to be removed, and after the loop, clean the first set 
 using the elements of the second one.


Ah, that's a neat solution, thanks! 

David.
 

 —Pierre-Yves

 On Thursday, February 27, 2014 4:54:58 PM UTC+1, David P. Sanders wrote:

 I need to iterate over a Set whose elements will be deleted in the 
 process.
 However, some deleted elements are included in the iteration, e.g. the 
 element 4 in the code below.

 Is this a bug, or am I misunderstanding?

 Thanks.

 julia s = Set(5, 3, 4, 6)
 Set{Int64}({5,4,6,3})

 julia for i in s
println(i=$i  s=$s)
delete!(s, i-1)
println(now s=$s\n)
end
 i=5  s=Set{Int64}({5,4,6,3})
 now s=Set{Int64}({5,6,3})

 i=4  s=Set{Int64}({5,6,3})
 now s=Set{Int64}({5,6})

 i=6  s=Set{Int64}({5,6})
 now s=Set{Int64}({6})





[julia-users] Iteration over a Set where elements may be deleted in the process -- bug?

2014-02-27 Thread David P. Sanders
I need to iterate over a Set whose elements will be deleted in the process.
However, some deleted elements are included in the iteration, e.g. the 
element 4 in the code below.

Is this a bug, or am I misunderstanding?

Thanks.

julia s = Set(5, 3, 4, 6)
Set{Int64}({5,4,6,3})

julia for i in s
   println(i=$i  s=$s)
   delete!(s, i-1)
   println(now s=$s\n)
   end
i=5  s=Set{Int64}({5,4,6,3})
now s=Set{Int64}({5,6,3})

i=4  s=Set{Int64}({5,6,3})
now s=Set{Int64}({5,6})

i=6  s=Set{Int64}({5,6})
now s=Set{Int64}({6})





[julia-users] Re: Iteration over a Set where elements may be deleted in the process -- bug?

2014-02-27 Thread David P. Sanders


El jueves, 27 de febrero de 2014 09:54:58 UTC-6, David P. Sanders escribió:

 I need to iterate over a Set whose elements will be deleted in the process.
 However, some deleted elements are included in the iteration, e.g. the 
 element 4 in the code below.


According to the following, it is enough to check if the element is `in` 
the Set.
It seems wrong that this should be necessary, though!
 

julia for i in s
   println(i=$i  s=$s  $(i in s))
   delete!(s, i-1)
   println(now s=$s\n)
   end
i=5  s=Set{Int64}({5,4,6,3})  true
now s=Set{Int64}({5,6,3})

i=4  s=Set{Int64}({5,6,3})  false
now s=Set{Int64}({5,6})

i=6  s=Set{Int64}({5,6})  true
now s=Set{Int64}({6})

 


 Is this a bug, or am I misunderstanding?

 Thanks.

 julia s = Set(5, 3, 4, 6)
 Set{Int64}({5,4,6,3})

 julia for i in s
println(i=$i  s=$s)
delete!(s, i-1)
println(now s=$s\n)
end
 i=5  s=Set{Int64}({5,4,6,3})
 now s=Set{Int64}({5,6,3})

 i=4  s=Set{Int64}({5,6,3})
 now s=Set{Int64}({5,6})

 i=6  s=Set{Int64}({5,6})
 now s=Set{Int64}({6})





Re: [julia-users] Re: Best data structure to remove elements one by one

2014-02-25 Thread David P. Sanders


El domingo, 23 de febrero de 2014 18:09:36 UTC-6, Jeff Bezanson escribió:

 This was just changed on master so that you'd do Set([i*2 for i in 1:5]). 


OK, that seems reasonable, thanks.


 On Sun, Feb 23, 2014 at 12:45 AM, David P. Sanders 
 dpsa...@gmail.comjavascript: 
 wrote: 
  
  
  El viernes, 21 de febrero de 2014 16:03:19 UTC-6, Steven G. Johnson 
  escribió: 
  
  
  
  On Friday, February 21, 2014 9:02:48 AM UTC-5, David P. Sanders wrote: 
  
  OK, I think I have answered my own question: a Set is the good 
 structure. 
  And to create a set from an array I can do something like 
  
  s = Set([3, 4, 5]...) 
  
  or 
  
  s = Set([i*2 for i in 1:5]...) 
  
  which would be the closest thing to a set comprehension? 
  
  
  The closest thing that avoids the overhead of creating an array first 
  would currently be a loop: 
  
  s = Set(Int) 
  for i in 1:5 
  push!(s, i*2) 
  end 
  
  
  OK, this is the conclusion I had reached, thanks. 
  
  
  
  At some point, if (when?) 
https://github.com/JuliaLang/julia/issues/4470 
  gets added, it will be easy to add support for syntax like Set(Int, i*2 
  for i in 1:5), where the second argument implicitly creates an iterable 
  type. 
  
  
  Sounds good! 
  



Re: [julia-users] List of indices of matrix satisfying certain criterion

2014-02-25 Thread David P. Sanders


El domingo, 23 de febrero de 2014 02:36:57 UTC-6, Mauro escribió:


 On Sun, 2014-02-23 at 05:51, dpsa...@gmail.com javascript: wrote: 
  El viernes, 21 de febrero de 2014 08:25:00 UTC-6, Mauro escribió: 
  
   Given a matrix, which will be large (say 10^5 x 10^5), I need to 
 extract 
   the list of indices (i.e. the pairs (x,y) of positions) of those 
 places 
  in 
   the matrix 
   where the value stored satisfies a certain condition. For a minimal 
   example, the condition can just be that the value is greater than 
 0.5. 
  
  Give you a logical array which you can use for indexing: 
r..5 
  and to get the linear indices 
find(r..5) 
  
  
  Great, didn't know about find. 
  Maybe I should just rewrite everything thinking in terms of 1D arrays. 
  (This is something I have taught in several courses, 
  but for some reason it didn't occur to me!) 

 No need to rewrite in terms of 1D arrays.  You can index both with a 
 linear index or with a 2D index (or higher D, depending on the array): 

 julia a = [1 2; 3  4] 
 2x2 Array{Int64,2}: 
  1  2 
  3  4 

 julia a[2] 
 3 

 julia a[2,1] 
 3 

 If you want to translate between the two use ind2sub and sub2ind: 

 julia ind = ind2sub(size(a), 2) 
 (2,1) 

 julia a[ind...] 
 3 


Excellent, thanks! 


[julia-users] Re: Best data structure to remove elements one by one

2014-02-22 Thread David P. Sanders


El viernes, 21 de febrero de 2014 16:03:19 UTC-6, Steven G. Johnson 
escribió:



 On Friday, February 21, 2014 9:02:48 AM UTC-5, David P. Sanders wrote: 

 OK, I think I have answered my own question: a Set is the good structure.
 And to create a set from an array I can do something like

 s = Set([3, 4, 5]...)

 or

 s = Set([i*2 for i in 1:5]...)

 which would be the closest thing to a set comprehension?


 The closest thing that avoids the overhead of creating an array first 
 would currently be a loop:

 s = Set(Int)
 for i in 1:5
 push!(s, i*2)
 end


OK, this is the conclusion I had reached, thanks.
 


 At some point, if (when?)
   https://github.com/JuliaLang/julia/issues/4470
 gets added, it will be easy to add support for syntax like Set(Int, i*2 
 for i in 1:5), where the second argument implicitly creates an iterable 
 type.


Sounds good!
 


Re: [julia-users] Best data structure for a dynamic array

2014-02-22 Thread David P. Sanders


El viernes, 21 de febrero de 2014 09:57:10 UTC-6, Kevin Squire escribió:

 It's hard to answer without more information.  Is this related to your 
 other, self-answered post, where you mention adding and deleting elements?


Yes, it's a simulation I'm working on, which is a particular kind of 
dynamics in a random environment.
I have a version in C++ but am converting it to Julia since it is so much 
more quick and flexible to write.
   


 If so, a Set is the better choice for anything but small arrays, as 
 deleting from the middle of a vector is inefficient for large vectors. 

 If insertion order is important, an OrderedSet (in the DataStructures 
 package) might be useful. It will be slightly slower than a Set, but more 
 efficient than deleting from an array.


Great, thanks -- didn't know about the DataStructures package.
It's often pretty hard to find my way around the julia ecosystem!

Thanks,
David

 


 But without more information, these might all be wrong (or inefficient) for 
 your problem. :-)

 Cheers, Kevin

 On Friday, February 21, 2014, David P. Sanders 
 dpsa...@gmail.comjavascript: 
 wrote:

 If I were to use instead a Set, instead of a Vector, how would this 
 impact performance?



[julia-users] Re: Best data structure to remove elements one by one

2014-02-21 Thread David P. Sanders


El viernes, 21 de febrero de 2014 07:49:27 UTC-6, David P. Sanders escribió:

 Hi,

 Suppose I have a collection of elements that I will create once at the 
 beginining, visit in an unknown order and delete one by one when they are 
 visited.
 What is the most efficient data structure for this in julia?

 I believe this would be an unordered_set (C++) or HashSet (java?), i.e. a 
 Set implemented via a dictionary. (But I am not a computer scientist, so 
 please correct me if I am wrong!)
 Should I in fact, then, just use a Set in julia?

 If, instead, I can add *and* remove elements in a random way, is the 
 answer the same?



OK, I think I have answered my own question: a Set is the good structure.
And to create a set from an array I can do something like

s = Set([3, 4, 5]...)

or

s = Set([i*2 for i in 1:5]...)

which would be the closest thing to a set comprehension?
(The ellipsis is necessary, otherwise a set with a single array object 
inside is created.)

Thanks,
David.

 


 Thanks,
 David.



[julia-users] Re: Best data structure for a dynamic array

2014-02-21 Thread David P. Sanders


El viernes, 21 de febrero de 2014 08:23:56 UTC-6, David P. Sanders escribió:

 Hi,

 I am having trouble finding the best equivalent of a list in Python, and 
 the Pythonic idiom of creating an empty list and then adding elements one 
 by one with .append().
 For example, how would I do this for a collection of ordered pairs (Int64, 
 Int64)? I don't seem able even to create the correct type:

 julia v = Vector{(Int64, Int64)}()
 ERROR: type cannot be constructed

 Then I want to do something like

 push!(v, (1, 2))

 Thanks,
 David.


I found a similar question which I think gives the (or at least an) answer:

julia v = (Int64, Int64)[]
0-element Array{(Int64,Int64),1}

julia push!(v, (1,2))
1-element Array{(Int64,Int64),1}:
 (1,2) 

Sorry for the noise.


[julia-users] Re: Best data structure for a dynamic array

2014-02-21 Thread David P. Sanders


El viernes, 21 de febrero de 2014 08:26:52 UTC-6, Mauro escribió:

 Yes, Julia vectors are basically like python lists.


OK, great, thanks!

David.
 


 julia v=(Int,Int)[]
 0-element Array{(Int64,Int64),1}

 julia push!(v,(1,2))
 1-element Array{(Int64,Int64),1}:
  (1,2)


 On Friday, February 21, 2014 2:23:56 PM UTC, David P. Sanders wrote:

 Hi,

 I am having trouble finding the best equivalent of a list in Python, and 
 the Pythonic idiom of creating an empty list and then adding elements one 
 by one with .append().
 For example, how would I do this for a collection of ordered pairs 
 (Int64, Int64)? I don't seem able even to create the correct type:

 julia v = Vector{(Int64, Int64)}()
 ERROR: type cannot be constructed

 Then I want to do something like

 push!(v, (1, 2))

 Thanks,
 David.



[julia-users] Re: Best data structure for a dynamic array

2014-02-21 Thread David P. Sanders
If I were to use instead a Set, instead of a Vector, how would this impact 
performance? 


Re: [julia-users] Re: Re: Use an array as the index for selecting an element from another array

2014-02-18 Thread David P. Sanders
On Tue, Feb 18, 2014 at 8:15 PM, Eric Davies iam...@gmail.com wrote:

 FYI:

 julia function test_index()
a = rand(100,100);
pos = [1,1];
s = 0.0
for i = 1:1
  s += a[pos[1], pos[2]]
end
end
 test_index (generic function with 1 method)

 julia function test_tuple()
a = rand(100,100);
pos = (1,1);
s = 0.0
for i = 1:1
s += a[pos...]
end
end
 test_tuple (generic function with 1 method)

 julia test_splat(); test_normal(); test_index(); test_tuple();

 julia @time test_splat()
 elapsed time: 0.004122959 seconds (2406892 bytes allocated)

 julia @time test_normal()
 elapsed time: 3.1479e-5 seconds (80128 bytes allocated)

 julia @time test_index()
 elapsed time: 4.3435e-5 seconds (80192 bytes allocated)

 julia @time test_tuple()
 elapsed time: 3.5078e-5 seconds (80128 bytes allocated)


 So if you know the number of dimensions you're indexing, you can gain a
 lot. Alternatively, if you have the option of using a tuple you can save a
 lot there too. This may have to do with the fact that the type of the tuple
 describes its length.


 On Tuesday, 18 February 2014 12:31:52 UTC-6, Aditya Mahajan wrote:

 On Mon, 17 Feb 2014, Stefan Karpinski wrote:

  r[pos...] will do it.

 This is considerable slower than normal array access.

 ~~~
 function test_splat()
  a = rand(100,100);
  pos = [1,1];

  # Just a silly test
  s = 0.0
  for i = 1:1
  s += a[pos...]
  end
 end

 function test_normal()
  a = rand(100,100);
  # Just a silly test
  s = 0.0
  for i = 1:1
  s += a[1,1]
  end
 end

 # Run both functions once
 test_splat()
 test_normal()

 @time test_splat()
 @time test_normal()
 


 Gives
 elapsed time: 0.006395408 seconds (2406892 bytes allocated)
 elapsed time: 1.5434e-5 seconds (80128 bytes allocated)

 Aditya


Many thanks, very interesting.
I was indeed worried about the relative times.

Best,
David.


[julia-users] Use an array as the index for selecting an element from another array

2014-02-17 Thread David P. Sanders
Hi,

Suppose I have the following:

r = rand(5, 5)

I can select a single element of the array r using

r[3, 4]

Now suppose that I have the position [3, 4] stored in a variable as

pos = [3, 4]

How can I use pos as the index for selecting the *single* element r[3, 4]?
I would like to do r[pos], but this returns a *two*-element array, given by 
the elements numbered 3 and 4 in the linear ordering of the array r.

This sounds like a silly question, but I can't find the answer!


Thanks,
David.


[julia-users] Re: Use an array as the index for selecting an element from another array

2014-02-17 Thread David P. Sanders



El lunes, 17 de febrero de 2014 18:57:10 UTC-6, David P. Sanders escribió:

 Hi,

 Suppose I have the following:

 r = rand(5, 5)

 I can select a single element of the array r using

 r[3, 4]

 Now suppose that I have the position [3, 4] stored in a variable as

 pos = [3, 4]

 How can I use pos as the index for selecting the *single* element r[3, 4]?


Of course, one solution is

r[pos[1], pos[2]]

but this is not suitable for a multi-dimensional array.


 

 I would like to do r[pos], but this returns a *two*-element array, given 
 by the elements numbered 3 and 4 in the linear ordering of the array r.

 This sounds like a silly question, but I can't find the answer!


 Thanks,
 David.



[julia-users] Re: Use an array as the index for selecting an element from another array

2014-02-17 Thread David P. Sanders


El lunes, 17 de febrero de 2014 19:03:55 UTC-6, Patrick O'Leary escribió:

 On Monday, February 17, 2014 6:57:10 PM UTC-6, David P. Sanders wrote:

 Hi,

 Suppose I have the following:

 r = rand(5, 5)

 I can select a single element of the array r using

 r[3, 4]

 Now suppose that I have the position [3, 4] stored in a variable as

 pos = [3, 4]

 How can I use pos as the index for selecting the *single* element r[3, 4]?
 I would like to do r[pos], but this returns a *two*-element array, given 
 by the elements numbered 3 and 4 in the linear ordering of the array r.


 You're looking for r[pos...]. The ellipsis operator will splat the 
 elements of pos as a comma-separated list of arguments. 



Many thanks for the 3 super-fast replies! r[pos...] is indeed exactly 
what I was looking for, thanks.
Maybe this could be included in the array docs?

Best,
David.
 


<    1   2   3