/// <summary> /// Computes the solution to a real system of linear equations A * X = B, where A is a general matrix. /// </summary> /// <param name="A">The square matrix.</param> /// <param name="B">The vector containing the right-hand side of the linear system.</param> /// <returns>A vector containing the solution to the linear system of equations.</returns> public Vector Solve(Matrix A, Vector B) { this.CheckDimensions(A, B); Matrix Solution = B.Clone(); Matrix AClon = A.Clone(); this.SolveInplace(AClon, Solution); return Solution.GetColumnVector(0); }
/// <summary> /// Computes the solution to a real system of linear equations A * X = B, where A is a general matrix. /// </summary> /// <param name="A">The square matrix.</param> /// <param name="B">The vector containing the right-hand side of the linear system.</param> /// <returns>A vector containing the solution to the linear system of equations.</returns> public double[] Solve(double[,] A, double[] B) { Vector Solution = new Vector(B); Matrix AClon = new Matrix(A); this.CheckDimensions(AClon, Solution); this.SolveInplace(AClon, Solution); double[] solutionDate = Solution.Data; return solutionDate; }
/// <summary> /// Unary minus. /// </summary> /// <returns> /// Vector r[i] = -this[i] /// </returns> public Vector UnaryMinus() { Vector v = new Vector(this._Type, this.Length); double[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] -= this._Data[i] ; } return v; }
/// <summary> /// Transposed vector. /// </summary> /// <returns>The transposed vector.</returns> /// <remarks> /// Transposition turns a row vector into a column vector ( Or a column vector into a row vector). /// </remarks> public Vector Transpose() { Vector AT = new Vector(this._Data); if (this._Type == VectorType.Column) AT.Type = VectorType.Row; else AT.Type = VectorType.Column; return AT; }
/// <summary> /// In place add a Vector. /// </summary> /// <param name="B">The vector B.</param> public void SubtractInplace(Vector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } for (int i = 0; i < this._Data.Length; i++) { this._Data[i] -= B[i]; } }
/// <summary> /// Subtract all elements of this vector from a scalar. /// </summary> /// <param name="s">The scalar.</param> /// <returns> /// Vector r[i] = this[i] - s /// </returns> public Vector SubtractFrom(double s) { Vector v = new Vector(this._Type, this.Length); double[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] = s - this._Data[i]; } return v; }
/// <summary> /// Subtract a Vector. /// </summary> /// <param name="B">The vector B.</param> /// <returns> /// Vector r[i] = this[i] - B[i] /// </returns> public Vector Subtract(Vector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } Vector r = new Vector(this._Type, this.Length); double[] rData = r.Data; for (int i = 0; i < rData.Length; i++) { rData[i] = this._Data[i] - B[i]; } return r; }
/// <summary> /// Multiply a scalar to all elements of this vector. /// </summary> /// <param name="s">The scalar.</param> /// <returns> /// Vector r[i] = this[i] * s /// </returns> public Vector Multiply(double s) { Vector v = new Vector(this._Type, this.Length); double[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] = this._Data[i] * s; } return v; }
/// <summary> /// Dot product of this vector with another vector. /// </summary> /// <param name="B">The other vector.</param> /// <remarks> /// The dot product is the result of multiplying all the components of two vectors together and adding the results, res= Sum(A[i]*B[i]). /// </remarks> /// <returns>r = Sum(this[i]*B[i])</returns> public double DotProduct( Vector B) { return Vector.DotProduct(this, B); }
/// <summary> /// Creates a copy of the vector. /// </summary> /// <returns>The copy of the vector.</returns> public Vector Clone() { Vector NewVector = new Vector(this._Type, this._Data); return NewVector; }
///// <summary> ///// Vector - Vector multiplication. ///// Row Vector * Column Vector: Inner product. ///// Column Vector * Row Vector: Outer product. ///// </summary> ///// <param name="A"> The left side vector of the multiplication operator.</param> ///// <param name="B">The right side vector of the multiplication operator.</param> ///// <returns>A value that represents the result of the vector multiplication.</returns> ///// <remarks> ///// The dot product is the result of multiplying all the components of two vectors together and adding the results. ///// </remarks> //public static Matrix operator *(Vector A, Vector B) //{ // Matrix matrixA = A; // Matrix matrixB = B; // return matrixA * matrixB; //} /// <summary> /// Dot product or scalar product. /// </summary> /// <param name="A"> The left side vector of the operator.</param> /// <param name="B">The right side vector of the operator.</param> /// <remarks> /// The dot product is the result of multiplying all the components of two vectors together and adding the results, res= Sum(A[i]*B[i]). /// </remarks> /// <returns>The dot product = Sum(A[i]*B[i])</returns> public static double DotProduct(Vector A, Vector B) { //if (A.Type != VectorType.Row || B.Type != VectorType.Column || B.Length != A.Length) //{ // throw new System.ArgumentException("Vector dimensions or type are not valid."); //} if ( B.Length != A.Length) { throw new System.ArgumentException("Vector dimensions must agree."); } double C = 0.0; double[] AData = A.Data; double[] BData = B.Data; for (int i = 0; i < AData.Length; i++) { C += AData[i] * BData[i]; } return C; }
/// <summary> ///Computes the singular value decomposition (SVD) of a real /// M-by-N matrix A. /// The SVD is written /// A = U * S * transpose(V) /// </summary> /// <param name="A">The A matrix.</param> /// <param name="S">A vector of singular values.</param> public void ComputeSVD(Matrix A, out Vector S) { if (this._dgesvd == null) this._dgesvd = new DGESVD(); Matrix ACopy = A.Clone(); double[] ACopyData = ACopy.Data; S = new Vector(Math.Min(A.RowCount, A.ColumnCount));// (output) DOUBLE PRECISION array, dimension (min(M,N)) double[] SingularValuesData = S.Data; Matrix U = new Matrix(1, 1); // A is MxN, U is MxM, como aqui no se requiere no importa double[] UData = U.Data; Matrix VT = new Matrix(1, 1);// A is MxN, V is NxN, como aqui no se requiere no importa double[] VTData = VT.Data; double[] Work = new double[1]; int LWork = -1; int Info = 0; //Calculamos LWORK this._dgesvd.Run("N", "N", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.RowCount, ref Work, 0, LWork, ref Info); LWork = Convert.ToInt32(Work[0]); if (LWork > 0) { Work = new double[LWork]; _dgesvd.Run("N", "N", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.RowCount, ref Work, 0, LWork, ref Info); } else { //Error } #region Error // <param name="INFO"> // (output) INTEGER // = 0: successful exit. // .LT. 0: if INFO = -i, the i-th argument had an illegal value. // .GT. 0: if DBDSQR did not converge, INFO specifies how many // superdiagonals of an intermediate bidiagonal form B // did not converge to zero. See the description of WORK // above for details. 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("DBDSQR did not converge."); } #endregion }
private void CheckDimensions(BaseMatrix matrixA, Vector vectorB) { if (matrixA.IsSquare != true) { throw new System.ArgumentException("Matrix A is not a square matrix."); } if (vectorB.Type != VectorType.Column) { throw new System.ArgumentException("The vector should be a column vector."); } if (matrixA.RowCount != vectorB.Length) { throw new System.ArgumentException("Matrix dimensions are not valid."); } }
/// <summary> /// Computes the solution to a real system of linear equations /// A * X = B, where A is a band matrix. /// </summary> /// <param name="A">The band matrix.</param> /// <param name="B">The vector containing the right-hand side of the linear system.</param> /// <returns>A vector containing the solution to the linear system of equations.</returns> public Vector Solve(BandMatrix A, Vector B) { Matrix myB = B; Vector solution = this.Solve(A, myB).GetColumnVector(0); return this.Solve(A, B); }
public VectorDebuggerDisplay(Vector TheVector) { this.MeVector = TheVector; }
public MainViewModel() { Model = new PlotModel { Title = "Chart" }; Model.Axes.Add(new LinearColorAxis { Position = AxisPosition.None, Minimum = 0.1, Maximum = 0.9, HighColor = OxyColors.Red, LowColor = OxyColors.Black }); Model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Condition number", FontSize = 14 }); Model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Solutions difference", FontSize = 14 }); var series = new ScatterSeries[8]; series[0] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[1] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[2] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[3] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[4] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[5] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[6] = new ScatterSeries { MarkerType = MarkerType.Circle }; series[7] = new ScatterSeries { MarkerType = MarkerType.Circle }; var equations = new List <(Matrix, DotNumerics.LinearAlgebra.Vector, DotNumerics.LinearAlgebra.Vector)>(); var matrix1 = new Matrix(2, 2); matrix1[0, 0] = -400.6; matrix1[0, 1] = 199.8; matrix1[1, 0] = 1198.8; matrix1[1, 1] = -600.4; var vector1 = new DotNumerics.LinearAlgebra.Vector(new double[] { 200, -600 }); var exactSolution1 = new DotNumerics.LinearAlgebra.Vector(new double[] { -0.2, 0.6 }); equations.Add((matrix1, vector1, exactSolution1)); var matrix2 = new Matrix(2, 2); matrix2[0, 0] = -401.98; matrix2[0, 1] = 200.34; matrix2[1, 0] = 1202.04; matrix2[1, 1] = -602.32; var vector2 = new DotNumerics.LinearAlgebra.Vector(new double[] { 722.264, -2166.272 }); var exactSolution2 = new DotNumerics.LinearAlgebra.Vector(new double[] { -0.8, 2 }); equations.Add((matrix2, vector2, exactSolution2)); var matrix3 = new Matrix(2, 2); matrix3[0, 0] = -400.94; matrix3[0, 1] = 200.2; matrix3[1, 0] = 1200.12; matrix3[1, 1] = -600.96; var vector3 = new DotNumerics.LinearAlgebra.Vector(new double[] { -160.268, 480.408 }); var exactSolution3 = new DotNumerics.LinearAlgebra.Vector(new double[] { 0.2, -0.4 }); equations.Add((matrix3, vector3, exactSolution3)); var matrix4 = new Matrix(2, 2); matrix4[0, 0] = -41; matrix4[0, 1] = 127; matrix4[1, 0] = 113; matrix4[1, 1] = -60; var vector4 = new DotNumerics.LinearAlgebra.Vector(new double[] { -787, 1589 }); var exactSolution4 = new DotNumerics.LinearAlgebra.Vector(new double[] { 13, -2 }); equations.Add((matrix4, vector4, exactSolution4)); var matrix5 = new Matrix(2, 2); matrix5[0, 0] = -402.9; matrix5[0, 1] = 200.7; matrix5[1, 0] = 1204.2; matrix5[1, 1] = -603.6; var vector5 = new DotNumerics.LinearAlgebra.Vector(new double[] { 201, -603 }); var exactSolution5 = new DotNumerics.LinearAlgebra.Vector(new double[] { -0.2, 0.6 }); equations.Add((matrix5, vector5, exactSolution5)); // bad condition matrix var matrix6 = new Matrix(2, 2); matrix6[0, 0] = 1; matrix6[0, 1] = 0.99; matrix6[1, 0] = 0.99; matrix6[1, 1] = 0.98; var vector6 = new DotNumerics.LinearAlgebra.Vector(new double[] { 2, 2 }); var exactSolution6 = new DotNumerics.LinearAlgebra.Vector(new double[] { 200, -200 }); equations.Add((matrix6, vector6, exactSolution6)); // Hilbert matrix var matrix7 = new Matrix(7, 7); for (var i = 0; i < 7; i++) { for (var j = 0; j < 7; j++) { matrix7[i, j] = 1; matrix7[i, j] /= 1 + i + j; } } var vector7 = new DotNumerics.LinearAlgebra.Vector(new double[] { 1, 1, 1, 1, 1, 1, 1 }); vector7[0] = vector7[0] * 5699 / 420; vector7[1] = vector7[1] * 4103 / 420; vector7[2] = vector7[2] * 19661 / 2520; vector7[3] = vector7[3] * 157 / 24; vector7[4] = vector7[4] * 156631 / 27720; vector7[5] = vector7[5] * 34523 / 6930; vector7[6] = vector7[6] * 146077 / 32760; var exactSolution7 = new DotNumerics.LinearAlgebra.Vector(new double[] { 2, 9, 4, 7, 11, 9, 2 }); equations.Add((matrix7, vector7, exactSolution7)); for (var i = 0; i < 7; i++) { var conditionNumber = new ConditionNumber(equations[i].Item1, equations[i].Item2, equations[i].Item3); var spectralCriterion = conditionNumber.CalculateSpectralCriterion(); var volumetricCriterion = conditionNumber.CalculateVolumetricCriterion(); var angleCriterion = conditionNumber.CalculateAngleCriterion(); var variedSolution1 = conditionNumber.CalculateVariedSolution(0.1); var variedSolution2 = conditionNumber.CalculateVariedSolution(0.01); var variedSolution3 = conditionNumber.CalculateVariedSolution(0.001); var difference1 = equations[i].Item3 - variedSolution1; var difference2 = equations[i].Item3 - variedSolution2; var difference3 = equations[i].Item3 - variedSolution3; double delta1 = 0; double delta2 = 0; double delta3 = 0; for (var j = 0; j < equations[i].Item1.RowCount; j++) { delta1 += difference1[j] * difference1[j]; delta2 += difference2[j] * difference2[j]; delta3 += difference3[j] * difference3[j]; } delta1 = Math.Sqrt(delta1); delta2 = Math.Sqrt(delta2); delta3 = Math.Sqrt(delta3); // red dots series[i].Points.Add(new ScatterPoint(spectralCriterion, delta1, 3, 1)); // green dots series[i].Points.Add(new ScatterPoint(spectralCriterion, delta2, 3, 0.4)); // black dots series[i].Points.Add(new ScatterPoint(spectralCriterion, delta3, 3, 0)); Model.Series.Add(series[i]); } }