Julia: UndefVarError: randperm not defined - julia

I am currently staring with Julia 1.0.0 under Windows 10. When using a function randperm(n) where n is an integer number, I got an error message:
ERROR: UndefVarError: randperm not defined.
Presumably, I have not loaded a library that contains this function. So, could anyone please tell me which library should I load so that I can use that function? Thank you in advance!
Nha

You need to load standard library Random, e.g.:
julia> using Random
julia> randperm(10)
10-element Array{Int64,1}:
6
3
7
10
2
8
5
9
4
1

Related

Easy way to stack vectors of vectors in Julia

I would like a way to programmatically "deconstruct" a vector of variable-length vectors in Julia. I do not care about the resulting vector's order.
For example, suppose that my vector of vectors is A = [[1], [2,3], [4,5,6]]. I can deconstruct A by writing vcat(A[1], A[2], A[3]), which returns [1,2,3,4,5,6]. However, if the length of A is large, then this approach becomes cumbersome. Is there a better, more scalable way to obtain the same result?
Try Iterators.flatten:
julia> collect(Iterators.flatten(A))
6-element Vector{Int64}:
1
2
3
4
5
6
(This yields a lazy representation hence I collected this before showing the output)
While I would second Przemyslaw's answer for any situation where you can get away with using a lazy representation, maybe a more direct answer to your question is:
julia> vcat(A...)
6-element Vector{Int64}:
1
2
3
4
5
6
whenever you feel the need to type out all elements of a collection as function arguments, splatting ... is your friend.
Splatting can however negatively impact performance, so it is generally recommended to use reduce, which has a specialisation for vcat:
julia> reduce(vcat, A)
6-element Vector{Int64}:
1
2
3
4
5
6

RREF not defined in Julia

I just install Julia, along with LinearAlgebra package.
When I type
A = [1 2 3 4; 4 5 6 7]
rref(A)
My terminal said
ERROR: UndefVarError: rref not defined
Stacktrace:
[1] top-level scope at REPL[22]:1
I am sort of confused. Do I need to install certain package in order to use reef function?
There s no such function in the LinearAlgebra package. There used to be a long time ago, but is has since moved to https://github.com/blegat/RowEchelon.jl.

Why Array{Float64,1} is not a subtype of Array{Real,1} in Julia?

I'm trying to write a Julia function, which can accept both 1-dimensional Int64 and Float64 array as input argument. How can I do this without defining two versions, one for Int64 and another for Float64?
I have tried using Array{Real,1} as input argument type. However, since Array{Int64,1} is not a subtype of Array{Real,1}, this cannot work.
A genuine, non secure way to do it is, with an example:
function square(x)
# The point is for element-wise operation
out = x.*x
end
Output:
julia> square(2)
4
julia> square([2 2 2])
1×3 Array{Int64,2}:
4 4 4

Why is inv() failing?

When I try and run the inv() function on the example from the Julia documentation (v1.0.3), I get an error. The code is as follows (straight from the docs):
julia> M = [2 5; 1 3]
2×2 Array{Int64,2}:
2 5
1 3
julia> N = inv(M)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array.
It does work with pinv(), but I get some extremely small floating point values. Any ideas why I can't get inv() to work for this extremely simple case?
The error message suggests that you have previously defined a variable called inv which is a floating point matrix, and then try to use this matrix as a function, e.g.
julia> inv = rand(2, 2);
julia> M = [2 5; 1 3];
julia> inv(M)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array.
You can reach the inv function by restarting (and hence clearing the meaning of inv) or using the fully qualified name:
julia> import LinearAlgebra
julia> LinearAlgebra.inv(M)
2×2 Array{Float64,2}:
3.0 -5.0
-1.0 2.0

IJulia plot One Liner

I am trying to switch from Mathematica to IJulia for data exploration, and I was wondering if there's a n analogue for the following Mathematica one-liner:
ListPlot[Import["/tmp/output.tsv"], Joined -> True]
output.tsv is a tab delineated list of (X,Y) pairs
Here's a lame attempt:
In [1]: using Gadfly; plot(readdlm("/tmp/output.tsv", '\t', Float64))
no method plot(Array{Float64,2},)
at In[1]:1
Gadfly will accept arrays, but you need to specify the x and y values. Also, you need to pass an aesthetic.
julia> a = [1 2 3; 4 5 6]
2x3 Array{Int64,2}:
1 2 3
4 5 6
julia> plot(a)
ERROR: no method plot(Array{Int64,2})
julia> plot(x=a[1,:], y=a[2,:], Geom.line)
Here is a screenshot from REPL (not IJulia):
You'd probably have to read it into a dataframe (DataFrames.readtable), since that's what Gadfly operates on. Other plotting packages such as Winston operate on raw data, but since you are reading structure data anyway, the DataFrames approach is probably best.

Resources