本文整理汇总了C#中System.Matrix.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Clone方法的具体用法?C# Matrix.Clone怎么用?C# Matrix.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Matrix
的用法示例。
在下文中一共展示了Matrix.Clone方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public static UserQR Create(Matrix<Complex> matrix, QRMethod method = QRMethod.Full)
{
if (matrix.RowCount < matrix.ColumnCount)
{
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
}
Matrix<Complex> q;
Matrix<Complex> r;
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new Complex[minmn][];
if (method == QRMethod.Full)
{
r = matrix.Clone();
q = Matrix<Complex>.Build.SameAs(matrix, matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++)
{
q.At(i, i, 1.0f);
}
for (var i = 0; i < minmn; i++)
{
u[i] = GenerateColumn(r, i, i);
ComputeQR(u[i], r, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.MaxDegreeOfParallelism);
}
for (var i = minmn - 1; i >= 0; i--)
{
ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.RowCount, Control.MaxDegreeOfParallelism);
}
}
else
{
q = matrix.Clone();
for (var i = 0; i < minmn; i++)
{
u[i] = GenerateColumn(q, i, i);
ComputeQR(u[i], q, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.MaxDegreeOfParallelism);
}
r = q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount);
q.Clear();
for (var i = 0; i < matrix.ColumnCount; i++)
{
q.At(i, i, 1.0f);
}
for (var i = minmn - 1; i >= 0; i--)
{
ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.ColumnCount, Control.MaxDegreeOfParallelism);
}
}
return new UserQR(q, r, method);
}
示例2: UserQR
/// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
MatrixR = matrix.Clone();
MatrixQ = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++)
{
MatrixQ.At(i, i, 1.0);
}
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new double[minmn][];
for (var i = 0; i < minmn; i++)
{
u[i] = GenerateColumn(MatrixR, i, matrix.RowCount - 1, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount - 1, i + 1, matrix.ColumnCount - 1);
}
for (var i = minmn - 1; i >= 0; i--)
{
ComputeQR(u[i], MatrixQ, i, matrix.RowCount - 1, i, matrix.RowCount - 1);
}
}
示例3: UserQR
/// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix<Complex32> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
MatrixR = matrix.Clone();
MatrixQ = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++)
{
MatrixQ.At(i, i, 1.0f);
}
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new Complex32[minmn][];
for (var i = 0; i < minmn; i++)
{
u[i] = GenerateColumn(MatrixR, i, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
}
for (var i = minmn - 1; i >= 0; i--)
{
ComputeQR(u[i], MatrixQ, i, matrix.RowCount, i, matrix.RowCount, Control.NumberOfParallelWorkerThreads);
}
}
示例4: QrDecomposition
/// <summary>Construct a QR decomposition.</summary>
public QrDecomposition(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
this.QR = (Matrix) value.Clone();
double[][] qr = this.QR.baseArray;
int m = value.Rows;
int n = value.Columns;
this.Rdiag = new double[n];
for (int k = 0; k < n; k++)
{
// Compute 2-norm of k-th column without under/overflow.
double nrm = 0;
for (int i = k; i < m; i++)
{
nrm = AForge.Mathematics.Tools.Hypotenuse(nrm,qr[i][k]);
}
if (nrm != 0.0)
{
// Form k-th Householder vector.
if (qr[k][k] < 0)
{
nrm = -nrm;
}
for (int i = k; i < m; i++)
{
qr[i][k] /= nrm;
}
qr[k][k] += 1.0;
// Apply transformation to remaining columns.
for (int j = k+1; j < n; j++)
{
double s = 0.0;
for (int i = k; i < m; i++)
{
s += qr[i][k]*qr[i][j];
}
s = -s/qr[k][k];
for (int i = k; i < m; i++)
{
qr[i][j] += s*qr[i][k];
}
}
}
this.Rdiag[k] = -nrm;
}
}
示例5: QRDecomposition
/// <summary>
/// QR Decomposition, computed by Householder reflections.
/// </summary>
/// <remarks>Provides access to R, the Householder vectors and computes Q.</remarks>
/// <param name="A">Rectangular matrix</param>
public QRDecomposition(
Matrix A
)
{
// TODO: it is usually considered as a poor practice to execute algorithms within a constructor.
// Initialize.
QR = A.Clone();
Rdiag = new double[n];
// Main loop.
for(int k = 0; k < n; k++)
{
// Compute 2-norm of k-th column without under/overflow.
double norm = 0;
for(int i = k; i < m; i++)
{
norm = Fn.Hypot(norm, QR[i][k]);
}
if(norm != 0.0)
{
// Form k-th Householder vector.
if(QR[k][k] < 0)
{
norm = -norm;
}
for(int i = k; i < m; i++)
{
QR[i][k] /= norm;
}
QR[k][k] += 1.0;
// Apply transformation to remaining columns.
for(int j = k + 1; j < n; j++)
{
double s = 0.0;
for(int i = k; i < m; i++)
{
s += QR[i][k] * QR[i][j];
}
s = (-s) / QR[k][k];
for(int i = k; i < m; i++)
{
QR[i][j] += s * QR[i][k];
}
}
}
Rdiag[k] = -norm;
}
InitOnDemandComputations();
}
示例6: Kalman
/// <summary>
/// Create a Kalman Filter using the specific values
/// </summary>
/// <param name="initialState">The m x 1 matrix</param>
/// <param name="transitionMatrix">The m x m matrix (A) </param>
/// <param name="controlMatrix">The m x n matrix (B)</param>
/// <param name="measurementMatrix">The n x m matrix (H)</param>
/// <param name="processNoiseCovarianceMatrix">The n x n matrix (Q)</param>
/// <param name="measurementNoiseCovarianceMatrix">The m x m matrix (R)</param>
public Kalman(
Matrix<float> initialState,
Matrix<float> transitionMatrix,
Matrix<float> controlMatrix,
Matrix<float> measurementMatrix,
Matrix<float> processNoiseCovarianceMatrix,
Matrix<float> measurementNoiseCovarianceMatrix
)
: this(initialState.Rows,
measurementMatrix.Rows,
controlMatrix == null ? 0 : controlMatrix.Rows)
{
PredictedState = initialState.Clone();
CorrectedState = initialState.Clone();
TransitionMatrix = transitionMatrix;
if (controlMatrix != null) ControlMatrix = controlMatrix;
MeasurementMatrix = measurementMatrix;
ProcessNoiseCovariance = processNoiseCovarianceMatrix;
MeasurementNoiseCovariance = measurementNoiseCovarianceMatrix;
}
示例7: Create
/// <summary>
/// Initializes a new instance of the <see cref="DenseGramSchmidt"/> class. This object creates an unitary matrix
/// using the modified Gram-Schmidt method.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is rank deficient</exception>
public static DenseGramSchmidt Create(Matrix<Complex32> matrix)
{
if (matrix.RowCount < matrix.ColumnCount)
{
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
}
var q = (DenseMatrix)matrix.Clone();
var r = new DenseMatrix(matrix.ColumnCount, matrix.ColumnCount);
Factorize(q.Values, q.RowCount, q.ColumnCount, r.Values);
return new DenseGramSchmidt(q, r);
}
示例8: UserCholesky
/// <summary>
/// Initializes a new instance of the <see cref="UserCholesky"/> class. This object will compute the
/// Cholesky factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not positive definite.</exception>
public UserCholesky(Matrix<Complex32> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
// Create a new matrix for the Cholesky factor, then perform factorization (while overwriting).
CholeskyFactor = matrix.Clone();
var tmpColumn = new Complex32[CholeskyFactor.RowCount];
// Main loop - along the diagonal
for (var ij = 0; ij < CholeskyFactor.RowCount; ij++)
{
// "Pivot" element
var tmpVal = CholeskyFactor.At(ij, ij);
if (tmpVal.Real > 0.0)
{
tmpVal = tmpVal.SquareRoot();
CholeskyFactor.At(ij, ij, tmpVal);
tmpColumn[ij] = tmpVal;
// Calculate multipliers and copy to local column
// Current column, below the diagonal
for (var i = ij + 1; i < CholeskyFactor.RowCount; i++)
{
CholeskyFactor.At(i, ij, CholeskyFactor.At(i, ij) / tmpVal);
tmpColumn[i] = CholeskyFactor.At(i, ij);
}
// Remaining columns, below the diagonal
DoCholeskyStep(CholeskyFactor, CholeskyFactor.RowCount, ij + 1, CholeskyFactor.RowCount, tmpColumn, Control.NumberOfParallelWorkerThreads);
}
else
{
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
}
for (var i = ij + 1; i < CholeskyFactor.RowCount; i++)
{
CholeskyFactor.At(ij, i, Complex32.Zero);
}
}
}
示例9: Main
private static void Main(string[] args)
{
var A = new Matrix(new double[,] { { 1, 2, 3 }, { 0, 1, 4 }, { 5, 6, 0 } });
var clone = A.Clone();
A[0, 0] = -99;
Console.WriteLine(A);
Console.WriteLine(clone);
A[2, 2] = -88;
Console.WriteLine(A);
Console.WriteLine(clone);
Console.Read();
}
示例10: Create
/// <summary>
/// Initializes a new instance of the <see cref="UserCholesky"/> class. This object will compute the
/// Cholesky factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not positive definite.</exception>
public static UserCholesky Create(Matrix<float> matrix)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
// Create a new matrix for the Cholesky factor, then perform factorization (while overwriting).
var factor = matrix.Clone();
var tmpColumn = new float[factor.RowCount];
// Main loop - along the diagonal
for (var ij = 0; ij < factor.RowCount; ij++)
{
// "Pivot" element
var tmpVal = factor.At(ij, ij);
if (tmpVal > 0.0)
{
tmpVal = (float) Math.Sqrt(tmpVal);
factor.At(ij, ij, tmpVal);
tmpColumn[ij] = tmpVal;
// Calculate multipliers and copy to local column
// Current column, below the diagonal
for (var i = ij + 1; i < factor.RowCount; i++)
{
factor.At(i, ij, factor.At(i, ij)/tmpVal);
tmpColumn[i] = factor.At(i, ij);
}
// Remaining columns, below the diagonal
DoCholeskyStep(factor, factor.RowCount, ij + 1, factor.RowCount, tmpColumn, Control.MaxDegreeOfParallelism);
}
else
{
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
}
for (var i = ij + 1; i < factor.RowCount; i++)
{
factor.At(ij, i, 0.0f);
}
}
return new UserCholesky(factor);
}
示例11: UserGramSchmidt
/// <summary>
/// Initializes a new instance of the <see cref="UserGramSchmidt"/> class. This object creates an unitary matrix
/// using the modified Gram-Schmidt method.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is rank deficient</exception>
public UserGramSchmidt(Matrix<Complex32> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
MatrixQ = matrix.Clone();
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount);
for (var k = 0; k < MatrixQ.ColumnCount; k++)
{
var norm = MatrixQ.Column(k).Norm(2).Real;
if (norm == 0.0f)
{
throw new ArgumentException(Resources.ArgumentMatrixNotRankDeficient);
}
MatrixR.At(k, k, norm);
for (var i = 0; i < MatrixQ.RowCount; i++)
{
MatrixQ.At(i, k, MatrixQ.At(i, k) / norm);
}
for (var j = k + 1; j < MatrixQ.ColumnCount; j++)
{
var dot = Complex32.Zero;
for (int i = 0; i < MatrixQ.RowCount; i++)
{
dot += MatrixQ.Column(k)[i].Conjugate() * MatrixQ.Column(j)[i];
}
MatrixR.At(k, j, dot);
for (var i = 0; i < MatrixQ.RowCount; i++)
{
var value = MatrixQ.At(i, j) - (MatrixQ.At(i, k) * dot);
MatrixQ.At(i, j, value);
}
}
}
}
示例12: CloneTest
public void CloneTest()
{
var original = new Matrix<double>(3, 2);
original[1, 1] = 1;
original[1, 2] = 2;
original[2, 1] = 3;
original[2, 2] = 4;
original[3, 1] = 5;
original[3, 2] = 6;
var clone = original.Clone();
Assert.AreEqual(1, clone[1, 1]);
Assert.AreEqual(2, clone[1, 2]);
Assert.AreEqual(3, clone[2, 1]);
Assert.AreEqual(4, clone[2, 2]);
Assert.AreEqual(5, clone[3, 1]);
Assert.AreEqual(6, clone[3, 2]);
}
示例13: UserCholesky
/// <summary>
/// Initializes a new instance of the <see cref="UserCholesky"/> class. This object will compute the
/// Cholesky factorization when the constructor is called and cache it's factorization.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not positive definite.</exception>
public UserCholesky(Matrix matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
// Create a new matrix for the Cholesky factor, then perform factorization (while overwriting).
CholeskyFactor = matrix.Clone();
for (var j = 0; j < CholeskyFactor.RowCount; j++)
{
var d = 0.0;
for (var k = 0; k < j; k++)
{
var s = 0.0;
for (var i = 0; i < k; i++)
{
s += CholeskyFactor.At(k, i) * CholeskyFactor.At(j, i);
}
s = (matrix.At(j, k) - s) / CholeskyFactor.At(k, k);
CholeskyFactor.At(j, k, s);
d += s * s;
}
d = matrix.At(j, j) - d;
if (d <= 0.0)
{
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
}
CholeskyFactor.At(j, j, Math.Sqrt(d));
for (var k = j + 1; k < CholeskyFactor.RowCount; k++)
{
CholeskyFactor.At(j, k, 0.0);
}
}
}
示例14: Create
/// <summary>
/// Initializes a new instance of the <see cref="UserGramSchmidt"/> class. This object creates an unitary matrix
/// using the modified Gram-Schmidt method.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is rank deficient</exception>
public static UserGramSchmidt Create(Matrix<Complex32> matrix)
{
if (matrix.RowCount < matrix.ColumnCount)
{
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
}
var q = matrix.Clone();
var r = Matrix<Complex32>.Build.SameAs(matrix, matrix.ColumnCount, matrix.ColumnCount);
for (var k = 0; k < q.ColumnCount; k++)
{
var norm = (float) q.Column(k).L2Norm();
if (norm == 0f)
{
throw new ArgumentException(Resources.ArgumentMatrixNotRankDeficient);
}
r.At(k, k, norm);
for (var i = 0; i < q.RowCount; i++)
{
q.At(i, k, (q.At(i, k) / norm));
}
for (var j = k + 1; j < q.ColumnCount; j++)
{
var dot = Complex32.Zero;
for (int i = 0; i < q.RowCount; i++)
{
dot += q.Column(k)[i].Conjugate() * q.Column(j)[i];
}
r.At(k, j, dot);
for (var i = 0; i < q.RowCount; i++)
{
var value = q.At(i, j) - (q.At(i, k) * dot);
q.At(i, j, value);
}
}
}
return new UserGramSchmidt(q, r);
}
示例15: UserGramSchmidt
/// <summary>
/// Initializes a new instance of the <see cref="UserGramSchmidt"/> class. This object creates an orthogonal matrix
/// using the modified Gram-Schmidt method.
/// </summary>
/// <param name="matrix">The matrix to factor.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is rank deficient</exception>
public UserGramSchmidt(Matrix<double> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
MatrixQ = matrix.Clone();
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount);
for (var k = 0; k < MatrixQ.ColumnCount; k++)
{
var norm = MatrixQ.Column(k).Norm(2);
if (norm == 0.0)
{
throw new ArgumentException(Resources.ArgumentMatrixNotRankDeficient);
}
MatrixR.At(k, k, norm);
for (var i = 0; i < MatrixQ.RowCount; i++)
{
MatrixQ.At(i, k, MatrixQ.At(i, k) / norm);
}
for (var j = k + 1; j < MatrixQ.ColumnCount; j++)
{
var dot = MatrixQ.Column(k).DotProduct(MatrixQ.Column(j));
MatrixR.At(k, j, dot);
for (var i = 0; i < MatrixQ.RowCount; i++)
{
var value = MatrixQ.At(i, j) - (MatrixQ.At(i, k) * dot);
MatrixQ.At(i, j, value);
}
}
}
}