[ACCEPTED]-Julia Initialized Array / Vector is not zero but random-julia
@waTeim is correct that when allocating 6 an array it is not initialized to 0 or any 5 specific value.
The way to allocate and initialize 4 a new array with a specific value in Julia 3 is with fill()
so for your b
you would do:
b = fill(2.0, 10)
This 2 gives you:
10-element Array{Float64,1}:
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
Or if you want a row vector:
b = fill(2.0, 1, 10)
Which 1 gives you
1x10 Array{Float64,2}:
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
Array Allocation
It appears that array allocation does not 27 initialize when the element type is unboxed (see 26 below). This happens at a very low level 25 in the system and is by the way is part 24 of the API when Julia is embedded in another program. At 23 the lowest level in the source code for 22 Julia object allocation is performed by 21 the function allocobj.
DLLEXPORT void *allocobj(size_t sz)
{
#ifdef MEMDEBUG
return alloc_big(sz);
#endif
if (sz > 2048)
return alloc_big(sz);
return pool_alloc(&pools[szclass(sz)]);
}
The function alloc_big is essentially 20 a call to malloc which does not initialize the 19 memory it allocates, while pool_alloc is a function 18 that acquires memory from set of free lists 17 managed by Julia and likewise also does 16 not initialize. This makes sense because 15 here is time that would likely be wasted 14 because the program will usually turn around 13 and initialize this to something program 12 specific anyway.
However, there is an exception 11 in the case of arrays, the function _new_array_ contains
if (!isunboxed)
memset(data, 0, tot);
The 10 value of this variable is determined from 9 this function. To be stored unboxed in an array, elements 8 must be leaf, immutable, non-pointer-containing 7 types.
static inline int store_unboxed(jl_value_t *el_type)
{
return jl_is_datatype(el_type) && jl_is_leaf_type(el_type) && jl_is_immutable(el_type) && jl_is_pointerfree((jl_datatype_t*)el_type);
}
The primitive types Bool, Int64, Float64, etc are 6 classified immutable as well as leaf (non-abstract) and 5 of course non-pointer and so will be stored 4 unboxed, and therefore not initialized.
julia> Float64.mutable
false
Further, immutable 3 composite types appears to also trigger 2 this behavior.
julia> immutable type Foo bar::Int end
julia> z=Array(Foo,1)
1-element Array{Foo,1}:
Foo(0)
julia> z=Array(Foo,1)
1-element Array{Foo,1}:
Foo(-4)
julia> z=Array(Foo,1)
1-element Array{Foo,1}:
Foo(4432839632)
Const Vector Initialization
Likely you want something 1 like this:
julia> const C = [2, 2, 2, 2.0, 2, 2, 2, 2, 2, 2]
10-element Array{Float64,1}:
2.0
2.0
...
2.0
julia> D = [ 1 ];
julia> C = D;
ERROR: invalid redefinition of constant C
A simple way to do it is as follows:
julia> zeros(3)
3-element Vector{Float64}:
0.0
0.0
0.0
or for 1 ones:
julia> ones(3)
3-element Vector{Float64}:
1.0
1.0
1.0
or for some particular value:
julia> ones(3)*2
3-element Vector{Float64}:
2.0
2.0
2.0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.