Skip to content

Commit

Permalink
Merge pull request #19 from ChrisRackauckas/printing_fix
Browse files Browse the repository at this point in the history
Fix Strang printing
  • Loading branch information
andreasnoack authored Aug 18, 2017
2 parents babf5fb + 96a8063 commit c59afab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/strang.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
export Strang

immutable Strang{T} <: AbstractArray{T, 2}
n :: Int
n :: Int
end
Strang(n::Int) = Strang{Float64}(n)
strang(T, n)= n >1 ? SymTridiagonal(2ones(T, n),-ones(T, n-1)) :
n==1 ? Diagonal([2one(T)]) : error("Invalid dimension ", n)

getindex{T}(S::Strang{T}, i, j) = getindex(strang(T, S.n), i, j)
function getindex{T}(S::Strang{T}, i, j)
i == j && return 2
abs(i - j) == 1 && return -1
0
end
getindex{T}(S::Strang{T}, I...) = getindex(S,ind2sub(size(S),I)...)
size(S::Strang, r::Int) = r==1 || r==2 ? S.n : throw(ArgumentError("Invalid dimension $r"))
size(S::Strang) = S.n, S.n
full{T}(S::Strang{T}) = full(strang(T, S.n))
Expand Down
6 changes: 6 additions & 0 deletions test/strang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Z = Strang(1)
n = rand(1:10)
Z = Strang(n)

for i in 1:n, j in 1:n
i==j && @test Z[i,j] == 2
abs(i-j)==1 && @test Z[i,j] == -1
abs(i-j)>1 && @test Z[i,j] == 0
end

#Matvec product
b = randn(n)
@test_approx_eq Z*b full(Z)*b
Expand Down

0 comments on commit c59afab

Please sign in to comment.