using System; using System.Collections.Generic; using System.Linq; using System.Text; using Azimuth.DenseLinearAlgebra; namespace Azimuth.UnitTests.DenseLinearAlgebra { public class IVectorTests : UnitTestSharp.TestFixture { public class PostMatrixMultiply : UnitTestSharp.TestFixture { public void NonSymmetricOrthogonal() { var matrix = new SquareMatrix(new Scalar[,] { { 0, -1, }, { 1, 0, }, }); var vector = new DenseVector(new Scalar[] { 3, 4 }); var expected = new DenseVector(new Scalar[] { 4, -3 }); CheckEqual(expected, vector.PostMatrixMultiplyBy(matrix)); } public void NonSquare_Fat() { var matrix = new RectangularMatrix(new Scalar[,] { { 0, -1, 1, }, { 1, 0, 1, }, }); var vector = new DenseVector(new Scalar[] { 3, 4 }); var expected = new DenseVector(new Scalar[] { 4, -3, 7 }); CheckEqual(expected, vector.PostMatrixMultiplyBy(matrix)); } public void NonSquare_Tall() { var matrix = new RectangularMatrix(new Scalar[,] { { 0 }, { 1 }, }); var vector = new DenseVector(new Scalar[] { 3, 4 }); var expected = new DenseVector(new Scalar[] { 4 }); CheckEqual(expected, vector.PostMatrixMultiplyBy(matrix)); } public void DimsDontMatch() { var matrix = new RectangularMatrix(new Scalar[,] { { 0, -1, 1, }, { 1, 0, 1, }, }); var vector = new DenseVector(new Scalar[] { 3 }); CheckThrow(typeof(Exception)); vector.PostMatrixMultiplyBy(matrix); } public void Basic_x33() { var matrix = new RectangularMatrix(new Scalar[,] { { 1, 2, 3, }, { 3, 2, 1, }, { 1, 0, 0, }, }); var actual = new DenseVector[3]; for(int i = 0; i < actual.Length; ++i) { actual[i] = new DenseVector(actual.Length); actual[i][i] = 1; actual[i] = actual[i].PostMatrixMultiplyBy(matrix); } var actualMat = new SquareMatrix(actual); CheckEqual(matrix.Transpose(), actualMat); } } public class PreMatrixMultiply : UnitTestSharp.TestFixture { public void NonSymmetricOrthogonal() { var matrix = new SquareMatrix(new Scalar[,] { { 0, -1, }, { 1, 0, }, }); var vector = new DenseVector(new Scalar[] { 3, 4 }); var expected = new DenseVector(new Scalar[] { -4, 3 }); CheckEqual(expected, vector.PreMatrixMultiplyBy(matrix)); } public void NonSquare_Fat() { var matrix = new RectangularMatrix(new Scalar[,] { { 0, -1, 1, }, { 1, 0, 1, }, }); var vector = new DenseVector(new Scalar[] { 4, -3, 7 }); var expected = new DenseVector(new Scalar[] { 10, 11 }); CheckEqual(expected, vector.PreMatrixMultiplyBy(matrix)); } public void NonSquare_Tall() { var matrix = new RectangularMatrix(new Scalar[,] { { 0, 1 }, }); var vector = new DenseVector(new Scalar[] { 3, 4 }); var expected = new DenseVector(new Scalar[] { 4 }); CheckEqual(expected, vector.PreMatrixMultiplyBy(matrix)); } public void DimsDontMatch() { var matrix = new RectangularMatrix(new Scalar[,] { { 0, -1, 1, }, { 1, 0, 1, }, }); var vector = new DenseVector(new Scalar[] { 3 }); CheckThrow(typeof(Exception)); vector.PreMatrixMultiplyBy(matrix); } public void AggressiveTesting() { var matrix = new RectangularMatrix(new Scalar[,] { { 1, 2, }, { 3, 4, }, }); var vector = new DenseVector(new Scalar[] { 5, 6, }); var expected = new DenseVector(new Scalar[] { 17, 39 }); vector = vector.PreMatrixMultiplyBy(matrix); CheckEqual(expected, vector); } } public class CountTests : UnitTestSharp.TestFixture { DenseVector dense = new DenseVector(new Scalar[] { -1, 1, -1, 0, 1, 2, 3 }); public void Basic() { CheckEqual(7, dense.Count()); } public void Negative() { CheckEqual(2, dense.Count(x => x < 0)); } public void Positive() { CheckEqual(5, dense.Count(x => x.Sign == 1)); } } public class AllTests : UnitTestSharp.TestFixture { DenseVector dense = new DenseVector(new Scalar[] { -1, 1, -1, 0, 1, 2, 3 }); public void False() { CheckFalse(dense.All(x => x.Sign == 1)); } public void True() { Check(dense.All(x => x < 50)); Check(dense.All(x => x > -50)); } } public class AnyTests : UnitTestSharp.TestFixture { DenseVector dense = new DenseVector(new Scalar[] { -1, 1, -1, 0, 1, 2, 3 }); public void False() { CheckFalse(dense.Any(x => x.Sign == 0)); } public void True() { Check(dense.Any(x => x.Sign == 1)); Check(dense.Any(x => x.Sign == -1)); } } } }