statistics.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.5.0).
03  * Copyright (c) 2007-2016 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.internal.math;
21 
22 import org.jenetics.internal.util.require;
23 
24 /**
25  * Some statistical (special) functions.
26  *
27  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
28  * @since 1.0
29  * @version 3.0
30  */
31 public final class statistics {
32     private statistics() {require.noInstance();}
33 
34     /**
35      * Return the minimum value of the given double array.
36      *
37      * @param values the double array.
38      * @return the minimum value or {@link Double#NaN} if the given array is
39      *         empty.
40      * @throws NullPointerException if the given array is {@code null}.
41      */
42     public static double min(final double[] values) {
43         double min = Double.NaN;
44         if (values.length > 0) {
45             min = values[0];
46 
47             for (int i = values.length; --i >= 1;) {
48                 if (values[i] < min) {
49                     min = values[i];
50                 }
51             }
52         }
53 
54         return min;
55     }
56 
57     /**
58      * Return the maximum value of the given double array.
59      *
60      * @param values the double array.
61      * @return the maximum value or {@link Double#NaN} if the given array is
62      *         empty.
63      * @throws NullPointerException if the given array is {@code null}.
64      */
65     public static double max(final double[] values) {
66         double max = Double.NaN;
67         if (values.length > 0) {
68             max = values[0];
69 
70             for (int i = values.length; --i >= 1;) {
71                 if (values[i] > max) {
72                     max = values[i];
73                 }
74             }
75         }
76 
77         return max;
78     }
79 
80     /**
81      * Add the values of the given array.
82      *
83      * @param values the values to add.
84      * @return the values sum.
85      * @throws NullPointerException if the values are null;
86      */
87     public static long sum(final long[] values) {
88         long sum = 0;
89         for (int i = values.length; --i >= 0;) {
90             sum += values[i];
91         }
92         return sum;
93     }
94 
95 }