Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active August 20, 2022 05:46
Show Gist options
  • Save antimon2/53c4db6ee7a06f05c43f202da2553df4 to your computer and use it in GitHub Desktop.
Save antimon2/53c4db6ee7a06f05c43f202da2553df4 to your computer and use it in GitHub Desktop.
ReshapeViewSample.jl
[compat]
julia = "1.6"
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.465Z",
"end_time": "2022-08-20T14:37:44.560000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.8.0\nCommit 5544a0fab76 (2022-08-17 13:38 UTC)\nPlatform Info:\n OS: Linux (x86_64-linux-gnu)\n CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-13.0.1 (ORCJIT, skylake)\n Threads: 1 on 12 virtual cores\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.467Z",
"end_time": "2022-08-20T14:37:44.586000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct ReshapableArrayView{T, N, P <: AbstractArray{T}} <: AbstractArray{T, N}\n parent::P\n shape::NTuple{N, Int}\n function ReshapableArrayView(parent::P, shape::NTuple{N, Int}) where {T, N, P <: AbstractArray{T}}\n length(parent) == prod(shape) || throw(DimensionMismatch(\n \"new shape $(shape) must be consistent with array size $(length(parent))\"))\n new{T, N, P}(parent, shape)\n end\nend",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.468Z",
"end_time": "2022-08-20T14:37:44.587000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.IndexStyle(::Type{<:ReshapableArrayView}) = IndexLinear()",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.469Z",
"end_time": "2022-08-20T14:37:44.589000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.length(a::ReshapableArrayView) = length(a.parent)\nBase.size(a::ReshapableArrayView) = a.shape\nBase.iterate(a::ReshapableArrayView, st...) = iterate(a.parent, st...)",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.470Z",
"end_time": "2022-08-20T14:37:44.590000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.getindex(a::ReshapableArrayView, index::Int) = a.parent[index]",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.471Z",
"end_time": "2022-08-20T14:37:46.006000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A0 = ReshapableArrayView(1:4, (2, 2))",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "2×2 ReshapableArrayView{Int64, 2, UnitRange{Int64}}:\n 1 3\n 2 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.472Z",
"end_time": "2022-08-20T14:37:46.195000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A0[1, 2]",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.474Z",
"end_time": "2022-08-20T14:37:46.195000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.setindex!(a::ReshapableArrayView, v, index::Int) = a.parent[index] = v",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.475Z",
"end_time": "2022-08-20T14:37:46.539000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A1 = ReshapableArrayView([1:4;], (2, 2))",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "2×2 ReshapableArrayView{Int64, 2, Vector{Int64}}:\n 1 3\n 2 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.477Z",
"end_time": "2022-08-20T14:37:46.548000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A1[1, 2] = 2",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.478Z",
"end_time": "2022-08-20T14:37:46.549000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A1",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "2×2 ReshapableArrayView{Int64, 2, Vector{Int64}}:\n 1 2\n 2 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.479Z",
"end_time": "2022-08-20T14:37:46.884000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A1.parent",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "4-element Vector{Int64}:\n 1\n 2\n 2\n 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.480Z",
"end_time": "2022-08-20T14:37:47.285000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "const _Dims = (NTuple{N, Int} where N) # === Base.Dims",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "Tuple{Vararg{Int64, N}} where N"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.481Z",
"end_time": "2022-08-20T14:37:47.286000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Base.reshape(a::ReshapableArrayView, dims::_Dims) = reshapeview(a, dims)",
"execution_count": 14,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.482Z",
"end_time": "2022-08-20T14:37:47.481000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "reshapeview(a::ReshapableArrayView, dims::_Dims) = ReshapableArrayView(a.parent, dims)",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "reshapeview (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.483Z",
"end_time": "2022-08-20T14:37:47.484000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "reshapeview(a, index) = reshapeview(a, Base._reshape_uncolon(a, (index,)))\nreshapeview(a, indices::Vararg{Int, N}) where N = reshapeview(a, indices)\nreshapeview(a, indices...) = reshapeview(a, Base._reshape_uncolon(a, indices))",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "reshapeview (generic function with 4 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.484Z",
"end_time": "2022-08-20T14:37:47.485000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "reshapeview(a::AbstractArray, dims::_Dims) = ReshapableArrayView(a, dims)",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "reshapeview (generic function with 5 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.486Z",
"end_time": "2022-08-20T14:37:47.918000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2 = reshapeview([1 2; 3 4], 4)",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "4-element ReshapableArrayView{Int64, 1, Matrix{Int64}}:\n 1\n 3\n 2\n 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.487Z",
"end_time": "2022-08-20T14:37:47.923000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2[2] = 2 ",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.488Z",
"end_time": "2022-08-20T14:37:47.925000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "4-element ReshapableArrayView{Int64, 1, Matrix{Int64}}:\n 1\n 2\n 2\n 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.489Z",
"end_time": "2022-08-20T14:37:48.504000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2.parent",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "2×2 Matrix{Int64}:\n 1 2\n 2 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.490Z",
"end_time": "2022-08-20T14:37:48.854000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2′ = reshapeview(A2, 1, :)",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "1×4 ReshapableArrayView{Int64, 2, Matrix{Int64}}:\n 1 2 2 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.491Z",
"end_time": "2022-08-20T14:37:49.436000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2′[2:3] .= 3",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "2-element view(::ReshapableArrayView{Int64, 1, Matrix{Int64}}, 2:3) with eltype Int64:\n 3\n 3"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.492Z",
"end_time": "2022-08-20T14:37:49.438000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2′",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "1×4 ReshapableArrayView{Int64, 2, Matrix{Int64}}:\n 1 3 3 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.493Z",
"end_time": "2022-08-20T14:37:49.439000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "4-element ReshapableArrayView{Int64, 1, Matrix{Int64}}:\n 1\n 3\n 3\n 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.494Z",
"end_time": "2022-08-20T14:37:49.440000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A2.parent",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "2×2 Matrix{Int64}:\n 1 3\n 3 4"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.495Z",
"end_time": "2022-08-20T14:37:49.705000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "axes(A2′)",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "(Base.OneTo(1), Base.OneTo(4))"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:37:43.496Z",
"end_time": "2022-08-20T14:37:50.938000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A3 = reshapeview(collect(1:60), 3, 4, 5)",
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 28,
"data": {
"text/plain": "3×4×5 ReshapableArrayView{Int64, 3, Vector{Int64}}:\n[:, :, 1] =\n 1 4 7 10\n 2 5 8 11\n 3 6 9 12\n\n[:, :, 2] =\n 13 16 19 22\n 14 17 20 23\n 15 18 21 24\n\n[:, :, 3] =\n 25 28 31 34\n 26 29 32 35\n 27 30 33 36\n\n[:, :, 4] =\n 37 40 43 46\n 38 41 44 47\n 39 42 45 48\n\n[:, :, 5] =\n 49 52 55 58\n 50 53 56 59\n 51 54 57 60"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-08-20T05:39:53.798Z",
"end_time": "2022-08-20T14:39:53.144000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A3[2, 3, 4]",
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "44"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.8",
"display_name": "Julia 1.8.0",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.8.0"
},
"gist": {
"id": "",
"data": {
"description": "ReshapeViewSample.jl",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment