Files
crtr/Assets/Cryville/Common/Math/ColumnVector.cs

67 lines
1.9 KiB
C#

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];
}
set {
content[i] = value;
}
}
/// <summary>
/// Performs dot operation with a <see cref="float" /> 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;
for (var i = 0; i < Size; i++)
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
return res;
}
/// <summary>
/// Fills a <see cref="System.Single" /> column vector with polynomial coefficients.
/// </summary>
/// <param name="vec">The column vector.</param>
/// <param name="num">The base number.</param>
public static void FillWithPolynomialCoefficients(ColumnVector<float> vec, float num) {
for (var i = 0; i < vec.Size; i++)
vec[i] = (float)System.Math.Pow(num, i);
}
}
}