You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/10/27 04:48:57 UTC

[commons-numbers] 01/03: Revert "NUMBERS-138: Square matrix for "Field" elements."

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 4d2129e7a0cd0a0e05177f1d72d16368cd76a3b4
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Oct 27 04:11:23 2019 +0100

    Revert "NUMBERS-138: Square matrix for "Field" elements."
    
    This reverts commit a8aedea0758d155cee66edd56920acd121281e44.
---
 commons-numbers-field/pom.xml                      |   5 -
 .../commons/numbers/field/FieldSquareMatrix.java   | 296 ---------------------
 .../numbers/field/FP64FieldSquareMatrixTest.java   | 196 --------------
 pom.xml                                            |   1 -
 4 files changed, 498 deletions(-)

diff --git a/commons-numbers-field/pom.xml b/commons-numbers-field/pom.xml
index bc40526..32c74fd 100644
--- a/commons-numbers-field/pom.xml
+++ b/commons-numbers-field/pom.xml
@@ -58,11 +58,6 @@
       <type>test-jar</type>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.apache.commons</groupId>
-      <artifactId>commons-math3</artifactId>
-      <scope>test</scope>
-    </dependency>
   </dependencies>
 
 </project>
diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java
deleted file mode 100644
index 8f83088..0000000
--- a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java
+++ /dev/null
@@ -1,296 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.numbers.field;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.ArrayList;
-
-/**
- * Square matrix whose elements define a {@link Field}.
- *
- * @param <T> Type of the field elements.
- */
-public class FieldSquareMatrix<T> {
-    /** Field. */
-    private final Field<T> field;
-    /** Dimension. */
-    private final int dim;
-    /** Data storage (in row-major order). */
-    private final T[] data;
-
-    /**
-     * @param f Field.
-     * @param n Dimension of the matrix.
-     * @throws IllegalArgumentException if {@code n <= 0}.
-     */
-    private FieldSquareMatrix(Field<T> f,
-                              int n) {
-        if (n <= 0) {
-            throw new IllegalArgumentException("Negative size");
-        }
-
-        field = f;
-        dim = n;
-        data = (T[]) new Object[n * n];
-    }
-
-    /**
-     * Factory method.
-     *
-     * @param f Field.
-     * @param n Dimension of the matrix.
-     * @return a new instance.
-     * @throws IllegalArgumentException if {@code n <= 0}.
-     */
-    public static <T> FieldSquareMatrix<T> create(Field<T> f,
-                                                  int n) {
-        return new FieldSquareMatrix<>(f, n);
-    }
-
-    /**
-     * Factory method.
-     *
-     * @param f Field.
-     * @param n Dimension of the matrix.
-     * @return a matrix with elements zet to {@link Field#zero() zero}.
-     * @throws IllegalArgumentException if {@code n <= 0}.
-     */
-    public static <T> FieldSquareMatrix<T> zero(Field<T> f,
-                                                int n) {
-        return create(f, n).fill(f.zero());
-    }
-
-    /**
-     * Factory method.
-     *
-     * @param f Field.
-     * @param n Dimension of the matrix.
-     * @return the identity matrix.
-     * @throws IllegalArgumentException if {@code n <= 0}.
-     */
-    public static <T> FieldSquareMatrix<T> identity(Field<T> f,
-                                                    int n) {
-        final FieldSquareMatrix<T> r = zero(f, n);
-
-        for (int i = 0; i < n; i++) {
-            r.set(i, i, f.one());
-        }
-
-        return r;
-    }
-
-    /**
-     * Copies this matrix.
-     *
-     * @return a new instance.
-     */
-    public FieldSquareMatrix<T> copy() {
-        final FieldSquareMatrix<T> r = create(field, dim);
-        System.arraycopy(data, 0, r.data, 0, data.length);
-        return r;
-    }
-
-    /**
-     * @return the dimension of the matrix.
-     */
-    public int getDimension() {
-        return dim;
-    }
-
-    /**
-     * @return the field associated with the matrix entries.
-     */
-    public Field<T> getField() {
-        return field;
-    }
-
-    /**
-     * Sets all elements to the given value.
-     *
-     * @param value Value of the elements of the matrix.
-     * @return {@code this}.
-     */
-    public FieldSquareMatrix<T> fill(T value) {
-        Arrays.fill(data, value);
-        return this;
-    }
-
-    /**
-     * Gets an element.
-     *
-     * @param i Row.
-     * @param j Column.
-     * @return the element at (i, j).
-     */
-    public T get(int i,
-                 int j) {
-        return data[i * dim + j];
-    }
-
-    /**
-     * Sets an element.
-     *
-     * @param i Row.
-     * @param j Column.
-     * @param value Value.
-     */
-    public void set(int i,
-                    int j,
-                    T value) {
-        data[i * dim + j] = value;
-    }
-
-    /**
-     * Addition.
-     *
-     * @param other Matrix to add.
-     * @return a new instance with the result of the addition.
-     */
-    public FieldSquareMatrix<T> add(FieldSquareMatrix<T> other) {
-        checkDimension(other);
-        final FieldSquareMatrix<T> r = create(field, dim);
-
-        for (int i = 0; i < data.length; i++) {
-            r.data[i] = field.add(data[i], other.data[i]);
-        }
-
-        return r;
-    }
-
-    /**
-     * Subtraction.
-     *
-     * @param other Matrix to subtract.
-     * @return a new instance with the result of the subtraction.
-     */
-    public FieldSquareMatrix<T> subtract(FieldSquareMatrix<T> other) {
-        checkDimension(other);
-        final FieldSquareMatrix<T> r = create(field, dim);
-
-        for (int i = 0; i < data.length; i++) {
-            r.data[i] = field.subtract(data[i], other.data[i]);
-        }
-
-        return r;
-    }
-
-    /**
-     * Negate.
-     *
-     * @return a new instance with the opposite matrix.
-     */
-    public FieldSquareMatrix<T> negate() {
-        final FieldSquareMatrix<T> r = create(field, dim);
-
-        for (int i = 0; i < data.length; i++) {
-            r.data[i] = field.negate(data[i]);
-        }
-
-        return r;
-    }
-
-    /**
-     * Multiplication.
-     *
-     * @param other Matrix to multiply with.
-     * @return a new instance with the result of the multiplication.
-     */
-    public FieldSquareMatrix<T> multiply(FieldSquareMatrix<T> other) {
-        checkDimension(other);
-        final FieldSquareMatrix<T> r = zero(field, dim);
-
-        for (int i = 0; i < dim; i++) {
-            final int o1 = i * dim;
-            for (int j = 0; j < dim; j++) {
-                final int o2 = o1 + j;
-                for (int k = 0; k < dim; k++) {
-                    r.data[o2] = field.add(r.data[o2],
-                                           field.multiply(data[o1 + k],
-                                                          other.data[k * dim + j]));
-                }
-            }
-        }
-
-        return r;
-    }
-
-    /**
-     * Multiplies the matrix with itself {@code p} times.
-     *
-     * @param p Exponent.
-     * @return a new instance.
-     * @throws IllegalArgumentException if {@code p < 0}.
-     */
-    public FieldSquareMatrix<T> pow(int p) {
-        if (p < 0) {
-            throw new IllegalArgumentException("Negative exponent: " + p);
-        }
-
-        if (p == 0) {
-            return identity(field, dim);
-        }
-
-        if (p == 1) {
-            return copy();
-        }
-
-        final int power = p - 1;
-
-        // Only log_2(p) operations are necessary by doing as follows:
-        //    5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2
-        // The same approach is used for A^p.
-
-        final char[] binary = Integer.toBinaryString(power).toCharArray();
-        final ArrayList<Integer> nonZeroPositions = new ArrayList<>();
-
-        for (int i = 0; i < binary.length; i++) {
-            if (binary[i] == '1') {
-                final int pos = binary.length - i - 1;
-                nonZeroPositions.add(pos);
-            }
-        }
-
-        final List<FieldSquareMatrix<T>> results = new ArrayList<>(binary.length);
-        results.add(this);
-        for (int i = 1; i < binary.length; i++) {
-            final FieldSquareMatrix<T> s = results.get(i - 1);
-            final FieldSquareMatrix<T> r = s.multiply(s);
-            results.add(r);
-        }
-
-        FieldSquareMatrix<T> r = this;
-        for (Integer i : nonZeroPositions) {
-            r = r.multiply(results.get(i));
-        }
-
-        return r;
-    }
-
-    /**
-     * Check that the given matrix has the same dimensions.
-     *
-     * @param other Matrix to check.
-     * @throws IllegalArgumentException if the dimensions do not match.
-     */
-    private void checkDimension(FieldSquareMatrix<T> other) {
-        if (dim != other.dim) {
-            throw new IllegalArgumentException("Dimension mismatch: " +
-                                               dim + " != " + other.dim);
-        }
-    }
-}
diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java
deleted file mode 100644
index 9b4cc06..0000000
--- a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.numbers.field;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-import org.apache.commons.math3.linear.RealMatrix;
-import org.apache.commons.math3.linear.Array2DRowRealMatrix;
-import org.apache.commons.math3.util.Pair;
-
-/**
- * Tests for {@link FieldSquareMatrix} (using {@link FP64} as field elements).
- */
-public class FP64FieldSquareMatrixTest {
-    @Test
-    public void testGetDimension() {
-        final int dim = 6;
-        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
-        Assertions.assertEquals(dim, a.getDimension());
-    }
-
-    @Test
-    public void testSetGet() {
-        final int dim = 20;
-        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
-
-        int count = 0;
-        for (int i = 0; i < dim; i++) {
-            for (int j = 0; j < dim; j++) {
-                a.set(i, j, FP64.of(count++));
-            }
-        }
-        Assertions.assertEquals(dim * dim, count);
-
-        count = 0;
-        for (int i = 0; i < dim; i++) {
-            for (int j = 0; j < dim; j++) {
-                Assertions.assertEquals((double) count++,
-                                        a.get(i, j).doubleValue(),
-                                        0d);
-            }
-        }
-    }
-
-    @Test
-    public void testAdd() {
-        final int dim = 6;
-        final double scale = 1e3;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
-
-        assertEquals(p1.getFirst().add(p2.getFirst()),
-                     p1.getSecond().add(p2.getSecond()),
-                     0d);
-    }
-
-    @Test
-    public void testSubtract() {
-        final int dim = 6;
-        final double scale = 1e3;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
-
-        assertEquals(p1.getFirst().subtract(p2.getFirst()),
-                     p1.getSecond().subtract(p2.getSecond()),
-                     0d);
-    }
-
-    @Test
-    public void testMultiply() {
-        final int dim = 7;
-        final double scale = 1e2;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
-
-        assertEquals(p1.getFirst().multiply(p2.getFirst()),
-                     p1.getSecond().multiply(p2.getSecond()),
-                     0d);
-    }
-
-    @Test
-    public void testNegate() {
-        final int dim = 13;
-        final double scale = 1;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
-
-        assertEquals(p.getFirst().negate(),
-                     p.getSecond().scalarMultiply(-1),
-                     0d);
-    }
-
-    @Test
-    public void testPowZero() {
-        final int dim = 5;
-        final double scale = 1e100;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
-
-        final int exp = 0;
-        assertEquals(p.getFirst().pow(exp),
-                     p.getSecond().power(exp),
-                     0d);
-    }
-
-    @Test
-    public void testPowOne() {
-        final int dim = 5;
-        final double scale = 1e100;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
-
-        final int exp = 1;
-        assertEquals(p.getFirst().pow(exp),
-                     p.getSecond().power(exp),
-                     0d);
-    }
-
-    @Test
-    public void testPow() {
-        final int dim = 5;
-        final double scale = 1e2;
-        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
-
-        final int exp = 4;
-        assertEquals(p.getFirst().pow(exp),
-                     p.getSecond().power(exp),
-                     0d);
-    }
-
-    @Test
-    public void testGetField() {
-        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), 7);
-        Assertions.assertEquals(FP64Field.get(), a.getField());
-    }
-
-    /**
-     * Compares with result obtained from "Commons Math".
-     *
-     * @param a "Commons Numbers" result.
-     * @param b "Commons Math" result.
-     * @param tol Tolerance.
-     */
-    private void assertEquals(FieldSquareMatrix<FP64> a,
-                              RealMatrix b,
-                              double tol) {
-        final int dim = a.getDimension();
-        if (dim != b.getRowDimension() ||
-            dim != b.getColumnDimension()) {
-            Assertions.fail("Dimension mismatch"); 
-        }
-
-        for (int i = 0; i < dim; i++) {
-            for (int j = 0; j < dim; j++) {
-                Assertions.assertEquals(a.get(i, j).doubleValue(),
-                                        b.getEntry(i, j),
-                                        tol,
-                                        "(" + i + ", " + j + ")");
-            }
-        }
-    }
-
-    /**
-     * Creates test matrices with random entries.
-     *
-     * @param dim Dimension.
-     * @param scale Range of the entries.
-     * @return a pair of matrices whose entries are in the interval
-     * {@code [-scale, scale]}.
-     */
-    private Pair<FieldSquareMatrix<FP64>, RealMatrix> createRandom(int dim,
-                                                                   double scale) {
-        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
-        final RealMatrix b = new Array2DRowRealMatrix(dim, dim);
-        for (int i = 0; i < dim; i++) {
-            for (int j = 0; j < dim; j++) {
-                final double v = scale * (2 * Math.random() - 1);
-                a.set(i, j, FP64.of(v));
-                b.setEntry(i, j, v);
-            }
-        }
-
-        return new Pair<>(a, b);
-    }
-}
diff --git a/pom.xml b/pom.xml
index 4bb5963..65ce73d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -60,7 +60,6 @@
     <numbers.checkstyle.version>3.0.0</numbers.checkstyle.version>
     <numbers.mathjax.version>2.7.2</numbers.mathjax.version>
     <numbers.junit.bom.version>5.4.2</numbers.junit.bom.version>
-    <numbers.commons.math3.version>3.6.1</numbers.commons.math3.version>
     <!-- Workaround to avoid duplicating config files. -->
     <numbers.parent.dir>${basedir}</numbers.parent.dir>