[julia-users] Re: Cannot find why Stack Overflow problem appears

2016-11-10 Thread Aleksandr Mikheev
I used debug tools, found out that something doesn't work when I try to 
insert a point into the node which already contains a point, and they both 
should go to the same subnode. For instance,


particles[1] = particle([0.1, 0.1, 0.1], 1.0, [0.0,0.0,0.0], [0.0,0.0,0.0])

tree = InsertParticle(1,1,tree,particles)

particles[2] = particle([0.13, 0.2, 0.15], 1.0, [0.0,0.0,0.0], [0.0,0.0,0.0
])

tree = InsertParticle(2,1,tree,particles)



leads to infinite loop. However, I cannot see the reason. 



[julia-users] Cannot find why Stack Overflow problem appears

2016-11-10 Thread Aleksandr Mikheev
I am trying to make an octree for randomly distributed set of particles 
. I have been trying to find where 
Stack Overflow Error appears whole day, but it was in vain. Could you 
please help me? The problem should be somewhere in (c == [0]) && (a > 0) part, 
however, I have no idea where. Thank you in advance!

type particle
  R::Vector{Float64}  # coordinates of the particle
  m::Float64  # mass of the particle
  a_tree::Float64 # acceleration of particle calculated from 
tree
  a_exact::Float64# acceleration of particle calculated by 
exact summation
end

type node
  parent::Int64   # node's parent number
  children::Vector{Int64} # set of childrens' numbers
  l::Float64  # size of the node
  R::Vector{Float64}  # radius of geometric center
  m::Float64  # total mass of the node
  ptcl::Int64 # pointer to the particle in it
end

function problem3_2(N::Int64)
particles = Vector{particle}(N) # Basically, our system
tree = Vector{node}(1)  # Initial tree (root)
tree[1] = node(0,[0],1.0,[0.0,0.0,0.0],0.0,0)

for i in eachindex(particles)
  particles[i] = particle([rand() - 1/2, rand() - 1/2, rand() - 1/2], 1.0, 
0.0, 0.0)
  tree = InsertParticle(i,1,tree,particles)
end

return tree
end

function InsertParticle(i::Int64,k::Int64,tree,particles)
  c = tree[k].children
  a = tree[k].ptcl
  if (c == [0]) && (a == 0)
tree[k].ptcl = i
  elseif (c == [0]) && (a > 0)
j_1 = length(tree) + DetermOctant(particles[tree[k].ptcl],tree[k])
j_2 = length(tree) + DetermOctant(particles[i],tree[k])
tree[k].children = [x for x in length(tree)+1:length(tree)+8]
  for p = 1:8
push!(tree,node(k,[0],tree[k].l/2,[0.0,0.0,0.0],0.0,0))
if (p == 1) || (p == 4) || (p == 5) || (p == 8)
  tree[k+p].R[1] = tree[k].R[1] + tree[k+p].l/2
else
  tree[k+p].R[1] = tree[k].R[1] - tree[k+p].l/2
end
if (p == 1) || (p == 2) || (p == 5) || (p == 6)
  tree[k+p].R[2] = tree[k].R[2] + tree[k+p].l/2
else
  tree[k+p].R[2] = tree[k].R[2] - tree[k+p].l/2
end
if (p == 1) || (p == 2) || (p == 3) || (p == 4)
  tree[k+p].R[3] = tree[k].R[3] + tree[k+p].l/2
else
  tree[k+p].R[3] = tree[k].R[3] - tree[k+p].l/2
end

end

InsertParticle(tree[k].ptcl,j_1,tree,particles)
InsertParticle(i,j_2,tree,particles)
tree[k].ptcl = 0
  elseif (c != [0])
  j = DetermOctant(particles[i],tree[k])
  InsertParticle(i,tree[k].children[j],tree,particles)
  end
return tree
end


function DetermOctant(x::particle,y::node)
  c1 = y.R[1]; c2 = y.R[2]; c3 = y.R[3]
  x1 = sign(x.R[1] - c1); x2 = sign(x.R[2] - c2); x3 = sign(x.R[3] - c3)
  if (x1 > 0) && (x2 > 0) && (x3 > 0)
n = 1
  elseif (x1 < 0) && (x2 > 0) && (x3 > 0)
n = 2
  elseif (x1 < 0) && (x2 < 0) && (x3 > 0)
n = 3
  elseif (x1 > 0) && (x2 < 0) && (x3 > 0)
n = 4
  elseif (x1 > 0) && (x2 > 0) && (x3 < 0)
n = 5
  elseif (x1 < 0) && (x2 > 0) && (x3 < 0)
n = 6
  elseif (x1 < 0) && (x2 < 0) && (x3 < 0)
n = 7
  else
n = 8
  end
return n
end




[julia-users] Re: Input a data from the console

2016-10-27 Thread Aleksandr Mikheev
Okay, I accidentally figured the answer for the P.S. by myself. 

четверг, 27 октября 2016 г., 18:16:25 UTC+3 пользователь Aleksandr Mikheev 
написал:
>
> Hello,
>
> How could I input a data from the console? For instance, I would like to 
> make such that user is able to input the value of x. Is there any way to do 
> it like in Fortran or something? I can't find anything in documentation.
>
> P.S. Also, I believe there is a way to input a string using readline() 
> function. However, if I do something like:
>
> a = readline()
> "asd"
>
> then I will get "\"asd\"\r\n".
>
> How to avoid these excess symbols?
>
> Thank you in advance!
>


[julia-users] Input a data from the console

2016-10-27 Thread Aleksandr Mikheev
Hello,

How could I input a data from the console? For instance, I would like to 
make such that user is able to input the value of x. Is there any way to do 
it like in Fortran or something? I can't find anything in documentation.

P.S. Also, I believe there is a way to input a string using readline() 
function. However, if I do something like:

a = readline()
"asd"

then I will get "\"asd\"\r\n".

How to avoid these excess symbols?

Thank you in advance!


Re: [julia-users] Re: Binary read

2016-10-26 Thread Aleksandr Mikheev
Unfortunately, I do not. However, I am most definitely sure that everything 
is okay with file since it is a training file.

среда, 26 октября 2016 г., 23:17:00 UTC+3 пользователь Stefan Karpinski 
написал:
>
> Do you have an example where you have the file and you know what the data 
> should be? You may want to look at the data in a hex editor and try to 
> figure out what's going on.
>
> On Wed, Oct 26, 2016 at 4:06 PM, Aleksandr Mikheev  > wrote:
>
>> So, to avoid the opening a new thread I will write here again. It seems I 
>> still have a problem which I cannot solve. I use 
>>
>> a_million_floats = read(f, Float64, 1_000_000)
>>
>>
>> as it was suggested, however the numbers I get are obviously wrong (there 
>> are numbers like 8.68e+272, -5.39e-142, and even NaN). I am pretty that 
>> these are not the right numbers. Also, I am pretty sure that the type is 
>> correct (it should be 'double' in C/C++). Furthermore, I have i5 CPU, so it 
>> should be little endian as it is supposed in .dat file. What could be wrong?
>>
>> Thanks in advance!
>>
>
>

[julia-users] Re: Binary read

2016-10-26 Thread Aleksandr Mikheev
So, to avoid the opening a new thread I will write here again. It seems I 
still have a problem which I cannot solve. I use 

a_million_floats = read(f, Float64, 1_000_000)


as it was suggested, however the numbers I get are obviously wrong (there 
are numbers like 8.68e+272, -5.39e-142, and even NaN). I am pretty that 
these are not the right numbers. Also, I am pretty sure that the type is 
correct (it should be 'double' in C/C++). Furthermore, I have i5 CPU, so it 
should be little endian as it is supposed in .dat file. What could be wrong?

Thanks in advance!


Re: [julia-users] Binary read

2016-10-25 Thread Aleksandr Mikheev
Thank you very much, Michele! One more question, if you do not mind. If I 
do something like this:

f = open("numbers.dat")

a = read(f, Int32)

a = read(f, Int32) 

a = read(f, Int32)


then a is different each time. I believe I read something about that you 
should use command close() each you opened something. Is this correct? 


вторник, 25 октября 2016 г., 23:11:01 UTC+3 пользователь Michele Zaffalon 
написал:
>
> You open the file in the correct way. To read the integer, do read(f, 
> Int32) followed by read(f, Float64, 1_000_000) to read the million floats. 
> See the manual at 
> http://docs.julialang.org/en/release-0.5/stdlib/io-network/
>
> On Tue, Oct 25, 2016 at 10:05 PM, Aleksandr Mikheev  > wrote:
>
>> Hello, sorry if this question have already been asked, but I could not 
>> find a similar thread. So, I have a .dat ("numbers.dat") file, which I 
>> should open. I believe I should do something like this: 
>>
>> f = open("numbers.dat")
>>
>>
>> And after that I tried to read it:
>>
>> readlines(f)
>>
>>
>> However, Julia writes the Error: "UnicodeError: invalid character 
>> index". My tutor told me that this is a binary file, so what I need is a 
>> binary read, such as fread in C/C++. I tried to read  the documentation, 
>> tried to google, but still cannot understand what should I do. Also, a 
>> quote from my tutor, which can probably help you to understand the problem 
>> better: "The first 4 bytes contain an integer (in this case, 1,000,000) 
>> and then 1,000,000 floats of 8 bytes each".
>>
>> Thank you in advance!
>>
>
>

[julia-users] Binary read

2016-10-25 Thread Aleksandr Mikheev
Hello, sorry if this question have already been asked, but I could not find 
a similar thread. So, I have a .dat ("numbers.dat") file, which I should 
open. I believe I should do something like this: 

f = open("numbers.dat")


And after that I tried to read it:

readlines(f)


However, Julia writes the Error: "UnicodeError: invalid character index". 
My tutor told me that this is a binary file, so what I need is a binary 
read, such as fread in C/C++. I tried to read  the documentation, tried to 
google, but still cannot understand what should I do. Also, a quote from my 
tutor, which can probably help you to understand the problem better: "The 
first 4 bytes contain an integer (in this case, 1,000,000) and then 
1,000,000 floats of 8 bytes each".

Thank you in advance!


[julia-users] Adding element to array and filtering multi-dimensional arrays.

2016-02-22 Thread Aleksandr Mikheev
Hi everyone. I have two simple questions, answer to which I cannot find by 
myself.

1. Is there any way to add an element to array? For example, I have 
Array{...}(n,m) or Vector{...}(n). And I'd like to expand it, i.e. I want 
to have now Array{n+1,m} or Vector{...}(n+1). How can I do this?

2. Imagine I have an array [1 2 3 4 5 6 7 8 9 10; 12 4 5 0 2 0 45 8 0 7]. 
Is there any easy way to remove all columns with 0 in second line? 
Something like filter!(...)? 

Sorry for my bad English. Thank you in advance.


Re: [julia-users] Using operator 'for' in loops with removing elements.

2016-02-09 Thread Aleksandr Mikheev
The problem is that condition is a bit more complicated acctually. I have 
an array of composite type elements and condition includes not only current 
i-th element's properties, but also some other elements. Moreover, for some 
purpose I need to swap my i-th element with last element before removing it 
(so basicly every time I remove the last element). So I guess removing 
elements one-by-one is my only option. But thank you for your advices, I 
will certainly use them in future!

вторник, 9 февраля 2016 г., 19:37:36 UTC+3 пользователь Milan Bouchet-Valat 
написал:
>
> Le mardi 09 février 2016 à 08:04 -0800, Aleksandr Mikheev a écrit : 
> > Hi everyone.  
> > 
> > Suppose I have the array called a. And I need to check all of its 
> > ellements on the some condition. Further, I need to remove those 
> > ellements which satisfy the condition. I know I can do this using 
> > operator 'while'. Something like this: 
> > 
> > i = 0 
> > while (i < length(a)) 
> > i = i + 1 
> > if (condition on a[i]) 
> > splice!(a, i) 
> > i = i - 1 
> > end 
> > end 
> > 
> > But I've heard that using operator 'while' is quite slower than using 
> > 'for'-loop. So is there any better way to do what I need to do? 
> I don't know where you've heard that, but that's wrong in general. In 
> the present case, what's likely going to be slow is removing elements 
> one-by-one, which involves moving all following elements repeatedly. A 
> faster strategy would be to copy elements to retain, which is what 
> filter!() does in Base: 
> https://github.com/JuliaLang/julia/blob/4895ef64fb1a3c2f0ac3e073b2f236f 
> 5e603d536/base/array.jl#L870 
> <https://github.com/JuliaLang/julia/blob/4895ef64fb1a3c2f0ac3e073b2f236f5e603d536/base/array.jl#L870>
>  
>
> You can use filter!() instead of writing the loop yourself. For 
> example, if the condition is > 1, do: 
> a = [1, 2, 3] 
> filter!(x->x > 1, a) 
>
> But anonymous functions are slow in 0.4 (fixed in 0.5), so you would need 
> to do: 
> f(x) = x > 1 
> filter!(f, a) 
>
>
> You can also use indexing if you don't mind taking a copy: 
> cond = Bool[x > 1 for x in a] 
> a[cond] 
>
> Finally, if the condition only involves arithmetic operators with 
> element-wise versions, you can also write: 
> a[a .> 1] 
>
>
>
> Regards 
>


[julia-users] Using operator 'for' in loops with removing elements.

2016-02-09 Thread Aleksandr Mikheev
Hi everyone. 

Suppose I have the array called a. And I need to check all of its ellements 
on the some condition. Further, I need to remove those ellements which 
satisfy the condition. I know I can do this using operator 'while'. 
Something like this:

i = 0
while (i < length(a))
i = i + 1
if (condition on a[i])
splice!(a, i)
i = i - 1
end
end

But I've heard that using operator 'while' is quite slower than using 
'for'-loop. So is there any better way to do what I need to do?

Sorry for bad English. Thank you in advance.


[julia-users] Re: Arrays of arrays.

2015-11-29 Thread Aleksandr Mikheev
Thank you, guys. I guess this
[Int[] for i = 1:3, j=1:3, k=1:3]
is exactly what I need. At the same time, now I understand that I should 
read more about creating and filling arrays. :) Once again, thanks.



[julia-users] Arrays of arrays.

2015-11-29 Thread Aleksandr Mikheev
Hi all. Once again I have some questions about Julia.

I know that there is a possibility to create a list of arrays. For exmple:

s = fill(Array(Int64,1),4)

And then I can do something like this:

s[1] = [1; 2]
s[1] = [s[1]; 5]

By parity of reasoning I did this:

s = fill(Array(Int64,1),4,4,4)

And it worked fine. But in both cases I had initial elements in s (like 
when I construct arrays with Array{Int64}(m,n)):

julia> s = fill(Array(Int64,1),3,3,3)
3x3x3 Array{Array{Int64,1},3}:
[:, :, 1] =
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]


[:, :, 2] =
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]


[:, :, 3] =
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]
 [2221816832]  [2221816832]  [2221816832]


Is there something I could do to prevent this? I know I could easily fix it 
by:


for i = 1:3
for j = 1:3
for k = 1:3
s[i,j,k] = []
end
end
end

But I guess this is a weird solution.

Thank you in advance!



[julia-users] Re: Some questions about types and removing elements.

2015-11-28 Thread Aleksandr Mikheev
Oh, really thank you, Eric. Now I see. One additional question, if possible.

If I use something like this:

type A
  A1::Int64
  A2::Int64
end

I can expect that one element of type A will occupy 128 (2*64) bits. But 
will happen in this situation?

type A{T<:Integer}
A1::T
A2::T

end



I mean, if I made

  
s = Vector{A}(N)

, how much space would it cost me?

Thank you in advance. 


[julia-users] Re: Some questions about types and removing elements.

2015-11-28 Thread Aleksandr Mikheev
Oh, really thank you, Eric. Now I see. One additional question, if possible.

If I use something like this:

type A
> A1::Int64
> A2::Int64
> end


I can expect that one element of type A will occupy 128 (2*64) bits. But 
will happen in this situation?

type A{T<:Integer}
A1::T
A2::T
end

I mean, if I made s = Vector{A}(N), how much space would it cost me?

Thank you in advance. 


[julia-users] Some questions about types and removing elements.

2015-11-28 Thread Aleksandr Mikheev
Hi all. I have 3 questions.


1. I still don't understand what exactly parametric types do. I mean what 
is the reason to use 

type A{Int64}
> A1::Int64
> A2::Int64
> end

 
instead of

type A
> A1::Int64
> A2::Int64
> end

?
Or am I misunderstanding something?

2. How should I use types like Int16 or Float16 correctly? I have some 
function 

GeometryFunc(L::Int16,phi::Float16,distribution::Array{Float16,2}) 


But when I try to input something like this:

GeometryFunc(100, 0.3, [0.95 1.01 1.03; 0.25 0.5 0.25])


I have got the error. 

ERROR: MethodError: 'GeometryFunc' has no method match no method matching 
> GeometryFunc(::Int64, ::Float64, ::Array{Float64,2})


I guess there are some problems with data I try to input. Or should I try 
to search something in the code?

3. How can I delete elements with specific indexes in the array? Or how can 
I delete all elements, except ones with specific indexes (this is what I am 
trying to do actually)? For example, I have s = [2, 4, 3, 6, 8, 9, 11] and 
I want to delete 3rd, 5th and 6th elements (or save 1st, 2nd, 4th and 7th). 
I know I could do something like:

indx = [3, 5, 6]

for i = 1:1:length(indx)

splice!(s,indx[i])

end

 
but is there any easier way? The closest thing I found was:

splice(s, k:m)

which removes all elements with indexes from k to m., but this is not what 
I want exactly. 


[julia-users] Some questions about types, and removing elements.

2015-11-28 Thread Aleksandr Mikheev


Hi all. I have 3 questions.


1. I still don't understand what exactly parametric types do. I mean what 
is the reason to use 

type A{Int64}
> A1::Int64
> A2::Int64
> end

 
instead of

type A
> A1::Int64
> A2::Int64
> end

?
Or am I misunderstanding something?

2. How should I use types like Int16 or Float16 correctly? I have some 
function 

GeometryFunc(L::Int16,phi::Float16,distribution::Array{Float16,2}) 


But when I try to input something like this:

GeometryFunc(100, 0.3, [0.95 1.01 1.03; 0.25 0.5 0.25])


I have got the error. 

ERROR: MethodError: 'GeometryFunc' has no method match no method matching 
> GeometryFunc(::Int64, ::Float64, ::Array{Float64,2})


I guess there are some problems with dad I try to input. Or should I try to 
search something in the code?

3. How can I delete elements with specific indexes in the array? Or how can 
I delete all elements, except ones with specific indexes (this is what I am 
trying to do actually)? For example, I have s = [2, 4, 3, 6, 8, 9, 11] and 
I want to delete 3rd, 5th and 6th elements (or save 1st, 2nd, 4th and 7th). 
I know I could do something like:

indx = [3, 5, 6]

for i = 1:1:length(indx)

splice!(s,indx[i])

end

 
but is there any easier way? The closest thing I found was:

splice(s, k:m)

which removes all elements with indexes from k to m., but this is not what 
I want exactly. 


[julia-users] Re: Easiest way to get indexes of elements that satisfy a certain condition

2015-11-25 Thread Aleksandr Mikheev
Oh, thank you, didn't know that.


[julia-users] Easiest way to get indexes of elements that satisfy a certain condition

2015-11-25 Thread Aleksandr Mikheev
Hi all,

So imagine I have an array. For example:

s = [1 2 5 7 3 3]


Also, I have x = 4. And I would like to have an array of indexes 
of elements s that satisfy:

s .< x

 
In Fortran or MATLAB I would do something like this:

indx = 1:1:size(s) (or length(s) in MATLAB)

And then:

indx = pack(indx,s>x)

 
or 

indx=indx(s < x)

 
In Julia I tried something like:

indx = indx.*(s .< x)


But I had a matrix in output for some reason. So what is the easiest 
solution in this situation?



[julia-users] Can't understand what did I break.

2015-11-24 Thread Aleksandr Mikheev
So some time ago I wrote a little code and it worked fine. Then I did some 
experiments, but I change everything back after that. However, it seems 
that something is broken in my code. I spent at least an hour to figure 
whats going on, but still can't figure it out. Can someone help me? Here is 
my code:

module Geometry
> export GeometryFunc
> type pores{Float64}
> R::Float64
> coords::Vector{Float64}
> end
> function GeometryFunc(L,phi,distribution)
>   V_avg = (4*pi/3)*sum((distribution[1,:].^3).*distribution[2,:])
>   N = round(Int,(L^3*log(1/(1-phi)))/V_avg)
>   lattice = Vector{pores}(N)
>   R_max = maximum(distribution[1,:])
>   for j=1:1:N
>   lattice[j]=pores(0.0,[0.0,0.0,0.0])
>   end
> for i=1:1:N
> lattice[i].coords = rand(3)*L
> Q = rand()
> if Q < distribution[2,1]
> lattice[i].R = distribution[1,1]
> else
> tempsum1 = 0
> tempsum2 = distribution[2,1]
> for k = 2:1:length(distribution[1,:])
> tempsum1 = tempsum1 + distribution[2,k-1]
> tempsum2 = tempsum2 + distribution[2,k]
> if Q < tempsum2 && Q > tempsum1
> lattice[i].R = distribution[1,k]
> end
> end
> end
> end
> return lattice, R_max, N
> end
> end


And here is how I usually call it:

lattice=Geometry.GeometryFunc(100,0.3,[0.95 0.97 0.99 1.01 1.03 1.05; 0.07 
> 0.10 0.25 0.40 0.10 0.08])


After running the code I can some constructions like:

Geometry.pores{Float64}(0.95, 
[96.62765023884106,36.948114669490664,70.08384291700011])


How it is even possible?



[julia-users] Re: What is the best way to delete unwanted subgraph?

2015-11-24 Thread Aleksandr Mikheev
It works! Thank you.


[julia-users] Re: What is the best way to delete unwanted subgraph?

2015-11-23 Thread Aleksandr Mikheev
Really sorry for silly questions, but I am really new to Julia language and 
also I am not very good in coding generally.


[julia-users] Re: What is the best way to delete unwanted subgraph?

2015-11-23 Thread Aleksandr Mikheev
Excuse me once again. How can I use rem_vertex!() now? I tried to update my 
package - didn't help. I tried to do a new file (called GraphFeatures), in 
witch I copied all the functions from here 

 - 
but it does not understand standart Graph type from LightGraphs:

ERROR: MethodError: `rem_vertex!` has no method matching 
> rem_vertex!(::LighGraphs.Graph, ::Int64)
> Closest candidates are:
>   rem_vertex!(::Union{GraphFeatures.DiGraph,GraphFeatures.Graph}, ::Int64)


 What should I do? 


[julia-users] What is the best way to delete unwanted subgraph?

2015-11-23 Thread Aleksandr Mikheev
Hi all,

I'm currently using LightGraphs package in my student research work. And I 
have some problems with it. Imagine we have a undirrected graph G, which 
contains, for example, 10 vertices and some edges. I would like to know 
which components of graph G are connected, so I call 
"connected_components(G)". Suppose I have this situation:

julia> connected_components(G)
6-element Array{Array{Int64,1},1}:
[1,2,3]
[4,7]
[5]
[6]
[8,9]
[10]

And now I would like to delete all subgraphs, except [1,2,3]. In other 
words, I would like to have subgraph [1,2,3] as a graph G further. Is there 
any effective methods to do this in LightGraphs or in any other packages? I 
mean, I can delete vertices one by one (I guess I saw this function 
somewhere in GitHub), but that would be pretty slow, I imagine.

Thank you in advance.