Skip to content

Commit 9f5a288

Browse files
authored
three-term multiplication involving conversions (#480)
1 parent c2dd5ca commit 9f5a288

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.8.33"
3+
version = "0.8.34"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Operators/general/algebra.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,14 @@ end
282282

283283
const PlusOrTimesOp = Union{PlusOperator,TimesOperator}
284284

285-
bandwidthssum(P, f=bandwidths) = mapreduce(f, (t1, t2) -> t1 .+ t2, P, init=(0, 0))
286-
_bandwidthssum(A::Operator, B::Operator, f=bandwidths) = __bandwidthssum(f(A), f(B))
287-
__bandwidthssum(A::NTuple{2,InfiniteCardinal{0}}, B::NTuple{2,InfiniteCardinal{0}}) = A
288-
__bandwidthssum(A::NTuple{2,InfiniteCardinal{0}}, B) = A
289-
__bandwidthssum(A, B::NTuple{2,InfiniteCardinal{0}}) = B
290-
__bandwidthssum(A, B) = reduce((t1, t2) -> t1 .+ t2, (A, B), init=(0, 0))
285+
bandwidthssum(f, ops) = mapfoldl(f, (t1, t2) -> t1 .+ t2, ops, init=(0, 0))
291286

292287
_timessize(ops) = (size(first(ops), 1), size(last(ops), 2))
293288
function TimesOperator(ops::AbstractVector{O},
294-
bw::Tuple{Any,Any}=bandwidthssum(ops),
289+
bw::Tuple{Any,Any}=bandwidthssum(bandwidths, ops),
295290
sz::Tuple{Any,Any}=_timessize(ops),
296-
bbw::Tuple{Any,Any}=bandwidthssum(ops, blockbandwidths),
297-
sbbw::Tuple{Any,Any}=bandwidthssum(ops, subblockbandwidths),
291+
bbw::Tuple{Any,Any}=bandwidthssum(blockbandwidths, ops),
292+
sbbw::Tuple{Any,Any}=bandwidthssum(subblockbandwidths, ops),
298293
ibbb::Bool=all(isbandedblockbanded, ops),
299294
irb::Bool=all(israggedbelow, ops),
300295
isaf::Bool = sz[1] == 1 && isconstspace(rangespace(first(ops)));
@@ -306,16 +301,19 @@ end
306301

307302
_extractops(A::TimesOperator, ::typeof(*)) = A.ops
308303

309-
function TimesOperator(A::Operator, B::Operator)
310-
v = collateops(*, A, B)
311-
ibbb = all(isbandedblockbanded, (A, B))
312-
irb = all(israggedbelow, (A, B))
313-
sz = _timessize((A, B))
304+
function TimesOperator(A::Operator, Bs::Operator...)
305+
ops = (A, Bs...)
306+
v = collateops(*, ops...)
307+
ibbb = all(isbandedblockbanded, ops)
308+
irb = all(israggedbelow, ops)
309+
sz = _timessize(ops)
314310
isaf = sz[1] == 1 && isconstspace(rangespace(A))
315-
anytimesop = any(x -> x isa TimesOperator, (A,B))
316-
TimesOperator(convert_vector(v), _bandwidthssum(A, B), sz,
317-
_bandwidthssum(A, B, blockbandwidths),
318-
_bandwidthssum(A, B, subblockbandwidths), ibbb, irb, isaf;
311+
anytimesop = any(x -> x isa TimesOperator, ops)
312+
bwsum = bandwidthssum(bandwidths, ops)
313+
bbwsum = bandwidthssum(blockbandwidths, ops)
314+
subbbwsum = bandwidthssum(subblockbandwidths, ops)
315+
TimesOperator(convert_vector(v), bwsum, sz,
316+
bbwsum, subbbwsum, ibbb, irb, isaf;
319317
anytimesop)
320318
end
321319

@@ -368,8 +366,8 @@ function __promotetimes(opsin, dsp, anytimesop)
368366
end
369367
end
370368
end
371-
reverse!(ops), bandwidthssum(ops), bandwidthssum(ops, blockbandwidths),
372-
bandwidthssum(ops, subblockbandwidths), all(isbandedblockbanded, ops),
369+
reverse!(ops), bandwidthssum(bandwidths, ops), bandwidthssum(blockbandwidths, ops),
370+
bandwidthssum(subblockbandwidths, ops), all(isbandedblockbanded, ops),
373371
all(israggedbelow, ops)
374372
end
375373
@inline function _op_bws(op)
@@ -396,9 +394,9 @@ end
396394
op2_dsp = op2:dsp
397395
op1_dsp = op1:rangespace(op2_dsp)
398396
ops = (op1_dsp, op2_dsp)
399-
return ops, bandwidthssum(ops),
400-
bandwidthssum(ops, blockbandwidths),
401-
bandwidthssum(ops, subblockbandwidths),
397+
return ops, bandwidthssum(bandwidths, ops),
398+
bandwidthssum(blockbandwidths, ops),
399+
bandwidthssum(subblockbandwidths, ops),
402400
all(isbandedblockbanded, ops),
403401
all(israggedbelow, ops)
404402
end
@@ -653,6 +651,10 @@ end
653651
*(A::Conversion, B::Operator) =
654652
isconstop(B) ? promotedomainspace(strictconvert(Number, B) * A, domainspace(B)) : TimesOperator(A, B)
655653

654+
function *(A::Conversion, B::Operator, C::Conversion)
655+
TimesOperator(A, B, C)
656+
end
657+
656658
@inline function ^(A::Operator, p::Integer)
657659
p < 0 && return ^(inv(A), -p)
658660
p == 0 && return ConstantOperator(one(eltype(A)), domainspace(A))

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ end
424424
@test g f
425425
ApproxFunBase.mul_coefficients!(Operator(2I), v)
426426
@test v Float64[2i^2 for i in 1:4]
427+
428+
C = Conversion(PointSpace(1:4), PointSpace(1:4))
429+
M = Multiplication(Fun(PointSpace(1:4)), PointSpace(1:4))
430+
M2 = @inferred C * M * C
431+
@test M2 * f M * f
427432
end
428433
@testset "ConstantOperator" begin
429434
C = ConstantOperator(3.0, PointSpace(1:4))

0 commit comments

Comments
 (0)