@Override public void setConcurrency(int concurrency) { // The setting of the concurrency can only reduce the number // of threads available in the context. int nbThreads = MathLib.min(parent.threads.length, concurrency); threads = new ConcurrentThreadImpl[nbThreads]; for (int i = 0; i < nbThreads; i++) { // Reused from parent threads. threads[i] = parent.threads[i]; } }
/** * Returns the arc sine of the specified value, * in the range of -<i>pi</i>/2 through <i>pi</i>/2. * * @param x the value whose arc sine is to be returned. * @return the arc sine in radians for the specified value. **/ public static double asin(double x) { if (x < -1.0 || x > 1.0) return MathLib.NaN; if (x == -1.0) return -HALF_PI; if (x == 1.0) return HALF_PI; return MathLib.atan(x / MathLib.sqrt(1.0 - x * x)); }
/** * Returns the remainder of the division of the specified two arguments. * * @param x the dividend. * @param y the divisor. * @return <code>x - round(x / y) * y</code> **/ public static double rem(double x, double y) { double tmp = x / y; if (MathLib.abs(tmp) <= Long.MAX_VALUE) return x - MathLib.round(tmp) * y; else return NaN; }
/** * Returns the value of the first argument raised to the power of the * second argument. * * @param x the base. * @param y the exponent. * @return <code>x<sup>y</sup></code> **/ public static double pow(double x, double y) { // Use close approximation (+/- LSB) if ((x < 0) && (y == (int) y)) return (((int) y) & 1) == 0 ? pow(-x, y) : -pow(-x, y); return MathLib.exp(y * MathLib.log(x)); }
int log10Value = (int) MathLib.floor(MathLib.log10(MathLib .abs(value))); int log10Error = (int) MathLib.floor(MathLib.log10(error)); int digits = log10Value - log10Error - 1; // Exact digits. digits = MathLib.max(1, digits + _errorDigits); boolean scientific = (MathLib.abs(value) >= 1E6) || (MathLib.abs(value) < 1E-6); boolean showZeros = false; arg1.append('('); TypeFormat.format(value, digits, scientific, showZeros, arg1); arg1.append(" ± "); scientific = (MathLib.abs(error) >= 1E6) || (MathLib.abs(error) < 1E-6); showZeros = true; TypeFormat.format(error, _errorDigits, scientific, showZeros, arg1);
return 0.0; if (m == Long.MIN_VALUE) return toDoublePow2(Long.MIN_VALUE >> 1, n + 1); if (m < 0) return -toDoublePow2(-m, n); int bitLength = MathLib.bitLength(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow. if (exp <= -54) return 0.0; return toDoublePow2(m, n + 54) / 18014398509481984L; // 2^54 Exact.
return 0.0; if (m == Long.MIN_VALUE) return toDoublePow10(Long.MIN_VALUE / 10, n + 1); if (m < 0) return -toDoublePow10(-m, n); if (n >= 0) { // Positive power. if (n > 308) int shift = 31 - MathLib.bitLength(x3); // -1..30 pow2 -= shift; long mantissa = (shift < 0) ? (x3 << 31) | (x2 >>> 1) : // x3 is 32 bits. (((x3 << 32) | x2) << shift) | (x1 >>> (32 - shift)); return toDoublePow2(mantissa, pow2); int shift = 63 - MathLib.bitLength(x1); x1 <<= shift; x1 |= x0 >>> (63 - shift); pow2 -= i; return toDoublePow2(x1, pow2);
/** * Returns this complex raised to the power of the specified complex * exponent. * * @param that the exponent. * @return <code>this**that</code>. */ public Complex pow(Complex that) { Complex c = FACTORY.object(); double r1 = MathLib.log(this.magnitude()); double i1 = this.argument(); double r2 = (r1 * that._real) - (i1 * that._imaginary); double i2 = (r1 * that._imaginary) + (i1 * that._real); double m = MathLib.exp(r2); c._real = m * MathLib.cos(i2); c._imaginary = m * MathLib.sin(i2); return c; }
/** * Set the {@link LocalContext local} concurrency. Concurrency is * hard limited by {@link #MAXIMUM_CONCURRENCY}. * * @param concurrency the new concurrency (<code>0</code> or negative * number to disable concurrency). */ public static void setConcurrency(int concurrency) { concurrency = MathLib.max(0, concurrency); concurrency = MathLib.min(((Integer) MAXIMUM_CONCURRENCY.get()).intValue(), concurrency); CONCURRENCY.set(new Integer(concurrency)); }
/** * Returns the root mean square value of the given inputs over the given * range. * * @param lowerFreqHz * >= 1 * @param upperFreqHz * < {@link #getUpperMeasurableFrequency()} * @param fftd * powers * * @return the rms value */ public double rms(int lowerFreqHz, int upperFreqHz, double... fftd) { assert fftd != null && fftd.length > 0; assert lowerFreqHz >= 1 && lowerFreqHz < upperFreqHz && upperFreqHz < getUpperMeasurableFrequency(); int divisor = fftd.length; double sum = 0; for (int i = lowerFreqHz; i <= upperFreqHz; i++) { sum += MathLib.pow(fftd[i], 2); } return MathLib.sqrt(new BigDecimal(sum).divide(new BigDecimal(divisor), 10, RoundingMode.HALF_UP).doubleValue()); }
private double getMax(double[] fftd, int lowerCutoffHz, int upperCutoffHz) { double max = Double.MIN_VALUE; for (int i = lowerCutoffHz; i <= upperCutoffHz; i++) { max = MathLib.max(max, fftd[i]); } return max; }
/** * Returns the closest <code>long</code> to the specified argument. * * @param d the <code>double</code> value to be rounded to a * <code>long</code> * @return the nearest <code>long</code> value. **/ public static long round(double d) { return (long) floor(d + 0.5d); } /**/
@SuppressWarnings("unchecked") @Override public Appendable format(Amount arg0, Appendable arg1) throws IOException { if (arg0.getUnit() instanceof Currency) return formatMoney(arg0, arg1); if (arg0.isExact()) { TypeFormat.format(arg0.getExactValue(), arg1); arg1.append(' '); return UnitFormat.getInstance().format(arg0.getUnit(), arg1); } double value = arg0.getEstimatedValue(); double error = arg0.getAbsoluteError(); int log10Value = (int) MathLib.floor(MathLib.log10(MathLib .abs(value))); int log10Error = (int) MathLib.floor(MathLib.log10(error)); int digits = log10Value - log10Error - 1; // Exact digits. boolean scientific = (MathLib.abs(value) >= 1E6) || (MathLib.abs(value) < 1E-6); boolean showZeros = true; TypeFormat.format(value, digits, scientific, showZeros, arg1); arg1.append(' '); return UnitFormat.getInstance().format(arg0.getUnit(), arg1); }
/** * Returns the decimal logarithm of the specified value. * * @param x the value greater than <code>0.0</code>. * @return the value y such as <code>10<sup>y</sup> == x</code> **/ public static double log10(double x) { return log(x) * INV_LOG10; } private static double INV_LOG10 = 0.43429448190325182765112891891661;
/** * Returns the magnitude of this complex number, also referred to * as the "modulus" or "length". * * @return the magnitude of this complex number. */ public double magnitude() { return MathLib.sqrt(_real * _real + _imaginary * _imaginary); }
/** * Indicates if this number is a power of two (equals to 2<sup> * ({@link #bitLength bitLength()} - 1)</sup>). * * @return <code>true</code> if this number is a power of two; * <code>false</code> otherwise. */ public boolean isPowerOfTwo() { if (_size == 0) return false; final int n = _size - 1; for (int j = 0; j < n; j++) { if (_words[j] != 0) return false; } final int bitLength = MathLib.bitLength(_words[n]); return _words[n] == (1L << (bitLength - 1)); }
/** * Returns one of the two square root of this complex number. * * @return <code>sqrt(this)</code>. */ public Complex sqrt() { Complex c = FACTORY.object(); double m = MathLib.sqrt(this.magnitude()); double a = this.argument() / 2.0; c._real = m * MathLib.cos(a); c._imaginary = m * MathLib.sin(a); return c; }
/** * Returns this complex raised to the specified power. * * @param e the exponent. * @return <code>this**e</code>. */ public Complex pow(double e) { Complex c = FACTORY.object(); double m = MathLib.pow(this.magnitude(), e); double a = this.argument() * e; c._real = m * MathLib.cos(a); c._imaginary = m * MathLib.sin(a); return c; }