/// <summary> /// Element-by-element multiplication. /// </summary> /// <param name="v">vector by which instance elements must be multiplied</param> /// <returns>vector containing <c>this[i] * v[i]</c> for all <c>i</c></returns> /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as /// <c>this</c></exception> public ArrayFieldVector <T> ebeMultiply(ArrayFieldVector <T> v) { checkVectorDimensions(v.data.Length); T[] outp = MathArrays.buildArray(field, data.Length); for (int i = 0; i < data.Length; i++) { outp[i] = data[i].multiply(v.data[i]); } return(new ArrayFieldVector <T>(field, outp, false)); }
/// <summary> /// Compute the dot product. /// </summary> /// <param name="v">vector with which dot product should be computed</param> /// <returns>the scalar dot product of <c>this</c> and <c>v</c></returns> /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as /// mo<c>this</c></exception> public T dotProduct(ArrayFieldVector <T> v) { checkVectorDimensions(v.data.Length); T dot = field.getZero(); for (int i = 0; i < data.Length; i++) { dot = dot.add(data[i].multiply(v.data[i])); } return(dot); }
/// <summary> /// Set a set of consecutive elements. /// </summary> /// <param name="index">index of first element to be set.</param> /// <param name="v">vector containing the values to set.</param> /// <exception cref="OutOfRangeException"> if the index is invalid.</exception> public void set(int index, ArrayFieldVector <T> v) { try { Array.Copy(v.data, 0, data, index, v.data.Length); } catch (IndexOutOfRangeException) { checkIndex(index); checkIndex(index + v.data.Length - 1); } }
/// <summary> /// Compute the outer product. /// </summary> /// <param name="v">vector with which outer product should be computed</param> /// <returns>the matrix outer product between instance and v</returns> public FieldMatrix <T> outerProduct(ArrayFieldVector <T> v) { int m = data.Length; int n = v.data.Length; FieldMatrix <T> outp = new Array2DRowFieldMatrix <T>(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { outp.setEntry(i, j, data[i].multiply(v.data[j])); } } return(outp); }
/// <summary> /// Element-by-element division. /// </summary> /// <param name="v">vector by which instance elements must be divided</param> /// <returns>a vector containing <c>this[i] / v[i]</c> for all <c>i</c></returns> /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as /// <c>this</c></exception> /// <exception cref="MathArithmeticException"> if one entry of <c>v</c> is zero. /// </exception> public ArrayFieldVector <T> ebeDivide(ArrayFieldVector <T> v) { checkVectorDimensions(v.data.Length); T[] outp = MathArrays.buildArray(field, data.Length); for (int i = 0; i < data.Length; i++) { try { outp[i] = data[i].divide(v.data[i]); } catch (MathArithmeticException) { throw new MathArithmeticException(new LocalizedFormats("INDEX"), i); } } return(new ArrayFieldVector <T>(field, outp, false)); }
/// <summary> /// Solve the linear equation A × X = B. /// <para>The A matrix is implicit here. It is </para> /// </summary> /// <param name="b">right-hand side of the equation A × X = B</param> /// <returns>a vector X such that A × X = B</returns> /// <exception cref="DimensionMismatchException"> if the matrices dimensions do /// not match.</exception> /// <exception cref="SingularMatrixException"> if the decomposed matrix is singular. /// </exception> public ArrayFieldVector <U> solve(ArrayFieldVector <U> b) { int m = pivot.Length; int length = b.getDimension(); if (length != m) { throw new DimensionMismatchException(length, m); } if (singular) { throw new SingularMatrixException(); } // Apply permutations to b U[] bp = MathArrays.buildArray(field, m); for (int row = 0; row < m; row++) { bp[row] = b.getEntry(pivot[row]); } // Solve LY = b for (int col = 0; col < m; col++) { U bpCol = bp[col]; for (int i = col + 1; i < m; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { bp[col] = bp[col].divide(lu[col][col]); U bpCol = bp[col]; for (int i = 0; i < col; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } return(new ArrayFieldVector <U>(bp, false)); }
/// <inheritdoc/> public FieldVector <T> getSubVector(int index, int n) { if (n < 0) { throw new NotPositiveException <Int32>(new LocalizedFormats("NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE"), n); } ArrayFieldVector <T> outp = new ArrayFieldVector <T>(field, n); try { Array.Copy(data, index, outp.data, 0, n); } catch (IndexOutOfRangeException) { checkIndex(index); checkIndex(index + n - 1); } return(outp); }
/// <summary> /// Construct a vector by appending a vector to this vector. /// </summary> /// <param name="v">vector to append to this one.</param> /// <returns>a new vector</returns> public ArrayFieldVector <T> append(ArrayFieldVector <T> v) { return(new ArrayFieldVector <T>(this, v)); }
/// <summary> /// Find the orthogonal projection of this vector onto another vector. /// </summary> /// <param name="v">vector onto which <c>this</c> must be projected</param> /// <returns>projection of <c>this</c> onto <c>v</c></returns> /// <exception cref="DimensionMismatchException"> if <c>v</c> is not the same size as /// <c>this</c></exception> /// <exception cref="MathArithmeticException"> if <c>v</c> is the null vector.</exception> public ArrayFieldVector <T> projection(ArrayFieldVector <T> v) { return((ArrayFieldVector <T>)v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)))); }
public ArrayFieldVector(T[] v1, ArrayFieldVector <T> v2) : this(v1, (FieldVector <T>)v2) { }
public ArrayFieldVector(ArrayFieldVector <T> v1, T[] v2) : this((FieldVector <T>)v1, v2) { }
public ArrayFieldVector(ArrayFieldVector <T> v1, ArrayFieldVector <T> v2) : this((FieldVector <T>)v1, (FieldVector <T>)v2) { }
/// <summary> /// Construct a vector from another vector. /// </summary> /// <param name="v">Vector to copy.</param> /// <param name="deep">If <c>true</c> perform a deep copy, otherwise perform /// a shallow copy</param> /// <exception cref="NullArgumentException"> if <c>v</c> is <c>null</c>.</exception> public ArrayFieldVector(ArrayFieldVector <T> v, Boolean deep) { MathUtils.checkNotNull(v); field = v.getField(); data = deep ? (T[])v.data.Clone() : v.data; }
/// <summary> /// Construct a vector from another vector, using a deep copy. /// </summary> /// <param name="v">Vector to copy.</param> /// <exception cref="NullArgumentException"> if <c>v</c> is <c>null</c>.</exception> public ArrayFieldVector(ArrayFieldVector <T> v) { MathUtils.checkNotNull(v); field = v.getField(); data = (T[])v.data.Clone(); }