Update Cryville.Common.

This commit is contained in:
2022-10-14 23:32:43 +08:00
parent d3cac8a28d
commit c8eee7ab3f
18 changed files with 497 additions and 47 deletions

View File

@@ -1,18 +1,38 @@
namespace Cryville.Common.Math {
/// <summary>
/// Represents a column vector of vector type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The vector type of the elements.</typeparam>
public class ColumnVector<T> {
readonly T[] content;
/// <summary>
/// The size of the vector.
/// </summary>
public int Size {
get;
private set;
}
/// <summary>
/// Creates a column vector with specified size.
/// </summary>
/// <param name="size">The size of the vector.</param>
public ColumnVector(int size) {
content = new T[size];
Size = size;
}
/// <summary>
/// Creates a column vector from an array.
/// </summary>
/// <param name="c">The array.</param>
public ColumnVector(T[] c) {
Size = c.Length;
content = c;
}
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <param name="i">The zero-based index of the element to get or set.</param>
/// <returns>The element at the specified index.</returns>
public T this[int i] {
get {
return content[i];
@@ -21,12 +41,24 @@ namespace Cryville.Common.Math {
content[i] = value;
}
}
/// <summary>
/// Performs dot operation with a <see cref="System.Single" /> column vector.
/// </summary>
/// <param name="lhs">The lefthand column vector.</param>
/// <param name="o">The vector operator.</param>
/// <returns>The result of the dot operation.</returns>
public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) {
T res = default(T);
for (var i = 0; i < Size; i++)
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
return res;
}
/// <summary>
/// Creates a <see cref="System.Single" /> column vector and fills it with polynomial coefficients.
/// </summary>
/// <param name="size">The size of the column vector.</param>
/// <param name="num">The base number.</param>
/// <returns>A <see cref="System.Single" /> column vector filled with polynomial coefficients.</returns>
public static ColumnVector<float> WithPolynomialCoefficients(int size, float num) {
var m = new ColumnVector<float>(size);
for (var i = 0; i < size; i++)

View File

@@ -1,6 +1,22 @@
namespace Cryville.Common.Math {
/// <summary>
/// Provides a set of operators for vector type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The vector type.</typeparam>
public interface IVectorOperator<T> {
/// <summary>
/// Adds two vectors.
/// </summary>
/// <param name="lhs">Lefthand vector.</param>
/// <param name="rhs">Righthand vector.</param>
/// <returns>The sum of the two vectors.</returns>
T Add(T lhs, T rhs);
/// <summary>
/// Multiplies a vector with a number.
/// </summary>
/// <param name="lhs">The number.</param>
/// <param name="rhs">The vector.</param>
/// <returns>The product of the number and the vector.</returns>
T ScalarMultiply(float lhs, T rhs);
}
}

View File

@@ -1,18 +1,41 @@
namespace Cryville.Common.Math {
/// <summary>
/// Represents a square matrix.
/// </summary>
public class SquareMatrix {
readonly float[,] content;
/// <summary>
/// The size of the matrix.
/// </summary>
public int Size {
get;
private set;
}
/// <summary>
/// Creates a square matrix with the specified size.
/// </summary>
/// <param name="size">The size of the matrix.</param>
public SquareMatrix(int size) {
content = new float[size, size];
Size = size;
}
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <param name="r">The zero-based row index.</param>
/// <param name="c">The zero-based column index.</param>
/// <returns>The element at the specified index.</returns>
public float this[int r, int c] {
get { return content[r, c]; }
set { content[r, c] = value; }
}
/// <summary>
/// Eliminates the square matrix against a column vector.
/// </summary>
/// <typeparam name="T">The vector type.</typeparam>
/// <param name="v">The column vector.</param>
/// <param name="o">The column operator.</param>
/// <returns>The column vector eliminated.</returns>
public ColumnVector<T> Eliminate<T>(ColumnVector<T> v, IVectorOperator<T> o) {
int s = Size;
float[,] d = (float[,])content.Clone();
@@ -48,6 +71,11 @@
}
return new ColumnVector<T>(res);
}
/// <summary>
/// Creates a square matrix and fills it with polynomial coefficients.
/// </summary>
/// <param name="size">The size of the square matrix.</param>
/// <returns>A square matrix filled with polynomial coefficients.</returns>
public static SquareMatrix WithPolynomialCoefficients(int size) {
var m = new SquareMatrix(size);
for (var r = 0; r < size; r++) {