Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia 1.0 compability #25

Merged
merged 3 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
language: cpp
compiler:
- clang
language: julia
os:
- linux
- osx
julia:
- 0.6
- 1.0
- nightly
notifications:
email: false
env:
matrix:
- JULIAVERSION="juliareleases"
- JULIAVERSION="julianightlies"
before_install:
- sudo add-apt-repository ppa:staticfloat/julia-deps -y
- sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y
- sudo apt-get update -qq -y
- sudo apt-get install libpcre3-dev julia -y
script:
- julia -e 'Pkg.init(); run(`ln -s $(pwd()) $(Pkg.dir())/SpecialMatrices`); Pkg.pin("SpecialMatrices"); Pkg.resolve()'
- julia -e 'using SpecialMatrices; @assert isdefined(:SpecialMatrices); @assert typeof(SpecialMatrices) === Module'
- if [ $JULIAVERSION = "julianightlies" ]; then julia --code-coverage test/runtests.jl; fi
- if [ $JULIAVERSION = "juliareleases" ]; then julia test/runtests.jl; fi
email: false

matrix:
allow_failures:
- julia: nightly

after_success:
- if [ $JULIAVERSION = "julianightlies" ]; then julia -e 'cd(Pkg.dir("SpecialMatrices")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'; fi
- julia -e 'VERSION >= v"0.7.0-DEV.5183" && using Pkg; Pkg.add("Coverage"); cd(Pkg.dir("SpecialMatrices")); using Coverage; Coveralls.submit(process_folder()); Codecov.submit(process_folder())'
38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@


# Special Matrices

A [Julia](http://julialang.org) package for working with special matrix types.

Use `Pkg.checkout("SpecialMatrices")` to access latest version.

This package extends the `Base.LinAlg` library with support for special
This package extends the `LinearAlgebra` library with support for special
matrices which are used in linear algebra. Every special matrix has its own type.
The full matrix is accessed by the command `full(A)`.
The full matrix is accessed by the command `Matrix(A)`.

[![Build Status](https://travis-ci.org/jiahao/SpecialMatrices.jl.svg)](https://travis-ci.org/jiahao/SpecialMatrices.jl) [![Coverage Status](https://img.shields.io/coveralls/jiahao/SpecialMatrices.jl.svg)](https://coveralls.io/r/jiahao/SpecialMatrices.jl)
[![Build Status](https://travis-ci.org/JuliaMatrices/SpecialMatrices.jl.svg)](https://travis-ci.org/JuliaMatrices/SpecialMatrices.jl) [![Coverage Status](https://img.shields.io/coveralls/jiahao/SpecialMatrices.jl.svg)](https://coveralls.io/r/jiahao/SpecialMatrices.jl)

## Currently supported special matrices

Expand Down Expand Up @@ -39,7 +35,7 @@ julia> Cauchy(pi)
## `Circulant` matrix

```julia
julia> Circulant([1:4])
julia> Circulant([1,2,3,4])
4x4 Circulant{Int64}:
1 4 3 2
2 1 4 3
Expand All @@ -61,14 +57,14 @@ Also, directly from a polynomial:
```julia
julia> using Polynomials

julia> P=Poly([2,3,4,5])
julia> P=Poly([2.0,3,4,5])
Poly(2 + 3x + 4x^2 + 5x^3)

julia> C=Companion(P)
3x3 Companion{Number}:
0 0 -0.4
1 0 -0.6
0 1 -0.8
3×3 Companion{Float64}:
0.0 0.0 -0.4
1.0 0.0 -0.6
0.0 1.0 -0.8
```

## [`Frobenius`](http://en.wikipedia.org/wiki/Frobenius_matrix) matrix
Expand All @@ -78,7 +74,7 @@ Example
```julia
julia> using SpecialMatrices

julia> F=Frobenius(3, [1.0:3.0]) #Specify subdiagonals of column 3
julia> F=Frobenius(3, [1.0,2.0,3.0]) #Specify subdiagonals of column 3
6x6 Frobenius{Float64}:
1.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 0.0
Expand All @@ -105,7 +101,7 @@ julia> F*F #Special form preserved if the same column has the subdiagonals
0.0 0.0 4.0 0.0 1.0 0.0
0.0 0.0 6.0 0.0 0.0 1.0

julia> F*Frobenius(2, [5.0:-1.0:2.0]) #Promotes to Matrix
julia> F*Frobenius(2, [5.0,4.0,3.0,2.0]) #Promotes to Matrix
6x6 Array{Float64,2}:
1.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 0.0
Expand All @@ -114,7 +110,7 @@ julia> F*Frobenius(2, [5.0:-1.0:2.0]) #Promotes to Matrix
0.0 13.0 2.0 0.0 1.0 0.0
0.0 17.0 3.0 0.0 0.0 1.0

julia> F*[10.0:10.0:60.0]
julia> F*[10.0,20,30,40,50,60.0]
6-element Array{Float64,1}:
10.0
20.0
Expand All @@ -129,7 +125,7 @@ julia> F*[10.0:10.0:60.0]
Input is a vector of odd length.

```julia
julia> Hankel([-4:4])
julia> Hankel(collect(-4:4))
5x5 Hankel{Int64}:
-4 -3 -2 -1 0
-3 -2 -1 0 1
Expand All @@ -144,15 +140,15 @@ julia> Hankel([-4:4])
julia> A=Hilbert(5)
Hilbert{Rational{Int64}}(5,5)

julia> full(A)
julia> Matrix(A)
5x5 Array{Rational{Int64},2}:
1//1 1//2 1//3 1//4 1//5
1//2 1//3 1//4 1//5 1//6
1//3 1//4 1//5 1//6 1//7
1//4 1//5 1//6 1//7 1//8
1//5 1//6 1//7 1//8 1//9

julia> full(Hilbert(5))
julia> Matrix(Hilbert(5))
5x5 Array{Rational{Int64},2}:
1//1 1//2 1//3 1//4 1//5
1//2 1//3 1//4 1//5 1//6
Expand Down Expand Up @@ -248,7 +244,7 @@ julia> Strang(6)
Input is a vector of odd length.

```julia
julia> Toeplitz([-4:4])
julia> Toeplitz(collect(-4:4))
5x5 Toeplitz{Int64}:
0 -1 -2 -3 -4
1 0 -1 -2 -3
Expand All @@ -260,7 +256,7 @@ julia> Toeplitz([-4:4])


```julia
julia> Vandermonde([1:5])
julia> Vandermonde(collect(1:5))
5x5 Vandermonde{Int64}:
1 1 1 1 1
1 2 4 8 16
Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.4
Polynomials
Polynomials 0.4.0
Compat 1.4.0
10 changes: 9 additions & 1 deletion src/SpecialMatrices.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module SpecialMatrices

import Base: A_mul_B!, full, getindex, inv, isassigned, size, *
using Compat
using Compat.LinearAlgebra

import Compat.LinearAlgebra: Matrix, inv
if VERSION >= v"0.7.0"
import Compat.LinearAlgebra: mul!
end

import Base: getindex, isassigned, size, *

include("cauchy.jl") #Cauchy matrix
include("companion.jl") #Companion matrix
Expand Down
6 changes: 3 additions & 3 deletions src/cauchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

export Cauchy

immutable Cauchy{T} <: AbstractMatrix{T}
struct Cauchy{T} <: AbstractMatrix{T}
x::Vector{T} #
y::Vector{T} #
end # immutable

function Cauchy(k::Number)
Cauchy([1:k],[1:k])
Cauchy(collect(1:k),collect(1:k))
end

function Cauchy(x::Vector)
Expand All @@ -26,4 +26,4 @@ function getindex(A::Cauchy,i::Integer,j::Integer)
end # getindex

# Dense version of Cauchy
full(A::Cauchy) =[A[i,j] for i=1:size(A,1), j=1:size(A,2)]
Matrix(A::Cauchy) = [A[i,j] for i=1:size(A,1), j=1:size(A,2)]
24 changes: 12 additions & 12 deletions src/companion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ end
using Polynomials
#Generate companion matrix from a polynomial

function Companion{T}(P::Poly{T})
function Companion(P::Poly{T}) where T
n = length(P)
c = Array{T}(n-1)
c = Array{T}(undef, n-1)
d=P.a[n]
for i=1:n-1
c[i]=P.a[i]/d
Expand All @@ -28,13 +28,13 @@ function size(C::Companion)
end

#XXX Inefficient but works
# getindex(C::Companion, i, j) = getindex(full(C), i, j)
# isassigned(C::Companion, i, j) = isassigned(full(C), i, j)
getindex{T}(C::Companion{T}, i::Int, j::Int) = (j==length(C.c)) ? -C.c[i] : (i==j+1 ? one(T) : zero(T) )
# getindex(C::Companion, i, j) = getindex(Matrix(C), i, j)
# isassigned(C::Companion, i, j) = isassigned(Matrix(C), i, j)
getindex(C::Companion{T}, i::Int, j::Int) where T = (j==length(C.c)) ? -C.c[i] : (i==j+1 ? one(T) : zero(T) )
isassigned(C::Companion, i::Int, j::Int) = (j==length(C.c)) ? isassigned(C.c,i) : true


function full{T}(C::Companion{T})
function Matrix(C::Companion{T}) where T
M = zeros(T, size(C)...)
M[:,end]=-C.c
for i=1:size(C,1)-1
Expand All @@ -44,17 +44,17 @@ function full{T}(C::Companion{T})
end

#Linear algebra stuff
function A_mul_B!{T}(C::Companion{T}, b::Vector{T})
function mul!(C::Companion{T}, b::Vector{T}) where T
x = b[end]
y = -C.c[1]*x
b[2:end] = b[1:end-1]-C.c[2:end]*x
b[1] = y
b
end
*{T}(C::Companion{T}, b::Vector{T}) = A_mul_B!(C, copy(b))
*(C::Companion{T}, b::Vector{T}) where T = mul!(C, copy(b))

function A_mul_B!{T}(A::Matrix{T}, C::Companion{T})
v = Array(T, size(A,1))
function mul!(A::Matrix{T}, C::Companion{T}) where T
v = Array{T}(undef, size(A,1))
for i=1:size(A,1)
v[i] =dot(vec(A[i,:]),-C.c)
end
Expand All @@ -64,9 +64,9 @@ function A_mul_B!{T}(A::Matrix{T}, C::Companion{T})
A[:,end] = v
A
end
*{T}(A::Matrix{T}, C::Companion{T}) = A_mul_B!(copy(A), C)
*(A::Matrix{T}, C::Companion{T}) where T = mul!(copy(A), C)

function inv{T}(C::Companion{T})
function inv(C::Companion{T}) where T
M = zeros(T, size(C)...)
for i=1:size(C,1)-1
M[i, i+1] = one(T)
Expand Down
18 changes: 9 additions & 9 deletions src/frobenius.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export Frobenius
#dense vector, so that the size can be inferred automatically as j+k where j is
#the index of the column and k is the number of subdiagonal elements.

immutable Frobenius{T} <: AbstractArray{T, 2}
struct Frobenius{T} <: AbstractArray{T, 2}
colidx :: Int
c :: Vector{T}
end
Expand All @@ -34,31 +34,31 @@ function size(F::Frobenius)
end

#XXX Inefficient but works
getindex(F::Frobenius, i, j) = getindex(full(F), i, j)
isassigned(F::Frobenius, i, j) = isassigned(full(F), i, j)
getindex(F::Frobenius, i, j) = getindex(Matrix(F), i, j)
isassigned(F::Frobenius, i::Integer, j::Integer) = isassigned(Matrix(F), i, j)

function full{T}(F::Frobenius{T})
M = eye(T, size(F, 1))
function Matrix(F::Frobenius{T}) where T
M = Matrix{T}(I, size(F))
M[F.colidx+1:end, F.colidx] = F.c
M
end

#Linear algebra stuff
function A_mul_B!{T}(F::Frobenius{T}, b::Vector{T})
function mul!(F::Frobenius{T}, b::Vector{T}) where T
(n = size(F, 2)) == length(b) || throw(DimensionMismatch("$n $(length(b))"))
for i=F.colidx+1:n
b[i] += F.c[i-F.colidx] * b[F.colidx]
end
b
end
*{T}(F::Frobenius{T}, b::Vector{T}) = A_mul_B!(F, copy(b))
*(F::Frobenius{T}, b::Vector{T}) where T = mul!(F, copy(b))

function *{T}(F::Frobenius{T}, G::Frobenius{T})
function *(F::Frobenius{T}, G::Frobenius{T}) where T
(n = size(F, 2)) == size(G, 2) || throw(DimensionMismatch(""))
if F.colidx == G.colidx #Answer is still expressible as a Frobenius
return Frobenius(F.colidx, F.c + G.c)
else
M = full(F)
M = Matrix(F)
M[G.colidx+1:end, G.colidx] = F.colidx < G.colidx ? G.c :
Frobenius(F.colidx-G.colidx, F.c) * G.c
return M
Expand Down
10 changes: 5 additions & 5 deletions src/hankel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ size(H::Hankel, r::Int) = (r==1 || r==2) ? 1 + div(length(H.c),2) :
size(H::Hankel) = size(H,1), size(H,2)

# Fast matrix x vector via fft()
function *{T}(A::Hankel{T},x::Vector{T})
function *(A::Hankel{T},x::Vector{T}) where T
Toeplitz(A.c)*reverse(x)
end

function A_mul_B!{T}(y::StridedVector{T},A::Hankel{T},x::StridedVector{T})
function mul!(y::StridedVector{T},A::Hankel{T},x::StridedVector{T}) where T
xx=reverse(x)
return A_mul_B!(y,Toeplitz(A.c),xx)
return mul!(y,Toeplitz(A.c),xx)
end

function full{T}(H::Hankel{T})
function Matrix(H::Hankel{T}) where T
n=size(H, 1)
M=Array{T}(n, n)
M=Array{T}(undef, n, n)
for i=1:n
M[:,i] = H.c[i:i+n-1]
end
Expand Down
7 changes: 3 additions & 4 deletions src/hilbert.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#The m-by-n Hilbert matrix has matrix elements
# H_{ij} = 1/(i+j-1)
import Base: full
export Hilbert

type Hilbert{T}
struct Hilbert{T}
m :: Int
n :: Int
end
Hilbert(T::Type, m::Integer, n::Integer) = Hilbert{T}(m, n)
Hilbert(m::Integer, n::Integer) = Hilbert{Rational{Int}}(m, n)
Hilbert(n::Integer) = Hilbert(n, n)

function full{T}(H::Hilbert{T})
function Matrix(H::Hilbert{T}) where T
Hf = zeros(T, H.m, H.n)
for i=1:H.m, j=1:H.n
Hf[i, j] = one(T)/(i+j-1)
end
Hf
end

function inv{T}(H::Hilbert{T})
function inv(H::Hilbert{T}) where T
invH=zeros(T,H.m,H.m)
for i=1:H.m, j=1:H.m
invH[i,j]=(-1)^(i+j)*(i+j-1)*binomial(H.m+i-1,H.m-j)*
Expand Down
4 changes: 2 additions & 2 deletions src/kahan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export Kahan

immutable Kahan{T<:Number,T1<:Number} <: AbstractMatrix{T}
struct Kahan{T<:Number,T1<:Number} <: AbstractMatrix{T}
m::Int # dimension
n::Int # dimension
theta::T # angle
Expand All @@ -27,7 +27,7 @@ function getindex(A::Kahan,i::Integer,j::Integer)
end # getindex

# Dense version of Kahan
full(A::Kahan) =[A[i,j] for i=1:size(A,1), j=1:size(A,2)]
Matrix(A::Kahan) =[A[i,j] for i=1:size(A,1), j=1:size(A,2)]



Loading