using LinearAlgebra, Random, Statistics
include(joinpath(@__DIR__, "observations.jl"))
counts = [7, 11, 13]
heterogeneous = [7, "Hill", 2.5, pi]
typed = Int16[7, 11]
names = String["Hill", "Bay"]
probe("vector_literals", (counts, heterogeneous, typed, names),
(eltype(counts), eltype(heterogeneous), eltype(typed), eltype(names),
eltype(Int16[]), eltype(String[]), isempty(Int16[]), isempty(String[])),
(Int, Any, Int16, String, Int16, String, true, true))
prefix = counts[1:2]
prefix[1] = 91
probe("vector_index", (counts, 2, 1:2), (counts[2], counts[end], prefix, counts[1]),
(11, 13, [91, 11], 7))
buffer = Array{Int16}(undef, 2)
cube = Array{Int16}(undef, (2, 2, 2))
fill!(buffer, 3)
fill!(cube, 4)
references = Vector{String}(undef, 1)
probe("array_undef", "assign numeric arrays before reading; probe an undefined reference",
(buffer, size(cube), sum(cube), isassigned(references, 1), outcome(() -> references[1])),
(Int16[3, 3], (2, 2, 2), 32, false, "UndefRefError"))
a, b, c = [7, 11], [13, 17], [19, 23]
probe("matrix_literals", (a, b, c),
(size([7 11 13]), [7;; 11;; 13], [7 11; 13 17], [7:8 13:14], [a b c],
[[7 11]; [13 17]; [19 23]]),
((1, 3), [7 11 13], [7 11; 13 17], [7 13; 8 14], [7 13 19; 11 17 23],
[7 11; 13 17; 19 23]))
probe("array_vcat", (a,b,c), ([a; b; c], vcat(a,b,c)),
([7,11,13,17,19,23], [7,11,13,17,19,23]))
probe("array_hcat", (a,b,c), ([a b c], hcat(a,b,c)),
([7 13 19; 11 17 23], [7 13 19; 11 17 23]))
probe("array_hvcat", (7,11,13,17), ([7 11; 13 17], hvcat((2,2),7,11,13,17)),
([7 11; 13 17], [7 11; 13 17]))
layered = cat([7 11], [13 17]; dims=3)
probe("array_cat", ([7 11],[13 17],3), (size(layered), layered[:,:,1], layered[:,:,2]),
((1,2,2), [7 11], [13 17]))
r = range(2, 5.5; step=2)
probe("array_range", (2,5.5,2),
([2:5...], collect(2:3:11), collect(Float32, 2:2:6),
collect(range(2; length=3)), collect(range(2; stop=4)),
collect(range(2; step=3,length=3)), collect(range(2,8;length=4)),
collect(range(;start=2,step=2,stop=8)), r isa AbstractRange, collect(r)),
([2,3,4,5], [2,5,8,11], Float32[2,4,6], [2,3,4], [2,3,4], [2,5,8],
[2.0,4.0,6.0,8.0], [2,4,6,8], true, [2.0,4.0]))
probe("array_comprehension", (2:4,5:6),
([n^2 for n in 2:4], [n*m for n in 2:4, m in 5:6],
collect(n^2 for n in 2:4), round(sum(1/n^2 for n in 1:4); digits=6)),
([4,9,16], [10 12;15 18;20 24], [4,9,16], 1.423611))
M = Int16[7 11 13; 17 19 23]
probe("array_eltype", M, eltype(M), Int16)
probe("array_length", M, length(M), 6)
probe("array_ndims", M, ndims(M), 2)
probe("array_size", M, size(M), (2,3))
probe("array_size_axis", (M,2), size(M,2), 3)
probe("array_axes", M, map(collect, axes(M)), ([1,2],[1,2,3]))
probe("array_eachindex", M, (collect(eachindex(M)), [M[i] for i in eachindex(M)]),
([1,2,3,4,5,6], [7,17,11,19,13,23]))
probe("array_stride", (M,1,2), (stride(M,1),stride(M,2)), (1,2))
typedzero = zeros(Int8, 2, 3)
defaultzero = zeros((2, 3))
probe("array_zeros", (Int8,2,3), (eltype(typedzero), eltype(defaultzero), size(typedzero), all(iszero, typedzero)),
(Int8,Float64,(2,3),true))
ones_sample = ones(Int16, 2, 1)
probe("array_ones", (Int16,2,1), (eltype(ones_sample), ones_sample), (Int16,Int16[1; 1;;]))
yes, no = trues(1,2), falses(2,1)
probe("array_trues", (1,2), (yes isa BitMatrix, size(yes), all(yes)), (true,(1,2),true))
probe("array_falses", (2,1), (no isa BitMatrix, size(no), any(no)), (true,(2,1),false))
reshaped = reshape(counts, 1, 3)
reshaped[1,2] = 41
probe("array_reshape", ([7,11,13],(1,3),41), (size(reshaped),counts[2]), ((1,3),41))
nested = [[7], [11]]
shallow, deep = copy(nested), deepcopy(nested)
nested[1][1] = 31
probe("array_copy", "nested vector, then mutate original child", (shallow === nested,shallow[1][1]), (false,31))
probe("array_deepcopy", "nested vector, then mutate original child", (deep[1] === nested[1],deep[1][1]), (false,7))
similar_buffer = similar(M, Float32, (2,2))
fill!(similar_buffer, 4)
probe("array_similar", (M,Float32,(2,2)), (eltype(similar_buffer),size(similar_buffer),sum(similar_buffer)),
(Float32,(2,2),16.0f0))
words = UInt16[0x0010, 0x00f0]
signed = reinterpret(Int16, words)
signed[1] = -2
probe("array_reinterpret", (UInt16[0x0010,0x00f0],Int16,-2), (words[1],eltype(signed),signed[2]),
(0xfffe,Int16,Int16(240)))
uniform = rand(MersenneTwister(19),Float32,2,3)
normal = randn(MersenneTwister(19),Float32,2,3)
probe("array_rand", (19,Float32,2,3),
(eltype(uniform),size(uniform),all(0 <= x < 1 for x in uniform),
uniform == rand(MersenneTwister(19),Float32,2,3)), (Float32,(2,3),true,true))
probe("array_randn", (19,Float32,2,3),
(eltype(normal),size(normal),all(isfinite,normal),normal == randn(MersenneTwister(19),Float32,2,3)),
(Float32,(2,3),true,true))
probe("array_identity", (Int16,2,3), Matrix{Int16}(I,2,3), Int16[1 0 0; 0 1 0])
same = fill!(counts, 5)
probe("array_fill_in_place", (3,5), (same === counts,counts), (true,[5,5,5]))
cell = [7]
repeated = fill(cell, 2)
repeated[1][1] = 31
probe("array_fill", (cell,2), (repeated[1] === repeated[2],repeated[2][1]), (true,31))
queue = Int[]
returned = push!(queue, 7)
probe("array_push", (Int[],7), (returned === queue,queue), (true,[7]))
append!(queue, [11,13])
probe("array_append", ([7],[11,13]), queue, [7,11,13])
pushfirst!(queue,5)
probe("array_pushfirst", ([7,11,13],5), queue, [5,7,11,13])
probe("array_pop", copy(queue), (pop!(queue),queue), (13,[5,7,11]))
insert!(queue,2,41)
probe("array_insert", ([5,7,11],2,41), queue, [5,41,7,11])
deleteat!(queue,2)
probe("array_deleteat", ([5,41,7,11],2), queue, [5,7,11])
probe("array_popfirst", copy(queue), (popfirst!(queue),queue), (5,[7,11]))
probe("array_splice", (copy(queue),1), (splice!(queue,1),queue), (7,[11]))
resize!(queue,3)
queue[2:3] .= 4
probe("array_resize", ([11],3,4), queue, [11,4,4])
empty!(queue)
probe("array_empty", [11,4,4], queue, Int[])
probe("array_extend_copy", (queue,9), (queue,[queue;9]), (Int[],[9]))
work_queue = Int[]
push!(work_queue,7)
append!(work_queue,[11,13])
pushfirst!(work_queue,5)
removed = pop!(work_queue)
insert!(work_queue,2,41)
inserted = copy(work_queue)
deleteat!(work_queue,2)
probe("array_queue_walk", "push 7, append 11/13, prepend 5, pop, insert and delete 41",
(removed,inserted,work_queue), (13,[5,41,7,11],[5,7,11]))
field = [7,11,13]
field[1] = 31
field .+= 2
field[1:2] .= 0
probe("array_broadcast_update", ([7,11,13],31,2,1:2), field, [0,0,15])
A = [2 1; 0 2]
B = [3 5; 7 11]
probe("array_broadcast", (A,B,[3,5]),
([3,5] .+ A, string.("site-", 7:9), 2 .* A .+ B, 2*A+B),
([5 4;5 7], ["site-7","site-8","site-9"], [7 7;7 15], [7 7;7 15]))
probe("array_unary", A, (+A,-A), ([2 1;0 2],[-2 -1;0 -2]))
right = [4,6]
probe("array_binary", (A,B,right),
(A+B,A-B,A*B,A^2,A\right,B/[2 0;0 1]),
([5 6;7 13],[-1 -4;-7 -9],[13 21;14 22],[4 4;0 4],[0.5,3.0],[1.5 5.0;3.5 11.0]))
probe("array_dot", (A,B),
(A .+ B,A .* B,A .- B,A .^ 2), ([5 6;7 13],[6 5;0 22],[-1 -4;-7 -9],[4 1;0 4]))
probe("array_compare", (A,B), (A==copy(A),A==B,A .== B), (true,false,falses(2,2)))
series = [7,11,13]
probe("array_reduce", (series,B),
(sum(series),prod(series),maximum(series),minimum(series),argmax(series),argmin(series),
mean(series),sort([13,7,11]),sum(B;dims=1)),
(31,1001,13,7,3,1,31/3,[7,11,13],[10 16]))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-containers-and-numbers/arrays.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
arrays.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a207.zip