diff 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
line wrap: on
line diff
--- a/src/LazyTensors/lazy_array.jl	Mon May 23 07:20:27 2022 +0200
+++ b/src/LazyTensors/lazy_array.jl	Tue Feb 10 22:41:19 2026 +0100
@@ -1,12 +1,11 @@
 """
     LazyArray{T,D} <: AbstractArray{T,D}
 
-Array which is calcualted lazily when indexing.
+Array which is calculated lazily when indexing.
 
 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`.
 """
 abstract type LazyArray{T,D} <: AbstractArray{T,D} end
-export LazyArray
 
 struct LazyConstantArray{T,D} <: LazyArray{T,D}
 	val::T
@@ -25,10 +24,9 @@
     f::F
     size::NTuple{D,Int}
 end
-export LazyFunctionArray
 
 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D}
-    T = typeof(f(ones(D)...))
+    T = typeof(f(ones(Int, D)...))
     return LazyFunctionArray{F,T,D}(f,size)
 end
 
@@ -36,13 +34,13 @@
 
 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D}
     @boundscheck checkbounds(lfa, I...)
-    return @inbounds lfa.f(I...)
+    return @inbounds @inline lfa.f(I...)
 end
 
 
 """
     LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D}
-Struct allowing for lazy evaluation of elementwise operations on `AbstractArray`s.
+Struct allowing for lazy evaluation of element-wise operations on `AbstractArray`s.
 
 A `LazyElementwiseOperation` contains two arrays together with an operation.
 The operations are carried out when the `LazyElementwiseOperation` is indexed.
@@ -93,17 +91,20 @@
 
 
 
-# NOTE: Är det knas att vi har till exempel * istället för .* ??
-# Oklart om det ens går att lösa..
+# Overload +,-,*,/ for LazyArrays 
+# Element wise operation for `*` and `/` are not overloaded for due to conflicts with the behavior
+# of regular `*` and `/` for AbstractArrays. Use tilde versions instead.
 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
+
 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b
-Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
 
-Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
-Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
+Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
 
-# Element wise operation for `*` and `\` are not overloaded due to conflicts with the behavior
-# of regular `*` and `/` for AbstractArrays. Use tilde versions instead.
+Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::T) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::T) where {T,D} = a -̃ b
 
-export +̃, -̃, *̃, /̃
+Base.@propagate_inbounds Base.:+(a::T, b::LazyArray{T,D}) where {T,D} = a +̃ b
+Base.@propagate_inbounds Base.:-(a::T, b::LazyArray{T,D}) where {T,D} = a -̃  b