Collections and Data Structures

Iteration

Sequential iteration is implemented by the iterate function. The general for loop:

1. for i in iter   # or  "for i = iter"
2. # body
3. end

is translated into:

1. next = iterate(iter)
2. while next !== nothing
3. (i, state) = next
4. # body
5. next = iterate(iter, state)
6. end

The state object may be anything, and should be chosen appropriately for each iterable type. See the manual section on the iteration interface for more details about defining a custom iterable type.

Base.iterate — Function

1. iterate(iter [, state]) -> Union{Nothing, Tuple{Any, Any}}

Advance the iterator to obtain the next element. If no elements remain, nothing should be returned. Otherwise, a 2-tuple of the next element and the new iteration state should be returned.

source

Base.IteratorSize — Type

1. IteratorSize(itertype::Type) -> IteratorSize

Given the type of an iterator, return one of the following values:

  • SizeUnknown() if the length (number of elements) cannot be determined in advance.
  • HasLength() if there is a fixed, finite length.
  • HasShape{N}() if there is a known length plus a notion of multidimensional shape (as for an array). In this case N should give the number of dimensions, and the axes function is valid for the iterator.
  • IsInfinite() if the iterator yields values forever.

The default value (for iterators that do not define this function) is HasLength(). This means that most iterators are assumed to implement length.

This trait is generally used to select between algorithms that pre-allocate space for their result, and algorithms that resize their result incrementally.

1. julia> Base.IteratorSize(1:5)
2. Base.HasShape{1}()

4. julia> Base.IteratorSize((2,3))
5. Base.HasLength()

source

Base.IteratorEltype — Type

1. IteratorEltype(itertype::Type) -> IteratorEltype

Given the type of an iterator, return one of the following values:

  • EltypeUnknown() if the type of elements yielded by the iterator is not known in advance.
  • HasEltype() if the element type is known, and eltype would return a meaningful value.

HasEltype() is the default, since iterators are assumed to implement eltype.

This trait is generally used to select between algorithms that pre-allocate a specific type of result, and algorithms that pick a result type based on the types of yielded values.

1. julia> Base.IteratorEltype(1:5)
2. Base.HasEltype()

source

Fully implemented by:

Constructors and Types

Base.AbstractRange — Type

1. AbstractRange{T}

Supertype for ranges with elements of type T. UnitRange and other types are subtypes of this.

source

Base.OrdinalRange — Type

1. OrdinalRange{T, S} <: AbstractRange{T}

Supertype for ordinal ranges with elements of type T with spacing(s) of type S. The steps should be always-exact multiples of oneunit, and T should be a “discrete” type, which cannot have values smaller than oneunit. For example, Integer or Date types would qualify, whereas Float64 would not (since this type can represent values smaller than oneunit(Float64). UnitRange, StepRange, and other types are subtypes of this.

source

Base.AbstractUnitRange — Type

1. AbstractUnitRange{T} <: OrdinalRange{T, T}

Supertype for ranges with a step size of oneunit(T) with elements of type T. UnitRange and other types are subtypes of this.

source

Base.StepRange — Type

1. StepRange{T, S} <: OrdinalRange{T, S}

Ranges with elements of type T with spacing of type S. The step between each element is constant, and the range is defined in terms of a start and stop of type T and a step of type S. Neither T nor S should be floating point types. The syntax a:b:c with b > 1 and a, b, and c all integers creates a StepRange.

Examples

1. julia> collect(StepRange(1, Int8(2), 10))
2. 5-element Vector{Int64}:
3. 1
4. 3
5. 5
6. 7
7. 9

9. julia> typeof(StepRange(1, Int8(2), 10))
10. StepRange{Int64, Int8}

12. julia> typeof(1:3:6)
13. StepRange{Int64, Int64}

source

Base.UnitRange — Type

1. UnitRange{T<:Real}

A range parameterized by a start and stop of type T, filled with elements spaced by 1 from start until stop is exceeded. The syntax a:b with a and b both Integers creates a UnitRange.

Examples

1. julia> collect(UnitRange(2.3, 5.2))
2. 3-element Vector{Float64}:
3. 2.3
4. 3.3
5. 4.3

7. julia> typeof(1:10)
8. UnitRange{Int64}

source

Base.LinRange — Type

1. LinRange{T,L}

A range with len linearly spaced elements between its start and stop. The size of the spacing is controlled by len, which must be an Integer.

Examples

1. julia> LinRange(1.5, 5.5, 9)
2. 9-element LinRange{Float64, Int64}:
3. 1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5

Compared to using range, directly constructing a LinRange should have less overhead but won’t try to correct for floating point errors:

1. julia> collect(range(-0.1, 0.3, length=5))
2. 5-element Array{Float64,1}:
3. -0.1
4. 0.0
5. 0.1
6. 0.2
7. 0.3

9. julia> collect(LinRange(-0.1, 0.3, 5))
10. 5-element Array{Float64,1}:
11. -0.1
12. -1.3877787807814457e-17
13. 0.09999999999999999
14. 0.19999999999999998
15. 0.3

source

General Collections

Base.isempty — Function

1. isempty(collection) -> Bool

Determine whether a collection is empty (has no elements).

Examples

1. julia> isempty([])
2. true

4. julia> isempty([1 2 3])
5. false

source

1. isempty(condition)

Return true if no tasks are waiting on the condition, false otherwise.

source

Base.empty! — Function

1. empty!(collection) -> collection

Remove all elements from a collection.

Examples

1. julia> A = Dict("a" => 1, "b" => 2)
2. Dict{String, Int64} with 2 entries:
3. "b" => 2
4. "a" => 1

6. julia> empty!(A);

8. julia> A
9. Dict{String, Int64}()

source

Base.length — Function

1. length(collection) -> Integer

Return the number of elements in the collection.

Use lastindex to get the last valid index of an indexable collection.

See also: size, ndims, eachindex.

Examples

1. julia> length(1:5)
2. 5

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

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

source

Base.checked_length — Function

1. Base.checked_length(r)

Calculates length(r), but may check for overflow errors where applicable when the result doesn’t fit into Union{Integer(eltype(r)),Int}.

source

Fully implemented by:

Iterable Collections

Base.in — Function

1. in(item, collection) -> Bool
2. ∈(item, collection) -> Bool

Determine whether an item is in the given collection, in the sense that it is \== to one of the values generated by iterating over the collection. Returns a Bool value, except if item is missing or collection contains missing but not item, in which case missing is returned (three-valued logic, matching the behavior of any and \==).

Some collections follow a slightly different definition. For example, Sets check whether the item isequal to one of the elements. Dicts look for key=>value pairs, and the key is compared using isequal. To test for the presence of a key in a dictionary, use haskey or k in keys(dict). For these collections, the result is always a Bool and never missing.

To determine whether an item is not in a given collection, see :∉. You may also negate the in by doing !(a in b) which is logically similar to “not in”.

When broadcasting with in.(items, collection) or items .∈ collection, both item and collection are broadcasted over, which is often not what is intended. For example, if both arguments are vectors (and the dimensions match), the result is a vector indicating whether each value in collection items is in the value at the corresponding position in collection. To get a vector indicating whether each value in items is in collection, wrap collection in a tuple or a Ref like this: in.(items, Ref(collection)) or items .∈ Ref(collection).

Examples

1. julia> a = 1:3:20
2. 1:3:19

4. julia> 4 in a
5. true

7. julia> 5 in a
8. false

10. julia> missing in [1, 2]
11. missing

13. julia> 1 in [2, missing]
14. missing

16. julia> 1 in [1, missing]
17. true

19. julia> missing in Set([1, 2])
20. false

22. julia> !(21 in a)
23. true

25. julia> !(19 in a)
26. false

28. julia> [1, 2] .∈ [2, 3]
29. 2-element BitVector:
30. 0
31. 0

33. julia> [1, 2] .∈ ([2, 3],)
34. 2-element BitVector:
35. 0
36. 1

See also: insorted, contains, occursin, issubset.

source

Base.:∉ — Function

1. ∉(item, collection) -> Bool
2. ∌(collection, item) -> Bool

Negation of ∈ and ∋, i.e. checks that item is not in collection.

When broadcasting with items .∉ collection, both item and collection are broadcasted over, which is often not what is intended. For example, if both arguments are vectors (and the dimensions match), the result is a vector indicating whether each value in collection items is not in the value at the corresponding position in collection. To get a vector indicating whether each value in items is not in collection, wrap collection in a tuple or a Ref like this: items .∉ Ref(collection).

Examples

1. julia> 1 ∉ 2:4
2. true

4. julia> 1 ∉ 1:3
5. false

7. julia> [1, 2] .∉ [2, 3]
8. 2-element BitVector:
9. 1
10. 1

12. julia> [1, 2] .∉ ([2, 3],)
13. 2-element BitVector:
14. 1
15. 0

source

Base.eltype — Function

1. eltype(type)

Determine the type of the elements generated by iterating a collection of the given type. For dictionary types, this will be a Pair{KeyType,ValType}. The definition eltype(x) = eltype(typeof(x)) is provided for convenience so that instances can be passed instead of types. However the form that accepts a type argument should be defined for new types.

See also: keytype, typeof.

Examples

1. julia> eltype(fill(1f0, (2,2)))
2. Float32

4. julia> eltype(fill(0x1, (2,2)))
5. UInt8

source

Base.indexin — Function

1. indexin(a, b)

Return an array containing the first index in b for each value in a that is a member of b. The output array contains nothing wherever a is not a member of b.

See also: sortperm, findfirst.

Examples

1. julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];

3. julia> b = ['a', 'b', 'c'];

5. julia> indexin(a, b)
6. 6-element Vector{Union{Nothing, Int64}}:
7. 1
8. 2
9. 3
10. 2
11. nothing
12. 1

14. julia> indexin(b, a)
15. 3-element Vector{Union{Nothing, Int64}}:
16. 1
17. 2
18. 3

source

Base.unique — Function

1. unique(itr)

Return an array containing only the unique elements of collection itr, as determined by isequal, in the order that the first of each set of equivalent elements originally appears. The element type of the input is preserved.

See also: unique!, allunique.

Examples

1. julia> unique([1, 2, 6, 2])
2. 3-element Vector{Int64}:
3. 1
4. 2
5. 6

7. julia> unique(Real[1, 1.0, 2])
8. 2-element Vector{Real}:
9. 1
10. 2

source

1. unique(f, itr)

Returns an array containing one value from itr for each unique value produced by f applied to elements of itr.

Examples

1. julia> unique(x -> x^2, [1, -1, 3, -3, 4])
2. 3-element Vector{Int64}:
3. 1
4. 3
5. 4

source

1. unique(A::AbstractArray; dims::Int)

Return unique regions of A along dimension dims.

Examples

1. julia> A = map(isodd, reshape(Vector(1:8), (2,2,2)))
2. 2×2×2 Array{Bool, 3}:
3. [:, :, 1] =
4. 1  1
5. 0  0

7. [:, :, 2] =
8. 1  1
9. 0  0

11. julia> unique(A)
12. 2-element Vector{Bool}:
13. 1
14. 0

16. julia> unique(A, dims=2)
17. 2×1×2 Array{Bool, 3}:
18. [:, :, 1] =
19. 1
20. 0

22. [:, :, 2] =
23. 1
24. 0

26. julia> unique(A, dims=3)
27. 2×2×1 Array{Bool, 3}:
28. [:, :, 1] =
29. 1  1
30. 0  0

source

Base.unique! — Function

1. unique!(f, A::AbstractVector)

Selects one value from A for each unique value produced by f applied to elements of A, then return the modified A.

Julia 1.1

This method is available as of Julia 1.1.

Examples

1. julia> unique!(x -> x^2, [1, -1, 3, -3, 4])
2. 3-element Vector{Int64}:
3. 1
4. 3
5. 4

7. julia> unique!(n -> n%3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6])
8. 3-element Vector{Int64}:
9. 5
10. 1
11. 9

13. julia> unique!(iseven, [2, 3, 5, 7, 9])
14. 2-element Vector{Int64}:
15. 2
16. 3

source

1. unique!(A::AbstractVector)

Remove duplicate items as determined by isequal, then return the modified A. unique! will return the elements of A in the order that they occur. If you do not care about the order of the returned data, then calling (sort!(A); unique!(A)) will be much more efficient as long as the elements of A can be sorted.

Examples

1. julia> unique!([1, 1, 1])
2. 1-element Vector{Int64}:
3. 1

5. julia> A = [7, 3, 2, 3, 7, 5];

7. julia> unique!(A)
8. 4-element Vector{Int64}:
9. 7
10. 3
11. 2
12. 5

14. julia> B = [7, 6, 42, 6, 7, 42];

16. julia> sort!(B);  # unique! is able to process sorted data much more efficiently.

18. julia> unique!(B)
19. 3-element Vector{Int64}:
20. 6
21. 7
22. 42

source

Base.allunique — Function

1. allunique(itr) -> Bool

Return true if all values from itr are distinct when compared with isequal.

See also: unique, issorted.

Examples

1. julia> a = [1; 2; 3]
2. 3-element Vector{Int64}:
3. 1
4. 2
5. 3

7. julia> allunique(a)
8. true

10. julia> allunique([a, a])
11. false

source

Base.reduce — Method

1. reduce(op, itr; [init])

Reduce the given collection itr with the given binary operator op. If provided, the initial value init must be a neutral element for op that will be returned for empty collections. It is unspecified whether init is used for non-empty collections.

For empty collections, providing init will be necessary, except for some special cases (e.g. when op is one of +, *, max, min, &, |) when Julia can determine the neutral element of op.

Reductions for certain commonly-used operators may have special implementations, and should be used instead: maximum(itr), minimum(itr), sum(itr), prod(itr), any(itr), all(itr).

The associativity of the reduction is implementation dependent. This means that you can’t use non-associative operations like - because it is undefined whether reduce(-,[1,2,3]) should be evaluated as (1-2)-3 or 1-(2-3). Use foldl or foldr instead for guaranteed left or right associativity.

Some operations accumulate error. Parallelism will be easier if the reduction can be executed in groups. Future versions of Julia might change the algorithm. Note that the elements are not reordered if you use an ordered collection.

Examples

1. julia> reduce(*, [2; 3; 4])
2. 24

4. julia> reduce(*, [2; 3; 4]; init=-1)
5. -24

source

Base.foldl — Method

1. foldl(op, itr; [init])

Like reduce, but with guaranteed left associativity. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.

Examples

1. julia> foldl(=>, 1:4)
2. ((1 => 2) => 3) => 4

4. julia> foldl(=>, 1:4; init=0)
5. (((0 => 1) => 2) => 3) => 4

source

Base.foldr — Method

1. foldr(op, itr; [init])

Like reduce, but with guaranteed right associativity. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.

Examples

1. julia> foldr(=>, 1:4)
2. 1 => (2 => (3 => 4))

4. julia> foldr(=>, 1:4; init=0)
5. 1 => (2 => (3 => (4 => 0)))

source

Base.maximum — Function

1. maximum(f, itr; [init])

Returns the largest result of calling function f on each element of itr.

The value returned for empty itr can be specified by init. It must be a neutral element for max (i.e. which is less than or equal to any other element) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> maximum(length, ["Julion", "Julia", "Jule"])
2. 6

4. julia> maximum(length, []; init=-1)
5. -1

7. julia> maximum(sin, Real[]; init=-1.0)  # good, since output of sin is >= -1
8. -1.0

source

1. maximum(itr; [init])

Returns the largest element in a collection.

The value returned for empty itr can be specified by init. It must be a neutral element for max (i.e. which is less than or equal to any other element) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> maximum(-20.5:10)
2. 9.5

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

7. julia> maximum(())
8. ERROR: ArgumentError: reducing over an empty collection is not allowed
9. Stacktrace:
10. [...]

12. julia> maximum((); init=-Inf)
13. -Inf

source

1. maximum(A::AbstractArray; dims)

Compute the maximum value of an array over the given dimensions. See also the max(a,b) function to take the maximum of two or more arguments, which can be applied elementwise to arrays via max.(a,b).

See also: maximum!, extrema, findmax, argmax.

Examples

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

6. julia> maximum(A, dims=1)
7. 1×2 Matrix{Int64}:
8. 3  4

10. julia> maximum(A, dims=2)
11. 2×1 Matrix{Int64}:
12. 2
13. 4

source

1. maximum(f, A::AbstractArray; dims)

Compute the maximum value by calling the function f on each element of an array over the given dimensions.

Examples

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

6. julia> maximum(abs2, A, dims=1)
7. 1×2 Matrix{Int64}:
8. 9  16

10. julia> maximum(abs2, A, dims=2)
11. 2×1 Matrix{Int64}:
12. 4
13. 16

source

Base.maximum! — Function

1. maximum!(r, A)

Compute the maximum value of A over the singleton dimensions of r, and write results to r.

Examples

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

6. julia> maximum!([1; 1], A)
7. 2-element Vector{Int64}:
8. 2
9. 4

11. julia> maximum!([1 1], A)
12. 1×2 Matrix{Int64}:
13. 3  4

source

Base.minimum — Function

1. minimum(f, itr; [init])

Returns the smallest result of calling function f on each element of itr.

The value returned for empty itr can be specified by init. It must be a neutral element for min (i.e. which is greater than or equal to any other element) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> minimum(length, ["Julion", "Julia", "Jule"])
2. 4

4. julia> minimum(length, []; init=typemax(Int64))
5. 9223372036854775807

7. julia> minimum(sin, Real[]; init=1.0)  # good, since output of sin is <= 1
8. 1.0

source

1. minimum(itr; [init])

Returns the smallest element in a collection.

The value returned for empty itr can be specified by init. It must be a neutral element for min (i.e. which is greater than or equal to any other element) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> minimum(-20.5:10)
2. -20.5

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

7. julia> minimum([])
8. ERROR: ArgumentError: reducing over an empty collection is not allowed
9. Stacktrace:
10. [...]

12. julia> minimum([]; init=Inf)
13. Inf

source

1. minimum(A::AbstractArray; dims)

Compute the minimum value of an array over the given dimensions. See also the min(a,b) function to take the minimum of two or more arguments, which can be applied elementwise to arrays via min.(a,b).

See also: minimum!, extrema, findmin, argmin.

Examples

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

6. julia> minimum(A, dims=1)
7. 1×2 Matrix{Int64}:
8. 1  2

10. julia> minimum(A, dims=2)
11. 2×1 Matrix{Int64}:
12. 1
13. 3

source

1. minimum(f, A::AbstractArray; dims)

Compute the minimum value by calling the function f on each element of an array over the given dimensions.

Examples

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

6. julia> minimum(abs2, A, dims=1)
7. 1×2 Matrix{Int64}:
8. 1  4

10. julia> minimum(abs2, A, dims=2)
11. 2×1 Matrix{Int64}:
12. 1
13. 9

source

Base.minimum! — Function

1. minimum!(r, A)

Compute the minimum value of A over the singleton dimensions of r, and write results to r.

Examples

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

6. julia> minimum!([1; 1], A)
7. 2-element Vector{Int64}:
8. 1
9. 3

11. julia> minimum!([1 1], A)
12. 1×2 Matrix{Int64}:
13. 1  2

source

Base.extrema — Function

1. extrema(itr) -> Tuple

Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.

Examples

1. julia> extrema(2:10)
2. (2, 10)

4. julia> extrema([9,pi,4.5])
5. (3.141592653589793, 9.0)

source

1. extrema(f, itr) -> Tuple

Compute both the minimum and maximum of f applied to each element in itr and return them as a 2-tuple. Only one pass is made over itr.

Julia 1.2

This method requires Julia 1.2 or later.

Examples

1. julia> extrema(sin, 0:π)
2. (0.0, 0.9092974268256817)

source

1. extrema(A::AbstractArray; dims) -> Array{Tuple}

Compute the minimum and maximum elements of an array over the given dimensions.

Examples

1. julia> A = reshape(Vector(1:2:16), (2,2,2))
2. 2×2×2 Array{Int64, 3}:
3. [:, :, 1] =
4. 1  5
5. 3  7

7. [:, :, 2] =
8. 9  13
9. 11  15

11. julia> extrema(A, dims = (1,2))
12. 1×1×2 Array{Tuple{Int64, Int64}, 3}:
13. [:, :, 1] =
14. (1, 7)

16. [:, :, 2] =
17. (9, 15)

source

1. extrema(f, A::AbstractArray; dims) -> Array{Tuple}

Compute the minimum and maximum of f applied to each element in the given dimensions of A.

Julia 1.2

This method requires Julia 1.2 or later.

source

Base.argmax — Function

1. argmax(r::AbstractRange)

Ranges can have multiple maximal elements. In that case argmax will return a maximal index, but not necessarily the first one.

source

1. argmax(f, domain)

Return a value x in the domain of f for which f(x) is maximised. If there are multiple maximal values for f(x) then the first one will be found.

domain must be a non-empty iterable.

Values are compared with isless.

Julia 1.7

This method requires Julia 1.7 or later.

Examples

1. julia> argmax(abs, -10:5)
2. -10

4. julia> argmax(cos, 0:π/2:2π)
5. 0.0

source

1. argmax(itr)

Return the index or key of the maximal element in a collection. If there are multiple maximal elements, then the first one will be returned.

The collection must not be empty.

Values are compared with isless.

See also: argmin, findmax.

Examples

1. julia> argmax([8, 0.1, -9, pi])
2. 1

4. julia> argmax([1, 7, 7, 6])
5. 2

7. julia> argmax([1, 7, 7, NaN])
8. 4

source

1. argmax(A; dims) -> indices

For an array input, return the indices of the maximum elements over the given dimensions. NaN is treated as greater than all other values except missing.

Examples

1. julia> A = [1.0 2; 3 4]
2. 2×2 Matrix{Float64}:
3. 1.0  2.0
4. 3.0  4.0

6. julia> argmax(A, dims=1)
7. 1×2 Matrix{CartesianIndex{2}}:
8. CartesianIndex(2, 1)  CartesianIndex(2, 2)

10. julia> argmax(A, dims=2)
11. 2×1 Matrix{CartesianIndex{2}}:
12. CartesianIndex(1, 2)
13. CartesianIndex(2, 2)

source

Base.argmin — Function

1. argmin(r::AbstractRange)

Ranges can have multiple minimal elements. In that case argmin will return a minimal index, but not necessarily the first one.

source

1. argmin(f, domain)

Return a value x in the domain of f for which f(x) is minimised. If there are multiple minimal values for f(x) then the first one will be found.

domain must be a non-empty iterable.

NaN is treated as less than all other values except missing.

Julia 1.7

This method requires Julia 1.7 or later.

Examples

1. julia> argmin(sign, -10:5)
2. -10

4. julia> argmin(x -> -x^3 + x^2 - 10, -5:5)
5. 5

7. julia> argmin(acos, 0:0.1:1)
8. 1.0

source

1. argmin(itr)

Return the index or key of the minimal element in a collection. If there are multiple minimal elements, then the first one will be returned.

The collection must not be empty.

NaN is treated as less than all other values except missing.

See also: argmax, findmin.

Examples

1. julia> argmin([8, 0.1, -9, pi])
2. 3

4. julia> argmin([7, 1, 1, 6])
5. 2

7. julia> argmin([7, 1, 1, NaN])
8. 4

source

1. argmin(A; dims) -> indices

For an array input, return the indices of the minimum elements over the given dimensions. NaN is treated as less than all other values except missing.

Examples

1. julia> A = [1.0 2; 3 4]
2. 2×2 Matrix{Float64}:
3. 1.0  2.0
4. 3.0  4.0

6. julia> argmin(A, dims=1)
7. 1×2 Matrix{CartesianIndex{2}}:
8. CartesianIndex(1, 1)  CartesianIndex(1, 2)

10. julia> argmin(A, dims=2)
11. 2×1 Matrix{CartesianIndex{2}}:
12. CartesianIndex(1, 1)
13. CartesianIndex(2, 1)

source

Base.findmax — Function

1. findmax(f, domain) -> (f(x), index)

Returns a pair of a value in the codomain (outputs of f) and the index of the corresponding value in the domain (inputs to f) such that f(x) is maximised. If there are multiple maximal points, then the first one will be returned.

domain must be a non-empty iterable.

Values are compared with isless.

Julia 1.7

This method requires Julia 1.7 or later.

Examples

1. julia> findmax(identity, 5:9)
2. (9, 5)

4. julia> findmax(-, 1:10)
5. (-1, 1)

7. julia> findmax(first, [(1, :a), (3, :b), (3, :c)])
8. (3, 2)

10. julia> findmax(cos, 0:π/2:2π)
11. (1.0, 1)

source

1. findmax(itr) -> (x, index)

Return the maximal element of the collection itr and its index or key. If there are multiple maximal elements, then the first one will be returned. Values are compared with isless.

See also: findmin, argmax, maximum.

Examples

1. julia> findmax([8, 0.1, -9, pi])
2. (8.0, 1)

4. julia> findmax([1, 7, 7, 6])
5. (7, 2)

7. julia> findmax([1, 7, 7, NaN])
8. (NaN, 4)

source

1. findmax(A; dims) -> (maxval, index)

For an array input, returns the value and index of the maximum over the given dimensions. NaN is treated as greater than all other values except missing.

Examples

1. julia> A = [1.0 2; 3 4]
2. 2×2 Matrix{Float64}:
3. 1.0  2.0
4. 3.0  4.0

6. julia> findmax(A, dims=1)
7. ([3.0 4.0], CartesianIndex{2}[CartesianIndex(2, 1) CartesianIndex(2, 2)])

9. julia> findmax(A, dims=2)
10. ([2.0; 4.0;;], CartesianIndex{2}[CartesianIndex(1, 2); CartesianIndex(2, 2);;])

source

Base.findmin — Function

1. findmin(f, domain) -> (f(x), index)

Returns a pair of a value in the codomain (outputs of f) and the index of the corresponding value in the domain (inputs to f) such that f(x) is minimised. If there are multiple minimal points, then the first one will be returned.

domain must be a non-empty iterable.

NaN is treated as less than all other values except missing.

Julia 1.7

This method requires Julia 1.7 or later.

Examples

1. julia> findmin(identity, 5:9)
2. (5, 1)

4. julia> findmin(-, 1:10)
5. (-10, 10)

7. julia> findmin(first, [(2, :a), (2, :b), (3, :c)])
8. (2, 1)

10. julia> findmin(cos, 0:π/2:2π)
11. (-1.0, 3)

source

1. findmin(itr) -> (x, index)

Return the minimal element of the collection itr and its index or key. If there are multiple minimal elements, then the first one will be returned. NaN is treated as less than all other values except missing.

See also: findmax, argmin, minimum.

Examples

1. julia> findmin([8, 0.1, -9, pi])
2. (-9.0, 3)

4. julia> findmin([1, 7, 7, 6])
5. (1, 1)

7. julia> findmin([1, 7, 7, NaN])
8. (NaN, 4)

source

1. findmin(A; dims) -> (minval, index)

For an array input, returns the value and index of the minimum over the given dimensions. NaN is treated as less than all other values except missing.

Examples

1. julia> A = [1.0 2; 3 4]
2. 2×2 Matrix{Float64}:
3. 1.0  2.0
4. 3.0  4.0

6. julia> findmin(A, dims=1)
7. ([1.0 2.0], CartesianIndex{2}[CartesianIndex(1, 1) CartesianIndex(1, 2)])

9. julia> findmin(A, dims=2)
10. ([1.0; 3.0;;], CartesianIndex{2}[CartesianIndex(1, 1); CartesianIndex(2, 1);;])

source

Base.findmax! — Function

1. findmax!(rval, rind, A) -> (maxval, index)

Find the maximum of A and the corresponding linear index along singleton dimensions of rval and rind, and store the results in rval and rind. NaN is treated as greater than all other values except missing.

source

Base.findmin! — Function

1. findmin!(rval, rind, A) -> (minval, index)

Find the minimum of A and the corresponding linear index along singleton dimensions of rval and rind, and store the results in rval and rind. NaN is treated as less than all other values except missing.

source

Base.sum — Function

1. sum(f, itr; [init])

Sum the results of calling function f on each element of itr.

The return type is Int for signed integers of less than system word size, and UInt for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.

The value returned for empty itr can be specified by init. It must be the additive identity (i.e. zero) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> sum(abs2, [2; 3; 4])
2. 29

Note the important difference between sum(A) and reduce(+, A) for arrays with small integer eltype:

1. julia> sum(Int8[100, 28])
2. 128

4. julia> reduce(+, Int8[100, 28])
5. -128

In the former case, the integers are widened to system word size and therefore the result is 128. In the latter case, no such widening happens and integer overflow results in -128.

source

1. sum(itr; [init])

Returns the sum of all elements in a collection.

The return type is Int for signed integers of less than system word size, and UInt for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.

The value returned for empty itr can be specified by init. It must be the additive identity (i.e. zero) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

See also: reduce, mapreduce, count, union.

Examples

1. julia> sum(1:20)
2. 210

4. julia> sum(1:20; init = 0.0)
5. 210.0

source

1. sum(A::AbstractArray; dims)

Sum elements of an array over the given dimensions.

Examples

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

6. julia> sum(A, dims=1)
7. 1×2 Matrix{Int64}:
8. 4  6

10. julia> sum(A, dims=2)
11. 2×1 Matrix{Int64}:
12. 3
13. 7

source

1. sum(f, A::AbstractArray; dims)

Sum the results of calling function f on each element of an array over the given dimensions.

Examples

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

6. julia> sum(abs2, A, dims=1)
7. 1×2 Matrix{Int64}:
8. 10  20

10. julia> sum(abs2, A, dims=2)
11. 2×1 Matrix{Int64}:
12. 5
13. 25

source

Base.sum! — Function

1. sum!(r, A)

Sum elements of A over the singleton dimensions of r, and write results to r.

Examples

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

6. julia> sum!([1; 1], A)
7. 2-element Vector{Int64}:
8. 3
9. 7

11. julia> sum!([1 1], A)
12. 1×2 Matrix{Int64}:
13. 4  6

source

Base.prod — Function

1. prod(f, itr; [init])

Returns the product of f applied to each element of itr.

The return type is Int for signed integers of less than system word size, and UInt for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.

The value returned for empty itr can be specified by init. It must be the multiplicative identity (i.e. one) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

Examples

1. julia> prod(abs2, [2; 3; 4])
2. 576

source

1. prod(itr; [init])

Returns the product of all elements of a collection.

The return type is Int for signed integers of less than system word size, and UInt for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.

The value returned for empty itr can be specified by init. It must be the multiplicative identity (i.e. one) as it is unspecified whether init is used for non-empty collections.

Julia 1.6

Keyword argument init requires Julia 1.6 or later.

See also: reduce, cumprod, any.

Examples

1. julia> prod(1:5)
2. 120

4. julia> prod(1:5; init = 1.0)
5. 120.0

source

1. prod(A::AbstractArray; dims)

Multiply elements of an array over the given dimensions.

**Example

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