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.math4.legacy.ode; 019 020import org.apache.commons.math4.legacy.core.Field; 021import org.apache.commons.math4.legacy.core.RealFieldElement; 022import org.apache.commons.math4.legacy.core.MathArrays; 023 024/** Container for time, main and secondary state vectors. 025 026 * @see FirstOrderFieldDifferentialEquations 027 * @see FieldSecondaryEquations 028 * @see FirstOrderFieldIntegrator 029 * @see FieldODEStateAndDerivative 030 * @param <T> the type of the field elements 031 * @since 3.6 032 */ 033 034public class FieldODEState<T extends RealFieldElement<T>> { 035 036 /** Time. */ 037 private final T time; 038 039 /** Main state at time. */ 040 private final T[] state; 041 042 /** Secondary state at time. */ 043 private final T[][] secondaryState; 044 045 /** Simple constructor. 046 * <p>Calling this constructor is equivalent to call {@link 047 * #FieldODEState(RealFieldElement, RealFieldElement[], RealFieldElement[][]) 048 * FieldODEState(time, state, null)}.</p> 049 * @param time time 050 * @param state state at time 051 */ 052 public FieldODEState(T time, T[] state) { 053 this(time, state, null); 054 } 055 056 /** Simple constructor. 057 * @param time time 058 * @param state state at time 059 * @param secondaryState state at time (may be null) 060 */ 061 public FieldODEState(T time, T[] state, T[][] secondaryState) { 062 this.time = time; 063 this.state = state.clone(); 064 this.secondaryState = copy(time.getField(), secondaryState); 065 } 066 067 /** Copy a two-dimensions array. 068 * @param field field to which elements belong 069 * @param original original array (may be null) 070 * @return copied array or null if original array was null 071 */ 072 protected T[][] copy(final Field<T> field, final T[][] original) { 073 074 // special handling of null arrays 075 if (original == null) { 076 return null; 077 } 078 079 // allocate the array 080 final T[][] copied = MathArrays.buildArray(field, original.length, -1); 081 082 // copy content 083 for (int i = 0; i < original.length; ++i) { 084 copied[i] = original[i].clone(); 085 } 086 087 return copied; 088 } 089 090 /** Get time. 091 * @return time 092 */ 093 public T getTime() { 094 return time; 095 } 096 097 /** Get main state dimension. 098 * @return main state dimension 099 */ 100 public int getStateDimension() { 101 return state.length; 102 } 103 104 /** Get main state at time. 105 * @return main state at time 106 */ 107 public T[] getState() { 108 return state.clone(); 109 } 110 111 /** Get the number of secondary states. 112 * @return number of secondary states. 113 */ 114 public int getNumberOfSecondaryStates() { 115 return secondaryState == null ? 0 : secondaryState.length; 116 } 117 118 /** Get secondary state dimension. 119 * @param index index of the secondary set as returned 120 * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)} 121 * (beware index 0 corresponds to main state, additional states start at 1) 122 * @return secondary state dimension 123 */ 124 public int getSecondaryStateDimension(final int index) { 125 return index == 0 ? state.length : secondaryState[index - 1].length; 126 } 127 128 /** Get secondary state at time. 129 * @param index index of the secondary set as returned 130 * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)} 131 * (beware index 0 corresponds to main state, additional states start at 1) 132 * @return secondary state at time 133 */ 134 public T[] getSecondaryState(final int index) { 135 return index == 0 ? state.clone() : secondaryState[index - 1].clone(); 136 } 137}