/// <summary> /// Compute the inverse of the upper triangle (in place). /// </summary> public static void InvertUpperTriangle(this DenseMatrix matrix) { int n = matrix.RowCount; Complex sum; for (int j = n - 1; j > -1; j--) { matrix.At(j, j, 1.0 / matrix.At(j, j)); for (int i = j - 1; i > -1; i--) { sum = 0.0; for (int k = j; k > i; k--) { sum -= matrix.At(i, k) * matrix.At(k, j); } matrix.At(i, j, sum / matrix.At(i, i)); } } }
/// <summary> /// Compute the inverse of the lower triangle (in place). /// </summary> public static void InvertLowerTriangle(this DenseMatrix matrix) { int n = matrix.RowCount; Complex sum; for (int j = 0; j < n; j++) { matrix.At(j, j, 1.0 / matrix.At(j, j)); for (int i = j + 1; i < n; i++) { sum = 0.0; for (int k = j; k < i; k++) { sum -= matrix.At(i, k) * matrix.At(k, j); } matrix.At(i, j, sum / matrix.At(i, i)); } } }
/// <summary> /// Outer product of two vectors /// </summary> /// <param name="u">First vector</param> /// <param name="v">Second vector</param> /// <returns>Matrix M[i,j] = u[i]*v[j] </returns> /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception> public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) { if (u == null) { throw new ArgumentNullException("u"); } if (v == null) { throw new ArgumentNullException("v"); } var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( 0, u.Count, i => { for (var j = 0; j < v.Count; j++) { matrix.At(i, j, u._values[i] * v._values[j]); } }); return matrix; }
/// <summary> /// Create a matrix based on this vector in row form (one single row). /// </summary> /// <returns>This vector as a row matrix.</returns> public override Matrix<Complex> ToRowMatrix() { var matrix = new DenseMatrix(1, _length); for (var i = 0; i < _values.Length; i++) { matrix.At(0, i, _values[i]); } return matrix; }
/// <summary> /// Create a matrix based on this vector in column form (one single column). /// </summary> /// <returns>This vector as a column matrix.</returns> public override Matrix<Complex> ToColumnMatrix() { var matrix = new DenseMatrix(_length, 1); for (var i = 0; i < _values.Length; i++) { matrix.At(i, 0, _values[i]); } return matrix; }
/// <summary> /// Create a matrix based on this vector in row form (one single row). /// </summary> /// <returns>This vector as a row matrix.</returns> public override Matrix<Complex> ToRowMatrix() { var matrix = new DenseMatrix(1, Count); for (var i = 0; i < Data.Length; i++) { matrix.At(0, i, Data[i]); } return matrix; }
/// <summary> /// Create a matrix based on this vector in column form (one single column). /// </summary> /// <returns>This vector as a column matrix.</returns> public override Matrix<Complex> ToColumnMatrix() { var matrix = new DenseMatrix(Count, 1); for (var i = 0; i < Data.Length; i++) { matrix.At(i, 0, Data[i]); } return matrix; }