Skip to content

Commit 739c470

Browse files
authored
improved TTFX in multiplication (#164)
1 parent 46d472d commit 739c470

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/LinearAlgebra/standardchop.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function standardchoplength(coeffs, tol)
5757
# Step 1: Convert COEFFS to a new monotonically nonincreasing
5858
# vector ENVELOPE normalized to begin with the value 1.
5959

60-
b = abs.(coeffs)
61-
m = b[end]*fill(1.0,n)
60+
b = map(abs, coeffs)
61+
m = fill(b[end]*1.0, n)
6262
for j = n-1:-1:1
6363
m[j] = max(b[j], m[j+1]);
6464
end
@@ -94,13 +94,16 @@ function standardchoplength(coeffs, tol)
9494
return plateauPoint
9595
end
9696

97-
j3 = sum(envelope .≥ tol^(7/6))
97+
j3 = count((tol^(7/6)), envelope)
9898
if j3 < j2
9999
j2 = j3 + 1
100100
envelope[j2] = tol^(7/6)
101101
end
102-
cc = log10.(envelope[1:j2])
103-
cc .+= range(0, stop=(-1/3)*log10(tol), length=j2)
102+
cc = map(log10, @view envelope[1:j2])
103+
rng = range(0, stop=(-1/3)*log10(tol), length=j2)
104+
for (i,ri) in enumerate(rng)
105+
cc[i] += ri
106+
end
104107
d = argmin(cc)
105108
return max(d - 1, 1)
106109
end

src/Operators/general/algebra.jl

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ domain(P::PlusOperator) = commondomain(P.ops)
6363

6464
_promote_eltypeof(As...) = _promote_eltypeof(As)
6565
_promote_eltypeof(As::Union{Vector, Tuple}) = mapreduce(eltype, promote_type, As)
66+
_promote_eltypeof(As::Vector{Operator{T}}) where {T} = T
6667

6768
_extractops(A, ::Any) = [A]
6869
_extractops(A::PlusOperator, ::typeof(+)) = A.ops
@@ -265,38 +266,26 @@ end
265266

266267

267268

268-
function promotetimes(opsin::Vector{B},dsp) where B<:Operator
269+
function promotetimes(opsin::Vector{<:Operator}, dsp = domainspace(last(opsin)))
270+
271+
@assert length(opsin) > 1 "need at least 2 operators"
269272
ops=Vector{Operator{_promote_eltypeof(opsin)}}(undef,0)
273+
sizehint!(ops, length(opsin))
270274

271275
for k=length(opsin):-1:1
272276
if !isa(opsin[k],Conversion)
273277
op=promotedomainspace(opsin[k],dsp)
274-
if op==()
275-
# do nothing
276-
elseif isa(op,TimesOperator)
277-
for j=length(op.ops):-1:1
278-
push!(ops,op.ops[j])
279-
end
280-
dsp=rangespace(op)
278+
dsp=rangespace(op)
279+
if isa(op,TimesOperator)
280+
append!(ops, view(op.ops, reverse(axes(op.ops,1))))
281281
else
282282
push!(ops,op)
283-
dsp=rangespace(op)
284283
end
285284
end
286285
end
287-
if isempty(ops)
288-
ConstantOperator(1.0,dsp)
289-
elseif length(ops)==1
290-
first(ops)
291-
else
292-
TimesOperator(reverse!(ops)) # change order in TImesOperator if this is slow
293-
end
286+
TimesOperator(reverse!(ops))
294287
end
295288

296-
promotetimes(opsin::Vector{B}) where {B<:Operator}=promotetimes(opsin,domainspace(last(opsin)))
297-
298-
299-
300289
domainspace(P::TimesOperator)=domainspace(last(P.ops))
301290
rangespace(P::TimesOperator)=rangespace(first(P.ops))
302291

@@ -490,7 +479,8 @@ function *(A::Operator,B::Operator)
490479
elseif isconstop(B)
491480
promotedomainspace(strictconvert(Number,B)*A,domainspace(B))
492481
else
493-
promotetimes(Operator{_promote_eltypeof(A, B)}[_extractops(A, *); _extractops(B, *)])
482+
promotetimes([_extractops(A, *); _extractops(B, *)],
483+
domainspace(B))
494484
end
495485
end
496486

@@ -619,7 +609,7 @@ function promotedomainspace(P::TimesOperator,sp::Space,cursp::Space)
619609
elseif length(P.ops)==2
620610
P.ops[1]*promotedomainspace(P.ops[end],sp)
621611
else
622-
promotetimes([P.ops[1:end-1];promotedomainspace(P.ops[end],sp)])
612+
promotetimes([P.ops[1:end-1];promotedomainspace(P.ops[end],sp)], sp)
623613
end
624614
end
625615

0 commit comments

Comments
 (0)