Skip to content

Commit 6d3063d

Browse files
author
maechler
committed
isSymmetric(<matrix>) gains optional trans = "C"
git-svn-id: https://svn.r-project.org/R/trunk@88000 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent ceae659 commit 6d3063d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

doc/NEWS.Rd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,18 @@
236236

237237
\item New datasets \code{penguins} and \code{penguins_raw} thanks to
238238
\I{Ella Kaye}, \I{Heather Turner}, and \I{Kristen Gorman}.
239+
240+
\item \code{isSymmetric(<matrix>)} gains a new option \code{trans = "C"};
241+
when set to non-default, it tests for \dQuote{simple} symmetry of
242+
complex matrices.
239243
}
240244
}
241245

242246
\subsection{BLAS and LAPACK}{
243247
\itemize{
244248
\item The bundled BLAS and LAPACK sources have been updated to
245249
those shipped as part of January 2025's LAPACK 3.12.1.
246-
250+
247251
\item It is intended that this will be the last update to BLAS and
248252
LAPACK in the \R sources. Those building \R from source are
249253
encouraged to use external BLAS and LAPACK and this will be required in
@@ -270,7 +274,7 @@
270274
\item Windows builds currently use the internal LAPACK and by
271275
default the internal BLAS: notes on how to swap the latter
272276
\emph{via} \file{Rblas.dll} are in file
273-
\file{src/extra/blas/Makefile.win}.
277+
\file{src/extra/blas/Makefile.win}.
274278
}
275279
}
276280

src/library/base/R/eigen.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/base/R/eigen.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2019 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -19,13 +19,14 @@
1919

2020
isSymmetric <- function(object, ...) UseMethod("isSymmetric")
2121

22-
isSymmetric.matrix <- function(object, tol = 100*.Machine$double.eps, tol1 = 8*tol, ...)
22+
isSymmetric.matrix <- function(object, tol = 100*.Machine$double.eps,
23+
tol1 = 8*tol, trans = "C", ...)
2324
{
2425
if(!is.matrix(object)) return(FALSE) ## we test for symmetric *matrix*
2526
## cheap pretest: is it square?
2627
d <- dim(object)
2728
if((n <- d[1L]) != d[2L]) return(FALSE)
28-
iCplx <- is.complex(object)
29+
iCplx <- is.complex(object) && trans == "C"
2930
if(n > 1L && length(tol1)) {
3031
## initial pre-tests, fast for large non-symmetric:
3132
Cj <- if(iCplx) Conj else identity
@@ -36,7 +37,7 @@ isSymmetric.matrix <- function(object, tol = 100*.Machine$double.eps, tol1 = 8*t
3637
test <-
3738
if(iCplx)
3839
all.equal.numeric(object, Conj(t(object)), tolerance = tol, ...)
39-
else # numeric, character, ..
40+
else # numeric, character, {complex, trans != "C"}, ..
4041
all.equal(object, t(object), tolerance = tol, ...)
4142
isTRUE(test)
4243
}

src/library/base/man/isSymmetric.Rd

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/isSymmetric.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2018 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{isSymmetric}
@@ -11,12 +11,14 @@
1111
Generic function to test if \code{object} is symmetric or not.
1212
Currently only a matrix method is implemented, where a
1313
\code{\link{complex}} matrix \code{Z} must be \dQuote{Hermitian} for
14-
\code{isSymmetric(Z)} to be true.
14+
\code{isSymmetric(Z)} to be true, and
15+
(since \R >= 4.5.0),
16+
\code{isSymmetric(Z, trans = "T")} checks for \dQuote{simple} symmetry.
1517
}
1618
\usage{
1719
isSymmetric(object, \dots)
1820
\method{isSymmetric}{matrix}(object, tol = 100 * .Machine$double.eps,
19-
tol1 = 8 * tol, \dots)
21+
tol1 = 8 * tol, trans = "C", \dots)
2022
}
2123
\arguments{
2224
\item{object}{any \R object; a \code{\link{matrix}} for the matrix method.}
@@ -26,6 +28,11 @@ isSymmetric(object, \dots)
2628
\sQuote{pre-tests} the first and last few rows for fast detection of
2729
\sQuote{obviously} asymmetric cases with this tolerance. Setting it
2830
to length zero will skip the pre-tests.}
31+
\item{trans}{a single \code{\link{character}}, only relevant for a
32+
\code{\link{complex}} matrix \code{Z}: if it is \code{"C"} (as by
33+
default), \code{Conj(t(Z))} must be the same as \code{Z} whereas
34+
otherwise (typically it is \code{"T"}) \code{t(Z)} must equal
35+
\code{Z}. The argument name is inherited from LAPACK.}
2936
\item{\dots}{further arguments passed to methods; the matrix method
3037
passes these to \code{\link{all.equal}}. If the row and column
3138
names of \code{object} are allowed to differ for the symmetry check
@@ -55,8 +62,9 @@ isSymmetric(D3) # TRUE
5562
isSymmetric(D3, tol = 0) # FALSE for zero-tolerance
5663

5764
## Complex Matrices - Hermitian or not
58-
Z <- sqrt(matrix(-1:2 + 0i, 2)); Z <- t(Conj(Z)) \%*\% Z
59-
Z
65+
z <- sqrt(matrix(-1:2 + 0i, 2)); Z <- t(Conj(z)) \%*\% z
66+
ZtZ <- t(z) \%*\% z
67+
Z ; ZtZ
6068
isSymmetric(Z) # TRUE
6169
isSymmetric(Z + 1) # TRUE
6270
isSymmetric(Z + 1i) # FALSE -- a Hermitian matrix has a *real* diagonal

0 commit comments

Comments
 (0)