All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hipparchus.util.FieldTuple Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
/*
 * Licensed to the Hipparchus project under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The Hipparchus project 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.hipparchus.util;

import java.util.Arrays;

import org.hipparchus.Field;
import org.hipparchus.FieldElement;
import org.hipparchus.RealFieldElement;
import org.hipparchus.exception.MathIllegalArgumentException;

/**
 * This class allows to perform the same computation of all components of a Tuple at once.
 * @param  the type of the field elements
 * @since 1.2
 */
public class FieldTuple> implements RealFieldElement> {

    /** Components of the tuple. */
    private final T[] values;

    /** Field the instance belongs to. */
    private final transient FieldTupleField field;

    /** Creates a new instance from its components.
     * @param x components of the tuple
     */
    @SafeVarargs
    public FieldTuple(final T... x) {
        this(new FieldTupleField<>(x[0].getField(), x.length), x.clone());
    }

    /** Creates a new instance from its components.
     * @param field field the instance belongs to
     * @param x components of the tuple (beware, it is not copied, it is shared with caller)
     */
    private FieldTuple(final FieldTupleField field, final T[] x) {
        this.values = x;
        this.field  = field;
    }

    /** Get the dimension of the tuple.
     * @return dimension of the tuple
     */
    public int getDimension() {
        return values.length;
    }

    /** Get one component of the tuple.
     * @param index index of the component, between 0 and {@link #getDimension() getDimension()} - 1
     * @return value of the component
     */
    public T getComponent(final int index) {
        return values[index];
    }

    /** Get all components of the tuple.
     * @return all components
     */
    public T[] getComponents() {
        return values.clone();
    }

    /** {@inheritDoc} */
    @Override
    public Field> getField() {
        return field;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple add(final FieldTuple a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].add(a.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple subtract(final FieldTuple a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].subtract(a.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple negate() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].negate();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple multiply(final FieldTuple a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].multiply(a.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple multiply(final int n) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].multiply(n);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple divide(final FieldTuple a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].divide(a.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple reciprocal() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].reciprocal();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof FieldTuple ) {
            @SuppressWarnings("unchecked")
            final FieldTuple that = (FieldTuple) obj;
            if (getDimension() == that.getDimension()) {
                boolean equals = true;
                for (int i = 0; i < values.length; ++i) {
                    equals &= values[i].equals(that.values[i]);
                }
                return equals;
            }
        }
        return false;
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode() {
        return  0x58f61de5 + Arrays.hashCode(values);
    }

    /** {@inheritDoc} */
    @Override
    public double getReal() {
        return values[0].getReal();
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple add(final double a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].add(a);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple subtract(final double a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].subtract(a);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple multiply(final double a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].multiply(a);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple divide(final double a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].divide(a);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple remainder(final double a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].remainder(a);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple remainder(final FieldTuple a) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].remainder(a.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple abs() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].abs();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple ceil() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].ceil();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple floor() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].floor();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple rint() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].rint();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public long round() {
        return values[0].round();
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple signum() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].signum();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple copySign(final FieldTuple sign) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].copySign(sign.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple copySign(final double sign) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].copySign(sign);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple scalb(final int n) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].scalb(n);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple hypot(final FieldTuple y) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].hypot(y.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple sqrt() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].sqrt();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple cbrt() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].cbrt();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple rootN(final int n) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].rootN(n);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple pow(final double p) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].pow(p);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple pow(final int n) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].pow(n);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple pow(final FieldTuple e) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].pow(e.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple exp() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].exp();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple expm1() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].expm1();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple log() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].log();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple log1p() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].log1p();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple log10() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].log10();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple cos() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].cos();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple sin() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].sin();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple tan() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].tan();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple acos() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].acos();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple asin() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].asin();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple atan() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].atan();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple atan2(final FieldTuple x) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].atan2(x.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple cosh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].cosh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple sinh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].sinh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple tanh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].tanh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple acosh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].acosh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple asinh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].asinh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple atanh() {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = values[i].atanh();
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final FieldTuple[] a, final FieldTuple[] b)
        throws MathIllegalArgumentException {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        MathUtils.checkDimension(a.length, b.length);
        final T[] aT = MathArrays.buildArray(values[0].getField(), a.length);
        final T[] bT = MathArrays.buildArray(values[0].getField(), b.length);
        for (int i = 0; i < values.length; ++i) {
            for (int j = 0; j < a.length; ++j) {
                aT[j] = a[j].values[i];
                bT[j] = b[j].values[i];
            }
            result.values[i] = aT[0].linearCombination(aT, bT);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final double[] a, final FieldTuple[] b)
        throws MathIllegalArgumentException {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        MathUtils.checkDimension(a.length, b.length);
        final T[] bT = MathArrays.buildArray(values[0].getField(), b.length);
        for (int i = 0; i < values.length; ++i) {
            for (int j = 0; j < a.length; ++j) {
                bT[j] = b[j].values[i];
            }
            result.values[i] = bT[0].linearCombination(a, bT);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final FieldTuple a1, final FieldTuple b1,
                                           final FieldTuple a2, final FieldTuple b2) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = a1.values[0].linearCombination(a1.values[i], b1.values[i],
                                                              a2.values[i], b2.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final double a1, final FieldTuple b1,
                                           final double a2, final FieldTuple b2) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = b1.values[0].linearCombination(a1, b1.values[i],
                                                              a2, b2.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final FieldTuple a1, final FieldTuple b1,
                                           final FieldTuple a2, final FieldTuple b2,
                                           final FieldTuple a3, final FieldTuple b3) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = a1.values[0].linearCombination(a1.values[i], b1.values[i],
                                                              a2.values[i], b2.values[i],
                                                              a3.values[i], b3.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final double a1, final FieldTuple b1,
                                           final double a2, final FieldTuple b2,
                                           final double a3, final FieldTuple b3) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = b1.values[0].linearCombination(a1, b1.values[i],
                                                              a2, b2.values[i],
                                                              a3, b3.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final FieldTuple a1, final FieldTuple b1,
                                           final FieldTuple a2, final FieldTuple b2,
                                           final FieldTuple a3, final FieldTuple b3,
                                           final FieldTuple a4, final FieldTuple b4) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = a1.values[0].linearCombination(a1.values[i], b1.values[i],
                                                              a2.values[i], b2.values[i],
                                                              a3.values[i], b3.values[i],
                                                              a4.values[i], b4.values[i]);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public FieldTuple linearCombination(final double a1, final FieldTuple b1,
                                           final double a2, final FieldTuple b2,
                                           final double a3, final FieldTuple b3,
                                           final double a4, final FieldTuple b4) {
        final FieldTuple result = new FieldTuple<>(field, MathArrays.buildArray(values[0].getField(), values.length));
        for (int i = 0; i < values.length; ++i) {
            result.values[i] = b1.values[0].linearCombination(a1, b1.values[i],
                                                              a2, b2.values[i],
                                                              a3, b3.values[i],
                                                              a4, b4.values[i]);
        }
        return result;
    }

    /** Field for {link FieldTuple} instances.
     * @param  the type of the field elements
     */
    private static class FieldTupleField> implements Field> {

        /** Constant function evaluating to 0.0. */
        private final FieldTuple zero;

        /** Constant function evaluating to 1.0. */
        private final FieldTuple one;

        /** Simple constructor.
         * @param field field to which the elements belong
         * @param dimension dimension of the tuple
         */
        FieldTupleField(final Field field, final int dimension) {
            final T[] zeroData = MathArrays.buildArray(field, dimension);
            Arrays.fill(zeroData, field.getZero());
            final T[] oneData  = MathArrays.buildArray(field, dimension);
            Arrays.fill(oneData, field.getOne());
            this.zero = new FieldTuple<>(this, zeroData);
            this.one  = new FieldTuple<>(this, oneData);
        }

        /** {@inheritDoc} */
        @Override
        public FieldTuple getZero() {
            return zero;
        }

        /** {@inheritDoc} */
        @Override
        public FieldTuple getOne() {
            return one;
        }

        /** {@inheritDoc} */
        @SuppressWarnings("unchecked")
        @Override
        public Class>> getRuntimeClass() {
            return (Class>>) zero.getClass();
        }

        /** {@inheritDoc} */
        @Override
        public boolean equals(final Object other) {
            if (other instanceof FieldTupleField) {
                @SuppressWarnings("unchecked")
                final FieldTupleField that = (FieldTupleField) other;
                return zero.getDimension() == that.zero.getDimension();
            } else {
                return false;
            }
        }

        /** {@inheritDoc} */
        @Override
        public int hashCode() {
            return 0xb4a533e1 ^ zero.getDimension();
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy