|
| 1 | +using FillArrays: Eye, SquareEye |
| 2 | +using LinearAlgebra: LinearAlgebra, mul!, pinv |
| 3 | + |
| 4 | +function check_mul_axes(a::AbstractMatrix, b::AbstractMatrix) |
| 5 | + return axes(a, 2) == axes(b, 1) || throw(DimensionMismatch("Incompatible matrix sizes.")) |
| 6 | +end |
| 7 | + |
| 8 | +function _mul(a::Eye, b::Eye) |
| 9 | + check_mul_axes(a, b) |
| 10 | + T = promote_type(eltype(a), eltype(b)) |
| 11 | + return Eye{T}((axes(a, 1), axes(b, 2))) |
| 12 | +end |
| 13 | +function _mul(a::SquareEye, b::SquareEye) |
| 14 | + check_mul_axes(a, b) |
| 15 | + return Diagonal(diagview(a) .* diagview(b)) |
| 16 | +end |
| 17 | + |
1 | 18 | for f in MATRIX_FUNCTIONS |
2 | 19 | @eval begin |
3 | | - function Base.$f(a::SquareEyeKronecker) |
| 20 | + function Base.$f(a::EyeKronecker) |
| 21 | + LinearAlgebra.checksquare(a.a) |
4 | 22 | return a.a ⊗ $f(a.b) |
5 | 23 | end |
6 | | - function Base.$f(a::KroneckerSquareEye) |
| 24 | + function Base.$f(a::KroneckerEye) |
| 25 | + LinearAlgebra.checksquare(a.b) |
7 | 26 | return $f(a.a) ⊗ a.b |
8 | 27 | end |
9 | | - function Base.$f(a::SquareEyeSquareEye) |
| 28 | + function Base.$f(a::EyeEye) |
| 29 | + LinearAlgebra.checksquare(a) |
10 | 30 | return throw(ArgumentError("`$($f)` on `Eye ⊗ Eye` is not supported.")) |
11 | 31 | end |
12 | 32 | end |
13 | 33 | end |
14 | 34 |
|
15 | | -function LinearAlgebra.pinv(a::SquareEyeKronecker; kwargs...) |
| 35 | +function LinearAlgebra.mul!( |
| 36 | + c::EyeKronecker, a::EyeKronecker, b::EyeKronecker, α::Number, β::Number |
| 37 | +) |
| 38 | + iszero(β) || |
| 39 | + iszero(c) || |
| 40 | + throw( |
| 41 | + ArgumentError( |
| 42 | + "Can't multiple KroneckerArrays with nonzero β and nonzero destination." |
| 43 | + ), |
| 44 | + ) |
| 45 | + check_mul_axes(a.a, b.a) |
| 46 | + mul!(c.b, a.b, b.b, α, β) |
| 47 | + return c |
| 48 | +end |
| 49 | +function LinearAlgebra.mul!( |
| 50 | + c::KroneckerEye, a::KroneckerEye, b::KroneckerEye, α::Number, β::Number |
| 51 | +) |
| 52 | + iszero(β) || |
| 53 | + iszero(c) || |
| 54 | + throw( |
| 55 | + ArgumentError( |
| 56 | + "Can't multiple KroneckerArrays with nonzero β and nonzero destination." |
| 57 | + ), |
| 58 | + ) |
| 59 | + check_mul_axes(a.b, b.b) |
| 60 | + mul!(c.a, a.a, b.a, α, β) |
| 61 | + return c |
| 62 | +end |
| 63 | +function LinearAlgebra.mul!(c::EyeEye, a::EyeEye, b::EyeEye, α::Number, β::Number) |
| 64 | + return throw(ArgumentError("Can't multiple `Eye ⊗ Eye` in-place.")) |
| 65 | +end |
| 66 | + |
| 67 | +function LinearAlgebra.pinv(a::EyeKronecker; kwargs...) |
16 | 68 | return a.a ⊗ pinv(a.b; kwargs...) |
17 | 69 | end |
18 | | -function LinearAlgebra.pinv(a::KroneckerSquareEye; kwargs...) |
| 70 | +function LinearAlgebra.pinv(a::KroneckerEye; kwargs...) |
19 | 71 | return pinv(a.a; kwargs...) ⊗ a.b |
20 | 72 | end |
21 | | -function LinearAlgebra.pinv(a::SquareEyeSquareEye; kwargs...) |
| 73 | +function LinearAlgebra.pinv(a::EyeEye; kwargs...) |
22 | 74 | return a |
23 | 75 | end |
0 commit comments