001 /*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.stat;
021
022 import static java.lang.Double.NaN;
023 import static java.util.Objects.requireNonNull;
024
025 import java.io.Serial;
026 import java.io.Serializable;
027 import java.util.IntSummaryStatistics;
028 import java.util.function.ToIntFunction;
029 import java.util.stream.Collector;
030
031 /**
032 * <i>Value</i> objects which contains statistical summary information.
033 *
034 * @see java.util.IntSummaryStatistics
035 *
036 * @param count the count of values recorded
037 * @param min the minimum value recorded, or {@link Integer#MAX_VALUE} if no
038 * values have been recorded
039 * @param max the maximum value recorded, or {@link Integer#MIN_VALUE} if no
040 * values have been recorded
041 * @param sum the sum of values recorded, or zero if no values have been recorded
042 * @param mean the arithmetic mean of values recorded, or zero if no values have
043 * been recorded
044 *
045 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
046 * @since 3.0
047 * @version 7.0
048 */
049 public record IntSummary(
050 long count,
051 int min,
052 int max,
053 long sum,
054 double mean
055 )
056 implements Serializable
057 {
058
059 @Serial
060 private static final long serialVersionUID = 2L;
061
062 @Override
063 public String toString() {
064 return String.format(
065 "IntSummary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s]",
066 count(), min(), max(), sum(), mean()
067 );
068 }
069
070 /**
071 * Return a new value object of the statistical summary, currently
072 * represented by the {@code statistics} object.
073 *
074 * @param statistics the creating (mutable) statistics class
075 * @return the statistical moments
076 */
077 public static IntSummary of(final IntSummaryStatistics statistics) {
078 return new IntSummary(
079 statistics.getCount(),
080 statistics.getMin(),
081 statistics.getMax(),
082 statistics.getSum(),
083 statistics.getAverage()
084 );
085 }
086
087 /**
088 * Return a {@code Collector} which applies an int-producing mapping
089 * function to each input element, and return summary-statistics for the
090 * resulting values.
091 *
092 * {@snippet lang="java":
093 * final Stream<SomeObject> stream = null; // @replace substring='null' replacement="..."
094 * final IntSummary summary = stream
095 * .collect(toIntSummary(v -> v.intValue()));
096 * }
097 *
098 * @param mapper a mapping function to apply to each element
099 * @param <T> the type of the input elements
100 * @return a {@code Collector} implementing the summary-statistics reduction
101 * @throws java.lang.NullPointerException if the given {@code mapper} is
102 * {@code null}
103 */
104 public static <T> Collector<T, ?, IntSummary>
105 toIntSummary(final ToIntFunction<? super T> mapper) {
106 requireNonNull(mapper);
107 return Collector.of(
108 IntSummaryStatistics::new,
109 (a, b) -> a.accept(mapper.applyAsInt(b)),
110 (a, b) -> {a.combine(b); return a;},
111 IntSummary::of
112 );
113 }
114
115
116 /* *************************************************************************
117 * Some static helper methods.
118 **************************************************************************/
119
120 /**
121 * Return the minimum value of the given double array.
122 *
123 * @since 4.0
124 *
125 * @param values the array.
126 * @return the minimum value or {@link Integer#MAX_VALUE} if the given array is
127 * empty.
128 * @throws NullPointerException if the given array is {@code null}.
129 */
130 public static int min(final int[] values) {
131 int min = Integer.MAX_VALUE;
132 if (values.length > 0) {
133 min = values[0];
134 for (int value : values) {
135 if (value < min) {
136 min = value;
137 }
138 }
139 }
140
141 return min;
142 }
143
144 /**
145 * Return the maximum value of the given double array.
146 *
147 * @since 4.0
148 *
149 * @param values the array.
150 * @return the maximum value or {@link Integer#MIN_VALUE} if the given array is
151 * empty.
152 * @throws NullPointerException if the given array is {@code null}.
153 */
154 public static int max(final int[] values) {
155 int max = Integer.MIN_VALUE;
156 if (values.length > 0) {
157 max = values[0];
158 for (int value : values) {
159 if (value > max) {
160 max = value;
161 }
162 }
163 }
164
165 return max;
166 }
167
168 /**
169 * Return the sum of the given double array.
170 *
171 * @since 4.0
172 *
173 * @param values the values to sum up.
174 * @return the sum of the given {@code values}.
175 * @throws NullPointerException if the given array is {@code null}.
176 */
177 public static long sum(final int[] values) {
178 long sum = 0;
179 for (int i = values.length; --i >= 0;) {
180 sum += values[i];
181 }
182 return sum;
183 }
184
185 /**
186 * Returns a double describing the arithmetic mean of the values, or
187 * {@link Double#NaN} if the {@code values} array is empty.
188 *
189 * @since 4.0
190 *
191 * @param values the values to calculate the mean of
192 * @return the arithmetic mean of the given {@code values} or
193 * {@link Double#NaN} if the {@code values} array is empty
194 * @throws NullPointerException if the given array is {@code null}.
195 */
196 public static double mean(final int[] values) {
197 return values.length > 0 ? (double)sum(values)/values.length : NaN;
198 }
199
200 }
|