-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlinearalgebra.jl
64 lines (53 loc) · 1.58 KB
/
linearalgebra.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
add!!(A, B) -> A′
`A .+= B` if possible; otherwise return `A .+ B`.
# Examples
```jldoctest
julia> using BangBang: add!!
julia> add!!((1,), (2,))
(3,)
julia> add!!([1], [2])
1-element Vector{Int64}:
3
```
"""
add!!
add!!(A, B) = materialize!!(A, instantiate(broadcasted(+, A, B)))
"""
mul!!(C, A, B, [α, β]) -> C′
"""
mul!!
mul!!(C, A, B) = may(mul!, C, A, B)
mul!!(C, A, B, α, β) = may(mul!, C, A, B, α, β)
pure(::typeof(mul!)) = NoBang.mul
_asbb(::typeof(mul!)) = mul!!
possible(::typeof(mul!), C, A, B) =
implements(push!, C) && _matmuleltype(A, B) <: eltype(C)
possible(::typeof(mul!), C, A, B, α, β) =
implements(push!, C) && _matmuleltype(C, A, B, α, β) <: eltype(C)
# Estimate `eltype` of `C`. This is how it's done in LinearAlgebra.jl
# but maybe it's better to use the approach of
# https://github.com/tpapp/AlgebraResultTypes.jl ?
_matprod(x, y) = x * y + x * y
_matprod(c, a, b, α, β) = a * b * α + a * b * α + c * β
_matmuleltype(A, B) = Base.promote_op(_matprod, eltype(A), eltype(B))
_matmuleltype(C, A, B, α, β) =
Base.promote_op(_matprod, eltype(C), eltype(A), eltype(B), typeof(α), typeof(β))
"""
lmul!!(A, B) -> B′
"""
lmul!!
lmul!!(A, B) = may(lmul!, A, B)
pure(::typeof(lmul!)) = *
_asbb(::typeof(lmul!)) = lmul!!
possible(::typeof(lmul!), A, B) =
implements(push!, B) && _matmuleltype(A, B) <: eltype(B)
"""
rmul!!(A, B) -> A′
"""
rmul!!
rmul!!(A, B) = may(rmul!, A, B)
pure(::typeof(rmul!)) = *
_asbb(::typeof(rmul!)) = rmul!!
possible(::typeof(rmul!), A, B) =
implements(push!, A) && _matmuleltype(A, B) <: eltype(A)