TableDlg.cs
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FitPolynom
{
public partial class TableDlg : Form
{
public TableDlg()
{
InitializeComponent();
dataGridView.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { Name = "X Value", ValueType = typeof(double) });
dataGridView.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { Name = "Y Value", ValueType = typeof(double) });
dataGridView.Columns.Add(new DataGridViewColumn(new ResultCell()) { Name = "Polynom", ValueType = typeof(double), ReadOnly = true });
dataGridView.Columns.Add(new DataGridViewColumn(new ResultCell()) { Name = "Error", ValueType = typeof(double), ReadOnly = true });
dataGridView.AllowUserToOrderColumns = false;
dataGridView.AllowUserToResizeRows = false;
fitGridView.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { Name = "Power", ValueType = typeof(int), ReadOnly = true });
fitGridView.Columns.Add(new DataGridViewColumn(new ResultCell()) { Name = "Value", ValueType = typeof(double), ReadOnly = true });
fitGridView.AllowUserToAddRows = false;
fitGridView.AllowUserToDeleteRows = false;
fitGridView.AllowUserToOrderColumns = false;
fitGridView.AllowUserToResizeRows = false;
numericUpDown.Value = Properties.Settings.Default.PolynomPower;
checkBoxAuto.Checked = Properties.Settings.Default.AutoFit;
btnFit.Enabled = !checkBoxAuto.Checked;
LoadData();
dataGridView.CellValueChanged += DataGridView_CellValueChanged;
dataGridView.RowsRemoved += CallDoFit;
numericUpDown.ValueChanged += CallDoFit;
}
private void LoadData()
{
var xValues = Properties.Settings.Default.XValues;
var yValues = Properties.Settings.Default.YValues;
if (xValues == null || yValues == null)
return;
dataGridView.RowCount = Math.Max(xValues.Count, yValues.Count) + 1;
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (xValues.Count > row.Index &&
!string.IsNullOrEmpty(xValues[row.Index]))
{
row.Cells[0].Value = Convert.ToDouble(xValues[row.Index]);
}
if (yValues.Count > row.Index &&
!string.IsNullOrEmpty(yValues[row.Index]))
{
row.Cells[1].Value = Convert.ToDouble(yValues[row.Index]);
}
}
DoFit();
}
private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
DoFit();
}
}
private void CallDoFit(object sender, EventArgs e)
{
DoFit();
}
public void DoFit(bool manual = false)
{
if (!manual && !checkBoxAuto.Checked)
return;
int power = Convert.ToInt32(numericUpDown.Value);
var fit = new Fit(power);
var data = GetValues();
var matrix = fit.BuildMatrix(data[0], data[1]);
var polynom = fit.SolveMatrix(matrix);
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.IsNewRow)
{
continue;
}
else if (!(row.Cells[0].Value is double) ||
!(row.Cells[1].Value is double))
{
if (!(row.Cells[0].Value is double) &&
!(row.Cells[1].Value is double))
{
row.Cells[2].Value = null;
}
else
{
row.Cells[2].Value = "Invalid data";
}
row.Cells[3].Value = null;
continue;
}
row.Cells[2].Value = Fit.Polynom(polynom, (double)row.Cells[0].Value);
row.Cells[3].Value = (double)row.Cells[1].Value - (double)row.Cells[2].Value;
}
fitGridView.Rows.Clear();
for (int index = 0; index <= power; index++)
fitGridView.Rows.Add(power - index, polynom[index]);
}
private double[][] GetValues()
{
var xs = new List<double>();
var ys = new List<double>();
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.IsNewRow ||
!(row.Cells[0].Value is double) ||
!(row.Cells[1].Value is double))
{
continue;
}
xs.Add((double)row.Cells[0].Value);
ys.Add((double)row.Cells[1].Value);
}
return new double[][]{ xs.ToArray(), ys.ToArray() } ;
}
private void btnFit_Click(object sender, EventArgs e)
{
DoFit(true);
}
private void checkBoxAuto_CheckedChanged(object sender, EventArgs e)
{
btnFit.Enabled = !checkBoxAuto.Checked;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Properties.Settings.Default.XValues = new StringCollection();
Properties.Settings.Default.YValues = new StringCollection();
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.IsNewRow)
continue;
Properties.Settings.Default.XValues.Add(row.Cells[0].Value == null ? null : row.Cells[0].Value.ToString());
Properties.Settings.Default.YValues.Add(row.Cells[1].Value == null ? null : row.Cells[1].Value.ToString());
}
Properties.Settings.Default.PolynomPower = Convert.ToInt32(numericUpDown.Value);
Properties.Settings.Default.AutoFit = checkBoxAuto.Checked;
Properties.Settings.Default.Save();
}
}
}