added Frobenius matrix norm

JIRA: MATH-232

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@724144 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-12-07 15:36:32 +00:00
parent 2dc7b2d460
commit 227b7b2a00
4 changed files with 33 additions and 0 deletions

View File

@ -216,6 +216,20 @@ public abstract class AbstractRealMatrix implements RealMatrix, Serializable {
return maxColSum;
}
/** {@inheritDoc} */
public double getFrobeniusNorm() {
final int rowCount = getRowDimension();
final int columnCount = getColumnDimension();
double sum2 = 0;
for (int col = 0; col < columnCount; ++col) {
for (int row = 0; row < rowCount; ++row) {
final double mij = getEntry(row, col);
sum2 += mij * mij;
}
}
return Math.sqrt(sum2);
}
/** {@inheritDoc} */
public RealMatrix getSubMatrix(final int startRow, final int endRow,
final int startColumn, final int endColumn)

View File

@ -113,6 +113,14 @@ public interface RealMatrix {
*/
double getNorm();
/**
* Returns the <a href="http://mathworld.wolfram.com/FrobeniusNorm.html">
* Frobenius norm</a> of the matrix.
*
* @return norm
*/
double getFrobeniusNorm();
/**
* Gets a submatrix. Rows and columns are indicated
* counting from 0 to n-1.

View File

@ -39,6 +39,9 @@ The <action> type attribute can be add,update,fix,remove.
</properties>
<body>
<release version="2.0" date="TBD" description="TBD">
<action dev="luc" type="add" issue="MATH-232" >
Added Frobenius matrix norm.
</action>
<action dev="luc" type="add" issue="MATH-231" >
Added an abstract matrix implementation simplifying extension by users.
</action>

View File

@ -159,6 +159,14 @@ public final class RealMatrixImplTest extends TestCase {
assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
}
/** test Frobenius norm */
public void testFrobeniusNorm() {
RealMatrixImpl m = new RealMatrixImpl(testData);
RealMatrixImpl m2 = new RealMatrixImpl(testData2);
assertEquals("testData Frobenius norm", Math.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
assertEquals("testData2 Frobenius norm", Math.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
}
/** test m-n = m + -n */
public void testPlusMinus() {
RealMatrixImpl m = new RealMatrixImpl(testData);