Mercurial > repos > public > sbplib_julia
changeset 186:715ff09bb2ce boundary_conditions
Rename and export types in LazyTensors follow julia conventions
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 20 Jun 2019 21:31:15 +0200 |
parents | 67da5ce895d8 |
children | e8e21db70112 |
files | LazyTensors/src/LazyTensors.jl LazyTensors/test/runtests.jl |
diffstat | 2 files changed, 59 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyTensors/src/LazyTensors.jl Thu Jun 20 21:15:48 2019 +0200 +++ b/LazyTensors/src/LazyTensors.jl Thu Jun 20 21:31:15 2019 +0200 @@ -2,28 +2,29 @@ """ - Mapping{T,R,D} + TensorMapping{T,R,D} Describes a mapping of a D dimension tensor to an R dimension tensor. The action of the mapping is implemented through the method - apply(t::Mapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} + apply(t::TensorMapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} The size of range tensor should be dependent on the size of the domain tensor and the type should implement the methods - range_size(::Mapping{T,R,D}, domain_size::NTuple{D,Integer}) where {T,R,D} - domain_size(::Mapping{T,R,D}, range_size::NTuple{R,Integer}) where {T,R,D} + range_size(::TensorMapping{T,R,D}, domain_size::NTuple{D,Integer}) where {T,R,D} + domain_size(::TensorMapping{T,R,D}, range_size::NTuple{R,Integer}) where {T,R,D} to allow querying for one or the other. Optionally the action of the transpose may be defined through - apply_transpose(t::Mapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} + apply_transpose(t::TensorMapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} """ -abstract type Mapping{T,R,D} end +abstract type TensorMapping{T,R,D} end +export TensorMapping """ - apply(t::Mapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} + apply(t::TensorMapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} Return the result of the mapping for a given index. """ @@ -31,7 +32,7 @@ export apply """ - apply_transpose(t::Mapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {R,D,T} + apply_transpose(t::TensorMapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {R,D,T} Return the result of the transposed mapping for a given index. """ @@ -41,24 +42,24 @@ """ Return the dimension of the range space of a given mapping """ -range_dim(::Mapping{T,R,D}) where {T,R,D} = R +range_dim(::TensorMapping{T,R,D}) where {T,R,D} = R """ Return the dimension of the domain space of a given mapping """ -domain_dim(::Mapping{T,R,D}) where {T,R,D} = D +domain_dim(::TensorMapping{T,R,D}) where {T,R,D} = D export range_dim, domain_dim """ - range_size(M::Mapping, domain_size) + range_size(M::TensorMapping, domain_size) Return the resulting range size for the mapping applied to a given domain_size """ function range_size end """ - domain_size(M::Mapping, range_size) + domain_size(M::TensorMapping, range_size) Return the resulting domain size for the mapping applied to a given range_size """ @@ -69,92 +70,95 @@ """ - Operator{T,D} + TensorOperator{T,D} -A `Mapping{T,D,D}` where the range and domain tensor have the same number of +A `TensorMapping{T,D,D}` where the range and domain tensor have the same number of dimensions and the same size. """ -abstract type Operator{T,D} <: Mapping{T,D,D} end -domain_size(::Operator{T,D}, range_size::NTuple{D,Integer}) where {T,D} = range_size -range_size(::Operator{T,D}, domain_size::NTuple{D,Integer}) where {T,D} = domain_size +abstract type TensorOperator{T,D} <: TensorMapping{T,D,D} end +export TensorOperator +domain_size(::TensorOperator{T,D}, range_size::NTuple{D,Integer}) where {T,D} = range_size +range_size(::TensorOperator{T,D}, domain_size::NTuple{D,Integer}) where {T,D} = domain_size """ - MappingTranspose{T,R,D} <: Mapping{T,D,R} + LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R} -Struct for lazy transpose of a Mapping. +Struct for lazy transpose of a TensorMapping. If a mapping implements the the `apply_transpose` method this allows working with -the transpose of mapping `m` by using `m'`. `m'` will work as a regular Mapping lazily calling +the transpose of mapping `m` by using `m'`. `m'` will work as a regular TensorMapping lazily calling the appropriate methods of `m`. """ -struct MappingTranspose{T,R,D} <: Mapping{T,D,R} - tm::Mapping{T,R,D} +struct LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R} + tm::TensorMapping{T,R,D} end +export LazyTensorMappingTranspose # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors? -Base.adjoint(t::Mapping) = MappingTranspose(t) -Base.adjoint(t::MappingTranspose) = t.tm +Base.adjoint(t::TensorMapping) = LazyTensorMappingTranspose(t) +Base.adjoint(t::LazyTensorMappingTranspose) = t.tm -apply(tm::MappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {T,R,D} = apply_transpose(tm.tm, v, I...) -apply_transpose(tm::MappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,D} = apply(tm.tm, v, I...) +apply(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {T,R,D} = apply_transpose(tm.tm, v, I...) +apply_transpose(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,D} = apply(tm.tm, v, I...) -range_size(tmt::MappingTranspose{T,R,D}, d_size::NTuple{R,Integer}) where {T,R,D} = domain_size(tmt.tm, domain_size) -domain_size(tmt::MappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, range_size) +range_size(tmt::LazyTensorMappingTranspose{T,R,D}, d_size::NTuple{R,Integer}) where {T,R,D} = domain_size(tmt.tm, domain_size) +domain_size(tmt::LazyTensorMappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, range_size) """ - Application{T,R,D} <: AbstractArray{T,R} + LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R} -Struct for lazy application of a Mapping. Created using `*`. +Struct for lazy application of a TensorMapping. Created using `*`. -Allows the result of a `Mapping` applied to a vector to be treated as an `AbstractArray`. -With a mapping `m` and a vector `v` the Application object can be created by `m*v`. +Allows the result of a `TensorMapping` applied to a vector to be treated as an `AbstractArray`. +With a mapping `m` and a vector `v` the LazyTensorMappingApplication object can be created by `m*v`. The actual result will be calcualted when indexing into `m*v`. """ -struct Application{T,R,D} <: AbstractArray{T,R} - t::Mapping{T,R,D} +struct LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R} + t::TensorMapping{T,R,D} o::AbstractArray{T,D} end - -Base.:*(tm::Mapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = Application(tm,o) +export LazyTensorMappingApplication -Base.getindex(ta::Application{T,R,D}, I::Vararg) where {T,R,D} = apply(ta.t, ta.o, I...) -Base.size(ta::Application{T,R,D}) where {T,R,D} = range_size(ta.t,size(ta.o)) +Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) + +Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg) where {T,R,D} = apply(ta.t, ta.o, I...) +Base.size(ta::LazyTensorMappingApplication{T,R,D}) where {T,R,D} = range_size(ta.t,size(ta.o)) # TODO: What else is needed to implement the AbstractArray interface? # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→' -Base.:*(args::Union{Mapping{T}, AbstractArray{T}}...) where T = foldr(*,args) +Base.:*(args::Union{TensorMapping{T}, AbstractArray{T}}...) where T = foldr(*,args) # # Should we overload some other infix binary operator? -# →(tm::Mapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = Application(tm,o) +# →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) # TODO: We need to be really careful about good error messages. -# For example what happens if you try to multiply Application with a Mapping(wrong order)? +# For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)? -# struct TensorMappingComposition{T,R,K,D} <: Mapping{T,R,D} -# t1::Mapping{T,R,K} -# t2::Mapping{T,K,D} +# struct LazyTensorMappingComposition{T,R,K,D} <: TensorMapping{T,R,D} +# t1::TensorMapping{T,R,K} +# t2::TensorMapping{T,K,D} # end -# Base.:∘(s::Mapping{T,R,K}, t::Mapping{T,K,D}) where {T,R,K,D} = TensorMappingComposition(s,t) +# Base.:∘(s::TensorMapping{T,R,K}, t::TensorMapping{T,K,D}) where {T,R,K,D} = LazyTensorMappingComposition(s,t) -# function range_size(tm::TensorMappingComposition{T,R,K,D}, domain_size::NTuple{D,Integer}) where {T,R,K,D} +# function range_size(tm::LazyTensorMappingComposition{T,R,K,D}, domain_size::NTuple{D,Integer}) where {T,R,K,D} # range_size(tm.t1, domain_size(tm.t2, domain_size)) # end -# function domain_size(tm::TensorMappingComposition{T,R,K,D}, range_size::NTuple{R,Integer}) where {T,R,K,D} +# function domain_size(tm::LazyTensorMappingComposition{T,R,K,D}, range_size::NTuple{R,Integer}) where {T,R,K,D} # domain_size(tm.t1, domain_size(tm.t2, range_size)) # end -# function apply(c::TensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D} -# apply(c.t1, Application(c.t2,v), I...) +# function apply(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D} +# apply(c.t1, LazyTensorMappingApplication(c.t2,v), I...) # end -# function apply_transpose(c::TensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D} -# apply_transpose(c.t2, Application(c.t1',v), I...) +# function apply_transpose(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D} +# apply_transpose(c.t2, LazyTensorMappingApplication(c.t1',v), I...) # end # # Have i gone too crazy with the type parameters? Maybe they aren't all needed?
--- a/LazyTensors/test/runtests.jl Thu Jun 20 21:15:48 2019 +0200 +++ b/LazyTensors/test/runtests.jl Thu Jun 20 21:31:15 2019 +0200 @@ -4,21 +4,21 @@ @testset "Generic Mapping methods" begin - struct DummyMapping{T,R,D} <: LazyTensors.Mapping{T,R,D} end + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply @test range_dim(DummyMapping{Int,2,3}()) == 2 @test domain_dim(DummyMapping{Int,2,3}()) == 3 @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0) == :apply end -struct DummyOperator{T,D} <: LazyTensors.Operator{T,D} end @testset "Generic Operator methods" begin + struct DummyOperator{T,D} <: TensorOperator{T,D} end @test range_size(DummyOperator{Int,2}(), (3,5)) == (3,5) @test domain_size(DummyOperator{Float64, 3}(), (3,3,1)) == (3,3,1) end @testset "Mapping transpose" begin - struct DummyMapping{T,R,D} <: LazyTensors.Mapping{T,R,D} end + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose @@ -37,7 +37,7 @@ end @testset "TensorApplication" begin - struct DummyMapping{T,R,D} <: LazyTensors.Mapping{T,R,D} end + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end LazyTensors.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = (:apply,v,i) LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose