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    
018    package org.apache.commons.math3.ode;
019    
020    import java.io.Serializable;
021    
022    import org.apache.commons.math3.exception.DimensionMismatchException;
023    
024    /**
025     * Class mapping the part of a complete state or derivative that pertains
026     * to a specific differential equation.
027     * <p>
028     * Instances of this class are guaranteed to be immutable.
029     * </p>
030     * @see SecondaryEquations
031     * @version $Id: EquationsMapper.java 1416643 2012-12-03 19:37:14Z tn $
032     * @since 3.0
033     */
034    public class EquationsMapper implements Serializable {
035    
036        /** Serializable UID. */
037        private static final long serialVersionUID = 20110925L;
038    
039        /** Index of the first equation element in complete state arrays. */
040        private final int firstIndex;
041    
042        /** Dimension of the secondary state parameters. */
043        private final int dimension;
044    
045        /** simple constructor.
046         * @param firstIndex index of the first equation element in complete state arrays
047         * @param dimension dimension of the secondary state parameters
048         */
049        public EquationsMapper(final int firstIndex, final int dimension) {
050            this.firstIndex = firstIndex;
051            this.dimension  = dimension;
052        }
053    
054        /** Get the index of the first equation element in complete state arrays.
055         * @return index of the first equation element in complete state arrays
056         */
057        public int getFirstIndex() {
058            return firstIndex;
059        }
060    
061        /** Get the dimension of the secondary state parameters.
062         * @return dimension of the secondary state parameters
063         */
064        public int getDimension() {
065            return dimension;
066        }
067    
068        /** Extract equation data from a complete state or derivative array.
069         * @param complete complete state or derivative array from which
070         * equation data should be retrieved
071         * @param equationData placeholder where to put equation data
072         * @throws DimensionMismatchException if the dimension of the equation data does not
073         * match the mapper dimension
074         */
075        public void extractEquationData(double[] complete, double[] equationData)
076            throws DimensionMismatchException {
077            if (equationData.length != dimension) {
078                throw new DimensionMismatchException(equationData.length, dimension);
079            }
080            System.arraycopy(complete, firstIndex, equationData, 0, dimension);
081        }
082    
083        /** Insert equation data into a complete state or derivative array.
084         * @param equationData equation data to be inserted into the complete array
085         * @param complete placeholder where to put equation data (only the
086         * part corresponding to the equation will be overwritten)
087         * @throws DimensionMismatchException if the dimension of the equation data does not
088         * match the mapper dimension
089         */
090        public void insertEquationData(double[] equationData, double[] complete)
091            throws DimensionMismatchException {
092            if (equationData.length != dimension) {
093                throw new DimensionMismatchException(equationData.length, dimension);
094            }
095            System.arraycopy(equationData, 0, complete, firstIndex, dimension);
096        }
097    
098    }