66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|