Skip to content

Commit 7a42ec2

Browse files
authored
Define matrix functions (#110)
1 parent b440455 commit 7a42ec2

8 files changed

Lines changed: 107 additions & 14 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedDimsArrays"
22
uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.7.16"
4+
version = "0.7.17"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -37,7 +37,7 @@ LinearAlgebra = "1.10"
3737
MapBroadcast = "0.1.6"
3838
Random = "1.10"
3939
SimpleTraits = "0.9.4"
40-
TensorAlgebra = "0.3, 0.4"
40+
TensorAlgebra = "0.4.1"
4141
TupleTools = "1.6.0"
4242
TypeParameterAccessors = "0.4"
4343
julia = "1.10"

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
77

88
[compat]
99
NamedDimsArrays = "0.7"
10-
TensorAlgebra = "0.3, 0.4"
10+
TensorAlgebra = "0.4"
1111
Literate = "2"
1212
Documenter = "1"

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55

66
[compat]
77
NamedDimsArrays = "0.7"
8-
TensorAlgebra = "0.3, 0.4"
8+
TensorAlgebra = "0.4"

src/abstractnameddimsarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ function replacenameddimsindices(a::AbstractNamedDimsArray, replacements::Pair..
309309
new_nameddimsindices = named.(dename.(old_nameddimsindices), last.(replacements))
310310
return replacenameddimsindices(a, (old_nameddimsindices .=> new_nameddimsindices)...)
311311
end
312+
function replacenameddimsindices(a::AbstractNamedDimsArray, replacements::Dict)
313+
return replacenameddimsindices(a) do name
314+
return get(replacements, name, name)
315+
end
316+
end
312317
function mapnameddimsindices(f, a::AbstractNamedDimsArray)
313318
return setnameddimsindices(a, map(f, nameddimsindices(a)))
314319
end

src/nameddimsoperator.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ Base.:*(a::AbstractNamedDimsOperator, b::AbstractNamedDimsOperator) = state(a) *
127127
Base.:*(a::AbstractNamedDimsOperator, b::AbstractNamedDimsArray) = state(a) * state(b)
128128
Base.:*(a::AbstractNamedDimsArray, b::AbstractNamedDimsOperator) = state(a) * state(b)
129129

130+
for f in MATRIX_FUNCTIONS
131+
@eval begin
132+
function Base.$f(a::AbstractNamedDimsOperator)
133+
c = codomain(a)
134+
d = domain(a)
135+
return operator($f(state(a), c, d), c .=> d)
136+
end
137+
end
138+
end
130139
struct NamedDimsOperator{T,N,P<:AbstractNamedDimsArray{T,N},D,C} <:
131140
AbstractNamedDimsOperator{T,N}
132141
parent::P

src/tensoralgebra.jl

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using TensorAlgebra:
44
blockedperm,
55
contract,
66
contract!,
7+
contractadd!,
78
eigen,
89
eigvals,
910
factorize,
@@ -25,14 +26,14 @@ using TensorAlgebra:
2526
using TensorAlgebra.BaseExtensions: BaseExtensions
2627
using TupleTools: TupleTools
2728

28-
function TensorAlgebra.contract!(
29+
function TensorAlgebra.contractadd!(
2930
a_dest::AbstractNamedDimsArray,
3031
a1::AbstractNamedDimsArray,
3132
a2::AbstractNamedDimsArray,
32-
α::Number=true,
33-
β::Number=false,
33+
α::Number,
34+
β::Number,
3435
)
35-
contract!(
36+
contractadd!(
3637
dename(a_dest),
3738
nameddimsindices(a_dest),
3839
dename(a1),
@@ -45,6 +46,12 @@ function TensorAlgebra.contract!(
4546
return a_dest
4647
end
4748

49+
function TensorAlgebra.contract!(
50+
a_dest::AbstractNamedDimsArray, a1::AbstractNamedDimsArray, a2::AbstractNamedDimsArray
51+
)
52+
return contractadd!(a_dest, a1, a2, true, false)
53+
end
54+
4855
function TensorAlgebra.contract(a1::AbstractNamedDimsArray, a2::AbstractNamedDimsArray)
4956
a_dest, nameddimsindices_dest = contract(
5057
dename(a1), nameddimsindices(a1), dename(a2), nameddimsindices(a2)
@@ -79,10 +86,17 @@ function LinearAlgebra.mul!(
7986
a_dest::AbstractNamedDimsArray,
8087
a1::AbstractNamedDimsArray,
8188
a2::AbstractNamedDimsArray,
82-
α::Number=true,
83-
β::Number=false,
89+
α::Number,
90+
β::Number,
8491
)
85-
contract!(a_dest, a1, a2, α, β)
92+
contractadd!(a_dest, a1, a2, α, β)
93+
return a_dest
94+
end
95+
96+
function LinearAlgebra.mul!(
97+
a_dest::AbstractNamedDimsArray, a1::AbstractNamedDimsArray, a2::AbstractNamedDimsArray
98+
)
99+
contract!(a_dest, a1, a2)
86100
return a_dest
87101
end
88102

@@ -301,3 +315,50 @@ function TensorAlgebra.right_null(a::AbstractNamedDimsArray, dimnames_codomain;
301315
domain = setdiff(nameddimsindices(a), codomain)
302316
return right_null(a, codomain, domain; kwargs...)
303317
end
318+
319+
const MATRIX_FUNCTIONS = [
320+
:exp,
321+
:cis,
322+
:log,
323+
:sqrt,
324+
:cbrt,
325+
:cos,
326+
:sin,
327+
:tan,
328+
:csc,
329+
:sec,
330+
:cot,
331+
:cosh,
332+
:sinh,
333+
:tanh,
334+
:csch,
335+
:sech,
336+
:coth,
337+
:acos,
338+
:asin,
339+
:atan,
340+
:acsc,
341+
:asec,
342+
:acot,
343+
:acosh,
344+
:asinh,
345+
:atanh,
346+
:acsch,
347+
:asech,
348+
:acoth,
349+
]
350+
351+
for f in MATRIX_FUNCTIONS
352+
@eval begin
353+
function Base.$f(
354+
a::AbstractNamedDimsArray, dimnames_codomain, dimnames_domain; kwargs...
355+
)
356+
codomain = to_nameddimsindices(a, dimnames_codomain)
357+
domain = to_nameddimsindices(a, dimnames_domain)
358+
fa_unnamed = TensorAlgebra.$f(
359+
dename(a), nameddimsindices(a), codomain, domain; kwargs...
360+
)
361+
return nameddimsarray(fa_unnamed, (codomain..., domain...))
362+
end
363+
end
364+
end

test/Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ GradedArrays = "bc96ca6e-b7c8-4bb6-888e-c93f838762c2"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
1010
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
11+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
1112
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1213
TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
1314
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
@@ -16,11 +17,11 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1617
Adapt = "4"
1718
Aqua = "0.8.9"
1819
BlockArrays = "1"
19-
BlockSparseArrays = "0.8, 0.9, 0.10"
20+
BlockSparseArrays = "0.10"
2021
Combinatorics = "1"
2122
GradedArrays = "0.4"
2223
NamedDimsArrays = "0.7"
2324
SafeTestsets = "0.1"
2425
Suppressor = "0.2"
25-
TensorAlgebra = "0.3, 0.4"
26+
TensorAlgebra = "0.4"
2627
Test = "1.10"

test/test_tensoralgebra.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using LinearAlgebra: factorize, lq, norm, qr, svd
2-
using NamedDimsArrays: dename, nameddimsindices, namedoneto
2+
using NamedDimsArrays: NamedDimsArrays, dename, nameddimsindices, namedoneto
3+
using StableRNGs: StableRNG
34
using TensorAlgebra:
45
TensorAlgebra,
56
contract,
@@ -14,6 +15,7 @@ using TensorAlgebra:
1415
right_polar,
1516
unmatricize
1617
using Test: @test, @testset, @test_broken
18+
1719
elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
1820
@testset "TensorAlgebra (eltype=$(elt))" for elt in elts
1921
@testset "contract" begin
@@ -45,6 +47,21 @@ elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
4547
@test dename(na_split, ("k", "i", "j", "l"))
4648
reshape(dename(na, ("a", "b")), (dename(k), dename(i), dename(j), dename(l)))
4749
end
50+
@testset "Matrix functions" begin
51+
for f in NamedDimsArrays.MATRIX_FUNCTIONS
52+
f == :cbrt && elt <: Complex && continue
53+
f == :cbrt && VERSION < v"1.11-" && continue
54+
@eval begin
55+
i, j, k, l = namedoneto.((2, 2, 2, 2), ("i", "j", "k", "l"))
56+
rng = StableRNG(123)
57+
a = randn(rng, $elt, (i, j, k, l))
58+
fa = $f(a, (j, l), (k, i))
59+
m = dename(matricize(a, (j, l) => "a", (k, i) => "b"), ("a", "b"))
60+
fm = dename(matricize(fa, (j, l) => "a", (k, i) => "b"), ("a", "b"))
61+
@test fm $f(m)
62+
end
63+
end
64+
end
4865
@testset "qr/lq" begin
4966
dims = (2, 2, 2, 2)
5067
i, j, k, l = namedoneto.(dims, ("i", "j", "k", "l"))

0 commit comments

Comments
 (0)