Skip to content

Commit c05b37c

Browse files
committed
add vector operations
1 parent 45eb206 commit c05b37c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

MLTests/VectorMath.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MLTests
8+
{
9+
public static class VectorMath
10+
{
11+
12+
public static double[] ElementwiseMultiplication(double[] vecA, double[] vecB)
13+
{
14+
return vecA.Select((elementA, index) => elementA * vecB[index]).ToArray();
15+
}
16+
17+
public static double[] ElementwiseAddition(double[] vecA, double[] vecB)
18+
{
19+
return vecA.Select((elementA, index) => elementA + vecB[index]).ToArray();
20+
}
21+
22+
public static double ElementwiseSum(double[] vecA)
23+
{
24+
return vecA.Sum();
25+
}
26+
27+
public static double ElementwiseAverage(double[] vecA)
28+
{
29+
return vecA.Sum() / vecA.Length;
30+
}
31+
}
32+
}

MLTests/VectorMathTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
4+
namespace MLTests
5+
{
6+
public class VectorMathTests
7+
{
8+
[Test]
9+
public void TestElementWiseMultiplication()
10+
{
11+
var A = new double[] { 0, 1, 0, 1 };
12+
var B = new double[] { 1, 0, 1, 0 };
13+
var C = new double[] { 0, 1, 1, 0 };
14+
var D = new double[] { 0.5, 0, 0.5, 0 };
15+
var E = new double[] { 0, 1, -1, 0 };
16+
17+
VectorMath.ElementwiseSum(VectorMath.ElementwiseMultiplication(C, E)).Should().Be(0.0);
18+
VectorMath.ElementwiseSum(VectorMath.ElementwiseMultiplication(C, C)).Should().Be(2);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)