using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MathNet.Numerics.LinearAlgebra;
namespace MatrixCalculator
{
static class MatrixConverter
{
///
/// Puts a matrix in a DataTable
///
public static DataTable GetTable(Matrix matrix)
{
DataTable table = new DataTable("matrix");
for (int i = 0; i < matrix.ColumnCount; i++)
table.Columns.Add();
for (int i = 0; i < matrix.RowCount; i++)
{
table.Rows.Add("");
for (int j = 0; j < matrix.ColumnCount; j++)
table.Rows[i][j] = matrix[i, j].ToString();
}
return table;
}
///
/// Converts a DataTable into a matrix
///
public static Matrix FromTable(DataTable table)
{
Matrix matrix = Matrix.Build.Dense(table.Rows.Count, table.Columns.Count);
for (int i = 0; i < matrix.RowCount; i++)
for (int j = 0; j < matrix.ColumnCount; j++)
{
double temp = 0;
double.TryParse(table.Rows[i][j].ToString(), out temp);
matrix[i, j] = temp;
}
return matrix;
}
}
}