arithmetic.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 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@gmx.at)
019  */
020 package org.jenetics.internal.math;
021 
022 import org.jenetics.internal.util.require;
023 
024 /**
025  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
026  * @since 3.0
027  * @version 3.0
028  */
029 public final class arithmetic {
030     private arithmetic() {require.noInstance();}
031 
032 
033     /**
034      * Normalize the given double array, so that it sum to one. The
035      * normalization is performed in place and the same {@code values} are
036      * returned.
037      *
038      * @param values the values to normalize.
039      * @return the {@code values} array.
040      * @throws NullPointerException if the given double array is {@code null}.
041      */
042     public static double[] normalize(final double[] values) {
043         final double sum = 1.0/DoubleAdder.sum(values);
044         for (int i = values.length; --i >= 0;) {
045             values[i] = values[i]*sum;
046         }
047 
048         return values;
049     }
050 
051     public static double[] normalize(final long[] values) {
052         final double[] result = new double[values.length];
053         final double sum = 1.0/statistics.sum(values);
054         for (int i = values.length; --i >= 0;) {
055             result[i] = values[i]*sum;
056         }
057 
058         return result;
059     }
060 
061     /**
062      * Component wise division of the given double array.
063      *
064      * @param values the double values to divide.
065      * @param divisor the divisor.
066      * @throws NullPointerException if the given double array is {@code null}.
067      */
068     public static void divide(final double[] values, final double divisor) {
069         for (int i = values.length; --i >= 0;) {
070             values[i] /= divisor;
071         }
072     }
073 
074     /**
075      * Binary exponentiation algorithm.
076      *
077      * @param b the base number.
078      * @param e the exponent.
079      * @return {@code b^e}.
080      */
081     public static long pow(final long b, final long e) {
082         long base = b;
083         long exp = e;
084         long result = 1;
085 
086         while (exp != 0) {
087             if ((exp & 1) != 0) {
088                 result *= base;
089             }
090             exp >>>= 1;
091             base *= base;
092         }
093 
094         return result;
095     }
096 
097     public static boolean isMultiplicationSave(final int a, final int b) {
098         final long m = (long)a*(long)b;
099         return (int)m == m;
100     }
101 }