Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#EXPONENT

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#EXPONENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) {
    if (e.compareTo(BigInteger.ZERO) < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (!BigInteger.ZERO.equals(e)) {
        if (e.testBit(0)) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e.shiftRight(1);
    }

    return result;
}
 
Example 2
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example 3
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise an int to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static int pow(final int k, long e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    int result = 1;
    int k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example 4
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, long e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example 5
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, long e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e >> 1;
    }

    return result;

}
 
Example 6
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException {
    if (e.compareTo(BigInteger.ZERO) < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (!BigInteger.ZERO.equals(e)) {
        if (e.testBit(0)) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e.shiftRight(1);
    }

    return result;
}
 
Example 7
Source File: ZipfDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a Zipf distribution.
 *
 * @param rng Random number generator.
 * @param numberOfElements Number of elements.
 * @param exponent Exponent.
 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
 * or {@code exponent <= 0}.
 * @since 3.1
 */
public ZipfDistribution(RandomGenerator rng,
                        int numberOfElements,
                        double exponent)
    throws NotStrictlyPositiveException {
    super(rng);

    if (numberOfElements <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION,
                                               numberOfElements);
    }
    if (exponent <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT,
                                               exponent);
    }

    this.numberOfElements = numberOfElements;
    this.exponent = exponent;
}
 
Example 8
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 * @deprecated As of 3.3. Please use {@link #pow(long,int)} instead.
 */
@Deprecated
public static long pow(final long k, long e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e >>= 1;
    }

    return result;
}
 
Example 9
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, int e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example 10
Source File: ZipfDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a Zipf distribution.
 *
 * @param rng Random number generator.
 * @param numberOfElements Number of elements.
 * @param exponent Exponent.
 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
 * or {@code exponent <= 0}.
 * @since 3.1
 */
public ZipfDistribution(RandomGenerator rng,
                        int numberOfElements,
                        double exponent)
    throws NotStrictlyPositiveException {
    super(rng);

    if (numberOfElements <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION,
                                               numberOfElements);
    }
    if (exponent <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT,
                                               exponent);
    }

    this.numberOfElements = numberOfElements;
    this.exponent = exponent;
}
 
Example 11
Source File: ZipfDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a Zipf distribution.
 *
 * @param rng Random number generator.
 * @param numberOfElements Number of elements.
 * @param exponent Exponent.
 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
 * or {@code exponent <= 0}.
 * @since 3.1
 */
public ZipfDistribution(RandomGenerator rng,
                        int numberOfElements,
                        double exponent)
    throws NotStrictlyPositiveException {
    super(rng);

    if (numberOfElements <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION,
                                               numberOfElements);
    }
    if (exponent <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT,
                                               exponent);
    }

    this.numberOfElements = numberOfElements;
    this.exponent = exponent;
}
 
Example 12
Source File: ZipfDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a Zipf distribution.
 *
 * @param rng Random number generator.
 * @param numberOfElements Number of elements.
 * @param exponent Exponent.
 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
 * or {@code exponent <= 0}.
 * @since 3.1
 */
public ZipfDistribution(RandomGenerator rng,
                        int numberOfElements,
                        double exponent)
    throws NotStrictlyPositiveException {
    super(rng);

    if (numberOfElements <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION,
                                               numberOfElements);
    }
    if (exponent <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT,
                                               exponent);
    }

    this.numberOfElements = numberOfElements;
    this.exponent = exponent;
}
 
Example 13
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a long to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static long pow(final long k, long e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    long result = 1l;
    long k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result *= k2p;
        }
        k2p *= k2p;
        e = e >> 1;
    }

    return result;
}
 
Example 14
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e >> 1;
    }

    return result;

}
 
Example 15
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a long power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, long e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (e != 0) {
        if ((e & 0x1) != 0) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e >> 1;
    }

    return result;

}
 
Example 16
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException {
    if (e.compareTo(BigInteger.ZERO) < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    BigInteger result = BigInteger.ONE;
    BigInteger k2p    = k;
    while (!BigInteger.ZERO.equals(e)) {
        if (e.testBit(0)) {
            result = result.multiply(k2p);
        }
        k2p = k2p.multiply(k2p);
        e = e.shiftRight(1);
    }

    return result;
}
 
Example 17
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Raise a long to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return \( k^e \)
 * @throws NotPositiveException if {@code e < 0}.
 * @throws MathArithmeticException if the result would overflow.
 */
public static long pow(final long k,
                       final int e)
    throws NotPositiveException,
           MathArithmeticException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    try {
        int exp = e;
        long result = 1;
        long k2p    = k;
        while (true) {
            if ((exp & 0x1) != 0) {
                result = mulAndCheck(result, k2p);
            }

            exp >>= 1;
            if (exp == 0) {
                break;
            }

            k2p = mulAndCheck(k2p, k2p);
        }

        return result;
    } catch (MathArithmeticException mae) {
        // Add context information.
        mae.getContext().addMessage(LocalizedFormats.OVERFLOW);
        mae.getContext().addMessage(LocalizedFormats.BASE, k);
        mae.getContext().addMessage(LocalizedFormats.EXPONENT, e);

        // Rethrow.
        throw mae;
    }
}
 
Example 18
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    return k.pow(e);
}
 
Example 19
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    return k.pow(e);
}
 
Example 20
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    return k.pow(e);
}