/// <summary> /// Copies the requested row elements into a new <see cref="Vector{T}"/>. /// </summary> /// <param name="rowIndex">The row to copy elements from.</param> /// <param name="columnIndex">The column to start copying from.</param> /// <param name="length">The number of elements to copy.</param> /// <param name="result">The <see cref="Vector{T}"/> to copy the column into.</param> /// <exception cref="ArgumentNullException">If the result <see cref="Vector{T}"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is negative, /// or greater than or equal to the number of columns.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is negative, /// or greater than or equal to the number of rows.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> + <paramref name="length"/> /// is greater than or equal to the number of rows.</exception> /// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception> /// <exception cref="ArgumentOutOfRangeException">If <strong>result.Count < length</strong>.</exception> public override void Row(int rowIndex, int columnIndex, int length, Vector<Complex> result) { if (result == null) { throw new ArgumentNullException("result"); } if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } if (columnIndex + length > ColumnCount) { throw new ArgumentOutOfRangeException("length"); } if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } if (result.Count < length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } // Determine bounds in columnIndices array where this item should be searched (using rowIndex) var startIndex = _rowIndex[rowIndex]; var endIndex = rowIndex < _rowIndex.Length - 1 ? _rowIndex[rowIndex + 1] : NonZerosCount; if (startIndex == endIndex) { result.Clear(); } else { // If there are non-zero elements use base class implementation for (int i = columnIndex, j = 0; i < columnIndex + length; i++, j++) { // Copy code from At(row, column) to avoid unnecessary lock var index = FindItem(rowIndex, i); result[j] = index >= 0 ? _nonZeroValues[index] : 0.0; } } }
/// <summary> /// Subtracts another vector to this vector and stores the result into the result vector. /// </summary> /// <param name="other"> /// The vector to subtract from this one. /// </param> /// <param name="result"> /// The vector to store the result of the subtraction. /// </param> protected override void DoSubtract(Vector<Complex> other, Vector<Complex> result) { if (ReferenceEquals(this, other)) { result.Clear(); return; } for (var index = 0; index < Count; index++) { result.At(index, At(index) - other.At(index)); } }
/// <summary> /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// </summary> /// <param name="scalar"> /// The scalar to multiply. /// </param> /// <param name="result"> /// The vector to store the result of the multiplication. /// </param> protected override void DoMultiply(Complex scalar, Vector<Complex> result) { if (scalar == Complex.One) { if (!ReferenceEquals(this, result)) { CopyTo(result); } return; } if (scalar == Complex.Zero) { result.Clear(); return; } var sparseResult = result as SparseVector; if (sparseResult == null) { result.Clear(); for (var index = 0; index < NonZerosCount; index++) { result.At(_nonZeroIndices[index], scalar * _nonZeroValues[index]); } } else { if (!ReferenceEquals(this, result)) { sparseResult.NonZerosCount = NonZerosCount; sparseResult._nonZeroIndices = new int[NonZerosCount]; Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt); sparseResult._nonZeroValues = new Complex[_nonZeroValues.Length]; } Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues, sparseResult._nonZeroValues); } }
/// <summary> /// Subtracts another vector to this vector and stores the result into the result vector. /// </summary> /// <param name="other"> /// The vector to subtract from this one. /// </param> /// <param name="result"> /// The vector to store the result of the subtraction. /// </param> protected override void DoSubtract(Vector<Complex> other, Vector<Complex> result) { if (ReferenceEquals(this, other)) { result.Clear(); return; } var otherSparse = other as SparseVector; if (otherSparse == null) { base.DoSubtract(other, result); return; } var resultSparse = result as SparseVector; if (resultSparse == null) { base.DoSubtract(other, result); return; } // TODO (ruegg, 2011-10-11): Options to optimize? var otherStorage = otherSparse._storage; if (ReferenceEquals(this, resultSparse)) { int i = 0, j = 0; while (j < otherStorage.ValueCount) { if (i >= _storage.ValueCount || _storage.Indices[i] > otherStorage.Indices[j]) { var otherValue = otherStorage.Values[j]; if (!Complex.Zero.Equals(otherValue)) { _storage.InsertAtIndexUnchecked(i++, otherStorage.Indices[j], -otherValue); } j++; } else if (_storage.Indices[i] == otherStorage.Indices[j]) { // TODO: result can be zero, remove? _storage.Values[i++] -= otherStorage.Values[j++]; } else { i++; } } } else { result.Clear(); int i = 0, j = 0, last = -1; while (i < _storage.ValueCount || j < otherStorage.ValueCount) { if (j >= otherStorage.ValueCount || i < _storage.ValueCount && _storage.Indices[i] <= otherStorage.Indices[j]) { var next = _storage.Indices[i]; if (next != last) { last = next; result.At(next, _storage.Values[i] - otherSparse.At(next)); } i++; } else { var next = otherStorage.Indices[j]; if (next != last) { last = next; result.At(next, At(next) - otherStorage.Values[j]); } j++; } } } }
/// <summary> /// Copies the values of this vector into the target vector. /// </summary> /// <param name="target"> /// The vector to copy elements into. /// </param> /// <exception cref="ArgumentNullException"> /// If <paramref name="target"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="target"/> is not the same size as this vector. /// </exception> public override void CopyTo(Vector<Complex> target) { if (target == null) { throw new ArgumentNullException("target"); } if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } if (ReferenceEquals(this, target)) { return; } var otherVector = target as SparseVector; if (otherVector == null) { target.Clear(); for (var index = 0; index < NonZerosCount; index++) { target.At(_nonZeroIndices[index], _nonZeroValues[index]); } } else { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value otherVector._nonZeroValues = new Complex[NonZerosCount]; otherVector._nonZeroIndices = new int[NonZerosCount]; otherVector.NonZerosCount = NonZerosCount; if (NonZerosCount != 0) { CommonParallel.For(0, NonZerosCount, index => otherVector._nonZeroValues[index] = _nonZeroValues[index]); Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); } } }
/// <summary> /// Negates vector and saves result to <paramref name="result"/> /// </summary> /// <param name="result">Target vector</param> protected override void DoNegate(Vector<Complex> result) { var sparseResult = result as SparseVector; if (sparseResult == null) { result.Clear(); for (var index = 0; index < _storage.ValueCount; index++) { result.At(_storage.Indices[index], -_storage.Values[index]); } } else { if (!ReferenceEquals(this, result)) { sparseResult._storage.ValueCount = _storage.ValueCount; sparseResult._storage.Indices = new int[_storage.ValueCount]; Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); sparseResult._storage.Values = new Complex[_storage.ValueCount]; Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); } Control.LinearAlgebraProvider.ScaleArray(-Complex.One, sparseResult._storage.Values, sparseResult._storage.Values); } }
/// <summary> /// Multiplies this matrix with a vector and places the results into the result matrix. /// </summary> /// <param name="rightSide">The vector to multiply with.</param> /// <param name="result">The result of the multiplication.</param> /// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception> /// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception> public override void Multiply(Vector<Complex> rightSide, Vector<Complex> result) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (ColumnCount != rightSide.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "rightSide"); } if (result == null) { throw new ArgumentNullException("result"); } if (RowCount != result.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } if (ReferenceEquals(rightSide, result)) { var tmp = result.CreateVector(result.Count); Multiply(rightSide, tmp); tmp.CopyTo(result); } else { // Clear the result vector result.Clear(); // Multiply the elements in the vector with the corresponding diagonal element in this. for (var r = 0; r < Data.Length; r++) { result[r] = Data[r] * rightSide[r]; } } }
/// <summary> /// Copies the requested column elements into the given vector. /// </summary> /// <param name="columnIndex">The column to copy elements from.</param> /// <param name="rowIndex">The row to start copying from.</param> /// <param name="length">The number of elements to copy.</param> /// <param name="result">The <see cref="Vector{T}"/> to copy the column into.</param> /// <exception cref="ArgumentNullException">If the result <see cref="Vector{T}"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is negative, /// or greater than or equal to the number of columns.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is negative, /// or greater than or equal to the number of rows.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> + <paramref name="length"/> /// is greater than or equal to the number of rows.</exception> /// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception> /// <exception cref="ArgumentOutOfRangeException">If <strong>result.Count < length</strong>.</exception> public override void Column(int columnIndex, int rowIndex, int length, Vector<Complex> result) { if (result == null) { throw new ArgumentNullException("result"); } if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } if (rowIndex + length > RowCount) { throw new ArgumentOutOfRangeException("length"); } if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } if (result.Count < length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } // Clear the result and copy the diagonal entry. result.Clear(); if (columnIndex >= rowIndex && columnIndex < rowIndex + length && columnIndex < Data.Length) { result[columnIndex - rowIndex] = Data[columnIndex]; } }
/// <summary> /// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector. /// </summary> /// <param name="leftSide">The vector to multiply with.</param> /// <param name="result">The result of the multiplication.</param> /// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception> /// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception> /// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception> public override void LeftMultiply(Vector<Complex> leftSide, Vector<Complex> result) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (RowCount != leftSide.Count) { throw DimensionsDontMatch<ArgumentException>(this, leftSide, "leftSide"); } if (result == null) { throw new ArgumentNullException("result"); } if (ColumnCount != result.Count) { throw DimensionsDontMatch<ArgumentException>(this, result, "result"); } if (ReferenceEquals(leftSide, result)) { var tmp = result.CreateVector(result.Count); LeftMultiply(leftSide, tmp); tmp.CopyTo(result); } else { // Clear the result vector result.Clear(); // Multiply the elements in the vector with the corresponding diagonal element in this. for (var r = 0; r < _data.Length; r++) { result[r] = _data[r] * leftSide[r]; } } }
/// <summary> /// Subtracts another vector to this vector and stores the result into the result vector. /// </summary> /// <param name="other"> /// The vector to subtract from this one. /// </param> /// <param name="result"> /// The vector to store the result of the subtraction. /// </param> protected override void DoSubtract(Vector<Complex> other, Vector<Complex> result) { if (ReferenceEquals(this, other)) { result.Clear(); return; } var otherSparse = other as SparseVector; if (otherSparse == null) { base.DoSubtract(other, result); return; } var resultSparse = result as SparseVector; if (resultSparse == null) { base.DoSubtract(other, result); return; } // TODO (ruegg, 2011-10-11): Options to optimize? if (ReferenceEquals(this, resultSparse)) { int i = 0, j = 0; while (i < NonZerosCount || j < otherSparse.NonZerosCount) { if (i < NonZerosCount && j < otherSparse.NonZerosCount && _nonZeroIndices[i] == otherSparse._nonZeroIndices[j]) { _nonZeroValues[i++] -= otherSparse._nonZeroValues[j++]; } else if (j >= otherSparse.NonZerosCount || i < NonZerosCount && _nonZeroIndices[i] < otherSparse._nonZeroIndices[j]) { i++; } else { var otherValue = otherSparse._nonZeroValues[j]; if (otherValue != Complex.Zero) { InsertAtUnchecked(i++, otherSparse._nonZeroIndices[j], -otherValue); } j++; } } } else { result.Clear(); int i = 0, j = 0, last = -1; while (i < NonZerosCount || j < otherSparse.NonZerosCount) { if (j >= otherSparse.NonZerosCount || i < NonZerosCount && _nonZeroIndices[i] <= otherSparse._nonZeroIndices[j]) { var next = _nonZeroIndices[i]; if (next != last) { last = next; result.At(next, _nonZeroValues[i] - otherSparse.At(next)); } i++; } else { var next = otherSparse._nonZeroIndices[j]; if (next != last) { last = next; result.At(next, At(next) - otherSparse._nonZeroValues[j]); } j++; } } } }