本文整理汇总了C#中Matrix.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Clear方法的具体用法?C# Matrix.Clear怎么用?C# Matrix.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
// Creates two matrices of the same size
var matrix1 = new Matrix<float>(Rows, Cols);
var matrix2 = new Matrix<float>(Rows, Cols);
// Generate values for the matrices by random generator
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
matrix1[r, c] = rand.Next(-100, 100) * 0.37f;
matrix2[r, c] = rand.Next(-100, 100) * 0.37f;
}
}
matrix2[1, 2] = 0;
// Print the result
Print(matrix1, "Matrix 1");
Print(matrix2, "Matrix 2");
Print(matrix1 + matrix2, "Matrix 1 + Matrix 2");
Print(matrix1 - matrix2, "Matrix 1 - Matrix 2");
Print(matrix1 * matrix2, "Matrix 1 * Matrix 2");
// Checks for non-zero elements
Console.WriteLine("Matrix 1 is: {0}ero matrix!", matrix1 ? "Non-z" : "Z");
matrix2.Clear();
Console.WriteLine("Matrix 2 is: {0}ero matrix!", matrix2 ? "Non-z" : "Z");
Console.WriteLine("Matrix 1 is: {0}ero matrix!", !matrix1 ? "Z" : "Non-z");
}
示例2: ComputeJacobian
public override void ComputeJacobian(Matrix<double> J)
{
J.Clear();
if(DoComputeJacobianNumerically)
{
ComputeJacobian_Numerical(J);
}
else
{
ComputeJacobian_Analitic(J);
}
}
示例3: Main
static void Main()
{
//Constructor test
Console.WriteLine("Constructor and ToString test:");
Matrix<int> theMatrix = new Matrix<int>(2, 3);
int counter = 1;
for (int rows = 0; rows < 2; rows++)
{
for (int cols = 0; cols < 3; cols++)
{ //Indexer test
theMatrix[rows, cols] = counter;
counter++;
}
}
//Print test
Console.WriteLine(theMatrix.ToString());
//+ operator Test
Console.WriteLine("+ operator overload");
Matrix<int> resultMatrix = new Matrix<int>(4, 6);
resultMatrix = theMatrix + theMatrix;
Console.WriteLine(resultMatrix.ToString());
//- operator Test
Console.WriteLine("- operator overload");
resultMatrix -= theMatrix;
Console.WriteLine(resultMatrix.ToString());
//* operator Test
Console.WriteLine("* operator overload");
resultMatrix *= theMatrix;
Console.WriteLine(resultMatrix);
//Bool operations test
Console.WriteLine("Bool operator test");
if (theMatrix)
{
Console.WriteLine("No zero elements");
}
theMatrix.Clear();
if (theMatrix)
{
Console.WriteLine("No zero elements");
}
else
{
Console.WriteLine("Empty matrix");
}
}
示例4: BuildHermitianMatrix
private void BuildHermitianMatrix(Matrix<Complex> hermitianMatrix, Matrix<double> pairMatrix, double k, int m)
{
hermitianMatrix.Clear();
Complex top = new Complex();
Complex down = new Complex();
for (int l = 0; l < m; l++)
{
switch ((int)pairMatrix[l, 2])
{
case 1: // 1
top = Complex.One; // Top
down = Complex.One; // Down
break;
case 2: // e^(ik)
top = new Complex(Math.Cos(k), Math.Sin(k)); // Top
down = new Complex(Math.Cos(k), -Math.Sin(k)); // Down
break;
case 3: // e^(-ik)
top = new Complex(Math.Cos(k), -Math.Sin(k)); // Top
down = new Complex(Math.Cos(k), Math.Sin(k)); // Down
break;
case 4: // [1 + e^(ik)]
top = new Complex(1 + Math.Cos(k), Math.Sin(k)); // Top
down = new Complex(1 + Math.Cos(k), -Math.Sin(k)); // Down
break;
case 5: // [1 + e^(-ik)]
top = new Complex(1 + Math.Cos(k), -Math.Sin(k)); // Top
down = new Complex(1 + Math.Cos(k), Math.Sin(k)); // Down
break;
default:
Console.WriteLine("Nie znana informacja o połączeniu!");
break;
}
int i = Convert.ToInt32(pairMatrix[l, 0]) - 1;
int j = Convert.ToInt32(pairMatrix[l, 1]) - 1;
hermitianMatrix[i, j] = top;
hermitianMatrix[j, i] = down;
}
}
示例5: DoMultiply
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
result.Clear();
var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[row];
var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount;
if (startIndex == endIndex)
{
continue;
}
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
other.Column(column, columnVector);
var sum = 0.0;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum);
}
}
}
示例6: UpperTriangle
/// <summary>
/// Puts the upper triangle of this matrix into the result matrix.
/// </summary>
/// <param name="result">Where to store the lower triangle.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void UpperTriangle(Matrix<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, _data[i]);
}
}
示例7: DoPointwiseMultiply
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>
/// <param name="other">The matrix to pointwise multiply with this one.</param>
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<double> other, Matrix<double> result)
{
result.Clear();
var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
for (var i = 0; i < RowCount; i++)
{
var endIndex = rowPointers[i + 1];
for (var j = rowPointers[i]; j < endIndex; j++)
{
var resVal = values[j]*other.At(i, columnIndices[j]);
if (resVal != 0d)
{
result.At(i, columnIndices[j], resVal);
}
}
}
}
示例8: UpperTriangle
/// <summary>
/// Puts the upper triangle of this matrix into the result matrix.
/// </summary>
/// <param name="result">Where to store the lower triangle.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void UpperTriangle(Matrix<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(this, result))
{
var tmp = Build.SameAs(result);
UpperTriangle(tmp);
tmp.CopyTo(result);
}
else
{
result.Clear();
UpperTriangleImpl(result);
}
}
示例9: StrictlyLowerTriangle
/// <summary>
/// Puts the strictly lower triangle of this matrix into the result matrix.
/// </summary>
/// <param name="result">Where to store the lower triangle.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void StrictlyLowerTriangle(Matrix<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
if (ReferenceEquals(this, result))
{
var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount);
StrictlyLowerTriangle(tmp);
tmp.CopyTo(result);
}
else
{
result.Clear();
StrictlyLowerTriangleImpl(result);
}
}
示例10: DoMultiply
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
result.Clear();
var columnVector = new DenseVector(other.RowCount);
var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
for (var row = 0; row < RowCount; row++)
{
var startIndex = rowPointers[row];
var endIndex = rowPointers[row + 1];
if (startIndex == endIndex)
{
continue;
}
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
other.Column(column, columnVector);
var sum = 0d;
for (var index = startIndex; index < endIndex; index++)
{
sum += values[index] * columnVector[columnIndices[index]];
}
result.At(row, column, sum);
}
}
}
示例11: DiagonalStack
/// <summary>
/// Diagonally stacks his matrix on top of the given matrix and places the combined matrix into the result matrix.
/// </summary>
/// <param name="lower">The lower, right matrix.</param>
/// <param name="result">The combined matrix</param>
/// <exception cref="ArgumentNullException">If lower is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not (this.Rows + lower.rows) x (this.Columns + lower.Columns).</exception>
public override void DiagonalStack(Matrix<double> lower, Matrix<double> result)
{
if (lower == null)
{
throw new ArgumentNullException("lower");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount + lower.RowCount || result.ColumnCount != ColumnCount + lower.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
// Clear the result matrix
result.Clear();
// Copy the diagonal part into the result matrix.
for (var i = 0; i < Data.Length; i++)
{
result[i, i] = Data[i];
}
// Copy the lower matrix into the result matrix.
CommonParallel.For(0, lower.RowCount, i => CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j))));
}
示例12: DoPointwiseMultiply
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>
/// <param name="other">The matrix to pointwise multiply with this one.</param>
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<double> other, Matrix<double> result)
{
result.Clear();
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
示例13: Append
/// <summary>
/// Concatenates this matrix with the given matrix and places the result into the result <see cref="SparseMatrix"/>.
/// </summary>
/// <param name="right">The matrix to concatenate.</param>
/// <param name="result">The combined <see cref="SparseMatrix"/>.</param>
public override void Append(Matrix<double> right, Matrix<double> result)
{
if (right == null)
{
throw new ArgumentNullException("right");
}
if (right.RowCount != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension);
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.ColumnCount != (ColumnCount + right.ColumnCount) || result.RowCount != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
}
// Clear the result matrix
result.Clear();
// Copy the diagonal part into the result matrix.
for (var i = 0; i < Data.Length; i++)
{
result[i, i] = Data[i];
}
// Copy the lower matrix into the result matrix.
for (var i = 0; i < right.RowCount; i++)
{
for (var j = 0; j < right.ColumnCount; j++)
{
result[i, j + RowCount] = right[i, j];
}
}
}
示例14: Stack
/// <summary>
/// Stacks this matrix on top of the given matrix and places the result into the result <see cref="SparseMatrix"/>.
/// </summary>
/// <param name="lower">The matrix to stack this matrix upon.</param>
/// <param name="result">The combined <see cref="SparseMatrix"/>.</param>
/// <exception cref="ArgumentNullException">If lower is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>upper.Columns != lower.Columns</strong>.</exception>
public override void Stack(Matrix<double> lower, Matrix<double> result)
{
if (lower == null)
{
throw new ArgumentNullException("lower");
}
if (lower.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "lower");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != (RowCount + lower.RowCount) || result.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
// Clear the result matrix
result.Clear();
// Copy the diagonal part into the result matrix.
for (var i = 0; i < Data.Length; i++)
{
result[i, i] = Data[i];
}
// Copy the lower matrix into the result matrix.
for (var i = 0; i < lower.RowCount; i++)
{
for (var j = 0; j < lower.ColumnCount; j++)
{
result[i + RowCount, j] = lower[i, j];
}
}
}
示例15: UpperTriangle
/// <summary>
/// Puts the upper triangle of this matrix into the result matrix.
/// </summary>
/// <param name="result">Where to store the lower triangle.</param>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void UpperTriangle(Matrix<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
result.Clear();
for (var i = 0; i < Data.Length; i++)
{
result[i, i] = Data[i];
}
}