Skip to content

Commit 49694bd

Browse files
committed
add matrix powers
1 parent 8dce3b8 commit 49694bd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/InfiniteLinearAlgebra.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
module InfiniteLinearAlgebra
22
using BlockArrays, BlockBandedMatrices, BandedMatrices, LazyArrays, FillArrays, InfiniteArrays, MatrixFactorizations, LinearAlgebra
33

4-
import Base: +, -, *, /, \, OneTo, getindex, promote_op, _unsafe_getindex, print_matrix_row, size,
4+
import Base: +, -, *, /, \, ^, OneTo, getindex, promote_op, _unsafe_getindex, print_matrix_row, size,
55
AbstractMatrix, AbstractArray, Matrix, Array, Vector, AbstractVector,
66
show, getproperty
7+
import Base.Broadcast: BroadcastStyle
8+
79
import InfiniteArrays: OneToInf, InfUnitRange, Infinity, InfStepRange
810
import FillArrays: AbstractFill
911
import BandedMatrices: BandedMatrix, _BandedMatrix, bandeddata
10-
import LinearAlgebra: lmul!, ldiv!, matprod, qr, QRPackedQ, AbstractTriangular, AbstractQ, adjoint, transpose,
11-
QR
12-
import LazyArrays: CachedArray, DenseColumnMajor, FillLayout, ApplyMatrix, check_mul_axes
13-
import MatrixFactorizations: ql, ql!, QLPackedQ, getL, reflector!, reflectorApply!,
14-
QL
12+
import LinearAlgebra: lmul!, ldiv!, matprod, qr, QRPackedQ, AbstractTriangular, AbstractQ, adjoint, transpose, QR
13+
import LazyArrays: CachedArray, DenseColumnMajor, FillLayout, ApplyMatrix, check_mul_axes, ApplyStyle, LazyArrayApplyStyle, LazyArrayStyle
14+
import MatrixFactorizations: ql, ql!, QLPackedQ, getL, reflector!, reflectorApply!, QL
1515

1616
import BlockArrays: BlockSizes, cumulsizes, _find_block, AbstractBlockVecOrMat, sizes_from_blocks
1717

@@ -21,6 +21,24 @@ import BlockBandedMatrices: _BlockSkylineMatrix, _BandedMatrix, AbstractBlockSiz
2121
BlockSkylineSizes, BlockSkylineMatrix, BlockBandedMatrix, _BlockBandedMatrix, BlockTridiagonal
2222

2323

24+
# Fix ∞ BandedMatrix
25+
ApplyStyle(::typeof(*), ::Type{<:BandedMatrix{<:Any,<:Any,<:OneToInf}}, _::Type{<:AbstractArray}...) =
26+
LazyArrayApplyStyle()
27+
28+
# BroadcastStyle(::Type{<:BandedMatrix{<:Any,<:Any,<:OneToInf}}) = LazyArrayStyle{2}()
29+
30+
^(A::BandedMatrix{T,<:Any,<:OneToInf}, p::Integer) where T =
31+
if p < 0
32+
inv(A)^(-p)
33+
elseif p == 0
34+
Eye{T}(∞)
35+
elseif p == 1
36+
copy(A)
37+
else
38+
A*A^(p-1)
39+
end
40+
41+
2442
if VERSION < v"1.2-"
2543
import Base: has_offset_axes
2644
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ import BandedMatrices: bandeddata, _BandedMatrix
4040
At = BandedMatrix(transpose(A))
4141
@test Ac[1:10,1:10] (A')[1:10,1:10] A[1:10,1:10]'
4242
@test At[1:10,1:10] transpose(A)[1:10,1:10] transpose(A[1:10,1:10])
43+
44+
A = _BandedMatrix(Fill(1,4,∞),∞,1,2)
45+
@test A*A isa ApplyArray
46+
@test (A^2)[1:10,1:10] == (A*A)[1:10,1:10] == (A[1:100,1:100]^2)[1:10,1:10]
47+
@test (A^3)[1:10,1:10] == (A*A*A)[1:10,1:10] == (A[1:100,1:100]^3)[1:10,1:10]
4348
end
49+
4450
include("test_hessenbergq.jl")
4551
include("test_infql.jl")
4652

test/test_infqr.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import BandedMatrices: _BandedMatrix, colsupport
44

55
A = _BandedMatrix(Vcat(Ones(1,∞), (1:∞)', Ones(1,∞)), ∞, 1, 1)
66
C = cache(A)
7-
V = view(C.data,1:10,1:11)
8-
V isa BandedMatrices.BandedSubBandedMatrix
7+
V = view(C.data,:,1:11)
8+
@V isa BandedMatrices.BandedSubBandedMatrix
9+
10+
911
qr!(V)
1012
qr!(C.data)
1113

0 commit comments

Comments
 (0)