|
| 1 | +#Methods operating on different special matrix types |
| 2 | + |
| 3 | +#Constructs two method definitions taking into account (assumed) commutativity |
| 4 | +# e.g. @commutative f{S,T}(x::S, y::T) = x+y is the same is defining |
| 5 | +# f{S,T}(x::S, y::T) = x+y |
| 6 | +# f{S,T}(y::T, x::S) = f(x, y) |
| 7 | +macro commutative(myexpr) |
| 8 | + @assert myexpr.head===:(=) || myexpr.head===:function #Make sure it is a function definition |
| 9 | + y = copy(myexpr.args[1].args[2:end]) |
| 10 | + reverse!(y) |
| 11 | + reversed_call = Expr(:(=), Expr(:call,myexpr.args[1].args[1],y...), myexpr.args[1]) |
| 12 | + esc(Expr(:block, myexpr, reversed_call)) |
| 13 | +end |
| 14 | + |
| 15 | +for op in (:+, :-) |
| 16 | + #matrixtype1 is the sparser matrix type |
| 17 | + for (idx, matrixtype1) in enumerate([:Diagonal, :Bidiagonal, :Tridiagonal, :Triangular, :Matrix]) |
| 18 | + #matrixtype2 is the denser matrix type |
| 19 | + for matrixtype2 in [:Diagonal, :Bidiagonal, :Tridiagonal, :Triangular, :Matrix][idx+1:end] |
| 20 | + @eval begin #TODO quite a few of these conversions are NOT defined... |
| 21 | + ($op)(A::($matrixtype1), B::($matrixtype2)) = ($op)(convert(($matrixtype2), A), B) |
| 22 | + ($op)(A::($matrixtype2), B::($matrixtype1)) = ($op)(A, convert(($matrixtype2), B)) |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + #matrixtype1 is the sparser matrix type |
| 28 | + for (idx, matrixtype1) in enumerate([:SymTridiagonal]) |
| 29 | + #matrixtype2 is the denser matrix type |
| 30 | + for matrixtype2 in [:Tridiagonal, :Triangular, :Matrix][idx+1:end] |
| 31 | + @eval begin |
| 32 | + ($op)(A::($matrixtype1), B::($matrixtype2)) = ($op)(convert(($matrixtype2), A), B) |
| 33 | + ($op)(A::($matrixtype2), B::($matrixtype1)) = ($op)(A, convert(($matrixtype2), B)) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | +end |
| 38 | + |
0 commit comments