public void LinearAlgebraSolve() { //using DotNumerics.LinearAlgebra; //using DotNumerics; Matrix A = new Matrix(3, 3); A[0, 0] = 2; A[0, 1] = 5; A[0, 2] = 3; A[1, 0] = 1; A[1, 1] = 5; A[1, 2] = 7; A[2, 0] = 8; A[2, 1] = 2; A[2, 2] = 3; Matrix B = new Matrix(3, 1); B[0, 0] = 5; B[1, 0] = 3; B[2, 0] = 8; LinearEquations leq = new LinearEquations(); Matrix X = leq.Solve(A, B); Matrix AXmB = A * X - B; ObjectDumper.Write("A="); ObjectDumper.Write(A.MatrixToString("0.000")); ObjectDumper.Write("B="); ObjectDumper.Write(B.MatrixToString("0.000")); ObjectDumper.Write("X="); ObjectDumper.Write(X.MatrixToString("0.000")); ObjectDumper.Write("A*X-B ="); ObjectDumper.Write(AXmB.MatrixToString("0.000")); }
private void test() { double[,] t = new double[, ] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, }; MatrixNxM mat = new MatrixNxM(t); //mat.DebugTest(); double[,] t2 = new double[, ] { { 9, -1, -1, 7 }, { -1, 10, -1, 8 }, { -1, -1, 15, 13 }, }; MatrixNxM m2 = new MatrixNxM(t2); LinearEquations le = new LinearEquations(); le.MatrixDataSource = t; //le.Matrix = m2; le.Solve(); }
/// <summary> /// Varies matrix and vector in equation and calculates new solution /// </summary> /// <param name="variation">Variation's value</param> /// <returns>Solution of varied equation</returns> public Vector CalculateVariedSolution(double variation) { var variedMatrix = matrix.Clone(); var variedVector = vector.Clone(); for (var i = 0; i < size; i++) { for (var j = 0; j < size; j++) { variedMatrix[i, j] += variation; } variedVector[i] += variation; } var solver = new LinearEquations(); return(solver.Solve(variedMatrix, variedVector)); }
private void Button_Click(object sender, RoutedEventArgs e) { LinearEquations LE = new LinearEquations(); LE.MatrixDataSource = currentData; LE.Solve(); if (LE.Errored) { textBlockStatus.Foreground = new SolidColorBrush(Colors.Red); } else { textBlockStatus.Foreground = new SolidColorBrush(Colors.Black); } textBlockStatus.Text = LE.ErrorCode.ToString(); dataGridProcessData.ItemsSource = LE.Matrix.Data == null ? null : ConvertArray2DataTable(LE.Matrix.Data).DefaultView; dataGridOutputData.ItemsSource = LE.Result == null ? null : ConvertArray2DataTable(LE.Result).DefaultView; }
private void UpdateCurve() { int l = ControlPoints.Count; if (l > 2) { var MatrixA = new BandMatrix(4 * (l - 1), 4 * (l - 1), 2, 2); //zamienic na 2x2 var MatrixBx = new DotNumerics.LinearAlgebra.Matrix(4 * (l - 1), 1); var MatrixBy = new DotNumerics.LinearAlgebra.Matrix(4 * (l - 1), 1); var MatrixBz = new DotNumerics.LinearAlgebra.Matrix(4 * (l - 1), 1); for (int i = 0; i < l; i++) { if (i == 0) //pierwszy element { MatrixA[0, 2] = 2; //MatrixA[0,3] = 6 * chords[i]; //blad MatrixA[1, 0] = 1; //MatrixA[1,1] = chords[i]; //MatrixA[1,2] = Math.Pow(chords[i], 2); //MatrixA[1,3] = Math.Pow(chords[i], 3); MatrixBx[1, 0] = ControlPoints[0].x; MatrixBx[0, 0] = 0; MatrixBy[1, 0] = ControlPoints[0].y; MatrixBy[0, 0] = 0; MatrixBz[1, 0] = ControlPoints[0].z; MatrixBz[0, 0] = 0; } else if (i == ControlPoints.Count - 1) { //ostatni element MatrixA[4 * (l - 1) - 2, 4 * (l - 1) - 4] = 1; MatrixA[4 * (l - 1) - 2, 4 * (l - 1) - 3] = chords[i - 1]; MatrixA[4 * (l - 1) - 2, 4 * (l - 1) - 2] = Math.Pow(chords[i - 1], 2); MatrixA[4 * (l - 1) - 2, 4 * (l - 1) - 1] = Math.Pow(chords[i - 1], 3); MatrixA[4 * (l - 1) - 1, 4 * (l - 1) - 2] = 2; MatrixBx[4 * (l - 1) - 2, 0] = ControlPoints[i].x; MatrixBx[4 * (l - 1) - 1, 0] = 0; MatrixBy[4 * (l - 1) - 2, 0] = ControlPoints[i].y; MatrixBy[4 * (l - 1) - 1, 0] = 0; MatrixBz[4 * (l - 1) - 2, 0] = ControlPoints[i].z; MatrixBz[4 * (l - 1) - 1, 0] = 0; } //pozostale elementy else { //C0 z poprzednim MatrixA[2 + 4 * (i - 1), 4 * (i - 1)] = 1; MatrixA[2 + 4 * (i - 1), 4 * (i - 1) + 1] = chords[i - 1]; MatrixA[2 + 4 * (i - 1), 4 * (i - 1) + 2] = Math.Pow(chords[i - 1], 2); MatrixA[2 + 4 * (i - 1), 4 * (i - 1) + 3] = Math.Pow(chords[i - 1], 3); MatrixBx[2 + 4 * (i - 1), 0] = ControlPoints[i].x; MatrixBy[2 + 4 * (i - 1), 0] = ControlPoints[i].y; MatrixBz[2 + 4 * (i - 1), 0] = ControlPoints[i].z; //ciaglosc C1 MatrixA[2 + 4 * (i - 1) + 1, 4 * (i - 1) + 1] = -1; MatrixA[2 + 4 * (i - 1) + 1, 4 * (i - 1) + 2] = -2 * chords[i - 1]; MatrixA[2 + 4 * (i - 1) + 1, 4 * (i - 1) + 3] = -3 * Math.Pow(chords[i - 1], 2); MatrixA[2 + 4 * (i - 1) + 1, 4 * (i) + 1] = 1; MatrixBx[2 + 4 * (i - 1) + 1, 0] = 0; MatrixBy[2 + 4 * (i - 1) + 1, 0] = 0; MatrixBz[2 + 4 * (i - 1) + 1, 0] = 0; //ciaglosc C2 MatrixA[2 + 4 * (i - 1) + 2, 4 * (i - 1) + 2] = -2; MatrixA[2 + 4 * (i - 1) + 2, 4 * (i - 1) + 3] = -6 * chords[i - 1]; MatrixA[2 + 4 * (i - 1) + 2, 4 * (i) + 2] = 2; MatrixBx[2 + 4 * (i - 1) + 2, 0] = 0; MatrixBy[2 + 4 * (i - 1) + 2, 0] = 0; MatrixBz[2 + 4 * (i - 1) + 2, 0] = 0; //C0 z nastepnym MatrixA[2 + 4 * (i - 1) + 3, 4 * i] = 1; MatrixBx[2 + 4 * (i - 1) + 3, 0] = ControlPoints[i].x; MatrixBy[2 + 4 * (i - 1) + 3, 0] = ControlPoints[i].y; MatrixBz[2 + 4 * (i - 1) + 3, 0] = ControlPoints[i].z; } } LinearEquations leq = new LinearEquations(); var x = leq.Solve(MatrixA, MatrixBx); var y = leq.Solve(MatrixA, MatrixBy); var z = leq.Solve(MatrixA, MatrixBz); _polyX.Clear(); _polyY.Clear(); _polyZ.Clear(); for (int i = 0; i < l - 1; i++) { _polyX.Add(new Polynomial(3)); _polyX[i][0] = x[4 * i, 0]; _polyX[i][1] = x[4 * i + 1, 0]; _polyX[i][2] = x[4 * i + 2, 0]; _polyX[i][3] = x[4 * i + 3, 0]; _polyY.Add(new Polynomial(3)); _polyY[i][0] = y[4 * i, 0]; _polyY[i][1] = y[4 * i + 1, 0]; _polyY[i][2] = y[4 * i + 2, 0]; _polyY[i][3] = y[4 * i + 3, 0]; _polyZ.Add(new Polynomial(3)); _polyZ[i][0] = z[4 * i, 0]; _polyZ[i][1] = z[4 * i + 1, 0]; _polyZ[i][2] = z[4 * i + 2, 0]; _polyZ[i][3] = z[4 * i + 3, 0]; } } }