swh:1:snp:d1587d616651317fdcebcbb237dce82c32266449
Tip revision: 60e3e5c657df01d9a041d00855064f7b4f851468 authored by Yohan Chalabi on 22 August 2012, 00:00:00 UTC
version 2160.83
version 2160.83
Tip revision: 60e3e5c
matrix-norm.R
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
################################################################################
# FUNCTION: DESCRIPTION:
# norm Returns the norm of a matrix
################################################################################
norm <-
function(x, p = 2)
{
# A function implemented by Diethelm Wuertz
# Description:
# Returns the spectral norm of a matrix
# Details:
# http://mathworld.wolfram.com/MatrixNorm.html:
# For p = 1
# The maximum absolute column sum norm |A|_1 is defined
# as the maximum of the sum of the absolute valued elements
# of columns of the matrix.
# For p = 2:
# The spectral |A|_2 norm is "the" of a matrix. This value
# is computed as the square root of the maximum eigenvalue
# of A^H A where A^H is the conjugate transpose.
# For p = Inf:
# The maximum absolute row sum norm |A|_inf is defined
# as the maximum of the sum of the absolute valued elements
# of rows of the matrix.
# FUNCTION:
# Compute Norm:
ans = NA
if (p == 1) {
x = abs(x)
ans = max(apply(x, 2, sum))
}
if (p == 2) {
ans = sqrt(max(eigen(t(x) %*% x)$values))
}
if (p == Inf) {
x = abs(x)
ans = max(apply(x, 1, sum))
}
if (is.na(ans)) stop("Invalid value for p")
# Return value:
ans
}
################################################################################