Skip to content

Commit 62c92a0

Browse files
authored
Remove ndims type parameter from AbstractArrayStyle (#5)
1 parent 41e9512 commit 62c92a0

8 files changed

Lines changed: 74 additions & 107 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "IntegrationTest"
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags: '*'
8+
paths:
9+
- 'Project.toml'
10+
pull_request:
11+
paths:
12+
- 'Project.toml'
13+
14+
jobs:
15+
integration-test:
16+
name: "IntegrationTest"
17+
strategy:
18+
matrix:
19+
pkg:
20+
- 'BlockSparseArrays'
21+
- 'DiagonalArrays'
22+
- 'ITensorBase'
23+
- 'ITensorNetworksNext'
24+
- 'KroneckerArrays'
25+
- 'NamedDimsArrays'
26+
- 'SparseArraysBase'
27+
uses: "ITensor/ITensorActions/.github/workflows/IntegrationTest.yml@main"
28+
with:
29+
localregistry: "https://github.com/ITensor/ITensorRegistry.git"
30+
pkg: "${{ matrix.pkg }}"

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FunctionImplementations"
22
uuid = "7c7cc465-9c6a-495f-bdd1-f42428e86d0c"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.2.1"
4+
version = "0.3.0"
55

66
[weakdeps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ FunctionImplementations = {path = ".."}
99
[compat]
1010
Documenter = "1"
1111
Literate = "2"
12-
FunctionImplementations = "0.2"
12+
FunctionImplementations = "0.3"

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ FunctionImplementations = "7c7cc465-9c6a-495f-bdd1-f42428e86d0c"
55
FunctionImplementations = {path = ".."}
66

77
[compat]
8-
FunctionImplementations = "0.2"
8+
FunctionImplementations = "0.3"

ext/FunctionImplementationsLinearAlgebraExt/FunctionImplementationsLinearAlgebraExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module FunctionImplementationsLinearAlgebraExt
33
import FunctionImplementations as FI
44
import LinearAlgebra as LA
55

6-
struct DiagonalStyle <: FI.AbstractMatrixStyle end
6+
struct DiagonalStyle <: FI.AbstractArrayStyle end
77
FI.Style(::Type{<:LA.Diagonal}) = DiagonalStyle()
88
const permuteddims_diag = DiagonalStyle()(FI.permuteddims)
99
function permuteddims_diag(a::AbstractArray, perm)

src/style.jl

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,32 @@ Style `s`.
2828
(s::Style)(f) = Implementation(f, s)
2929

3030
"""
31-
`FunctionImplementations.AbstractArrayStyle{N} <: Style` is the abstract supertype for any style
31+
`FunctionImplementations.AbstractArrayStyle <: Style` is the abstract supertype for any style
3232
associated with an `AbstractArray` type.
33-
The `N` parameter is the dimensionality, which can be handy for AbstractArray types
34-
that only support specific dimensionalities:
35-
36-
struct SparseMatrixStyle <: FunctionImplementations.AbstractArrayStyle{2} end
37-
FunctionImplementations.Style(::Type{<:SparseMatrixCSC}) = SparseMatrixStyle()
38-
39-
For `AbstractArray` types that support arbitrary dimensionality, `N` can be set to `Any`:
40-
41-
struct MyArrayStyle <: FunctionImplementations.AbstractArrayStyle{Any} end
42-
FunctionImplementations.Style(::Type{<:MyArray}) = MyArrayStyle()
43-
44-
In cases where you want to be able to mix multiple `AbstractArrayStyle`s and keep track
45-
of dimensionality, your style needs to support a `Val` constructor:
46-
47-
struct MyArrayStyleDim{N} <: FunctionImplementations.AbstractArrayStyle{N} end
48-
(::Type{<:MyArrayStyleDim})(::Val{N}) where N = MyArrayStyleDim{N}()
4933
5034
Note that if two or more `AbstractArrayStyle` subtypes conflict, the resulting
5135
style will fall back to that of `Array`s. If this is undesirable, you may need to
5236
define binary [`Style`](@ref) rules to control the output type.
5337
5438
See also [`FunctionImplementations.DefaultArrayStyle`](@ref).
5539
"""
56-
abstract type AbstractArrayStyle{N} <: Style end
57-
abstract type AbstractVectorStyle <: AbstractArrayStyle{1} end
58-
abstract type AbstractMatrixStyle <: AbstractArrayStyle{2} end
40+
abstract type AbstractArrayStyle <: Style end
5941

6042
"""
61-
`FunctionImplementations.DefaultArrayStyle{N}()` is a [`FunctionImplementations.Style`](@ref) indicating that an object
62-
behaves as an `N`-dimensional array. Specifically, `DefaultArrayStyle` is
63-
used for any
64-
`AbstractArray` type that hasn't defined a specialized style, and in the absence of
65-
overrides from other arguments the resulting output type is `Array`.
43+
`FunctionImplementations.DefaultArrayStyle()` is a [`FunctionImplementations.Style`](@ref)
44+
indicating that an object behaves as an array. Specifically, `DefaultArrayStyle` is
45+
used for any `AbstractArray` type that hasn't defined a specialized style, and in the
46+
absence of overrides from other arguments the resulting output type is `Array`.
6647
"""
67-
struct DefaultArrayStyle{N} <: AbstractArrayStyle{N} end
68-
DefaultArrayStyle() = DefaultArrayStyle{Any}()
69-
DefaultArrayStyle(::Val{N}) where {N} = DefaultArrayStyle{N}()
70-
DefaultArrayStyle{M}(::Val{N}) where {N, M} = DefaultArrayStyle{N}()
71-
const DefaultVectorStyle = DefaultArrayStyle{1}
72-
const DefaultMatrixStyle = DefaultArrayStyle{2}
73-
Style(::Type{<:AbstractArray{T, N}}) where {T, N} = DefaultArrayStyle{N}()
48+
struct DefaultArrayStyle <: AbstractArrayStyle end
49+
Style(::Type{<:AbstractArray}) = DefaultArrayStyle()
7450

7551
# `ArrayConflict` is an internal type signaling that two or more different `AbstractArrayStyle`
7652
# objects were supplied as arguments, and that no rule was defined for resolving the
7753
# conflict. The resulting output is `Array`. While this is the same output type
7854
# produced by `DefaultArrayStyle`, `ArrayConflict` "poisons" the Style so that
7955
# 3 or more arguments still return an `ArrayConflict`.
80-
struct ArrayConflict <: AbstractArrayStyle{Any} end
81-
ArrayConflict(::Val) = ArrayConflict()
56+
struct ArrayConflict <: AbstractArrayStyle end
8257

8358
### Binary Style rules
8459
"""
@@ -100,17 +75,14 @@ Style(::UnknownStyle, ::UnknownStyle) = UnknownStyle()
10075
Style(::S, ::UnknownStyle) where {S <: Style} = S()
10176
# Precedence rules
10277
Style(::A, ::A) where {A <: AbstractArrayStyle} = A()
103-
function Style(a::A, b::B) where {A <: AbstractArrayStyle{M}, B <: AbstractArrayStyle{N}} where {M, N}
104-
if Base.typename(A) === Base.typename(B)
105-
return A(Val(Any))
78+
function Style(a::A, b::B) where {A <: AbstractArrayStyle, B <: AbstractArrayStyle}
79+
if Base.typename(A) Base.typename(B)
80+
return A()
10681
end
10782
return UnknownStyle()
10883
end
10984
# Any specific array type beats DefaultArrayStyle
110-
Style(a::AbstractArrayStyle{Any}, ::DefaultArrayStyle) = a
111-
Style(a::AbstractArrayStyle{N}, ::DefaultArrayStyle{N}) where {N} = a
112-
Style(a::AbstractArrayStyle{M}, ::DefaultArrayStyle{N}) where {M, N} =
113-
typeof(a)(Val(Any))
85+
Style(a::AbstractArrayStyle, ::DefaultArrayStyle) = a
11486

11587
## logic for deciding the Style
11688

@@ -124,12 +96,12 @@ Uses [`Style`](@ref) to get the style for each argument, and uses
12496
# Examples
12597
```jldoctest
12698
julia> FunctionImplementations.style([1], [1 2; 3 4])
127-
FunctionImplementations.DefaultArrayStyle{Any}()
99+
FunctionImplementations.DefaultArrayStyle()
128100
```
129101
"""
130102
function style end
131103

132-
style() = DefaultArrayStyle{0}()
104+
style() = DefaultArrayStyle()
133105
style(c) = result_style(Style(typeof(c)))
134106
style(c1, c2) = result_style(style(c1), style(c2))
135107
@inline style(c1, c2, cs...) = result_style(style(c1), style(c2, cs...))
@@ -143,11 +115,11 @@ determine a common `Style`.
143115
# Examples
144116
145117
```jldoctest
146-
julia> FunctionImplementations.result_style(FunctionImplementations.DefaultArrayStyle{0}(), FunctionImplementations.DefaultArrayStyle{3}())
147-
FunctionImplementations.DefaultArrayStyle{Any}()
118+
julia> FunctionImplementations.result_style(FunctionImplementations.DefaultArrayStyle(), FunctionImplementations.DefaultArrayStyle())
119+
FunctionImplementations.DefaultArrayStyle()
148120
149-
julia> FunctionImplementations.result_style(FunctionImplementations.UnknownStyle(), FunctionImplementations.DefaultArrayStyle{1}())
150-
FunctionImplementations.DefaultArrayStyle{1}()
121+
julia> FunctionImplementations.result_style(FunctionImplementations.UnknownStyle(), FunctionImplementations.DefaultArrayStyle())
122+
FunctionImplementations.DefaultArrayStyle()
151123
```
152124
"""
153125
function result_style end

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FunctionImplementations = {path = ".."}
1111

1212
[compat]
1313
Aqua = "0.8"
14-
FunctionImplementations = "0.2"
14+
FunctionImplementations = "0.3"
1515
LinearAlgebra = "1.10"
1616
SafeTestsets = "0.1"
1717
Suppressor = "0.2"

test/test_basics.jl

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ using Test: @test, @testset
1616
# Test the shorthand for creating an Implementation by calling a Style with a
1717
# function.
1818
@test FI.style([1, 2, 3])(getindex)
19-
FI.Implementation(getindex, FI.DefaultArrayStyle{1}())
19+
FI.Implementation(getindex, FI.DefaultArrayStyle())
2020
end
2121
@testset "Style" begin
2222
# Test basic Style trait for different array types
23-
@test FI.Style(typeof([1, 2, 3])) isa FI.DefaultArrayStyle{1}
24-
@test FI.style([1, 2, 3]) isa FI.DefaultArrayStyle{1}
25-
@test FI.Style(typeof([1 2; 3 4])) isa FI.DefaultArrayStyle{2}
26-
@test FI.Style(typeof(rand(2, 3, 4))) isa FI.DefaultArrayStyle{3}
23+
@test FI.Style(typeof([1, 2, 3])) FI.DefaultArrayStyle()
24+
@test FI.style([1, 2, 3]) FI.DefaultArrayStyle()
25+
@test FI.Style(typeof([1 2; 3 4])) FI.DefaultArrayStyle()
26+
@test FI.Style(typeof(rand(2, 3, 4))) FI.DefaultArrayStyle()
2727

2828
# Test custom Style definition
2929
struct CustomStyle <: FI.Style end
@@ -35,91 +35,56 @@ using Test: @test, @testset
3535
struct MyArray{T, N} <: AbstractArray{T, N}
3636
data::Array{T, N}
3737
end
38-
struct MyArrayStyle <: FI.AbstractArrayStyle{Any} end
38+
struct MyArrayStyle <: FI.AbstractArrayStyle end
3939
FI.Style(::Type{<:MyArray}) = MyArrayStyle()
4040
@test FI.Style(MyArray) isa MyArrayStyle
4141

4242
# Test style homogeneity rule (same type returns preserved)
43-
s1 = FI.DefaultArrayStyle{1}()
44-
s2 = FI.DefaultArrayStyle{1}()
45-
@test FI.Style(s1, s2) s1
43+
s1 = FI.DefaultArrayStyle()
44+
s2 = FI.DefaultArrayStyle()
45+
@test FI.Style(s1, s2) FI.DefaultArrayStyle()
4646

4747
# Test UnknownStyle precedence
4848
unknown = FI.UnknownStyle()
49-
known = FI.DefaultArrayStyle{1}()
49+
known = FI.DefaultArrayStyle()
5050
@test FI.Style(known, unknown) known
5151
@test FI.Style(unknown, unknown) unknown
5252

53-
# Test AbstractArrayStyle with different dimensions uses max
54-
@test FI.Style(
55-
FI.DefaultArrayStyle{1}(),
56-
FI.DefaultArrayStyle{2}()
57-
) isa FI.DefaultArrayStyle{Any}
58-
59-
# Test DefaultArrayStyle Val constructor preserves type when dimension matches
60-
default_style = FI.DefaultArrayStyle{1}(Val(1))
61-
@test FI.DefaultArrayStyle{1}(Val(1)) isa FI.DefaultArrayStyle{1}
62-
63-
# Test DefaultArrayStyle Val constructor changes dimension
64-
@test FI.DefaultArrayStyle{1}(Val(2)) isa FI.DefaultArrayStyle{2}
65-
66-
# Test DefaultArrayStyle constructor defaults to Any dimension
67-
@test FI.DefaultArrayStyle() isa FI.DefaultArrayStyle{Any}
68-
69-
# Test const aliases
70-
@test FI.DefaultVectorStyle FI.DefaultArrayStyle{1}
71-
@test FI.DefaultMatrixStyle FI.DefaultArrayStyle{2}
72-
7353
# Test ArrayConflict
7454
conflict = FI.ArrayConflict()
7555
@test conflict isa FI.ArrayConflict
76-
@test conflict isa FI.AbstractArrayStyle{Any}
77-
78-
# Test ArrayConflict Val constructor
79-
conflict_val = FI.ArrayConflict(Val(3))
80-
@test conflict_val isa FI.ArrayConflict
56+
@test conflict isa FI.AbstractArrayStyle
8157

8258
# Test style with no arguments
83-
@test FI.style() isa FI.DefaultArrayStyle{0}
59+
@test FI.style() FI.DefaultArrayStyle()
8460

8561
# Test style with single argument
86-
@test FI.style([1, 2]) isa FI.DefaultArrayStyle{1}
87-
@test FI.style([1 2; 3 4]) isa FI.DefaultArrayStyle{2}
62+
@test FI.style([1, 2]) FI.DefaultArrayStyle()
63+
@test FI.style([1 2; 3 4]) FI.DefaultArrayStyle()
8864

8965
# Test style with two arguments
9066
result = FI.style([1, 2], [1 2; 3 4])
91-
@test result isa FI.DefaultArrayStyle{Any}
67+
@test result FI.DefaultArrayStyle()
9268

9369
# Test style with same dimensions
9470
result = FI.style([1], [2])
95-
@test result isa FI.DefaultArrayStyle{1}
71+
@test result FI.DefaultArrayStyle()
9672

9773
# Test style with multiple arguments
9874
result = FI.style([1], [1 2], rand(2, 3, 4))
99-
@test result isa FI.DefaultArrayStyle{Any}
75+
@test result FI.DefaultArrayStyle()
10076

10177
# Test result_style with single argument
102-
@test FI.result_style(FI.DefaultArrayStyle{1}()) isa FI.DefaultArrayStyle{1}
78+
@test FI.result_style(FI.DefaultArrayStyle()) isa FI.DefaultArrayStyle
10379

10480
# Test result_style with two identical styles
105-
s = FI.DefaultArrayStyle{2}()
81+
s = FI.DefaultArrayStyle()
10682
@test FI.result_style(s, s) s
10783

10884
# Test result_style with UnknownStyle
109-
known = FI.DefaultArrayStyle{1}()
85+
known = FI.DefaultArrayStyle()
11086
unknown = FI.UnknownStyle()
11187
@test FI.result_style(known, unknown) known
11288
@test FI.result_style(unknown, known) known
113-
114-
# Test result_style with different dimension DefaultArrayStyle uses max
115-
result = FI.result_style(
116-
FI.DefaultArrayStyle{1}(),
117-
FI.DefaultArrayStyle{2}()
118-
)
119-
@test result isa FI.DefaultArrayStyle{Any}
120-
121-
# Test result_style with same shape behaves consistently
122-
same_style = FI.DefaultArrayStyle{2}()
123-
@test FI.result_style(same_style, same_style) same_style
12489
end
12590
end

0 commit comments

Comments
 (0)