Skip to content

Commit 0a1a612

Browse files
committed
Merge pull request #14601 from tpapp/master
cosmetic fixes for `LinAlg.chksquare`
2 parents 9464e6f + 375c4ac commit 0a1a612

File tree

15 files changed

+144
-126
lines changed

15 files changed

+144
-126
lines changed

base/linalg.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,18 @@ function chkstride1(A...)
179179
end
180180
end
181181

182-
# Check that matrix is square
183-
function chksquare(A)
182+
"""
183+
LinAlg.checksquare(A)
184+
185+
Check that a matrix is square, then return its common dimension. For multiple arguments, return a vector.
186+
"""
187+
function checksquare(A)
184188
m,n = size(A)
185189
m == n || throw(DimensionMismatch("matrix is not square"))
186190
m
187191
end
188192

189-
function chksquare(A...)
193+
function checksquare(A...)
190194
sizes = Int[]
191195
for a in A
192196
size(a,1)==size(a,2) || throw(DimensionMismatch("matrix is not square: dimensions are $(size(a))"))

base/linalg/arnoldi.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function _eigs(A, B;
122122
tol=0.0, maxiter::Integer=300, sigma=nothing, v0::Vector=zeros(eltype(A),(0,)),
123123
ritzvec::Bool=true)
124124

125-
n = chksquare(A)
125+
n = checksquare(A)
126126

127127
T = eltype(A)
128128
iscmplx = T <: Complex

base/linalg/blas.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export
5656

5757
const libblas = Base.libblas_name
5858

59-
import ..LinAlg: BlasReal, BlasComplex, BlasFloat, BlasInt, DimensionMismatch, chksquare, axpy!
59+
import ..LinAlg: BlasReal, BlasComplex, BlasFloat, BlasInt, DimensionMismatch, checksquare, axpy!
6060

6161
"""
6262
blas_set_num_threads(n)
@@ -682,7 +682,7 @@ for (fname, elty) in ((:dtrmv_,:Float64),
682682
# * .. Array Arguments ..
683683
# DOUBLE PRECISION A(LDA,*),X(*)
684684
function trmv!(uplo::Char, trans::Char, diag::Char, A::StridedMatrix{$elty}, x::StridedVector{$elty})
685-
n = chksquare(A)
685+
n = checksquare(A)
686686
if n != length(x)
687687
throw(DimensionMismatch("A has size ($n,$n), x has length $(length(x))"))
688688
end
@@ -731,7 +731,7 @@ for (fname, elty) in ((:dtrsv_,:Float64),
731731
# .. Array Arguments ..
732732
# DOUBLE PRECISION A(LDA,*),X(*)
733733
function trsv!(uplo::Char, trans::Char, diag::Char, A::StridedMatrix{$elty}, x::StridedVector{$elty})
734-
n = chksquare(A)
734+
n = checksquare(A)
735735
if n != length(x)
736736
throw(DimensionMismatch("size of A is $n != length(x) = $(length(x))"))
737737
end
@@ -795,7 +795,7 @@ for (fname, elty) in ((:dsyr_,:Float64),
795795
(:csyr_,:Complex64))
796796
@eval begin
797797
function syr!(uplo::Char, α::$elty, x::StridedVector{$elty}, A::StridedMatrix{$elty})
798-
n = chksquare(A)
798+
n = checksquare(A)
799799
if length(x) != n
800800
throw(DimensionMismatch("A has size ($n,$n), x has length $(length(x))"))
801801
end
@@ -824,7 +824,7 @@ for (fname, elty, relty) in ((:zher_,:Complex128, :Float64),
824824
(:cher_,:Complex64, :Float32))
825825
@eval begin
826826
function her!(uplo::Char, α::$relty, x::StridedVector{$elty}, A::StridedMatrix{$elty})
827-
n = chksquare(A)
827+
n = checksquare(A)
828828
if length(x) != n
829829
throw(DimensionMismatch("A has size ($n,$n), x has length $(length(x))"))
830830
end
@@ -922,7 +922,7 @@ for (mfname, elty) in ((:dsymm_,:Float64),
922922
# DOUBLE PRECISION A(LDA,*),B(LDB,*),C(LDC,*)
923923
function symm!(side::Char, uplo::Char, alpha::($elty), A::StridedMatrix{$elty}, B::StridedMatrix{$elty}, beta::($elty), C::StridedMatrix{$elty})
924924
m, n = size(C)
925-
j = chksquare(A)
925+
j = checksquare(A)
926926
if j != (side == 'L' ? m : n)
927927
throw(DimensionMismatch("A has size $(size(A)), C has size ($m,$n)"))
928928
end
@@ -991,7 +991,7 @@ for (mfname, elty) in ((:zhemm_,:Complex128),
991991
# DOUBLE PRECISION A(LDA,*),B(LDB,*),C(LDC,*)
992992
function hemm!(side::Char, uplo::Char, alpha::($elty), A::StridedMatrix{$elty}, B::StridedMatrix{$elty}, beta::($elty), C::StridedMatrix{$elty})
993993
m, n = size(C)
994-
j = chksquare(A)
994+
j = checksquare(A)
995995
if j != (side == 'L' ? m : n)
996996
throw(DimensionMismatch("A has size $(size(A)), C has size ($m,$n)"))
997997
end
@@ -1050,7 +1050,7 @@ for (fname, elty) in ((:dsyrk_,:Float64),
10501050
function syrk!(uplo::Char, trans::Char,
10511051
alpha::($elty), A::StridedVecOrMat{$elty},
10521052
beta::($elty), C::StridedMatrix{$elty})
1053-
n = chksquare(C)
1053+
n = checksquare(C)
10541054
nn = size(A, trans == 'N' ? 1 : 2)
10551055
if nn != n throw(DimensionMismatch("C has size ($n,$n), corresponding dimension of A is $nn")) end
10561056
k = size(A, trans == 'N' ? 2 : 1)
@@ -1103,7 +1103,7 @@ for (fname, elty, relty) in ((:zherk_, :Complex128, :Float64),
11031103
# COMPLEX A(LDA,*),C(LDC,*)
11041104
function herk!(uplo::Char, trans::Char, α::$relty, A::StridedVecOrMat{$elty},
11051105
β::$relty, C::StridedMatrix{$elty})
1106-
n = chksquare(C)
1106+
n = checksquare(C)
11071107
nn = size(A, trans == 'N' ? 1 : 2)
11081108
if nn != n
11091109
throw(DimensionMismatch("the matrix to update has dimension $n but the implied dimension of the update is $(size(A, trans == 'N' ? 1 : 2))"))
@@ -1144,7 +1144,7 @@ for (fname, elty) in ((:dsyr2k_,:Float64),
11441144
function syr2k!(uplo::Char, trans::Char,
11451145
alpha::($elty), A::StridedVecOrMat{$elty}, B::StridedVecOrMat{$elty},
11461146
beta::($elty), C::StridedMatrix{$elty})
1147-
n = chksquare(C)
1147+
n = checksquare(C)
11481148
nn = size(A, trans == 'N' ? 1 : 2)
11491149
if nn != n throw(DimensionMismatch("C has size ($n,$n), corresponding dimension of A is $nn")) end
11501150
k = size(A, trans == 'N' ? 2 : 1)
@@ -1181,7 +1181,7 @@ for (fname, elty1, elty2) in ((:zher2k_,:Complex128,:Float64), (:cher2k_,:Comple
11811181
function her2k!(uplo::Char, trans::Char, alpha::($elty1),
11821182
A::StridedVecOrMat{$elty1}, B::StridedVecOrMat{$elty1},
11831183
beta::($elty2), C::StridedMatrix{$elty1})
1184-
n = chksquare(C)
1184+
n = checksquare(C)
11851185
nn = size(A, trans == 'N' ? 1 : 2)
11861186
if nn != n throw(DimensionMismatch("C has size ($n,$n), corresponding dimension of A is $nn")) end
11871187
k = size(A, trans == 'N' ? 2 : 1)
@@ -1258,7 +1258,7 @@ for (mmname, smname, elty) in
12581258
function trmm!(side::Char, uplo::Char, transa::Char, diag::Char, alpha::Number,
12591259
A::StridedMatrix{$elty}, B::StridedMatrix{$elty})
12601260
m, n = size(B)
1261-
nA = chksquare(A)
1261+
nA = checksquare(A)
12621262
if nA != (side == 'L' ? m : n)
12631263
throw(DimensionMismatch("size of A, $(size(A)), doesn't match $side size of B with dims, $(size(B))"))
12641264
end
@@ -1283,7 +1283,7 @@ for (mmname, smname, elty) in
12831283
function trsm!(side::Char, uplo::Char, transa::Char, diag::Char,
12841284
alpha::$elty, A::StridedMatrix{$elty}, B::StridedMatrix{$elty})
12851285
m, n = size(B)
1286-
k = chksquare(A)
1286+
k = checksquare(A)
12871287
if k != (side == 'L' ? m : n)
12881288
throw(DimensionMismatch("size of A is $n, size(B)=($m,$n) and transa='$transa'"))
12891289
end

base/linalg/cholesky.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434
chol!(A::StridedMatrix) = chol!(A, UpperTriangular)
3535

3636
function chol!{T}(A::AbstractMatrix{T}, ::Type{UpperTriangular})
37-
n = chksquare(A)
37+
n = checksquare(A)
3838
@inbounds begin
3939
for k = 1:n
4040
for i = 1:k - 1
@@ -54,7 +54,7 @@ function chol!{T}(A::AbstractMatrix{T}, ::Type{UpperTriangular})
5454
return UpperTriangular(A)
5555
end
5656
function chol!{T}(A::AbstractMatrix{T}, ::Type{LowerTriangular})
57-
n = chksquare(A)
57+
n = checksquare(A)
5858
@inbounds begin
5959
for k = 1:n
6060
for i = 1:k - 1

base/linalg/dense.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ end
142142
diagm(x::Number) = (X = Array(typeof(x),1,1); X[1,1] = x; X)
143143

144144
function trace{T}(A::Matrix{T})
145-
n = chksquare(A)
145+
n = checksquare(A)
146146
t = zero(T)
147147
for i=1:n
148148
t += A[i,i]
@@ -175,7 +175,7 @@ function ^(A::Matrix, p::Number)
175175
if isinteger(p)
176176
return A^Integer(real(p))
177177
end
178-
chksquare(A)
178+
checksquare(A)
179179
v, X = eig(A)
180180
any(v.<0) && (v = complex(v))
181181
Xinv = ishermitian(A) ? X' : inv(X)
@@ -190,7 +190,7 @@ expm(x::Number) = exp(x)
190190
## Destructive matrix exponential using algorithm from Higham, 2008,
191191
## "Functions of Matrices: Theory and Computation", SIAM
192192
function expm!{T<:BlasFloat}(A::StridedMatrix{T})
193-
n = chksquare(A)
193+
n = checksquare(A)
194194
if ishermitian(A)
195195
return full(expm(Hermitian(A)))
196196
end
@@ -308,7 +308,7 @@ function logm(A::StridedMatrix)
308308
end
309309

310310
# Use Schur decomposition
311-
n = chksquare(A)
311+
n = checksquare(A)
312312
if istriu(A)
313313
retmat = full(logm(UpperTriangular(complex(A))))
314314
d = diag(A)
@@ -343,7 +343,7 @@ function sqrtm{T<:Real}(A::StridedMatrix{T})
343343
if issym(A)
344344
return full(sqrtm(Symmetric(A)))
345345
end
346-
n = chksquare(A)
346+
n = checksquare(A)
347347
if istriu(A)
348348
return full(sqrtm(UpperTriangular(A)))
349349
else
@@ -356,7 +356,7 @@ function sqrtm{T<:Complex}(A::StridedMatrix{T})
356356
if ishermitian(A)
357357
return full(sqrtm(Hermitian(A)))
358358
end
359-
n = chksquare(A)
359+
n = checksquare(A)
360360
if istriu(A)
361361
return full(sqrtm(UpperTriangular(A)))
362362
else
@@ -511,7 +511,7 @@ function cond(A::AbstractMatrix, p::Real=2)
511511
maxv = maximum(v)
512512
return maxv == 0.0 ? oftype(real(A[1,1]),Inf) : maxv / minimum(v)
513513
elseif p == 1 || p == Inf
514-
chksquare(A)
514+
checksquare(A)
515515
return cond(lufact(A), p)
516516
end
517517
throw(ArgumentError("p-norm must be 1, 2 or Inf, got $p"))

base/linalg/generic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ end
334334
rank(x::Number) = x==0 ? 0 : 1
335335

336336
function trace(A::AbstractMatrix)
337-
chksquare(A)
337+
checksquare(A)
338338
sum(diag(A))
339339
end
340340
trace(x::Number) = x
@@ -347,7 +347,7 @@ trace(x::Number) = x
347347
inv(a::StridedMatrix) = throw(ArgumentError("argument must be a square matrix"))
348348
function inv{T}(A::AbstractMatrix{T})
349349
S = typeof(zero(T)/one(T))
350-
A_ldiv_B!(factorize(convert(AbstractMatrix{S}, A)), eye(S, chksquare(A)))
350+
A_ldiv_B!(factorize(convert(AbstractMatrix{S}, A)), eye(S, checksquare(A)))
351351
end
352352

353353
function (\)(A::AbstractMatrix, B::AbstractVecOrMat)

0 commit comments

Comments
 (0)