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