public static Matrix CalculatGivensRotation(Complex a, Complex b) { Complex c, s, r; r = ComplexMath.Sqrt(a.Conjugate() * a + b.Conjugate() * b); if (b == 0) { c = 1; s = 0; } else if (a == 0) { c = 0; s = ComplexMath.Sign(b.Conjugate()); } else { c = a.Abs() / r; s = ComplexMath.Sign(a) * b.Conjugate() / r; } Matrix givensRot = new Matrix(2); givensRot[0, 0] = c; givensRot[0, 1] = s; givensRot[1, 0] = -s.Conjugate(); givensRot[1, 1] = c; return givensRot; }
/// <summary> /// RowEchelon /// </summary> /// <returns> </returns> public Matrix RowEchelon() { var echelon = Elements; var rows = (int) Rows; var cols = (int) Cols; var row = 0; var col = 0; for (; row < rows; ) { var maxPivot = new Complex(0); //echelon[row, col] //First Leftmost Max Column var pivotRow = row; for (var i = row; i < rows; ++i) { var pivot = echelon[i, col]; if (!(pivot.Abs() > maxPivot.Abs())) continue; pivotRow = i; maxPivot = pivot; } if (maxPivot == 0) { ++col; if (col == cols) break; continue; } //Interchange rows if (row != pivotRow) { var tmpRow = new Complex[cols]; for (var j = 0; j < cols; ++j) tmpRow[j] = echelon[row, j]; for (var j = 0; j < cols; ++j) echelon[row, j] = echelon[pivotRow, j]; for (var j = 0; j < cols; ++j) echelon[pivotRow, j] = tmpRow[j]; } //Leading 1 for (var j = col; j < cols; ++j) echelon[row, j] /= maxPivot; //Leading 0 for (var i = row + 1; i < rows; ++i) { var multiple = echelon[i, col]; for (var j = col; j < cols; ++j) echelon[i, j] -= multiple * echelon[row, j]; } ++row; ++col; } return new Matrix(echelon); }
/// <summary> /// Reduce RowEchelon /// </summary> /// <returns> </returns> public Matrix ReduceRowEchelon() { var rechelon = RowEchelon().Elements; var rows = (int) Rows; var cols = (int) Cols; for (var row = rows - 1; row >= 0; --row) { var col = 0; var nonzero = new Complex(0); //First Leftmost Nonzero Column (Leading 1) for (; col < cols; ++col) { nonzero = rechelon[row, col]; if (Math.Abs(nonzero.Abs() - 0) > EPSILON) break; } if (nonzero == 0) continue; //Trailing 0 for (var i = row - 1; i >= 0; --i) { var multiple = rechelon[i, col]; for (uint j = 0; j < cols; ++j) rechelon[i, j] -= multiple * rechelon[row, j]; } } return new Matrix(rechelon); }