public static List <DenseVector> ReadDataFromCSV(string path) { var reader = new StreamReader(path); var list = new List <DenseVector>(); if (!reader.EndOfStream) { reader.ReadLine(); // header } while (!reader.EndOfStream) { var line = reader.ReadLine(); // data var values = line.Split(','); var vector = new DenseVector(new double[values.Count()]); for (int i = 0; i < vector.Count(); i++) { vector[i] = Double.Parse(values[i], CultureInfo.InvariantCulture); } list.Add(vector); } return(list); }
public static string ToCSVString(this DenseVector vector) { var sb = new StringBuilder(); for (int i = 0; i < vector.Count(); i++) { sb.Append(","); sb.Append(vector[i].ToString()); } return(sb.ToString()); }
public void SolveGauss(DataGridView dt1, DataGridView dt2) { if (dt1.RowCount != 0) { int M = dt1.RowCount; int N = dt1.ColumnCount - 1; // поправим из-за столбца с id List <string> list = new List <string>(); ArrayList lis = new ArrayList(); var matrs = new DenseMatrix(M, N); bool toOneUse = true; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { matrs[i, j] = Convert.ToDouble(dt1.Rows[i].Cells[j].Value, CultureInfo.InvariantCulture); } } int ch = 0; double[] vec = new double[35]; for (int i = 0; i < M; i++) { vec[i] = Convert.ToDouble(dt1[35, i].Value); ++ch; } var v = new DenseVector(vec); int q = v.Count(); var x = matrs.Solve(v); string output = string.Join(" ", x); string[] name = { "ТА", "КП", "СОС", "ДС", "ДЗ", "ВА", "ЗЗ", "ПК", "СК", "ФР", "ДП", "Оср", "ПР.ТА", "ПР.ВА", "ВР", "Вал.пр.", "КрУр", "Пр.прод", "Проч.д", "Проч.р", "ЧП", "RСовК", "Пр.до_нал.", "Себ.прод.", "НерПр", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10" }; String[] words = output.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); dt2.RowCount = N; int i1; for (i1 = 0; i1 < N; ++i1) { double result = Convert.ToDouble(words[i1]); Math.Round(result); dt2.Rows[i1].Cells[0].Value = name[i1]; dt2.Rows[i1].Cells[1].Value = Math.Round(result); } int rang = matrs.Rank(); var lu = matrs.LU(); if (lu.Determinant == 0 && toOneUse) { MessageBox.Show("Система несовместна и не имеет решений, т.к ее определитель равен " + lu.Determinant + ".", "Исследование СЛАУ", MessageBoxButtons.OK, MessageBoxIcon.Error); toOneUse = false; } if (rang > N && toOneUse) { MessageBox.Show("Ранк матрицы= " + matrs.Rank() + " Система не имеет решений.", "Исследование системы", MessageBoxButtons.OK, MessageBoxIcon.Error); toOneUse = false; } else if (lu.Determinant != 0 && rang <= N) { var r = matrs * x - v; string output1 = string.Join(" ", r); String[] word = output1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); dt2.RowCount = N; int j1; for (j1 = 0; j1 < N; ++j1) { double result1 = Convert.ToDouble(word[j1]); dt2.Rows[j1].Cells[2].Value = result1.ToString("0.##############"); } } } else { MessageBox.Show("Таблица пустая! Нельзя запустить расчет.", "Система", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } }