예제 #1
0
        public void CanMap(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);
            Matrix <T> a      = matrix.Map(x => x, Zeros.AllowSkip);

            Assert.That(a, Is.EqualTo(matrix));
            Assert.That(a.Storage.IsDense, Is.EqualTo(matrix.Storage.IsDense));
            Assert.That(a.Storage.IsFullyMutable, Is.EqualTo(matrix.Storage.IsFullyMutable));

            T one = Matrix <T> .Build.One;

            Assert.That(matrix.Map(x => x, Zeros.Include), Is.EqualTo(matrix));
            Assert.That(matrix.Map(x => one, Zeros.Include), Is.EqualTo(Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount, one)));

            // Map into existing - we skip zeros, but existing values must still be reset to zero
            var dense = Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount, one);

            matrix.Map(x => x, dense, Zeros.AllowSkip);
            Assert.That(dense, Is.EqualTo(matrix));

            // Map into self, without using the proper MapInplace method:
            var copy = matrix.Clone();

            copy.Map(x => x, copy, Zeros.AllowSkip);
            Assert.That(copy, Is.EqualTo(matrix));
            if (matrix.Storage.IsFullyMutable)
            {
                copy.Map(x => one, copy, Zeros.AllowSkip);
                Assert.That(copy, Is.EqualTo(Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount, one)));
            }
        }
        public void CanRemoveColumn(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            for (var position = 0; position < matrix.ColumnCount; position++)
            {
                var result = matrix.RemoveColumn(position);
                Assert.That(result.ColumnCount, Is.EqualTo(matrix.ColumnCount - 1));
                for (int jr = 0, jm = 0; jr < result.ColumnCount; jr++, jm++)
                {
                    if (jr == position)
                    {
                        jm++;
                    }
                    for (var i = 0; i < result.RowCount; i++)
                    {
                        Assert.That(result[i, jr], Is.EqualTo(matrix[i, jm]));
                    }
                }
            }

            // Invalid
            Assert.That(() => matrix.RemoveColumn(-1), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix.RemoveColumn(matrix.ColumnCount + 1), Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
예제 #3
0
    static int Main()
    {
        TestMatrix <Test> tMat = new TestMatrix <Test> ();

        tMat.setStuff(new Test(), 0, 0);
        return(0);
    }
        public void IsNotEqualToPermutation(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            if (!matrix.Storage.IsFullyMutable)
            {
                return;
            }

            Matrix <T> permutation;

            if (matrix.RowCount >= 2 && matrix.Row(1).Any(x => !Zero.Equals(x)))
            {
                matrix.ClearRow(0);
                permutation = matrix.Clone();
                permutation.ClearRow(1);
                permutation.SetRow(0, matrix.Row(1));
            }
            else if (matrix.ColumnCount >= 2 && matrix.Column(1).Any(x => !Zero.Equals(x)))
            {
                matrix.ClearColumn(0);
                permutation = matrix.Clone();
                permutation.ClearColumn(1);
                permutation.SetColumn(0, matrix.Column(1));
            }
            else
            {
                return;
            }

            Assert.That(matrix, Is.Not.EqualTo(permutation));
            Assert.IsFalse(matrix.Equals(permutation));
        }
        public void CanDiagonalStackIntoResult(TestMatrix leftTestMatrix, TestMatrix rightTestMatrix)
        {
            Matrix <T> left  = Get(leftTestMatrix);
            Matrix <T> right = Get(rightTestMatrix);

            var result = Matrix <T> .Build.Dense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount);

            left.DiagonalStack(right, result);

            Assert.That(result.RowCount, Is.EqualTo(left.RowCount + right.RowCount));
            Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount));
            for (var i = 0; i < left.RowCount; i++)
            {
                for (var j = 0; j < left.ColumnCount; j++)
                {
                    Assert.That(result[i, j], Is.EqualTo(left[i, j]));
                }
            }
            for (var i = 0; i < right.RowCount; i++)
            {
                for (var j = 0; j < right.ColumnCount; j++)
                {
                    Assert.That(result[left.RowCount + i, left.ColumnCount + j], Is.EqualTo(right[i, j]));
                }
            }

            // Invalid
            Assert.That(() => left.DiagonalStack(right, default(Matrix <T>)), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => left.DiagonalStack(right, Matrix <T> .Build.Dense(left.RowCount + right.RowCount + 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException);
            Assert.That(() => left.DiagonalStack(right, Matrix <T> .Build.Dense(left.RowCount + right.RowCount - 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException);
            Assert.That(() => left.DiagonalStack(right, Matrix <T> .Build.Dense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount + 1)), Throws.ArgumentException);
            Assert.That(() => left.DiagonalStack(right, Matrix <T> .Build.Dense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount - 1)), Throws.ArgumentException);
        }
        public void CanDiagonalStack(TestMatrix leftTestMatrix, TestMatrix rightTestMatrix)
        {
            Matrix <T> left  = Get(leftTestMatrix);
            Matrix <T> right = Get(rightTestMatrix);

            var result = left.DiagonalStack(right);

            Assert.That(result.RowCount, Is.EqualTo(left.RowCount + right.RowCount));
            Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount));
            for (var i = 0; i < left.RowCount; i++)
            {
                for (var j = 0; j < left.ColumnCount; j++)
                {
                    Assert.That(result[i, j], Is.EqualTo(left[i, j]), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType());
                }
            }
            for (var i = 0; i < right.RowCount; i++)
            {
                for (var j = 0; j < right.ColumnCount; j++)
                {
                    Assert.That(result[left.RowCount + i, left.ColumnCount + j], Is.EqualTo(right[i, j]), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType());
                }
            }

            // Invalid
            Assert.That(() => left.DiagonalStack(default(Matrix <T>)), Throws.InstanceOf <ArgumentNullException>(), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType());
        }
        public void CanGetColumn(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            // First Column
            var firstcol = matrix.Column(0);

            Assert.That(firstcol.Count, Is.EqualTo(matrix.RowCount));
            for (var i = 0; i < matrix.RowCount; i++)
            {
                Assert.AreEqual(matrix[i, 0], firstcol[i]);
            }

            // Last Column
            var lastcol = matrix.Column(matrix.ColumnCount - 1);

            Assert.That(lastcol.Count, Is.EqualTo(matrix.RowCount));
            for (var i = 0; i < matrix.RowCount; i++)
            {
                Assert.AreEqual(matrix[i, matrix.ColumnCount - 1], lastcol[i]);
            }

            // Invalid Columns
            Assert.That(() => matrix.Column(-1), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix.Column(matrix.ColumnCount), Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
        public void CanPermuteColumns(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            Assume.That(matrix.Storage.IsFullyMutable);

            var m           = matrix.Clone();
            var rnd         = new System.Random(0);
            var permutation = new Permutation(Enumerable.Range(0, matrix.ColumnCount).OrderBy(i => rnd.Next()).ToArray());

            m.PermuteColumns(permutation);

            Assert.That(m, Is.Not.SameAs(matrix));
            Assert.That(m.RowCount, Is.EqualTo(matrix.RowCount));
            Assert.That(m.ColumnCount, Is.EqualTo(matrix.ColumnCount));
            var inverse = permutation.Inverse();

            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.That(m[i, j], Is.EqualTo(matrix[i, inverse[j]]));
                }
            }
        }
        public void CanStackIntoResult(TestMatrix topTestMatrix, TestMatrix bottomTestMatrix)
        {
            Matrix <T> top    = Get(topTestMatrix);
            Matrix <T> bottom = Get(bottomTestMatrix);

            Assume.That(top.ColumnCount, Is.EqualTo(bottom.ColumnCount));

            // THEN
            var result = Matrix <T> .Build.Dense(top.RowCount + bottom.RowCount, top.ColumnCount);

            top.Stack(bottom, result);

            Assert.That(result.RowCount, Is.EqualTo(top.RowCount + bottom.RowCount));
            for (var i = 0; i < result.RowCount; i++)
            {
                for (var j = 0; j < result.ColumnCount; j++)
                {
                    Assert.That(result[i, j], Is.EqualTo(i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j]));
                }
            }

            // Invalid
            Assert.That(() => top.Stack(bottom, default(Matrix <T>)), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => top.Stack(bottom, Matrix <T> .Build.Dense(top.RowCount + bottom.RowCount + 1, top.ColumnCount)), Throws.ArgumentException);
            Assert.That(() => top.Stack(bottom, Matrix <T> .Build.Dense(top.RowCount + bottom.RowCount - 1, top.ColumnCount)), Throws.ArgumentException);
            Assert.That(() => top.Stack(bottom, Matrix <T> .Build.Dense(top.RowCount + bottom.RowCount, top.ColumnCount + 1)), Throws.ArgumentException);
            Assert.That(() => top.Stack(bottom, Matrix <T> .Build.Dense(top.RowCount + bottom.RowCount, top.ColumnCount - 1)), Throws.ArgumentException);
        }
예제 #10
0
        public void CanMapIndexedInplace(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);
            var        a      = matrix.Clone();

            a.MapIndexedInplace((i, j, x) =>
            {
                if (i != 0 || j != 1)
                {
                    Assert.That(matrix.At(i, j), Is.EqualTo(x));
                }
                return(x);
            }, Zeros.AllowSkip);
            Assert.That(a, Is.EqualTo(matrix));

            if (matrix.Storage.IsFullyMutable)
            {
                a.MapIndexedInplace((i, j, x) => x, Zeros.Include);
                Assert.That(a, Is.EqualTo(matrix));

                T one = Matrix <T> .Build.One;
                a.MapIndexedInplace((i, j, x) => i == j ? one : x, Zeros.AllowSkip);
                Assert.That(a.Diagonal().All(x => one.Equals(x) || Zero.Equals(x)), Is.True);
                Assert.That(a.EnumerateIndexed().All(z => (z.Item1 == z.Item2) || (matrix.At(z.Item1, z.Item2).Equals(z.Item3))));

                a.MapIndexedInplace((i, j, x) => one, Zeros.Include);
                Assert.That(a, Is.EqualTo(Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount, one)));
            }
        }
예제 #11
0
        public void Constructor__2DArray_ShouldCreateMatrixMatchingTheInputArray(float[,] array)
        {
            // arrange
            // act
            var result = new TestMatrix(array);

            // assert
            using var _ = new AssertionScope();
            result.Rows.Should().Be(array.GetLength(0));
            result.Columns.Should().Be(array.GetLength(1));
            foreach (var row in Enumerable.Range(0, result.Rows))
            {
                foreach (var col in Enumerable.Range(0, result.Columns))
                {
                    result[row, col]
                    .Should()
                    .Be(
                        array[row, col],
                        "matrix[{0}, {1}] should equal array[{0}, {1}]",
                        row,
                        col
                        );
                }
            }
        }
        public void CanSetColumnArray(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            // First Column
            var m = matrix.Clone();

            m.SetColumn(0, Vector <T> .Build.Dense(matrix.RowCount).ToArray());
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.That(m[i, j], Is.EqualTo(j == 0 ? Zero : matrix[i, j]));
                }
            }

            // Last Column
            m = matrix.Clone();
            m.SetColumn(matrix.ColumnCount - 1, new T[matrix.RowCount]);
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.That(m[i, j], Is.EqualTo(j == matrix.ColumnCount - 1 ? Zero : matrix[i, j]));
                }
            }

            // Invalid Rows
            Assert.That(() => matrix.SetColumn(0, default(T[])), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => matrix.SetColumn(-1, new T[matrix.RowCount]), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix.SetColumn(matrix.ColumnCount, new T[matrix.RowCount]), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix.SetColumn(0, new T[matrix.RowCount - 1]), Throws.ArgumentException);
            Assert.That(() => matrix.SetColumn(0, new T[matrix.RowCount + 1]), Throws.ArgumentException);
        }
        public void CanGetRow(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            // First Row
            var firstrow = matrix.Row(0);

            Assert.That(firstrow.Count, Is.EqualTo(matrix.ColumnCount));
            for (var j = 0; j < matrix.ColumnCount; j++)
            {
                Assert.AreEqual(matrix[0, j], firstrow[j]);
            }

            // Last Row
            var lastrow = matrix.Row(matrix.RowCount - 1);

            Assert.That(lastrow.Count, Is.EqualTo(matrix.ColumnCount));
            for (var j = 0; j < matrix.ColumnCount; j++)
            {
                Assert.AreEqual(matrix[matrix.RowCount - 1, j], lastrow[j]);
            }

            // Invalid Rows
            Assert.That(() => matrix.Row(-1), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix.Row(matrix.RowCount), Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
        public void CanGetStrictlyLowerTriangleIntoResult(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            var dense = Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount);

            matrix.StrictlyLowerTriangle(dense);
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.That(dense[i, j], Is.EqualTo(i > j ? matrix[i, j] : Zero));
                }
            }

            var sparse = Matrix <T> .Build.Sparse(matrix.RowCount, matrix.ColumnCount);

            matrix.StrictlyLowerTriangle(sparse);
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.That(sparse[i, j], Is.EqualTo(i > j ? matrix[i, j] : Zero));
                }
            }

            Assert.That(() => matrix.StrictlyLowerTriangle(null), Throws.Exception);
            Assert.That(() => matrix.StrictlyLowerTriangle(Matrix <T> .Build.Sparse(matrix.RowCount + 1, matrix.ColumnCount)), Throws.ArgumentException);
            Assert.That(() => matrix.StrictlyLowerTriangle(Matrix <T> .Build.Dense(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException);
        }
예제 #15
0
 public static void Main()
 {
     TestAttribute.Test();
     TestPoints.Test();
     TestGenericList.Test();
     TestMatrix.Test();
 }
        public void CanClear(TestMatrix testMatrix)
        {
            Matrix <T> matrix  = Get(testMatrix);
            var        cleared = matrix.Clone();

            cleared.Clear();
            Assert.That(cleared, Is.EqualTo(Matrix <T> .Build.SameAs(matrix)));
        }
        public void IsNotEqualToNonMatrixType(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            Assert.That(matrix, Is.Not.EqualTo(2));
            Assert.IsFalse(matrix.Equals(2));
            Assert.IsFalse(matrix.Equals((object)2));
            Assert.IsFalse(((object)matrix).Equals(2));
            Assert.IsFalse(matrix == (object)2);
        }
        public void CanCloneUsingICloneable(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);
            var        clone  = (Matrix <T>)((ICloneable)matrix).Clone();

            Assert.That(clone, Is.Not.SameAs(matrix));
            Assert.That(clone, Is.EqualTo(matrix));
            Assert.That(clone.RowCount, Is.EqualTo(matrix.RowCount));
            Assert.That(clone.ColumnCount, Is.EqualTo(matrix.ColumnCount));
        }
예제 #19
0
        public static Matrix <double> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
            case TestMatrix.DenseSquare3x3: return(M.DenseOfArray(new[, ] {
                    { 1d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d, 2d }
                }));

            case TestMatrix.DenseSquare3x3b: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, 2.2d }, { -4.4d, 5.5d, 6.6d }
                }));

            case TestMatrix.DenseSquare3x3c: return(M.DenseOfArray(new[, ] {
                    { 1d, 2d, 3d }, { 2d, 2d, 0d }, { 3d, 0d, 3d }
                }));

            case TestMatrix.DenseSquare4x4: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d, -4.4d }, { 0d, 1.1d, 2.2d, 3.3d }, { 1d, 2.1d, 6.2d, 4.3d }, { -4.4d, 5.5d, 6.6d, -7.7d }
                }));

            case TestMatrix.DenseSquare4x4b: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }
                }));

            case TestMatrix.DenseTall3x2: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d }, { 0d, 1.1d }, { -4.4d, 5.5d }
                }));

            case TestMatrix.DenseWide2x3: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, 2.2d }
                }));

            case TestMatrix.SparseSquare3x3: return(M.SparseOfArray(new[, ] {
                    { 7d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d, 2d }
                }));

            case TestMatrix.SparseSquare3x3b: return(M.SparseOfArray(new[, ] {
                    { 7d, 1d, 2d }, { 1d, 0d, 0d }, { -2d, 0d, 0d }
                }));

            case TestMatrix.SparseWide2x3: return(M.SparseOfArray(new[, ] {
                    { -1.1d, 0d, 0d }, { 0d, 1.1d, 2.2d }
                }));

            case TestMatrix.DiagonalSquare3x3: return(M.Diagonal(3, 3, new[] { 1d, -2d, 1.5d }));

            case TestMatrix.DiagonalSquare3x3b: return(M.Diagonal(3, 3, new[] { 1d, 0d, -1.5d }));

            case TestMatrix.UserSquare3x3: return(new UserDefinedMatrix(new[, ] {
                    { 0d, 1d, 2d }, { -1d, 7.7d, 0d }, { -2d, 0d, 0d }
                }));

            default: throw new NotSupportedException();
            }
        }
예제 #20
0
        public static Matrix <Complex> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
            case TestMatrix.DenseSquare3x3: return(M.DenseOfArray(new[, ] {
                    { 1d, new Complex(1.1d, -4d), 2d }, { 1d, 1d, 2d }, { 1d, new Complex(1d, 2d), 2d }
                }));

            case TestMatrix.DenseSquare3x3b: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, new Complex(2.2d, -1.2d) }, { -4.4d, 5.5d, 6.6d }
                }));

            case TestMatrix.DenseSquare3x3c: return(M.DenseOfArray(new[, ] {
                    { 1d, 2d, 3d }, { 2d, new Complex(2d, 2d), 0d }, { 3d, Complex.Zero, 3d }
                }));

            case TestMatrix.DenseSquare4x4: return(M.DenseOfArray(new[, ] {
                    { new Complex(-1.1d, -2d), -2.2d, -3.3d, -4.4d }, { 0d, 1.1d, 2.2d, 3.3d }, { 1d, 2.1d, 6.2d, 4.3d }, { -4.4d, 5.5d, 6.6d, -7.7d }
                }));

            case TestMatrix.DenseSquare4x4b: return(M.DenseOfArray(new[, ] {
                    { -1.1d, new Complex(-2.2d, 3.4d), -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }
                }));

            case TestMatrix.DenseTall3x2: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d }, { Complex.Zero, 1.1d }, { -4.4d, 5.5d }
                }));

            case TestMatrix.DenseWide2x3: return(M.DenseOfArray(new[, ] {
                    { -1.1d, -2.2d, -3.3d }, { 0d, new Complex(1.1d, 0.1d), 2.2d }
                }));

            case TestMatrix.SparseSquare3x3: return(M.SparseOfArray(new[, ]  {
                    { 7d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d + Complex.ImaginaryOne, 2d }
                }));

            case TestMatrix.SparseSquare3x3b: return(M.SparseOfArray(new[, ] {
                    { 7d, 1d, 2d }, { new Complex(1d, 2d), 0d, Complex.Zero }, { -2d, 0d, 0d }
                }));

            case TestMatrix.SparseWide2x3: return(M.SparseOfArray(new[, ] {
                    { -1.1d, 0d, 0d }, { 0d, new Complex(1.1d, 2d), 2.2d }
                }));

            case TestMatrix.DiagonalSquare3x3: return(M.Diagonal(3, 3, new[] { new Complex(1d, 1d), -2d, 1.5d }));

            case TestMatrix.DiagonalSquare3x3b: return(M.Diagonal(3, 3, new[] { new Complex(1d, 2d), 0d, -1.5d }));

            case TestMatrix.UserSquare3x3: return(new UserDefinedMatrix(new[, ] {
                    { 0d, 1d, 2d }, { -1d, 7.7d, 0d }, { -2d, Complex.Zero, 0d }
                }));

            default: throw new NotSupportedException();
            }
        }
예제 #21
0
        public static Matrix <Complex32> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
            case TestMatrix.DenseSquare3x3: return(M.DenseOfArray(new[, ] {
                    { 1f, new Complex32(1.1f, -4f), 2f }, { 1f, 1f, 2f }, { 1f, new Complex32(1f, 2f), 2f }
                }));

            case TestMatrix.DenseSquare3x3b: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, new Complex32(2.2f, -1.2f) }, { -4.4f, 5.5f, 6.6f }
                }));

            case TestMatrix.DenseSquare3x3c: return(M.DenseOfArray(new[, ] {
                    { 1f, 2f, 3f }, { 2f, new Complex32(2f, 2f), 0f }, { 3f, Complex32.Zero, 3f }
                }));

            case TestMatrix.DenseSquare4x4: return(M.DenseOfArray(new[, ] {
                    { new Complex32(-1.1f, -2f), -2.2f, -3.3f, -4.4f }, { 0f, 1.1f, 2.2f, 3.3f }, { 1f, 2.1f, 6.2f, 4.3f }, { -4.4f, 5.5f, 6.6f, -7.7f }
                }));

            case TestMatrix.DenseSquare4x4b: return(M.DenseOfArray(new[, ] {
                    { -1.1f, new Complex32(-2.2f, 3.4f), -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }
                }));

            case TestMatrix.DenseTall3x2: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f }, { Complex32.Zero, 1.1f }, { -4.4f, 5.5f }
                }));

            case TestMatrix.DenseWide2x3: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f }, { 0f, new Complex32(1.1f, 0.1f), 2.2f }
                }));

            case TestMatrix.SparseSquare3x3: return(M.SparseOfArray(new[, ] {
                    { 7f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f + Complex32.ImaginaryOne, 2f }
                }));

            case TestMatrix.SparseSquare3x3b: return(M.SparseOfArray(new[, ] {
                    { 7f, 1f, 2f }, { new Complex32(1f, 2f), 0f, Complex32.Zero }, { -2f, 0f, 0f }
                }));

            case TestMatrix.SparseWide2x3: return(M.SparseOfArray(new[, ] {
                    { -1.1f, 0f, 0f }, { 0f, new Complex32(1.1f, 2f), 2.2f }
                }));

            case TestMatrix.DiagonalSquare3x3: return(M.Diagonal(3, 3, new[] { new Complex32(1f, 1f), -2f, 1.5f }));

            case TestMatrix.DiagonalSquare3x3b: return(M.Diagonal(3, 3, new[] { new Complex32(1f, 2f), 0f, -1.5f }));

            case TestMatrix.UserSquare3x3: return(new UserDefinedMatrix(new[, ] {
                    { 0f, 1f, 2f }, { -1f, 7.7f, 0f }, { -2f, Complex32.Zero, 0f }
                }));

            default: throw new NotSupportedException();
            }
        }
예제 #22
0
        public static Matrix <float> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
            case TestMatrix.DenseSquare3x3: return(M.DenseOfArray(new[, ] {
                    { 1f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f, 2f }
                }));

            case TestMatrix.DenseSquare3x3b: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, 2.2f }, { -4.4f, 5.5f, 6.6f }
                }));

            case TestMatrix.DenseSquare3x3c: return(M.DenseOfArray(new[, ] {
                    { 1f, 2f, 3f }, { 2f, 2f, 0f }, { 3f, 0f, 3f }
                }));

            case TestMatrix.DenseSquare4x4: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f, -4.4f }, { 0f, 1.1f, 2.2f, 3.3f }, { 1f, 2.1f, 6.2f, 4.3f }, { -4.4f, 5.5f, 6.6f, -7.7f }
                }));

            case TestMatrix.DenseSquare4x4b: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }
                }));

            case TestMatrix.DenseTall3x2: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f }, { 0f, 1.1f }, { -4.4f, 5.5f }
                }));

            case TestMatrix.DenseWide2x3: return(M.DenseOfArray(new[, ] {
                    { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, 2.2f }
                }));

            case TestMatrix.SparseSquare3x3: return(M.SparseOfArray(new[, ] {
                    { 7f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f, 2f }
                }));

            case TestMatrix.SparseSquare3x3b: return(M.SparseOfArray(new[, ] {
                    { 7f, 1f, 2f }, { 1f, 0f, 0f }, { -2f, 0f, 0f }
                }));

            case TestMatrix.SparseWide2x3: return(M.SparseOfArray(new[, ] {
                    { -1.1f, 0f, 0f }, { 0f, 1.1f, 2.2f }
                }));

            case TestMatrix.DiagonalSquare3x3: return(M.Diagonal(3, 3, new[] { 1f, -2f, 1.5f }));

            case TestMatrix.DiagonalSquare3x3b: return(M.Diagonal(3, 3, new[] { 1f, 0f, -1.5f }));

            case TestMatrix.UserSquare3x3: return(new UserDefinedMatrix(new[, ]  {
                    { 0f, 1f, 2f }, { -1f, 7.7f, 0f }, { -2f, 0f, 0f }
                }));

            default: throw new NotSupportedException();
            }
        }
예제 #23
0
        public void Equals__Object_ShouldReturnFalse_WhenOtherObjectIsNotMatrix(object obj)
        {
            // arrange
            var sut = new TestMatrix(2, 2);

            // act
            var result = sut.Equals(obj);

            // assert
            result.Should().BeFalse();
        }
        public void CanToRowWiseArray(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);
            var        array  = matrix.ToRowWiseArray();

            Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount));
            for (int i = 0; i < array.Length; i++)
            {
                Assert.That(array[i], Is.EqualTo(matrix[i / matrix.ColumnCount, i % matrix.ColumnCount]));
            }
        }
예제 #25
0
        public void IsEqualToItself(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            Assert.That(matrix, Is.EqualTo(matrix));
            Assert.IsTrue(matrix.Equals(matrix));
            Assert.IsTrue(matrix.Equals((object)matrix));
            Assert.IsTrue(((object)matrix).Equals(matrix));
            Assert.IsTrue(matrix == (object)matrix);
            Assert.IsTrue((object)matrix == matrix);
        }
        public void CanGetDiagonal(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            var diag = matrix.Diagonal();

            Assert.That(diag.Count, Is.EqualTo(Math.Min(matrix.RowCount, matrix.ColumnCount)));
            for (var i = 0; i < Math.Min(matrix.RowCount, matrix.ColumnCount); i++)
            {
                Assert.That(diag[i], Is.EqualTo(matrix[i, i]));
            }
        }
        public void CanGetFieldsByIndex(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            Assert.That(() => matrix[0, 0], Throws.Nothing);
            Assert.That(() => matrix[0, matrix.ColumnCount - 1], Throws.Nothing);
            Assert.That(() => matrix[matrix.RowCount - 1, 0], Throws.Nothing);

            Assert.That(() => matrix[-1, 1], Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix[1, -1], Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => matrix[0, matrix.ColumnCount], Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
        public void CanClearColumns(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);

            Assume.That(matrix.ColumnCount, Is.GreaterThanOrEqualTo(2));

            var cleared = matrix.Clone();

            cleared.ClearColumns(1);
            Assert.That(cleared.At(0, 0), Is.EqualTo(matrix.At(0, 0)));
            Assert.That(cleared.At(0, 1), Is.EqualTo(Zero));
        }
        public void CanCreateSameKind(TestMatrix testMatrix)
        {
            Matrix <T> matrix = Get(testMatrix);
            var        empty  = Matrix <T> .Build.SameAs(matrix, 5, 6);

            Assert.That(empty, Is.EqualTo(Matrix <T> .Build.Dense(5, 6)));
            Assert.That(empty.Storage.IsDense, Is.EqualTo(matrix.Storage.IsDense));

            Assert.That(() => Matrix <T> .Build.SameAs(matrix, 0, 2), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => Matrix <T> .Build.SameAs(matrix, 2, 0), Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(() => Matrix <T> .Build.SameAs(matrix, -1, -1), Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
        public void IsEqualToItself(TestMatrix testMatrix)
        {
            Matrix <T> matrix       = Get(testMatrix);
            object     matrixObject = matrix;

            Assert.That(matrix, Is.EqualTo(matrix));
            Assert.IsTrue(matrix.Equals(matrix));
            Assert.IsTrue(matrix.Equals(matrixObject));
            Assert.IsTrue(matrixObject.Equals(matrix));
            Assert.IsTrue(matrix == matrixObject);
            Assert.IsTrue(matrixObject == matrix);
        }
예제 #31
0
        public static Matrix<Complex> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
                case TestMatrix.DenseSquare3x3: return M.DenseOfArray(new[,] { { 1d, new Complex(1.1d, -4d), 2d }, { 1d, 1d, 2d }, { 1d, new Complex(1d, 2d), 2d } });
                case TestMatrix.DenseSquare3x3b: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, new Complex(2.2d, -1.2d) }, { -4.4d, 5.5d, 6.6d } });
                case TestMatrix.DenseSquare3x3c: return M.DenseOfArray(new[,] { { 1d, 2d, 3d }, { 2d, new Complex(2d, 2d), 0d }, { 3d, Complex.Zero, 3d } });
                case TestMatrix.DenseSquare4x4: return M.DenseOfArray(new[,] { { new Complex(-1.1d, -2d), -2.2d, -3.3d, -4.4d }, { 0d, 1.1d, 2.2d, 3.3d }, { 1d, 2.1d, 6.2d, 4.3d }, { -4.4d, 5.5d, 6.6d, -7.7d } });
                case TestMatrix.DenseSquare4x4b: return M.DenseOfArray(new[,] { { -1.1d, new Complex(-2.2d, 3.4d), -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d } });
                case TestMatrix.DenseTall3x2: return M.DenseOfArray(new[,] { { -1.1d, -2.2d }, { Complex.Zero, 1.1d }, { -4.4d, 5.5d } });
                case TestMatrix.DenseWide2x3: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d }, { 0d, new Complex(1.1d, 0.1d), 2.2d } });

                case TestMatrix.SparseSquare3x3: return M.SparseOfArray(new[,]  { { 7d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d + Complex.ImaginaryOne, 2d } });
                case TestMatrix.SparseSquare3x3b: return M.SparseOfArray(new[,] { { 7d, 1d, 2d }, { new Complex(1d, 2d), 0d, Complex.Zero }, { -2d, 0d, 0d } });
                case TestMatrix.SparseWide2x3: return M.SparseOfArray(new[,] { { -1.1d, 0d, 0d }, { 0d, new Complex(1.1d, 2d), 2.2d } });

                case TestMatrix.DiagonalSquare3x3: return M.Diagonal(3, 3, new[] { new Complex(1d, 1d), -2d, 1.5d });
                case TestMatrix.DiagonalSquare3x3b: return M.Diagonal(3, 3, new[] { new Complex(1d, 2d), 0d, -1.5d });

                case TestMatrix.UserSquare3x3: return new UserDefinedMatrix(new[,] { { 0d, 1d, 2d }, { -1d, 7.7d, 0d }, { -2d, Complex.Zero, 0d } });
                default: throw new NotSupportedException();
            }
        }
예제 #32
0
        public static Matrix<Complex32> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
                case TestMatrix.DenseSquare3x3: return M.DenseOfArray(new[,] { { 1f, new Complex32(1.1f, -4f), 2f }, { 1f, 1f, 2f }, { 1f, new Complex32(1f, 2f), 2f } });
                case TestMatrix.DenseSquare3x3b: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, new Complex32(2.2f, -1.2f) }, { -4.4f, 5.5f, 6.6f } });
                case TestMatrix.DenseSquare3x3c: return M.DenseOfArray(new[,] { { 1f, 2f, 3f }, { 2f, new Complex32(2f, 2f), 0f }, { 3f, Complex32.Zero, 3f } });
                case TestMatrix.DenseSquare4x4: return M.DenseOfArray(new[,] { { new Complex32(-1.1f, -2f), -2.2f, -3.3f, -4.4f }, { 0f, 1.1f, 2.2f, 3.3f }, { 1f, 2.1f, 6.2f, 4.3f }, { -4.4f, 5.5f, 6.6f, -7.7f } });
                case TestMatrix.DenseSquare4x4b: return M.DenseOfArray(new[,] { { -1.1f, new Complex32(-2.2f, 3.4f), -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f } });
                case TestMatrix.DenseTall3x2: return M.DenseOfArray(new[,] { { -1.1f, -2.2f }, { Complex32.Zero, 1.1f }, { -4.4f, 5.5f } });
                case TestMatrix.DenseWide2x3: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f }, { 0f, new Complex32(1.1f, 0.1f), 2.2f } });

                case TestMatrix.SparseSquare3x3: return M.SparseOfArray(new[,] { { 7f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f + Complex32.ImaginaryOne, 2f } });
                case TestMatrix.SparseSquare3x3b: return M.SparseOfArray(new[,] { { 7f, 1f, 2f }, { new Complex32(1f, 2f), 0f, Complex32.Zero }, { -2f, 0f, 0f } });
                case TestMatrix.SparseWide2x3: return M.SparseOfArray(new[,] { { -1.1f, 0f, 0f }, { 0f, new Complex32(1.1f, 2f), 2.2f } });

                case TestMatrix.DiagonalSquare3x3: return M.Diagonal(3, 3, new[] { new Complex32(1f, 1f), -2f, 1.5f });
                case TestMatrix.DiagonalSquare3x3b: return M.Diagonal(3, 3, new[] { new Complex32(1f, 2f), 0f, -1.5f });

                case TestMatrix.UserSquare3x3: return new UserDefinedMatrix(new[,] { { 0f, 1f, 2f }, { -1f, 7.7f, 0f }, { -2f, Complex32.Zero, 0f } });

                default: throw new NotSupportedException();
            }
        }
예제 #33
0
        public static Matrix<double> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
                case TestMatrix.DenseSquare3x3: return M.DenseOfArray(new[,] { { 1d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d, 2d } });
                case TestMatrix.DenseSquare3x3b: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, 2.2d }, { -4.4d, 5.5d, 6.6d } });
                case TestMatrix.DenseSquare3x3c: return M.DenseOfArray(new[,] { { 1d, 2d, 3d }, { 2d, 2d, 0d }, { 3d, 0d, 3d } });
                case TestMatrix.DenseSquare4x4: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d, -4.4d }, { 0d, 1.1d, 2.2d, 3.3d }, { 1d, 2.1d, 6.2d, 4.3d }, { -4.4d, 5.5d, 6.6d, -7.7d } });
                case TestMatrix.DenseSquare4x4b: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d }, { -1.1d, -2.2d, -3.3d, -4.4d } });
                case TestMatrix.DenseTall3x2: return M.DenseOfArray(new[,] { { -1.1d, -2.2d }, { 0d, 1.1d }, { -4.4d, 5.5d } });
                case TestMatrix.DenseWide2x3: return M.DenseOfArray(new[,] { { -1.1d, -2.2d, -3.3d }, { 0d, 1.1d, 2.2d } });

                case TestMatrix.SparseSquare3x3: return M.SparseOfArray(new[,] { { 7d, 1d, 2d }, { 1d, 1d, 2d }, { 1d, 1d, 2d } });
                case TestMatrix.SparseSquare3x3b: return M.SparseOfArray(new[,] { { 7d, 1d, 2d }, { 1d, 0d, 0d }, { -2d, 0d, 0d } });
                case TestMatrix.SparseWide2x3: return M.SparseOfArray(new[,] { { -1.1d, 0d, 0d }, { 0d, 1.1d, 2.2d } });

                case TestMatrix.DiagonalSquare3x3: return M.Diagonal(3, 3, new[] { 1d, -2d, 1.5d });
                case TestMatrix.DiagonalSquare3x3b: return M.Diagonal(3, 3, new[] { 1d, 0d, -1.5d });

                case TestMatrix.UserSquare3x3: return new UserDefinedMatrix(new[,] { { 0d, 1d, 2d }, { -1d, 7.7d, 0d }, { -2d, 0d, 0d } });

                default: throw new NotSupportedException();
            }
        }
예제 #34
0
        public static Matrix<float> Matrix(TestMatrix matrix)
        {
            switch (matrix)
            {
                case TestMatrix.DenseSquare3x3: return M.DenseOfArray(new[,] { { 1f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f, 2f } });
                case TestMatrix.DenseSquare3x3b: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, 2.2f }, { -4.4f, 5.5f, 6.6f } });
                case TestMatrix.DenseSquare3x3c: return M.DenseOfArray(new[,] { { 1f, 2f, 3f }, { 2f, 2f, 0f }, { 3f, 0f, 3f } });
                case TestMatrix.DenseSquare4x4: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f, -4.4f }, { 0f, 1.1f, 2.2f, 3.3f }, { 1f, 2.1f, 6.2f, 4.3f }, { -4.4f, 5.5f, 6.6f, -7.7f } });
                case TestMatrix.DenseSquare4x4b: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f } });
                case TestMatrix.DenseTall3x2: return M.DenseOfArray(new[,] { { -1.1f, -2.2f }, { 0f, 1.1f }, { -4.4f, 5.5f } });
                case TestMatrix.DenseWide2x3: return M.DenseOfArray(new[,] { { -1.1f, -2.2f, -3.3f }, { 0f, 1.1f, 2.2f } });

                case TestMatrix.SparseSquare3x3: return M.SparseOfArray(new[,] { { 7f, 1f, 2f }, { 1f, 1f, 2f }, { 1f, 1f, 2f } });
                case TestMatrix.SparseSquare3x3b: return M.SparseOfArray(new[,] { { 7f, 1f, 2f }, { 1f, 0f, 0f }, { -2f, 0f, 0f } });
                case TestMatrix.SparseWide2x3: return M.SparseOfArray(new[,] { { -1.1f, 0f, 0f }, { 0f, 1.1f, 2.2f } });

                case TestMatrix.DiagonalSquare3x3: return M.Diagonal(3, 3, new[] { 1f, -2f, 1.5f });
                case TestMatrix.DiagonalSquare3x3b: return M.Diagonal(3, 3, new[] { 1f, 0f, -1.5f });

                case TestMatrix.UserSquare3x3: return new UserDefinedMatrix(new[,]  { { 0f, 1f, 2f }, { -1f, 7.7f, 0f }, { -2f, 0f, 0f } });

                default: throw new NotSupportedException();
            }
        }