/// <summary> /// Computes all eigenvalues and eigenvectors of a of a real symmetric matrix A. /// </summary> /// <param name="A">The real symmetric matrix A.</param> /// <param name="EigenVects">The eigenvectors.</param> /// <returns>The eigenvalues.</returns> public Matrix GetEigenvalues(SymmetricMatrix A, out Matrix EigenVects) { if (this._dsyev == null) this._dsyev = new DSYEV(); this.CheckDimensions(A); EigenVects = new Matrix(A.RowCount, A.ColumnCount, A.Data); double[] EigenVectsData = EigenVects.Data; Matrix EigenVals = new Matrix(A.RowCount, 1); double[] EigenValsData = EigenVals.Data; int Info = 0; double[] Work = new double[1]; int LWork = -1; //Calculamos LWORK ideal _dsyev.Run("V", "U", A.RowCount, ref EigenVectsData, 0, A.RowCount, ref EigenValsData, 0, ref Work, 0, LWork, ref Info); LWork = Convert.ToInt32(Work[0]); if (LWork > 0) { Work = new double[LWork]; _dsyev.Run("V", "U", A.RowCount, ref EigenVectsData, 0, A.RowCount, ref EigenValsData, 0, ref Work, 0, LWork, ref Info); } else { //Error } #region Error /// = 0: successful exit /// .LT. 0: if INFO = -i, the i-th argument had an illegal value /// .GT. 0: if INFO = i, the algorithm failed to converge; i /// off-diagonal elements of an intermediate tridiagonal /// form did not converge to zero. if (Info < 0) { string infoSTg = Math.Abs(Info).ToString(); throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value"); } else if (Info > 0) { string infoSTg = Math.Abs(Info).ToString(); throw new Exception("The algorithm failed to converge."); } #endregion return EigenVals; }
public SymmetricMatrix Clone() { SymmetricMatrix NewMatrix = new SymmetricMatrix(this._RowCount, this._Data); return NewMatrix; }
/// <summary> /// Matrix subtraction. /// </summary> /// <param name="A"> The left side matrix of the subtraction operator.</param> /// <param name="B">The right side matrix of the subtraction operator.</param> /// <returns>A matrix that represents the result of the matrix subtraction.</returns> public static SymmetricMatrix operator -(SymmetricMatrix A, SymmetricMatrix B) { if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount) { throw new System.ArgumentException("Matrix dimensions are not valid."); } SymmetricMatrix C = new SymmetricMatrix(A.RowCount); double[] AData = A.Data; double[] BData = B.Data; double[] CData = C.Data; for (int i = 0; i < AData.Length; i++) { CData[i] = AData[i] - BData[i]; } return C; }
/// <summary> /// Generate a matrix with random elements /// </summary> /// <param name="size">Size</param> /// <param name="Seed"> /// A number used to calculate a starting value for the pseudo-random number /// sequence. If a negative number is specified, the absolute value of the number /// is used. /// </param> /// <returns>An m-by-n matrix with uniformly distributed /// random elements in <c>[0, 1)</c> interval.</returns> public static SymmetricMatrix Random(int size, int Seed) { System.Random random = new System.Random(Seed); SymmetricMatrix X = new SymmetricMatrix(size); double[] XData = X.Data; for (int j = 0; j < X.ColumnCount; j++) { for (int i = 0; i < X.RowCount; i++) { X[i, j] = random.NextDouble(); } } return X; }
/// <summary> /// Scalar-Matrix multiplication. /// </summary> /// <param name="s"> The left side scalar of the multiplication operator.</param> /// <param name="A">The right side matrix of the multiplication operator.</param> /// <returns>A matrix that represents the result of the multiplication.</returns> public static SymmetricMatrix operator *(double s, SymmetricMatrix A) { SymmetricMatrix C = new SymmetricMatrix(A.RowCount); double[] AData = A.Data; double[] CData = C.Data; Matrix.MultiplicationSM(s, AData, CData); return C; }