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.Field; 021import org.apache.commons.math3.RealFieldElement; 022import org.apache.commons.math3.util.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 091 /** Get time. 092 * @return time 093 */ 094 public T getTime() { 095 return time; 096 } 097 098 /** Get main state dimension. 099 * @return main state dimension 100 */ 101 public int getStateDimension() { 102 return state.length; 103 } 104 105 /** Get main state at time. 106 * @return main state at time 107 */ 108 public T[] getState() { 109 return state.clone(); 110 } 111 112 /** Get the number of secondary states. 113 * @return number of secondary states. 114 */ 115 public int getNumberOfSecondaryStates() { 116 return secondaryState == null ? 0 : secondaryState.length; 117 } 118 119 /** Get secondary state dimension. 120 * @param index index of the secondary set as returned 121 * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)} 122 * (beware index 0 corresponds to main state, additional states start at 1) 123 * @return secondary state dimension 124 */ 125 public int getSecondaryStateDimension(final int index) { 126 return index == 0 ? state.length : secondaryState[index - 1].length; 127 } 128 129 /** Get secondary state at time. 130 * @param index index of the secondary set as returned 131 * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryEquations)} 132 * (beware index 0 corresponds to main state, additional states start at 1) 133 * @return secondary state at time 134 */ 135 public T[] getSecondaryState(final int index) { 136 return index == 0 ? state.clone() : secondaryState[index - 1].clone(); 137 } 138 139}