|
| 1 | +using System; |
| 2 | +using FluentAssertions; |
| 3 | +using NUnit.Framework; |
| 4 | +using Utilities.Extensions; |
| 5 | + |
| 6 | +namespace Utilities.Tests.Extensions |
| 7 | +{ |
| 8 | + public class VectorExtensionsTests |
| 9 | + { |
| 10 | + [Test] |
| 11 | + public void Copy_ShouldReturnCopyOfVector() |
| 12 | + { |
| 13 | + var vector = new double[] { 0, 1, 2, 3 }; |
| 14 | + |
| 15 | + var vectorCopy = vector.Copy(); |
| 16 | + |
| 17 | + vectorCopy.Should().BeEquivalentTo(vector); |
| 18 | + vectorCopy.Should().NotBeSameAs(vector); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void OuterProduct_ShouldCalculateOuterProduct() |
| 23 | + { |
| 24 | + var lhs = new double[] { -2, -1, 0, 1, 2 }; |
| 25 | + var rhs = new double[] { 1, 2, 3 }; |
| 26 | + |
| 27 | + var result = new double[,] |
| 28 | + { |
| 29 | + { -2, -4, -6 }, |
| 30 | + { -1, -2, -3 }, |
| 31 | + { 0, 0, 0 }, |
| 32 | + { 1, 2, 3 }, |
| 33 | + { 2, 4, 6 }, |
| 34 | + }; |
| 35 | + |
| 36 | + var actualResult = lhs.OuterProduct(rhs); |
| 37 | + |
| 38 | + actualResult.Should().BeEquivalentTo(result); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void Dot_ShouldThrowArgumentException_WhenDimensionsDoNotMatch() |
| 43 | + { |
| 44 | + var lhs = new double[] { 1, 2, 3 }; |
| 45 | + var rhs = new double[] { 1, 2, 3, 4 }; |
| 46 | + |
| 47 | + var func = () => lhs.Dot(rhs); |
| 48 | + |
| 49 | + func.Should().Throw<ArgumentException>() |
| 50 | + .WithMessage("Dot product arguments must have same dimension"); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void Dot_ShouldCalculateDotProduct() |
| 55 | + { |
| 56 | + var lhs = new double[] { 1, 2, 3 }; |
| 57 | + var rhs = new double[] { 4, 5, 6 }; |
| 58 | + |
| 59 | + var actualResult = lhs.Dot(rhs); |
| 60 | + |
| 61 | + actualResult.Should().Be(32); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void Magnitude_ShouldCalculateMagnitude() |
| 66 | + { |
| 67 | + var vector = new double[] { -3, 4 }; |
| 68 | + |
| 69 | + var actualResult = vector.Magnitude(); |
| 70 | + |
| 71 | + actualResult.Should().BeApproximately(5.0, 0.0001); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void Scale_ShouldCalculateScale() |
| 76 | + { |
| 77 | + var vector = new double[] { -1, 0, 1 }; |
| 78 | + var factor = 2; |
| 79 | + |
| 80 | + var result = new double[] { -2, 0, 2 }; |
| 81 | + |
| 82 | + var actualResult = vector.Scale(factor); |
| 83 | + |
| 84 | + actualResult.Should().BeEquivalentTo(result); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void ToColumnVector_ShouldReturnColumnVector() |
| 89 | + { |
| 90 | + var vector = new double[] { 1, 2, 3, 4 }; |
| 91 | + var result = new double[,] { { 1 }, { 2 }, { 3 }, { 4 } }; |
| 92 | + |
| 93 | + var actualResult = vector.ToColumnVector(); |
| 94 | + |
| 95 | + actualResult.Should().BeEquivalentTo(result); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void ToRowVector_ShouldThrowInvalidOperationException_WhenSourceIsNotAColumnVector() |
| 100 | + { |
| 101 | + var source = new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; |
| 102 | + |
| 103 | + var func = () => source.ToRowVector(); |
| 104 | + |
| 105 | + func.Should().Throw<InvalidOperationException>() |
| 106 | + .WithMessage("The column vector should have only 1 element in width."); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void ToRowVector_ShouldReturnRowVector() |
| 111 | + { |
| 112 | + var source = new double[,] { { 1 }, { 2 }, { 3 }, { 4 } }; |
| 113 | + var result = new double[] { 1, 2, 3, 4 }; |
| 114 | + |
| 115 | + var actualResult = source.ToRowVector(); |
| 116 | + |
| 117 | + actualResult.Should().BeEquivalentTo(result); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public void ToDiagonalMatrix_ShouldReturnDiagonalMatrix() |
| 122 | + { |
| 123 | + var source = new double[] { 1, 2, 3, 4 }; |
| 124 | + var result = new double[,] |
| 125 | + { |
| 126 | + { 1, 0, 0, 0 }, |
| 127 | + { 0, 2, 0, 0 }, |
| 128 | + { 0, 0, 3, 0 }, |
| 129 | + { 0, 0, 0, 4 }, |
| 130 | + }; |
| 131 | + |
| 132 | + var actualResult = source.ToDiagonalMatrix(); |
| 133 | + |
| 134 | + actualResult.Should().BeEquivalentTo(result); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments