Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permutecomps/permutecomps! #66

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/man/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CPD
ncomps
normalizecomps
normalizecomps!
permutecomps
permutecomps!
GCPDecompositions.default_constraints
GCPDecompositions.default_algorithm
GCPDecompositions.default_init
Expand Down
1 change: 1 addition & 0 deletions docs/src/related/tensortoolbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
| `NORMALIZE(X,'sort',1)` | TODO |
| `NORMALIZE(X,[],1,I)` | `normalizecomps(X, 1; dims=I)` |
| `NORMALIZE(X,[],2,I)` | `normalizecomps(X, 2; dims=I)` |
| `X = ARRANGE(X,P)` where `P` is a permutation | `permutecomps!(X, P)` |

## Noteworthy differences

Expand Down
2 changes: 1 addition & 1 deletion src/GCPDecompositions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Random: default_rng

# Exports
export CPD
export ncomps, normalizecomps, normalizecomps!
export ncomps, normalizecomps, normalizecomps!, permutecomps, permutecomps!
export gcp
export GCPLosses, GCPConstraints, GCPAlgorithms

Expand Down
34 changes: 34 additions & 0 deletions src/cpd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,37 @@ function _normalizecomps!(
# Return normalized CPD
return M
end

"""
permutecomps(M::CPD, perm)

Permute the components of `M`.
`perm` is a vector or a tuple of length `ncomps(M)` specifying the permutation.

See also: `permutecomps!`.
"""
permutecomps(M::CPD, perm) = permutecomps!(deepcopy(M), perm)

"""
permutecomps!(M::CPD, perm)

Permute the components of `M` in-place.
`perm` is a vector or a tuple of length `ncomps(M)` specifying the permutation.

See also: `permutecomps`.
"""
permutecomps!(M::CPD, perm) = permutecomps!(M, collect(perm))
function permutecomps!(M::CPD, perm::Vector)
# Check that perm is a valid permutation
(length(perm) == ncomps(M) && isperm(perm)) ||
throw(ArgumentError("`perm` is not a valid permutation of the components"))

# Permute weights and factor matrices
M.λ .= M.λ[perm]
for k in Base.OneTo(ndims(M))
M.U[k] .= M.U[k][:, perm]
end

# Return CPD with permuted components
return M
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Expand All @@ -10,6 +11,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"

[compat]
Combinatorics = "1.0.2"
Distributions = "0.25.102"
IntervalSets = "0.7.7"
LossFunctions = "0.11.1"
Expand Down
31 changes: 31 additions & 0 deletions test/items/cpd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,34 @@ end
end
end
end

@testitem "permutecomps" begin
using Combinatorics

@testset "N=$N, K=$K" for N in 1:3, K in 1:3
T = Float64
λfull = T[1, 100, 10000]
U1full, U2full, U3full = T[1 2 3; 4 5 6], T[-1 0 1], T[1 2 3; 4 5 6; 7 8 9]
λ = λfull[1:K]
U = (U1full[:, 1:K], U2full[:, 1:K], U3full[:, 1:K])[1:N]
M = CPD(λ, U)

@testset "perm=$perm" for perm in permutations(1:K)
Mback = deepcopy(M)
Mperm = permutecomps(M, perm)

# Check for mutation
@test M.λ == Mback.λ
@test M.U == Mback.U

# Check weights and factors
@test Mperm.λ == M.λ[perm]
@test all(k -> Mperm.U[k] == M.U[k][:, perm], 1:ndims(Mperm))

# Check in-place version
permutecomps!(M, perm)
@test M.λ == Mperm.λ
@test M.U == Mperm.U
end
end
end
Loading