Skip to content
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
1 change: 0 additions & 1 deletion .JuliaFormatter.toml

This file was deleted.

54 changes: 11 additions & 43 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
name: FormatCheck
name: 'Format'

on:
push:
branches:
- 'main'
- 'master'
- 'release-'
tags: '*'
pull_request:
pull_request_target:
paths: ['**/*.jl']
types: [opened, synchronize, reopened, ready_for_review]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
version:
- '1' # automatically expands to the latest stable 1.x release of Julia
os:
- ubuntu-latest
arch:
- x64
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
permissions:
contents: read
actions: write
pull-requests: write

- uses: actions/checkout@v4
- name: Install JuliaFormatter and format
# This will use the latest version by default but you can set the version like so:
#
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
jobs:
formatcheck:
uses: "QuantumKitHub/QuantumKitHubActions/.github/workflows/FormatCheck.yml@main"
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/fredrikekre/runic-pre-commit
rev: v2.0.1
hooks:
- id: runic
2 changes: 1 addition & 1 deletion src/SparseArrayKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include("linearalgebra.jl")
#-----------------
using PackageExtensionCompat
function __init__()
@require_extensions
return @require_extensions
end

end
6 changes: 3 additions & 3 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ function Base.:/(x::SparseArray, a::Number)
return mul!(similar(x, Base.promote_eltypeof(a, x)), x, inv(a))
end
function Base.:+(x::SparseArray, y::SparseArray)
return (T=Base.promote_eltypeof(x, y); axpy!(+one(T), y, copy!(similar(x, T), x)))
return (T = Base.promote_eltypeof(x, y); axpy!(+one(T), y, copy!(similar(x, T), x)))
end
function Base.:-(x::SparseArray, y::SparseArray)
return (T=Base.promote_eltypeof(x, y); axpy!(-one(T), y, copy!(similar(x, T), x)))
return (T = Base.promote_eltypeof(x, y); axpy!(-one(T), y, copy!(similar(x, T), x)))
end

Base.:-(x::SparseArray) = LinearAlgebra.lmul!(-one(eltype(x)), copy(x))

Base.zero(x::SparseArray) = similar(x)
Base.iszero(x::SparseArray) = nonzero_length(x) == 0

function Base.one(x::SparseArray{<:Any,2})
function Base.one(x::SparseArray{<:Any, 2})
m, n = size(x)
m == n ||
throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
Expand Down
31 changes: 16 additions & 15 deletions src/linearalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,34 @@ function LinearAlgebra.axpy!(α::Number, x::SparseArray, y::SparseArray)
return y
end

function LinearAlgebra.norm(x::SparseArray, p::Real=2)
function LinearAlgebra.norm(x::SparseArray, p::Real = 2)
return norm(nonzero_values(x), p)
end
LinearAlgebra.dot(x::SparseArray, y::SparseArray) = inner(x, y)

# matrix functions
const SV{T} = SparseArray{T,1}
const SM{T} = SparseArray{T,2}
const ASM{T} = Union{SparseArray{T,2},
Transpose{T,<:SparseArray{T,2}},
Adjoint{T,<:SparseArray{T,2}}}
const SV{T} = SparseArray{T, 1}
const SM{T} = SparseArray{T, 2}
const ASM{T} = Union{SparseArray{T, 2}, Transpose{T, <:SparseArray{T, 2}}, Adjoint{T, <:SparseArray{T, 2}}}

LinearAlgebra.mul!(C::SM, A::ASM, B::ASM) = mul!(C, A, B, one(eltype(C)), zero(eltype(C)))
function LinearAlgebra.mul!(C::SM, A::ASM, B::ASM, α::Number, β::Number)
conjA = A isa Adjoint
conjB = B isa Adjoint
oindA = A isa Union{Adjoint,Transpose} ? (2,) : (1,)
cindA = A isa Union{Adjoint,Transpose} ? (1,) : (2,)
oindB = B isa Union{Adjoint,Transpose} ? (1,) : (2,)
cindB = B isa Union{Adjoint,Transpose} ? (2,) : (1,)
oindA = A isa Union{Adjoint, Transpose} ? (2,) : (1,)
cindA = A isa Union{Adjoint, Transpose} ? (1,) : (2,)
oindB = B isa Union{Adjoint, Transpose} ? (1,) : (2,)
cindB = B isa Union{Adjoint, Transpose} ? (2,) : (1,)

AA = A isa Union{Adjoint,Transpose} ? parent(A) : A
BB = B isa Union{Adjoint,Transpose} ? parent(B) : B
AA = A isa Union{Adjoint, Transpose} ? parent(A) : A
BB = B isa Union{Adjoint, Transpose} ? parent(B) : B

return tensorcontract!(C, AA, (oindA, cindA), conjA, BB, (cindB, oindB), conjB,
((1, 2), ()), α,
β)
return tensorcontract!(
C,
AA, (oindA, cindA), conjA,
BB, (cindB, oindB), conjB,
((1, 2), ()), α, β
)
end

LinearAlgebra.adjoint!(C::SM, A::SM) = tensoradd!(C, A, ((2, 1), ()), true, One(), Zero())
Expand Down
76 changes: 42 additions & 34 deletions src/sparsearray.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# simple wrapper to give indices a custom wrapping behaviour
struct SparseArray{T,N} <: AbstractArray{T,N}
data::Dict{CartesianIndex{N},T}
dims::NTuple{N,Int64}
function SparseArray{T,N}(::UndefInitializer, dims::Dims{N}) where {T,N}
return new{T,N}(Dict{CartesianIndex{N},T}(), dims)
struct SparseArray{T, N} <: AbstractArray{T, N}
data::Dict{CartesianIndex{N}, T}
dims::NTuple{N, Int64}
function SparseArray{T, N}(::UndefInitializer, dims::Dims{N}) where {T, N}
return new{T, N}(Dict{CartesianIndex{N}, T}(), dims)
end
function SparseArray(a::SparseArray{T,N}) where {T,N}
return new{T,N}(copy(a.data), a.dims)
function SparseArray(a::SparseArray{T, N}) where {T, N}
return new{T, N}(copy(a.data), a.dims)
end
function SparseArray{T,N}(a::Dict{CartesianIndex{N},T},
dims::NTuple{N,Int64}) where {T,N}
return new{T,N}(a, dims)
function SparseArray{T, N}(
a::Dict{CartesianIndex{N}, T}, dims::NTuple{N, Int64}
) where {T, N}
return new{T, N}(a, dims)
end
end
function SparseArray{T}(::UndefInitializer, dims::Dims{N}) where {T,N}
return SparseArray{T,N}(undef, dims)
function SparseArray{T}(::UndefInitializer, dims::Dims{N}) where {T, N}
return SparseArray{T, N}(undef, dims)
end
SparseArray{T}(::UndefInitializer, dims...) where {T} = SparseArray{T}(undef, dims)
function SparseArray{T}(a::UniformScaling, dims::Dims{2}) where {T}
Expand All @@ -36,16 +37,17 @@ _zero!(x::SparseArray) = (empty!(x.data); return x)
_sizehint!(x::SparseArray, n) = sizehint!(x.data, n)

# elementary getindex and setindex!
@inline function Base.getindex(a::SparseArray{T,N}, I::CartesianIndex{N}) where {T,N}
@inline function Base.getindex(a::SparseArray{T, N}, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
return get(a.data, I, zero(T))
end
Base.@propagate_inbounds function Base.getindex(a::SparseArray{T,N},
I::Vararg{Int,N}) where {T,N}
Base.@propagate_inbounds function Base.getindex(
a::SparseArray{T, N}, I::Vararg{Int, N}
) where {T, N}
return getindex(a, CartesianIndex(I))
end

@inline function Base.setindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N}
@inline function Base.setindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
if !iszero(v)
a.data[I] = v
Expand All @@ -54,12 +56,14 @@ end
end
return v
end
Base.@propagate_inbounds function Base.setindex!(a::SparseArray{T,N},
v, I::Vararg{Int,N}) where {T,N}
Base.@propagate_inbounds function Base.setindex!(
a::SparseArray{T, N},
v, I::Vararg{Int, N}
) where {T, N}
return setindex!(a, v, CartesianIndex(I))
end

@inline function increaseindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N}
@inline function increaseindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
@boundscheck checkbounds(a, I)
iszero(v) && return
h = a.data
Expand Down Expand Up @@ -100,8 +104,9 @@ end
_findfirstvalue(v, r) = findfirst(==(v), r)

# slicing should produce SparseArray
function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N},
I::Vararg{Union{Int,AbstractVector{Int}},N}) where {T,N}
function Base._unsafe_getindex(
::IndexCartesian, a::SparseArray{T, N}, I::Vararg{Union{Int, AbstractVector{Int}}, N}
) where {T, N}
@boundscheck checkbounds(a, I...)
indices = Base.to_indices(a, I)
b = SparseArray{T}(undef, length.(Base.index_shape(indices...)))
Expand All @@ -114,36 +119,36 @@ function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N},
return b
end

Base.Array(a::SparseArray{T,N}) where {T,N} = Array{T,N}(a)
function Base.Array{T,N}(a::SparseArray) where {T,N}
Base.Array(a::SparseArray{T, N}) where {T, N} = Array{T, N}(a)
function Base.Array{T, N}(a::SparseArray) where {T, N}
d = fill(zero(T), size(a))
for (I, v) in a.data
d[I] = v
end
return d
end

SparseArray(a::AbstractArray{T,N}) where {T,N} = SparseArray{T,N}(a)
SparseArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = SparseArray{T,N}(a)
function SparseArray{T,N}(a::AbstractArray{<:Any,N}) where {T,N}
d = SparseArray{T,N}(undef, size(a))
SparseArray(a::AbstractArray{T, N}) where {T, N} = SparseArray{T, N}(a)
SparseArray{T}(a::AbstractArray{<:Any, N}) where {T, N} = SparseArray{T, N}(a)
function SparseArray{T, N}(a::AbstractArray{<:Any, N}) where {T, N}
d = SparseArray{T, N}(undef, size(a))
for I in CartesianIndices(a)
iszero(a[I]) && continue
d[I] = a[I]
end
return d
end
Base.convert(::Type{S}, a::S) where {S<:SparseArray} = a
Base.convert(::Type{S}, a::S) where {S <: SparseArray} = a
Base.convert(S::Type{<:SparseArray}, a::AbstractArray) = S(a)

function SparseArray(A::Adjoint{T,<:SparseArray{T,2}}) where {T}
function SparseArray(A::Adjoint{T, <:SparseArray{T, 2}}) where {T}
B = SparseArray{T}(undef, size(A))
for (I, v) in parent(A).data
B[I[2], I[1]] = conj(v)
end
return B
end
function SparseArray(A::Transpose{T,<:SparseArray{T,2}}) where {T}
function SparseArray(A::Transpose{T, <:SparseArray{T, 2}}) where {T}
B = SparseArray{T}(undef, size(A))
for (I, v) in parent(A).data
B[I[2], I[1]] = v
Expand All @@ -166,16 +171,18 @@ function Base.copy!(dst::SparseArray, src::SparseArray)
return dst
end

function Base.similar(::SparseArray, ::Type{S}, dims::Dims{N}) where {S,N}
function Base.similar(::SparseArray, ::Type{S}, dims::Dims{N}) where {S, N}
return SparseArray{S}(undef, dims)
end

# show and friends
function Base.show(io::IO, ::MIME"text/plain", x::SparseArray)
xnnz = nonzero_length(x)
print(io, join(size(x), "×"), " ", typeof(x), " with ", xnnz, " stored ",
xnnz == 1 ? "entry" : "entries")
if xnnz != 0
print(
io, join(size(x), "×"), " ", typeof(x), " with ", xnnz, " stored ",
xnnz == 1 ? "entry" : "entries"
)
return if xnnz != 0
println(io, ":")
show(IOContext(io, :typeinfo => eltype(x)), x)
end
Expand All @@ -202,4 +209,5 @@ function Base.show(io::IOContext, x::SparseArray)
println(io, " ", join(" " .^ pads, " "), " \u22ee")
end
end
return
end
Loading
Loading