Skip to content

Commit 4d3462b

Browse files
authored
add vec (#120)
* add vec * vec and reshape * Update quasiadjtrans.jl * increase coverage * Update runtests.jl * increase coverage * Update test_quasilazy.jl
1 parent 97a133c commit 4d3462b

File tree

11 files changed

+81
-11
lines changed

11 files changed

+81
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuasiArrays"
22
uuid = "c4ea9172-b204-11e9-377d-29865faadc5c"
33
authors = ["Sheehan Olver <solver@mac.com>"]
4-
version = "0.12.1"
4+
version = "0.12.2"
55

66
[deps]
77
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/QuasiArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import LazyArrays: MemoryLayout, UnknownLayout, Mul, ApplyLayout, BroadcastLayou
5252
_mul, rowsupport, DiagonalLayout, adjointlayout, transposelayout, conjlayout,
5353
sublayout, call, LazyArrayStyle, layout_getindex, _broadcast2broadcastarray, _applyarray_summary, _broadcastarray_summary,
5454
_broadcasted_mul, simplifiable, simplify, _mul_colsupport, _mul_rowsupport,
55-
_adjoint, _transpose
55+
_adjoint, _transpose, _vec_mul_arguments, _transposeifnumber
5656

5757
import Base.IteratorsMD
5858

src/abstractquasiarraymath.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ imag(x::AbstractQuasiArray{<:Real}) = zero(x)
4949
nd = ndims(A)
5050
d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i))))
5151
return view(A, idxs...)
52-
end
52+
end

src/calculus.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ for Sum in (:sum, :cumsum)
2626
end
2727
end
2828

29+
_colon2one(::Colon) = 1
30+
_colon2one(dims::Int) = dims
31+
2932
function cumsum_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiVector, dims)
3033
a = arguments(LAY, V)
31-
apply(*, cumsum(a[1]; dims=dims), tail(a)...)
34+
apply(*, cumsum(a[1]; dims=_colon2one(dims)), tail(a)...)
3235
end
3336

34-
function sum_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiVector, ::Colon)
37+
function sum_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiVector, dims)
3538
a = arguments(LAY, V)
36-
only(*(sum(a[1]; dims=1), tail(a)...))
39+
only(*(sum(a[1]; dims=_colon2one(dims)), tail(a)...))
3740
end
3841

39-
function sum_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiMatrix, ::Colon)
42+
function sum_layout(LAY::ApplyLayout{typeof(*)}, V::AbstractQuasiMatrix, dims)
4043
a = arguments(LAY, V)
41-
only(*(sum(a[1]; dims=1), front(tail(a))..., sum(a[end]; dims=2)))
44+
only(*(sum(a[1]; dims=_colon2one(dims)), front(tail(a))..., sum(a[end]; dims=2)))
4245
end
4346

4447
sum_layout(::MemoryLayout, A, dims) = sum_size(size(A), A, dims)

src/matmul.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,7 @@ broadcasted(::LazyQuasiArrayStyle{N}, ::typeof(\), x::Number, A::MulQuasiArray{<
141141
ApplyQuasiArray(*, _ldiv_scal_reduce(x, arguments(A)...)...)
142142
broadcasted(::LazyQuasiArrayStyle{N}, ::typeof(/), A::MulQuasiArray{<:Any,N}, x::Number) where N =
143143
ApplyQuasiArray(*, _ldiv_scal_reduce(x, arguments(A)...)...)
144+
145+
_transposeifnumber(a::AbstractQuasiArray{<:Number}) = transpose(a)
146+
_vec_mul_arguments(args, (kr,jr)::Tuple{AbstractQuasiVector,Number}) = _mat_mul_arguments(args, (kr,jr))
147+
_vec_mul_arguments(args, (kr,jr)::Tuple{Number,AbstractQuasiVector}) = _vec_mul_arguments(reverse(map(_transposeifnumber, args)), (jr,kr))

src/quasiadjtrans.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ call(::ApplyLayout{typeof(*)}, V::QuasiTranspose) = *
230230
arguments(LAY::ApplyLayout{typeof(*)}, V::QuasiAdjoint) = reverse(adjoint.(arguments(LAY, V')))
231231
arguments(LAY::ApplyLayout{typeof(*)}, V::QuasiTranspose) = reverse(transpose.(arguments(LAY, V')))
232232

233+
call(lay::BroadcastLayout, At::QuasiTranspose) = call(lay, transpose(At))
234+
call(lay::BroadcastLayout, At::QuasiAdjoint{<:Real}) = call(lay, At')
235+
233236

234237
# This is used in ContinuumArrays.jl to ensure x' is lazy
235238
BroadcastStyle(::Type{<:QuasiAdjoint{<:Any,<:Inclusion}}) = LazyQuasiArrayStyle{2}()

src/quasireshapedarray.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1+
vec_layout(lay, _) = error("overload vec_layout(::$(typeof(lay)), _)")
2+
3+
4+
"""
5+
vec(a::AbstractQuasiMatrix)
6+
7+
reshapes a quasi-matrix into quasi-vector. The axes of the resulting
8+
quasi-vector depends on the axes of the quasi-matrix: if the axes are
9+
`(ax, Base.OneTo(1))` or `(Base.OneTo(1), ax)` then the unary dimension is dropped.
10+
If the axes are both continuous then it may form a quasi-vector defined on the rectangle,
11+
if MultivariateOrthogonalPolynomials.jl is loaded.
12+
"""
13+
vec(a::AbstractQuasiMatrix) = vec_layout(MemoryLayout(a), a)
14+
15+
116
reshape(parent::AbstractQuasiArray{T,N}, ndims::Val{N}) where {T,N} = parent
217
function reshape(parent::AbstractQuasiArray, ndims::Val{N}) where N
318
reshape(parent, rdims(Val(N), axes(parent)))
4-
end
19+
end
20+
21+
22+
reshape_layout(lay, a, dims...) = error("overload reshape_layout(::$(typeof(lay)), _, dims...)")
23+
24+
25+
"""
26+
reshape(a::AbstractQuasiVector)
27+
28+
will reshape a quasi-vector defined on a rectangle to a quasi-matrix.
29+
"""
30+
reshape(a::AbstractQuasiArray, dims...) = reshape_layout(MemoryLayout(a), a, dims...)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include("test_arrayops.jl")
66
include("test_quasisubarray.jl")
77
include("test_quasipermutedims.jl")
88
include("test_quasireducedim.jl")
9+
include("test_quasireshapedarray.jl")
910

1011
include("test_dense.jl")
1112
include("test_quasiadjtrans.jl")

test/test_quasiadjtrans.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33

44
using QuasiArrays, Test, Base64, LinearAlgebra
5-
import QuasiArrays: MemoryLayout
5+
using QuasiArrays: MemoryLayout
6+
using LazyArrays: BroadcastLayout
7+
68

79

810
@testset "QuasiAdjoint/Transpose" begin
@@ -258,4 +260,16 @@ import QuasiArrays: MemoryLayout
258260
@test summary(x') == "adjoint(QuasiVector{Int64, Tuple{Vector{Float64}}})"
259261
@test summary(transpose(x)) == "transpose(QuasiVector{Int64, Tuple{Vector{Float64}}})"
260262
end
263+
264+
@testset "Broadcast AdjTrans" begin
265+
x = QuasiArray([1,2],[0,0.5])
266+
@test MemoryLayout(BroadcastQuasiArray(exp, x)') isa BroadcastLayout
267+
@test MemoryLayout(transpose(BroadcastQuasiArray(exp, x))) isa BroadcastLayout
268+
@test BroadcastQuasiArray(exp, x)'[1,0.5] == exp(2)
269+
@test transpose(BroadcastQuasiArray(exp, x))[1,0.5] == exp(2)
270+
@test BroadcastQuasiArray(exp, x)'[1,0.5:0.5] == [exp(2)]
271+
@test transpose(BroadcastQuasiArray(exp, x))[1,0.5:0.5] == [exp(2)]
272+
@test exp.(axes(x,1)') isa BroadcastQuasiArray
273+
@test exp.(transpose(axes(x,1))) isa BroadcastQuasiArray
274+
end
261275
end

test/test_quasilazy.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using QuasiArrays, LazyArrays, ArrayLayouts, Base64, Test
22
import QuasiArrays: QuasiLazyLayout, QuasiArrayApplyStyle, LazyQuasiMatrix, LazyQuasiArrayStyle
3-
import LazyArrays: MulStyle, ApplyStyle
3+
import LazyArrays: MulStyle, ApplyStyle, arguments
44

55
struct MyQuasiLazyMatrix <: LazyQuasiMatrix{Float64}
66
A::QuasiArray
@@ -86,6 +86,18 @@ Base.getindex(A::MyQuasiLazyMatrix, x::Float64, y::Float64) = A.A[x,y]
8686
A = ApplyQuasiArray(*, ones(Inclusion([1,2,3]), Inclusion([4,5])), fill(2,Inclusion([4,5])))
8787
@test stringmime("text/plain", A) == "(ones(Inclusion([1, 2, 3]), Inclusion([4, 5]))) * (fill(2, Inclusion([4, 5])))"
8888
end
89+
90+
@testset "sub *" begin
91+
A = QuasiArray(rand(3,3),(0:0.5:1,Base.OneTo(3)))
92+
B = randn(3,3)
93+
M = ApplyQuasiArray(*, A, B)
94+
@test arguments(view(M,0.5,:)) == (B', A[0.5,:])
95+
@test arguments(view(M,:,2)) == (A, B[:,2])
96+
97+
M = ApplyQuasiArray(*, B, A')
98+
@test arguments(view(M,:,0.5)) == (B, A[0.5,:])
99+
@test arguments(view(M,2,:)) == (A, B[2,:])
100+
end
89101
end
90102
@testset "\\" begin
91103
A = QuasiArray(rand(3,3),(0:0.5:1,0:0.5:1))

0 commit comments

Comments
 (0)