Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit fa73427

Browse files
committedFeb 3, 2019
Basic Matrix class. Some minor functions might still be missing, but the class is usable
1 parent 5f46d05 commit fa73427

16 files changed

+787
-20
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,5 @@ paket-files/
258258

259259
# Python Tools for Visual Studio (PTVS)
260260
__pycache__/
261-
*.pyc
261+
*.pyc
262+
/MachineLearningExamples

‎MachineLearning.sln

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.28307.271
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MachineLearning", "MachineLearning\MachineLearning.csproj", "{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MachineLearningTests", "MachineLearningTests\MachineLearningTests.csproj", "{05EE43D7-F097-4221-93F0-9E58D462867C}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MachineLearningPlayground", "MachineLearningExamples\MachineLearningPlayground.csproj", "{00F7414E-8FC3-4503-B1F5-EAA4B4A6154E}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +19,14 @@ Global
1519
{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{05EE43D7-F097-4221-93F0-9E58D462867C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{05EE43D7-F097-4221-93F0-9E58D462867C}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{05EE43D7-F097-4221-93F0-9E58D462867C}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{05EE43D7-F097-4221-93F0-9E58D462867C}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{00F7414E-8FC3-4503-B1F5-EAA4B4A6154E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{00F7414E-8FC3-4503-B1F5-EAA4B4A6154E}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{00F7414E-8FC3-4503-B1F5-EAA4B4A6154E}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{00F7414E-8FC3-4503-B1F5-EAA4B4A6154E}.Release|Any CPU.Build.0 = Release|Any CPU
1830
EndGlobalSection
1931
GlobalSection(SolutionProperties) = preSolution
2032
HideSolutionNode = FALSE

‎MachineLearning/Exceptions.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class DotProductNotPossibleException : Exception
10+
{
11+
public DotProductNotPossibleException(){ }
12+
13+
public DotProductNotPossibleException(string message) : base(message) { }
14+
15+
public DotProductNotPossibleException(string message, Exception inner) : base(message, inner) { }
16+
}
17+
18+
public class MatrixAdditionNotPossibleException : Exception
19+
{
20+
public MatrixAdditionNotPossibleException() { }
21+
22+
public MatrixAdditionNotPossibleException(string message) : base(message) { }
23+
24+
public MatrixAdditionNotPossibleException(string message, Exception inner) : base(message) { }
25+
}
26+
}

‎MachineLearning/Class1.cs ‎MachineLearning/Globals.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace MachineLearning
88
{
9-
public class Class1
9+
class Globals
1010
{
11+
public static Graph DefaultGraph;
1112
}
1213
}

‎MachineLearning/Graph.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class Graph
10+
{
11+
public ArrayList Operations { get; set; }
12+
public ArrayList Variables { get; set; }
13+
public ArrayList Placeholders { get; set; }
14+
15+
public Graph()
16+
{
17+
Operations = new ArrayList();
18+
Placeholders = new ArrayList();
19+
Variables = new ArrayList();
20+
}
21+
22+
public void SetAsDefault()
23+
{
24+
Globals.DefaultGraph = this;
25+
}
26+
}
27+
}

‎MachineLearning/MachineLearning.csproj

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>6a02e4f2-b745-4699-984e-0a1a0b2b6ac0</ProjectGuid>
7+
<ProjectGuid>{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>MachineLearning</RootNamespace>
@@ -31,24 +31,19 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34-
<Reference Include="System"/>
35-
36-
<Reference Include="System.Core"/>
37-
<Reference Include="System.Xml.Linq"/>
38-
<Reference Include="System.Data.DataSetExtensions"/>
39-
40-
41-
<Reference Include="Microsoft.CSharp"/>
42-
43-
<Reference Include="System.Data"/>
44-
45-
<Reference Include="System.Net.Http"/>
46-
47-
<Reference Include="System.Xml"/>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
4842
</ItemGroup>
4943
<ItemGroup>
50-
<Compile Include="Class1.cs" />
44+
<Compile Include="Exceptions.cs" />
45+
<Compile Include="Matrix.cs" />
5146
<Compile Include="Properties\AssemblyInfo.cs" />
5247
</ItemGroup>
5348
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54-
</Project>
49+
</Project>

‎MachineLearning/Matrix.cs

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System;
2+
using System.Collections;
3+
4+
namespace MachineLearning
5+
{
6+
public class Matrix
7+
{
8+
private double[,] _matrix;
9+
10+
public int Rows
11+
{
12+
get { return _matrix.GetLength(0); }
13+
}
14+
public int Columns
15+
{
16+
get { return _matrix.GetLength(1); }
17+
}
18+
19+
public Matrix(double[,] pMatrix)
20+
{
21+
_matrix = pMatrix;
22+
}
23+
24+
public Matrix(int pRows, int pColumns)
25+
{
26+
_matrix = new double[pRows, pColumns];
27+
for(int row = 0; row < Rows; row++)
28+
{
29+
for(int col = 0; col < Columns; col++)
30+
{
31+
_matrix[row, col] = 0.0;
32+
}
33+
}
34+
}
35+
36+
37+
public static Matrix operator +(Matrix m1, Matrix m2)
38+
{
39+
if(m1.Rows != m2.Rows || m1.Columns != m2.Columns)
40+
{
41+
throw (new MatrixAdditionNotPossibleException("Matrix dimensions are not indentical: Matrix1: "
42+
+ m1.Rows + "x" + m1.Columns + ". Matrix2: "
43+
+ m2.Rows + "x" + m2.Columns + "."));
44+
}
45+
Matrix result = new Matrix(m1.Rows, m1.Columns);
46+
for(int row = 0; row < m1.Rows; row++)
47+
{
48+
for(int col = 0; col < m1.Columns; col++)
49+
{
50+
result[row, col] = m1[row, col] + m2[row, col];
51+
}
52+
}
53+
return result;
54+
}
55+
56+
public static Matrix operator +(Matrix m1, double operand)
57+
{
58+
Matrix result = new Matrix(m1.Rows, m1.Columns);
59+
for (int row = 0; row < m1.Rows; row++)
60+
{
61+
for (int col = 0; col < m1.Columns; col++)
62+
{
63+
result[row, col] = m1[row, col] + operand;
64+
}
65+
}
66+
return result;
67+
}
68+
69+
public static Matrix operator -(Matrix m1, Matrix m2)
70+
{
71+
return m1 + (m2 * -1);
72+
}
73+
74+
public static Matrix operator -(Matrix m1, double operand)
75+
{
76+
return m1 + (operand * -1);
77+
}
78+
79+
public static Matrix operator *(Matrix m1, Matrix m2)
80+
{
81+
if(m1.Columns != m2.Rows)
82+
{
83+
throw (new DotProductNotPossibleException("Column Count of Matrix 1 ("
84+
+ m1.Rows + ") does not match up with Row Count of Matrix 2 ("
85+
+ m2.Columns + ")."));
86+
}
87+
Matrix result = new Matrix(m1.Rows, m2.Columns);
88+
for(int m1Row = 0; m1Row < m1.Rows; m1Row++)
89+
{
90+
for(int m2Col = 0; m2Col < m2.Columns; m2Col++)
91+
{
92+
double sum = 0;
93+
for(int i = 0; i < m1.Columns; i++)
94+
{
95+
sum += m2[i, m2Col] * m1[m1Row, i];
96+
}
97+
result[m1Row, m2Col] = sum;
98+
}
99+
}
100+
return result;
101+
}
102+
103+
public static Matrix operator *(Matrix m1, double scalar)
104+
{
105+
Matrix result = new Matrix(m1.Rows, m1.Columns);
106+
for(int row = 0; row < m1.Rows; row++)
107+
{
108+
for(int col = 0; col < m1.Columns; col++)
109+
{
110+
result[row, col] = m1[row,col] * scalar;
111+
}
112+
}
113+
return result;
114+
}
115+
116+
public static Matrix operator /(Matrix m1, double scalar)
117+
{
118+
return m1 * (1 / scalar);
119+
}
120+
121+
public Matrix Transpose()
122+
{
123+
Matrix transposed = new Matrix(Columns, Rows);
124+
for(int row = 0; row < Rows; row++)
125+
{
126+
for(int col = 0; col < Columns; col++)
127+
{
128+
transposed[col, row] = this[row, col];
129+
}
130+
}
131+
return transposed;
132+
}
133+
134+
public bool Equals(Matrix m1)
135+
{
136+
if (m1.Rows != Rows) return false;
137+
if (m1.Columns != Columns) return false;
138+
for(int row = 0; row < Rows; row++)
139+
{
140+
for(int col = 0; col < Columns; col++)
141+
{
142+
if (this[row, col] != m1[row, col]) return false;
143+
}
144+
}
145+
return true;
146+
}
147+
148+
public static Matrix Identitiy(int size)
149+
{
150+
Matrix identity = new Matrix(size, size);
151+
for(int row = 0; row < size; row++)
152+
{
153+
for(int col = 0; col < size; col++)
154+
{
155+
if(row == col)
156+
{
157+
identity[row, col] = 1.0;
158+
}
159+
}
160+
}
161+
return identity;
162+
}
163+
164+
public double this[int i, int j]
165+
{
166+
get { return _matrix[i, j]; }
167+
set { _matrix[i, j] = value; }
168+
}
169+
}
170+
}

‎MachineLearning/Operations.cs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class Operation
10+
{
11+
public ArrayList InputNodes { get; }
12+
public ArrayList OutputNodes { get; set; }
13+
public ArrayList Inputs { get; set; }
14+
public object Output { get; set; }
15+
16+
public Operation(ArrayList pInputNodes)
17+
{
18+
InputNodes = pInputNodes;
19+
OutputNodes = new ArrayList();
20+
21+
foreach(object node in InputNodes)
22+
{
23+
if(node is Variable)
24+
{
25+
((Variable) node).OutputNodes.Add(this);
26+
}else if(node is Placeholder)
27+
{
28+
((Placeholder)node).OutputNodes.Add(this);
29+
}else if(node is Operation)
30+
{
31+
((Operation)node).OutputNodes.Add(this);
32+
}
33+
34+
}
35+
36+
Globals.DefaultGraph.Operations.Add(this);
37+
}
38+
39+
public object Compute(params Object[] objects) { return null; }
40+
}
41+
42+
public class Add : Operation
43+
{
44+
45+
public Add(object x, object y) : base(new ArrayList() { x, y, })
46+
{
47+
48+
}
49+
50+
public new double Compute(params Object[] objects)
51+
{
52+
Inputs = new ArrayList() { objects[0], objects[1] };
53+
return (double) objects[0] + (double) objects[1];
54+
}
55+
}
56+
57+
public class Multiply : Operation
58+
{
59+
public Multiply(object x, object y) : base(new ArrayList() { x, y, })
60+
{
61+
62+
}
63+
64+
public new double Compute(params Object[] objects)
65+
{
66+
Inputs = new ArrayList() { objects[0], objects[1] };
67+
return (double) objects[0] * (double) objects[1];
68+
}
69+
}
70+
71+
72+
public class MatMul : Operation
73+
{
74+
public MatMul(Matrix x, Matrix y) : base(new ArrayList() { x, y, })
75+
{
76+
77+
}
78+
79+
public new Matrix Compute(params Object[] objects)
80+
{
81+
Inputs = new ArrayList() { objects[0], objects[1] };
82+
return (Matrix) objects[0] * (Matrix) objects[1];
83+
}
84+
}
85+
}

‎MachineLearning/Placeholder.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class Placeholder
10+
{
11+
public ArrayList OutputNodes { get; set; }
12+
public object Output { get; set; }
13+
public Placeholder()
14+
{
15+
OutputNodes = new ArrayList();
16+
17+
Globals.DefaultGraph.Placeholders.Add(this);
18+
}
19+
}
20+
}

‎MachineLearning/Session.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace MachineLearning
9+
{
10+
public class Session
11+
{
12+
public object Run(Operation operation, Dictionary<Placeholder, object> feed_dict)
13+
{
14+
ArrayList nodesPostorder = new Traverser(operation).Traverse();
15+
foreach(object node in nodesPostorder)
16+
{
17+
if(node is Placeholder)
18+
{
19+
((Placeholder)node).Output = feed_dict[(Placeholder) node];
20+
}else if (node is Variable)
21+
{
22+
((Variable)node).Output = ((Variable)node).Value;
23+
}else if (node is Operation)
24+
{
25+
foreach(object inputNode in ((Operation)node).InputNodes)
26+
{
27+
if(inputNode is Variable)
28+
((Operation)node).Inputs.Add(((Variable) inputNode).Output);
29+
else if(inputNode is Placeholder)
30+
((Operation)node).Inputs.Add(((Placeholder)inputNode).Output);
31+
32+
((Operation)node).Output = ((Operation)node).Compute(((Operation) node).InputNodes);
33+
}
34+
}
35+
}
36+
return operation.Output;
37+
}
38+
}
39+
}

‎MachineLearning/Traverser.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class Traverser
10+
{
11+
private readonly Operation startOperation;
12+
13+
public Traverser(Operation pOperation)
14+
{
15+
startOperation = pOperation;
16+
}
17+
18+
public ArrayList Traverse()
19+
{
20+
return Recurse(startOperation);
21+
}
22+
23+
public ArrayList Recurse(object node)
24+
{
25+
ArrayList nodesPostorder = new ArrayList();
26+
if(node is Operation)
27+
{
28+
foreach(object input_node in ((Operation)node).InputNodes)
29+
{
30+
nodesPostorder.Add(Recurse(input_node));
31+
}
32+
}
33+
nodesPostorder.Add(node);
34+
return nodesPostorder;
35+
}
36+
}
37+
}

‎MachineLearning/Variable.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MachineLearning
8+
{
9+
public class Variable
10+
{
11+
public ArrayList OutputNodes { get; set; }
12+
public object Value { get; set; }
13+
public object Output { get; set; }
14+
15+
public Variable(object pInitialValue)
16+
{
17+
Value = pInitialValue;
18+
OutputNodes = new ArrayList();
19+
20+
Globals.DefaultGraph.Variables.Add(this);
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{05EE43D7-F097-4221-93F0-9E58D462867C}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>MachineLearningTests</RootNamespace>
11+
<AssemblyName>MachineLearningTests</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
16+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
17+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
18+
<IsCodedUITest>False</IsCodedUITest>
19+
<TestProjectType>UnitTest</TestProjectType>
20+
<TargetFrameworkProfile />
21+
<NuGetPackageImportStamp>
22+
</NuGetPackageImportStamp>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
25+
<DebugSymbols>true</DebugSymbols>
26+
<DebugType>full</DebugType>
27+
<Optimize>false</Optimize>
28+
<OutputPath>bin\Debug\</OutputPath>
29+
<DefineConstants>DEBUG;TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
34+
<DebugType>pdbonly</DebugType>
35+
<Optimize>true</Optimize>
36+
<OutputPath>bin\Release\</OutputPath>
37+
<DefineConstants>TRACE</DefineConstants>
38+
<ErrorReport>prompt</ErrorReport>
39+
<WarningLevel>4</WarningLevel>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
43+
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
46+
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
</ItemGroup>
50+
<Choose>
51+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
52+
<ItemGroup>
53+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
54+
</ItemGroup>
55+
</When>
56+
<Otherwise />
57+
</Choose>
58+
<ItemGroup>
59+
<Compile Include="MatrixTests.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="packages.config" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<ProjectReference Include="..\MachineLearning\MachineLearning.csproj">
67+
<Project>{6A02E4F2-B745-4699-984E-0A1A0B2B6AC0}</Project>
68+
<Name>MachineLearning</Name>
69+
</ProjectReference>
70+
</ItemGroup>
71+
<Choose>
72+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
73+
<ItemGroup>
74+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
75+
<Private>False</Private>
76+
</Reference>
77+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
78+
<Private>False</Private>
79+
</Reference>
80+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
81+
<Private>False</Private>
82+
</Reference>
83+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
84+
<Private>False</Private>
85+
</Reference>
86+
</ItemGroup>
87+
</When>
88+
</Choose>
89+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
90+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
91+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
92+
<PropertyGroup>
93+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
94+
</PropertyGroup>
95+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
96+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
97+
</Target>
98+
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
99+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
100+
Other similar extension points exist, see Microsoft.Common.targets.
101+
<Target Name="BeforeBuild">
102+
</Target>
103+
<Target Name="AfterBuild">
104+
</Target>
105+
-->
106+
</Project>

‎MachineLearningTests/MatrixTests.cs

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using MachineLearning;
3+
using System;
4+
using System.Collections;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace MachineLearning.Tests
10+
{
11+
[TestClass()]
12+
public class MatrixTests
13+
{
14+
[TestMethod()]
15+
public void MatrixInitTest()
16+
{
17+
Matrix m1 = new Matrix(new double[,]
18+
{
19+
{1.0, 2.0, 3.0},
20+
{4.0, 5.0, 6.0},
21+
});
22+
Matrix m2 = new Matrix(new double[,]
23+
{
24+
{ 7.0, 8.0},
25+
{ 9.0, 10.0},
26+
{11.0, 12.0},
27+
});
28+
29+
Matrix m3 = new Matrix(5, 5);
30+
Assert.AreEqual(m3[1,2], 0.0);
31+
32+
Assert.AreEqual(m1.Rows, 2);
33+
Assert.AreEqual(m1.Columns, 3);
34+
Assert.AreEqual(m1[0,1], 2.0);
35+
}
36+
37+
[TestMethod()]
38+
public void MatrixMatmulTest()
39+
{
40+
Matrix m1 = new Matrix(new double[,]
41+
{
42+
{1.0, 2.0, 3.0},
43+
{4.0, 5.0, 6.0},
44+
});
45+
Matrix m2 = new Matrix(new double[,]
46+
{
47+
{ 7.0, 8.0},
48+
{ 9.0, 10.0},
49+
{11.0, 12.0},
50+
});
51+
Matrix m3 = new Matrix(new double[,]
52+
{
53+
{ 7.0, 8.0},
54+
{ 9.0, 10.0},
55+
{11.0, 12.0},
56+
{13.0, 14.0},
57+
});
58+
59+
Matrix dotProduct = m1 * m2;
60+
Matrix dotProductActual = new Matrix(new double[,]
61+
{
62+
{ 58.0, 64.0},
63+
{139.0, 154.0},
64+
});
65+
Assert.IsTrue(dotProduct.Equals(dotProductActual));
66+
Assert.ThrowsException<DotProductNotPossibleException>(delegate () { Matrix _ = m1 * m3; });
67+
}
68+
69+
[TestMethod()]
70+
public void MatrixScalarMulTest()
71+
{
72+
Matrix m1 = new Matrix(new double[,]
73+
{
74+
{1.0, 2.0, 3.0},
75+
{4.0, 5.0, 6.0},
76+
});
77+
78+
Matrix scalarProduct = m1 * 2;
79+
80+
Matrix scalarProductActual = new Matrix(new double[,]
81+
{
82+
{2.0, 4.0, 6.0},
83+
{8.0, 10.0, 12.0},
84+
});
85+
86+
Assert.IsTrue(scalarProduct.Equals(scalarProductActual));
87+
}
88+
89+
[TestMethod()]
90+
public void MatrixScalarDivTest()
91+
{
92+
Matrix m1 = new Matrix(new double[,]
93+
{
94+
{1.0, 2.0, 3.0},
95+
{4.0, 5.0, 6.0},
96+
});
97+
98+
Matrix scalarDivision = m1 / 2;
99+
100+
Matrix scalarDivisionActual = new Matrix(new double[,]
101+
{
102+
{0.5, 1.0, 1.5},
103+
{2.0, 2.5, 3.0},
104+
});
105+
106+
Assert.IsTrue(scalarDivision.Equals(scalarDivisionActual));
107+
}
108+
109+
[TestMethod()]
110+
public void MatrixAdditionTest()
111+
{
112+
Matrix m1 = new Matrix(new double[,]
113+
{
114+
{1.0, 2.0 },
115+
{4.0, 5.0 },
116+
});
117+
Matrix m2 = new Matrix(new double[,]
118+
{
119+
{ 7.0, 8.0},
120+
{ 9.0, 10.0},
121+
});
122+
123+
Matrix m3 = new Matrix(new double[,]
124+
{
125+
{ 7.0, 8.0, 5.0},
126+
{ 9.0, 10.0, 6.0},
127+
});
128+
129+
Matrix matrixAddition = m1 + m2;
130+
Matrix matrixAdditionActual = new Matrix(new double[,]
131+
{
132+
{ 8.0, 10.0},
133+
{13.0, 15.0},
134+
});
135+
136+
Assert.IsTrue(matrixAddition.Equals(matrixAdditionActual));
137+
138+
Assert.ThrowsException<MatrixAdditionNotPossibleException>(delegate() { Matrix _ = m1 + m3; });
139+
}
140+
141+
[TestMethod()]
142+
public void MatrixTranspositionTest()
143+
{
144+
Matrix m1 = new Matrix(new double[,]
145+
{
146+
{1.0, 2.0},
147+
{3.0, 4.0},
148+
{5.0, 6.0},
149+
});
150+
151+
Matrix transposed = m1.Transpose();
152+
Matrix transposedActual = new Matrix(new double[,]
153+
{
154+
{1.0, 3.0, 5.0 },
155+
{2.0, 4.0, 6.0 },
156+
});
157+
Assert.IsTrue(transposed.Equals(transposedActual));
158+
}
159+
160+
[TestMethod()]
161+
public void MatrixEqualityTest()
162+
{
163+
Matrix m1 = new Matrix(new double[,]
164+
{
165+
{1.0, 2.0, 3.0 },
166+
{4.0, 5.0, 6.0 }
167+
});
168+
Matrix m2 = new Matrix(new double[,]
169+
{
170+
{1.0, 2.0, 3.0 },
171+
{4.0, 5.0, 6.0 }
172+
});
173+
Matrix m3 = new Matrix(new double[,]
174+
{
175+
{1.0, 2.0},
176+
{3.0, 4.0},
177+
{5.0, 6.0},
178+
});
179+
180+
Assert.IsFalse(m2.Equals(m3));
181+
Assert.IsTrue(m1.Equals(m2));
182+
}
183+
}
184+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("MachineLearningTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("MachineLearningTests")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("05ee43d7-f097-4221-93f0-9e58d462867c")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

‎MachineLearningTests/packages.config

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net461" />
4+
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net461" />
5+
</packages>

0 commit comments

Comments
 (0)
This repository has been archived.