-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move FiniteDifferences support to package extension (#132)
* Move FiniteDifferences support to package extension * Refactor implementation
- Loading branch information
Showing
3 changed files
with
33 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module TensorKitFiniteDifferencesExt | ||
|
||
using TensorKit | ||
using TensorKit: sqrtdim, isqrtdim | ||
using VectorInterface: scale! | ||
using FiniteDifferences | ||
|
||
function FiniteDifferences.to_vec(t::T) where {T<:TensorKit.TrivialTensorMap} | ||
vec, from_vec = to_vec(t.data) | ||
return vec, x -> T(from_vec(x), codomain(t), domain(t)) | ||
end | ||
function FiniteDifferences.to_vec(t::AbstractTensorMap) | ||
# convert to vector of vectors to make use of existing functionality | ||
vec_of_vecs = [b * sqrtdim(c) for (c, b) in blocks(t)] | ||
vec, back = FiniteDifferences.to_vec(vec_of_vecs) | ||
|
||
function from_vec(x) | ||
t′ = similar(t) | ||
xvec_of_vecs = back(x) | ||
for (i, (c, b)) in enumerate(blocks(t′)) | ||
scale!(b, xvec_of_vecs[i], isqrtdim(c)) | ||
end | ||
return t′ | ||
end | ||
|
||
return vec, from_vec | ||
end | ||
FiniteDifferences.to_vec(t::TensorKit.AdjointTensorMap) = to_vec(copy(t)) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters