using System.Numerics; namespace Qrakhen.Alqebra; public interface IVector { int Dimensions { get; } T this[int index] { get; } double Magnitude { get; } } public readonly record struct Vector(params T[] Values) : IVector where T : INumber { public int Dimensions => Values.Length; public T this[int index] => Values[index]; public double Magnitude => Math.Sqrt(Values.Select(n => n * n).Sum(n => (dynamic)n)); } public static class VectorExtensions { /// /// Slower add that works with vectors of any dimension. /// public static Vector Add(this Vector self, Vector other) where T : INumber { ArgumentOutOfRangeException.ThrowIfNotEqual(self.Dimensions, other.Dimensions); T[] values = new T[self.Dimensions]; for (int i = 0; i < self.Dimensions; i++) { values[i] = self[i] + other[i]; } return new Vector(values); // i think this v is just too slow return new Vector(self.Values.Zip(other.Values).Select(tuple => tuple.First + tuple.Second).ToArray()); } }