2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.lang.Math;
24 import java.util.Arrays;
27 * A collection of miscellaneous mathematical operations
28 * @AUTHOR MorellThomas
33 * calculates the mean of an array
37 * TODO whats with nans??
39 public static double mean(double[] m)
42 for (int i = 0; i < m.length; i++)
44 if (!Double.isNaN(m[i]))
49 return sum / m.length;
53 * calculates the square root of each element in an array
59 * make general with function passed -> apply function to each element
61 public static double[] sqrt(double[] m)
63 double[] sqrts = new double[m.length];
64 for (int i = 0; i < m.length; i++)
66 sqrts[i] = Math.sqrt(m[i]);
72 * calculate element wise multiplication of two arrays
79 public static double[] elementwiseMultiply(byte[] a, double[] b) throws RuntimeException
81 if (a.length != b.length)
83 throw new SameLengthException(a.length, b.length);
85 double[] result = new double[a.length];
86 for (int i = 0; i < a.length; i++)
88 result[i] = a[i] * b[i];
92 public static double[] elementwiseMultiply(double[] a, double[] b) throws RuntimeException
94 if (a.length != b.length)
96 throw new SameLengthException(a.length, b.length);
98 double[] result = new double[a.length];
99 for (int i = 0; i < a.length; i++)
101 result[i] = a[i] * b[i];
105 public static byte[] elementwiseMultiply(byte[] a, byte[] b) throws RuntimeException
107 if (a.length != b.length)
109 throw new SameLengthException(a.length, b.length);
111 byte[] result = new byte[a.length];
112 for (int i = 0; i < a.length; i++)
114 result[i] = (byte) (a[i] * b[i]);
120 * calculate element wise division of two arrays
127 public static double[] elementwiseDivide(double[] a, double[] b) throws RuntimeException
129 if (a.length != b.length)
131 throw new SameLengthException(a.length, b.length);
133 double[] result = new double[a.length];
134 for (int i = 0; i < a.length; i++)
136 result[i] = a[i] / b[i];
142 * returns true if two arrays are element wise within a tolerance
146 * @param rtol ~ relative tolerance
147 * @param atol ~ absolute tolerance
148 * @param equalNAN ~ whether NaN at the same position return true
152 public static boolean allClose(double[] a, double[] b, double rtol, double atol, boolean equalNAN)
154 boolean areEqual = true;
155 for (int i = 0; i < a.length; i++)
157 if (equalNAN && (Double.isNaN(a[i]) && Double.isNaN(b[i])))
161 if (Math.abs(a[i] - b[i]) > (atol + rtol * Math.abs(b[i])))
171 * returns the index of the maximum and the maximum value of an array
177 public static int[] findMax(int[] a)
181 for (int i = 0; i < a.length; i++)
189 return new int[]{maxIndex, max};