Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparse Vector #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ encog-core-cs/bin/
encog-core-cs/obj/
encog-core-test/bin/
encog-core-test/obj/
_NCrunch_encog-core-cs/

encog-core-cs.5.1.ReSharper.user
EncogCmd/VSdoc/EncogCmd.hhc
Expand Down
25 changes: 25 additions & 0 deletions ConsoleExamples/ConsoleExamples.v2.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<ProjectConfiguration>
<BuildPriority>1000</BuildPriority>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
<AllowCodeAnalysis>false</AllowCodeAnalysis>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<DetectStackOverflow>true</DetectStackOverflow>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<UseBuildPlatform />
<ProxyProcessPath />
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
</ProjectConfiguration>
12 changes: 7 additions & 5 deletions encog-core-cs.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "encog-core-cs", "encog-core-cs\encog-core-cs.csproj", "{AC6FADF9-0904-4EBD-B22C-1C787C7E7A95}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleExamples", "ConsoleExamples\ConsoleExamples.csproj", "{A65A5878-6336-4ACF-9C40-540F8FAF2CC7}"
Expand All @@ -15,9 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProjectSection
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = encog-core-cs.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Expand Down Expand Up @@ -61,4 +60,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = encog-core-cs.vsmdi
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions encog-core-cs.v2.ncrunchsolution
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<SolutionConfiguration>
<FileVersion>1</FileVersion>
<InferProjectReferencesUsingAssemblyNames>false</InferProjectReferencesUsingAssemblyNames>
<AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>UseStaticAnalysis</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>UseStaticAnalysis</FrameworkUtilisationTypeForMSpec>
<FrameworkUtilisationTypeForMSTest>UseStaticAnalysis</FrameworkUtilisationTypeForMSTest>
<FrameworkUtilisationTypeForXUnitV2>UseStaticAnalysis</FrameworkUtilisationTypeForXUnitV2>
<EngineModes>Run all tests automatically:BFRydWU=;Run all tests manually:BUZhbHNl;Run impacted tests automatically, others manually (experimental!):CklzSW1wYWN0ZWQ=;Run pinned tests automatically, others manually:CElzUGlubmVk</EngineModes>
<MetricsExclusionList>
</MetricsExclusionList>
</SolutionConfiguration>
30 changes: 18 additions & 12 deletions encog-core-cs/ML/Data/Basic/BasicMLData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Encog.ML.Data.Basic
/// data in an array.
/// </summary>
[Serializable]
public class BasicMLData: IMLDataModifiable
public class BasicMLData : IMLDataModifiable, IHasArithimeticOperations
{
protected double[] _data;

Expand All @@ -42,12 +42,12 @@ public class BasicMLData: IMLDataModifiable
/// <param name="d">The data to construct this object with.</param>
public BasicMLData(double[] d, bool copy = true)
{
if(copy)
{
_data = new double[d.Length];
EngineArray.ArrayCopy(d, _data);
}
else _data = d;
if (copy)
{
_data = new double[d.Length];
EngineArray.ArrayCopy(d, _data);
}
else _data = d;
}


Expand Down Expand Up @@ -178,9 +178,15 @@ public IMLData Minus(IMLData o)
return result;
}

public void CopyTo(double[] target, int targetIndex, int count)
{
EngineArray.ArrayCopy(_data, 0, target, targetIndex, count);
}
}
/// <summary>
/// Copies the source data to the target array at the specified index.
/// </summary>
/// <param name="target"></param>
/// <param name="targetIndex"></param>
/// <param name="count">The maximum number of items to copy.</param>
public void CopyTo(double[] target, int targetIndex, int count)
{
EngineArray.ArrayCopy(_data, 0, target, targetIndex, count);
}
}
}
23 changes: 19 additions & 4 deletions encog-core-cs/ML/Data/Basic/BasicMLDataCentroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BasicMLDataCentroid : ICentroid<IMLData>
/// <summary>
/// The value this centroid is based on.
/// </summary>
private readonly BasicMLData _value;
private readonly IMLDataModifiable _value;

/// <summary>
/// How many items have been added to the centroid.
Expand All @@ -46,15 +46,15 @@ public class BasicMLDataCentroid : ICentroid<IMLData>
/// <param name="o">The object to base the centroid on.</param>
public BasicMLDataCentroid(IMLData o)
{
this._value = (BasicMLData)o.Clone();
this._value = (IMLDataModifiable)o.Clone();
_size = 1;
}

/// <inheritdoc/>
public void Add(IMLData d)
{
for (int i = 0; i < _value.Count; i++)
_value.Data[i] = ((_value.Data[i] * _size + d[i]) / (_size + 1));
_value[i] = ((_value[i] * _size + d[i]) / (_size + 1));
_size++;
}

Expand All @@ -70,13 +70,28 @@ public void Remove(IMLData d)
/// <inheritdoc/>
public double Distance(IMLData d)
{
IMLData diff = _value.Minus(d);
IMLData diff = Minus(_value, d);
double sum = 0.0;

for (int i = 0; i < diff.Count; i++)
sum += diff[i] * diff[i];

return Math.Sqrt(sum);
}

private static IMLData Minus(IMLDataModifiable a, IMLData b)
{
if (a.Count != b.Count)
{
throw new EncogError("Counts must match.");
}

var result = (IMLDataModifiable)Activator.CreateInstance(a.GetType(), a.Count);

for (int i = 0; i < a.Count; i++)
result[i] = a[i] - b[i];

return result;
}
}
}
5 changes: 1 addition & 4 deletions encog-core-cs/ML/Data/Basic/BasicMLDataPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using System.Text;
using Encog.Util.KMeans;
using Encog.ML.Data.Sparse;

namespace Encog.ML.Data.Basic
{
Expand Down Expand Up @@ -174,10 +175,6 @@ public double Significance
/// <inheritdoc/>
public ICentroid<IMLDataPair> CreateCentroid()
{
if (!(Input is BasicMLData))
{
throw new EncogError("The input data type of " + Input.GetType().Name + " must be BasicMLData.");
}
return new BasicMLDataPairCentroid(this);
}
}
Expand Down
15 changes: 10 additions & 5 deletions encog-core-cs/ML/Data/Basic/BasicMLDataPairCentroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BasicMLDataPairCentroid : ICentroid<IMLDataPair>
/// <summary>
/// The value the centroid is based on.
/// </summary>
private readonly BasicMLData _value;
private readonly IMLDataModifiable _value;

/// <summary>
/// How many items have been added to the centroid.
Expand All @@ -44,9 +44,15 @@ public class BasicMLDataPairCentroid : ICentroid<IMLDataPair>
/// Construct the centroid.
/// </summary>
/// <param name="o"> The pair to base the centroid on.</param>
public BasicMLDataPairCentroid(BasicMLDataPair o)
public BasicMLDataPairCentroid(IMLDataPair o)
{
_value = (BasicMLData)o.Input.Clone();
if(!(o.Input is IHasArithimeticOperations))
{
throw new ArgumentException(string.Format("It is not possible to base a data pair centroid on data structures which don't have arithmetic operations. The failed type is {0}",
o.Input.GetType().FullName));
}

_value = (IMLDataModifiable)o.Input.Clone();
_size = 1;
}

Expand All @@ -61,7 +67,7 @@ public void Remove(IMLDataPair d)
/// <inheritdoc/>
public double Distance(IMLDataPair d)
{
IMLData diff = _value.Minus(d.Input);
IMLData diff = ((IHasArithimeticOperations)_value).Minus(d.Input);
double sum = 0.0;

for (int i = 0; i < diff.Count; i++)
Expand All @@ -78,6 +84,5 @@ public void Add(IMLDataPair d)
((_value[i] * _size) + d.Input[i]) / (_size + 1);
_size++;
}

}
}
10 changes: 10 additions & 0 deletions encog-core-cs/ML/Data/IHasArithimeticOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace Encog.ML.Data
{
public interface IHasArithimeticOperations
{
IMLData Minus(IMLData o);
IMLData Plus(IMLData o);
IMLData Times(double d);
}
}
10 changes: 9 additions & 1 deletion encog-core-cs/ML/Data/IMLData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ public interface IMLData : ICloneable, ICentroidFactory<IMLData>
void CopyTo(double[] target, int targetIndex, int count);
}

public interface IMLDataModifiable: IMLData
/// <summary>
/// Used when it is possible to change the value of the underlying IMLData.
/// </summary>
public interface IMLDataModifiable : IMLData
{
/// <summary>
/// Set the specified index.
/// </summary>
/// <param name="x">The index to access.</param>
new double this[int x] { get; set; }

/// <summary>
/// Constructs an array of the data which is used to construct the object.
/// </summary>
double[] Data { get; }
}
}
Loading