本文整理汇总了C#中Vector.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# Vector.Clear方法的具体用法?C# Vector.Clear怎么用?C# Vector.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector
的用法示例。
在下文中一共展示了Vector.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoModulus
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(float divisor, Vector<float> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index] % divisor);
}
}
}
示例2: DoConjugate
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
var sparseResult = result as SparseVector;
if (sparseResult != null)
{
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 Complex32[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ConjugateArray(sparseResult._storage.Values, sparseResult._storage.Values);
return;
}
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index].Conjugate());
}
}
示例3: LeftMultiply
/// <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<double> leftSide, Vector<double> 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];
}
}
}
示例4: Multiply
/// <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<Complex32> rightSide, Vector<Complex32> 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];
}
}
}
示例5: Row
/// <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="ArgumentOutOfRangeException">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<float> 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 ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive);
}
if (result.Count < length)
{
throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength);
}
// Clear the result and copy the diagonal entry.
result.Clear();
if (rowIndex >= columnIndex && rowIndex < columnIndex + length && rowIndex < _data.Length)
{
result[rowIndex - columnIndex] = _data[rowIndex];
}
}
示例6: DoModulus
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(double divisor, Vector<double> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < NonZerosCount; index++)
{
_nonZeroValues[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < NonZerosCount; index++)
{
result.At(_nonZeroIndices[index], _nonZeroValues[index] % divisor);
}
}
}
示例7: Column
/// <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<Complex32> 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];
}
}
示例8: DoMultiply
/// <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(Complex32 scalar, Vector<Complex32> result)
{
if (scalar == Complex32.One)
{
if (!ReferenceEquals(this, result))
{
CopyTo(result);
}
return;
}
if (scalar == Complex32.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 Complex32[_nonZeroValues.Length];
}
Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues, sparseResult._nonZeroValues);
}
}
示例9: DoSubtract
/// <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<Complex32> other, Vector<Complex32> 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 != Complex32.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++;
}
}
}
}
示例10: DoModulus
/// <summary>
/// Computes the canonical modulus, where the result has the sign of the divisor,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(double divisor, Vector<double> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < _storage.ValueCount; index++)
{
_storage.Values[index] = Euclid.Modulus(_storage.Values[index], divisor);
}
}
else
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], Euclid.Modulus(_storage.Values[index], divisor));
}
}
}
示例11: Row
/// <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="ArgumentOutOfRangeException">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<float> 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 ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive);
}
if (result.Count < length)
{
throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength);
}
var rowPointers = _storage.RowPointers;
var values = _storage.Values;
var valueCount = _storage.ValueCount;
// Determine bounds in columnIndices array where this item should be searched (using rowIndex)
var startIndex = rowPointers[rowIndex];
var endIndex = rowIndex < rowPointers.Length - 1 ? rowPointers[rowIndex + 1] : valueCount;
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 = _storage.FindItem(rowIndex, i);
result[j] = index >= 0 ? values[index] : 0f;
}
}
}
示例12: DoMultiply
/// <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(float scalar, Vector<float> result)
{
if (scalar == 1.0)
{
if (!ReferenceEquals(this, result))
{
CopyTo(result);
}
return;
}
if (scalar == 0)
{
result.Clear();
return;
}
var sparseResult = result as SparseVector;
if (sparseResult == null)
{
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], scalar * _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 float[_storage.ValueCount];
Buffer.BlockCopy(_storage.Values, 0, sparseResult._storage.Values, 0, _storage.ValueCount * Constants.SizeOfFloat);
}
Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._storage.Values, sparseResult._storage.Values);
}
}
示例13: Partition
public static IEnumerable Partition(bool all, int size, int step, IEnumerable pad, IEnumerable seq)
{
if (size <= 0)
{
throw new LispException("Invalid size: {0}", size);
}
if (step <= 0)
{
throw new LispException("Invalid step: {0}", step);
}
// We never need more than size-1 pad elements
var source = Runtime.Append(seq, Take(size - 1, pad));
var v = new Vector();
while (source != null)
{
while (source != null && v.Count < size)
{
v.Add(Car(source));
source = Cdr(source);
}
if (all || v.Count == size)
{
yield return AsList(v);
}
if (source != null)
{
if (step < size)
{
v.RemoveRange(0, step);
}
else if (size < step)
{
source = Runtime.Drop(step - size, source);
v.Clear();
}
else {
v.Clear();
}
}
}
}
示例14: DoSubtract
/// <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<double> other, Vector<double> result)
{
if (ReferenceEquals(this, other))
{
result.Clear();
return;
}
for (var index = 0; index < Count; index++)
{
result.At(index, At(index) - other.At(index));
}
}
示例15: DoNegate
/// <summary>
/// Negates vector and saves result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoNegate(Vector<float> 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 float[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0f, sparseResult._storage.Values, sparseResult._storage.Values);
}
}