comparison ext/DiffinitiveSparseArraysExt.jl @ 1858:4a9be96f2569 feature/documenter_logo

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 12 Jan 2025 21:18:44 +0100
parents 471a948cd2b2
children
comparison
equal deleted inserted replaced
1857:ffde7dad9da5 1858:4a9be96f2569
1 module DiffinitiveSparseArraysExt
2
3 using Diffinitive
4 using Diffinitive.LazyTensors
5
6 using SparseArrays
7 using Tokens
8
9 """
10 sparse(t::LazyTensor)
11
12 The sparse matrix representation of `t`.
13
14 If `L` is a `LazyTensor` and `v` a tensor, then `A = sparse(L)` is constructed
15 so that `A*reshape(v,:) == reshape(L*v,:)`.
16 """
17 function SparseArrays.sparse(t::LazyTensor)
18 v = ArrayToken(:v, prod(domain_size(t)))
19
20 v̄ = reshape(v,domain_size(t)...)
21 tv = reshape(t*v̄, :)
22 return Tokens._to_matrix(tv, prod(range_size(t)), prod(domain_size(t)))
23 end
24
25 end