Mercurial > repos > public > sbplib_julia
changeset 184:6945c15a6a7a boundary_conditions
Rename package LazyTensor to LazyTensors
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 20 Jun 2019 21:14:20 +0200 |
parents | b78548f98a75 |
children | 67da5ce895d8 |
files | LazyTensor/Manifest.toml LazyTensor/Project.toml LazyTensor/src/LazyTensor.jl LazyTensor/test/runtests.jl LazyTensors/Manifest.toml LazyTensors/Project.toml LazyTensors/src/LazyTensors.jl LazyTensors/test/runtests.jl |
diffstat | 8 files changed, 233 insertions(+), 233 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyTensor/Manifest.toml Thu Jun 20 21:13:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -# This file is machine-generated - editing it directly is not advised -
--- a/LazyTensor/Project.toml Thu Jun 20 21:13:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -name = "LazyTensor" -uuid = "62fbed2c-918d-11e9-279b-eb3a325b37d3" -authors = ["Jonatan Werpers <jonatan.werpers@it.uu.se>"] -version = "0.1.0" - -[extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test"]
--- a/LazyTensor/src/LazyTensor.jl Thu Jun 20 21:13:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -module LazyTensor - - -""" - Mapping{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} - -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} - -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} -""" -abstract type Mapping{T,R,D} end - -""" - apply(t::Mapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} - -Return the result of the mapping for a given index. -""" -function apply end -export apply - -""" - apply_transpose(t::Mapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {R,D,T} - -Return the result of the transposed mapping for a given index. -""" -function apply_transpose end -export apply_transpose - -""" -Return the dimension of the range space of a given mapping -""" -range_dim(::Mapping{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 - -export range_dim, domain_dim - -""" - range_size(M::Mapping, 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) - -Return the resulting domain size for the mapping applied to a given range_size -""" -function domain_size end - -export range_size, domain_size -# TODO: Think about boundschecking! - - -""" - Operator{T,D} - -A `Mapping{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 - - - -""" - MappingTranspose{T,R,D} <: Mapping{T,D,R} - -Struct for lazy transpose of a Mapping. - -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 appropriate methods of `m`. -""" -struct MappingTranspose{T,R,D} <: Mapping{T,D,R} - tm::Mapping{T,R,D} -end - -# # 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 - -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...) - -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) - - -""" - Application{T,R,D} <: AbstractArray{T,R} - -Struct for lazy application of a Mapping. 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`. -The actual result will be calcualted when indexing into `m*v`. -""" -struct Application{T,R,D} <: AbstractArray{T,R} - t::Mapping{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) - -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)) -# 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) -# # Should we overload some other infix binary operator? -# →(tm::Mapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = Application(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)? - - - -# struct TensorMappingComposition{T,R,K,D} <: Mapping{T,R,D} -# t1::Mapping{T,R,K} -# t2::Mapping{T,K,D} -# end - -# Base.:∘(s::Mapping{T,R,K}, t::Mapping{T,K,D}) where {T,R,K,D} = TensorMappingComposition(s,t) - -# function range_size(tm::TensorMappingComposition{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} -# 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...) -# 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...) -# end - -# # Have i gone too crazy with the type parameters? Maybe they aren't all needed? - -# export → - - -end # module
--- a/LazyTensor/test/runtests.jl Thu Jun 20 21:13:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -using Test -using LazyTensor - - - -@testset "Generic Mapping methods" begin - struct DummyMapping{T,R,D} <: LazyTensor.Mapping{T,R,D} end - LazyTensor.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} <: LazyTensor.Operator{T,D} end -@testset "Generic Operator methods" begin - @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} <: LazyTensor.Mapping{T,R,D} end - - LazyTensor.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply - LazyTensor.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose - - LazyTensor.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = :range_size - LazyTensor.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = :domain_size - - m = DummyMapping{Float64,2,3}() - @test m'' == m - @test apply(m',zeros(Float64,(0,0)),0) == :apply_transpose - @test apply(m'',zeros(Float64,(0,0,0)),0) == :apply - @test apply_transpose(m', zeros(Float64,(0,0,0)),0) == :apply - - @test range_size(m', (0,0)) == :domain_size - @test domain_size(m', (0,0,0)) == :range_size -end - -@testset "TensorApplication" begin - struct DummyMapping{T,R,D} <: LazyTensor.Mapping{T,R,D} end - - LazyTensor.apply(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = (:apply,v,i) - LazyTensor.apply_transpose(m::DummyMapping{T,R,D}, v, i) where {T,R,D} = :apply_transpose - - LazyTensor.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = 2 .* domain_size - LazyTensor.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = range_size.÷2 - - - m = DummyMapping{Int, 1, 1}() - v = [0,1,2] - @test m*v isa AbstractVector{Int} - @test size(m*v) == 2 .*size(v) - @test (m*v)[0] == (:apply,v,0) - @test m*m*v isa AbstractVector{Int} - @test (m*m*v)[0] == (:apply,m*v,0) -end \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyTensors/Manifest.toml Thu Jun 20 21:14:20 2019 +0200 @@ -0,0 +1,2 @@ +# This file is machine-generated - editing it directly is not advised +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyTensors/Project.toml Thu Jun 20 21:14:20 2019 +0200 @@ -0,0 +1,10 @@ +name = "LazyTensors" +uuid = "62fbed2c-918d-11e9-279b-eb3a325b37d3" +authors = ["Jonatan Werpers <jonatan.werpers@it.uu.se>"] +version = "0.1.0" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyTensors/src/LazyTensors.jl Thu Jun 20 21:14:20 2019 +0200 @@ -0,0 +1,165 @@ +module LazyTensors + + +""" + Mapping{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} + +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} + +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} +""" +abstract type Mapping{T,R,D} end + +""" + apply(t::Mapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {R,D,T} + +Return the result of the mapping for a given index. +""" +function apply end +export apply + +""" + apply_transpose(t::Mapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {R,D,T} + +Return the result of the transposed mapping for a given index. +""" +function apply_transpose end +export apply_transpose + +""" +Return the dimension of the range space of a given mapping +""" +range_dim(::Mapping{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 + +export range_dim, domain_dim + +""" + range_size(M::Mapping, 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) + +Return the resulting domain size for the mapping applied to a given range_size +""" +function domain_size end + +export range_size, domain_size +# TODO: Think about boundschecking! + + +""" + Operator{T,D} + +A `Mapping{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 + + + +""" + MappingTranspose{T,R,D} <: Mapping{T,D,R} + +Struct for lazy transpose of a Mapping. + +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 appropriate methods of `m`. +""" +struct MappingTranspose{T,R,D} <: Mapping{T,D,R} + tm::Mapping{T,R,D} +end + +# # 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 + +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...) + +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) + + +""" + Application{T,R,D} <: AbstractArray{T,R} + +Struct for lazy application of a Mapping. 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`. +The actual result will be calcualted when indexing into `m*v`. +""" +struct Application{T,R,D} <: AbstractArray{T,R} + t::Mapping{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) + +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)) +# 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) +# # Should we overload some other infix binary operator? +# →(tm::Mapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = Application(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)? + + + +# struct TensorMappingComposition{T,R,K,D} <: Mapping{T,R,D} +# t1::Mapping{T,R,K} +# t2::Mapping{T,K,D} +# end + +# Base.:∘(s::Mapping{T,R,K}, t::Mapping{T,K,D}) where {T,R,K,D} = TensorMappingComposition(s,t) + +# function range_size(tm::TensorMappingComposition{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} +# 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...) +# 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...) +# end + +# # Have i gone too crazy with the type parameters? Maybe they aren't all needed? + +# export → + + +end # module
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LazyTensors/test/runtests.jl Thu Jun 20 21:14:20 2019 +0200 @@ -0,0 +1,56 @@ +using Test +using LazyTensors + + + +@testset "Generic Mapping methods" begin + struct DummyMapping{T,R,D} <: LazyTensors.Mapping{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 + @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 + + 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 + + LazyTensors.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = :range_size + LazyTensors.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = :domain_size + + m = DummyMapping{Float64,2,3}() + @test m'' == m + @test apply(m',zeros(Float64,(0,0)),0) == :apply_transpose + @test apply(m'',zeros(Float64,(0,0,0)),0) == :apply + @test apply_transpose(m', zeros(Float64,(0,0,0)),0) == :apply + + @test range_size(m', (0,0)) == :domain_size + @test domain_size(m', (0,0,0)) == :range_size +end + +@testset "TensorApplication" begin + struct DummyMapping{T,R,D} <: LazyTensors.Mapping{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 + + LazyTensors.range_size(m::DummyMapping{T,R,D}, domain_size) where {T,R,D} = 2 .* domain_size + LazyTensors.domain_size(m::DummyMapping{T,R,D}, range_size) where {T,R,D} = range_size.÷2 + + + m = DummyMapping{Int, 1, 1}() + v = [0,1,2] + @test m*v isa AbstractVector{Int} + @test size(m*v) == 2 .*size(v) + @test (m*v)[0] == (:apply,v,0) + @test m*m*v isa AbstractVector{Int} + @test (m*m*v)[0] == (:apply,m*v,0) +end \ No newline at end of file