Skip to content

Commit 4715c63

Browse files
committed
Deprecate scale! in favor of mul!, mul1!, and mul2!
1 parent bea2fb7 commit 4715c63

19 files changed

+195
-192
lines changed

Diff for: stdlib/IterativeEigensolvers/src/IterativeEigensolvers.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module IterativeEigensolvers
99

1010
using LinearAlgebra: BlasFloat, BlasInt, SVD, checksquare, mul!,
1111
UniformScaling, issymmetric, ishermitian,
12-
factorize, I, scale!, qr
12+
factorize, I, mul1!, qr
1313
import LinearAlgebra
1414

1515
export eigs, svds
@@ -317,10 +317,10 @@ function _svds(X; nsv::Int = 6, ritzvec::Bool = true, tol::Float64 = 0.0, maxite
317317
# left_sv = sqrt(2) * ex[2][ 1:size(X,1), ind ] .* sign.(ex[1][ind]')
318318
if size(X, 1) >= size(X, 2)
319319
V = ex[2]
320-
U = qr(scale!(X*V, inv.(svals)))[1]
320+
U = qr(mul1!(X*V, Diagonal(inv.(svals))))[1]
321321
else
322322
U = ex[2]
323-
V = qr(scale!(X'U, inv.(svals)))[1]
323+
V = qr(mul1!(X'U, Diagonal(inv.(svals))))[1]
324324
end
325325

326326
# right_sv = sqrt(2) * ex[2][ size(X,1)+1:end, ind ]

Diff for: stdlib/LinearAlgebra/docs/src/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ LinearAlgebra.tril!
354354
LinearAlgebra.diagind
355355
LinearAlgebra.diag
356356
LinearAlgebra.diagm
357-
LinearAlgebra.scale!
357+
LinearAlgebra.mul1!
358+
LinearAlgebra.mul2!
358359
LinearAlgebra.rank
359360
LinearAlgebra.norm
360361
LinearAlgebra.vecnorm

Diff for: stdlib/LinearAlgebra/src/LinearAlgebra.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export
9898
istril,
9999
istriu,
100100
kron,
101+
ldiv!,
101102
ldltfact!,
102103
ldltfact,
103104
linreg,
@@ -107,6 +108,9 @@ export
107108
lufact,
108109
lufact!,
109110
lyap,
111+
mul!,
112+
mul1!,
113+
mul2!,
110114
norm,
111115
normalize,
112116
normalize!,
@@ -122,7 +126,7 @@ export
122126
lqfact!,
123127
lqfact,
124128
rank,
125-
scale!,
129+
rdiv!,
126130
schur,
127131
schurfact!,
128132
schurfact,

Diff for: stdlib/LinearAlgebra/src/dense.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ const NRM2_CUTOFF = 32
1414
# This constant should ideally be determined by the actual CPU cache size
1515
const ISONE_CUTOFF = 2^21 # 2M
1616

17-
function scale!(X::Array{T}, s::T) where T<:BlasFloat
17+
function mul1!(X::Array{T}, s::T) where T<:BlasFloat
1818
s == 0 && return fill!(X, zero(T))
1919
s == 1 && return X
2020
if length(X) < SCAL_CUTOFF
21-
generic_scale!(X, s)
21+
generic_mul1!(X, s)
2222
else
2323
BLAS.scal!(length(X), s, X, 1)
2424
end
2525
X
2626
end
2727

28-
scale!(s::T, X::Array{T}) where {T<:BlasFloat} = scale!(X, s)
28+
mul2!(s::T, X::Array{T}) where {T<:BlasFloat} = mul1!(X, s)
2929

30-
scale!(X::Array{T}, s::Number) where {T<:BlasFloat} = scale!(X, convert(T, s))
31-
function scale!(X::Array{T}, s::Real) where T<:BlasComplex
30+
mul1!(X::Array{T}, s::Number) where {T<:BlasFloat} = mul1!(X, convert(T, s))
31+
function mul1!(X::Array{T}, s::Real) where T<:BlasComplex
3232
R = typeof(real(zero(T)))
3333
GC.@preserve X BLAS.scal!(2*length(X), convert(R,s), convert(Ptr{R},pointer(X)), 1)
3434
X
@@ -1402,7 +1402,7 @@ function sylvester(A::StridedMatrix{T},B::StridedMatrix{T},C::StridedMatrix{T})
14021402

14031403
D = -(adjoint(QA) * (C*QB))
14041404
Y, scale = LAPACK.trsyl!('N','N', RA, RB, D)
1405-
scale!(QA*(Y * adjoint(QB)), inv(scale))
1405+
mul1!(QA*(Y * adjoint(QB)), inv(scale))
14061406
end
14071407
sylvester(A::StridedMatrix{T}, B::StridedMatrix{T}, C::StridedMatrix{T}) where {T<:Integer} = sylvester(float(A), float(B), float(C))
14081408

@@ -1445,7 +1445,7 @@ function lyap(A::StridedMatrix{T}, C::StridedMatrix{T}) where {T<:BlasFloat}
14451445

14461446
D = -(adjoint(Q) * (C*Q))
14471447
Y, scale = LAPACK.trsyl!('N', T <: Complex ? 'C' : 'T', R, R, D)
1448-
scale!(Q*(Y * adjoint(Q)), inv(scale))
1448+
mul1!(Q*(Y * adjoint(Q)), inv(scale))
14491449
end
14501450
lyap(A::StridedMatrix{T}, C::StridedMatrix{T}) where {T<:Integer} = lyap(float(A), float(C))
14511451
lyap(a::T, c::T) where {T<:Number} = -c/(2a)

Diff for: stdlib/LinearAlgebra/src/deprecated.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1268,3 +1268,11 @@ function getindex(F::Factorization, s::Symbol)
12681268
return getproperty(F, s)
12691269
end
12701270
@deprecate getq(F::Factorization) F.Q
1271+
1272+
# Deprecate scaling
1273+
@deprecate scale!(A::AbstractArray, b::Number) mul1!(A, b)
1274+
@deprecate scale!(a::Number, B::AbstractArray) mul2!(a, B)
1275+
@deprecate scale!(A::AbstractMatrix, b::AbstractVector) mul1!(A, Diagonal(b))
1276+
@deprecate scale!(a::AbstractVector, B::AbstractMatrix) mul2!(Diagonal(a), B)
1277+
@deprecate scale!(C::AbstractMatrix, A::AbstractMatrix, b::AbstractVector) mul!(X, A, Diagonal(b))
1278+
@deprecate scale!(C::AbstractMatrix, a::AbstractVector, B::AbstractMatrix) mul!(X, Diagonal(a), B)

Diff for: stdlib/LinearAlgebra/src/diagonal.jl

+30-10
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,19 @@ end
155155
(*)(D::Diagonal, B::AbstractTriangular) = mul2!(D, copy(B))
156156

157157
(*)(A::AbstractMatrix, D::Diagonal) =
158-
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), A, D.diag)
158+
mul!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), A, D)
159159
(*)(D::Diagonal, A::AbstractMatrix) =
160-
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), D.diag, A)
160+
mul!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), D, A)
161161

162+
function mul1!(A::AbstractMatrix, D::Diagonal)
163+
A .= A .* transpose(D.diag)
164+
return A
165+
end
166+
167+
function mul2!(D::Diagonal, B::AbstractMatrix)
168+
B .= D.diag .* B
169+
return B
170+
end
162171

163172
mul1!(A::Union{LowerTriangular,UpperTriangular}, D::Diagonal) = typeof(A)(mul1!(A.data, D))
164173
function mul1!(A::UnitLowerTriangular, D::Diagonal)
@@ -233,15 +242,26 @@ end
233242
*(D::Transpose{<:Any,<:Diagonal}, B::Transpose{<:Any,<:Diagonal}) =
234243
Diagonal(transpose.(D.parent.diag) .* transpose.(B.parent.diag))
235244

236-
mul1!(A::Diagonal,B::Diagonal) = Diagonal(A.diag .*= B.diag)
237-
mul2!(A::Diagonal,B::Diagonal) = Diagonal(B.diag .= A.diag .* B.diag)
245+
mul1!(A::Diagonal, B::Diagonal) = Diagonal(A.diag .*= B.diag)
246+
mul2!(A::Diagonal, B::Diagonal) = Diagonal(B.diag .= A.diag .* B.diag)
238247

239-
mul2!(A::Diagonal, B::AbstractMatrix) = scale!(A.diag, B)
240-
mul2!(adjA::Adjoint{<:Any,<:Diagonal}, B::AbstractMatrix) = (A = adjA.parent; scale!(conj(A.diag),B))
241-
mul2!(transA::Transpose{<:Any,<:Diagonal}, B::AbstractMatrix) = (A = transA.parent; scale!(A.diag,B))
242-
mul1!(A::AbstractMatrix, B::Diagonal) = scale!(A,B.diag)
243-
mul1!(A::AbstractMatrix, adjB::Adjoint{<:Any,<:Diagonal}) = (B = adjB.parent; scale!(A,conj(B.diag)))
244-
mul1!(A::AbstractMatrix, transB::Transpose{<:Any,<:Diagonal}) = (B = transB.parent; scale!(A,B.diag))
248+
function mul2!(adjA::Adjoint{<:Any,<:Diagonal}, B::AbstractMatrix)
249+
A = adjA.parent
250+
return mul2!(conj(A.diag), B)
251+
end
252+
function mul2!(transA::Transpose{<:Any,<:Diagonal}, B::AbstractMatrix)
253+
A = transA.parent
254+
return mul2!(A.diag, B)
255+
end
256+
257+
function mul1!(A::AbstractMatrix, adjB::Adjoint{<:Any,<:Diagonal})
258+
B = adjB.parent
259+
return mul1!(A, conj(B.diag))
260+
end
261+
function mul1!(A::AbstractMatrix, transB::Transpose{<:Any,<:Diagonal})
262+
B = transB.parent
263+
return mul1!(A, B.diag)
264+
end
245265

246266
# Get ambiguous method if try to unify AbstractVector/AbstractMatrix here using AbstractVecOrMat
247267
mul!(out::AbstractVector, A::Diagonal, in::AbstractVector) = out .= A.diag .* in

Diff for: stdlib/LinearAlgebra/src/generic.jl

+28-46
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
# For better performance when input and output are the same array
66
# See https://github.com/JuliaLang/julia/issues/8415#issuecomment-56608729
7-
function generic_scale!(X::AbstractArray, s::Number)
7+
function generic_mul1!(X::AbstractArray, s::Number)
88
@simd for I in eachindex(X)
99
@inbounds X[I] *= s
1010
end
1111
X
1212
end
1313

14-
function generic_scale!(s::Number, X::AbstractArray)
14+
function generic_mul2!(s::Number, X::AbstractArray)
1515
@simd for I in eachindex(X)
1616
@inbounds X[I] = s*X[I]
1717
end
1818
X
1919
end
2020

21-
function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
21+
function generic_mul!(C::AbstractArray, X::AbstractArray, s::Number)
2222
if _length(C) != _length(X)
2323
throw(DimensionMismatch("first array has length $(_length(C)) which does not match the length of the second, $(_length(X))."))
2424
end
@@ -28,7 +28,7 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
2828
C
2929
end
3030

31-
function generic_scale!(C::AbstractArray, s::Number, X::AbstractArray)
31+
function generic_mul!(C::AbstractArray, s::Number, X::AbstractArray)
3232
if _length(C) != _length(X)
3333
throw(DimensionMismatch("first array has length $(_length(C)) which does not
3434
match the length of the second, $(_length(X))."))
@@ -39,50 +39,48 @@ match the length of the second, $(_length(X))."))
3939
C
4040
end
4141

42-
scale!(C::AbstractArray, s::Number, X::AbstractArray) = generic_scale!(C, X, s)
43-
scale!(C::AbstractArray, X::AbstractArray, s::Number) = generic_scale!(C, s, X)
42+
mul!(C::AbstractArray, s::Number, X::AbstractArray) = generic_mul!(C, X, s)
43+
mul!(C::AbstractArray, X::AbstractArray, s::Number) = generic_mul!(C, s, X)
4444

4545
"""
46-
scale!(A, b)
47-
scale!(b, A)
46+
mul1!(A, b)
4847
4948
Scale an array `A` by a scalar `b` overwriting `A` in-place.
5049
51-
If `A` is a matrix and `b` is a vector, then `scale!(A,b)` scales each column `i` of `A` by
52-
`b[i]` (similar to `A*Diagonal(b)`), while `scale!(b,A)` scales each row `i` of `A` by `b[i]`
53-
(similar to `Diagonal(b)*A`), again operating in-place on `A`. An `InexactError` exception is
54-
thrown if the scaling produces a number not representable by the element type of `A`,
55-
e.g. for integer types.
56-
5750
# Examples
5851
```jldoctest
5952
julia> a = [1 2; 3 4]
6053
2×2 Array{Int64,2}:
6154
1 2
6255
3 4
6356
64-
julia> b = [1; 2]
65-
2-element Array{Int64,1}:
66-
1
67-
2
68-
69-
julia> scale!(a,b)
57+
julia> mul1!(a, 2)
7058
2×2 Array{Int64,2}:
71-
1 4
72-
3 8
59+
2 4
60+
6 8
61+
```
62+
"""
63+
mul1!(X::AbstractArray, s::Number) = generic_mul1!(X, s)
7364

74-
julia> a = [1 2; 3 4];
65+
"""
66+
mul2!(A, b)
7567
76-
julia> b = [1; 2];
68+
Scale an array `A` by a scalar `b` overwriting `A` in-place.
7769
78-
julia> scale!(b,a)
70+
# Examples
71+
```jldoctest
72+
julia> a = [1 2; 3 4]
7973
2×2 Array{Int64,2}:
8074
1 2
75+
3 4
76+
77+
julia> mul2!(2, a)
78+
2×2 Array{Int64,2}:
79+
2 4
8180
6 8
8281
```
8382
"""
84-
scale!(X::AbstractArray, s::Number) = generic_scale!(X, s)
85-
scale!(s::Number, X::AbstractArray) = generic_scale!(s, X)
83+
mul2!(s::Number, X::AbstractArray) = generic_mul2!(s, X)
8684

8785
"""
8886
cross(x, y)
@@ -1185,22 +1183,6 @@ function linreg(x::AbstractVector, y::AbstractVector)
11851183
return (a, b)
11861184
end
11871185

1188-
# multiply by diagonal matrix as vector
1189-
#diagmm!(C::AbstractMatrix, A::AbstractMatrix, b::AbstractVector)
1190-
1191-
#diagmm!(C::AbstractMatrix, b::AbstractVector, A::AbstractMatrix)
1192-
1193-
scale!(A::AbstractMatrix, b::AbstractVector) = scale!(A,A,b)
1194-
scale!(b::AbstractVector, A::AbstractMatrix) = scale!(A,b,A)
1195-
1196-
#diagmm(A::AbstractMatrix, b::AbstractVector)
1197-
#diagmm(b::AbstractVector, A::AbstractMatrix)
1198-
1199-
#^(A::AbstractMatrix, p::Number)
1200-
1201-
#findmax(a::AbstractArray)
1202-
#findmin(a::AbstractArray)
1203-
12041186
"""
12051187
peakflops(n::Integer=2000; parallel::Bool=false)
12061188
@@ -1457,12 +1439,12 @@ end
14571439

14581440
if nrm δ # Safe to multiply with inverse
14591441
invnrm = inv(nrm)
1460-
scale!(v, invnrm)
1442+
mul1!(v, invnrm)
14611443

14621444
else # scale elements to avoid overflow
14631445
εδ = eps(one(nrm))/δ
1464-
scale!(v, εδ)
1465-
scale!(v, inv(nrm*εδ))
1446+
mul1!(v, εδ)
1447+
mul1!(v, inv(nrm*εδ))
14661448
end
14671449

14681450
v

Diff for: stdlib/LinearAlgebra/src/matmul.jl

-32
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,6 @@
44

55
matprod(x, y) = x*y + x*y
66

7-
# multiply by diagonal matrix as vector
8-
function scale!(C::AbstractMatrix, A::AbstractMatrix, b::AbstractVector)
9-
m, n = size(A)
10-
if size(A) != size(C)
11-
throw(DimensionMismatch("size of A, $(size(A)), does not match size of C, $(size(C))"))
12-
end
13-
if n != length(b)
14-
throw(DimensionMismatch("second dimension of A, $n, does not match length of b, $(length(b))"))
15-
end
16-
@inbounds for j = 1:n
17-
bj = b[j]
18-
for i = 1:m
19-
C[i,j] = A[i,j]*bj
20-
end
21-
end
22-
C
23-
end
24-
25-
function scale!(C::AbstractMatrix, b::AbstractVector, A::AbstractMatrix)
26-
m, n = size(A)
27-
if size(A) != size(C)
28-
throw(DimensionMismatch("size of A, $(size(A)), does not match size of C, $(size(C))"))
29-
end
30-
if m != length(b)
31-
throw(DimensionMismatch("first dimension of A, $m, does not match length of b, $(length(b))"))
32-
end
33-
@inbounds for j = 1:n, i = 1:m
34-
C[i,j] = A[i,j]*b[i]
35-
end
36-
C
37-
end
38-
397
# Dot products
408

419
vecdot(x::Union{DenseArray{T},StridedVector{T}}, y::Union{DenseArray{T},StridedVector{T}}) where {T<:BlasReal} = BLAS.dot(x, y)

0 commit comments

Comments
 (0)