If you don’t know what a function does, then you can get documentation on it by typing ? and then the function name at the REPL
help?> similar
search: similar
similar(array, [element_type=eltype(array)], [dims=size(array)])
Create an uninitialized mutable array with the given element type and size, based upon the given
source array. The second and third arguments are both optional, defaulting to the given array's eltype
and size. The dimensions may be specified either as a single tuple argument or as a series of integer
arguments.
Custom AbstractArray subtypes may choose which specific array type is best-suited to return for the
given element type and dimensionality. If they do not specialize this method, the default is an
Array{element_type}(dims...).
For example, similar(1:10, 1, 4) returns an uninitialized Array{Int,2} since ranges are neither
mutable nor support 2 dimensions:
julia> similar(1:10, 1, 4)
1×4 Array{Int64,2}:
4419743872 4374413872 4419743888 0
Conversely, similar(trues(10,10), 2) returns an uninitialized BitVector with two elements since
BitArrays are both mutable and can support 1-dimensional arrays:
julia> similar(trues(10,10), 2)
2-element BitArray{1}:
false
false
Since BitArrays can only store elements of type Bool, however, if you request a different element type
it will create a regular Array instead:
julia> similar(falses(10), Float64, 2, 4)
2×4 Array{Float64,2}:
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
similar(storagetype, indices)
Create an uninitialized mutable array analogous to that specified by storagetype, but with indices
specified by the last argument. storagetype might be a type or a function.
Examples:
similar(Array{Int}, indices(A))
creates an array that "acts like" an Array{Int} (and might indeed be backed by one), but which is
indexed identically to A. If A has conventional indexing, this will be identical to
Array{Int}(size(A)), but if A has unconventional indexing then the indices of the result will match A.
similar(BitArray, (indices(A, 2),))
would create a 1-dimensional logical array whose indices match those of the columns of A.
similar(dims->zeros(Int, dims), indices(A))
would create an array of Int, initialized to zero, matching the indices of A.
It is true that it is annoying to make the user have to provide y
in order for them to use your function. The reason that you would do so is if you are going to call the simplp
function lots of times. If you use the simplp(x, xm1)
method, then Julia will have to allocate a y
vector every time that you call the function. Therefore, Julia will have to allocate memory every time that you call simplp(x, xm1)
. This can get very expensive if you are filtering a lot of large vectors. On the other hand, if you use simplp(y, x, xm1)
, then you can allocate y
once and every time that you call the function, Julia will use the same piece of memory to write to y
. Julia also gives you some tools for introspecting these sorts of things, for example:
julia> x = 1:10 |> float;
julia> simplp(x, 0) # Warm up.
10-element Array{Float64,1}:
1.0
3.0
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
julia> y = similar(x);
julia> simplp!(y, x, 0) # Warm up.
10-element Array{Float64,1}:
1.0
3.0
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
julia> @time simplp(x, 0)
0.000005 seconds (84 allocations: 6.248 KiB)
10-element Array{Float64,1}:
1.0
3.0
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
julia> @time simplp!(y, x, 0)
0.000005 seconds (4 allocations: 160 bytes)
10-element Array{Float64,1}:
1.0
3.0
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
You can see a significant difference in the number of allocations between the two methods. If you decide to get a bit more serious about measuring the performance of the functions that you write, then I would suggest BenchmarkTools.