package cse1030; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class ArrayMath { public static double[] cos(double[] a) { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.cos(a[i]); } return result; } public static double[] sin(double[] a) { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.sin(a[i]); } return result; } public static double[] tan(double[] a) { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.tan(a[i]); } return result; } public static double max(double[] a) { double currentMax = Double.NEGATIVE_INFINITY; for (int i = 0; i < a.length; i++) { if (a[i] > currentMax) { currentMax = a[i]; } } return currentMax; } public static double min(double[] a) { double currentMin = Double.POSITIVE_INFINITY; for (int i = 0; i < a.length; i++) { if (a[i] < currentMin) { currentMin = a[i]; } } return currentMin; } public static double mean(double[] a) { double mu = 0.0; for (int i = 0; i < a.length; i++) { mu += a[i] / a.length; } return mu; } public static int[] mode(int[] a) { // count the number of times each element appears in a Map m = new HashMap(); for (Integer key : a) { Integer count = m.get(key); if (count == null) { m.put(key, 1); } else { m.put(key, count + 1); } } // find the largest value in the map; // the corresponding key is a mode (there may be multiple modes) Integer modeCount = Collections.max(m.values()); // find all of the modes List modes = new ArrayList(); for (Map.Entry entry : m.entrySet()) { if (entry.getValue().equals(modeCount)) { modes.add(entry.getKey()); } } // copy the modes into an array of int int[] result = new int[modes.size()]; for (int i = 0; i < modes.size(); i++) { result[i] = modes.get(i); } return result; } }