Search in sources :

Example 1 with Fraction

use of org.apache.sis.math.Fraction in project sis by apache.

the class LinearConverterTest method assertScale.

/**
 * Asserts that the given converter is a linear converter with the given scale factor and no offset.
 * The scale factor is given by the ratio of the given numerator and denominator.
 *
 * @param  numerator    the expected numerator in the conversion factor.
 * @param  denominator  the expected denominator in the conversion factor.
 * @param  converter    the converter to verify.
 */
private static void assertScale(final int numerator, final int denominator, final LinearConverter converter) {
    final double derivative = numerator / (double) denominator;
    final Number[] coefficients = converter.coefficients();
    assertEquals("coefficients.length", 2, coefficients.length);
    assertEquals("offset", 0, coefficients[0].doubleValue(), STRICT);
    assertEquals("scale", derivative, coefficients[1].doubleValue(), STRICT);
    if (denominator != 1) {
        assertInstanceOf("coefficients[1]", Fraction.class, coefficients[1]);
        final Fraction f = (Fraction) coefficients[1];
        assertEquals("numerator", numerator, f.numerator);
        assertEquals("denominator", denominator, f.denominator);
    }
    assertEquals("derivative", derivative, converter.derivative(0), STRICT);
}
Also used : Fraction(org.apache.sis.math.Fraction)

Example 2 with Fraction

use of org.apache.sis.math.Fraction in project sis by apache.

the class UnitDimensionTest method testRationalPower.

/**
 * Tests a dimension with rational power. This tests use the specific detectivity, which dimension is T^2.5 / (M⋅L).
 */
@Test
@DependsOnMethod({ "testMultiply", "testDivide", "testPow", "testRoot" })
public void testRationalPower() {
    final Dimension dim = specificDetectivity();
    final Map<Dimension, Fraction> expected = new HashMap<>(4);
    assertNull(expected.put(TIME, new Fraction(5, 2)));
    assertNull(expected.put(MASS, new Fraction(-1, 1)));
    assertNull(expected.put(LENGTH, new Fraction(-1, 1)));
    assertMapEquals(expected, ((UnitDimension) dim).components);
    try {
        dim.getBaseDimensions().toString();
        fail("Mapping from Fraction to Integer should not be allowed.");
    } catch (UnconvertibleObjectException e) {
        final String message = e.getMessage();
        assertTrue(message, message.contains("Integer"));
    }
// 'toString()' formatting tested in UnitFormatTest.testRationalPower().
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) HashMap(java.util.HashMap) Fraction(org.apache.sis.math.Fraction) Dimension(javax.measure.Dimension) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 3 with Fraction

use of org.apache.sis.math.Fraction in project sis by apache.

the class UnitFormat method formatComponent.

/**
 * Formats a single unit or dimension raised to the given power.
 *
 * @param  entry    the base unit or base dimension to format, together with its power.
 * @param  inverse  {@code true} for inverting the power sign.
 * @param  style    whether to allow Unicode characters.
 */
private static void formatComponent(final Map.Entry<?, ? extends Number> entry, final boolean inverse, final Style style, final Appendable toAppendTo) throws IOException {
    formatSymbol(entry.getKey(), style, toAppendTo);
    final Number power = entry.getValue();
    int n;
    if (power instanceof Fraction) {
        Fraction f = (Fraction) power;
        if (f.denominator != 1) {
            if (inverse) {
                f = f.negate();
            }
            style.appendPower(toAppendTo, f);
            return;
        }
        n = f.numerator;
    } else {
        n = power.intValue();
    }
    if (inverse)
        n = -n;
    if (n != 1) {
        style.appendPower(toAppendTo, n);
    }
}
Also used : Fraction(org.apache.sis.math.Fraction)

Example 4 with Fraction

use of org.apache.sis.math.Fraction in project sis by apache.

the class UnitDimension method combine.

/**
 * Returns the product or the quotient of this dimension with the specified one.
 *
 * @param  other   the dimension by which to multiply or divide this dimension.
 * @param  divide  {@code false} for a multiplication, {@code true} for a division.
 * @return the product or division of this dimension by the given dimension.
 */
private UnitDimension combine(final Dimension other, final boolean divide) {
    final Map<UnitDimension, Fraction> product = new LinkedHashMap<>(components);
    for (final Map.Entry<? extends Dimension, Fraction> entry : getBaseDimensions(other).entrySet()) {
        final Dimension dim = entry.getKey();
        Fraction p = entry.getValue();
        if (divide) {
            p = p.negate();
        }
        if (dim instanceof UnitDimension) {
            product.merge((UnitDimension) dim, p, (sum, toAdd) -> {
                sum = sum.add(toAdd);
                return (sum.numerator != 0) ? sum : null;
            });
        } else if (p.numerator != 0) {
            throw new UnsupportedImplementationException(Errors.format(Errors.Keys.UnsupportedImplementation_1, dim.getClass()));
        }
    }
    return create(product);
}
Also used : Fraction(org.apache.sis.math.Fraction) Dimension(javax.measure.Dimension) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) UnsupportedImplementationException(org.apache.sis.util.UnsupportedImplementationException)

Aggregations

Fraction (org.apache.sis.math.Fraction)4 Dimension (javax.measure.Dimension)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 UnsupportedImplementationException (org.apache.sis.util.UnsupportedImplementationException)1 Test (org.junit.Test)1