lcsim/src/org/lcsim/recon/emid/hmatrix
diff -u -r1.5 -r1.6
--- HMatrix.java 22 Jul 2006 18:39:24 -0000 1.5
+++ HMatrix.java 5 Jun 2008 17:41:50 -0000 1.6
@@ -73,7 +73,7 @@
*plain ASCII format and Java serialized format.
*
*@author Norman A. Graf
- *@version $Id: HMatrix.java,v 1.5 2006/07/22 18:39:24 proulx Exp $
+ *@version $Id: HMatrix.java,v 1.6 2008/06/05 17:41:50 ngraf Exp $
*/
public class HMatrix implements Serializable
{
@@ -86,7 +86,8 @@
private double[] _tmp2; // temporary array
- public HMatrix(){}
+ public HMatrix()
+ {}
/**
* Constructor
*
@@ -122,19 +123,19 @@
try
{
BufferedReader bufferedreader
- = new BufferedReader(new InputStreamReader(in));
+ = new BufferedReader(new InputStreamReader(in));
create(bufferedreader);
bufferedreader.close();
-
+
}
catch(IOException _ex)
{
System.err.println("HMatrixBuilder::read -> Error reading HMatrix from input reader.");
System.exit(0);
}
- }
+ }
}
-
+
/**
* Calculates the chi-squared for the measurement compared to
@@ -174,9 +175,9 @@
* @return The value of the diagonal chi-squared.
*/
public double chisquaredDiagonal(double[] dat)
- {
+ {
double chisqr = 0.;
- // diagonal terms of covariance matrix times difference vector
+ // diagonal terms of covariance matrix times difference vector
for(int i=0; i<_dim; ++i)
{
//measured-predicted values
@@ -185,8 +186,8 @@
chisqr += _tmp[i]*_tmp[i]/_cov[i];
}
return chisqr;
- }
-
+ }
+
/**
*Output stream
*
@@ -212,63 +213,96 @@
return sb.toString();
}
+// /**
+// * Writes the HMatrix to a Serialized file
+// * @return
+// */
+//
+// public void writeSerialized(String filename)
+// {
+//
+// try
+// {
+// FileOutputStream fos = new FileOutputStream(filename);
+// ObjectOutputStream objOs = new ObjectOutputStream(fos);
+// objOs.writeObject(this);
+// objOs.flush();
+// fos.close();
+// } catch (NotSerializableException se)
+// {
+// System.err.println(se);
+// } catch (FileNotFoundException fe)
+// {
+// System.err.println(fe);
+// } catch (IOException se)
+// {
+// System.err.println(se);
+// }
+//
+// }
+
+
+// /**
+// * Reads the HMatrix from a Serialized file
+// * @return
+// */
+//
+// public static HMatrix readSerialized(String filename)
+// {
+// HMatrix hm = null;
+//
+// try
+// {
+// FileInputStream fis = new FileInputStream(filename);
+// ObjectInputStream objIs = new ObjectInputStream(fis);
+// hm = (HMatrix)objIs.readObject();
+// fis.close();
+// } catch (NotSerializableException se)
+// {
+// System.err.println(se);
+// } catch (FileNotFoundException fe)
+// {
+// System.err.println(fe);
+// } catch (IOException se)
+// {
+// System.err.println(se);
+// } catch (ClassNotFoundException ce)
+// {
+// System.err.println(ce);
+// }
+// return hm;
+// }
+
+
/**
- * Writes the HMatrix to a Serialized file
+ * Return the vector of averages
+ * @return the measurement vector averages
*/
-/*
- public void writeSerialized(String filename)
+ public double[] averageVector()
{
-
- try
- {
- FileOutputStream fos = new FileOutputStream(filename);
- ObjectOutputStream objOs = new ObjectOutputStream(fos);
- objOs.writeObject(this);
- objOs.flush();
- fos.close();
- } catch (NotSerializableException se)
- {
- System.err.println(se);
- } catch (FileNotFoundException fe)
- {
- System.err.println(fe);
- } catch (IOException se)
- {
- System.err.println(se);
- }
-
+ double[] a = new double[_dim];
+ System.arraycopy(_vec, 0, a, 0, _dim);
+ return a;
}
-*/
+
/**
- * Reads the HMatrix from a Serialized file
+ * Return the inverse covariance matrix packed in lower-diagonal form
+ * @return the inverse covariance matrix packed in lower-diagonal form
*/
- /*
- public static HMatrix readSerialized(String filename)
+ public double[] packedInverseCovarianceMatrix()
{
- HMatrix hm = null;
-
- try
- {
- FileInputStream fis = new FileInputStream(filename);
- ObjectInputStream objIs = new ObjectInputStream(fis);
- hm = (HMatrix)objIs.readObject();
- fis.close();
- } catch (NotSerializableException se)
- {
- System.err.println(se);
- } catch (FileNotFoundException fe)
- {
- System.err.println(fe);
- } catch (IOException se)
- {
- System.err.println(se);
- } catch (ClassNotFoundException ce)
+ int dim = (_dim*(_dim+1))/2;
+ double[] a = new double[dim];
+ int counter = 0;
+ for(int i=0; i<_dim; ++i)
{
- System.err.println(ce);
+ for(int j=0; j<i+1; ++j)
+ {
+ a[counter++] = _invcov[i][j];
+ }
}
- return hm;
+ return a;
}
-*/
/**
* Writes out the HMatrix to an ASCII file
*/
@@ -297,7 +331,7 @@
for(int i = 0; i < _dim; i++) s5 = s5 + _cov[i] + " ";
printwriter.println(s5.substring(0, s5.length() - 1));
}
-
+
//Now the covariance matrix
for(int i = 0; i < _dim; i++)
{
@@ -478,21 +512,21 @@
public static HMatrix create(InputStream in)
{
-
- try
- {
- BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(in));
- HMatrix hm = create(bufferedreader);
- bufferedreader.close();
- return hm;
- }
- catch(IOException _ex)
- {
- System.err.println("HMatrixBuilder::read -> Error reading HMatrix from input reader.");
- System.exit(0);
- }
- return null;
- }
+
+ try
+ {
+ BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(in));
+ HMatrix hm = create(bufferedreader);
+ bufferedreader.close();
+ return hm;
+ }
+ catch(IOException _ex)
+ {
+ System.err.println("HMatrixBuilder::read -> Error reading HMatrix from input reader.");
+ System.exit(0);
+ }
+ return null;
+ }
@@ -515,65 +549,65 @@
else
if(!flag)
{
- dim = Integer.parseInt(s1.substring(0, s1.indexOf(" ")));
- key = Integer.parseInt(s1.substring(s1.indexOf(" ") + 1, s1.length()));
- vec = new double[dim];
- covDiag = new double[dim];
- cov = new double[dim][dim];
- flag = true;
+ dim = Integer.parseInt(s1.substring(0, s1.indexOf(" ")));
+ key = Integer.parseInt(s1.substring(s1.indexOf(" ") + 1, s1.length()));
+ vec = new double[dim];
+ covDiag = new double[dim];
+ cov = new double[dim][dim];
+ flag = true;
}
else
{
- if (!readVec)
- {
+ if (!readVec)
+ {
// System.out.println("read vec");
// System.out.println(s1);
- // Read the vector of averages
- while(s1.length() > 0 && s1.indexOf(" ") != -1)
- {
- double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
- s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
- vec[ii] = d;
- ii++;
- }
- double d1 = Double.valueOf(s1).doubleValue();
- vec[ii] = d1;
- readVec = true;
- ii=0;
- }
- else if(!readCovDiag)
- {
+ // Read the vector of averages
+ while(s1.length() > 0 && s1.indexOf(" ") != -1)
+ {
+ double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
+ s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
+ vec[ii] = d;
+ ii++;
+ }
+ double d1 = Double.valueOf(s1).doubleValue();
+ vec[ii] = d1;
+ readVec = true;
+ ii=0;
+ }
+ else if(!readCovDiag)
+ {
// System.out.println("read covDiag");
// System.out.println(s1);
- // Read the vector of variance of averages
- while(s1.length() > 0 && s1.indexOf(" ") != -1)
- {
- double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
- s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
- covDiag[ii] = d;
- ii++;
- }
- double d1 = Double.valueOf(s1).doubleValue();
- covDiag[ii] = d1;
- readCovDiag = true;
- }
- else
- {
+ // Read the vector of variance of averages
+ while(s1.length() > 0 && s1.indexOf(" ") != -1)
+ {
+ double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
+ s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
+ covDiag[ii] = d;
+ ii++;
+ }
+ double d1 = Double.valueOf(s1).doubleValue();
+ covDiag[ii] = d1;
+ readCovDiag = true;
+ }
+ else
+ {
// System.out.println("read cov");
// System.out.println(s1);
- // Read the covariance matrix
- while(s1.length() > 0 && s1.indexOf(" ") != -1)
- {
- double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
- s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
- cov[i][j] = d;
- j++;
- }
- double d1 = Double.valueOf(s1).doubleValue();
- cov[i][j] = d1;
- i++;
- j = 0;
- }
+ // Read the covariance matrix
+ while(s1.length() > 0 && s1.indexOf(" ") != -1)
+ {
+ double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
+ s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
+ cov[i][j] = d;
+ j++;
+ }
+ double d1 = Double.valueOf(s1).doubleValue();
+ cov[i][j] = d1;
+ i++;
+ j = 0;
+ }
}
return new HMatrix(dim, key, vec, covDiag, cov);
}