예제 #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"));
        }
예제 #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);
        }
예제 #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);
        }
예제 #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());
        }
예제 #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]);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #11
0
        public void TransposeDoubleOperationVector()
        {
            var a = MathS.Vector(1, 2);

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