comparison LazyTensors/src/lazy_operations.jl @ 205:70e1f3401d82 boundary_conditions

Merge. Throw away tests written by Jonatan
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 24 Jun 2019 14:43:37 +0200
parents 9d9f7b0e514b 19e579a5031f
children 7b0650021b36
comparison
equal deleted inserted replaced
204:9d9f7b0e514b 205:70e1f3401d82
51 """ 51 """
52 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: LazyArray{T,D} 52 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: LazyArray{T,D}
53 a::T1 53 a::T1
54 b::T2 54 b::T2
55 55
56 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}} 56 @inline function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}}
57 # TODO: Remove boundscheck once assert can be turned off by
58 # optimization flags. Ugly fix.
59 # NOTE: Seems like adding the boundscheck introduces allocations. Can this
60 # be fixed? Since boundscheck is removed when inlined into inbounds this
61 # should not in practise effect performance.
62 @boundscheck @assert size(a)==size(b)
57 return new{T,D,Op,T1,T2}(a,b) 63 return new{T,D,Op,T1,T2}(a,b)
58 end 64 end
59 end 65 end
60 66
61 Base.size(v::LazyElementwiseOperation) = size(v.a) 67 Base.size(v::LazyElementwiseOperation) = size(v.a)
90 return leo.a[I...] / leo.b[I...] 96 return leo.a[I...] / leo.b[I...]
91 end 97 end
92 98
93 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which 99 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which
94 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde 100 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde
95 @inline +̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) 101 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b)
96 @inline -̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b) 102 Base.@propagate_inbounds -̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b)
97 @inline *̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) 103 Base.@propagate_inbounds *̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b)
98 @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) 104 Base.@propagate_inbounds /̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b)
99
100 105
101 # NOTE: Är det knas att vi har till exempel * istället för .* ?? 106 # NOTE: Är det knas att vi har till exempel * istället för .* ??
102 # Oklart om det ens går att lösa.. 107 # Oklart om det ens går att lösa..
103 Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b 108 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
104 Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b 109 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b
105 Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b 110 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
106 111
107 Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b 112 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
108 Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b 113 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
109 Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b 114 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
110 115
111 Base.:*(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b 116 Base.@propagate_inbounds Base.:*(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b
112 Base.:*(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a *̃ b 117 Base.@propagate_inbounds Base.:*(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a *̃ b
113 Base.:*(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b 118 Base.@propagate_inbounds Base.:*(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a *̃ b
114 119
115 # TODO: / seems to be ambiguous 120 # TODO: / seems to be ambiguous
116 # Base.:/(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a /̃ b 121 Base.@propagate_inbounds Base.:/(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a /̃ b
117 # Base.:/(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = a /̃ b 122 Base.@propagate_inbounds Base.:/(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a /̃ b
123 Base.@propagate_inbounds Base.:/(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a /̃ b
124
125 # NOTE: Need to define (/) for AbstractVectorOrMat and Union of LazyArrays in order to avoid ambiguities
126 # with (/) in the LinearAlgebra package. Not sure if it actually could be usefull.
127 Base.@propagate_inbounds Base.:/(a::AbstractVecOrMat, b::Union{LazyArray{T,1}, LazyArray{T,2}}) where {T} = a /̃ b
128 Base.@propagate_inbounds Base.:/(a::Union{LazyArray{T,1}, LazyArray{T,2}}, b::AbstractVecOrMat) where {T} = a /̃ b
118 129
119 export +̃, -̃, *̃, /̃ 130 export +̃, -̃, *̃, /̃
120 131
121 132
122 133