/// <summary> /// Linear algebraic matrix-vector multiplication; <tt>z = alpha * A * y + beta*z</tt>. /// </summary> /// <param name="y"> /// The source vector. /// </param> /// <param name="z"> /// The vector where results are to be storedd Set this parameter to <tt>null</tt> to indicate that a new result vector shall be constructed. /// </param> /// <param name="alpha"> /// The alpha. /// </param> /// <param name="beta"> /// The beta. /// </param> /// <param name="transposeA"> /// Whether A must be transposed. /// </param> /// <returns> /// z (for convenience only). /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>A.columns() != y.Count || A.rows() > z.Count)</tt>. /// </exception> public virtual DoubleMatrix1D ZMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, bool transposeA) { if (transposeA) { return(ViewDice().ZMult(y, z, alpha, beta, false)); } if (z == null) { z = new DenseDoubleMatrix1D(Rows); } if (Columns != y.Size || Rows > z.Size) { throw new ArgumentOutOfRangeException("Incompatible args: " + this + ", " + y + ", " + z); } for (int i = Rows; --i >= 0;) { double s = 0; for (int j = Columns; --j >= 0;) { s += this[i, j] * y[j]; } z[i] = (alpha * s) + (beta * z[i]); } return(z); }
/// <summary> /// Construct a matrix which is the concatenation of all given parts. /// Cells are copied. /// </summary> /// <param name="parts"> /// The parts. /// </param> /// <returns> /// A matrix. /// </returns> public DoubleMatrix1D Make(DoubleMatrix1D[] parts) { if (parts.Length == 0) { return(Make(0)); } int size = 0; for (int i = 0; i < parts.Length; i++) { size += parts[i].Size; } DoubleMatrix1D vector = Make(size); size = 0; for (int i = 0; i < parts.Length; i++) { vector.ViewPart(size, parts[i].Size).Assign(parts[i]); size += parts[i].Size; } return(vector); }
/// <summary> /// Constructs and returns a deep copy of the receiver. /// </summary> /// <returns> /// A deep copy of the receiver. /// </returns> public DoubleMatrix1D Copy() { DoubleMatrix1D copy = Like(); copy.Assign(this); return(copy); }
/// <summary> /// Returns the dot product of two vectors x and y, which is <tt>Sum(x[i]*y[i])</tt>, /// where <tt>x == this</tt>. /// Operates on cells at indexes <tt>from .. Min(size(),y.size(),from+length)-1</tt>. /// </summary> /// <param name="y"> /// The second vector. /// </param> /// <param name="from"> /// The first index to be considered. /// </param> /// <param name="length"> /// The number of cells to be considered. /// </param> /// <returns> /// The sum of products; zero if <tt>from < 0 || length < 0</tt>. /// </returns> public virtual double ZDotProduct(DoubleMatrix1D y, int from, int length) { if (from < 0 || length <= 0) { return(0); } int tail = from + length; if (Size < tail) { tail = Size; } if (y.Size < tail) { tail = y.Size; } length = tail - from; double sum = 0; int i = tail - 1; for (int k = length; --k >= 0; i--) { sum += this[i] * y[i]; } return(sum); }
public DoubleMatrix1D Assign(DoubleMatrix1D y, Cern.Colt.Function.DoubleDoubleFunction function, IntArrayList nonZeroIndexes) { CheckSize(y); int[] nonZeroElements = nonZeroIndexes.ToArray(); // specialized for speed if (function == Cern.Jet.Math.Functions.DoubleDoubleFunctions.Mult) { // x[i] = x[i] * y[i] int j = 0; for (int index = nonZeroIndexes.Count; --index >= 0;) { int i = nonZeroElements[index]; for (; j < i; j++) { this[j] = 0; // x[i] = 0 for all zeros } this[i] = this[i] * y[i]; // x[i] * y[i] for all nonZeros j++; } } //else if (function is Cern.Jet.Math.PlusMult.Apply) //{ //} else { // the general case x[i] = f(x[i],y[i]) return(Assign(y, function)); } return(this); }
/// <summary> /// Assigns the result of a function to each cell; <tt>x[i] = function(x[i],y[i])</tt>. /// </summary> /// <param name="y"> /// The secondary matrix to operate on. /// </param> /// <param name="function"> /// A function taking as first argument the current cell's value of <tt>this</tt>, /// and as second argument the current cell's value of <tt>y</tt>. /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>size() != y.size()</tt>. /// </exception> public virtual DoubleMatrix1D Assign(DoubleMatrix1D y, DoubleDoubleFunction function) { CheckSize(y); for (int i = Size; --i >= 0;) { this[i] = function(this[i], y[i]); } return(this); }
/// <summary> /// C = A||B; Constructs a new matrix which is the concatenation of two other matrices. /// </summary> /// <param name="a"> /// The matrix A. /// </param> /// <param name="b"> /// The matrix B. /// </param> /// <returns> /// The concatenation of A and B. /// </returns> public DoubleMatrix1D AppendColumns(DoubleMatrix1D a, DoubleMatrix1D b) { // concatenate DoubleMatrix1D matrix = Make(a.Size + b.Size); matrix.ViewPart(0, a.Size).Assign(a); matrix.ViewPart(a.Size, b.Size).Assign(b); return(matrix); }
/// <summary> /// Swaps each element <tt>this[i]</tt> with <tt>other[i]</tt>. /// </summary> /// <param name="other"> /// The other matrix. /// </param> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>size() != other.size()</tt>. /// </exception> public virtual void Swap(DoubleMatrix1D other) { CheckSize(other); for (int i = Size; --i >= 0;) { double tmp = this[i]; this[i] = other[i]; other[i] = tmp; } }
/// <summary> /// Constructs a list from the given matrix. /// The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the list, and vice-versa. /// </summary> /// <param name="values"> /// The values to be filled into the new list. /// </param> /// <returns> /// A new list. /// </returns> public List <double> ToList(DoubleMatrix1D values) { int size = values.Size; var list = new List <double>(size); for (int i = size; --i >= 0;) { list.Add(values[i]); } return(list); }
/// <summary> /// Constructs a matrix from the values of the given list. /// The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa. /// </summary> /// <param name="values"> /// The values to be filled into the new matrix. /// </param> /// <returns> /// A new matrix. /// </returns> public DoubleMatrix1D Make(IList <double> values) { int size = values.Count; DoubleMatrix1D vector = Make(size); for (int i = size; --i >= 0;) { vector[i] = values[i]; } return(vector); }
/// <summary> /// Constructs a new diagonal matrix whose diagonal elements are the elements of <tt>vector</tt>. /// Cells values are copied. The new matrix is not a view. /// </summary> /// <param name="vector"> /// The vector. /// </param> /// <returns> /// A new matrix. /// </returns> public DoubleMatrix2D Diagonal(DoubleMatrix1D vector) { int size = vector.Size; DoubleMatrix2D diag = Make(size, size); for (int i = size; --i >= 0;) { diag[i, i] = vector[i]; } return(diag); }
/// <summary> /// Constructs a new vector consisting of the diagonal elements of <tt>A</tt>. /// Cells values are copied. The new vector is not a view. /// </summary> /// <param name="a"> /// The amatrix, need not be square. /// </param> /// <returns> /// A new vector. /// </returns> public DoubleMatrix1D Diagonal(DoubleMatrix2D a) { int min = Math.Min(a.Rows, a.Columns); DoubleMatrix1D diag = make1D(min); for (int i = min; --i >= 0;) { diag[i] = a[i, i]; } return(diag); }
/// <summary> /// Constructs a matrix with cells having descending values. /// For debugging purposes. /// </summary> /// <param name="size"> /// The size. /// </param> /// <returns> /// A matrix with cells having descending values. /// </returns> public DoubleMatrix1D Descending(int size) { DoubleMatrix1D matrix = Make(size); int v = 0; for (int i = size; --i >= 0;) { matrix[size] = v++; } return(matrix); }
/// <summary> /// Returns <tt>true</tt> if both matrices share at least one identical cell. /// </summary> /// <param name="other"> /// The other matrix. /// </param> /// <returns> /// <tt>true</tt> if both matrices share at least one identical cell. /// </returns> protected virtual bool HaveSharedCells(DoubleMatrix1D other) { if (other == null) { return(false); } if (this == other) { return(true); } return(GetContent().HaveSharedCellsRaw(other.GetContent())); }
/// <summary> /// Constructs a new matrix which is A duplicated <tt>repeat</tt> times. /// </summary> /// <param name="a"> /// The matrix to duplicate. /// </param> /// <param name="repeat"> /// The number of repetitions. /// </param> /// <returns> /// A matrix. /// </returns> public DoubleMatrix1D Repeat(DoubleMatrix1D a, int repeat) { int size = a.Size; DoubleMatrix1D matrix = Make(repeat * size); for (int i = repeat; --i >= 0;) { matrix.ViewPart(size * i, size).Assign(a); } return(matrix); }
/// <summary> /// Applies a function to each corresponding cell of two matrices and aggregates the results. /// </summary> /// <param name="other"> /// The other matrix. /// </param> /// <param name="aggr"> /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell values. /// </param> /// <param name="f"> /// A function transforming the current cell values. /// </param> /// <returns> /// The aggregated measure. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>size() != other.size()</tt>. /// </exception> public double Aggregate(DoubleMatrix1D other, DoubleDoubleFunction aggr, DoubleDoubleFunction f) { CheckSize(other); if (Size == 0) { return(double.NaN); } double a = f(this[Size - 1], other[Size - 1]); for (int i = Size - 1; --i >= 0;) { a = aggr(a, f(this[i], other[i])); } return(a); }
/// <summary> /// Returns the dot product of two vectors x and y, which is <tt>Sum(x[i]*y[i])</tt>, /// where <tt>x == this</tt>. /// </summary> /// <param name="y"> /// The second vector. /// </param> /// <param name="from"> /// The from. /// </param> /// <param name="length"> /// The length. /// </param> /// <param name="nonZeroIndexes"> /// The indexes of cells in <tt>y</tt>having a non-zero value. /// </param> /// <returns> /// The sum of products. /// </returns> public virtual double ZDotProduct(DoubleMatrix1D y, int from, int length, IntArrayList nonZeroIndexes) { // determine minimum length if (from < 0 || length <= 0) { return(0); } int tail = from + length; if (Size < tail) { tail = Size; } if (y.Size < tail) { tail = y.Size; } length = tail - from; if (length <= 0) { return(0); } // setup int[] nonZeroIndexElements = nonZeroIndexes.ToArray(); int index = 0; int s = nonZeroIndexes.Count; // skip to start while ((index < s) && nonZeroIndexElements[index] < from) { index++; } // now the sparse dot product int i; double sum = 0; while ((--length >= 0) && (index < s) && ((i = nonZeroIndexElements[index]) < tail)) { sum += this[i] * y[i]; index++; } return(sum); }
/// <summary> /// Replaces all cell values of the receiver with the values of another matrix. /// Both matrices must have the same size. /// If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>. /// </summary> /// <param name="other"> /// The source matrix to copy from (may be identical to the receiver). /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// If <tt>size() != other.size()</tt>. /// </exception> public virtual DoubleMatrix1D Assign(DoubleMatrix1D other) { if (other == this) { return(this); } CheckSize(other); if (HaveSharedCells(other)) { other = other.Copy(); } for (int i = Size; --i >= 0;) { this[i] = other[i]; } return(this); }
public DoubleMatrix1D Assign(DoubleMatrix1D y, Cern.Jet.Math.PlusMult function, IntArrayList nonZeroIndexes) { CheckSize(y); int[] nonZeroElements = nonZeroIndexes.ToArray(); double multiplicator = function.Multiplicator; if (multiplicator == 0) { // x[i] = x[i] + 0*y[i] return(this); } else if (multiplicator == 1) { // x[i] = x[i] + y[i] for (int index = nonZeroIndexes.Count; --index >= 0;) { int i = nonZeroElements[index]; this[i] = this[i] + y[i]; } } else if (multiplicator == -1) { // x[i] = x[i] - y[i] for (int index = nonZeroIndexes.Count; --index >= 0;) { int i = nonZeroElements[index]; this[i] = this[i] - y[i]; } } else { // the general case x[i] = x[i] + mult*y[i] for (int index = nonZeroIndexes.Count; --index >= 0;) { int i = nonZeroElements[index]; this[i] = this[i] + multiplicator * y[i]; } } return(this); }
public DoubleMatrix1D Assign(DoubleMatrix1D y, Cern.Jet.Math.PlusMult function) { return(Assign(y, function.Apply)); }
/// <summary> /// Linear algebraic matrix-vector multiplication; <tt>z = A * y</tt>; /// Equivalent to <tt>return A.zMult(y,z,1,0);</tt> /// </summary> /// <param name="y"> /// The matrix. /// </param> /// <param name="z"> /// The vector. /// </param> /// <returns> /// The matrix-vector multiplication. /// </returns> public DoubleMatrix1D ZMult(DoubleMatrix1D y, DoubleMatrix1D z) { return(ZMult(y, z, 1, (z == null ? 1 : 0), false)); }
/// <summary> /// Returns the dot product of two vectors x and y, which is <tt>Sum(x[i]*y[i])</tt>, /// where <tt>x == this</tt>. /// </summary> /// <param name="y"> /// The second vector. /// </param> /// <param name="nonZeroIndexes"> /// The indexes of cells in <tt>y</tt>having a non-zero value. /// </param> /// <returns> /// The sum of products. /// </returns> protected double ZDotProduct(DoubleMatrix1D y, IntArrayList nonZeroIndexes) { return(ZDotProduct(y, 0, Size, nonZeroIndexes)); }
/// <summary> /// Returns <tt>true</tt> if both matrices share at least one identical cell. /// </summary> /// <param name="other"> /// The other matrix. /// </param> /// <returns> /// <tt>true</tt> if both matrices share at least one identical cell. /// </returns> protected virtual bool HaveSharedCellsRaw(DoubleMatrix1D other) { return(false); }
/// <summary> /// Returns the dot product of two vectors x and y, which is <tt>Sum(x[i]*y[i])</tt>, /// where <tt>x == this</tt>. /// Operates on cells at indexes <tt>0 .. Math.min(size(),y.size())</tt>. /// </summary> /// <param name="y"> /// The second vector. /// </param> /// <returns> /// The sum of products. /// </returns> public virtual double ZDotProduct(DoubleMatrix1D y) { return(ZDotProduct(y, 0, Size)); }