NUMBERS-51: Field API.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/ec180800 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/ec180800 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/ec180800 Branch: refs/heads/master Commit: ec180800ccec8c7929de2ab402fdc614670471e3 Parents: c098401 Author: Gilles Sadowski <gil...@harfang.homelinux.org> Authored: Tue Dec 26 02:44:26 2017 +0100 Committer: Gilles Sadowski <gil...@harfang.homelinux.org> Committed: Tue Dec 26 02:44:26 2017 +0100 ---------------------------------------------------------------------- commons-numbers-field/pom.xml | 14 ++++ .../commons/numbers/field/AbstractField.java | 69 ++++++++++++++++ .../org/apache/commons/numbers/field/Field.java | 83 ++++++++++++++++++++ pom.xml | 26 +++--- 4 files changed, 180 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/ec180800/commons-numbers-field/pom.xml ---------------------------------------------------------------------- diff --git a/commons-numbers-field/pom.xml b/commons-numbers-field/pom.xml index 1dba08e..061351d 100644 --- a/commons-numbers-field/pom.xml +++ b/commons-numbers-field/pom.xml @@ -42,4 +42,18 @@ <numbers.parent.dir>${basedir}/..</numbers.parent.dir> </properties> + <dependencies> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-core</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-core</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + </dependencies> + </project> http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/ec180800/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/AbstractField.java ---------------------------------------------------------------------- diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/AbstractField.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/AbstractField.java new file mode 100644 index 0000000..b5a4ad0 --- /dev/null +++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/AbstractField.java @@ -0,0 +1,69 @@ +/* + * 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.apache.commons.numbers.core.NativeOperators; + +/** + * Boiler-plate code for concrete implementations of {@link Field}. + * + * @param <T> Type of the field elements. + */ +public abstract class AbstractField<T extends NativeOperators<T>> + implements Field<T> { + /** {@inheritDoc} */ + @Override + public T add(T a, T b) { + return a.add(b); + } + + /** {@inheritDoc} */ + @Override + public T subtract(T a, T b) { + return a.subtract(b); + } + + /** {@inheritDoc} */ + @Override + public T negate(T a) { + return a.negate(); + } + + /** {@inheritDoc} */ + @Override + public T multiply(int n, T a) { + return a.multiply(n); + } + + /** {@inheritDoc} */ + @Override + public T multiply(T a, T b) { + return a.multiply(b); + } + + /** {@inheritDoc} */ + @Override + public T divide(T a, T b) { + return a.divide(b); + } + + /** {@inheritDoc} */ + @Override + public T reciprocal(T a) { + return a.reciprocal(); + } +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/ec180800/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/Field.java ---------------------------------------------------------------------- diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/Field.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/Field.java new file mode 100644 index 0000000..0171811 --- /dev/null +++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/Field.java @@ -0,0 +1,83 @@ +/* + * 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; + +/** + * Interface representing a <a href="http://mathworld.wolfram.com/Field.html">field</a>. + * + * @param <T> Type of the field elements. + */ +public interface Field<T> { + /** + * @param a Field element. + * @param b Field element. + * @return {@code a + b}. + */ + T add(T a, T b); + + /** + * @param a Field element. + * @param b Field element. + * @return {@code a - b}. + */ + T subtract(T a, T b); + + /** + * @param a Field element. + * @return {@code -a}. + */ + T negate(T a); + + /** + * @param a Field element. + * @param n Number of times {@code a} must be added to itself. + * @return {@code n a}. + */ + T multiply(int n, T a); + + /** + * @param a Field element. + * @param b Field element. + * @return {@code a * b}. + */ + T multiply(T a, T b); + + /** + * @param a Field element. + * @param b Field element. + * @return <code>a * b<sup>-1</sup></code>. + */ + T divide(T a, T b); + + /** + * @param a Field element. + * @return <code>a<sup>-1</sup></code>. + */ + T reciprocal(T a); + + /** + * @return the field element {@code 1} such that for all {@code a}, + * {@code 1 * a == a}. + */ + T one(); + + /** + * @return the field element {@code 0} such that for all {@code a}, + * {@code 0 + a == a}. + */ + T zero(); +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/ec180800/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index ba4dc6a..aceccb0 100644 --- a/pom.xml +++ b/pom.xml @@ -74,18 +74,19 @@ <dependencyManagement> <dependencies> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-numbers-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-numbers-core</artifactId> - <version>${project.version}</version> - <type>test-jar</type> - <scope>test</scope> - </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-core</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> @@ -602,6 +603,7 @@ <module>commons-numbers-gamma</module> <module>commons-numbers-combinatorics</module> <module>commons-numbers-arrays</module> + <module>commons-numbers-field</module> </modules> </project>