Esempio n. 1
0
        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"));
        }
Esempio n. 2
0
        [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);
        }
Esempio n. 3
0
        [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);
        }
Esempio n. 4
0
        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());
        }
Esempio n. 5
0
        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]);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        [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);
        }
Esempio n. 8
0
        [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);
        }
Esempio n. 9
0
        [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);
        }
Esempio n. 10
0
        [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);
        }
Esempio n. 11
0
        public void TransposeDoubleOperationVector()
        {
            var a = MathS.Vector(1, 2);

            Assert.Equal(a, a.T.T);
        }
Esempio n. 12
0
 [Fact] public void ZeroVector()
 => Assert.Equal(MathS.Vector(0, 0, 0, 0), MathS.ZeroVector(4));
Esempio n. 13
0
 [Fact] public void LeftScalarDivisionUnsuccessfulInnerSimplified()
 => Assert.Equal(2 / MathS.Vector(3, 4), (2 / MathS.Vector(3, 4)).InnerSimplified);
Esempio n. 14
0
 [Fact] public void LeftScalarDivisionUnsuccessfulEvaled()
 => Assert.Equal(2 / MathS.Vector(3, 4), (2 / MathS.Vector(3, 4)).Evaled);
Esempio n. 15
0
 [Fact] public void MatrixAddition1()
 => Assert.Equal(MathS.Vector(3, 2), MathS.Vector(1, 2) + MathS.Vector(2, 0));
Esempio n. 16
0
 [Fact] public void WithColumnVector()
 => Assert.Equal(MathS.Vector(1, 2, 3), MathS.ZeroVector(3).WithColumn(0, MathS.Vector(1, 2, 3)));
Esempio n. 17
0
 [Fact] public void RightScalarDivision()
 => Assert.Equal(MathS.Vector("1.5", 2), (MathS.Vector(3, 4) / 2).InnerSimplified);
Esempio n. 18
0
 [Fact] public void RightScalarMulitiplication()
 => Assert.Equal(MathS.Vector(6, 8), (MathS.Vector(3, 4) * 2).InnerSimplified);
Esempio n. 19
0
 [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);
Esempio n. 20
0
 [Fact] public void MatrixSubtraction1()
 => Assert.Equal(MathS.Vector(-1, 2), MathS.Vector(1, 2) - MathS.Vector(2, 0));
Esempio n. 21
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);
Esempio n. 22
0
 /// <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());
Esempio n. 23
0
 [Fact] public void WithRowVector()
 => Assert.Equal(MathS.Vector(0, 0, 3, 0), MathS.ZeroVector(4).WithRow(2, MathS.Vector(3)));
Esempio n. 24
0
 [Fact] public void VectorSingle() =>
 Test(@"\begin{bmatrix}x\end{bmatrix}", MathS.Vector(x));
Esempio n. 25
0
 [Fact] public void LeftScalarSubtraction()
 => Assert.Equal(MathS.Vector(-1, -2), (2 - MathS.Vector(3, 4)).InnerSimplified);
Esempio n. 26
0
 [Fact] public void RightScalarSubtraction()
 => Assert.Equal(MathS.Vector(1, 2), (MathS.Vector(3, 4) - 2).InnerSimplified);
Esempio n. 27
0
 [Fact] public void LeftScalarAddition()
 => Assert.Equal(MathS.Vector(5, 6), (2 + MathS.Vector(3, 4)).InnerSimplified);
Esempio n. 28
0
 [Fact] public void MatrixMultiplication1()
 => Assert.Equal(MathS.Vector(1, -1), H * MathS.Vector(0, 1));
Esempio n. 29
0
 [Fact] public void RightScalarAddition()
 => Assert.Equal(MathS.Vector(5, 6), (MathS.Vector(3, 4) + 2).InnerSimplified);
Esempio n. 30
0
 [Fact] public void Vector() =>
 Test(@"\begin{bmatrix}11 & 12 & 21 & 22 & 31 & 32\end{bmatrix}", MathS.Vector(11, 12, 21, 22, 31, 32).T);