JAL-4159 peek in a Jalview project's PCA viewer's title to decide if it is a "pasimap...
[jalview.git] / src / jalview / math / MiscMath.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.math;
22
23 import jalview.util.Format;
24
25 import java.lang.Math;
26 import java.util.Arrays;
27
28 /**
29  * A collection of miscellaneous mathematical operations
30  * @AUTHOR MorellThomas
31  */
32 public class MiscMath
33 {
34   /**
35   * prints an array
36   * @param m ~ array
37   */
38   public static void print(double[] m, String format)
39   {
40     System.out.print("[ ");
41     for (double a : m)
42     {
43       Format.print(System.out, format + " ", a);
44     }
45     System.out.println("]");
46   }
47
48   /**
49   * calculates the mean of an array 
50   *
51   * @param m ~ array
52   * @return
53   */
54   public static double mean(double[] m)
55   {
56     double sum = 0;
57     int nanCount = 0;
58     for (int i = 0; i < m.length; i++)
59     {
60       if (!Double.isNaN(m[i]))  // ignore NaN values in the array
61       {
62         sum += m[i];
63       } else {
64         nanCount++;
65       }
66     }
67     return sum / (double) (m.length - nanCount);
68   }
69
70   /**
71   * calculates the sum of an array 
72   *
73   * @param m ~ array
74   * @return
75   */
76   public static double sum(double[] m)
77   {
78     double sum = 0;
79     for (int i = 0; i < m.length; i++)
80     {
81       if (!Double.isNaN(m[i]))  // ignore NaN values in the array
82       {
83         sum += m[i];
84       }
85     }
86     return sum;
87   }
88
89   /**
90   * calculates the square root of each element in an array
91   *
92   * @param m ~ array
93   *
94   * @return
95   * TODO
96   * make general with function passed -> apply function to each element
97   */
98   public static double[] sqrt(double[] m)
99   {
100     double[] sqrts = new double[m.length];
101     for (int i = 0; i < m.length; i++)
102     {
103       sqrts[i] = Math.sqrt(m[i]);
104     }
105     return sqrts;
106   }
107
108   /**
109   * calculate element wise multiplication of two arrays with the same length
110   *
111   * @param a ~ array
112   * @param b ~ array
113   *
114   * @return
115   */
116   public static double[] elementwiseMultiply(byte[] a, double[] b) throws RuntimeException
117   {
118     if (a.length != b.length)   // throw exception if the arrays do not have the same length
119     {
120       throw new SameLengthException(a.length, b.length);
121     }
122     double[] result = new double[a.length];
123     for (int i = 0; i < a.length; i++)
124     {
125       result[i] = a[i] * b[i];
126     }
127     return result;
128   }
129   public static double[] elementwiseMultiply(double[] a, double[] b) throws RuntimeException
130   {
131     if (a.length != b.length)   // throw exception if the arrays do not have the same length
132     {
133       throw new SameLengthException(a.length, b.length);
134     }
135     double[] result = new double[a.length];
136     for (int i = 0; i < a.length; i++)
137     {
138       result[i] = a[i] * b[i];
139     }
140     return result;
141   }
142   public static byte[] elementwiseMultiply(byte[] a, byte[] b) throws RuntimeException
143   {
144     if (a.length != b.length)   // throw exception if the arrays do not have the same length
145     {
146       throw new SameLengthException(a.length, b.length);
147     }
148     byte[] result = new byte[a.length];
149     for (int i = 0; i < a.length; i++)
150     {
151       result[i] = (byte) (a[i] * b[i]);
152     }
153     return result;
154   }
155   public static double[] elementwiseMultiply(double[] a, double b)
156   {
157     double[] result = new double[a.length];
158     for (int i = 0; i < a.length; i++)
159     {
160       result[i] = a[i] * b;
161     }
162     return result;
163   }
164
165   /**
166   * calculate element wise division of two arrays ~ a / b
167   *
168   * @param a ~ array
169   * @param b ~ array
170   *
171   * @return
172   */
173   public static double[] elementwiseDivide(double[] a, double[] b) throws RuntimeException
174   {
175     if (a.length != b.length)   // throw exception if the arrays do not have the same length
176     {
177       throw new SameLengthException(a.length, b.length);
178     }
179     double[] result = new double[a.length];
180     for (int i = 0; i < a.length; i++)
181     {
182       result[i] = a[i] / b[i];
183     }
184     return result;
185   }
186
187   /**
188   * calculate element wise addition of two arrays
189   *
190   * @param a ~ array
191   * @param b ~ array
192   *
193   * @return
194   */
195   public static double[] elementwiseAdd(double[] a, double[] b) throws RuntimeException
196   {
197     if (a.length != b.length)   // throw exception if the arrays do not have the same length
198     {
199       throw new SameLengthException(a.length, b.length);
200     }
201     double[] result = new double[a.length];
202
203     for (int i = 0; i < a.length; i++)
204     {
205       result[i] += a[i] + b[i];
206     }
207     return result;
208   }
209   public static double[] elementwiseAdd(double[] a, double b)
210   {
211     double[] result = new double[a.length];
212     for (int i = 0; i < a.length; i++)
213     {
214       result[i] = a[i] + b;
215     }
216     return result;
217   }
218
219   /**
220   * returns true if two arrays are element wise within a tolerance
221   *
222   * @param a ~ array
223   * @param b ~ array
224   * @param rtol ~ relative tolerance
225   * @param atol ~ absolute tolerance
226   * @param equalNAN ~ whether NaN at the same position return true
227   *
228   * @return
229   */
230   public static boolean allClose(double[] a, double[] b, double rtol, double atol, boolean equalNAN)
231   {
232     boolean areEqual = true;
233     for (int i = 0; i < a.length; i++)
234     {
235       if (equalNAN && (Double.isNaN(a[i]) && Double.isNaN(b[i])))       // if equalNAN == true -> skip the NaN pair
236       {
237         continue;
238       }
239       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
240       {
241         areEqual = false;
242         break;
243       }
244     }
245     return areEqual;
246   }
247
248   /**
249   * returns the index of the maximum and the maximum value of an array
250   * 
251   * @param a ~ array
252   *
253   * @return
254   */
255   public static int[] findMax(int[] a)
256   {
257     int max = 0;
258     int maxIndex = 0;
259     for (int i = 0; i < a.length; i++)
260     {
261       if (a[i] > max)
262       {
263         max = a[i];
264         maxIndex = i;
265       }
266     }
267     return new int[]{maxIndex, max};
268   }
269
270   /**
271   * returns the dot product of two arrays
272   * @param a ~ array a
273   * @param b ~ array b
274   *
275   * @return
276   */
277   public static double dot(double[] a, double[] b)
278   {
279     if (a.length != b.length)
280     {
281       throw new IllegalArgumentException(String.format("Vectors do not have the same length (%d, %d)!", a.length, b.length));
282     }
283
284     double aibi = 0;
285     for (int i = 0; i < a.length; i++)
286     {
287       aibi += a[i] * b[i];
288     }
289     return aibi;
290   }
291
292   /**
293   * returns the euklidian norm of the vector
294   * @param v ~ vector
295   *
296   * @return
297   */
298   public static double norm(double[] v)
299   {
300     double result = 0;
301     for (double i : v)
302     {
303       result += Math.pow(i, 2);
304     }
305   return Math.sqrt(result);
306   }
307
308   /**
309   * returns the number of NaN in the vector
310   * @param v ~ vector
311   *
312   * @return
313   */
314   public static int countNaN(double[] v)
315   {
316     int cnt = 0;
317     for (double i : v)
318     {
319       if (Double.isNaN(i))
320       {
321         cnt++;
322       }
323     }
324     return cnt;
325   }
326
327   /**
328   * recursively calculates the permutations of total n items with r items per combination
329   * according to n!/(n-r)! by only multiplying the relevant terms
330   * @param n 
331   * @param r
332   *
333   * @return permutations
334   */
335   public static long permutations(int n, int r)
336   {
337     if (n < r)
338       return permutations(r, n);
339
340     long result = 1l;
341     for (int i = 0; i < r; i++)
342     {
343       result *= (n-i);
344     }
345     return result;
346   }
347
348   /**
349    * calculate all unique combinations of n elements into r sized groups
350    * @param n
351    * @param r
352    *
353    * @return
354    */
355   public static int combinations(int n, int r)
356   {
357     int result = 1;
358     for (int i = 0; i < r; i++)
359     {
360       result *= (n-1);
361     }
362     return (int) (result / MiscMath.factorial(r));
363   }
364
365   /**
366    * calculate the factorial of n (n >= 0)
367    * @param n
368    *
369    * @return
370    */
371   public static int factorial(int n)
372   {
373     int result = 1;
374     for (int i = 0; i < n; i++)
375     {
376       result *= (n - i);
377     }
378     return result;
379   }
380
381 }