00001
00002
00003
00004 package org.math.plot.utils;
00005
00006 import static org.math.plot.utils.Array.*;
00007
00008 public class Histogram {
00009
00010
00011 public static double[][] histogram_classes(double[] values, double[] bounds) {
00012 return mergeColumns(centers(bounds), histogram(values, bounds));
00013 }
00014
00015 public static double[][] histogram_classes(double[] values, double min, double max, int n) {
00016 double[] bounds = bounds(values, min, max, n);
00017 return mergeColumns(centers(bounds), histogram(values, bounds));
00018 }
00019
00020 public static double[][] histogram_classes(double[] values, int n) {
00021 double[] bounds = bounds(values, n);
00022 return mergeColumns(centers(bounds), histogram(values, bounds));
00023 }
00024
00025 public static double[] histogram(double[] values, double[] bounds) {
00026 double[] h = new double[bounds.length - 1];
00027 for (int i = 0; i < values.length; i++) {
00028 for (int j = 0; j < h.length; j++) {
00029 if (((bounds[j + 1] - values[i]) * (bounds[j] - values[i]) < 0) || ((bounds[j] == values[i])))
00030 h[j]++;
00031 }
00032 }
00033 return h;
00034 }
00035
00036 public static double[] histogram(double[] values, double min, double max, int n) {
00037 double[] bounds = bounds(values, min, max, n);
00038 return histogram(values, bounds);
00039 }
00040
00041 public static double[] histogram(double[] values, int n) {
00042 return histogram(values, n);
00043 }
00044
00045 private static double[] bounds(double[] values, int n) {
00046 double min = min(values);
00047 double max = max(values);
00048 return bounds(values, min, max, n);
00049 }
00050
00051 private static double[] bounds(double[] values, double min, double max, int n) {
00052 double[] bounds = new double[n + 1];
00053 for (int i = 0; i < bounds.length; i++) {
00054 bounds[i] = min + (max - min) * i / (double) n;
00055 }
00056 return bounds;
00057 }
00058
00059 private static double[] centers(double[] bounds) {
00060 double[] center = new double[bounds.length - 1];
00061 for (int i = 0; i < center.length; i++)
00062 center[i] = (bounds[i] + bounds[i + 1]) / 2;
00063 return center;
00064 }
00065
00066
00067
00068 public static double[][] histogram_classes_2D(double[][] values, double[] boundsX, double[] boundsY) {
00069 return insertColumn(centers_2D(boundsX, boundsY), histogram_2D(values, boundsX, boundsY), 2);
00070 }
00071
00072 public static double[][] histogram_classes_2D(double[][] values, double minX, double maxX, int nX, double minY, double maxY, int nY) {
00073 double[] valuesX = getColumnCopy(values, 0);
00074 double[] valuesY = getColumnCopy(values, 1);
00075 double[] boundsX = bounds(valuesX, minX, maxX, nX);
00076 double[] boundsY = bounds(valuesY, minY, maxY, nY);
00077 return insertColumn(centers_2D(boundsX, boundsY), histogram_2D(values, boundsX, boundsY), 2);
00078 }
00079
00080 public static double[][] histogram_classes_2D(double[][] values, int nX, int nY) {
00081 double[] valuesX = getColumnCopy(values, 0);
00082 double[] valuesY = getColumnCopy(values, 1);
00083 double[] boundsX = bounds(valuesX, nX);
00084 double[] boundsY = bounds(valuesY, nY);
00085 return insertColumn(centers_2D(boundsX, boundsY), histogram_2D(values, boundsX, boundsY), 2);
00086 }
00087
00088 public static double[] histogram_2D(double[][] values, double[] boundsX, double[] boundsY) {
00089 double[] h = new double[(boundsX.length - 1) * (boundsY.length - 1)];
00090 for (int n = 0; n < values.length; n++) {
00091 for (int i = 0; i < boundsX.length - 1; i++) {
00092 for (int j = 0; j < boundsY.length - 1; j++) {
00093 if ((((boundsX[i + 1] - values[n][0]) * (boundsX[i] - values[n][0]) < 0) || ((boundsX[i] == values[n][0])))
00094 && (((boundsY[j + 1] - values[n][1]) * (boundsY[j] - values[n][1]) < 0) || ((boundsY[j] == values[n][1]))))
00095 h[index2(i, j, boundsX.length - 1)]++;
00096 }
00097 }
00098 }
00099 return h;
00100 }
00101
00102 public static double[] histogram_2D(double[][] values, double minX, double maxX, int nX, double minY, double maxY, int nY) {
00103 double[] valuesX = getColumnCopy(values, 0);
00104 double[] valuesY = getColumnCopy(values, 1);
00105 double[] boundsX = bounds(valuesX, minX, maxX, nX);
00106 double[] boundsY = bounds(valuesY, minY, maxY, nY);
00107 return histogram_2D(values, boundsX, boundsY);
00108 }
00109
00110 public static double[] histogram_2D(double[][] values, int nX, int nY) {
00111 double[] valuesX = getColumnCopy(values, 0);
00112 double[] valuesY = getColumnCopy(values, 1);
00113 double[] boundsX = bounds(valuesX, nX);
00114 double[] boundsY = bounds(valuesY, nY);
00115 return histogram_2D(values, boundsX, boundsY);
00116 }
00117
00118 private static double[][] centers_2D(double[] boundsX, double[] boundsY) {
00119 int nb_centers = (boundsX.length - 1) * (boundsY.length - 1);
00120 double[][] center = new double[nb_centers][2];
00121 for (int i = 0; i < boundsX.length - 1; i++) {
00122 for (int j = 0; j < boundsY.length - 1; j++) {
00123 int k = index2(i, j, boundsX.length - 1);
00124 center[k][0] = (boundsX[i] + boundsX[i + 1]) / 2;
00125 center[k][1] = (boundsY[j] + boundsY[j + 1]) / 2;
00126 }
00127 }
00128 return center;
00129 }
00130
00131 private static int index2(int i, int j, int imax) {
00132 return i + imax * j;
00133 }
00134
00135
00136
00137
00138
00139
00140 public static double[][] histogram_classes_3D(double[][] values, int nX, int nY, int nZ) {
00141 double[] valuesX = getColumnCopy(values, 0);
00142 double[] valuesY = getColumnCopy(values, 1);
00143 double[] valuesZ = getColumnCopy(values, 2);
00144 double[] boundsX = bounds(valuesX, nX);
00145 double[] boundsY = bounds(valuesY, nY);
00146 double[] boundsZ = bounds(valuesZ, nZ);
00147 return insertColumn(centers_3D(boundsX, boundsY, boundsZ), histogram_3D(values, boundsX, boundsY, boundsZ), 3);
00148 }
00149
00150 public static double[] histogram_3D(double[][] values, double[] boundsX, double[] boundsY, double[] boundsZ) {
00151 double[] h = new double[(boundsX.length - 1) * (boundsY.length - 1)* (boundsZ.length - 1)];
00152 for (int n = 0; n < values.length; n++) {
00153 for (int i = 0; i < boundsX.length - 1; i++) {
00154 for (int j = 0; j < boundsY.length - 1; j++) {
00155 for (int k = 0; k < boundsZ.length - 1; k++) {
00156 if ((((boundsX[i + 1] - values[n][0]) * (boundsX[i] - values[n][0]) < 0) || ((boundsX[i] == values[n][0])))
00157 && (((boundsY[j + 1] - values[n][1]) * (boundsY[j] - values[n][1]) < 0) || ((boundsY[j] == values[n][1])))&& (((boundsZ[k + 1] - values[n][2]) * (boundsZ[k] - values[n][2]) < 0) || ((boundsZ[k] == values[n][2]))))
00158 h[index3(i, j,k, boundsX.length - 1, boundsY.length - 1)]++;
00159 }}
00160 }
00161 }
00162 return h;
00163 }
00164
00165
00166
00167 private static double[][] centers_3D(double[] boundsX, double[] boundsY, double[] boundsZ) {
00168 int nb_centers = (boundsX.length - 1) * (boundsY.length - 1)* (boundsZ.length - 1);
00169 double[][] center = new double[nb_centers][3];
00170 for (int i = 0; i < boundsX.length - 1; i++) {
00171 for (int j = 0; j < boundsY.length - 1; j++) {
00172 for (int k = 0; k < boundsZ.length - 1; k++) {
00173 int l = index3(i, j,k, boundsX.length - 1, boundsY.length - 1);
00174 center[l][0] = (boundsX[i] + boundsX[i + 1]) / 2;
00175 center[l][1] = (boundsY[j] + boundsY[j + 1]) / 2;
00176 center[l][2] = (boundsZ[k] + boundsZ[k + 1]) / 2;
00177 }
00178 }
00179 }
00180 return center;
00181 }
00182
00183
00184
00185
00186 private static int index3(int i, int j, int k,int imax, int jmax) {
00187 return i + imax * j + imax*jmax*k;
00188 }
00189
00190 }