/// <summary> /// Initializes a new instance of the <see cref="UserEvd"/> class. This object will compute the /// the eigenvalue decomposition when the constructor is called and cache it's decomposition. /// </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 EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception> public static UserEvd Create(Matrix<float> matrix) { if (matrix.RowCount != matrix.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } var order = matrix.RowCount; // Initialize matricies for eigenvalues and eigenvectors var eigenVectors = Matrix<float>.Build.SameAs(matrix, order, order); var blockDiagonal = Matrix<float>.Build.SameAs(matrix, order, order); var eigenValues = new LinearAlgebra.Complex.DenseVector(order); var isSymmetric = true; for (var i = 0; isSymmetric && i < order; i++) { for (var j = 0; isSymmetric && j < order; j++) { isSymmetric &= matrix.At(i, j) == matrix.At(j, i); } } var d = new float[order]; var e = new float[order]; if (isSymmetric) { matrix.CopyTo(eigenVectors); d = eigenVectors.Row(order - 1).ToArray(); SymmetricTridiagonalize(eigenVectors, d, e, order); SymmetricDiagonalize(eigenVectors, d, e, order); } else { var matrixH = matrix.ToArray(); NonsymmetricReduceToHessenberg(eigenVectors, matrixH, order); NonsymmetricReduceHessenberToRealSchur(eigenVectors, matrixH, d, e, order); } for (var i = 0; i < order; i++) { blockDiagonal.At(i, i, d[i]); if (e[i] > 0) { blockDiagonal.At(i, i + 1, e[i]); } else if (e[i] < 0) { blockDiagonal.At(i, i - 1, e[i]); } } for (var i = 0; i < order; i++) { eigenValues[i] = new Complex(d[i], e[i]); } return new UserEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric); }
/// <summary> /// Adds another matrix to this matrix. /// </summary> /// <param name="other">The matrix to add to this matrix.</param> /// <param name="result">The matrix to store the result of the addition.</param> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result) { // diagonal + diagonal = diagonal var diagOther = other as DiagonalMatrix; var diagResult = result as DiagonalMatrix; if (diagOther != null && diagResult != null) { Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data); return; } other.CopyTo(result); for (int i = 0; i < _data.Length; i++) { result.At(i, i, result.At(i, i) + _data[i]); } }
/// <summary> /// Solves a system of linear equations, <b>AX = B</b>, with A Cholesky factorized. /// </summary> /// <param name="input">The right hand side <see cref="Matrix{T}"/>, <b>B</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> public override void Solve(Matrix<Complex32> input, Matrix<Complex32> result) { if (input == null) { throw new ArgumentNullException("input"); } if (result == null) { throw new ArgumentNullException("result"); } // Check for proper dimensions. if (result.RowCount != input.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } if (result.ColumnCount != input.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } if (input.RowCount != CholeskyFactor.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } input.CopyTo(result); var order = CholeskyFactor.RowCount; for (var c = 0; c < result.ColumnCount; c++) { // Solve L*Y = B; Complex32 sum; for (var i = 0; i < order; i++) { sum = result.At(i, c); for (var k = i - 1; k >= 0; k--) { sum -= CholeskyFactor.At(i, k) * result.At(k, c); } result.At(i, c, sum / CholeskyFactor.At(i, i)); } // Solve L'*X = Y; for (var i = order - 1; i >= 0; i--) { sum = result.At(i, c); for (var k = i + 1; k < order; k++) { sum -= CholeskyFactor.At(k, i).Conjugate() * result.At(k, c); } result.At(i, c, sum / CholeskyFactor.At(i, i)); } } }
/// <summary> /// Solves a system of linear equations, <c>AX = B</c>, with A LU factorized. /// </summary> /// <param name="input">The right hand side <see cref="Matrix{T}"/>, <c>B</c>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <c>X</c>.</param> public override void Solve(Matrix<Complex> input, Matrix<Complex> result) { // Check for proper arguments. if (input == null) { throw new ArgumentNullException("input"); } if (result == null) { throw new ArgumentNullException("result"); } // Check for proper dimensions. if (result.RowCount != input.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } if (result.ColumnCount != input.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } if (input.RowCount != Factors.RowCount) { throw Matrix.DimensionsDontMatch<ArgumentException>(input, Factors); } // Copy the contents of input to result. input.CopyTo(result); for (var i = 0; i < Pivots.Length; i++) { if (Pivots[i] == i) { continue; } var p = Pivots[i]; for (var j = 0; j < result.ColumnCount; j++) { var temp = result.At(p, j); result.At(p, j, result.At(i, j)); result.At(i, j, temp); } } var order = Factors.RowCount; // Solve L*Y = P*B for (var k = 0; k < order; k++) { for (var i = k + 1; i < order; i++) { for (var j = 0; j < result.ColumnCount; j++) { var temp = result.At(k, j)*Factors.At(i, k); result.At(i, j, result.At(i, j) - temp); } } } // Solve U*X = Y; for (var k = order - 1; k >= 0; k--) { for (var j = 0; j < result.ColumnCount; j++) { result.At(k, j, (result.At(k, j)/Factors.At(k, k))); } for (var i = 0; i < k; i++) { for (var j = 0; j < result.ColumnCount; j++) { var temp = result.At(k, j)*Factors.At(i, k); result.At(i, j, result.At(i, j) - temp); } } } }
/// <summary> /// Solves a system of linear equations, <b>AX = B</b>, with A Cholesky factorized. /// </summary> /// <param name="input">The right hand side <see cref="Matrix{T}"/>, <b>B</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> public override void Solve(Matrix<float> input, Matrix<float> result) { if (result.RowCount != input.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } if (result.ColumnCount != input.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } if (input.RowCount != Factor.RowCount) { throw Matrix.DimensionsDontMatch<ArgumentException>(input, Factor); } input.CopyTo(result); var order = Factor.RowCount; for (var c = 0; c < result.ColumnCount; c++) { // Solve L*Y = B; float sum; for (var i = 0; i < order; i++) { sum = result.At(i, c); for (var k = i - 1; k >= 0; k--) { sum -= Factor.At(i, k)*result.At(k, c); } result.At(i, c, sum/Factor.At(i, i)); } // Solve L'*X = Y; for (var i = order - 1; i >= 0; i--) { sum = result.At(i, c); for (var k = i + 1; k < order; k++) { sum -= Factor.At(k, i)*result.At(k, c); } result.At(i, c, sum/Factor.At(i, i)); } } }
/// <summary> /// Initializes a new instance of the <see cref="UserEvd"/> class. This object will compute the /// the eigenvalue decomposition when the constructor is called and cache it's decomposition. /// </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 EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception> public UserEvd(Matrix<float> matrix) { if (matrix == null) { throw new ArgumentNullException("matrix"); } if (matrix.RowCount != matrix.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } var order = matrix.RowCount; // Initialize matricies for eigenvalues and eigenvectors MatrixEv = matrix.CreateMatrix(order, order); MatrixD = matrix.CreateMatrix(order, order); VectorEv = new LinearAlgebra.Complex.DenseVector(order); IsSymmetric = true; for (var i = 0; IsSymmetric && i < order; i++) { for (var j = 0; IsSymmetric && j < order; j++) { IsSymmetric &= matrix.At(i, j) == matrix.At(j, i); } } var d = new float[order]; var e = new float[order]; if (IsSymmetric) { matrix.CopyTo(MatrixEv); d = MatrixEv.Row(order - 1).ToArray(); SymmetricTridiagonalize(d, e, order); SymmetricDiagonalize(d, e, order); } else { var matrixH = matrix.ToArray(); NonsymmetricReduceToHessenberg(matrixH, order); NonsymmetricReduceHessenberToRealSchur(matrixH, d, e, order); } for (var i = 0; i < order; i++) { MatrixD.At(i, i, d[i]); if (e[i] > 0) { MatrixD.At(i, i + 1, e[i]); } else if (e[i] < 0) { MatrixD.At(i, i - 1, e[i]); } } for (var i = 0; i < order; i++) { VectorEv[i] = new Complex(d[i], e[i]); } }
/// <summary> /// Initializes a new instance of the <see cref="UserEvd"/> class. This object will compute the /// the eigenvalue decomposition when the constructor is called and cache it's decomposition. /// </summary> /// <param name="matrix">The matrix to factor.</param> /// <param name="symmetricity">If it is known whether the matrix is symmetric or not the routine can skip checking it itself.</param> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception> public static UserEvd Create(Matrix<double> matrix, Symmetricity symmetricity) { if (matrix.RowCount != matrix.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } var order = matrix.RowCount; // Initialize matricies for eigenvalues and eigenvectors var eigenVectors = Matrix<double>.Build.SameAs(matrix, order, order); var blockDiagonal = Matrix<double>.Build.SameAs(matrix, order, order); var eigenValues = new LinearAlgebra.Complex.DenseVector(order); bool isSymmetric; switch (symmetricity) { case Symmetricity.Symmetric: case Symmetricity.Hermitian: isSymmetric = true; break; case Symmetricity.Asymmetric: isSymmetric = false; break; default: isSymmetric = matrix.IsSymmetric(); break; } var d = new double[order]; var e = new double[order]; if (isSymmetric) { matrix.CopyTo(eigenVectors); d = eigenVectors.Row(order - 1).ToArray(); SymmetricTridiagonalize(eigenVectors, d, e, order); SymmetricDiagonalize(eigenVectors, d, e, order); } else { var matrixH = matrix.ToArray(); NonsymmetricReduceToHessenberg(eigenVectors, matrixH, order); NonsymmetricReduceHessenberToRealSchur(eigenVectors, matrixH, d, e, order); } for (var i = 0; i < order; i++) { blockDiagonal.At(i, i, d[i]); if (e[i] > 0) { blockDiagonal.At(i, i + 1, e[i]); } else if (e[i] < 0) { blockDiagonal.At(i, i - 1, e[i]); } } for (var i = 0; i < order; i++) { eigenValues[i] = new Complex(d[i], e[i]); } return new UserEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric); }
public void TestImageDFT() { Image<Gray, float> matA = EmguAssert.LoadImage<Gray, float>("stuff.jpg"); //The matrix to be convolved with matA, a bluring filter Matrix<float> matB = new Matrix<float>( new float[,] { {1.0f / 16.0f, 1.0f / 16.0f, 1.0f / 16.0f}, {1.0f / 16.0f, 8.0f / 16.0f, 1.0f / 16.0f}, {1.0f / 16.0f, 1.0f / 16.0f, 1.0f / 16.0f}}); Image<Gray, float> convolvedImage = new Image<Gray, float>(new Size(matA.Width + matB.Width -1, matA.Height + matB.Height -1)); Matrix<float> dftA = new Matrix<float>( CvInvoke.GetOptimalDFTSize(convolvedImage.Rows), CvInvoke.GetOptimalDFTSize(convolvedImage.Cols)); matA.CopyTo(dftA.GetSubRect(matA.ROI)); CvInvoke.Dft(dftA, dftA, Emgu.CV.CvEnum.DxtType.Forward, matA.Rows); Matrix<float> dftB = new Matrix<float>(dftA.Size); matB.CopyTo(dftB.GetSubRect(new Rectangle(Point.Empty, matB.Size))); CvInvoke.Dft(dftB, dftB, Emgu.CV.CvEnum.DxtType.Forward, matB.Rows); CvInvoke.MulSpectrums(dftA, dftB, dftA, Emgu.CV.CvEnum.MulSpectrumsType.Default, false); CvInvoke.Dft(dftA, dftA, Emgu.CV.CvEnum.DxtType.Inverse, convolvedImage.Rows); dftA.GetSubRect(new Rectangle(Point.Empty, convolvedImage.Size)).CopyTo(convolvedImage); }
private static void ForwardAlgorithm(ref List<Particle> particleListNew, Matrix<float> matRnormalized, Matrix<float> stateMat, Matrix<float> condMat, int tempModel = 5) { //Matrix<float> condMat = new Matrix<float>(3, 2); //condMat[0, 0] = 0.7f; //condMat[0, 1] = 0.3f; //condMat[1, 0] = 0.7f; //condMat[1, 1] = 0.3f; //condMat[2, 0] = 0.3f; //condMat[2, 1] = 0.7f; //stateMat = new Matrix<float>(3, 3); //stateMat[0, 0] = 0.8f; //stateMat[0, 1] = 0.15f; //stateMat[0, 2] = 0.05f; //stateMat[1, 0] = 0.2f; //stateMat[1, 1] = 0.5f; //stateMat[1, 2] = 0.3f; //stateMat[2, 0] = 0.05f; //stateMat[2, 1] = 0.25f; //stateMat[2, 2] = 0.7f; int Dcount = 0; int Acount = 0; for (int i = 0; i < particleListNew.Count; i++) { if (particleListNew[i].isReady() == false) { particleListNew[i].pAlone.Add(0.5f); continue; } particleListNew[i].pAlone.Add(matRnormalized[i, i]); particleListNew[i].pAlone.RemoveAt(0); //float obserPA = particleListNew[i].pAlone[particleListNew[i].pAlone.Count - tempModel - 1]; Matrix<float> obserMat0 = new Matrix<float>(3, 3); Matrix<float> obserMat1 = new Matrix<float>(3, 3); for (int j = 0; j < 3; j++) { obserMat0[j, j] = condMat[j, 0]; obserMat1[j, j] = condMat[j, 1]; } Matrix<float> S = new Matrix<float>(3, tempModel + 1); S[0, 0] = 0.33f; S[1, 0] = 0.33f; S[2, 0] = 0.33f; for (int j = 0; j < tempModel; j++) { Matrix<float> inMat = new Matrix<float>(3, 1); inMat[0, 0] = S[0, j]; inMat[1, 0] = S[1, j]; inMat[2, 0] = S[2, j]; Matrix<float> outMat = new Matrix<float>(3, 1); float obserPA = particleListNew[i].pAlone[particleListNew[i].pAlone.Count - tempModel + j]; Matrix<float> obserMat = new Matrix<float>(3, 3); if (obserPA > 0.5) obserMat0.CopyTo(obserMat); else obserMat1.CopyTo(obserMat); outMat = obserMat * stateMat.Transpose() * inMat; outMat = outMat / outMat.Sum; S[0, j + 1] = outMat[0, 0]; S[1, j + 1] = outMat[1, 0]; S[2, j + 1] = outMat[2, 0]; } double pDEAD = S[0, tempModel]; double pDYING = S[1, tempModel]; double pALIVE = S[2, tempModel]; //for (int j = 0; j < particleListNew.Count; j++) //{ // if (i == j) // continue; // pG += matRnormalized[i, j] * stateMat[1, particleListNew[i].lastState]; //} //double pA = 1 - pG; //swHere.WriteLine("P(A) = {0}\nP(G) = {1}\nsum = {2}\n", pA, pG, pG+pA); Matrix<float> pOut = new Matrix<float>(3, 1); pOut[0, 0] = (float)pDEAD; pOut[1, 0] = (float)pDYING; pOut[2, 0] = (float)pALIVE; Matrix<float> pResult = new Matrix<float>(4, 1); Point maxValP = new Point(); Point minValP = new Point(); double minVal, maxVal; pOut.MinMax(out minVal, out maxVal, out minValP, out maxValP); switch (maxValP.Y) { case 0: particleListNew[i].lastState = particleListNew[i].state; particleListNew[i].state = 0; break; case 1: particleListNew[i].lastState = particleListNew[i].state; particleListNew[i].state = 1; break; case 2: particleListNew[i].lastState = particleListNew[i].state; particleListNew[i].state = 2; break; } //} } Console.WriteLine("FRAME: {2}\ngrouped = {0}\nalone = {1}", Acount, Dcount, particleListNew[0].GetFrameNow()); //swHere.Flush(); //swHere.Dispose(); }
public void TestImageDFT() { Image<Gray, float> matA = new Image<Gray, float>("stuff.jpg"); //The matrix to be convoled with matA, a bluring filter Matrix<float> matB = new Matrix<float>( new float[,] { {1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f}, {1.0f/16.0f, 8.0f/16.0f, 1.0f/16.0f}, {1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f}}); Image<Gray, float> convolvedImage = new Image<Gray, float>(matA.Size + matB.Size - new Size(1, 1)); Matrix<float> dftA = new Matrix<float>( CvInvoke.cvGetOptimalDFTSize(convolvedImage.Rows), CvInvoke.cvGetOptimalDFTSize(convolvedImage.Cols)); matA.CopyTo(dftA.GetSubRect(matA.ROI)); CvInvoke.cvDFT(dftA, dftA, Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, matA.Rows); Matrix<float> dftB = new Matrix<float>(dftA.Size); matB.CopyTo(dftB.GetSubRect(new Rectangle(Point.Empty, matB.Size))); CvInvoke.cvDFT(dftB, dftB, Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, matB.Rows); CvInvoke.cvMulSpectrums(dftA, dftB, dftA, Emgu.CV.CvEnum.MUL_SPECTRUMS_TYPE.DEFAULT); CvInvoke.cvDFT(dftA, dftA, Emgu.CV.CvEnum.CV_DXT.CV_DXT_INVERSE, convolvedImage.Rows); dftA.GetSubRect(new Rectangle(Point.Empty, convolvedImage.Size)).CopyTo(convolvedImage); }