private void btnLinalgLU_Click(object sender, EventArgs e) { if (Linalg == null) { Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra(); } float[,] M = new float[, ] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } }; float[] b = new float[] { 1, 2, 3, 4 }; double[,] M2 = new double[, ] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } }; double[] b2 = new double[] { 1, 2, 3, 4 }; //float[,] M = new float[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } }; //float[] b = new float[] { 1, 2, 3 }; //double[,] M2 = new double[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } }; //double[] b2 = new double[] { 1, 2, 3 }; LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M2); double[] sol2 = MM.LinearSolve(b2); double[] sol3 = Linalg.LinSolve(M2, b2, 1e-10, 10); float[] sol = floatLinalg.LinSolve(M, b, 1e-5f, 10); }
private void AtualizaPtosTabela() { tblPtos.Rows.Clear(); int i = 0; //Interpolação da equação int Ordem = 4; LinearAlgebra.Matrix M = new LinearAlgebra.Matrix(MarkedCurve.Count, Ordem); LinearAlgebra.Matrix MLocal = new LinearAlgebra.Matrix(MarkedCurve.Count, Ordem); double[] y = new double[MarkedCurve.Count]; double[] yLocal = new double[MarkedCurve.Count]; foreach (PointF p in MarkedCurve) { PointF pG = ConvToGlobal(p); DataRow r = tblPtos.NewRow(); r["dblPonto"] = (1 + i).ToString(); r["dblX"] = pG.X.ToString(); r["dblY"] = pG.Y.ToString(); tblPtos.Rows.Add(r); //Equação y[i] = pG.Y; yLocal[i] = p.Y; double val = 1; double valLocal = 1; for (int k = 0; k < Ordem; k++) { M[i, k] = val; val *= pG.X; MLocal[i, k] = valLocal; valLocal *= p.X; } i++; } //4 pontos p identificar ax³+bx²+cx+d=y(x) if (MarkedCurve.Count >= 4) { try { coefs = M.IdentifyParameters(y); try { coefsLocal = MLocal.IdentifyParameters(yLocal); } catch { } string s = ((float)coefs[3]).ToString() + "x³"; if (coefs[2] >= 0) { s += "+"; } s += ((float)coefs[2]).ToString() + "x²"; if (coefs[1] >= 0) { s += "+"; } s += ((float)coefs[1]).ToString() + "x"; if (coefs[0] >= 0) { s += "+"; } s += ((float)coefs[0]).ToString(); frmPtos.lblEq.Text = s; } catch { } } else { frmPtos.lblEq.Text = ""; coefs = null; coefsLocal = null; } //Tenta adivinhar a curva if (MarkedCurve.Count > 3) { try { PredictedCurve = PointPredictor.PreviewNextPoints(MarkedCurve, PickSize, picGraf); } catch { PredictedCurve = null; } } else { PredictedCurve = null; } aceitarSugestãoToolStripMenuItem.Enabled = (PredictedCurve != null); frmPtos.lblEq.Refresh(); picGraf.Refresh(); }
private void btnLinalg_Click(object sender, EventArgs e) { if (Linalg == null) { Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra(); } //CLCalc.Program.DefaultCQ = 0; //float[,] M = new float[5, 3] { { 1, 1, 1 }, { 1, -1, -1 }, { -2, 3, 4 }, { -1, 1, 5 }, { 2, 4, 7 } }; //float[] b = new float[5] { 1, 2, 3, 4, 5 }; //float[] err; //float[] x = new float[3]; //x = Linalg.LeastSquaresGS(M, b, x, out err); int n = 1500; Random rnd = new Random(); double[,] M = new double[n, n]; double[] b = new double[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i, j] = 1; //M[i, j] = rnd.NextDouble(); } M[i, i] += n; b[i] = i + 1; } //double[] err; //double[] x = new double[n]; //x = Linalg.LeastSquaresGS(M, b, x, out err); //Testes com LU decomp System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //OpenCL sw.Start(); double[] sol3 = Linalg.LinSolve(M, b, 1e-18, 20); sw.Stop(); this.Text = " " + sw.Elapsed.TotalSeconds.ToString(); sw.Reset(); Application.DoEvents(); //Convencional sw.Start(); LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M); double[] sol2 = MM.LinearSolve(b); sw.Stop(); this.Text += " " + sw.Elapsed.TotalSeconds.ToString(); sw.Reset(); Application.DoEvents(); double erro = 0; for (int i = 0; i < sol3.Length; i++) { erro += (sol3[i] - sol2[i]) * (sol3[i] - sol2[i]); } this.Text += " " + erro.ToString(); }
/// <summary>Analyze points in relative coordinates to identify /// graph color and background color</summary> /// <param name="Points">Points to analyze</param> /// <param name="PickSize">Pick size in pixels</param> /// <param name="pBox">Picturebox holding the graph</param> public static Color GetGraphColor(List <PointF> Points, int PickSize, PictureBox pBox) { //Initializations Bitmap BmpGraf = (Bitmap)pBox.Image; if (BmpGraf == null) { return(Color.White); } float[] relPickSize = new float[2]; relPickSize[0] = (float)PickSize / (float)pBox.Width; relPickSize[1] = (float)PickSize / (float)pBox.Height; List <int>[] Histogram = new List <int> [3]; for (int i = 0; i < 3; i++) { Histogram[i] = new List <int>(); for (int j = 0; j < 256; j++) { Histogram[i].Add(0); } } double totalPixels = 0; foreach (PointF p in Points) { int x0 = (int)((p.X - relPickSize[0]) * BmpGraf.Width); x0--; int xf = (int)((p.X + relPickSize[0]) * BmpGraf.Width); xf++; int y0 = (int)((p.Y - relPickSize[1]) * BmpGraf.Height); y0--; int yf = (int)((p.Y + relPickSize[1]) * BmpGraf.Height); yf++; if (x0 < 0) { x0 = 0; } if (xf >= BmpGraf.Width) { xf = BmpGraf.Width - 1; } if (y0 < 0) { y0 = 0; } if (yf >= BmpGraf.Height) { yf = BmpGraf.Height - 1; } for (int x = x0; x < xf; x++) { for (int y = y0; y < yf; y++) { Color pix = BmpGraf.GetPixel(x, y); Histogram[0][pix.R]++; Histogram[1][pix.G]++; Histogram[2][pix.B]++; totalPixels++; } } } //Interpolation int Order = 6; double[] vals = new double[256]; LinearAlgebra.Matrix M = new LinearAlgebra.Matrix(256, Order); for (int i = 0; i < 256; i++) { vals[i] = Histogram[0][i]; double temp = 1; for (int j = 0; j < Order; j++) { M[i, j] = temp; temp *= i; } } //Assumes white background and calculates graph color for (int j = 237; j < 256; j++) { int min = Histogram[0][j]; if (Histogram[1][j] < min) { min = Histogram[1][j]; } if (Histogram[2][j] < min) { min = Histogram[2][j]; } for (int i = 0; i < 3; i++) { Histogram[i][j] -= min; } } //Graph color mean float[] GraphColor = new float[3]; for (int i = 0; i < 3; i++) { totalPixels = 0; for (int j = 0; j < 256; j++) { totalPixels += Histogram[i][j]; GraphColor[i] += (float)j * (float)Histogram[i][j]; } GraphColor[i] /= (float)totalPixels; } Color resp = Color.FromArgb((int)GraphColor[0], (int)GraphColor[1], (int)GraphColor[2]); return(resp); }
/// <summary>Tries to guess next curve points</summary> /// <param name="Points">Points to analyze, already in graph</param> /// <param name="PickSize">Pick size in pixels</param> /// <param name="pBox">Picturebox holding the graph</param> public static List <PointF> OldPreviewNextPoints(List <PointF> Points, int PickSize, PictureBox pBox, GroupBox gb) { List <PointF> Predicted = new List <PointF>(); //Initializations Bitmap BmpGraf = (Bitmap)pBox.Image; if (BmpGraf == null) { return(Predicted); } float[] relPickSize = new float[2]; relPickSize[0] = (float)PickSize / (float)pBox.Width; relPickSize[1] = (float)PickSize / (float)pBox.Height; Color GraphColor = GetGraphColor(Points, PickSize, pBox); //gb.ForeColor = GraphColor; bool NewPointFound = true; int numPtsPreviewed = 0; int Order = 3; int NPts = 4; while (NewPointFound && numPtsPreviewed < MaxPreviewPoints) { NewPointFound = false; //Creates estimation matrix double[] x = new double[NPts + numPtsPreviewed], y = new double[NPts + numPtsPreviewed]; LinearAlgebra.Matrix M = new LinearAlgebra.Matrix(NPts + numPtsPreviewed, Order); //Estimated points for (int i = 0; i < numPtsPreviewed; i++) { x[i] = Predicted[numPtsPreviewed - 1 - i].X; y[i] = Predicted[numPtsPreviewed - 1 - i].Y; float val = 1; for (int j = 0; j < Order; j++) { M[i, j] = val; val *= i; } } //Existing points for (int i = 0; i < NPts; i++) { x[i + numPtsPreviewed] = Points[Points.Count - 1 - i].X; y[i + numPtsPreviewed] = Points[Points.Count - 1 - i].Y; float val = 1; for (int j = 0; j < Order; j++) { M[i + numPtsPreviewed, j] = val; val *= i + numPtsPreviewed; } } double[] cy = M.IdentifyParameters(y); double[] cx = M.IdentifyParameters(x); //Assuming next point is close to Next PointF Next = new PointF(PolyVal(cx, -0.15f), PolyVal(cy, -0.15f)); //Try looking for point bool found; PointF Correction = LookFor(GraphColor, Next, BmpGraf, relPickSize, out found); //if (found) { Predicted.Add(Correction); numPtsPreviewed++; NewPointFound = true; } } return(Predicted); }
private void btnLinalg_Click(object sender, EventArgs e) { if (Linalg == null) Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra(); //CLCalc.Program.DefaultCQ = 0; //float[,] M = new float[5, 3] { { 1, 1, 1 }, { 1, -1, -1 }, { -2, 3, 4 }, { -1, 1, 5 }, { 2, 4, 7 } }; //float[] b = new float[5] { 1, 2, 3, 4, 5 }; //float[] err; //float[] x = new float[3]; //x = Linalg.LeastSquaresGS(M, b, x, out err); int n = 1500; Random rnd = new Random(); double[,] M = new double[n, n]; double[] b = new double[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i, j] = 1; //M[i, j] = rnd.NextDouble(); } M[i, i] += n; b[i] = i+1; } //double[] err; //double[] x = new double[n]; //x = Linalg.LeastSquaresGS(M, b, x, out err); //Testes com LU decomp System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //OpenCL sw.Start(); double[] sol3 = Linalg.LinSolve(M, b, 1e-18, 20); sw.Stop(); this.Text = " " + sw.Elapsed.TotalSeconds.ToString(); sw.Reset(); Application.DoEvents(); //Convencional sw.Start(); LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M); double[] sol2 = MM.LinearSolve(b); sw.Stop(); this.Text += " " + sw.Elapsed.TotalSeconds.ToString(); sw.Reset(); Application.DoEvents(); double erro = 0; for (int i = 0; i < sol3.Length; i++) erro += (sol3[i] - sol2[i]) * (sol3[i] - sol2[i]); this.Text += " " + erro.ToString(); }
private void btnLinalgLU_Click(object sender, EventArgs e) { if (Linalg == null) Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra(); float[,] M = new float[,] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } }; float[] b = new float[] { 1, 2, 3, 4 }; double[,] M2 = new double[,] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } }; double[] b2 = new double[] { 1, 2, 3, 4 }; //float[,] M = new float[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } }; //float[] b = new float[] { 1, 2, 3 }; //double[,] M2 = new double[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } }; //double[] b2 = new double[] { 1, 2, 3 }; LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M2); double[] sol2 = MM.LinearSolve(b2); double[] sol3 = Linalg.LinSolve(M2, b2, 1e-10, 10); float[] sol = floatLinalg.LinSolve(M, b, 1e-5f, 10); }