Added source code.
This commit is contained in:
144
Source/MatrixCalculator/Helpers/GridViewHelper.cs
Normal file
144
Source/MatrixCalculator/Helpers/GridViewHelper.cs
Normal file
@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using MathNet.Numerics.LinearAlgebra;
|
||||
|
||||
namespace MatrixCalculator
|
||||
{
|
||||
static class GridViewHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads a matrix in a DataGridView
|
||||
/// </summary>
|
||||
public static void PutMatrix(Matrix<double> m, DataGridView grid)
|
||||
{
|
||||
// Reset grid
|
||||
grid.Rows.Clear();
|
||||
grid.Columns.Clear();
|
||||
|
||||
// Add columns
|
||||
for (int i = 0; i < m.ColumnCount; i++)
|
||||
{
|
||||
var col = new DataGridViewTextBoxColumn()
|
||||
{
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
|
||||
};
|
||||
|
||||
grid.Columns.Add(col);
|
||||
}
|
||||
|
||||
// Add rows and data
|
||||
for (int i = 0; i < m.RowCount; i++)
|
||||
{
|
||||
grid.Rows.Add();
|
||||
|
||||
for (int j = 0; j < m.ColumnCount; j++)
|
||||
grid[j, i].Value = MyDouble.String(m[i, j]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the matrix from a DataGridView
|
||||
/// </summary>
|
||||
public static Matrix<double> GetMatrix(DataGridView grid)
|
||||
{
|
||||
Matrix<double> m = Matrix<double>.Build.Dense(grid.Rows.Count, grid.Columns.Count);
|
||||
|
||||
for (int i = 0; i < m.RowCount; i++)
|
||||
for (int j = 0; j < m.ColumnCount; j++)
|
||||
{
|
||||
double temp = 0;
|
||||
|
||||
if (grid[j, i].Value != null)
|
||||
double.TryParse(grid[j, i].Value.ToString(), out temp);
|
||||
|
||||
m[i, j] = temp;
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes a DataGridView
|
||||
/// </summary>
|
||||
/// <param name="grid">The DataGridView</param>
|
||||
/// <param name="ncols">The new number of columns</param>
|
||||
/// <param name="nrows">The new number of rows</param>
|
||||
public static void Resize(DataGridView grid, int ncols, int nrows)
|
||||
{
|
||||
// Add columns
|
||||
while (grid.Columns.Count < ncols)
|
||||
{
|
||||
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn()
|
||||
{
|
||||
AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells,
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable
|
||||
};
|
||||
|
||||
grid.Columns.Add(col);
|
||||
}
|
||||
|
||||
// Remove columns
|
||||
while (grid.Columns.Count > ncols)
|
||||
grid.Columns.Remove(grid.Columns.GetLastColumn(DataGridViewElementStates.Displayed, DataGridViewElementStates.None));
|
||||
|
||||
// Add rows
|
||||
while (grid.Rows.Count < nrows)
|
||||
grid.Rows.Add();
|
||||
|
||||
// Remove rows
|
||||
while (grid.Rows.Count > nrows)
|
||||
grid.Rows.RemoveAt(grid.Rows.GetLastRow(DataGridViewElementStates.Displayed));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the selected area to clipboard
|
||||
/// </summary>
|
||||
public static void CopySelected(DataGridView grid)
|
||||
{
|
||||
int minRow, minCol, maxRow, maxCol;
|
||||
minRow = minCol = int.MaxValue;
|
||||
maxRow = maxCol = int.MinValue;
|
||||
|
||||
foreach (DataGridViewCell i in grid.SelectedCells)
|
||||
{
|
||||
minRow = Math.Min(minRow, i.RowIndex);
|
||||
minCol = Math.Min(minCol, i.ColumnIndex);
|
||||
maxRow = Math.Max(maxRow, i.RowIndex);
|
||||
maxCol = Math.Max(maxCol, i.ColumnIndex);
|
||||
}
|
||||
|
||||
string csv = CsvParser.ToCSV(grid, minRow, minCol, maxRow, maxCol, true);
|
||||
Clipboard.SetText(csv, TextDataFormat.CommaSeparatedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pastes clipboard content into selected cell and beyond
|
||||
/// </summary>
|
||||
public static void Paste(DataGridView grid)
|
||||
{
|
||||
// Where to paste
|
||||
int row = 0, col = 0;
|
||||
if (grid.CurrentCell != null) {
|
||||
row = grid.CurrentCell.RowIndex;
|
||||
col = grid.CurrentCell.ColumnIndex;
|
||||
}
|
||||
|
||||
// Get data
|
||||
string data = "";
|
||||
if (Clipboard.ContainsText(TextDataFormat.CommaSeparatedValue)) data = Clipboard.GetText(TextDataFormat.CommaSeparatedValue);
|
||||
else if (Clipboard.ContainsText()) data = Clipboard.GetText();
|
||||
|
||||
var table = CsvParser.GetTable(data, MatrixCalculator.Properties.Settings.Default.InternalCsvSeparators.ToArray());
|
||||
|
||||
// Put data in table
|
||||
for (int i = 0; i < table.Rows.Count; i++)
|
||||
for (int j = 0; j < table.Columns.Count; j++)
|
||||
if (table.Rows[i][j].ToString() != "" && row + i < grid.Rows.Count && col + j < grid.Columns.Count)
|
||||
grid[j + col, i + row].Value = table.Rows[i][j];
|
||||
}
|
||||
}
|
||||
}
|
51
Source/MatrixCalculator/Helpers/MatrixConverter.cs
Normal file
51
Source/MatrixCalculator/Helpers/MatrixConverter.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using MathNet.Numerics.LinearAlgebra;
|
||||
|
||||
namespace MatrixCalculator
|
||||
{
|
||||
static class MatrixConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Puts a matrix in a DataTable
|
||||
/// </summary>
|
||||
public static DataTable GetTable(Matrix<double> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a DataTable into a matrix
|
||||
/// </summary>
|
||||
public static Matrix<double> FromTable(DataTable table)
|
||||
{
|
||||
Matrix<double> matrix = Matrix<double>.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;
|
||||
}
|
||||
}
|
||||
}
|
65
Source/MatrixCalculator/Helpers/MatrixHelper.cs
Normal file
65
Source/MatrixCalculator/Helpers/MatrixHelper.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MathNet.Numerics.LinearAlgebra;
|
||||
|
||||
namespace MatrixCalculator
|
||||
{
|
||||
static class MatrixHelper
|
||||
{
|
||||
public static bool Equal(Matrix<double> a, Matrix<double> b)
|
||||
{
|
||||
if (a.RowCount != b.RowCount || a.ColumnCount != b.ColumnCount) return false;
|
||||
|
||||
for (int i = 0; i < a.RowCount; i++)
|
||||
for (int j = 0; j < a.ColumnCount; j++)
|
||||
if (a[i, j] != b[i, j]) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Matrix<double> Power(Matrix<double> mat, int power)
|
||||
{
|
||||
// Solution
|
||||
Matrix<double> sol = Matrix<double>.Build.DenseIdentity(mat.RowCount, mat.ColumnCount);
|
||||
|
||||
if (power < 0) {
|
||||
mat = mat.Inverse();
|
||||
power *= -1;
|
||||
}
|
||||
|
||||
// Now raise to power binary
|
||||
for (int i = 0; (1 << i) <= power; i++)
|
||||
{
|
||||
if ((power & (1 << i)) != 0) sol = (sol * mat);
|
||||
mat = (mat * mat);
|
||||
}
|
||||
|
||||
// Done
|
||||
return sol;
|
||||
}
|
||||
|
||||
public static double Max(Matrix<double> mat)
|
||||
{
|
||||
double max = double.MinValue;
|
||||
|
||||
for (int i = 0; i < mat.RowCount; i++)
|
||||
for (int j = 0; j < mat.ColumnCount; j++)
|
||||
max = Math.Max(mat[i, j], max);
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
public static double Min(Matrix<double> mat)
|
||||
{
|
||||
double min = double.MaxValue;
|
||||
|
||||
for (int i = 0; i < mat.RowCount; i++)
|
||||
for (int j = 0; j < mat.ColumnCount; j++)
|
||||
min = Math.Min(mat[i, j], min);
|
||||
|
||||
return min;
|
||||
}
|
||||
}
|
||||
}
|
16
Source/MatrixCalculator/Helpers/MyDouble.cs
Normal file
16
Source/MatrixCalculator/Helpers/MyDouble.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MatrixCalculator
|
||||
{
|
||||
static class MyDouble
|
||||
{
|
||||
public static string String(double n)
|
||||
{
|
||||
if (MatrixCalculator.Properties.Settings.Default.DoublePrecision == -1) return n.ToString();
|
||||
else return Math.Round(n, MatrixCalculator.Properties.Settings.Default.DoublePrecision).ToString();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user