当前位置: 首页>>代码示例>>Java>>正文


Java Fraction类代码示例

本文整理汇总了Java中org.apache.commons.math3.fraction.Fraction的典型用法代码示例。如果您正苦于以下问题:Java Fraction类的具体用法?Java Fraction怎么用?Java Fraction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Fraction类属于org.apache.commons.math3.fraction包,在下文中一共展示了Fraction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setUp

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Before
public void setUp() throws FractionConversionException {
    javaMap.put(50, new Fraction(100.0));
    javaMap.put(75, new Fraction(75.0));
    javaMap.put(25, new Fraction(500.0));
    javaMap.put(Integer.MAX_VALUE, new Fraction(Integer.MAX_VALUE));
    javaMap.put(0, new Fraction(-1.0));
    javaMap.put(1, new Fraction(0.0));
    javaMap.put(33, new Fraction(-0.1));
    javaMap.put(23234234, new Fraction(-242343.0));
    javaMap.put(23321, new Fraction (Integer.MIN_VALUE));
    javaMap.put(-4444, new Fraction(332.0));
    javaMap.put(-1, new Fraction(-2323.0));
    javaMap.put(Integer.MIN_VALUE, new Fraction(44.0));

    /* Add a few more to cause the table to rehash */
    javaMap.putAll(generate());

}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:20,代码来源:OpenIntToFieldTest.java

示例2: permuteRows

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
/** Returns the result of applying the given row permutation to the matrix */
protected FieldMatrix<Fraction> permuteRows(FieldMatrix<Fraction> matrix, int[] permutation) {
    if (!matrix.isSquare()) {
        throw new NonSquareMatrixException(matrix.getRowDimension(),
                                           matrix.getColumnDimension());
    }
    if (matrix.getRowDimension() != permutation.length) {
        throw new DimensionMismatchException(matrix.getRowDimension(), permutation.length);
    }
    int n = matrix.getRowDimension();
    int m = matrix.getColumnDimension();
    Fraction out[][] = new Fraction[m][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            out[i][j] = matrix.getEntry(permutation[i], j);
        }
    }
    return new Array2DRowFieldMatrix<Fraction>(out);
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:20,代码来源:FieldMatrixImplTest.java

示例3: testRemove2

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testRemove2() {
    OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
    int mapSize = javaMap.size();
    int count = 0;
    Set<Integer> keysInMap = new HashSet<Integer>(javaMap.keySet());
    for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
        keysInMap.remove(mapEntry.getKey());
        map.remove(mapEntry.getKey());
        Assert.assertEquals(--mapSize, map.size());
        Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey())));
        if (count++ > 5)
            break;
    }

    /* Ensure that put and get still work correctly after removals */
    assertPutAndGet(map, mapSize, keysInMap);
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:19,代码来源:OpenIntToFieldTest.java

示例4: testIterator

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testIterator() {
    OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
    OpenIntToFieldHashMap<Fraction>.Iterator iterator = map.iterator();
    for (int i = 0; i < map.size(); ++i) {
        Assert.assertTrue(iterator.hasNext());
        iterator.advance();
        int key = iterator.key();
        Assert.assertTrue(map.containsKey(key));
        Assert.assertEquals(javaMap.get(key), map.get(key));
        Assert.assertEquals(javaMap.get(key), iterator.value());
        Assert.assertTrue(javaMap.containsKey(key));
    }
    Assert.assertFalse(iterator.hasNext());
    try {
        iterator.advance();
        Assert.fail("an exception should have been thrown");
    } catch (NoSuchElementException nsee) {
        // expected
    }
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:22,代码来源:OpenIntToFieldTest.java

示例5: testPutKeysWithCollisions

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
/**
 * Regression test for a bug in findInsertionIndex where the hashing in the second probing
 * loop was inconsistent with the first causing duplicate keys after the right sequence
 * of puts and removes.
 */
@Test
public void testPutKeysWithCollisions() {
    OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<Fraction>(field);
    int key1 = -1996012590;
    Fraction value1 = new Fraction(1);
    map.put(key1, value1);
    int key2 = 835099822;
    map.put(key2, value1);
    int key3 = 1008859686;
    map.put(key3, value1);
    Assert.assertEquals(value1, map.get(key3));
    Assert.assertEquals(3, map.size());

    map.remove(key2);
    Fraction value2 = new Fraction(2);
    map.put(key3, value2);
    Assert.assertEquals(value2, map.get(key3));
    Assert.assertEquals(2, map.size());
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:25,代码来源:OpenIntToFieldTest.java

示例6: testPutKeysWithCollision2

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
/**
 * Similar to testPutKeysWithCollisions() but exercises the codepaths in a slightly
 * different manner.
 */
@Test
public void testPutKeysWithCollision2() {
    OpenIntToFieldHashMap<Fraction>map = new OpenIntToFieldHashMap<Fraction>(field);
    int key1 = 837989881;
    Fraction value1 = new Fraction(1);
    map.put(key1, value1);
    int key2 = 476463321;
    map.put(key2, value1);
    Assert.assertEquals(2, map.size());
    Assert.assertEquals(value1, map.get(key2));

    map.remove(key1);
    Fraction value2 = new Fraction(2);
    map.put(key2, value2);
    Assert.assertEquals(1, map.size());
    Assert.assertEquals(value2, map.get(key2));
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:22,代码来源:OpenIntToFieldTest.java

示例7: testOuterProduct

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testOuterProduct() {
    final SparseFieldVector<Fraction> u
        = new SparseFieldVector<Fraction>(FractionField.getInstance(),
                                          new Fraction[] {new Fraction(1),
                                                          new Fraction(2),
                                                          new Fraction(-3)});
    final SparseFieldVector<Fraction> v
        = new SparseFieldVector<Fraction>(FractionField.getInstance(),
                                          new Fraction[] {new Fraction(4),
                                                          new Fraction(-2)});

    final FieldMatrix<Fraction> uv = u.outerProduct(v);

    final double tol = Math.ulp(1d);
    Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:23,代码来源:SparseFieldVectorTest.java

示例8: testOuterProduct

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testOuterProduct() {
    final ArrayFieldVector<Fraction> u
        = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
                                         new Fraction[] {new Fraction(1),
                                                         new Fraction(2),
                                                         new Fraction(-3)});
    final ArrayFieldVector<Fraction> v
        = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
                                         new Fraction[] {new Fraction(4),
                                                         new Fraction(-2)});

    final FieldMatrix<Fraction> uv = u.outerProduct(v);

    final double tol = Math.ulp(1d);
    Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
    Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
    Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:23,代码来源:ArrayFieldVectorTest.java

示例9: testOperate

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
/** test operate */
@Test
public void testOperate() {
    FieldMatrix<Fraction> m = createSparseMatrix(id);
    assertClose("identity operate", testVector, m.operate(testVector),
            entryTolerance);
    assertClose("identity operate", testVector, m.operate(
            new ArrayFieldVector<Fraction>(testVector)).toArray(), entryTolerance);
    m = createSparseMatrix(bigSingular);
    try {
        m.operate(testVector);
        Assert.fail("Expecting illegalArgumentException");
    } catch (MathIllegalArgumentException ex) {
        // ignored
    }
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:17,代码来源:SparseFieldMatrixTest.java

示例10: testGetSubMatrix

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testGetSubMatrix() {
    FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
    checkGetSubMatrix(m, subRows23Cols00,  2 , 3 , 0, 0);
    checkGetSubMatrix(m, subRows00Cols33,  0 , 0 , 3, 3);
    checkGetSubMatrix(m, subRows01Cols23,  0 , 1 , 2, 3);
    checkGetSubMatrix(m, subRows02Cols13,  new int[] { 0, 2 }, new int[] { 1, 3 });
    checkGetSubMatrix(m, subRows03Cols12,  new int[] { 0, 3 }, new int[] { 1, 2 });
    checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
    checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
    checkGetSubMatrix(m, subRows31Cols31,  new int[] { 3, 1 }, new int[] { 3, 1 });
    checkGetSubMatrix(m, subRows31Cols31,  new int[] { 3, 1 }, new int[] { 3, 1 });
    checkGetSubMatrix(m, null,  1, 0, 2, 4);
    checkGetSubMatrix(m, null, -1, 1, 2, 2);
    checkGetSubMatrix(m, null,  1, 0, 2, 2);
    checkGetSubMatrix(m, null,  1, 0, 2, 4);
    checkGetSubMatrix(m, null, new int[] {},    new int[] { 0 });
    checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 });
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:20,代码来源:BlockFieldMatrixTest.java

示例11: testGetSetMatrixLarge

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testGetSetMatrixLarge() {
    int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
    FieldMatrix<Fraction> m =
        new BlockFieldMatrix<Fraction>(FractionField.getInstance(), n, n);
    FieldMatrix<Fraction> sub =
        new BlockFieldMatrix<Fraction>(FractionField.getInstance(), n - 4, n - 4).scalarAdd(new Fraction(1));

    m.setSubMatrix(sub.getData(), 2, 2);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) {
                Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
            } else {
                Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
            }
        }
    }
    Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3));
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:21,代码来源:BlockFieldMatrixTest.java

示例12: testCopySubMatrix

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testCopySubMatrix() {
    FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
    checkCopy(m, subRows23Cols00,  2 , 3 , 0, 0);
    checkCopy(m, subRows00Cols33,  0 , 0 , 3, 3);
    checkCopy(m, subRows01Cols23,  0 , 1 , 2, 3);
    checkCopy(m, subRows02Cols13,  new int[] { 0, 2 }, new int[] { 1, 3 });
    checkCopy(m, subRows03Cols12,  new int[] { 0, 3 }, new int[] { 1, 2 });
    checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
    checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
    checkCopy(m, subRows31Cols31,  new int[] { 3, 1 }, new int[] { 3, 1 });
    checkCopy(m, subRows31Cols31,  new int[] { 3, 1 }, new int[] { 3, 1 });

    checkCopy(m, null,  1, 0, 2, 4);
    checkCopy(m, null, -1, 1, 2, 2);
    checkCopy(m, null,  1, 0, 2, 2);
    checkCopy(m, null,  1, 0, 2, 4);
    checkCopy(m, null, new int[] {}, new int[] { 0 });
    checkCopy(m, null, new int[] { 0 }, new int[] { 4 });
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:21,代码来源:BlockFieldMatrixTest.java

示例13: testGetSetRowMatrixLarge

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testGetSetRowMatrixLarge() {
    int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
    FieldMatrix<Fraction> m =
        new BlockFieldMatrix<Fraction>(FractionField.getInstance(), n, n);
    FieldMatrix<Fraction> sub =
        new BlockFieldMatrix<Fraction>(FractionField.getInstance(), 1, n).scalarAdd(new Fraction(1));

    m.setRowMatrix(2, sub);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i != 2) {
                Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
            } else {
                Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
            }
        }
    }
    Assert.assertEquals(sub, m.getRowMatrix(2));

}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:22,代码来源:BlockFieldMatrixTest.java

示例14: testGetSetColumnLarge

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
@Test
public void testGetSetColumnLarge() {
    int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
    FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(FractionField.getInstance(), n, n);
    Fraction[] sub = new Fraction[n];
    Arrays.fill(sub, new Fraction(1));

    m.setColumn(2, sub);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (j != 2) {
                Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
            } else {
                Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
            }
        }
    }
    checkArrays(sub, m.getColumn(2));

}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:21,代码来源:BlockFieldMatrixTest.java

示例15: testPremultiplyVector

import org.apache.commons.math3.fraction.Fraction; //导入依赖的package包/类
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
    FieldMatrix<Fraction> m = createSparseMatrix(testData);
    assertClose("premultiply", m.preMultiply(testVector), preMultTest,
        normTolerance);
    assertClose("premultiply", m.preMultiply(
        new ArrayFieldVector<Fraction>(testVector).getData()), preMultTest, normTolerance);
    m = createSparseMatrix(bigSingular);
    try {
        m.preMultiply(testVector);
        Assert.fail("expecting MathIllegalArgumentException");
    } catch (MathIllegalArgumentException ex) {
        // ignored
    }
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:17,代码来源:SparseFieldMatrixTest.java


注:本文中的org.apache.commons.math3.fraction.Fraction类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。