Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_array.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | dfb43fdac9fc |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 """ | 1 """ |
| 2 LazyArray{T,D} <: AbstractArray{T,D} | 2 LazyArray{T,D} <: AbstractArray{T,D} |
| 3 | 3 |
| 4 Array which is calcualted lazily when indexing. | 4 Array which is calculated lazily when indexing. |
| 5 | 5 |
| 6 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`. | 6 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`. |
| 7 """ | 7 """ |
| 8 abstract type LazyArray{T,D} <: AbstractArray{T,D} end | 8 abstract type LazyArray{T,D} <: AbstractArray{T,D} end |
| 9 export LazyArray | |
| 10 | 9 |
| 11 struct LazyConstantArray{T,D} <: LazyArray{T,D} | 10 struct LazyConstantArray{T,D} <: LazyArray{T,D} |
| 12 val::T | 11 val::T |
| 13 size::NTuple{D,Int} | 12 size::NTuple{D,Int} |
| 14 end | 13 end |
| 23 """ | 22 """ |
| 24 struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} | 23 struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} |
| 25 f::F | 24 f::F |
| 26 size::NTuple{D,Int} | 25 size::NTuple{D,Int} |
| 27 end | 26 end |
| 28 export LazyFunctionArray | |
| 29 | 27 |
| 30 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} | 28 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} |
| 31 T = typeof(f(ones(D)...)) | 29 T = typeof(f(ones(Int, D)...)) |
| 32 return LazyFunctionArray{F,T,D}(f,size) | 30 return LazyFunctionArray{F,T,D}(f,size) |
| 33 end | 31 end |
| 34 | 32 |
| 35 Base.size(lfa::LazyFunctionArray) = lfa.size | 33 Base.size(lfa::LazyFunctionArray) = lfa.size |
| 36 | 34 |
| 37 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} | 35 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} |
| 38 @boundscheck checkbounds(lfa, I...) | 36 @boundscheck checkbounds(lfa, I...) |
| 39 return @inbounds lfa.f(I...) | 37 return @inbounds @inline lfa.f(I...) |
| 40 end | 38 end |
| 41 | 39 |
| 42 | 40 |
| 43 """ | 41 """ |
| 44 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} | 42 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} |
| 45 Struct allowing for lazy evaluation of elementwise operations on `AbstractArray`s. | 43 Struct allowing for lazy evaluation of element-wise operations on `AbstractArray`s. |
| 46 | 44 |
| 47 A `LazyElementwiseOperation` contains two arrays together with an operation. | 45 A `LazyElementwiseOperation` contains two arrays together with an operation. |
| 48 The operations are carried out when the `LazyElementwiseOperation` is indexed. | 46 The operations are carried out when the `LazyElementwiseOperation` is indexed. |
| 49 """ | 47 """ |
| 50 struct LazyElementwiseOperation{T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}} <: LazyArray{T,D} | 48 struct LazyElementwiseOperation{T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}} <: LazyArray{T,D} |
| 91 Base.@propagate_inbounds *̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) | 89 Base.@propagate_inbounds *̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) |
| 92 Base.@propagate_inbounds /̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) | 90 Base.@propagate_inbounds /̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) |
| 93 | 91 |
| 94 | 92 |
| 95 | 93 |
| 96 # NOTE: Är det knas att vi har till exempel * istället för .* ?? | 94 # Overload +,-,*,/ for LazyArrays |
| 97 # Oklart om det ens går att lösa.. | 95 # Element wise operation for `*` and `/` are not overloaded for due to conflicts with the behavior |
| 96 # of regular `*` and `/` for AbstractArrays. Use tilde versions instead. | |
| 98 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b | 97 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b |
| 98 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | |
| 99 | |
| 99 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b | 100 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b |
| 101 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b | |
| 102 | |
| 100 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b | 103 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b |
| 101 | |
| 102 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | |
| 103 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b | |
| 104 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | 104 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b |
| 105 | 105 |
| 106 # Element wise operation for `*` and `\` are not overloaded due to conflicts with the behavior | 106 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::T) where {T,D} = a +̃ b |
| 107 # of regular `*` and `/` for AbstractArrays. Use tilde versions instead. | 107 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::T) where {T,D} = a -̃ b |
| 108 | 108 |
| 109 export +̃, -̃, *̃, /̃ | 109 Base.@propagate_inbounds Base.:+(a::T, b::LazyArray{T,D}) where {T,D} = a +̃ b |
| 110 Base.@propagate_inbounds Base.:-(a::T, b::LazyArray{T,D}) where {T,D} = a -̃ b |
