*/
package jalview.math;
+import jalview.util.Format;
+
import java.lang.Math;
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;
}
/**
}
/**
- * calculate element wise multiplication of two arrays
+ * calculate element wise multiplication of two arrays with the same length
*
* @param a ~ array
* @param b ~ array
*/
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);
}
}
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);
}
}
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);
}
}
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
*/
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);
}
}
/**
+ * 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
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;
/**
* 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)
{
}
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;
+ }
+
}