Skip to content

Commit c6e4fc3

Browse files
authored
Runic formatter (#22)
* runic formatter * update workflows
1 parent 50958d8 commit c6e4fc3

File tree

13 files changed

+168
-153
lines changed

13 files changed

+168
-153
lines changed

.JuliaFormatter.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/FormatCheck.yml

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
1-
name: FormatCheck
1+
name: 'Format'
22

33
on:
4-
push:
5-
branches:
6-
- 'main'
7-
- 'master'
8-
- 'release-'
9-
tags: '*'
10-
pull_request:
4+
pull_request_target:
5+
paths: ['**/*.jl']
6+
types: [opened, synchronize, reopened, ready_for_review]
117

12-
jobs:
13-
build:
14-
runs-on: ${{ matrix.os }}
15-
strategy:
16-
matrix:
17-
version:
18-
- '1' # automatically expands to the latest stable 1.x release of Julia
19-
os:
20-
- ubuntu-latest
21-
arch:
22-
- x64
23-
steps:
24-
- uses: julia-actions/setup-julia@latest
25-
with:
26-
version: ${{ matrix.version }}
27-
arch: ${{ matrix.arch }}
8+
permissions:
9+
contents: read
10+
actions: write
11+
pull-requests: write
2812

29-
- uses: actions/checkout@v4
30-
- name: Install JuliaFormatter and format
31-
# This will use the latest version by default but you can set the version like so:
32-
#
33-
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
34-
run: |
35-
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
36-
julia -e 'using JuliaFormatter; format(".", verbose=true)'
37-
- name: Format check
38-
run: |
39-
julia -e '
40-
out = Cmd(`git diff --name-only`) |> read |> String
41-
if out == ""
42-
exit(0)
43-
else
44-
@error "Some files have not been formatted !!!"
45-
write(stdout, out)
46-
exit(1)
47-
end'
13+
jobs:
14+
formatcheck:
15+
uses: "QuantumKitHub/QuantumKitHubActions/.github/workflows/FormatCheck.yml@main"

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
repos:
2+
- repo: https://github.com/fredrikekre/runic-pre-commit
3+
rev: v2.0.1
4+
hooks:
5+
- id: runic

src/SparseArrayKit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include("linearalgebra.jl")
1818
#-----------------
1919
using PackageExtensionCompat
2020
function __init__()
21-
@require_extensions
21+
return @require_extensions
2222
end
2323

2424
end

src/base.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ function Base.:/(x::SparseArray, a::Number)
1010
return mul!(similar(x, Base.promote_eltypeof(a, x)), x, inv(a))
1111
end
1212
function Base.:+(x::SparseArray, y::SparseArray)
13-
return (T=Base.promote_eltypeof(x, y); axpy!(+one(T), y, copy!(similar(x, T), x)))
13+
return (T = Base.promote_eltypeof(x, y); axpy!(+one(T), y, copy!(similar(x, T), x)))
1414
end
1515
function Base.:-(x::SparseArray, y::SparseArray)
16-
return (T=Base.promote_eltypeof(x, y); axpy!(-one(T), y, copy!(similar(x, T), x)))
16+
return (T = Base.promote_eltypeof(x, y); axpy!(-one(T), y, copy!(similar(x, T), x)))
1717
end
1818

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

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

24-
function Base.one(x::SparseArray{<:Any,2})
24+
function Base.one(x::SparseArray{<:Any, 2})
2525
m, n = size(x)
2626
m == n ||
2727
throw(DimensionMismatch("multiplicative identity defined only for square matrices"))

src/linearalgebra.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,34 @@ function LinearAlgebra.axpy!(α::Number, x::SparseArray, y::SparseArray)
6363
return y
6464
end
6565

66-
function LinearAlgebra.norm(x::SparseArray, p::Real=2)
66+
function LinearAlgebra.norm(x::SparseArray, p::Real = 2)
6767
return norm(nonzero_values(x), p)
6868
end
6969
LinearAlgebra.dot(x::SparseArray, y::SparseArray) = inner(x, y)
7070

7171
# matrix functions
72-
const SV{T} = SparseArray{T,1}
73-
const SM{T} = SparseArray{T,2}
74-
const ASM{T} = Union{SparseArray{T,2},
75-
Transpose{T,<:SparseArray{T,2}},
76-
Adjoint{T,<:SparseArray{T,2}}}
72+
const SV{T} = SparseArray{T, 1}
73+
const SM{T} = SparseArray{T, 2}
74+
const ASM{T} = Union{SparseArray{T, 2}, Transpose{T, <:SparseArray{T, 2}}, Adjoint{T, <:SparseArray{T, 2}}}
7775

7876
LinearAlgebra.mul!(C::SM, A::ASM, B::ASM) = mul!(C, A, B, one(eltype(C)), zero(eltype(C)))
7977
function LinearAlgebra.mul!(C::SM, A::ASM, B::ASM, α::Number, β::Number)
8078
conjA = A isa Adjoint
8179
conjB = B isa Adjoint
82-
oindA = A isa Union{Adjoint,Transpose} ? (2,) : (1,)
83-
cindA = A isa Union{Adjoint,Transpose} ? (1,) : (2,)
84-
oindB = B isa Union{Adjoint,Transpose} ? (1,) : (2,)
85-
cindB = B isa Union{Adjoint,Transpose} ? (2,) : (1,)
80+
oindA = A isa Union{Adjoint, Transpose} ? (2,) : (1,)
81+
cindA = A isa Union{Adjoint, Transpose} ? (1,) : (2,)
82+
oindB = B isa Union{Adjoint, Transpose} ? (1,) : (2,)
83+
cindB = B isa Union{Adjoint, Transpose} ? (2,) : (1,)
8684

87-
AA = A isa Union{Adjoint,Transpose} ? parent(A) : A
88-
BB = B isa Union{Adjoint,Transpose} ? parent(B) : B
85+
AA = A isa Union{Adjoint, Transpose} ? parent(A) : A
86+
BB = B isa Union{Adjoint, Transpose} ? parent(B) : B
8987

90-
return tensorcontract!(C, AA, (oindA, cindA), conjA, BB, (cindB, oindB), conjB,
91-
((1, 2), ()), α,
92-
β)
88+
return tensorcontract!(
89+
C,
90+
AA, (oindA, cindA), conjA,
91+
BB, (cindB, oindB), conjB,
92+
((1, 2), ()), α, β
93+
)
9394
end
9495

9596
LinearAlgebra.adjoint!(C::SM, A::SM) = tensoradd!(C, A, ((2, 1), ()), true, One(), Zero())

src/sparsearray.jl

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# simple wrapper to give indices a custom wrapping behaviour
2-
struct SparseArray{T,N} <: AbstractArray{T,N}
3-
data::Dict{CartesianIndex{N},T}
4-
dims::NTuple{N,Int64}
5-
function SparseArray{T,N}(::UndefInitializer, dims::Dims{N}) where {T,N}
6-
return new{T,N}(Dict{CartesianIndex{N},T}(), dims)
2+
struct SparseArray{T, N} <: AbstractArray{T, N}
3+
data::Dict{CartesianIndex{N}, T}
4+
dims::NTuple{N, Int64}
5+
function SparseArray{T, N}(::UndefInitializer, dims::Dims{N}) where {T, N}
6+
return new{T, N}(Dict{CartesianIndex{N}, T}(), dims)
77
end
8-
function SparseArray(a::SparseArray{T,N}) where {T,N}
9-
return new{T,N}(copy(a.data), a.dims)
8+
function SparseArray(a::SparseArray{T, N}) where {T, N}
9+
return new{T, N}(copy(a.data), a.dims)
1010
end
11-
function SparseArray{T,N}(a::Dict{CartesianIndex{N},T},
12-
dims::NTuple{N,Int64}) where {T,N}
13-
return new{T,N}(a, dims)
11+
function SparseArray{T, N}(
12+
a::Dict{CartesianIndex{N}, T}, dims::NTuple{N, Int64}
13+
) where {T, N}
14+
return new{T, N}(a, dims)
1415
end
1516
end
16-
function SparseArray{T}(::UndefInitializer, dims::Dims{N}) where {T,N}
17-
return SparseArray{T,N}(undef, dims)
17+
function SparseArray{T}(::UndefInitializer, dims::Dims{N}) where {T, N}
18+
return SparseArray{T, N}(undef, dims)
1819
end
1920
SparseArray{T}(::UndefInitializer, dims...) where {T} = SparseArray{T}(undef, dims)
2021
function SparseArray{T}(a::UniformScaling, dims::Dims{2}) where {T}
@@ -36,16 +37,17 @@ _zero!(x::SparseArray) = (empty!(x.data); return x)
3637
_sizehint!(x::SparseArray, n) = sizehint!(x.data, n)
3738

3839
# elementary getindex and setindex!
39-
@inline function Base.getindex(a::SparseArray{T,N}, I::CartesianIndex{N}) where {T,N}
40+
@inline function Base.getindex(a::SparseArray{T, N}, I::CartesianIndex{N}) where {T, N}
4041
@boundscheck checkbounds(a, I)
4142
return get(a.data, I, zero(T))
4243
end
43-
Base.@propagate_inbounds function Base.getindex(a::SparseArray{T,N},
44-
I::Vararg{Int,N}) where {T,N}
44+
Base.@propagate_inbounds function Base.getindex(
45+
a::SparseArray{T, N}, I::Vararg{Int, N}
46+
) where {T, N}
4547
return getindex(a, CartesianIndex(I))
4648
end
4749

48-
@inline function Base.setindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N}
50+
@inline function Base.setindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
4951
@boundscheck checkbounds(a, I)
5052
if !iszero(v)
5153
a.data[I] = v
@@ -54,12 +56,14 @@ end
5456
end
5557
return v
5658
end
57-
Base.@propagate_inbounds function Base.setindex!(a::SparseArray{T,N},
58-
v, I::Vararg{Int,N}) where {T,N}
59+
Base.@propagate_inbounds function Base.setindex!(
60+
a::SparseArray{T, N},
61+
v, I::Vararg{Int, N}
62+
) where {T, N}
5963
return setindex!(a, v, CartesianIndex(I))
6064
end
6165

62-
@inline function increaseindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N}
66+
@inline function increaseindex!(a::SparseArray{T, N}, v, I::CartesianIndex{N}) where {T, N}
6367
@boundscheck checkbounds(a, I)
6468
iszero(v) && return
6569
h = a.data
@@ -100,8 +104,9 @@ end
100104
_findfirstvalue(v, r) = findfirst(==(v), r)
101105

102106
# slicing should produce SparseArray
103-
function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N},
104-
I::Vararg{Union{Int,AbstractVector{Int}},N}) where {T,N}
107+
function Base._unsafe_getindex(
108+
::IndexCartesian, a::SparseArray{T, N}, I::Vararg{Union{Int, AbstractVector{Int}}, N}
109+
) where {T, N}
105110
@boundscheck checkbounds(a, I...)
106111
indices = Base.to_indices(a, I)
107112
b = SparseArray{T}(undef, length.(Base.index_shape(indices...)))
@@ -114,36 +119,36 @@ function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N},
114119
return b
115120
end
116121

117-
Base.Array(a::SparseArray{T,N}) where {T,N} = Array{T,N}(a)
118-
function Base.Array{T,N}(a::SparseArray) where {T,N}
122+
Base.Array(a::SparseArray{T, N}) where {T, N} = Array{T, N}(a)
123+
function Base.Array{T, N}(a::SparseArray) where {T, N}
119124
d = fill(zero(T), size(a))
120125
for (I, v) in a.data
121126
d[I] = v
122127
end
123128
return d
124129
end
125130

126-
SparseArray(a::AbstractArray{T,N}) where {T,N} = SparseArray{T,N}(a)
127-
SparseArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = SparseArray{T,N}(a)
128-
function SparseArray{T,N}(a::AbstractArray{<:Any,N}) where {T,N}
129-
d = SparseArray{T,N}(undef, size(a))
131+
SparseArray(a::AbstractArray{T, N}) where {T, N} = SparseArray{T, N}(a)
132+
SparseArray{T}(a::AbstractArray{<:Any, N}) where {T, N} = SparseArray{T, N}(a)
133+
function SparseArray{T, N}(a::AbstractArray{<:Any, N}) where {T, N}
134+
d = SparseArray{T, N}(undef, size(a))
130135
for I in CartesianIndices(a)
131136
iszero(a[I]) && continue
132137
d[I] = a[I]
133138
end
134139
return d
135140
end
136-
Base.convert(::Type{S}, a::S) where {S<:SparseArray} = a
141+
Base.convert(::Type{S}, a::S) where {S <: SparseArray} = a
137142
Base.convert(S::Type{<:SparseArray}, a::AbstractArray) = S(a)
138143

139-
function SparseArray(A::Adjoint{T,<:SparseArray{T,2}}) where {T}
144+
function SparseArray(A::Adjoint{T, <:SparseArray{T, 2}}) where {T}
140145
B = SparseArray{T}(undef, size(A))
141146
for (I, v) in parent(A).data
142147
B[I[2], I[1]] = conj(v)
143148
end
144149
return B
145150
end
146-
function SparseArray(A::Transpose{T,<:SparseArray{T,2}}) where {T}
151+
function SparseArray(A::Transpose{T, <:SparseArray{T, 2}}) where {T}
147152
B = SparseArray{T}(undef, size(A))
148153
for (I, v) in parent(A).data
149154
B[I[2], I[1]] = v
@@ -166,16 +171,18 @@ function Base.copy!(dst::SparseArray, src::SparseArray)
166171
return dst
167172
end
168173

169-
function Base.similar(::SparseArray, ::Type{S}, dims::Dims{N}) where {S,N}
174+
function Base.similar(::SparseArray, ::Type{S}, dims::Dims{N}) where {S, N}
170175
return SparseArray{S}(undef, dims)
171176
end
172177

173178
# show and friends
174179
function Base.show(io::IO, ::MIME"text/plain", x::SparseArray)
175180
xnnz = nonzero_length(x)
176-
print(io, join(size(x), "×"), " ", typeof(x), " with ", xnnz, " stored ",
177-
xnnz == 1 ? "entry" : "entries")
178-
if xnnz != 0
181+
print(
182+
io, join(size(x), "×"), " ", typeof(x), " with ", xnnz, " stored ",
183+
xnnz == 1 ? "entry" : "entries"
184+
)
185+
return if xnnz != 0
179186
println(io, ":")
180187
show(IOContext(io, :typeinfo => eltype(x)), x)
181188
end
@@ -202,4 +209,5 @@ function Base.show(io::IOContext, x::SparseArray)
202209
println(io, " ", join(" " .^ pads, " "), " \u22ee")
203210
end
204211
end
212+
return
205213
end

0 commit comments

Comments
 (0)