Implemented least-squares optimisation in ccAnalysis
[jalview.git] / src / jalview / math / MiscMath.java
index 4fde68a..d3cec30 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.math;
 
+import jalview.util.Format;
+
 import java.lang.Math;
 import java.util.Arrays;
 
@@ -30,23 +32,58 @@ import java.util.Arrays;
 public class MiscMath
 {
   /**
+  * prints an array
+  * @param m ~ array
+  */
+  public static void print(double[] m, String format)
+  {
+    System.out.print("[ ");
+    for (double a : m)
+    {
+      Format.print(System.out, format + " ", a);
+    }
+    System.out.println("]");
+  }
+
+  /**
   * calculates the mean of an array 
   *
   * @param m ~ array
   * @return
-  * TODO whats with nans??
   */
   public static double mean(double[] m)
   {
     double sum = 0;
+    int nanCount = 0;
     for (int i = 0; i < m.length; i++)
     {
-      if (!Double.isNaN(m[i]))
+      if (!Double.isNaN(m[i])) // ignore NaN values in the array
       {
         sum += m[i];
+      } else {
+       nanCount++;
       }
     }
-    return sum / m.length;
+    return sum / (double) (m.length - nanCount);
+  }
+
+  /**
+  * calculates the sum of an array 
+  *
+  * @param m ~ array
+  * @return
+  */
+  public static double sum(double[] m)
+  {
+    double sum = 0;
+    for (int i = 0; i < m.length; i++)
+    {
+      if (!Double.isNaN(m[i])) // ignore NaN values in the array
+      {
+        sum += m[i];
+      }
+    }
+    return sum;
   }
 
   /**
@@ -69,7 +106,7 @@ public class MiscMath
   }
 
   /**
-  * calculate element wise multiplication of two arrays
+  * calculate element wise multiplication of two arrays with the same length
   *
   * @param a ~ array
   * @param b ~ array
@@ -78,7 +115,7 @@ public class MiscMath
   */
   public static double[] elementwiseMultiply(byte[] a, double[] b) throws RuntimeException
   {
-    if (a.length != b.length)
+    if (a.length != b.length)  // throw exception if the arrays do not have the same length
     {
       throw new SameLengthException(a.length, b.length);
     }
@@ -91,7 +128,7 @@ public class MiscMath
   }
   public static double[] elementwiseMultiply(double[] a, double[] b) throws RuntimeException
   {
-    if (a.length != b.length)
+    if (a.length != b.length)  // throw exception if the arrays do not have the same length
     {
       throw new SameLengthException(a.length, b.length);
     }
@@ -104,7 +141,7 @@ public class MiscMath
   }
   public static byte[] elementwiseMultiply(byte[] a, byte[] b) throws RuntimeException
   {
-    if (a.length != b.length)
+    if (a.length != b.length)  // throw exception if the arrays do not have the same length
     {
       throw new SameLengthException(a.length, b.length);
     }
@@ -115,9 +152,18 @@ public class MiscMath
     }
     return result;
   }
+  public static double[] elementwiseMultiply(double[] a, double b)
+  {
+    double[] result = new double[a.length];
+    for (int i = 0; i < a.length; i++)
+    {
+      result[i] = a[i] * b;
+    }
+    return result;
+  }
 
   /**
-  * calculate element wise division of two arrays
+  * calculate element wise division of two arrays ~ a / b
   *
   * @param a ~ array
   * @param b ~ array
@@ -126,7 +172,7 @@ public class MiscMath
   */
   public static double[] elementwiseDivide(double[] a, double[] b) throws RuntimeException
   {
-    if (a.length != b.length)
+    if (a.length != b.length)  // throw exception if the arrays do not have the same length
     {
       throw new SameLengthException(a.length, b.length);
     }
@@ -139,6 +185,38 @@ public class MiscMath
   }
 
   /**
+  * calculate element wise addition of two arrays
+  *
+  * @param a ~ array
+  * @param b ~ array
+  *
+  * @return
+  */
+  public static double[] elementwiseAdd(double[] a, double[] b) throws RuntimeException
+  {
+    if (a.length != b.length)  // throw exception if the arrays do not have the same length
+    {
+      throw new SameLengthException(a.length, b.length);
+    }
+    double[] result = new double[a.length];
+
+    for (int i = 0; i < a.length; i++)
+    {
+      result[i] += a[i] + b[i];
+    }
+    return result;
+  }
+  public static double[] elementwiseAdd(double[] a, double b)
+  {
+    double[] result = new double[a.length];
+    for (int i = 0; i < a.length; i++)
+    {
+      result[i] = a[i] + b;
+    }
+    return result;
+  }
+
+  /**
   * returns true if two arrays are element wise within a tolerance
   *
   * @param a ~ array
@@ -154,11 +232,11 @@ public class MiscMath
     boolean areEqual = true;
     for (int i = 0; i < a.length; i++)
     {
-      if (equalNAN && (Double.isNaN(a[i]) && Double.isNaN(b[i])))
+      if (equalNAN && (Double.isNaN(a[i]) && Double.isNaN(b[i])))      // if equalNAN == true -> skip the NaN pair
       {
        continue;
       }
-      if (Math.abs(a[i] - b[i]) > (atol + rtol * Math.abs(b[i])))
+      if (Math.abs(a[i] - b[i]) > (atol + rtol * Math.abs(b[i])))      // check for the similarity condition -> if not met -> break and return false
       {
        areEqual = false;
        break;
@@ -170,9 +248,9 @@ public class MiscMath
   /**
   * returns the index of the maximum and the maximum value of an array
   * 
-  * @pararm a ~ array
+  * @param a ~ array
   *
-  * @returns
+  * @return
   */
   public static int[] findMax(int[] a)
   {
@@ -188,4 +266,62 @@ public class MiscMath
     }
     return new int[]{maxIndex, max};
   }
+
+  /**
+  * returns the dot product of two arrays
+  * @param a ~ array a
+  * @param b ~ array b
+  *
+  * @return
+  */
+  public static double dot(double[] a, double[] b)
+  {
+    if (a.length != b.length)
+    {
+      throw new IllegalArgumentException(String.format("Vectors do not have the same length (%d, %d)!", a.length, b.length));
+    }
+
+    double aibi = 0;
+    for (int i = 0; i < a.length; i++)
+    {
+      aibi += a[i] * b[i];
+    }
+    return aibi;
+  }
+
+  /**
+  * returns the euklidian norm of the vector
+  * @param v ~ vector
+  *
+  * @return
+  */
+  public static double norm(double[] v)
+  {
+    double result = 0;
+    for (double i : v)
+    {
+      result += Math.pow(i, 2);
+    }
+  return Math.sqrt(result);
+  }
+
+  /**
+  * returns the number of NaN in the vector
+  * @param v ~ vector
+  *
+  * @return
+  */
+  public static int countNaN(double[] v)
+  {
+    int cnt = 0;
+    for (double i : v)
+    {
+      if (Double.isNaN(i))
+      {
+       cnt++;
+      }
+    }
+    return cnt;
+  }
+
 }