001package ch.akuhn.matrix;
002
003/**
004 * Utilities
005 * 
006 * @author Adrian Kuhn
007 */
008public class Util {
009
010        /**
011         * Max
012         * 
013         * @param ds
014         * @param max
015         * @return the max
016         */
017        public static double max(double[] ds, double max) {
018                for (int n = 0; n < ds.length; n++)
019                        max = Math.max(max, ds[n]);
020                return max;
021        }
022
023        /**
024         * Max
025         * 
026         * @param dss
027         * @param max
028         * @return the max
029         */
030        public static double max(double[][] dss, double max) {
031                for (final double[] ds : dss)
032                        max = max(ds, max);
033                return max;
034        }
035
036        /**
037         * Min
038         * 
039         * @param ds
040         * @param min
041         * @return the min
042         */
043        public static double min(double[] ds, double min) {
044                for (int n = 0; n < ds.length; n++)
045                        min = Math.min(min, ds[n]);
046                return min;
047        }
048
049        /**
050         * Min
051         * 
052         * @param dss
053         * @param min
054         * @return the min
055         */
056        public static double min(double[][] dss, double min) {
057                for (final double[] ds : dss)
058                        min = max(ds, min);
059                return min;
060        }
061
062        /**
063         * Sum
064         * 
065         * @param dss
066         * @return the sum
067         */
068        public static double sum(double[][] dss) {
069                double sum = 0;
070                for (final double[] ds : dss)
071                        sum += sum(ds);
072                return sum;
073        }
074
075        /**
076         * Sum
077         * 
078         * @param ds
079         * @return the sum
080         */
081        public static double sum(double[] ds) {
082                double sum = 0;
083                for (int n = 0; n < ds.length; n++)
084                        sum += ds[n];
085                return sum;
086        }
087
088        /**
089         * Count
090         * 
091         * @param dss
092         * @return the sum of the row lengths
093         */
094        public static int count(double[][] dss) {
095                int length = 0;
096                for (final double[] ds : dss)
097                        length += ds.length;
098                return length;
099        }
100
101        /**
102         * Get the histogram
103         * 
104         * @param values
105         * @param binCount
106         * @return the histogram
107         */
108        public static int[] getHistogram(double[][] values, int binCount) {
109                double max = Double.MIN_VALUE;
110                for (final double[] row : values) {
111                        for (final double each : row) {
112                                max = Math.max(max, each);
113                        }
114                }
115                max = 10; // FIXME
116                final int[] bins = new int[binCount];
117                for (final double[] row : values) {
118                        for (final double each : row) {
119                                final int index = (int) Math.floor(each / max * (binCount - 1));
120                                bins[Math.min(binCount - 1, index)]++;
121                        }
122                }
123                return bins;
124        }
125
126        /**
127         * Multiply by constant
128         * 
129         * @param dss
130         * @param d
131         */
132        public static void times(double[][] dss, double d) {
133                for (final double[] ds : dss)
134                        for (int i = 0; i < ds.length; i++)
135                                ds[i] *= d;
136        }
137
138}