Mercurial > repos > public > sbplib_julia
diff ext/DiffinitiveSparseArraysExt.jl @ 1736:863385aae454 feature/grids/curvilinear
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 10 Sep 2024 21:59:10 +0200 |
parents | 471a948cd2b2 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ext/DiffinitiveSparseArraysExt.jl Tue Sep 10 21:59:10 2024 +0200 @@ -0,0 +1,25 @@ +module DiffinitiveSparseArraysExt + +using Diffinitive +using Diffinitive.LazyTensors + +using SparseArrays +using Tokens + +""" + sparse(t::LazyTensor) + +The sparse matrix representation of `t`. + +If `L` is a `LazyTensor` and `v` a tensor, then `A = sparse(L)` is constructed +so that `A*reshape(v,:) == reshape(L*v,:)`. +""" +function SparseArrays.sparse(t::LazyTensor) + v = ArrayToken(:v, prod(domain_size(t))) + + v̄ = reshape(v,domain_size(t)...) + tv = reshape(t*v̄, :) + return Tokens._to_matrix(tv, prod(range_size(t)), prod(domain_size(t))) +end + +end