|
| 1 | +using LinearAlgebra: LinearAlgebra |
1 | 2 | using TypeParameterAccessors: similartype |
2 | 3 |
|
3 | 4 | export DiagBlockSparse, DiagBlockSparseTensor |
@@ -448,22 +449,16 @@ function dense( |
448 | 449 | end |
449 | 450 |
|
450 | 451 | # convert to Dense |
451 | | -function dense(T::TensorT) where {TensorT<:DiagBlockSparseTensor} |
452 | | - R = zeros(dense(TensorT), inds(T)) |
453 | | - for i in 1:diaglength(T) |
454 | | - setdiagindex!(R, getdiagindex(T, i), i) |
455 | | - end |
456 | | - return R |
| 452 | +function dense(T::DiagBlockSparseTensor) |
| 453 | + return dense(denseblocks(T)) |
457 | 454 | end |
458 | 455 |
|
459 | 456 | # convert to BlockSparse |
460 | 457 | function denseblocks(D::Tensor) |
461 | 458 | nzblocksD = nzblocks(D) |
462 | | - T = BlockSparseTensor(eltype(D), nzblocksD, inds(D)) |
| 459 | + T = BlockSparseTensor(datatype(D), nzblocksD, inds(D)) |
463 | 460 | for b in nzblocksD |
464 | | - for n in 1:diaglength(D) |
465 | | - setdiagindex!(T, getdiagindex(D, n), n) |
466 | | - end |
| 461 | + T[b] = D[b] |
467 | 462 | end |
468 | 463 | return T |
469 | 464 | end |
@@ -588,8 +583,13 @@ function _contract!!( |
588 | 583 | return R |
589 | 584 | end |
590 | 585 |
|
591 | | -# TODO: Improve this with FillArrays.jl |
592 | | -norm(S::UniformDiagBlockSparseTensor) = sqrt(mindim(S) * abs2(data(S))) |
| 586 | +function LinearAlgebra.norm(D::UniformDiagBlockSparseTensor) |
| 587 | + normD² = zero(eltype(D)) |
| 588 | + for b in nzblocks(D) |
| 589 | + normD² += norm(D[b])^2 |
| 590 | + end |
| 591 | + return √(abs(normD²)) |
| 592 | +end |
593 | 593 |
|
594 | 594 | function contraction_output( |
595 | 595 | T1::TensorT1, labelsT1, T2::TensorT2, labelsT2, labelsR |
|
0 commit comments