001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.ode;
019
020import org.apache.commons.math3.RealFieldElement;
021
022/** Container for time, main and secondary state vectors as well as their derivatives.
023
024 * @see FirstOrderFieldDifferentialEquations
025 * @see FieldSecondaryEquations
026 * @see FirstOrderFieldIntegrator
027 * @param <T> the type of the field elements
028 * @since 3.6
029 */
030
031public class FieldODEStateAndDerivative<T extends RealFieldElement<T>> extends FieldODEState<T> {
032
033    /** Derivative of the main state at time. */
034    private final T[] derivative;
035
036    /** Derivative of the secondary state at time. */
037    private final T[][] secondaryDerivative;
038
039    /** Simple constructor.
040     * <p>Calling this constructor is equivalent to call {@link
041     * #FieldODEStateAndDerivative(RealFieldElement, RealFieldElement[], RealFieldElement[],
042     * RealFieldElement[][], RealFieldElement[][]) FieldODEStateAndDerivative(time, state,
043     * derivative, null, null)}.</p>
044     * @param time time
045     * @param state state at time
046     * @param derivative derivative of the state at time
047     */
048    public FieldODEStateAndDerivative(T time, T[] state, T[] derivative) {
049        this(time, state, derivative, null, null);
050    }
051
052    /** Simple constructor.
053     * @param time time
054     * @param state state at time
055     * @param derivative derivative of the state at time
056     * @param secondaryState state at time (may be null)
057     * @param secondaryDerivative derivative of the state at time (may be null)
058     */
059    public FieldODEStateAndDerivative(T time, T[] state, T[] derivative, T[][] secondaryState, T[][] secondaryDerivative) {
060        super(time, state, secondaryState);
061        this.derivative          = derivative.clone();
062        this.secondaryDerivative = copy(time.getField(), secondaryDerivative);
063    }
064
065    /** Get derivative of the main state at time.
066     * @return derivative of the main state at time
067     */
068    public T[] getDerivative() {
069        return derivative.clone();
070    }
071
072    /** Get derivative of the secondary state at time.
073     * @param index index of the secondary set as returned
074     * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)}
075     * (beware index 0 corresponds to main state, additional states start at 1)
076     * @return derivative of the secondary state at time
077     */
078    public T[] getSecondaryDerivative(final int index) {
079        return index == 0 ? derivative.clone() : secondaryDerivative[index - 1].clone();
080    }
081
082}