public Fraction determinant() { QuadMatr m = new QuadMatr(this.NCol); Array.Copy(this.A, m.A, this.A.Length); int n = this.NCol; Fraction fr1, fr2; Fraction d = new Fraction(1, 1); double sumRow = 0, sumCol = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (m.A[i, j] == 0) { sumRow = 0; sumCol = 0; for (int p = 0; p < n; p++) { sumCol += Math.Abs(m.A[i, p].ToDouble()); sumRow += Math.Abs(m.A[p, j].ToDouble()); } if (sumCol == 0 || sumRow == 0) { return(new Fraction()); } } } } for (int shag = 0; shag < n - 1; shag++) { for (int i = shag; i < n; i++) { if (m.A[i, shag] != 0) { if (m.A[shag, shag] == 0) { this.swapRows(shag, i); } else { for (int j = 0; j < n; j++) { if (i != j && m.A[j, shag] != 0 && j >= shag) { if (j < i) { fr1 = m.A[i, shag]; fr2 = m.A[j, shag]; m.multRows(j, i, fr1 / fr2); } else { fr1 = m.A[j, shag]; fr2 = m.A[i, shag]; m.multRows(i, j, fr1 / fr2); } } } } } } } for (int i = 0; i < n; i++) { d *= m.A[i, i]; } return(d); }
public QuadMatr TransformMatr(int type) { if (this.determinant() == 0) { throw new Exception("Определитель матрицы равен нулю, обратная матрица не существует!"); } else { Fraction m = new Fraction(); int n = this.NCol; QuadMatr e = new QuadMatr(n); QuadMatr x = new QuadMatr(n); QuadMatr y = new QuadMatr(n); QuadMatr b = new QuadMatr(n); QuadMatr c = new QuadMatr(n); e.setEMatr(); for (int j = 0; j < n; j++) { for (int i = j; i < n; i++) { for (int g = 0; g <= j - 1; g++) { m += b.A[i, g] * c.A[g, j]; } b.A[i, j] = this.A[i, j] - m; m.FractTxt = "0"; for (int g = 0; g <= j - 1; g++) { m += b.A[j, g] * c.A[g, i]; } c.A[j, i] = (this.A[j, i] - m) / b.A[j, j]; m.FractTxt = "0"; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int g = 0; g <= j - 1; g++) { m += b.A[j, g] * y.A[g, i]; } y.A[j, i] = (e.A[j, i] - m) / b.A[j, j]; m.FractTxt = "0"; } } for (int i = 0; i < n; i++) { for (int j = n - 1; j >= 0; j--) { for (int g = 0; g < n - j - 1; g++) { m += c.A[j, n - g - 1] * x.A[n - g - 1, i]; } x.A[j, i] = y.A[j, i] - m; m.FractTxt = "0"; } } switch (type) { case 1: return(x); case 2: return(b); default: return(c); } } }
public QuadMatr getFrobeniusMatr() { int n = this.NCol; QuadMatr mFr = new QuadMatr(n); QuadMatr e = new QuadMatr(n); QuadMatr m1 = new QuadMatr(n); Array.Copy(this.A, mFr.A, this.A.Length); for (int g = 0; g < n - 1; g++) { Fraction d = new Fraction(); for (int i = 0; i <= n - g - 2; i++) { d += mFr.A[n - g - 1, i]; } if (d == 0) { QuadMatr b = new QuadMatr(n - g - 1); for (int i = 0; i < n - g - 1; i++) { for (int j = 0; j < n - g - 1; j++) { b.A[i, j] = mFr.A[i, j]; } } b = b.getFrobeniusMatr(); for (int i = 0; i < n - g - 1; i++) { for (int j = 0; j < n - g - 1; j++) { mFr.A[i, j] = b.A[i, j]; mFr.A[i, j].Color = Color.FromRgb(127, 251, 189); } } for (int i = n - g - 1; i < n; i++) { for (int j = n - g - 1; j < n; j++) { mFr.A[i, j].Color = Color.FromRgb(127, 251, 189); } } return(mFr); } else if (mFr.A[n - g - 1, n - g - 2] == 0) { mFr.swapCols(n - g - 1, n - g - 2); mFr.swapRows(n - g - 1, n - g - 2); } e.setEMatr(); for (int i = 0; i < n; i++) { e.A[n - g - 2, i].FractTxt = mFr.A[n - g - 1, i].FractTxt; } if (e.determinant() != 0) { mFr = (e * mFr * e.TransformMatr(1)); } else { mFr = (e * mFr * e.TransformMatr(1)); } } return(mFr); }
public Fraction GetPolyNewtonDiff2Val(Fraction fr, int poryadok, int row = 1) { if (poryadok >= this.NRow - row - 1) { poryadok = this.NRow - row - 1; if (poryadok >= 5) { poryadok = 5; } } Fraction rez = new Fraction(); if (isRavnom()) { Fraction m = new Fraction(); Fraction q = new Fraction(); Fraction k = new Fraction(); Fraction l1 = new Fraction(); Fraction l2 = new Fraction(); Fraction l3 = new Fraction(); Fraction l4 = new Fraction(); Fraction l5 = new Fraction(); Fraction h = new Fraction(); m = this.matr.A[1, 0].CopyFr; k = this.matr.A[2, 0].CopyFr; h = k - m; k = this.matr.A[row, 0].CopyFr; q = (fr - k) / h; switch (poryadok) { case 1: l1 = this.matr.A[row, 2].CopyFr; rez = l1; break; case 2: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; rez = h.Invert() * (l1 + l2 * (q * 2 - 1) / MyFuncs.factorial(2)); break; case 3: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; rez = h.Invert().Power(2) * (l2 + l3 * (q - 1)); break; case 4: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; l4 = this.matr.A[row, 5].CopyFr; rez = h.Invert().Power(2) * (l2 + l3 * (q - 1) + l4 * (6 * q.Power(2) - 18 * q + 11) / 12); break; case 5: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; l4 = this.matr.A[row, 5].CopyFr; l5 = this.matr.A[row, 6].CopyFr; rez = h.Invert().Power(2) * (l2 + l3 * (q - 1) + l4 * (6 * q.Power(2) - 18 * q + 11) / 12 + l5 * (2 * q.Power(3) - 12 * q.Power(2) + 21 * q - 10) / 12); break; default: MessageBox.Show("Максимальный поддерживаемый порядок = 5!"); break; } } return(rez); }
public Fraction GetPolyNewtonDiff1Val(Fraction fr, int poryadok, int row = 1) { if (poryadok >= this.NRow - row - 1) { poryadok = this.NRow - row - 1; if (poryadok >= 5) { poryadok = 5; } } Fraction rez = new Fraction(); if (isRavnom()) { Fraction q = new Fraction(); Fraction l1 = new Fraction(); Fraction l2 = new Fraction(); Fraction l3 = new Fraction(); Fraction l4 = new Fraction(); Fraction l5 = new Fraction(); Fraction h = new Fraction(); q = this.matr.A[1, 0].CopyFr; h = this.matr.A[2, 0].CopyFr; h -= q; q = this.matr.A[row, 0].CopyFr; q = (fr - q) / h; switch (poryadok) { case 1: l1 = this.matr.A[row, 2].CopyFr; rez = h.Invert() * (l1); break; case 2: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; rez = h.Invert() * (l1 + l2 * (2 * q - 1) / 2); break; case 3: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; rez = h.Invert() * (l1 + l2 * (2 * q - 1) / 2 + l3 * (3 * q.Power(2) - 6 * q + 2) / 6); break; case 4: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; l4 = this.matr.A[row, 5].CopyFr; rez = h.Invert() * (l1 + l2 * (2 * q - 1) / 2 + l3 * (3 * q.Power(2) - 6 * q + 2) / 6 + l4 * (2 * q.Power(3) - 9 * q.Power(2) + 11 * q - 3) / 12); break; case 5: l1 = this.matr.A[row, 2].CopyFr; l2 = this.matr.A[row, 3].CopyFr; l3 = this.matr.A[row, 4].CopyFr; l4 = this.matr.A[row, 5].CopyFr; l5 = this.matr.A[row, 6].CopyFr; rez = h.Invert() * (l1 + l2 * (2 * q - 1) / 2 + l3 * (3 * q.Power(2) - 6 * q + 2) / 6 + l4 * (2 * q.Power(3) - 9 * q.Power(2) + 11 * q - 3) / 12 + l5 * (5 * q.Power(4) - 40 * q.Power(3) + 105 * q.Power(2) - 100 * q + 24) / 120); break; default: MessageBox.Show("Максимальный поддерживаемый порядок = 5!"); break; } } return(rez); }
public void DrawIntegr(ref Grid gr, Fraction a, Fraction b, int res = 100) { int por = this.NCol - 2; int row = 1; Fraction xfrMin = new Fraction(); Fraction yfrMin = new Fraction(); Fraction xfrMax = new Fraction(); Fraction yfrMax = new Fraction(); //Fraction xfr0 = new Fraction((int)((gr.Width - 20) / 2), 1); //Fraction yfr0 = new Fraction((int)((gr.Height - 20) / 2), 1); xfrMin = this.matr.A[1, 0].CopyFr; //yfrMin.FractTxt = this.txtBxs[1, 1].Text; //double yMin = yfrMin.ToDouble(); Fraction h = new Fraction(); xfrMax = this.matr.A[this.NRow - 1, 0].CopyFr; //double xMax = xfrMax.ToDouble(); yfrMax = this.matr.A[this.NRow - 1, 1].CopyFr; yfrMin = this.matr.A[1, 1].CopyFr; for (Fraction i = xfrMin; i <= xfrMax; i += (xfrMax - xfrMin) / 50) { if (GetPolyNewtonVal(i, por, row) >= yfrMax) { yfrMax = GetPolyNewtonVal(i, por, row); } } for (Fraction i = xfrMin; i <= xfrMax; i += (xfrMax - xfrMin) / 50) { if (GetPolyNewtonVal(i, por, row) < yfrMin) { yfrMin = GetPolyNewtonVal(i, por, row); } } CoordAxis axis = new CoordAxis(ref gr, xfrMin.ToDouble(), xfrMax.ToDouble(), yfrMin.ToDouble(), yfrMax.ToDouble(), 10); //Fraction yMult = new Fraction((int)((gr.Height - 20) * 1000), 1000); h = (b - a) / res; PointCollection pc = new PointCollection(); for (Fraction i = a; i <= b; i += h) { //pc.Add(new System.Windows.Point((xfr0 + i*xMult).ToDouble(), (yfr0 - GetPolyNewtonVal(i, por, row)*yMult).ToDouble())); pc.Add(axis.ConvertCoord(i.ToDouble(), GetPolyNewtonVal(i, por, row).ToDouble())); } //double wdt = gr.Width - 20; //double hgt = gr.Height - 20; PointCollection pgPt = new PointCollection(); pgPt.Add(axis.ConvertCoord(a.ToDouble(), 0)); foreach (Point pt in pc) { pgPt.Add(pt); } pgPt.Add(axis.ConvertCoord(b.ToDouble(), 0)); Polygon pg = new Polygon(); pg.Points = pgPt; pg.Fill = Brushes.Pink; gr.Children.Add(pg); }
public void DrawPolynom(ref Grid gr, int por, Fraction x, int row = 1, int res = 100) { Fraction xfrMin = new Fraction(); Fraction yfrMin = new Fraction(); Fraction xfrMax = new Fraction(); Fraction yfrMax = new Fraction(); xfrMin = this.matr.A[1, 0].CopyFr; yfrMin = this.matr.A[1, 1].CopyFr; //double yMin = yfrMin.ToDouble(); Fraction h = new Fraction(); xfrMax = this.matr.A[this.NRow - 1, 0].CopyFr; //double xMax = xfrMax.ToDouble(); yfrMax = this.matr.A[this.NRow - 1, 1].CopyFr; for (Fraction i = xfrMin; i <= xfrMax; i += (xfrMax - xfrMin) / res) { if (GetPolyNewtonVal(i, por, row) >= yfrMax) { yfrMax = GetPolyNewtonVal(i, por, row); } } for (Fraction i = xfrMin; i <= xfrMax; i += (xfrMax - xfrMin) / res) { if (GetPolyNewtonVal(i, por, row) < yfrMin) { yfrMin = GetPolyNewtonVal(i, por, row); } } CoordAxis axis = new CoordAxis(ref gr, xfrMin.ToDouble(), xfrMax.ToDouble(), yfrMin.ToDouble(), yfrMax.ToDouble(), 10); axis.DrawAxis(); //Fraction xMult = new Fraction((int)((gr.Width - 20)*1000), 1000); //xMult /= xfrMax*2; //Fraction yMult = new Fraction((int)((gr.Height - 20)*1000), 1000); //yMult /= yfrMax*2; h = (xfrMax - xfrMin) / res; Polyline pl = new Polyline(); Ellipse el = new Ellipse(); el.HorizontalAlignment = HorizontalAlignment.Left; el.VerticalAlignment = VerticalAlignment.Top; el.Width = 8; el.Height = 8; Point valXY = axis.ConvertCoord(x.ToDouble(), (GetPolyNewtonVal(x, por, row).ToDouble())); System.Windows.Thickness th = new System.Windows.Thickness(valXY.X - el.Width / 2, valXY.Y - el.Height / 2, 0, 0); el.Margin = th; el.Stroke = Brushes.Red; el.Fill = Brushes.Red; PointCollection pc = new PointCollection(); for (Fraction i = xfrMin; i <= xfrMax; i += h) { //pc.Add(new System.Windows.Point((xfr0 + i*xMult).ToDouble(), (yfr0 - GetPolyNewtonVal(i, por, row)*yMult).ToDouble())); pc.Add(axis.ConvertCoord(i.ToDouble(), GetPolyNewtonVal(i, por, row).ToDouble())); } pl.Points = pc; pl.Stroke = Brushes.Blue; pl.StrokeThickness = 4; double wdt = gr.Width - 20; double hgt = gr.Height - 20; Line xVal = new Line(); //xVal.X1 = (xfr0 + x * xMult).ToDouble(); xVal.X1 = axis.ConvertCoord(x.ToDouble(), GetPolyNewtonVal(x, por, row).ToDouble()).X; xVal.Y1 = axis.ConvertCoord(x.ToDouble(), GetPolyNewtonVal(x, por, row).ToDouble()).Y; xVal.X2 = xVal.X1; xVal.Y2 = axis.crossAxis.Y; xVal.Stroke = Brushes.Red; xVal.StrokeDashArray = new DoubleCollection() { 8, 4 }; Line yVal = new Line(); yVal.X1 = axis.ConvertCoord(x.ToDouble(), GetPolyNewtonVal(x, por, row).ToDouble()).X; yVal.Y1 = axis.ConvertCoord(x.ToDouble(), GetPolyNewtonVal(x, por, row).ToDouble()).Y; yVal.X2 = axis.crossAxis.X; yVal.Y2 = yVal.Y1; yVal.Stroke = Brushes.Red; yVal.StrokeDashArray = new DoubleCollection() { 8, 4 }; gr.Children.Add(pl); gr.Children.Add(xVal); gr.Children.Add(yVal); TextBox valX = new TextBox(); valX.Text = "" + x.ToDouble(); valX.Width = 100; valX.Height = 25; valX.HorizontalAlignment = HorizontalAlignment.Left; valX.VerticalAlignment = VerticalAlignment.Top; TextBox valY = new TextBox(); valY.Text = "" + GetPolyNewtonVal(x, por, row).ToDouble(); valY.Width = 200; valY.Height = 25; valY.HorizontalAlignment = HorizontalAlignment.Left; valY.VerticalAlignment = VerticalAlignment.Top; TextBox polyErr = new TextBox(); TextBox diff1 = new TextBox(); TextBox diff2 = new TextBox(); TextBox diff1Err = new TextBox(); TextBox diff2Err = new TextBox(); polyErr.Text = "погр. интерполирования равна " + GetPolyNewtonErr(x, por, row).ToDouble(); diff1.Text = "первая производная равна " + GetPolyNewtonDiff1Val(x, por, row).ToDouble(); diff1Err.Text = "ошибка первой производной " + GetPolyNewtonDiff1Err(x, por, row).ToDouble(); diff2.Text = "вторая производная равна " + GetPolyNewtonDiff2Val(x, por, row).ToDouble(); diff2Err.Text = "ошибка второй производной " + GetPolyNewtonDiff2Err(x, por, row).ToDouble(); polyErr.Width = 300; polyErr.Height = 25; diff1.Width = 300; diff1.Height = 25; diff2.Width = 300; diff2.Height = 25; diff1Err.Width = 300; diff1Err.Height = 25; diff2Err.Width = 300; diff2Err.Height = 25; SolidColorBrush br = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); valY.BorderBrush = br; valX.BorderBrush = br; polyErr.BorderBrush = br; diff1.BorderBrush = br; diff2.BorderBrush = br; diff1Err.BorderBrush = br; diff2Err.BorderBrush = br; valX.Background = br; valY.Background = br; polyErr.Background = br; diff1.Background = br; diff2.Background = br; diff1Err.Background = br; diff2Err.Background = br; valX.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center; diff1.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left; diff2.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left; polyErr.Margin = new System.Windows.Thickness(0, 0, wdt - diff1.Width, hgt - diff1.Height); diff1.Margin = new System.Windows.Thickness(0, 0, wdt - diff1.Width, hgt - 3 * diff1.Height); diff1Err.Margin = new System.Windows.Thickness(0, 0, wdt - diff1.Width, hgt - 5 * diff1.Height); diff2.Margin = new System.Windows.Thickness(0, 0, wdt - diff2.Width, hgt - 7 * diff2.Height); diff2Err.Margin = new System.Windows.Thickness(0, 0, wdt - diff2.Width, hgt - 9 * diff2.Height); if (valXY.Y <= axis.crossAxis.Y) { valX.Margin = new System.Windows.Thickness(valXY.X - valX.Width / 2, axis.crossAxis.Y - valY.Height, 0, 0); } else { valX.Margin = new System.Windows.Thickness(valXY.X - valX.Width / 2, axis.crossAxis.Y + valY.Height, 0, 0); } if (valXY.X <= axis.crossAxis.X) { valY.Margin = new System.Windows.Thickness(axis.crossAxis.X - axis.bord - valY.Width, valXY.Y - valY.Height, 0, 0); valY.TextAlignment = axis.taY; } else { valY.Margin = new System.Windows.Thickness(axis.crossAxis.X + axis.bord, valXY.Y - valY.Height, 0, 0); //valY.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left; valY.TextAlignment = axis.taY; } gr.Children.Add(el); gr.Children.Add(valX); gr.Children.Add(valY); gr.Children.Add(polyErr); gr.Children.Add(diff1); gr.Children.Add(diff2); gr.Children.Add(diff1Err); gr.Children.Add(diff2Err); }