Skip to content

Commit

Permalink
4-tensor Mul/InvPlan (#172)
Browse files Browse the repository at this point in the history
* 4-tensor Mul/InvPlan

* Update test_splines.jl
  • Loading branch information
dlfivefifty authored Dec 5, 2023
1 parent 1cb30aa commit 576463f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ContinuumArrays"
uuid = "7ae1f121-cc2c-504b-ac30-9b923412ae5c"
version = "0.17"
version = "0.17.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
139 changes: 64 additions & 75 deletions src/plans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,6 @@ InvPlan(fact, dims) = InvPlan((fact,), dims)
size(F::InvPlan) = size.(F.factorizations, 1)


function *(P::InvPlan{<:Any,<:Tuple,Int}, x::AbstractVector)
@assert P.dims == 1
only(P.factorizations) \ x # Only a single factorization when dims isa Int
end

function *(P::InvPlan{<:Any,<:Tuple,Int}, X::AbstractMatrix)
if P.dims == 1
only(P.factorizations) \ X # Only a single factorization when dims isa Int
else
@assert P.dims == 2
permutedims(only(P.factorizations) \ permutedims(X))
end
end

function *(P::InvPlan{<:Any,<:Tuple,Int}, X::AbstractArray{<:Any,3})
Y = similar(X)
if P.dims == 1
for j in axes(X,3)
Y[:,:,j] = only(P.factorizations) \ X[:,:,j]
end
elseif P.dims == 2
for k in axes(X,1)
Y[k,:,:] = only(P.factorizations) \ X[k,:,:]
end
else
@assert P.dims == 3
for k in axes(X,1), j in axes(X,2)
Y[k,j,:] = only(P.factorizations) \ X[k,j,:]
end
end
Y
end

function *(P::InvPlan, X::AbstractArray)
for d in P.dims
X = InvPlan(P.factorizations[d], d) * X
end
X
end


"""
MulPlan(matrix, dims)
Expand All @@ -96,44 +55,74 @@ end
MulPlan(mats::Tuple, dims) = MulPlan{eltype(mats), typeof(mats), typeof(dims)}(mats, dims)
MulPlan(mats::AbstractMatrix, dims) = MulPlan((mats,), dims)

function *(P::MulPlan{<:Any,<:Tuple,Int}, x::AbstractVector)
@assert P.dims == 1
only(P.matrices) * x
end

function *(P::MulPlan{<:Any,<:Tuple,Int}, X::AbstractMatrix)
if P.dims == 1
only(P.matrices) * X
else
@assert P.dims == 2
permutedims(only(P.matrices) * permutedims(X))
end
end

function *(P::MulPlan{<:Any,<:Tuple,Int}, X::AbstractArray{<:Any,3})
Y = similar(X)
if P.dims == 1
for j in axes(X,3)
Y[:,:,j] = only(P.matrices) * X[:,:,j]
for (Pln,op,fld) in ((:MulPlan, :*, :(:matrices)), (:InvPlan, :\, :(:factorizations)))
@eval begin
function *(P::$Pln{<:Any,<:Tuple,Int}, x::AbstractVector)
@assert P.dims == 1
$op(only(getfield(P, $fld)), x) # Only a single factorization when dims isa Int
end
elseif P.dims == 2
for k in axes(X,1)
Y[k,:,:] = only(P.matrices) * X[k,:,:]

function *(P::$Pln{<:Any,<:Tuple,Int}, X::AbstractMatrix)
if P.dims == 1
$op(only(getfield(P, $fld)), X) # Only a single factorization when dims isa Int
else
@assert P.dims == 2
permutedims($op(only(getfield(P, $fld)), permutedims(X)))
end
end
else
@assert P.dims == 3
for k in axes(X,1), j in axes(X,2)
Y[k,j,:] = only(P.matrices) * X[k,j,:]

function *(P::$Pln{<:Any,<:Tuple,Int}, X::AbstractArray{<:Any,3})
Y = similar(X)
if P.dims == 1
for j in axes(X,3)
Y[:,:,j] = $op(only(getfield(P, $fld)), X[:,:,j])
end
elseif P.dims == 2
for k in axes(X,1)
Y[k,:,:] = $op(only(getfield(P, $fld)), X[k,:,:])
end
else
@assert P.dims == 3
for k in axes(X,1), j in axes(X,2)
Y[k,j,:] = $op(only(getfield(P, $fld)), X[k,j,:])
end
end
Y
end

function *(P::$Pln{<:Any,<:Tuple,Int}, X::AbstractArray{<:Any,4})
Y = similar(X)
if P.dims == 1
for j in axes(X,3), l in axes(X,4)
Y[:,:,j,l] = $op(only(getfield(P, $fld)), X[:,:,j,l])
end
elseif P.dims == 2
for k in axes(X,1), l in axes(X,4)
Y[k,:,:,l] = $op(only(getfield(P, $fld)), X[k,:,:,l])
end
elseif P.dims == 3
for k in axes(X,1), j in axes(X,2)
Y[k,j,:,:] = $op(only(getfield(P, $fld)), X[k,j,:,:])
end
elseif P.dims == 4
for k in axes(X,1), j in axes(X,2), l in axes(X,3)
Y[k,j,l,:] = $op(only(getfield(P, $fld)), X[k,j,l,:])
end
end
Y
end



*(P::$Pln{<:Any,<:Tuple,Int}, X::AbstractArray) = error("Overload")

function *(P::$Pln, X::AbstractArray)
for (fac,dim) in zip(getfield(P, $fld), P.dims)
X = $Pln(fac, dim) * X
end
X
end
end
Y
end

function *(P::MulPlan, X::AbstractArray)
for d in P.dims
X = MulPlan(P.matrices[d], d) * X
end
X
end

*(A::AbstractMatrix, P::MulPlan) = MulPlan(Ref(A) .* P.matrices, P.dims)
Expand Down
22 changes: 22 additions & 0 deletions test/test_splines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,28 @@ import ContinuumArrays: basis, AdjointBasisLayout, ExpansionLayout, BasisLayout,
X[k, j, :] = L[g,:] \ X[k, j, :]
end
@test PX X

n = size(L,2)
X = randn(n, n, n, n)
P = plan_transform(L, X)
PX = P * X
for k = 1:n, j = 1:n, l = 1:n
X[:, k, j, l] = L[g,:] \ X[:, k, j, l]
end
for k = 1:n, j = 1:n, l = 1:n
X[k, :, j, l] = L[g,:] \ X[k, :, j, l]
end
for k = 1:n, j = 1:n, l = 1:n
X[k, j, :, l] = L[g,:] \ X[k, j, :, l]
end
for k = 1:n, j = 1:n, l = 1:n
X[k, j, l, :] = L[g,:] \ X[k, j, l, :]
end
@test PX X

X = randn(n, n, n, n, n)
P = plan_transform(L, X)
@test_throws ErrorException P * X
end

@testset "Mul coefficients" begin
Expand Down

2 comments on commit 576463f

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96573

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.17.1 -m "<description of version>" 576463f71ae3e08e60d3e519f7dfc82af85c141f
git push origin v0.17.1

Please sign in to comment.