📄 matrix.cs
字号:
{
get
{
double f = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
f = Hypotenuse(f, data[i][j]);
}
}
return f;
}
}
/// <summary>Unary minus.</summary>
public static Matrix Negate(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
int rows = value.Rows;
int columns = value.Columns;
double[][] data = value.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = -data[i][j];
}
}
return X;
}
/// <summary>Unary minus.</summary>
public static Matrix operator-(Matrix value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return Negate(value);
}
/// <summary>Matrix equality.</summary>
public static bool operator==(Matrix left, Matrix right)
{
return Equals(left, right);
}
/// <summary>Matrix inequality.</summary>
public static bool operator!=(Matrix left, Matrix right)
{
return !Equals(left, right);
}
/// <summary>Matrix addition.</summary>
public static Matrix Add(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("Matrix dimension do not match.");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] + right[i,j];
}
}
return X;
}
/// <summary>Matrix addition.</summary>
public static Matrix operator+(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Add(left, right);
}
/// <summary>Matrix subtraction.</summary>
public static Matrix Subtract(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
if ((rows != right.Rows) || (columns != right.Columns))
{
throw new ArgumentException("Matrix dimension do not match.");
}
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] - right[i,j];
}
}
return X;
}
/// <summary>Matrix subtraction.</summary>
public static Matrix operator-(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Subtract(left, right);
}
/// <summary>Matrix-scalar multiplication.</summary>
public static Matrix Multiply(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
int rows = left.Rows;
int columns = left.Columns;
double[][] data = left.Array;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = data[i][j] * right;
}
}
return X;
}
/// <summary>Matrix-scalar multiplication.</summary>
public static Matrix operator*(Matrix left, double right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
return Multiply(left, right);
}
/// <summary>Matrix-matrix multiplication.</summary>
public static Matrix Multiply(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
int rows = left.Rows;
double[][] data = left.Array;
if (right.Rows != left.columns)
{
throw new ArgumentException("Matrix dimensions are not valid.");
}
int columns = right.Columns;
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
int size = left.columns;
double[] column = new double[size];
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < size; k++)
{
column[k] = right[k,j];
}
for (int i = 0; i < rows; i++)
{
double[] row = data[i];
double s = 0;
for (int k = 0; k < size; k++)
{
s += row[k] * column[k];
}
x[i][j] = s;
}
}
return X;
}
/// <summary>Matrix-matrix multiplication.</summary>
public static Matrix operator*(Matrix left, Matrix right)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (right == null)
{
throw new ArgumentNullException("right");
}
return Multiply(left, right);
}
/// <summary>Returns the LHS solution vetor if the matrix is square or the least squares solution otherwise.</summary>
public Matrix Solve(Matrix rightHandSide)
{
return (rows == columns) ? new LuDecomposition(this).Solve(rightHandSide) : new QrDecomposition(this).Solve(rightHandSide);
}
/// <summary>Inverse of the matrix if matrix is square, pseudoinverse otherwise.</summary>
public Matrix Inverse
{
get
{
return this.Solve(Diagonal(rows, rows, 1.0));
}
}
/// <summary>Determinant if matrix is square.</summary>
public double Determinant
{
get
{
return new LuDecomposition(this).Determinant;
}
}
/// <summary>Returns the trace of the matrix.</summary>
/// <returns>Sum of the diagonal elements.</returns>
public double Trace
{
get
{
double trace = 0;
for (int i = 0; i < Math.Min(rows, columns); i++)
{
trace += data[i][i];
}
return trace;
}
}
/// <summary>Returns a matrix filled with random values.</summary>
public static Matrix Random(int rows, int columns)
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = random.NextDouble();
}
}
return X;
}
/// <summary>Returns a diagonal matrix of the given size.</summary>
public static Matrix Diagonal(int rows, int columns, double value)
{
Matrix X = new Matrix(rows, columns);
double[][] x = X.Array;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
x[i][j] = ((i == j) ? value : 0.0);
}
}
return X;
}
/// <summary>Returns the matrix in a textual form.</summary>
public override string ToString()
{
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
writer.Write(this.data[i][j] + " ");
}
writer.WriteLine();
}
return writer.ToString();
}
}
private static double Hypotenuse(double a, double b)
{
if (Math.Abs(a) > Math.Abs(b))
{
double r = b / a;
return Math.Abs(a) * Math.Sqrt(1 + r * r);
}
if (b != 0)
{
double r = a / b;
return Math.Abs(b) * Math.Sqrt(1 + r * r);
}
return 0.0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -