Example #1
0
  @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);
  }
Example #2
0
  @Test
  public void testZeroVectors() {

    // when the field is not specified, array cannot be empty
    try {
      new ArrayFieldVector<Fraction>(new Fraction[0]);
      Assert.fail("MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // expected behavior
    }
    try {
      new ArrayFieldVector<Fraction>(new Fraction[0], true);
      Assert.fail("MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // expected behavior
    }
    try {
      new ArrayFieldVector<Fraction>(new Fraction[0], false);
      Assert.fail("MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // expected behavior
    }

    // when the field is specified, array can be empty
    Assert.assertEquals(
        0,
        new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0])
            .getDimension());
    Assert.assertEquals(
        0,
        new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0], true)
            .getDimension());
    Assert.assertEquals(
        0,
        new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0], false)
            .getDimension());
  }
Example #3
0
  @Test
  public void testConstructors() {

    ArrayFieldVector<Fraction> v0 = new ArrayFieldVector<Fraction>(FractionField.getInstance());
    Assert.assertEquals(0, v0.getDimension());

    ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(FractionField.getInstance(), 7);
    Assert.assertEquals(7, v1.getDimension());
    Assert.assertEquals(new Fraction(0), v1.getEntry(6));

    ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(5, new Fraction(123, 100));
    Assert.assertEquals(5, v2.getDimension());
    Assert.assertEquals(new Fraction(123, 100), v2.getEntry(4));

    ArrayFieldVector<Fraction> v3 =
        new ArrayFieldVector<Fraction>(FractionField.getInstance(), vec1);
    Assert.assertEquals(3, v3.getDimension());
    Assert.assertEquals(new Fraction(2), v3.getEntry(1));

    ArrayFieldVector<Fraction> v4 =
        new ArrayFieldVector<Fraction>(FractionField.getInstance(), vec4, 3, 2);
    Assert.assertEquals(2, v4.getDimension());
    Assert.assertEquals(new Fraction(4), v4.getEntry(0));
    try {
      new ArrayFieldVector<Fraction>(vec4, 8, 3);
      Assert.fail("MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // expected behavior
    }

    FieldVector<Fraction> v5_i = new ArrayFieldVector<Fraction>(dvec1);
    Assert.assertEquals(9, v5_i.getDimension());
    Assert.assertEquals(new Fraction(9), v5_i.getEntry(8));

    ArrayFieldVector<Fraction> v5 = new ArrayFieldVector<Fraction>(dvec1);
    Assert.assertEquals(9, v5.getDimension());
    Assert.assertEquals(new Fraction(9), v5.getEntry(8));

    ArrayFieldVector<Fraction> v6 = new ArrayFieldVector<Fraction>(dvec1, 3, 2);
    Assert.assertEquals(2, v6.getDimension());
    Assert.assertEquals(new Fraction(4), v6.getEntry(0));
    try {
      new ArrayFieldVector<Fraction>(dvec1, 8, 3);
      Assert.fail("MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // expected behavior
    }

    ArrayFieldVector<Fraction> v7 = new ArrayFieldVector<Fraction>(v1);
    Assert.assertEquals(7, v7.getDimension());
    Assert.assertEquals(new Fraction(0), v7.getEntry(6));

    FieldVectorTestImpl<Fraction> v7_i = new FieldVectorTestImpl<Fraction>(vec1);

    ArrayFieldVector<Fraction> v7_2 = new ArrayFieldVector<Fraction>(v7_i);
    Assert.assertEquals(3, v7_2.getDimension());
    Assert.assertEquals(new Fraction(2), v7_2.getEntry(1));

    ArrayFieldVector<Fraction> v8 = new ArrayFieldVector<Fraction>(v1, true);
    Assert.assertEquals(7, v8.getDimension());
    Assert.assertEquals(new Fraction(0), v8.getEntry(6));
    Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef());

    ArrayFieldVector<Fraction> v8_2 = new ArrayFieldVector<Fraction>(v1, false);
    Assert.assertEquals(7, v8_2.getDimension());
    Assert.assertEquals(new Fraction(0), v8_2.getEntry(6));
    Assert.assertArrayEquals(v1.getDataRef(), v8_2.getDataRef());

    ArrayFieldVector<Fraction> v9 = new ArrayFieldVector<Fraction>(v1, v3);
    Assert.assertEquals(10, v9.getDimension());
    Assert.assertEquals(new Fraction(1), v9.getEntry(7));
  }