Starting source code - basic matrix operations and univarient stats plus test code. Submitted by Phil Steitz.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140823 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
925847780e
commit
e4694325bc
|
@ -3,7 +3,7 @@
|
|||
|
||||
<!--
|
||||
"math" component of the Jakarta Commons Subproject
|
||||
$Id: build.xml,v 1.1 2003/05/12 15:07:41 rdonkin Exp $
|
||||
$Id: build.xml,v 1.2 2003/05/12 19:02:11 rdonkin Exp $
|
||||
-->
|
||||
|
||||
|
||||
|
@ -232,7 +232,7 @@
|
|||
<echo message="Running math tests ..."/>
|
||||
<java classname="${test.runner}" fork="yes"
|
||||
failonerror="${test.failonerror}">
|
||||
<arg value="org.apache.commons.math.SomeTestCase"/>
|
||||
<arg value="org.apache.commons.math.MathTestSuite"/>
|
||||
<classpath refid="test.classpath"/>
|
||||
</java>
|
||||
</target>
|
||||
|
|
|
@ -0,0 +1,179 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* Maintains a frequency distribution. <br>
|
||||
* Accepts int, long or string values, converting
|
||||
* all to Strings and maintaining frequency counts.
|
||||
*
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
|
||||
public class Freq {
|
||||
|
||||
private String name;
|
||||
|
||||
private Hashtable freqTable;
|
||||
|
||||
/** instance initializer */
|
||||
{
|
||||
freqTable = new Hashtable();
|
||||
}
|
||||
|
||||
public Freq() {
|
||||
}
|
||||
|
||||
public Freq(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer outBuffer = new StringBuffer();
|
||||
outBuffer.append("Value \t Frequency \n");
|
||||
Enumeration e = freqTable.keys();
|
||||
Long count = null;
|
||||
String value = null;
|
||||
while (e.hasMoreElements()) {
|
||||
value = (String)e.nextElement();
|
||||
count = (Long)freqTable.get(value);
|
||||
outBuffer.append(value);
|
||||
outBuffer.append("\t");
|
||||
outBuffer.append(count.toString());
|
||||
outBuffer.append("\n");
|
||||
}
|
||||
return outBuffer.toString();
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Adds 1 to the frequency count for v */
|
||||
public void addValue(java.lang.String v) {
|
||||
insertValue(v);
|
||||
}
|
||||
|
||||
/** Adds 1 to the frequency count for v */
|
||||
public void addValue(int v) {
|
||||
insertValue((new Integer(v)).toString());
|
||||
}
|
||||
|
||||
/** Adds 1 to the frequency count for v */
|
||||
public void addValue(long v) {
|
||||
insertValue((new Long(v)).toString());
|
||||
}
|
||||
|
||||
/** Returns the number of values = v */
|
||||
public long getCount(String v) {
|
||||
Long ct = (Long)freqTable.get(v);
|
||||
if (ct == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return ct.longValue();
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the sum of all frequencies */
|
||||
public long getSumFreq() {
|
||||
Enumeration e = freqTable.keys();
|
||||
long count = 0;
|
||||
String value = null;
|
||||
while (e.hasMoreElements()) {
|
||||
value = (String)e.nextElement();
|
||||
count += ((Long)freqTable.get(value)).longValue();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/** Returns the percentage of values = v */
|
||||
public double getPct(String v) {
|
||||
return (new Double(getCount(v))).doubleValue()
|
||||
/(new Double(getSumFreq())).doubleValue();
|
||||
}
|
||||
|
||||
/** Clears the frequency table */
|
||||
public void clear() {
|
||||
freqTable.clear();
|
||||
}
|
||||
|
||||
/** Adds 1 to the frequency count for v */
|
||||
private void insertValue(String v) {
|
||||
Long ct = (Long)freqTable.get(v);
|
||||
if (ct == null) {
|
||||
Long val = new Long(1);
|
||||
freqTable.put(v,val);
|
||||
} else {
|
||||
freqTable.put(v,(new Long(ct.longValue()+1)));
|
||||
}
|
||||
}
|
||||
|
||||
/** Getter for property name.
|
||||
* @return Value of property name.
|
||||
*/
|
||||
public java.lang.String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/** Setter for property name.
|
||||
* @param name New value of property name.
|
||||
*/
|
||||
public void setName(java.lang.String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,226 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math;
|
||||
|
||||
/**
|
||||
* Interface defining a real-valued matrix with basic algebraic operations
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:03:41 $
|
||||
*/
|
||||
public interface RealMatrix {
|
||||
|
||||
public RealMatrix copy();
|
||||
|
||||
/**
|
||||
* Compute the sum of *this and m
|
||||
* @param m matrix to be added
|
||||
* @return this + m
|
||||
* @exception IllegalArgumentException if m is not the same size as *this
|
||||
*/
|
||||
public RealMatrix add(RealMatrix m);
|
||||
|
||||
/**
|
||||
* Compute *this minus m
|
||||
* @param m matrix to be subtracted
|
||||
* @return this + m
|
||||
* @exception IllegalArgumentException if m is not the same size as *this
|
||||
*/
|
||||
public RealMatrix subtract(RealMatrix m);
|
||||
|
||||
/**
|
||||
* Returns the rank of the matrix
|
||||
* @return the rank of this matrix
|
||||
*/
|
||||
public int getRank();
|
||||
|
||||
/**
|
||||
* Returns the result of adding d to each entry of *this
|
||||
* @param d value to be added to each entry
|
||||
* @return d + this
|
||||
*/
|
||||
public RealMatrix scalarAdd(double d);
|
||||
|
||||
/**
|
||||
* Returns the result multiplying each entry of *this by d
|
||||
* @param d value to multiply all entries by
|
||||
* @return d*this
|
||||
*/
|
||||
public RealMatrix scalarMultiply(double d);
|
||||
|
||||
/**
|
||||
* Returns the result postmultiplyin *this by m
|
||||
* @param m matrix to postmultiply by
|
||||
* @return this*m
|
||||
* @throws IllegalArgumentException
|
||||
* if columnDimension(this) != rowDimension(m)
|
||||
*/
|
||||
public RealMatrix multiply(RealMatrix m);
|
||||
|
||||
/**
|
||||
* Returns matrix entries as a two-dimensional array
|
||||
* @return 2-dimensional array of entries
|
||||
*/
|
||||
public double[][] getData();
|
||||
|
||||
/**
|
||||
* Sets/overwrites the underlying data for the matrix
|
||||
* @param 2-dimensional array of entries
|
||||
*/
|
||||
public void setData(double[][] data);
|
||||
|
||||
/**
|
||||
* Returns the norm of the matrix
|
||||
* @return norm
|
||||
*/
|
||||
public double getNorm();
|
||||
|
||||
/**
|
||||
* Returns entries in row as an array
|
||||
* @param row the row to be fetched
|
||||
* @return array of entries in the row
|
||||
* @throws IllegalArgumentException if row > rowDimension
|
||||
*/
|
||||
public double[] getRow(int row);
|
||||
|
||||
/**
|
||||
* Returns entries in column as an array
|
||||
* @param col column to fetch
|
||||
* @return array of entries in the column
|
||||
* @throws IllegalArgumentException if column > columnDimension
|
||||
*/
|
||||
public double[] getColumn(int col);
|
||||
|
||||
/**
|
||||
* Returns the entry in the specified row and column
|
||||
* @param row row location of entry to be fetched
|
||||
* @param col column location of entry to be fetched
|
||||
* @return matrix entry in row,column
|
||||
* @throws IllegalArgumentException if entry does not exist
|
||||
*/
|
||||
public double getEntry(int row, int column);
|
||||
|
||||
/**
|
||||
* Sets the entry in the specified row and column to the specified value
|
||||
* @param row row location of entry to be set
|
||||
* @param col column location of entry to be set
|
||||
* @param value value to set
|
||||
* @throws IllegalArgumentException if entry does not exist
|
||||
*/
|
||||
public void setEntry(int row, int column, double value);
|
||||
|
||||
/**
|
||||
* Returns the transpose of this matrix
|
||||
* @return transpose matrix
|
||||
*/
|
||||
public RealMatrix transpose();
|
||||
|
||||
/**
|
||||
* Returns the inverse of this matrix
|
||||
* @return inverse matrix
|
||||
* @throws IllegalArgumentException if *this is not invertible
|
||||
*/
|
||||
public RealMatrix inverse();
|
||||
|
||||
/**
|
||||
* Returns the determinant of this matrix
|
||||
* @returns determinant
|
||||
*/
|
||||
public double getDeterminant();
|
||||
|
||||
/**
|
||||
* Is this a square matrix?
|
||||
* @return true if the matrix is square (rowDimension = columnDimension)
|
||||
*/
|
||||
public boolean isSquare();
|
||||
|
||||
/**
|
||||
* Is this a singular matrix?
|
||||
* @return true if the matrix is singular
|
||||
*/
|
||||
public boolean isSingular();
|
||||
|
||||
/**
|
||||
* Returns the number of rows in the matrix
|
||||
* @return rowDimension
|
||||
*/
|
||||
public int getRowDimension();
|
||||
|
||||
/**
|
||||
* Returns the number of columns in the matrix
|
||||
* @return columnDimension
|
||||
*/
|
||||
public int getColumnDimension();
|
||||
|
||||
/**
|
||||
* Returns the trace of the matrix
|
||||
* @return trace
|
||||
*/
|
||||
public double getTrace();
|
||||
|
||||
/**
|
||||
* Returns the result of multiplying this by vector v
|
||||
* @return this*v
|
||||
* @throws IllegalArgumentException if columnDimension != v.size()
|
||||
*/
|
||||
public double[] operate(double[] v);
|
||||
|
||||
/**
|
||||
* Returns the result of premultiplying this by vector v
|
||||
* @return v*this
|
||||
* @throws IllegalArgumentException if rowDimension != v.size()
|
||||
*/
|
||||
public RealMatrix preMultiply(double[] v);
|
||||
}
|
||||
|
|
@ -0,0 +1,377 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math;
|
||||
|
||||
/**
|
||||
* Implementation for RealMatrix using double[][] array
|
||||
* @author Phil Stetiz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:04:10 $
|
||||
*/
|
||||
public class RealMatrixImpl implements RealMatrix {
|
||||
|
||||
private double data[][];
|
||||
|
||||
public RealMatrixImpl() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RealMatrix with the supplied row and column dimensions
|
||||
* @param rowDimension the number of rows in the new matrix
|
||||
* @param columnDimension the number of columns in the new matrix
|
||||
* @return newly created matrix
|
||||
*/
|
||||
public RealMatrixImpl(int rowDimension,
|
||||
int columnDimension) {
|
||||
data = new double[rowDimension][columnDimension];
|
||||
}
|
||||
|
||||
public RealMatrixImpl(double[][] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new RealMatrix which is a copy of *this
|
||||
* @return the cloned matrix
|
||||
*/
|
||||
public RealMatrix copy() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the sum of *this and m
|
||||
* @param m matrix to be added
|
||||
* @return this + m
|
||||
* @exception IllegalArgumentException if m is not the same size as *this
|
||||
*/
|
||||
public RealMatrix add(RealMatrix m) {
|
||||
if (this.getColumnDimension() != m.getColumnDimension() ||
|
||||
this.getRowDimension() != m.getRowDimension()) {
|
||||
throw new IllegalArgumentException("matrix dimension mismatch");
|
||||
}
|
||||
int rowCount = this.getRowDimension();
|
||||
int columnCount = this.getColumnDimension();
|
||||
double[][] outData = new double[rowCount][columnCount];
|
||||
double[][] mData = m.getData();
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
for (int col = 0; col < columnCount; col++) {
|
||||
outData[row][col] = data[row][col] + mData[row][col];
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(outData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute *this minus m
|
||||
* @param m matrix to be subtracted
|
||||
* @return this + m
|
||||
* @exception IllegalArgumentException if m is not the same size as *this
|
||||
*/
|
||||
public RealMatrix subtract(RealMatrix m) {
|
||||
if (this.getColumnDimension() != m.getColumnDimension() ||
|
||||
this.getRowDimension() != m.getRowDimension()) {
|
||||
throw new IllegalArgumentException("matrix dimension mismatch");
|
||||
}
|
||||
int rowCount = this.getRowDimension();
|
||||
int columnCount = this.getColumnDimension();
|
||||
double[][] outData = new double[rowCount][columnCount];
|
||||
double[][] mData = m.getData();
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
for (int col = 0; col < columnCount; col++) {
|
||||
outData[row][col] = data[row][col] - mData[row][col];
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(outData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the rank of the matrix
|
||||
* @return the rank of this matrix
|
||||
*/
|
||||
public int getRank() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the result of adding d to each entry of *this
|
||||
* @param d value to be added to each entry
|
||||
* @return d + this
|
||||
*/
|
||||
public RealMatrix scalarAdd(double d) {
|
||||
int rowCount = this.getRowDimension();
|
||||
int columnCount = this.getColumnDimension();
|
||||
double[][] outData = new double[rowCount][columnCount];
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
for (int col = 0; col < columnCount; col++) {
|
||||
outData[row][col] = data[row][col] + d;
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(outData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result multiplying each entry of *this by d
|
||||
* @param d value to multiply all entries by
|
||||
* @return d*this
|
||||
*/
|
||||
public RealMatrix scalarMultiply(double d) {
|
||||
int rowCount = this.getRowDimension();
|
||||
int columnCount = this.getColumnDimension();
|
||||
double[][] outData = new double[rowCount][columnCount];
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
for (int col = 0; col < columnCount; col++) {
|
||||
outData[row][col] = data[row][col]*d;
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(outData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result postmultiplying *this by m
|
||||
* @param m matrix to postmultiply by
|
||||
* @return this*m
|
||||
* @throws IllegalArgumentException
|
||||
* if columnDimension(this) != rowDimension(m)
|
||||
*/
|
||||
public RealMatrix multiply(RealMatrix m) {
|
||||
if (this.getColumnDimension() != m.getRowDimension()) {
|
||||
throw new IllegalArgumentException
|
||||
("Matrices are not multiplication compatible.");
|
||||
}
|
||||
double[][] mData = m.getData();
|
||||
double[][] outData =
|
||||
new double[this.getRowDimension()][m.getColumnDimension()];
|
||||
double sum = 0;
|
||||
for (int row = 0; row < this.getRowDimension(); row++) {
|
||||
for (int col = 0; col < m.getColumnDimension(); col++) {
|
||||
sum = 0;
|
||||
for (int i = 0; i < this.getColumnDimension(); i++) {
|
||||
sum += data[row][i] * mData[i][col];
|
||||
}
|
||||
outData[row][col] = sum;
|
||||
}
|
||||
}
|
||||
return new RealMatrixImpl(outData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns matrix entries as a two-dimensional array
|
||||
* @return 2-dimensional array of entries
|
||||
*/
|
||||
public double[][] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets/overwrites the underlying data for the matrix
|
||||
* @param 2-dimensional array of entries
|
||||
*/
|
||||
public void setData(double[][] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 1-norm of the matrix (max column sum)
|
||||
* @return norm
|
||||
*/
|
||||
public double getNorm() {
|
||||
double maxColSum = 0;
|
||||
for (int col = 0; col < this.getColumnDimension(); col++) {
|
||||
double sum = 0;
|
||||
for (int row = 0; row < this.getRowDimension(); row++) {
|
||||
sum += Math.abs(data[row][col]);
|
||||
}
|
||||
maxColSum = Math.max(maxColSum,sum);
|
||||
}
|
||||
return maxColSum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns entries in row as an array
|
||||
* @param row the row to be fetched
|
||||
* @return array of entries in the row
|
||||
* @throws IllegalArgumentException if row > rowDimension
|
||||
*/
|
||||
public double[] getRow(int row) {
|
||||
return data[row];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns entries in column as an array
|
||||
* @param col column to fetch
|
||||
* @return array of entries in the column
|
||||
* @throws IllegalArgumentException if column > columnDimension
|
||||
*/
|
||||
public double[] getColumn(int col) {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entry in the specified row and column
|
||||
* @param row row location of entry to be fetched
|
||||
* @param col column location of entry to be fetched
|
||||
* @return matrix entry in row,column
|
||||
* @throws IllegalArgumentException if entry does not exist
|
||||
*/
|
||||
public double getEntry(int row, int column) {
|
||||
if (row < 1 || column < 1 || row > this.getRowDimension()
|
||||
|| column > this.getColumnDimension()) {
|
||||
throw new IllegalArgumentException
|
||||
("matrix entry does not exist");
|
||||
}
|
||||
return data[row-1][column-1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry in the specified row and column to the specified value
|
||||
* @param row row location of entry to be set
|
||||
* @param col column location of entry to be set
|
||||
* @param value value to set
|
||||
* @throws IllegalArgumentException if entry does not exist
|
||||
*/
|
||||
public void setEntry(int row, int column, double value) {
|
||||
if (row < 1 || column < 1 || row > this.getRowDimension()
|
||||
|| column > this.getColumnDimension()) {
|
||||
throw new IllegalArgumentException
|
||||
("matrix entry does not exist");
|
||||
}
|
||||
data[row-1][column-1] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the transpose of this matrix
|
||||
* @return transpose matrix
|
||||
*/
|
||||
public RealMatrix transpose() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the inverse of this matrix
|
||||
* @return inverse matrix
|
||||
* @throws IllegalArgumentException if *this is not invertible
|
||||
*/
|
||||
public RealMatrix inverse() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the determinant of this matrix
|
||||
* @return determinant
|
||||
*/
|
||||
public double getDeterminant() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a square matrix?
|
||||
* @return true if the matrix is square (rowDimension = columnDimension)
|
||||
*/
|
||||
public boolean isSquare() {
|
||||
return (this.getColumnDimension() == this.getRowDimension());
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a singular matrix?
|
||||
* @return true if the matrix is singular
|
||||
*/
|
||||
public boolean isSingular() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rows in the matrix
|
||||
* @return rowDimension
|
||||
*/
|
||||
public int getRowDimension() {
|
||||
return data.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of columns in the matrix
|
||||
* @return columnDimension
|
||||
*/
|
||||
public int getColumnDimension() {
|
||||
return data[0].length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trace of the matrix
|
||||
* @return trace
|
||||
*/
|
||||
public double getTrace() {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of multiplying this by the vector b
|
||||
* @return this*v
|
||||
* @throws IllegalArgumentException if columnDimension != v.size()
|
||||
*/
|
||||
public double[] operate(double[] v) {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of premultiplying this by the vector v
|
||||
* @return v*this
|
||||
* @throws IllegalArgumentException if rowDimension != v.size()
|
||||
*/
|
||||
public RealMatrix preMultiply(double[] v) {
|
||||
throw new UnsupportedOperationException("not implemented yet");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,259 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
/**
|
||||
*
|
||||
* Accumulates univariate statistics for values fed in
|
||||
* through the addValue() method. Does not store raw data values.
|
||||
* All data (including n) are represented internally as doubles.
|
||||
* Integers, floats and longs can be added, but will be converted
|
||||
* to doubles by addValue().
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:04:10 $
|
||||
*
|
||||
*/
|
||||
public class Univariate {
|
||||
|
||||
/** running sum of values that have been added */
|
||||
private double sum = 0.0;
|
||||
|
||||
/** running sum of squares that have been added */
|
||||
private double sumsq = 0.0;
|
||||
|
||||
/** count of values that have been added */
|
||||
private double n = 0.0;
|
||||
|
||||
/** min of values that have been added */
|
||||
private double min = Double.MAX_VALUE;
|
||||
|
||||
/** max of values that have been added */
|
||||
private double max = Double.MIN_VALUE;
|
||||
|
||||
/** display name */
|
||||
private String name = "";
|
||||
|
||||
/** Creates new univariate */
|
||||
public Univariate() {
|
||||
clear();
|
||||
}
|
||||
|
||||
/** Creates a new univariate with the given name */
|
||||
public Univariate(java.lang.String name) {
|
||||
this.name = name;
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value, updating running sums.<br>
|
||||
* Converts value to a double before adding.
|
||||
* @param v the value to be added
|
||||
*/
|
||||
public void addValue(int v) {
|
||||
double f = (new Double(v)).doubleValue();
|
||||
insertValue(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value, updating running sums.<br>
|
||||
* Converts value to a double before adding.
|
||||
* @param v the value to be added
|
||||
*/
|
||||
public void addValue(long v) {
|
||||
double f = (new Double(v)).doubleValue();
|
||||
insertValue(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value, updating running sums.<br>
|
||||
* Converts value to a double before adding.
|
||||
* @param v the value to be added
|
||||
*/
|
||||
public void addValue(float v) {
|
||||
insertValue(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value, updating running sums.
|
||||
* @param v the value to be added
|
||||
*/
|
||||
public void addValue(double v) {
|
||||
insertValue(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mean of the values that have been added
|
||||
* @return mean value
|
||||
*/
|
||||
public double getMean() {
|
||||
// FIXME: throw something meaningful if n = 0
|
||||
return sum/n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the variance of the values that have been added
|
||||
* @return variance value
|
||||
*/
|
||||
public double getVariance() {
|
||||
double xbar = getMean();
|
||||
// FIXME: throw something meaningful if n = 0
|
||||
return (sumsq - xbar*xbar*n)/(n-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the standard deviation of the values that have been added
|
||||
* @return standard deviation value
|
||||
*/
|
||||
public double getStandardDeviation() {
|
||||
// FIXME: throw something meaningful if n = 0
|
||||
return (new Double(Math.sqrt
|
||||
((new Double(getVariance())).doubleValue()))).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the value, updating running sums.
|
||||
* @param v the value to be added
|
||||
*/
|
||||
private void insertValue(double v) {
|
||||
n += 1.0;
|
||||
if (v < min) min = v;
|
||||
if (v > max) max = v;
|
||||
sum += v;
|
||||
sumsq += v*v;
|
||||
}
|
||||
|
||||
/** Getter for property max.
|
||||
* @return Value of property max.
|
||||
*/
|
||||
public double getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/** Setter for property max.
|
||||
* @param max New value of property max.
|
||||
*/
|
||||
public void setMax(double max) {
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/** Getter for property min.
|
||||
* @return Value of property min.
|
||||
*/
|
||||
public double getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
/** Getter for property n.
|
||||
* @return Value of property n.
|
||||
*/
|
||||
public double getN() {
|
||||
return n;
|
||||
}
|
||||
|
||||
/** Getter for property sum.
|
||||
* @return Value of property sum.
|
||||
*/
|
||||
public double getSum() {
|
||||
return sum;
|
||||
}
|
||||
|
||||
/** Getter for property sumsq.
|
||||
* @return Value of property sumsq.
|
||||
*/
|
||||
public double getSumsq() {
|
||||
return sumsq;
|
||||
}
|
||||
|
||||
/** Getter for property name.
|
||||
* @return Value of property name.
|
||||
*/
|
||||
public java.lang.String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/** Setter for property name.
|
||||
* @param name New value of property name.
|
||||
*/
|
||||
public void setName(java.lang.String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a text report displaying
|
||||
* univariate statistics from values that
|
||||
* have been added.
|
||||
* @return String with line feeds displaying statistics
|
||||
*/
|
||||
public String toString() {
|
||||
StringBuffer outBuffer = new StringBuffer();
|
||||
outBuffer.append(name + "\n");
|
||||
outBuffer.append("n: " + n + "\n");
|
||||
outBuffer.append("min: " + min + "\n");
|
||||
outBuffer.append("max: " + max + "\n");
|
||||
outBuffer.append("mean: " + getMean() + "\n");
|
||||
outBuffer.append("std dev: " + getStandardDeviation() + "\n");
|
||||
return outBuffer.toString();
|
||||
}
|
||||
|
||||
/** Resets all sums to 0, resets min and max */
|
||||
public void clear() {
|
||||
this.sum = 0.0;
|
||||
this.sumsq = 0.0;
|
||||
this.n = 0.0;
|
||||
this.min = Double.MAX_VALUE;
|
||||
this.max = Double.MIN_VALUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test cases for the {@link Freq} class.
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:04:38 $
|
||||
*/
|
||||
|
||||
public final class FreqTest extends TestCase {
|
||||
private long oneL = 1;
|
||||
private long twoL = 2;
|
||||
private int oneI = 1;
|
||||
private int twoI = 2;
|
||||
private String oneS = "1";
|
||||
private String twoS = "2";
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
public FreqTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(FreqTest.class);
|
||||
suite.setName("Freq Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
/** test freq counts */
|
||||
public void testCounts() {
|
||||
Freq f = new Freq("test counts");
|
||||
assertEquals("total count",0,f.getSumFreq());
|
||||
f.addValue(oneL);
|
||||
f.addValue(twoL);
|
||||
f.addValue(oneS);
|
||||
f.addValue(oneI);
|
||||
assertEquals("one frequency count",3,f.getCount("1"));
|
||||
assertEquals("two frequency count",1,f.getCount("2"));
|
||||
assertEquals("foo frequency count",0,f.getCount("foo"));
|
||||
assertEquals("total count",4,f.getSumFreq());
|
||||
f.clear();
|
||||
assertEquals("total count",0,f.getSumFreq());
|
||||
}
|
||||
|
||||
/** test pcts */
|
||||
public void testPcts() {
|
||||
Freq f = new Freq("test counts");
|
||||
f.addValue(oneL);
|
||||
f.addValue(twoL);
|
||||
f.addValue(oneI);
|
||||
f.addValue(twoI);
|
||||
f.addValue("foo");
|
||||
f.addValue("foo");
|
||||
f.addValue("foo");
|
||||
f.addValue("foo");
|
||||
assertEquals("one pct",0.25,f.getPct("1"),tolerance);
|
||||
assertEquals("two pct",0.25,f.getPct("2"),tolerance);
|
||||
assertEquals("foo pct",0.5,f.getPct("foo"),tolerance);
|
||||
assertEquals("bar pct",0,f.getPct("bar"),tolerance);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.textui.TestRunner;
|
||||
/**
|
||||
* Test suite for the Math package.
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Id: MathTestSuite.java,v 1.1 2003/05/12 19:04:38 rdonkin Exp $
|
||||
*/
|
||||
public class MathTestSuite extends TestCase {
|
||||
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*/
|
||||
public MathTestSuite(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Command-line interface.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(suite());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the suite of tests
|
||||
*/
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.setName("Commons Math Tests");
|
||||
suite.addTest(RealMatrixImplTest.suite());
|
||||
suite.addTest(FreqTest.suite());
|
||||
suite.addTest(UnivariateTest.suite());
|
||||
return suite;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test cases for the {@link RealMatrixImpl} class.
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:02:53 $
|
||||
*/
|
||||
|
||||
public final class RealMatrixImplTest extends TestCase {
|
||||
|
||||
private double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
|
||||
private double[][] testDataInv =
|
||||
{ {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
|
||||
private double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
|
||||
private double[][] testDataPlusInv =
|
||||
{ {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
|
||||
private double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
|
||||
private double[] testVector = {1,2,3};
|
||||
private double entryTolerance = Math.pow(2,-64);
|
||||
private double normTolerance = Math.pow(2,-64);
|
||||
|
||||
public RealMatrixImplTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(RealMatrixImplTest.class);
|
||||
suite.setName("RealMatrixImpl Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
/** test dimensions */
|
||||
public void testDimensions() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl m2 = new RealMatrixImpl(testData2);
|
||||
assertEquals("testData row dimension",3,m.getRowDimension());
|
||||
assertEquals("testData column dimension",3,m.getColumnDimension());
|
||||
assertTrue("testData is square",m.isSquare());
|
||||
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
|
||||
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
|
||||
assertTrue("testData2 is not square",!m2.isSquare());
|
||||
}
|
||||
|
||||
/** test add */
|
||||
public void testAdd() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl mInv = new RealMatrixImpl(testDataInv);
|
||||
RealMatrixImpl mPlusMInv = (RealMatrixImpl)m.add(mInv);
|
||||
double[][] sumEntries = mPlusMInv.getData();
|
||||
for (int row = 0; row < m.getRowDimension(); row++) {
|
||||
for (int col = 0; col < m.getColumnDimension(); col++) {
|
||||
assertEquals("sum entry entry",
|
||||
testDataPlusInv[row][col],sumEntries[row][col],
|
||||
entryTolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** test add failure */
|
||||
public void testAddFail() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl m2 = new RealMatrixImpl(testData2);
|
||||
try {
|
||||
RealMatrixImpl mPlusMInv = (RealMatrixImpl)m.add(m2);
|
||||
fail("IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
/** test norm */
|
||||
public void testNorm() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl m2 = new RealMatrixImpl(testData2);
|
||||
assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
|
||||
assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
|
||||
}
|
||||
|
||||
/** test m-n = m + -n */
|
||||
public void testPlusMinus() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl m2 = new RealMatrixImpl(testDataInv);
|
||||
assertClose("m-n = m + -n",m.subtract(m2),
|
||||
m2.scalarMultiply(-1d).add(m),entryTolerance);
|
||||
}
|
||||
|
||||
/** test multiply */
|
||||
public void testMultiply() {
|
||||
RealMatrixImpl m = new RealMatrixImpl(testData);
|
||||
RealMatrixImpl mInv = new RealMatrixImpl(testDataInv);
|
||||
RealMatrixImpl identity = new RealMatrixImpl(id);
|
||||
RealMatrixImpl m2 = new RealMatrixImpl(testData2);
|
||||
assertClose("inverse multiply",m.multiply(mInv),
|
||||
identity,entryTolerance);
|
||||
assertClose("inverse multiply",mInv.multiply(m),
|
||||
identity,entryTolerance);
|
||||
assertClose("identity multiply",m.multiply(identity),
|
||||
m,entryTolerance);
|
||||
assertClose("identity multiply",identity.multiply(mInv),
|
||||
mInv,entryTolerance);
|
||||
assertClose("identity multiply",m2.multiply(identity),
|
||||
m2,entryTolerance);
|
||||
}
|
||||
|
||||
private void assertClose(String msg, RealMatrix m, RealMatrix n,
|
||||
double tolerance) {
|
||||
assertTrue(msg,m.subtract(n).getNorm() < tolerance);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/12 19:02:53 $
|
||||
*/
|
||||
|
||||
public final class UnivariateTest extends TestCase {
|
||||
private double one = 1;
|
||||
private float twoF = 2;
|
||||
private long twoL = 2;
|
||||
private int three = 3;
|
||||
private double mean = 2;
|
||||
private double sumSq = 18;
|
||||
private double sum = 8;
|
||||
private double var = 0.666666666666666666667;
|
||||
private double std = Math.sqrt(var);
|
||||
private double n = 4;
|
||||
private double min = 1;
|
||||
private double max = 3;
|
||||
private double tolerance = 10E-15;
|
||||
|
||||
public UnivariateTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(UnivariateTest.class);
|
||||
suite.setName("Freq Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
/** test stats */
|
||||
public void testStats() {
|
||||
Univariate u = new Univariate("test univariate");
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
u.addValue(one);
|
||||
u.addValue(twoF);
|
||||
u.addValue(twoL);
|
||||
u.addValue(three);
|
||||
assertEquals("N",n,u.getN(),tolerance);
|
||||
assertEquals("sum",sum,u.getSum(),tolerance);
|
||||
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
|
||||
assertEquals("var",var,u.getVariance(),tolerance);
|
||||
assertEquals("std",std,u.getStandardDeviation(),tolerance);
|
||||
assertEquals("mean",mean,u.getMean(),tolerance);
|
||||
assertEquals("min",min,u.getMin(),tolerance);
|
||||
assertEquals("max",max,u.getMax(),tolerance);
|
||||
u.clear();
|
||||
assertEquals("total count",0,u.getN(),tolerance);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue