Merge branch 'develop' into bug/JAL-4421_customise_volume_name_of_DMG_installers
[jalview.git] / src / jalview / math / Matrix.java
index ad91464..7ae4b94 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.util.Format;
 import jalview.util.MessageManager;
 
 import java.io.PrintStream;
+import java.lang.Math;
 import java.util.Arrays;
 
 /**
@@ -107,6 +108,32 @@ public class Matrix implements MatrixI
     }
   }
 
+  public Matrix(float[][] values)
+  {
+    this.rows = values.length;
+    this.cols = this.rows == 0 ? 0 : values[0].length;
+
+    /*
+     * make a copy of the values array, for immutability
+     */
+    this.value = new double[rows][];
+    int i = 0;
+    for (float[] row : values)
+    {
+      if (row != null)
+      {
+        value[i] = new double[row.length];
+       int j = 0;
+       for (float oldValue : row)
+       {
+         value[i][j] = oldValue;
+         j++;
+       }
+      }
+      i++;
+    }
+  }
+
   @Override
   public MatrixI transpose()
   {
@@ -164,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]);
+         }
         }
       }
     }
@@ -803,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];
@@ -831,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]);
     }
@@ -973,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)
   {
@@ -1022,4 +1084,360 @@ public class Matrix implements MatrixI
     }
     return true;
   }
+
+  /**
+   * Returns a copy in which  every value in the matrix is its absolute
+   * 
+   * @return
+   */
+  @Override
+  public MatrixI absolute()
+  {
+    MatrixI copy = this.copy();
+    for (int i = 0; i < copy.width(); i++)
+    {
+      double[] row = copy.getRow(i);
+      if (row != null)
+      {
+        for (int j = 0; j < row.length; j++)
+        {
+          row[j] = Math.abs(row[j]);
+        }
+      }
+    }
+    return copy;
+  }
+
+  /**
+   * Returns the mean of each row
+   * 
+   * @return
+   */
+  @Override
+  public double[] meanRow()
+  {
+    double[] mean = new double[rows];
+    int i = 0;
+    for (double[] row : value)
+    {
+      if (row != null)
+      {
+       mean[i++] = MiscMath.mean(row);
+      }
+    }
+    return mean;
+  }
+
+  /**
+   * Returns the mean of each column
+   * 
+   * @return
+   */
+  @Override
+  public double[] meanCol()
+  {
+    double[] mean = new double[cols];
+    for (int j = 0; j < cols; j++)
+    {
+      double[] column = getColumn(j);
+      if (column != null)
+      {
+       mean[j] = MiscMath.mean(column);
+      }
+    }
+    return mean;
+  }
+
+  /**
+  * 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
+  */
+  @Override
+  public void fillDiagonal()
+  {
+    int n = this.rows;
+    int m = this.cols;
+    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++)
+      {
+       if (i != j)
+       {
+         this.addValue(i, j, copy.getValue(i,j));
+       }
+      }
+    }
+  }
+
+  /**
+  * counts the number of Double.NaN in the matrix
+  *
+  * @return
+  */
+  @Override
+  public int countNaN()
+  {
+    int NaN = 0;
+    for (int i = 0; i < this.rows; i++)
+    {
+      for (int j = 0; j < this.cols; j++)
+      {
+       if (Double.isNaN(this.getValue(i,j)))
+       {
+         NaN++;
+       }
+      }
+    }
+    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;
+  }
+
+  /**
+  * mirrors columns of the matrix
+  *
+  * @return
+  */
+  @Override
+  public MatrixI mirrorCol()
+  {
+    double[][] result = new double[rows][cols];
+    for (int i = 0; i < rows; i++)
+    {
+      int k = cols - 1;        // reverse col
+      for (int j = 0; j < cols; j++)
+      {
+       result[i][k--] = this.getValue(i,j);
+      }
+    }
+    MatrixI resultMatrix = new Matrix(result);
+    if (d != null)
+      resultMatrix.setD(d);
+    if (e != null)
+      resultMatrix.setE(e);
+
+    return resultMatrix;
+  }
 }