This is the sort of thing that defeats the purpose of having the RealMatrix package. Such computations could be used to abuse the contents of the internal data structure effectively breaking the original Matrix.

We need a more OO strategy for capturing the requirements of your example. I believe it should be Functional in nature (like Functors).

IE something like:

double[] robustMeans = ....

Matrix base = ...

Matrix diff = base.apply(
   new GroovyMatrixFunctor(){
        public double result(int x, int y, double original_value){
                return original_value - robustMeans[y];
        }
   });

Internally to RealMatrixImpl the functor would be handled like this:

public RealMatrix apply(GroovyMatrixFunctor functor){

   double[][] result = new double[cases][variables];

   for (int x = 0; x < cases; x++){
        for (int y = 0; y < variables; y++){
            result[x][y] = functor.result(x,y, data[x][y]);
        }
   }
   return new NonCopyingRealMatrixImpl(result);
}

Which would apply the "GroovyMatrixFunction" to all the internal values to produce a new Matrix returned to "diff". NonCopyingRealMatrixImpl would just use the results[][] directly instead of copying its contents.

The elegance of this is that you can apply calculations using the internal array contents directly without actually exposing those contents in a way that destroys their immutability.

We can basically rewrite all the various Matrix operations as Functors which get applied internally. In the Sparse Matrix case, all the same Functors could be reused, only the implementation of "RealMatrix.apply" would change.

-Mark

Kim van der Linde wrote:


Phil Steitz wrote:

"Spooky" -- delightful word choice ;-)  The getDataRef  method is
there to limit copy operations and to support hacking the underlying
array -- evil, hazardous, breaks encapsulation -- altogether
"spooky"; but users may want this in some cases.


I do:

Matrix diffMatrix = new Matrix(cases, variables);
double diffArray[][] = diffMatrix.getDataRef();
double baseArray[][] = baseMatrix.getDataRef();
//verbose(true, "Robust baseMatrix\n"+baseMatrix.toSquareString());
for (int x = 0; x < cases; x++)
{
  for (int y = 0; y < variables; y++)
  {
    diffArray[x][y] = baseArray[x][y] - robustMeans[y];
  }
}
//verbose(true, "Robust diffArray\n"+diffMatrix.toSquareString());
robustDist = Distances.mahalanobis(diffMatrix, robustCovar);
det = Math.sqrt(robustCovar.getDeterminant());

Just an example.......

Kim


-- Mark Diggory Software Developer Harvard MIT Data Center http://www.hmdc.harvard.edu

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to