Julia: check whether array entry is undef - julia

What is the best way in Julia to check whether an array entry is #undef?
Example:
julia> a = Array(Vector,2)
julia> isdefined(a[1]) # fails
julia> isempty(a[1]) # fails

You can push the access into isdefined by using isdefined(a, 1) instead of isdefined(a[1]):
julia> a = Array(Vector,2);
julia> a[2] = {10}
1-element Array{Any,1}:
10
julia> a
2-element Array{Array{T,1},1}:
#undef
{10}
julia> isdefined(a[1])
ERROR: access to undefined reference
in getindex at array.jl:246
julia> isdefined(a, 1)
false
julia> isdefined(a, 2)
true

Related

How do I check if an array is empty in Julia?

I am trying to see if there's a handy way to check if an array in Julia is empty or not.
In Julia you can use the isempty() function documented here.
julia> a = []
0-element Array{Any,1}
julia> isempty(a)
true
julia> length(a)
0
julia> b = [1]
1-element Array{Int64,1}:
1
julia> isempty(b)
false
Note that I included the length check as well in case that will help your use case.
For arrays one can also simply use a == []. The types are ignored in this comparison (as usual).
julia> a = []
a == []
0-element Array{Any,1}
julia> a == []
true
julia> a == Int[]
true
julia> String[] == Int[]
true
From Julia help:
isempty determines whether a collection is empty (has no elements).
e.g.
julia> isempty([])
true
julia> isempty(())
true

Julia--unrecognized subtype of nested unionall

While writing a function with the following signature,
f(x::Vector{Tuple{Vector{<:Real}, Vector{<:Real}}})
I ran into an error I do not understand. I tried calling this function f on z, defined as follows:
z = [([1,2], [3,4])]
(This is an array of tuples, where each tuple contains two arrays of real numbers; z as defined above only contains one such tuple.)
The type of z is
Array{Tuple{Array{Int64,1},Array{Float64,1}},1}
(as found by calling typeof(z)). I had expected this to be a subtype of
Vector{Tuple{Vector{<:Real}, Vector{<:Real}}}
, the type in the function f above.
However, when I run the code
z::Vector{Tuple{Vector{<:Real}, Vector{<:Real}}}
I see the following error:
ERROR: TypeError: in typeassert, expected Array{Tuple{Array{#s6,1} where #s6<:Real,Array{#s5,1} where #s5<:Real},1}, gotArray{Tuple{Array{Int64,1},Array{Float64,1}},1}
Likewise, I get a method error when calling f(z). Why isn't Array{Tuple{Array{Int64,1},Array{Int64,1}},1} a subtype of Vector{Tuple{Vector{<:Real}, Vector{<:Real}}}?
The reason is that:
julia> Tuple{Array{Int64,1},Array{Int64,1}} <: Tuple{Vector{<:Real}, Vector{<:Real}}
true
but clearly:
julia> Tuple{Array{Int64,1},Array{Int64,1}} >: Tuple{Vector{<:Real}, Vector{<:Real}}
false
and becasue types in Julia (except a Tuple, but here we have a Vector) are invariant (see here), you have that Vector{S} is not as subtype of Vector{T} even if S <: T.
so you need to write one additional subtyping qualification:
f(x::Vector{<:Tuple{Vector{<:Real}, Vector{<:Real}}})
and similarly:
julia> z::Vector{<:Tuple{Vector{<:Real}, Vector{<:Real}}}
1-element Array{Tuple{Array{Int64,1},Array{Int64,1}},1}:
([1, 2], [3, 4])
or extract the parameter using where:
julia> f2(x::Vector{Tuple{Vector{T}, Vector{T}}}) where {T<:Real} = x
f2 (generic function with 1 method)
julia> f2(z)
1-element Array{Tuple{Array{Int64,1},Array{Int64,1}},1}:
([1, 2], [3, 4])
or
julia> f3(x::Vector{Tuple{Vector{T}, Vector{S}}}) where {T<:Real, S<:Real} = x
f3 (generic function with 1 method)
julia> f3(z)
1-element Array{Tuple{Array{Int64,1},Array{Int64,1}},1}:
([1, 2], [3, 4])
(choose the first of second form against your decision if two elements of the tuple must have the same type or not)

Assign atoms in the Earth to a variable in Julia

In Python 3.6, the following works to assign a variable an estimate of the atoms in the Earth:
In[6]: atoms_in_earth = 10**50
In[7]: atoms_in_earth
Out[7]: 100000000000000000000000000000000000000000000000000
However, the following does not work in Julia 1.0.0:
julia> atoms_in_earth = 10^50
-5376172055173529600
julia> atoms_in_earth = BigInt(10^50)
-5376172055173529600
julia> atoms_in_earth = BigFloat(10^50)
-5.3761720551735296e+18
julia> atoms_in_earth = big(10^50)
-5376172055173529600
julia> atoms_in_earth = big"10^50"
ERROR: ArgumentError: invalid number format 10^50 for BigInt or BigFloat
Stacktrace:
[1] top-level scope at none:0
I was able to get these methods to work:
julia> atoms_in_earth = big"1_0000000000_0000000000_0000000000_0000000000_0000000000"
100000000000000000000000000000000000000000000000000
julia> float(ans)
1.0e+50
julia> atoms_in_earth = parse(BigInt, '1' * '0'^50)
100000000000000000000000000000000000000000000000000
julia> float(ans)
1.0e+50
I am hoping there is an easier way to do this in Julia.
What am I missing?
Julia uses native integers by default, and these can overflow. Python uses big integers by default (of arbitrary precision, it's size limit depend of the amount of available memory) and does not overflow (but is slower because of this).
Your first example overflows for Int64:
julia> atoms_in_earth = 10^50
-5376172055173529600
Your second, third and fourth examples, have already overflown, before you converted them to bigs:
julia> atoms_in_earth = BigInt(10^50)
-5376172055173529600
julia> atoms_in_earth = BigFloat(10^50)
-5.3761720551735296e+18
julia> atoms_in_earth = big(10^50)
-5376172055173529600
Your fifth example is not a valid big literal:
julia> atoms_in_earth = big"10^50"
ERROR: ArgumentError: invalid number format 10^50 for BigInt or BigFloat
But you can create a small number as a BigInt, in your example 10 and any further operations will be promoted to big arithmetic, form then on:
julia> x = 10
10
julia> typeof(x)
Int64
julia> x = BigInt(10)
10
julia> typeof(x)
BigInt
julia> big(10) == big"10" == big(10)
true
julia> y = x^50
100000000000000000000000000000000000000000000000000
julia> typeof(y)
BigInt
In this case 50 gets promoted to a BigInt as well, before computing x^50 and thus yielding a BigInt in the end.
julia> BigInt(10)^50 == big"10"^50 == big(10)^50
true
This works:
julia> atoms_in_earth = big"1e50"
1.0e+50
julia> typeof(ans)
BigFloat

What functions are called to display an (Array) variable on the julia REPL?

Say I enter:
julia> X = randn(3,4)
3x4 Array{Float64,2}:
-0.862092 0.78568 0.140078 -0.0409951
-0.157692 0.0838577 1.38264 -0.296809
1.40242 -0.628556 -0.500275 0.258898
What functions were called to produce the output given?
Note that overloading Base.show does not seem to be sufficient to change this behaviour, so I'm unsure where to go.
julia> Base.show(io::IO, A::Array{Float64, 2}) = println("Humbug")
show (generic function with 120 methods)
julia> X
3x4 Array{Float64,2}:
-0.862092 0.78568 0.140078 -0.0409951
-0.157692 0.0838577 1.38264 -0.296809
1.40242 -0.628556 -0.500275 0.258898
Is it perhaps the case that I would have to change Base/array.jl source code and rebuild julia before such a change would work? Note the difference between this and a user defined type:
julia> type foo
x::Float32
s::ASCIIString
end
julia> ob = foo(1., "boo")
foo(1.0f0,"boo")
julia> Base.show(io::IO, someob::foo) = print("Humbug!")
show (generic function with 123 methods)
julia> ob
Humbug!
well, you should overload display():
julia> Base.display(A::Array{Float64, 2}) = println("Humbug")
display (generic function with 11 methods)
julia> X
Humbug
you can find the definition in REPL.jl.

Julia: Assignment in Arrays

When indexing more than one level for an array, it works fine. But when I used it to assign values, it did not. Does anyone know why A does not change below?
In [4]: A = rand(6)
Out [4]: 6-element Array{Float64,1}:
0.111552
0.155126
0.78485
0.147477
0.362078
0.959022
In [5]: A[3:5][[true,false,true]]
Out [5]: 2-element Array{Float64,1}:
0.78485
0.362078
In [6]: A[3:5][[true,false,true]] = [99, 999]
Out [6]: 2-element Array{Int64,1}:
99
999
In [7]: A
Out [7]: 6-element Array{Float64,1}:
0.111552
0.155126
0.78485
0.147477
0.362078
0.959022
This is because indexing arrays by ranges and vectors returns a new array with the output (instead of a view into the original array). Your statement is equivalent to the following:
julia> A = rand(6)
6-element Array{Float64,1}:
0.806919
0.445286
0.882625
0.556251
0.719156
0.276755
julia> B = A[3:5]
3-element Array{Float64,1}:
0.882625
0.556251
0.719156
julia> B[[true,false,true]] = [99, 999]
2-element Array{Int64,1}:
99
999
julia> A'
1x6 Array{Float64,2}:
0.806919 0.445286 0.882625 0.556251 0.719156 0.276755
julia> B'
1x3 Array{Float64,2}:
99.0 0.556251 999.0
You can actually see that this is what Julia is doing through some of its expression utilities. Note the explicit parentheses — it's calling setindex! on the result of indexing, which has made a copy. (GenSym() is an internal way of specifying a temporary variable):
julia> :(A[3:5][[true,false,true]] = [99, 999])
:((A[3:5])[[true,false,true]] = [99,999])
julia> expand(:(A[3:5][[true,false,true]] = [99, 999]))
:(begin
GenSym(0) = (top(vect))(99,999)
setindex!(getindex(A,colon(3,5)),GenSym(0),(top(vect))(true,false,true))
return GenSym(0)
end)
The goal is to eventually have all array indexing return views instead of copies, but that's still a work-in-progress.

Resources