|
| 1 | +export RealOrComplex, BlockArray |
| 2 | +export blocksize, |
| 3 | + blocklength, |
| 4 | + blockvecnorm, |
| 5 | + blockmaxabs, |
| 6 | + blocksimilar, |
| 7 | + blockcopy, |
| 8 | + blockcopy!, |
| 9 | + blockset!, |
| 10 | + blockvecdot, |
| 11 | + blockzeros, |
| 12 | + blockaxpy! |
| 13 | + |
| 14 | +# Define block-arrays |
| 15 | + |
| 16 | +const RealOrComplex{R} = Union{R, Complex{R}} |
| 17 | +const BlockArray{R} = Union{AbstractArray{C, N} where {C <: RealOrComplex{R}, N}, Tuple{Vararg{AbstractArray{C, N} where {C <: RealOrComplex{R}, N}}}} |
| 18 | + |
| 19 | +# Operations on block-arrays |
| 20 | + |
| 21 | +blocksize(x::Tuple) = blocksize.(x) |
| 22 | +blocksize(x::AbstractArray) = size(x) |
| 23 | + |
| 24 | +blocklength(x::Tuple) = sum(blocklength.(x)) |
| 25 | +blocklength(x::AbstractArray) = length(x) |
| 26 | + |
| 27 | +blockvecnorm(x::Tuple) = sqrt(blockvecdot(x, x)) |
| 28 | +blockvecnorm{R <: Number}(x::AbstractArray{R}) = vecnorm(x) |
| 29 | + |
| 30 | +blockmaxabs(x::Tuple) = maximum(blockmaxabs.(x)) |
| 31 | +blockmaxabs{R <: Number}(x::AbstractArray{R}) = maximum(abs, x) |
| 32 | + |
| 33 | +blocksimilar(x::Tuple) = blocksimilar.(x) |
| 34 | +blocksimilar(x::AbstractArray) = similar(x) |
| 35 | + |
| 36 | +blockcopy(x::Tuple) = blockcopy.(x) |
| 37 | +blockcopy(x::Array) = copy(x) |
| 38 | + |
| 39 | +blockcopy!(y::Tuple, x::Tuple) = blockcopy!.(y, x) |
| 40 | +blockcopy!(y::AbstractArray, x::AbstractArray) = copy!(y, x) |
| 41 | + |
| 42 | +blockset!(y::Tuple, x) = blockset!.(y, x) |
| 43 | +blockset!(y::AbstractArray, x) = (y .= x) |
| 44 | + |
| 45 | +blockvecdot{T <: Tuple}(x::T, y::T) = sum(blockvecdot.(x,y)) |
| 46 | +blockvecdot{R <: Number}(x::AbstractArray{R}, y::AbstractArray{R}) = vecdot(x, y) |
| 47 | + |
| 48 | +blockzeros(t::Tuple, s::Tuple) = blockzeros.(t, s) |
| 49 | +blockzeros(t::Type, n::NTuple{N, Integer} where {N}) = zeros(t, n) |
| 50 | +blockzeros(t::Tuple) = blockzeros.(t) |
| 51 | +blockzeros(n::NTuple{N, Integer} where {N}) = zeros(n) |
| 52 | +blockzeros(n::Integer) = zeros(n) |
| 53 | +blockzeros(a::AbstractArray) = zeros(a) |
| 54 | + |
| 55 | +blockaxpy!(z::Tuple, x, alpha::Real, y::Tuple) = blockaxpy!.(z, x, alpha, y) |
| 56 | +blockaxpy!(z::AbstractArray, x, alpha::Real, y::AbstractArray) = (z .= x .+ alpha.*y) |
| 57 | + |
| 58 | +# Define broadcast |
| 59 | + |
| 60 | +import Base: broadcast! |
| 61 | + |
| 62 | +function broadcast!(f::Any, dest::Tuple, op1::Tuple) |
| 63 | + for k = eachindex(dest) |
| 64 | + broadcast!(f, dest[k], op1[k]) |
| 65 | + end |
| 66 | + return dest |
| 67 | +end |
| 68 | + |
| 69 | +function broadcast!(f::Any, dest::Tuple, op1::Tuple, op2::Tuple) |
| 70 | + for k = eachindex(dest) |
| 71 | + broadcast!(f, dest[k], op1[k], op2[k]) |
| 72 | + end |
| 73 | + return dest |
| 74 | +end |
| 75 | + |
| 76 | +function broadcast!(f::Any, dest::Tuple, coef::Number, op2::Tuple) |
| 77 | + for k = eachindex(dest) |
| 78 | + broadcast!(f, dest[k], coef, op2[k]) |
| 79 | + end |
| 80 | + return dest |
| 81 | +end |
| 82 | + |
| 83 | +function broadcast!(f::Any, dest::Tuple, op1::Tuple, coef::Number, op2::Tuple) |
| 84 | + for k = eachindex(dest) |
| 85 | + broadcast!(f, dest[k], op1[k], coef, op2[k]) |
| 86 | + end |
| 87 | + return dest |
| 88 | +end |
| 89 | + |
| 90 | +function broadcast!(f::Any, dest::Tuple, op1::Tuple, coef::Number, op2::Tuple, op3::Tuple) |
| 91 | + for k = eachindex(dest) |
| 92 | + broadcast!(f, dest[k], op1[k], coef, op2[k], op3[k]) |
| 93 | + end |
| 94 | + return dest |
| 95 | +end |
0 commit comments