Implemented least-squares optimisation in ccAnalysis
[jalview.git] / src / jalview / math / Matrix.java
index 5eaa7c3..7870c14 100755 (executable)
@@ -191,7 +191,10 @@ public class Matrix implements MatrixI
          */
         for (int k = 0; k < in.width(); k++)
         {
-          tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
+         if (!Double.isNaN(in.getValue(i,k)) && !Double.isNaN(this.value[k][j]))
+         {
+            tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
+         }
         }
       }
     }
@@ -830,12 +833,24 @@ public class Matrix implements MatrixI
   }
 
   /**
+  * returns the matrix as a double[][] array
+  *
+  * @return
+  */
+  @Override
+  public double[][] asArray()
+  {
+    return value;
+  }
+
+  /**
    * Returns an array containing the values in the specified column
    * 
    * @param col
    * 
    * @return
    */
+  @Override
   public double[] getColumn(int col)
   {
     double[] out = new double[rows];
@@ -858,7 +873,7 @@ public class Matrix implements MatrixI
   @Override
   public void printD(PrintStream ps, String format)
   {
-    for (int j = 0; j < rows; j++)
+    for (int j = 0; j < d.length; j++)
     {
       Format.print(ps, format, d[j]);
     }
@@ -1000,6 +1015,26 @@ public class Matrix implements MatrixI
     }
   }
 
+  /**
+   * Add d to all entries of this matrix
+   * 
+   * @param d ~ value to add
+   */
+  @Override
+  public void add(double d)
+  {
+    for (double[] row : value)
+    {
+      if (row != null)
+      {
+        for (int i = 0; i < row.length; i++)
+        {
+          row[i] += d;
+        }
+      }
+    }
+  }
+
   @Override
   public void setD(double[] v)
   {
@@ -1053,6 +1088,7 @@ public class Matrix implements MatrixI
   /**
    * Returns a copy in which  every value in the matrix is its absolute
    * 
+   * @return
    */
   @Override
   public MatrixI absolute()
@@ -1075,6 +1111,7 @@ public class Matrix implements MatrixI
   /**
    * Returns the mean of each row
    * 
+   * @return
    */
   @Override
   public double[] meanRow()
@@ -1094,6 +1131,7 @@ public class Matrix implements MatrixI
   /**
    * Returns the mean of each column
    * 
+   * @return
    */
   @Override
   public double[] meanCol()
@@ -1111,8 +1149,54 @@ public class Matrix implements MatrixI
   }
 
   /**
+  * return a flattened matrix containing the sum of each column
+  *
+  * @return
+  */
+  @Override
+  public double[] sumCol()
+  {
+    double[] sum = new double[cols];
+    for (int j = 0; j < cols; j++)
+    {
+      double[] column = getColumn(j);
+      if (column != null)
+      {
+       sum[j] = MiscMath.sum(column);
+      }
+    } 
+    return sum;
+  }
+
+  /**
+  * returns the mean value of the complete matrix
+  *
+  * @return
+  */
+  @Override
+  public double mean()
+  {
+    double sum = 0;
+    int nanCount = 0;
+    for (double[] row : value)
+    {
+      for (double col : row)
+      {
+       if (!Double.isNaN(col))
+       {
+         sum += col;
+       } else {
+         nanCount++;
+       }
+      }
+    }
+    return sum / (double) (this.rows * this.cols - nanCount);
+  }
+
+  /**
   * fills up a diagonal matrix with its transposed copy
   * !other side should be filled with 0
+  * !keeps Double.NaN found in either side
   *
   * TODO check on which side it was diagonal and only do calculations for the other side
   */
@@ -1121,8 +1205,8 @@ public class Matrix implements MatrixI
   {
     int n = this.rows;
     int m = this.cols;
-    MatrixI copy = this.transpose();
-    for (int i = 0; i < n; i++)
+    MatrixI copy = this.transpose();   // goes through each element in the matrix and
+    for (int i = 0; i < n; i++)                // adds the value in the transposed copy to the original value
     {
       for (int j = 0; j < m; j++)
       {
@@ -1156,4 +1240,179 @@ public class Matrix implements MatrixI
     return NaN;
   }
 
+  /**
+  * performs an element-wise addition of this matrix by another matrix ~ this - m
+  * @param m ~ other matrix
+  *
+  * @return
+  */
+  @Override
+  public MatrixI add(MatrixI m)
+  {
+    if (m.width() != cols || m.height() != rows)
+    {
+      throw new IllegalArgumentException("Can't add a " + m.height() + "x" + m.width() + " to a " + this.rows + "x" + this.cols + " matrix");
+    }
+    double[][] tmp = new double[this.rows][this.cols];
+    for (int i = 0; i < this.rows; i++)
+    {
+      for (int j = 0; j < this.cols; j++)
+      {
+       tmp[i][j] = this.getValue(i,j) + m.getValue(i,j);
+      }
+    }
+    return new Matrix(tmp);
+  }
+
+  /**
+  * performs an element-wise subtraction of this matrix by another matrix ~ this - m
+  * @param m ~ other matrix
+  *
+  * @return
+  */
+  @Override
+  public MatrixI subtract(MatrixI m)
+  {
+    if (m.width() != cols || m.height() != rows)
+    {
+      throw new IllegalArgumentException("Can't subtract a " + m.height() + "x" + m.width() + " from a " + this.rows + "x" + this.cols + " matrix");
+    }
+    double[][] tmp = new double[this.rows][this.cols];
+    for (int i = 0; i < this.rows; i++)
+    {
+      for (int j = 0; j < this.cols; j++)
+      {
+       tmp[i][j] = this.getValue(i,j) -  m.getValue(i,j);
+      }
+    }
+    return new Matrix(tmp);
+  }
+
+  /**
+  * performs an element-wise multiplication of this matrix by another matrix ~ this * m
+  * @param m ~ other matrix
+  *
+  * @return
+  */
+  @Override
+  public MatrixI elementwiseMultiply(MatrixI m)
+  {
+    if (m.width() != cols || m.height() != rows)
+    {
+      throw new IllegalArgumentException("Can't multiply a " + this.rows + "x" + this.cols + " by a " + m.height() + "x" + m.width() + " matrix");
+    }
+    double[][] tmp = new double[this.rows][this.cols];
+    for (int i = 0; i < this.rows; i++)
+    {
+      for (int j = 0; j < this.cols; j++)
+      {
+        tmp[i][j] = this.getValue(i, j) * m.getValue(i,j);
+      }
+    }
+    return new Matrix(tmp);
+  }
+
+  /**
+  * performs an element-wise division of this matrix by another matrix ~ this / m
+  * @param m ~ other matrix
+  *
+  * @return
+  */
+  @Override
+  public MatrixI elementwiseDivide(MatrixI m)
+  {
+    if (m.width() != cols || m.height() != rows)
+    {
+      throw new IllegalArgumentException("Can't divide a " + this.rows + "x" + this.cols + " by a " + m.height() + "x" + m.width() + " matrix");
+    }
+    double[][] tmp = new double[this.rows][this.cols];
+    for (int i = 0; i < this.rows; i++)
+    {
+      for (int j = 0; j < this.cols; j++)
+      {
+        tmp[i][j] = this.getValue(i, j) / m.getValue(i,j);
+      }
+    }
+    return new Matrix(tmp);
+  }
+
+  /**
+  * calculate the root-mean-square for tow matrices
+  * @param m ~ other matrix
+  *
+  * @return
+  */
+  @Override
+  public double rmsd(MatrixI m)
+  {
+    MatrixI squaredDeviates = this.subtract(m);
+    squaredDeviates = squaredDeviates.preMultiply(squaredDeviates);
+    return Math.sqrt(squaredDeviates.mean());
+  }
+
+  /**
+  * calculates the Frobenius norm of this matrix
+  *
+  * @return
+  */
+  @Override
+  public double norm()
+  {
+    double result = 0;
+    for (double[] row : value)
+    {
+      for (double val : row)
+      {
+       result += Math.pow(val, 2);
+      }
+    }
+    return Math.sqrt(result);
+  }
+
+  /**
+  * returns the sum of all values in this matrix
+  *
+  * @return
+  */
+  @Override
+  public double sum()
+  {
+    double sum = 0;
+    for (double[] row : value)
+    {
+      for (double val : row)
+      {
+       sum += (Double.isNaN(val)) ? 0.0 : val;
+      }
+    }
+    return sum;
+  }
+
+  /**
+  * returns the sum-product of this matrix with vector v
+  * @param v ~ vector
+  *
+  * @return
+  */
+  @Override
+  public double[] sumProduct(double[] v)
+  {
+    if (v.length != cols)
+    {
+      throw new IllegalArgumentException("Vector and matrix do not have the same dimension! (" + v.length + " != " + cols + ")");
+    }
+    double[] result = new double[rows];
+    for (int i = 0; i < rows; i++)
+    {
+      double[] row = value[i];
+      double sum = 0;
+      for (int j = 0; j < row.length; j++)
+      {
+       sum += row[j] * v[j];
+      }
+      result[i] = sum;
+    }
+    return result;
+  }
+
 }