Skip to content

Commit ba7d63e

Browse files
committed
learn matrix
1 parent 14fc172 commit ba7d63e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

MLTests/MatrixTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using FluentAssertions;
2+
using Numpy;
3+
using NUnit.Framework;
4+
5+
namespace MLTests
6+
{
7+
public class MatrixTests
8+
{
9+
[Test]
10+
public void ShouldMatrixBe()
11+
{
12+
var vectorA = np.array(new double[] { 0, 1, 2, 3 });
13+
14+
var twoOnTwoMatrix = np.zeros(2, 2);
15+
var twoOnTwoData = twoOnTwoMatrix.GetData<double>();
16+
twoOnTwoData.Length.Should().Be(4);
17+
twoOnTwoData.Sum().Should().Be(0);
18+
19+
var randomMatrix = np.random.rand(2, 5);
20+
var randomData = randomMatrix.GetData<double>();
21+
randomData.Length.Should().Be(10);
22+
randomData.Sum().Should().NotBe(0);
23+
24+
var weightsA = np.array(new double[,] {
25+
{1, 2},
26+
{3, 4},
27+
});
28+
var dataWeights = weightsA.GetData<double>();
29+
dataWeights.SequenceEqual(new double[] { 1, 2, 3, 4 }).Should().Be(true);
30+
}
31+
32+
[Test]
33+
public void ShouldMatrixMultiple()
34+
{
35+
var vectorA = np.array(new double[] { 0, 1, 2, 3 });
36+
var vectorB = np.array(new double[] { 4, 5, 6, 7 });
37+
var vectorC = np.array(new double[,] { {0, 1, 2, 3 }, {4, 5, 6, 7} });
38+
39+
var mulVectorA = vectorA * 0.1;
40+
var mulADouble = mulVectorA.GetData<double>().Select(e => Math.Round(e, 2)).ToArray();
41+
mulADouble.SequenceEqual(new double[] { 0, 0.1, 0.2, 0.3 }).Should().BeTrue();
42+
43+
var mulVectorB = vectorA * vectorB;
44+
mulVectorB.GetData<double>().SequenceEqual(new double[] { 0, 5, 12, 21 }).Should().BeTrue();
45+
46+
var mulAC = vectorA * vectorC;
47+
var mulACDouble = mulAC.GetData<double>().Select(e => Math.Round(e, 2)).ToArray();
48+
mulACDouble.SequenceEqual(new double[] {0, 1, 4, 9, 0, 5, 12, 21}).Should().BeTrue();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)