Arrays

Constructors and Types

Core.AbstractArray — Type

1. AbstractArray{T,N}

Supertype for N-dimensional arrays (or array-like types) with elements of type T. Array and other types are subtypes of this. See the manual section on the AbstractArray interface.

See also: AbstractVector, AbstractMatrix, eltype, ndims.

source

Base.AbstractVector — Type

1. AbstractVector{T}

Supertype for one-dimensional arrays (or array-like types) with elements of type T. Alias for AbstractArray{T,1}.

source

Base.AbstractMatrix — Type

1. AbstractMatrix{T}

Supertype for two-dimensional arrays (or array-like types) with elements of type T. Alias for AbstractArray{T,2}.

source

Base.AbstractVecOrMat — Type

1. AbstractVecOrMat{T}

Union type of AbstractVector{T} and AbstractMatrix{T}.

source

Core.Array — Type

1. Array{T,N} <: AbstractArray{T,N}

N-dimensional dense array with elements of type T.

source

Core.Array — Method

1. Array{T}(undef, dims)
2. Array{T,N}(undef, dims)

Construct an uninitialized N-dimensional Array containing elements of type T. N can either be supplied explicitly, as in Array{T,N}(undef, dims), or be determined by the length or number of dims. dims may be a tuple or a series of integer arguments corresponding to the lengths in each dimension. If the rank N is supplied explicitly, then it must match the length or number of dims. Here undef is the UndefInitializer.

Examples

1. julia> A = Array{Float64, 2}(undef, 2, 3) # N given explicitly
2. 2×3 Matrix{Float64}:
3. 6.90198e-310  6.90198e-310  6.90198e-310
4. 6.90198e-310  6.90198e-310  0.0

6. julia> B = Array{Float64}(undef, 4) # N determined by the input
7. 4-element Vector{Float64}:
8. 2.360075077e-314
9. NaN
10. 2.2671131793e-314
11. 2.299821756e-314

13. julia> similar(B, 2, 4, 1) # use typeof(B), and the given size
14. 2×4×1 Array{Float64, 3}:
15. [:, :, 1] =
16. 2.26703e-314  2.26708e-314  0.0           2.80997e-314
17. 0.0           2.26703e-314  2.26708e-314  0.0

source

Core.Array — Method

1. Array{T}(nothing, dims)
2. Array{T,N}(nothing, dims)

Construct an N-dimensional Array containing elements of type T, initialized with nothing entries. Element type T must be able to hold these values, i.e. Nothing <: T.

Examples

1. julia> Array{Union{Nothing, String}}(nothing, 2)
2. 2-element Vector{Union{Nothing, String}}:
3. nothing
4. nothing

6. julia> Array{Union{Nothing, Int}}(nothing, 2, 3)
7. 2×3 Matrix{Union{Nothing, Int64}}:
8. nothing  nothing  nothing
9. nothing  nothing  nothing

source

Core.Array — Method

1. Array{T}(missing, dims)
2. Array{T,N}(missing, dims)

Construct an N-dimensional Array containing elements of type T, initialized with missing entries. Element type T must be able to hold these values, i.e. Missing <: T.

Examples

1. julia> Array{Union{Missing, String}}(missing, 2)
2. 2-element Vector{Union{Missing, String}}:
3. missing
4. missing

6. julia> Array{Union{Missing, Int}}(missing, 2, 3)
7. 2×3 Matrix{Union{Missing, Int64}}:
8. missing  missing  missing
9. missing  missing  missing

source

Core.UndefInitializer — Type

1. UndefInitializer

Singleton type used in array initialization, indicating the array-constructor-caller would like an uninitialized array. See also undef, an alias for UndefInitializer().

Examples

1. julia> Array{Float64, 1}(UndefInitializer(), 3)
2. 3-element Array{Float64, 1}:
3. 2.2752528595e-314
4. 2.202942107e-314
5. 2.275252907e-314

source

Core.undef — Constant

1. undef

Alias for UndefInitializer(), which constructs an instance of the singleton type UndefInitializer, used in array initialization to indicate the array-constructor-caller would like an uninitialized array.

See also: missing, similar.

Examples

1. julia> Array{Float64, 1}(undef, 3)
2. 3-element Vector{Float64}:
3. 2.2752528595e-314
4. 2.202942107e-314
5. 2.275252907e-314

source

Base.Vector — Type

1. Vector{T} <: AbstractVector{T}

One-dimensional dense array with elements of type T, often used to represent a mathematical vector. Alias for Array{T,1}.

See also empty, similar and zero for creating vectors.

source

Base.Vector — Method

1. Vector{T}(undef, n)

Construct an uninitialized Vector{T} of length n.

Examples

1. julia> Vector{Float64}(undef, 3)
2. 3-element Array{Float64, 1}:
3. 6.90966e-310
4. 6.90966e-310
5. 6.90966e-310

source

Base.Vector — Method

1. Vector{T}(nothing, m)

Construct a Vector{T} of length m, initialized with nothing entries. Element type T must be able to hold these values, i.e. Nothing <: T.

Examples

1. julia> Vector{Union{Nothing, String}}(nothing, 2)
2. 2-element Vector{Union{Nothing, String}}:
3. nothing
4. nothing

source

Base.Vector — Method

1. Vector{T}(missing, m)

Construct a Vector{T} of length m, initialized with missing entries. Element type T must be able to hold these values, i.e. Missing <: T.

Examples

1. julia> Vector{Union{Missing, String}}(missing, 2)
2. 2-element Vector{Union{Missing, String}}:
3. missing
4. missing

source

Base.Matrix — Type

1. Matrix{T} <: AbstractMatrix{T}

Two-dimensional dense array with elements of type T, often used to represent a mathematical matrix. Alias for Array{T,2}.

See also fill, zeros, undef and similar for creating matrices.

source

Base.Matrix — Method

1. Matrix{T}(undef, m, n)

Construct an uninitialized Matrix{T} of size m×n.

Examples

1. julia> Matrix{Float64}(undef, 2, 3)
2. 2×3 Array{Float64, 2}:
3. 2.36365e-314  2.28473e-314    5.0e-324
4. 2.26704e-314  2.26711e-314  NaN

6. julia> similar(ans, Int32, 2, 2)
7. 2×2 Matrix{Int32}:
8. 490537216  1277177453
9. 1  1936748399

source

Base.Matrix — Method

1. Matrix{T}(nothing, m, n)

Construct a Matrix{T} of size m×n, initialized with nothing entries. Element type T must be able to hold these values, i.e. Nothing <: T.

Examples

1. julia> Matrix{Union{Nothing, String}}(nothing, 2, 3)
2. 2×3 Matrix{Union{Nothing, String}}:
3. nothing  nothing  nothing
4. nothing  nothing  nothing

source

Base.Matrix — Method

1. Matrix{T}(missing, m, n)

Construct a Matrix{T} of size m×n, initialized with missing entries. Element type T must be able to hold these values, i.e. Missing <: T.

Examples

1. julia> Matrix{Union{Missing, String}}(missing, 2, 3)
2. 2×3 Matrix{Union{Missing, String}}:
3. missing  missing  missing
4. missing  missing  missing

source

Base.VecOrMat — Type

1. VecOrMat{T}

Union type of Vector{T} and Matrix{T} which allows functions to accept either a Matrix or a Vector.

Examples

1. julia> Vector{Float64} <: VecOrMat{Float64}
2. true

4. julia> Matrix{Float64} <: VecOrMat{Float64}
5. true

7. julia> Array{Float64, 3} <: VecOrMat{Float64}
8. false

source

Core.DenseArray — Type

1. DenseArray{T, N} <: AbstractArray{T,N}

N-dimensional dense array with elements of type T. The elements of a dense array are stored contiguously in memory.

source

Base.DenseVector — Type

1. DenseVector{T}

One-dimensional DenseArray with elements of type T. Alias for DenseArray{T,1}.

source

Base.DenseMatrix — Type

1. DenseMatrix{T}

Two-dimensional DenseArray with elements of type T. Alias for DenseArray{T,2}.

source

Base.DenseVecOrMat — Type

1. DenseVecOrMat{T}

Union type of DenseVector{T} and DenseMatrix{T}.

source

Base.StridedArray — Type

1. StridedArray{T, N}

A hard-coded Union of common array types that follow the strided array interface, with elements of type T and N dimensions.

If A is a StridedArray, then its elements are stored in memory with offsets, which may vary between dimensions but are constant within a dimension. For example, A could have stride 2 in dimension 1, and stride 3 in dimension 2. Incrementing A along dimension d jumps in memory by [strides(A, d)] slots. Strided arrays are particularly important and useful because they can sometimes be passed directly as pointers to foreign language libraries like BLAS.

source

Base.StridedVector — Type

1. StridedVector{T}

One dimensional StridedArray with elements of type T.

source

Base.StridedMatrix — Type

1. StridedMatrix{T}

Two dimensional StridedArray with elements of type T.

source

Base.StridedVecOrMat — Type

1. StridedVecOrMat{T}

Union type of StridedVector and StridedMatrix with elements of type T.

source

Base.getindex — Method

1. getindex(type[, elements...])

Construct a 1-d array of the specified type. This is usually called with the syntax Type[]. Element values can be specified using Type[a,b,c,...].

Examples

1. julia> Int8[1, 2, 3]
2. 3-element Vector{Int8}:
3. 1
4. 2
5. 3

7. julia> getindex(Int8, 1, 2, 3)
8. 3-element Vector{Int8}:
9. 1
10. 2
11. 3

source

Base.zeros — Function

1. zeros([T=Float64,] dims::Tuple)
2. zeros([T=Float64,] dims...)

Create an Array, with element type T, of all zeros with size specified by dims. See also fill, ones, zero.

Examples

1. julia> zeros(1)
2. 1-element Vector{Float64}:
3. 0.0

5. julia> zeros(Int8, 2, 3)
6. 2×3 Matrix{Int8}:
7. 0  0  0
8. 0  0  0

source

Base.ones — Function

1. ones([T=Float64,] dims::Tuple)
2. ones([T=Float64,] dims...)

Create an Array, with element type T, of all ones with size specified by dims. See also fill, zeros.

Examples

1. julia> ones(1,2)
2. 1×2 Matrix{Float64}:
3. 1.0  1.0

5. julia> ones(ComplexF64, 2, 3)
6. 2×3 Matrix{ComplexF64}:
7. 1.0+0.0im  1.0+0.0im  1.0+0.0im
8. 1.0+0.0im  1.0+0.0im  1.0+0.0im

source

Base.BitArray — Type

1. BitArray{N} <: AbstractArray{Bool, N}

Space-efficient N-dimensional boolean array, using just one bit for each boolean value.

BitArrays pack up to 64 values into every 8 bytes, resulting in an 8x space efficiency over Array{Bool, N} and allowing some operations to work on 64 values at once.

By default, Julia returns BitArrays from broadcasting operations that generate boolean elements (including dotted-comparisons like .==) as well as from the functions trues and falses.

Note

Due to its packed storage format, concurrent access to the elements of a BitArray where at least one of them is a write is not thread safe.

source

Base.BitArray — Method

1. BitArray(undef, dims::Integer...)
2. BitArray{N}(undef, dims::NTuple{N,Int})

Construct an undef BitArray with the given dimensions. Behaves identically to the Array constructor. See undef.

Examples

1. julia> BitArray(undef, 2, 2)
2. 2×2 BitMatrix:
3. 0  0
4. 0  0

6. julia> BitArray(undef, (3, 1))
7. 3×1 BitMatrix:
8. 0
9. 0
10. 0

source

Base.BitArray — Method

1. BitArray(itr)

Construct a BitArray generated by the given iterable object. The shape is inferred from the itr object.

Examples

1. julia> BitArray([1 0; 0 1])
2. 2×2 BitMatrix:
3. 1  0
4. 0  1

6. julia> BitArray(x+y == 3 for x = 1:2, y = 1:3)
7. 2×3 BitMatrix:
8. 0  1  0
9. 1  0  0

11. julia> BitArray(x+y == 3 for x = 1:2 for y = 1:3)
12. 6-element BitVector:
13. 0
14. 1
15. 0
16. 1
17. 0
18. 0

source

Base.trues — Function

1. trues(dims)

Create a BitArray with all values set to true.

Examples

1. julia> trues(2,3)
2. 2×3 BitMatrix:
3. 1  1  1
4. 1  1  1

source

Base.falses — Function

1. falses(dims)

Create a BitArray with all values set to false.

Examples

1. julia> falses(2,3)
2. 2×3 BitMatrix:
3. 0  0  0
4. 0  0  0

source

Base.fill — Function

1. fill(value, dims::Tuple)
2. fill(value, dims...)

Create an array of size dims with every location set to value.

For example, fill(1.0, (5,5)) returns a 5×5 array of floats, with 1.0 in every location of the array.

The dimension lengths dims may be specified as either a tuple or a sequence of arguments. An N-length tuple or N arguments following the value specify an N-dimensional array. Thus, a common idiom for creating a zero-dimensional array with its only location set to x is fill(x).

Every location of the returned array is set to (and is thus \=== to) the value that was passed; this means that if the value is itself modified, all elements of the filled array will reflect that modification because they’re still that very value. This is of no concern with fill(1.0, (5,5)) as the value 1.0 is immutable and cannot itself be modified, but can be unexpected with mutable values like — most commonly — arrays. For example, fill([], 3) places the very same empty array in all three locations of the returned vector:

1. julia> v = fill([], 3)
2. 3-element Vector{Vector{Any}}:
3. []
4. []
5. []

7. julia> v[1] === v[2] === v[3]
8. true

10. julia> value = v[1]
11. Any[]

13. julia> push!(value, 867_5309)
14. 1-element Vector{Any}:
15. 8675309

17. julia> v
18. 3-element Vector{Vector{Any}}:
19. [8675309]
20. [8675309]
21. [8675309]

To create an array of many independent inner arrays, use a comprehension instead. This creates a new and distinct array on each iteration of the loop:

1. julia> v2 = [[] for _ in 1:3]
2. 3-element Vector{Vector{Any}}:
3. []
4. []
5. []

7. julia> v2[1] === v2[2] === v2[3]
8. false

10. julia> push!(v2[1], 8675309)
11. 1-element Vector{Any}:
12. 8675309

14. julia> v2
15. 3-element Vector{Vector{Any}}:
16. [8675309]
17. []
18. []

See also: fill!, zeros, ones, similar.

Examples

1. julia> fill(1.0, (2,3))
2. 2×3 Matrix{Float64}:
3. 1.0  1.0  1.0
4. 1.0  1.0  1.0

6. julia> fill(42)
7. 0-dimensional Array{Int64, 0}:
8. 42

10. julia> A = fill(zeros(2), 2) # sets both elements to the same [0.0, 0.0] vector
11. 2-element Vector{Vector{Float64}}:
12. [0.0, 0.0]
13. [0.0, 0.0]

15. julia> A[1][1] = 42; # modifies the filled value to be [42.0, 0.0]

17. julia> A # both A[1] and A[2] are the very same vector
18. 2-element Vector{Vector{Float64}}:
19. [42.0, 0.0]
20. [42.0, 0.0]

source

Base.fill! — Function

1. fill!(A, x)

Fill array A with the value x. If x is an object reference, all elements will refer to the same object. fill!(A, Foo()) will return A filled with the result of evaluating Foo() once.

Examples

1. julia> A = zeros(2,3)
2. 2×3 Matrix{Float64}:
3. 0.0  0.0  0.0
4. 0.0  0.0  0.0

6. julia> fill!(A, 2.)
7. 2×3 Matrix{Float64}:
8. 2.0  2.0  2.0
9. 2.0  2.0  2.0

11. julia> a = [1, 1, 1]; A = fill!(Vector{Vector{Int}}(undef, 3), a); a[1] = 2; A
12. 3-element Vector{Vector{Int64}}:
13. [2, 1, 1]
14. [2, 1, 1]
15. [2, 1, 1]

17. julia> x = 0; f() = (global x += 1; x); fill!(Vector{Int}(undef, 3), f())
18. 3-element Vector{Int64}:
19. 1
20. 1
21. 1

source

Base.empty — Function

1. empty(x::Tuple)

Returns an empty tuple, ().

source

1. empty(v::AbstractVector, [eltype])

Create an empty vector similar to v, optionally changing the eltype.

See also: empty!, isempty, isassigned.

Examples

1. julia> empty([1.0, 2.0, 3.0])
2. Float64[]

4. julia> empty([1.0, 2.0, 3.0], String)
5. String[]

source

1. empty(a::AbstractDict, [index_type=keytype(a)], [value_type=valtype(a)])

Create an empty AbstractDict container which can accept indices of type index_type and values of type value_type. The second and third arguments are optional and default to the input’s keytype and valtype, respectively. (If only one of the two types is specified, it is assumed to be the value_type, and the index_type we default to keytype(a)).

Custom AbstractDict subtypes may choose which specific dictionary type is best suited to return for the given index and value types, by specializing on the three-argument signature. The default is to return an empty Dict.

source

Base.similar — Function

1. 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}(undef, dims...).

For example, similar(1:10, 1, 4) returns an uninitialized Array{Int,2} since ranges are neither mutable nor support 2 dimensions:

1. julia> similar(1:10, 1, 4)
2. 1×4 Matrix{Int64}:
3. 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:

1. julia> similar(trues(10,10), 2)
2. 2-element BitVector:
3. 0
4. 0

Since BitArrays can only store elements of type Bool, however, if you request a different element type it will create a regular Array instead:

1. julia> similar(falses(10), Float64, 2, 4)
2. 2×4 Matrix{Float64}:
3. 2.18425e-314  2.18425e-314  2.18425e-314  2.18425e-314
4. 2.18425e-314  2.18425e-314  2.18425e-314  2.18425e-314

See also: undef, isassigned.

source

1. similar(storagetype, axes)

Create an uninitialized mutable array analogous to that specified by storagetype, but with axes specified by the last argument.

Examples:

1. similar(Array{Int}, axes(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}(undef, size(A)), but if A has unconventional indexing then the indices of the result will match A.

1. similar(BitArray, (axes(A, 2),))

would create a 1-dimensional logical array whose indices match those of the columns of A.

source

Basic functions

Base.ndims — Function

1. ndims(A::AbstractArray) -> Integer

Return the number of dimensions of A.

See also: size, axes.

Examples

1. julia> A = fill(1, (3,4,5));

3. julia> ndims(A)
4. 3

source

Base.size — Function

1. size(A::AbstractArray, [dim])

Return a tuple containing the dimensions of A. Optionally you can specify a dimension to just get the length of that dimension.

Note that size may not be defined for arrays with non-standard indices, in which case axes may be useful. See the manual chapter on arrays with custom indices.

See also: length, ndims, eachindex, sizeof.

Examples

1. julia> A = fill(1, (2,3,4));

3. julia> size(A)
4. (2, 3, 4)

6. julia> size(A, 2)
7. 3

source

Base.axes — Method

1. axes(A)

Return the tuple of valid indices for array A.

See also: size, keys, eachindex.

Examples

1. julia> A = fill(1, (5,6,7));

3. julia> axes(A)
4. (Base.OneTo(5), Base.OneTo(6), Base.OneTo(7))

source

Base.axes — Method

1. axes(A, d)

Return the valid range of indices for array A along dimension d.

See also size, and the manual chapter on arrays with custom indices.

Examples

1. julia> A = fill(1, (5,6,7));

3. julia> axes(A, 2)
4. Base.OneTo(6)

Usage note

Each of the indices has to be an AbstractUnitRange{<:Integer}, but at the same time can be a type that uses custom indices. So, for example, if you need a subset, use generalized indexing constructs like begin/end or firstindex/lastindex:

1. ix = axes(v, 1)
2. ix[2:end]          # will work for eg Vector, but may fail in general
3. ix[(begin+1):end]  # works for generalized indexes

source

Base.length — Method

1. length(A::AbstractArray)

Return the number of elements in the array, defaults to prod(size(A)).

Examples

1. julia> length([1, 2, 3, 4])
2. 4

4. julia> length([1 2; 3 4])
5. 4

source

Base.keys — Method

1. keys(a::AbstractArray)

Return an efficient array describing all valid indices for a arranged in the shape of a itself.

They keys of 1-dimensional arrays (vectors) are integers, whereas all other N-dimensional arrays use CartesianIndex to describe their locations. Often the special array types LinearIndices and CartesianIndices are used to efficiently represent these arrays of integers and CartesianIndexes, respectively.

Note that the keys of an array might not be the most efficient index type; for maximum performance use eachindex instead.

source

Base.eachindex — Function

1. eachindex(A...)

Create an iterable object for visiting each index of an AbstractArray A in an efficient manner. For array types that have opted into fast linear indexing (like Array), this is simply the range 1:length(A). For other array types, return a specialized Cartesian range to efficiently index into the array with indices specified for every dimension. For other iterables, including strings and dictionaries, return an iterator object supporting arbitrary index types (e.g. unevenly spaced or non-integer indices).

If you supply more than one AbstractArray argument, eachindex will create an iterable object that is fast for all arguments (a UnitRange if all inputs have fast linear indexing, a CartesianIndices otherwise). If the arrays have different sizes and/or dimensionalities, a DimensionMismatch exception will be thrown.

Examples

1. julia> A = [1 2; 3 4];

3. julia> for i in eachindex(A) # linear indexing
4. println(i)
5. end
6. 1
7. 2
8. 3
9. 4

11. julia> for i in eachindex(view(A, 1:2, 1:1)) # Cartesian indexing
12. println(i)
13. end
14. CartesianIndex(1, 1)
15. CartesianIndex(2, 1)

source

Base.IndexStyle — Type

1. IndexStyle(A)
2. IndexStyle(typeof(A))

IndexStyle specifies the “native indexing style” for array A. When you define a new AbstractArray type, you can choose to implement either linear indexing (with IndexLinear) or cartesian indexing. If you decide to only implement linear indexing, then you must set this trait for your array type:

1. Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()

The default is IndexCartesian().

Julia’s internal indexing machinery will automatically (and invisibly) recompute all indexing operations into the preferred style. This allows users to access elements of your array using any indexing style, even when explicit methods have not been provided.

If you define both styles of indexing for your AbstractArray, this trait can be used to select the most performant indexing style. Some methods check this trait on their inputs, and dispatch to different algorithms depending on the most efficient access pattern. In particular, eachindex creates an iterator whose type depends on the setting of this trait.

source

Base.IndexLinear — Type

1. IndexLinear()

Subtype of IndexStyle used to describe arrays which are optimally indexed by one linear index.

A linear indexing style uses one integer index to describe the position in the array (even if it’s a multidimensional array) and column-major ordering is used to efficiently access the elements. This means that requesting eachindex from an array that is IndexLinear will return a simple one-dimensional range, even if it is multidimensional.

A custom array that reports its IndexStyle as IndexLinear only needs to implement indexing (and indexed assignment) with a single Int index; all other indexing expressions — including multidimensional accesses — will be recomputed to the linear index. For example, if A were a 2×3 custom matrix with linear indexing, and we referenced A[1, 3], this would be recomputed to the equivalent linear index and call A[5] since 2*1 + 3 = 5.

See also IndexCartesian.

source

Base.IndexCartesian — Type

1. IndexCartesian()

Subtype of IndexStyle used to describe arrays which are optimally indexed by a Cartesian index. This is the default for new custom AbstractArray subtypes.

A Cartesian indexing style uses multiple integer indices to describe the position in a multidimensional array, with exactly one index per dimension. This means that requesting eachindex from an array that is IndexCartesian will return a range of CartesianIndices.

A N-dimensional custom array that reports its IndexStyle as IndexCartesian needs to implement indexing (and indexed assignment) with exactly N Int indices; all other indexing expressions — including linear indexing — will be recomputed to the equivalent Cartesian location. For example, if A were a 2×3 custom matrix with cartesian indexing, and we referenced A[5], this would be recomputed to the equivalent Cartesian index and call A[1, 3] since 5 = 2*1 + 3.

It is significantly more expensive to compute Cartesian indices from a linear index than it is to go the other way. The former operation requires division — a very costly operation — whereas the latter only uses multiplication and addition and is essentially free. This asymmetry means it is far more costly to use linear indexing with an IndexCartesian array than it is to use Cartesian indexing with an IndexLinear array.

See also IndexLinear.

source

Base.conj! — Function

1. conj!(A)

Transform an array to its complex conjugate in-place.

See also conj.

Examples

1. julia> A = [1+im 2-im; 2+2im 3+im]
2. 2×2 Matrix{Complex{Int64}}:
3. 1+1im  2-1im
4. 2+2im  3+1im

6. julia> conj!(A);

8. julia> A
9. 2×2 Matrix{Complex{Int64}}:
10. 1-1im  2+1im
11. 2-2im  3-1im

source

Base.stride — Function

1. stride(A, k::Integer)

Return the distance in memory (in number of elements) between adjacent elements in dimension k.

See also: strides.

Examples

1. julia> A = fill(1, (3,4,5));

3. julia> stride(A,2)
4. 3

6. julia> stride(A,3)
7. 12

source

Base.strides — Function

1. strides(A)

Return a tuple of the memory strides in each dimension.

See also: stride.

Examples

1. julia> A = fill(1, (3,4,5));

3. julia> strides(A)
4. (1, 3, 12)

source

Broadcast and vectorization

See also the dot syntax for vectorizing functions; for example, f.(args...) implicitly calls broadcast(f, args...). Rather than relying on “vectorized” methods of functions like sin to operate on arrays, you should use sin.(a) to vectorize via broadcast.

Base.Broadcast.broadcast — Function

1. broadcast(f, As...)

Broadcast the function f over the arrays, tuples, collections, Refs and/or scalars As.

Broadcasting applies the function f over the elements of the container arguments and the scalars themselves in As. Singleton and missing dimensions are expanded to match the extents of the other arguments by virtually repeating the value. By default, only a limited number of types are considered scalars, including Numbers, Strings, Symbols, Types, Functions and some common singletons like missing and nothing. All other arguments are iterated over or indexed into elementwise.

The resulting container type is established by the following rules:

  • If all the arguments are scalars or zero-dimensional arrays, it returns an unwrapped scalar.
  • If at least one argument is a tuple and all others are scalars or zero-dimensional arrays, it returns a tuple.
  • All other combinations of arguments default to returning an Array, but custom container types can define their own implementation and promotion-like rules to customize the result when they appear as arguments.

A special syntax exists for broadcasting: f.(args...) is equivalent to broadcast(f, args...), and nested f.(g.(args...)) calls are fused into a single broadcast loop.

Examples

1. julia> A = [1, 2, 3, 4, 5]
2. 5-element Vector{Int64}:
3. 1
4. 2
5. 3
6. 4
7. 5

9. julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
10. 5×2 Matrix{Int64}:
11. 1   2
12. 3   4
13. 5   6
14. 7   8
15. 9  10

17. julia> broadcast(+, A, B)
18. 5×2 Matrix{Int64}:
19. 2   3
20. 5   6
21. 8   9
22. 11  12
23. 14  15

25. julia> parse.(Int, ["1", "2"])
26. 2-element Vector{Int64}:
27. 1
28. 2

30. julia> abs.((1, -2))
31. (1, 2)

33. julia> broadcast(+, 1.0, (0, -2.0))
34. (1.0, -1.0)

36. julia> (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1]))
37. 2-element Vector{Vector{Int64}}:
38. [1, 1]
39. [2, 2]

41. julia> string.(("one","two","three","four"), ": ", 1:4)
42. 4-element Vector{String}:
43. "one: 1"
44. "two: 2"
45. "three: 3"
46. "four: 4"

source

Base.Broadcast.broadcast! — Function

1. broadcast!(f, dest, As...)

Like broadcast, but store the result of broadcast(f, As...) in the dest array. Note that dest is only used to store the result, and does not supply arguments to f unless it is also listed in the As, as in broadcast!(f, A, A, B) to perform A[:] = broadcast(f, A, B).

Examples

1. julia> A = [1.0; 0.0]; B = [0.0; 0.0];

3. julia> broadcast!(+, B, A, (0, -2.0));

5. julia> B
6. 2-element Vector{Float64}:
7. 1.0
8. -2.0

10. julia> A
11. 2-element Vector{Float64}:
12. 1.0
13. 0.0

15. julia> broadcast!(+, A, A, (0, -2.0));

17. julia> A
18. 2-element Vector{Float64}:
19. 1.0
20. -2.0

source

Base.Broadcast.@__dot__ — Macro

1. @. expr

Convert every function call or operator in expr into a “dot call” (e.g. convert f(x) to f.(x)), and convert every assignment in expr to a “dot assignment” (e.g. convert += to .+=).

If you want to avoid adding dots for selected function calls in expr, splice those function calls in with $. For example, @. sqrt(abs($sort(x))) is equivalent to sqrt.(abs.(sort(x))) (no dot for sort).

(@. is equivalent to a call to @__dot__.)

Examples

1. julia> x = 1.0:3.0; y = similar(x);

3. julia> @. y = x + 3 * sin(x)
4. 3-element Vector{Float64}:
5. 3.5244129544236893
6. 4.727892280477045
7. 3.4233600241796016

source

For specializing broadcast on custom types, see

Base.Broadcast.BroadcastStyle — Type

BroadcastStyle is an abstract type and trait-function used to determine behavior of objects under broadcasting. BroadcastStyle(typeof(x)) returns the style associated with x. To customize the broadcasting behavior of a type, one can declare a style by defining a type/method pair

1. struct MyContainerStyle <: BroadcastStyle end
2. Base.BroadcastStyle(::Type{<:MyContainer}) = MyContainerStyle()

One then writes method(s) (at least similar) operating on Broadcasted{MyContainerStyle}. There are also several pre-defined subtypes of BroadcastStyle that you may be able to leverage; see the Interfaces chapter for more information.

source

Base.Broadcast.AbstractArrayStyle — Type

Broadcast.AbstractArrayStyle{N} <: BroadcastStyle is the abstract supertype for any style associated with an AbstractArray type. The N parameter is the dimensionality, which can be handy for AbstractArray types that only support specific dimensionalities:

1. struct SparseMatrixStyle <: Broadcast.AbstractArrayStyle{2} end
2. Base.BroadcastStyle(::Type{<:SparseMatrixCSC}) = SparseMatrixStyle()

For AbstractArray types that support arbitrary dimensionality, N can be set to Any:

1. struct MyArrayStyle <: Broadcast.AbstractArrayStyle{Any} end
2. Base.BroadcastStyle(::Type{<:MyArray}) = MyArrayStyle()

In cases where you want to be able to mix multiple AbstractArrayStyles and keep track of dimensionality, your style needs to support a Val constructor:

1. struct MyArrayStyleDim{N} <: Broadcast.AbstractArrayStyle{N} end
2. (::Type{<:MyArrayStyleDim})(::Val{N}) where N = MyArrayStyleDim{N}()

Note that if two or more AbstractArrayStyle subtypes conflict, broadcasting machinery will fall back to producing Arrays. If this is undesirable, you may need to define binary BroadcastStyle rules to control the output type.

See also Broadcast.DefaultArrayStyle.

source

Base.Broadcast.ArrayStyle — Type

Broadcast.ArrayStyle{MyArrayType}() is a BroadcastStyle indicating that an object behaves as an array for broadcasting. It presents a simple way to construct Broadcast.AbstractArrayStyles for specific AbstractArray container types. Broadcast styles created this way lose track of dimensionality; if keeping track is important for your type, you should create your own custom Broadcast.AbstractArrayStyle.

source

Base.Broadcast.DefaultArrayStyle — Type

Broadcast.DefaultArrayStyle{N}() is a BroadcastStyle indicating that

(本文超出站点单文档体积上限,已截断)