1   /* Copyright 2002-2015 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.propagation.semianalytical.dsst.utilities.hansen;
18  
19  import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
20  
21  /**
22   * A quadratic matrix of
23   * {@link org.apache.commons.math3.analysis.polynomials.PolynomialFunction}.
24   *
25   * @author Petre Bazavan
26   * @author Lucian Barbulescu
27   */
28  public class PolynomialFunctionMatrix {
29  
30      /** The order of the matrix. */
31      private int order;
32      /** The elements of the matrix. */
33      private PolynomialFunction elements[][];
34  
35      /**
36       * Create a matrix.
37       *
38       * <p>
39       * All elements are null
40       *
41       * @param order
42       *            the order of the matrix
43       */
44      PolynomialFunctionMatrix(final int order) {
45          this.order = order;
46          this.elements = new PolynomialFunction[order][order];
47      }
48  
49      /**
50       * Set an element of the matrix.
51       *
52       * @param line
53       *            the line
54       * @param column
55       *            the column
56       * @param value
57       *            the value
58       */
59      public void setElem(final int line, final int column, final PolynomialFunction value) {
60          elements[line][column] = value;
61      }
62  
63      /**
64       * Get the value of an element.
65       *
66       * @param line
67       *            the line
68       * @param column
69       *            the column
70       * @return the value
71       */
72      public PolynomialFunction getElem(final int line, final int column) {
73          return elements[line][column];
74      }
75  
76      /**
77       * Multiply the argument matrix with the current matrix.
78       *
79       * @param matrix
80       *            the argument matrix
81       * @return the result of the multiplication
82       */
83      public PolynomialFunctionMatrix multiply(final PolynomialFunctionMatrix matrix) {
84          final PolynomialFunctionMatrix result = new PolynomialFunctionMatrix(order);
85          for (int i = 0; i < order; i++) {
86              for (int j = 0; j < order; j++) {
87                  PolynomialFunction cc = HansenUtilities.ZERO;
88                  for (int k = 0; k < order; k++) {
89                      cc = cc.add(matrix.getElem(i, k).multiply(elements[k][j]));
90                  }
91                  result.setElem(i, j, cc);
92              }
93          }
94          return result;
95      }
96  
97      /**
98       * Set values for all elements.
99       *
100      * @param polynomials
101      *            the values that will be used for the matrix
102      */
103     public void setMatrix(final PolynomialFunction[][] polynomials) {
104         elements = polynomials.clone();
105     }
106 
107     /**
108      * Set the value of a line of the matrix.
109      *
110      * @param line
111      *            the line number
112      * @param polynomials
113      *            the values to set
114      */
115     public void setMatrixLine(final int line, final PolynomialFunction[] polynomials) {
116         elements[line] = polynomials;
117     }
118 
119     /**
120      * Get a line of the matrix.
121      *
122      * @param line
123      *            the line number
124      * @return the line of the matrix as a vector
125      */
126     public PolynomialFunction[] getMatrixLine(final int line) {
127         return elements[line].clone();
128     }
129 
130     /**
131      * Add the argument matrix with the current matrix.
132      *
133      * @param matrix
134      *            the argument matrix
135      * @return the result of the addition
136      */
137     public PolynomialFunctionMatrix add(final PolynomialFunctionMatrix matrix) {
138         final PolynomialFunctionMatrix c = new PolynomialFunctionMatrix(order);
139         for (int i = 0; i < order; i++) {
140             for (int j = 0; j < order; j++) {
141                 c.setElem(i, j, elements[i][j].add(matrix.getElem(i, j)));
142             }
143         }
144         return c;
145     }
146 }