View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.linear;
23  
24  import org.hipparchus.Field;
25  import org.hipparchus.FieldElement;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.exception.MathRuntimeException;
28  import org.hipparchus.exception.NullArgumentException;
29  
30  /**
31   * Interface defining a field-valued vector with basic algebraic operations.
32   * <p>
33   * vector element indexing is 0-based -- e.g., <code>getEntry(0)</code>
34   * returns the first element of the vector.
35   * </p>
36   * <p>
37   * The various <code>mapXxx</code> and <code>mapXxxToSelf</code> methods operate
38   * on vectors element-wise, i.e. they perform the same operation (adding a scalar,
39   * applying a function ...) on each element in turn. The <code>mapXxx</code>
40   * versions create a new vector to hold the result and do not change the instance.
41   * The <code>mapXxxToSelf</code> versions use the instance itself to store the
42   * results, so the instance is changed by these methods. In both cases, the result
43   * vector is returned by the methods, this allows to use the <i>fluent API</i>
44   * style, like this:
45   * </p>
46   * <pre>
47   *   RealVector result = v.mapAddToSelf(3.0).mapTanToSelf().mapSquareToSelf();
48   * </pre>
49   * <p>
50   * Note that as almost all operations on {@link FieldElement} throw {@link
51   * NullArgumentException} when operating on a null element, it is the responsibility
52   * of {@code FieldVector} implementations to make sure no null elements
53   * are inserted into the vector. This must be done in all constructors and
54   * all setters.
55   * </p>
56   *
57   * @param <T> the type of the field elements
58   */
59  public interface FieldVector<T extends FieldElement<T>>  {
60  
61      /**
62       * Get the type of field elements of the vector.
63       * @return type of field elements of the vector
64       */
65      Field<T> getField();
66  
67      /**
68       * Returns a (deep) copy of this.
69       * @return vector copy
70       */
71      FieldVector<T> copy();
72  
73      /**
74       * Compute the sum of {@code this} and {@code v}.
75       * @param v vector to be added
76       * @return {@code this + v}
77       * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
78       */
79      FieldVector<T> add(FieldVector<T> v) throws MathIllegalArgumentException;
80  
81      /**
82       * Compute {@code this} minus {@code v}.
83       * @param v vector to be subtracted
84       * @return {@code this - v}
85       * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
86       */
87      FieldVector<T> subtract(FieldVector<T> v) throws MathIllegalArgumentException;
88  
89      /**
90       * Map an addition operation to each entry.
91       * @param d value to be added to each entry
92       * @return {@code this + d}
93       * @throws NullArgumentException if {@code d} is {@code null}.
94       */
95      FieldVector<T> mapAdd(T d) throws NullArgumentException;
96  
97      /**
98       * Map an addition operation to each entry.
99       * <p>The instance <strong>is</strong> changed by this method.</p>
100      * @param d value to be added to each entry
101      * @return for convenience, return {@code this}
102      * @throws NullArgumentException if {@code d} is {@code null}.
103      */
104     FieldVector<T> mapAddToSelf(T d) throws NullArgumentException;
105 
106     /**
107      * Map a subtraction operation to each entry.
108      * @param d value to be subtracted to each entry
109      * @return {@code this - d}
110      * @throws NullArgumentException if {@code d} is {@code null}
111      */
112     FieldVector<T> mapSubtract(T d) throws NullArgumentException;
113 
114     /**
115      * Map a subtraction operation to each entry.
116      * <p>The instance <strong>is</strong> changed by this method.</p>
117      * @param d value to be subtracted to each entry
118      * @return for convenience, return {@code this}
119      * @throws NullArgumentException if {@code d} is {@code null}
120      */
121     FieldVector<T> mapSubtractToSelf(T d) throws NullArgumentException;
122 
123     /**
124      * Map a multiplication operation to each entry.
125      * @param d value to multiply all entries by
126      * @return {@code this * d}
127      * @throws NullArgumentException if {@code d} is {@code null}.
128      */
129     FieldVector<T> mapMultiply(T d) throws NullArgumentException;
130 
131     /**
132      * Map a multiplication operation to each entry.
133      * <p>The instance <strong>is</strong> changed by this method.</p>
134      * @param d value to multiply all entries by
135      * @return for convenience, return {@code this}
136      * @throws NullArgumentException if {@code d} is {@code null}.
137      */
138     FieldVector<T> mapMultiplyToSelf(T d) throws NullArgumentException;
139 
140     /**
141      * Map a division operation to each entry.
142      * @param d value to divide all entries by
143      * @return {@code this / d}
144      * @throws NullArgumentException if {@code d} is {@code null}.
145      * @throws MathRuntimeException if {@code d} is zero.
146      */
147     FieldVector<T> mapDivide(T d)
148         throws NullArgumentException, MathRuntimeException;
149 
150     /**
151      * Map a division operation to each entry.
152      * <p>The instance <strong>is</strong> changed by this method.</p>
153      * @param d value to divide all entries by
154      * @return for convenience, return {@code this}
155      * @throws NullArgumentException if {@code d} is {@code null}.
156      * @throws MathRuntimeException if {@code d} is zero.
157      */
158     FieldVector<T> mapDivideToSelf(T d)
159         throws NullArgumentException, MathRuntimeException;
160 
161     /**
162      * Map the 1/x function to each entry.
163      * @return a vector containing the result of applying the function to each entry.
164      * @throws MathRuntimeException if one of the entries is zero.
165      */
166     FieldVector<T> mapInv() throws MathRuntimeException;
167 
168     /**
169      * Map the 1/x function to each entry.
170      * <p>The instance <strong>is</strong> changed by this method.</p>
171      * @return for convenience, return {@code this}
172      * @throws MathRuntimeException if one of the entries is zero.
173      */
174     FieldVector<T> mapInvToSelf() throws MathRuntimeException;
175 
176     /**
177      * Element-by-element multiplication.
178      * @param v vector by which instance elements must be multiplied
179      * @return a vector containing {@code this[i] * v[i]} for all {@code i}
180      * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
181      */
182     FieldVector<T> ebeMultiply(FieldVector<T> v)
183         throws MathIllegalArgumentException;
184 
185     /**
186      * Element-by-element division.
187      * @param v vector by which instance elements must be divided
188      * @return a vector containing {@code this[i] / v[i]} for all {@code i}
189      * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
190      * @throws MathRuntimeException if one entry of {@code v} is zero.
191      */
192     FieldVector<T> ebeDivide(FieldVector<T> v)
193         throws MathIllegalArgumentException, MathRuntimeException;
194 
195     /**
196      * Compute the dot product.
197      * @param v vector with which dot product should be computed
198      * @return the scalar dot product of {@code this} and {@code v}
199      * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
200      */
201     T dotProduct(FieldVector<T> v) throws MathIllegalArgumentException;
202 
203     /**
204      * Find the orthogonal projection of this vector onto another vector.
205      * @param v vector onto which {@code this} must be projected
206      * @return projection of {@code this} onto {@code v}
207      * @throws MathIllegalArgumentException if {@code v} is not the same size as {@code this}
208      * @throws MathRuntimeException if {@code v} is the null vector.
209      */
210     FieldVector<T> projection(FieldVector<T> v)
211         throws MathIllegalArgumentException, MathRuntimeException;
212 
213     /**
214      * Compute the outer product.
215      * @param v vector with which outer product should be computed
216      * @return the matrix outer product between instance and v
217      */
218     FieldMatrix<T> outerProduct(FieldVector<T> v);
219 
220     /**
221      * Returns the entry in the specified index.
222      *
223      * @param index Index location of entry to be fetched.
224      * @return the vector entry at {@code index}.
225      * @throws MathIllegalArgumentException if the index is not valid.
226      * @see #setEntry(int, FieldElement)
227      */
228     T getEntry(int index) throws MathIllegalArgumentException;
229 
230     /**
231      * Set a single element.
232      * @param index element index.
233      * @param value new value for the element.
234      * @throws MathIllegalArgumentException if the index is not valid.
235      * @see #getEntry(int)
236      */
237     void setEntry(int index, T value) throws MathIllegalArgumentException;
238 
239     /**
240      * Returns the size of the vector.
241      * @return size
242      */
243     int getDimension();
244 
245     /**
246      * Construct a vector by appending a vector to this vector.
247      * @param v vector to append to this one.
248      * @return a new vector
249      */
250     FieldVector<T> append(FieldVector<T> v);
251 
252     /**
253      * Construct a vector by appending a T to this vector.
254      * @param d T to append.
255      * @return a new vector
256      */
257     FieldVector<T> append(T d);
258 
259     /**
260      * Get a subvector from consecutive elements.
261      * @param index index of first element.
262      * @param n number of elements to be retrieved.
263      * @return a vector containing n elements.
264      * @throws MathIllegalArgumentException if the index is not valid.
265      * @throws MathIllegalArgumentException if the number of elements if not positive.
266      */
267     FieldVector<T> getSubVector(int index, int n)
268         throws MathIllegalArgumentException;
269 
270     /**
271      * Set a set of consecutive elements.
272      * @param index index of first element to be set.
273      * @param v vector containing the values to set.
274      * @throws MathIllegalArgumentException if the index is not valid.
275      */
276     void setSubVector(int index, FieldVector<T> v) throws MathIllegalArgumentException;
277 
278     /**
279      * Set all elements to a single value.
280      * @param value single value to set for all elements
281      */
282     void set(T value);
283 
284     /**
285      * Convert the vector to a T array.
286      * <p>The array is independent from vector data, it's elements
287      * are copied.</p>
288      * @return array containing a copy of vector elements
289      */
290     T[] toArray();
291 
292 }