commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math RealMatrixImpl.java Univariate.java
Date Mon, 12 May 2003 19:04:10 GMT
rdonkin     2003/05/12 12:04:10

  Added:       math/src/java/org/apache/commons/math RealMatrixImpl.java
                        Univariate.java
  Log:
  Starting source code - basic matrix operations and univarient stats plus test code. Submitted
by Phil Steitz.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/RealMatrixImpl.java
  
  Index: RealMatrixImpl.java
  ===================================================================
  /* ====================================================================
   * 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");
      }
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java
  
  Index: Univariate.java
  ===================================================================
  /* ====================================================================
   * 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;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message