internal static Matrix TryBuildingMatrix(List <Entity> elements) { if (!elements.Any()) { return(MathS.Vector(elements.ToArray())); } var first = elements.First(); if (first is not Matrix { IsVector : true } firstVec) { return(MathS.Vector(elements.ToArray())); } var tb = new MatrixBuilder(firstVec.RowCount); foreach (var row in elements) { if (row is not Matrix { IsVector: true } rowVec) { return(MathS.Vector(elements.ToArray())); } if (rowVec.RowCount != firstVec.RowCount) { return(MathS.Vector(elements.ToArray())); } tb.Add(rowVec); } return(tb.ToMatrix() ?? throw new AngouriBugException("Should've been checked already")); }
[Fact] public void WithElementVector2() { var v = MathS.ZeroVector(5); Assert.Equal(MathS.Vector(0, 0, 5, 6, 0), v.WithElement(2, 5).WithElement(3, 6)); Assert.Equal(MathS.Vector(0, 0, 0, 0, 0), v); }
[Fact] public void WithElementVector1() { var v = MathS.Vector(1, 2, 3); Assert.Equal(5, v.WithElement(2, 5)[2]); Assert.Equal(MathS.Vector(1, 2, 3), v); }
public void ScalarProduct1() { var a = MathS.Vector(1, 2, 3); var b = MathS.Vector(1, 2, 4); var scalar = a.T * b; Assert.Equal(17, scalar.EvalNumerical()); }
public void TransposeVector() { var a = MathS.Vector(1, 2); Assert.Equal(1, a[0]); Assert.Equal(2, a[1]); Assert.Equal(1, a.T[0, 0]); Assert.Equal(2, a.T[0, 1]); }
public void TransposeImmutability() { var a = MathS.Vector(1, 2); var aT = MathS.Matrix(new Entity[, ] { { 1, 2 } }); Assert.Equal(aT, a.T); var origin = MathS.Vector(1, 2); Assert.Equal(origin, a); }
[Fact] public void WithColumnMatrix1() { var m = MathS.I_4.WithColumn(2, MathS.Vector(1, 2, 3, 4)); Assert.Equal( MathS.Matrix(new Entity[, ] { { 1, 0, 1, 0 }, { 0, 1, 2, 0 }, { 0, 0, 3, 0 }, { 0, 0, 4, 1 } }), m); }
[Fact] public void WithRowMatrix1() { var m = MathS.I_4.WithRow(2, MathS.Vector(1, 2, 3, 4).T); Assert.Equal( MathS.Matrix(new Entity[, ] { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 2, 3, 4 }, { 0, 0, 0, 1 } }), m); }
[Fact] public void WithColumnMatrix2() { var m = MathS.IdentityMatrix(5) .WithColumn(2, MathS.Vector(1, 2, 3, 4, 5)) .WithColumn(4, MathS.Vector(1, 3, 5, 9, 1)) .WithColumn(4, MathS.Vector(5, 6, 7, 8, 9)); Assert.Equal( MathS.Matrix(new Entity[, ] { { 1, 0, 1, 0, 5 }, { 0, 1, 2, 0, 6 }, { 0, 0, 3, 0, 7 }, { 0, 0, 4, 1, 8 }, { 0, 0, 5, 0, 9 } }), m); }
[Fact] public void WithRowMatrix2() { var m = MathS.IdentityMatrix(5) .WithRow(2, MathS.Vector(1, 2, 3, 4, 5).T) .WithRow(4, MathS.Vector(1, 3, 5, 9, 1).T) .WithRow(4, MathS.Vector(5, 6, 7, 8, 9).T); Assert.Equal( MathS.Matrix(new Entity[, ] { { 1, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0 }, { 1, 2, 3, 4, 5 }, { 0, 0, 0, 1, 0 }, { 5, 6, 7, 8, 9 } }), m); }
public void TransposeDoubleOperationVector() { var a = MathS.Vector(1, 2); Assert.Equal(a, a.T.T); }
[Fact] public void ZeroVector() => Assert.Equal(MathS.Vector(0, 0, 0, 0), MathS.ZeroVector(4));
[Fact] public void LeftScalarDivisionUnsuccessfulInnerSimplified() => Assert.Equal(2 / MathS.Vector(3, 4), (2 / MathS.Vector(3, 4)).InnerSimplified);
[Fact] public void LeftScalarDivisionUnsuccessfulEvaled() => Assert.Equal(2 / MathS.Vector(3, 4), (2 / MathS.Vector(3, 4)).Evaled);
[Fact] public void MatrixAddition1() => Assert.Equal(MathS.Vector(3, 2), MathS.Vector(1, 2) + MathS.Vector(2, 0));
[Fact] public void WithColumnVector() => Assert.Equal(MathS.Vector(1, 2, 3), MathS.ZeroVector(3).WithColumn(0, MathS.Vector(1, 2, 3)));
[Fact] public void RightScalarDivision() => Assert.Equal(MathS.Vector("1.5", 2), (MathS.Vector(3, 4) / 2).InnerSimplified);
[Fact] public void RightScalarMulitiplication() => Assert.Equal(MathS.Vector(6, 8), (MathS.Vector(3, 4) * 2).InnerSimplified);
[Fact] public void UnsuccessfulMatrixSubtraction2InnerSimplified() => Assert.Equal(MathS.Vector(1, 2) - (Entity)MathS.Vector(2, 0, 3), (MathS.Vector(1, 2) - (Entity)MathS.Vector(2, 0, 3)).InnerSimplified);
[Fact] public void MatrixSubtraction1() => Assert.Equal(MathS.Vector(-1, 2), MathS.Vector(1, 2) - MathS.Vector(2, 0));
[Fact] public void UnsuccessfulMatrixAddition2Evaled() => Assert.Equal(MathS.Vector(1, 2) + (Entity)MathS.Vector(2, 0, 3), (MathS.Vector(1, 2) + (Entity)MathS.Vector(2, 0, 3)).Evaled);
/// <summary> /// Converts a given sequence of elements into a vector, /// which is a one-column matrix /// </summary> public static Matrix ToVector(this IEnumerable <Entity> elements) => MathS.Vector(elements.ToArray());
[Fact] public void WithRowVector() => Assert.Equal(MathS.Vector(0, 0, 3, 0), MathS.ZeroVector(4).WithRow(2, MathS.Vector(3)));
[Fact] public void VectorSingle() => Test(@"\begin{bmatrix}x\end{bmatrix}", MathS.Vector(x));
[Fact] public void LeftScalarSubtraction() => Assert.Equal(MathS.Vector(-1, -2), (2 - MathS.Vector(3, 4)).InnerSimplified);
[Fact] public void RightScalarSubtraction() => Assert.Equal(MathS.Vector(1, 2), (MathS.Vector(3, 4) - 2).InnerSimplified);
[Fact] public void LeftScalarAddition() => Assert.Equal(MathS.Vector(5, 6), (2 + MathS.Vector(3, 4)).InnerSimplified);
[Fact] public void MatrixMultiplication1() => Assert.Equal(MathS.Vector(1, -1), H * MathS.Vector(0, 1));
[Fact] public void RightScalarAddition() => Assert.Equal(MathS.Vector(5, 6), (MathS.Vector(3, 4) + 2).InnerSimplified);
[Fact] public void Vector() => Test(@"\begin{bmatrix}11 & 12 & 21 & 22 & 31 & 32\end{bmatrix}", MathS.Vector(11, 12, 21, 22, 31, 32).T);