commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kim van der Linde <...@kimvdlinde.com>
Subject Re: [math] API changes for RC2
Date Tue, 28 Sep 2004 15:31:14 GMT
Yup, see here under (although it is a bit long). What I do is wait with 
generating the matrix till it is needed, and I still do use the 
getData() method, but do not need the getDataRef() anymore. If you want 
to get rid of that too, I would be less happy with that.

This routine takles about 30 second on my computer, not that long, but I 
have to run it in a wider project several 100 times, which requires for 
me to keep the overhead low. For that reason, I would like to keep some 
direct access methods in the RealMatrix to the underlying data, but I 
think generally that access to the whole double[][] array should be 
sufficient.

btw Matrix is the derived RealMatrixImpl class I use.

Kim

Mark R. Diggory wrote:

> Can you give us an example of your solution?

The new code:

   Matrix diffMatrix = null;
   double diffArray[][] = new double[cases][variables];
   double baseArray[][] = baseMatrix.getData();
   bestDist = new double[noSelected];
   boolean isSingular = false;
   for (int i = 0; i < nrep; i++) //nrep = generally between 2000-3000
   {
    index = subset.next();
    subMatrix = baseMatrix.getSubMatrix(index, 0, variables - 1);
    transientCovar = SpecialMatrices.covar(subMatrix);
    transientMeans = subMatrix.columnMeans();
    try
    {
     transientCovarInv = transientCovar.inverse();
     for (int x = 0; x < cases; x++)
     {
      for (int y = 0; y < variables; y++)
      {
       diffArray[x][y] = baseArray[x][y] - transientMeans[y];
      }
     }
     // Non-squared MD's
     diffMatrix = new Matrix(diffArray);
     transientDist = Distances.mahalanobis(diffMatrix, transientCovar);
     if (transientDist!=null) // cathing negative MD's
     {
      medianDist = Median.get(transientDist); // Get median
      det = transientCovar.getDeterminant();
      volume = Math.sqrt(Math.pow(medianDist,2*variables)*det);

      if (volume < object)
      {
       bestMedianDist = medianDist;
       object = volume;
       for (int x = 0; x < noSelected; x++)
       {
        inbest[x] = index[x];
       }
       bestCovar = transientCovar.copy();
       bestMeans = subMatrix.columnMeans();
       bestDiffMatrix = diffMatrix.copy();
      }
     }
    }
    catch (InvalidMatrixException ime)
    {
     singularCount++;
    }
   }

The old code:

   Matrix diffMatrix = new Matrix(cases, variables);
   double diffArray[][] = diffMatrix.getDataRef();
   double baseArray[][] = baseMatrix.getDataRef();
   bestDist = new double[noSelected];
   boolean isSingular = false;
   for (int i = 0; i < nrep; i++)
   {
    index = subset.next();
    subMatrix = baseMatrix.getSubMatrix(index, 0, variables - 1);
    transientCovar = SpecialMatrices.covar(subMatrix);
    transientMeans = subMatrix.columnMeans();
    try
    {
     transientCovarInv = transientCovar.inverse();
     for (int x = 0; x < cases; x++)
     {
      for (int y = 0; y < variables; y++)
      {
       diffArray[x][y] = baseArray[x][y] - transientMeans[y];
      }
     }
     // Non-squared MD's
     transientDist = Distances.mahalanobis(diffMatrix, transientCovar);
     if (transientDist!=null)
     {
      medianDist = Median.get(transientDist); // Get median
      det = transientCovar.getDeterminant();
      volume = Math.sqrt(Math.pow(medianDist,2*variables)*det);
      if (volume < object)
      {
       bestMedianDist = medianDist;
       object = volume;
       for (int x = 0; x < noSelected; x++)
       {
        inbest[x] = index[x];
       }
       bestCovar = transientCovar.copy();
       bestMeans = subMatrix.columnMeans();
       bestDiffMatrix = diffMatrix.copy();
      }
     }
    }
    catch (InvalidMatrixException ime)
    {
     singularCount++;
    }
   }

-- 
http://www.kimvdlinde.com

---------------------------------------------------------------------
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