Skip to content

Commit

Permalink
Merge branch 'development' into portable
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Jan 19, 2014
2 parents e9e5e72 + 98b0085 commit 049fc1d
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Sources/Accord.MachineLearning/Clustering/KMeans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ public int[] Compute(double[][] data, double threshold = 1e-5, bool computeInfor
return labels;
}

/// <summary>
/// Performs the actual clustering, given a set of data points and
/// a convergence threshold. The remaining parameters must be set
/// before returning the method.
/// </summary>
///
protected virtual void PerformClustering(double[][] data, double threshold,
double[][] newCentroids, int[] count,
int[] labels, double[][] centroids)
Expand Down
1 change: 1 addition & 0 deletions Sources/Accord.Math/Accord.Math.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Environments\REnvironment.cs" />
<Compile Include="Functions\Gabor.cs" />
<Compile Include="Geometry\Circle.cs" />
<Compile Include="Geometry\DiscreteCurveEvolution.cs" />
<Compile Include="Geometry\Plane.cs" />
<Compile Include="Geometry\Point3.cs" />
<Compile Include="Kinematics\DenavitHartenbergMatrix.cs" />
Expand Down
194 changes: 194 additions & 0 deletions Sources/Accord.Math/Geometry/DiscreteCurveEvolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Accord Math Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © Diego Catalano, 2014
// diego.catalano at live.com
//
// Copyright © César Souza, 2009-2014
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// The following code has been based on the original work conducted by
// Dr. Longin Jan Latecki, with explicit permissions from the original
// author to be redistributed in the LGPL. The original work was given
// in the following publication:
//
// L.J. Latecki and R. Lakaemper; Convexity rule for shape decomposition based
// on discrete contour evolution. Computer Vision and Image Understanding 73 (3),
// 441-454, 1999.
//

namespace Accord.Math.Geometry
{
using System;
using System.Collections.Generic;
using Accord.Math;
using AForge;
using AForge.Math;
using AForge.Math.Geometry;

/// <summary>
/// Discrete Curve Evolution.
/// </summary>
///
/// <remarks>
/// <para>
/// The Discrete Curve Evolution (DCE) algorithm can be used to simplify
/// contour curves. It can preserve the outline of a shape by preserving
/// its most visually critical points.</para>
///
/// <para>
/// The implementation available in the framework has been contributed by
/// Diego Catalano, from the Catalano Framework for Java. The original work
/// has been developed by Dr. Longin Jan Latecki, and has been redistributed
/// under the LGPL with explicit permission from the original author, as long
/// as the following references are acknowledged in derived applications:</para>
///
/// <para>
/// L.J. Latecki and R. Lakaemper; Convexity rule for shape decomposition based
/// on discrete contour evolution. Computer Vision and Image Understanding 73 (3),
/// 441-454, 1999.</para>
///
/// <para>
/// References:
/// <list type="bullet">
/// <item><description><a
/// href="http://knight.temple.edu/~lakaemper/courses/cis2168_2010FALL/assignments/assig05_folder/CVIU1999.pdf">
/// L.J. Latecki and R. Lakaemper; Convexity rule for shape decomposition based
/// on discrete contour evolution. Computer Vision and Image Understanding 73 (3),
/// 441-454, 1999.</a></description></item>
/// </list>
/// </para>
/// </remarks>
///
public class DiscreteCurveEvolution : IShapeOptimizer
{

private int vertices = 20;

/// <summary>
/// Gets or sets the number of vertices.
/// </summary>
///
public int NumberOfVertices
{
get { return vertices; }
set
{
if (value <= 0 || value > 3)
throw new ArgumentOutOfRangeException("value",
"Number of vertices should be between 0 and 3.");

vertices = value;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="DiscreteCurveEvolution"/> class.
/// </summary>
///
public DiscreteCurveEvolution() { }

/// <summary>
/// Initializes a new instance of the <see cref="DiscreteCurveEvolution"/> class.
/// </summary>
///
/// <param name="vertices">Number of vertices.</param>
///
public DiscreteCurveEvolution(int vertices)
{
NumberOfVertices = vertices;
}

/// <summary>
/// Optimize specified shape.
/// </summary>
///
/// <param name="shape">Shape to be optimized.</param>
///
/// <returns>
/// Returns final optimized shape, which may have reduced amount of points.
/// </returns>
///
public List<IntPoint> OptimizeShape(List<IntPoint> shape)
{
if (vertices > shape.Count)
throw new ArgumentException("Number of points left must be higher than number of the shape.");

var complex = new List<Complex>();
for (int i = 0; i < shape.Count; i++)
complex.Add(new Complex(shape[i].X, shape[i].Y));

for (int i = 0; i < shape.Count - vertices; i++)
{
double[] winkelmaass = winkel(complex);

int index = 0;
Matrix.Min(winkelmaass, out index);

complex.RemoveAt(index);
}

var newShape = new List<IntPoint>(complex.Count);

for (int i = 0; i < complex.Count; i++)
newShape.Add(new IntPoint((int)complex[i].Re, (int)complex[i].Im));

return newShape;
}

private static double[] winkel(List<Complex> z)
{
int n = z.Count;
double max = -double.MaxValue;

double[] his = new double[n];
for (int j = 1; j < n - 1; j++)
{
Complex c = z[j - 1] - z[j + 1];

double lm = c.Magnitude;

c = z[j] - z[j + 1];

double lr = c.Magnitude;

c = z[j - 1] - z[j];

double ll = c.Magnitude;

double alpha = Math.Acos((lr * lr + ll * ll - lm * lm) / (2 * lr * ll));

// turning angle (0-180)
double a = 180 - alpha * 180 / Math.PI;

// the original relevance measure
his[j] = a * lr * ll / (lr + ll);

if (his[j] > max)
max = his[j];

}

his[0] = max;
his[n - 1] = max;

return his;
}

}
}
16 changes: 16 additions & 0 deletions Sources/Accord.Math/Matrix/Matrix.Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,22 @@ public static int[] Vector(int a, int b, int increment = 1)

return list.ToArray();
}

/// <summary>
/// Creates a vector with the given dimension and starting values.
/// </summary>
///
public static double[] Vector(double a, double b, int points)
{
double[] list = new double[points];

double increment = (b - a) / points;

for (int i = 0; i < list.Length; i++)
list[i] = increment * i;

return list;
}
#endregion

#region Special vectors
Expand Down
35 changes: 34 additions & 1 deletion Sources/Accord.Math/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,40 @@ public static void StableSort<T>(this T[] values, out int[] order)
for (int i = 0; i < keys.Length; i++)
order[i] = keys[i].Key;
}
}

/// <summary>
/// Interpolates data using a piece-wise linear function.
/// </summary>
///
/// <param name="value">The value to be calculated.</param>
/// <param name="x">The input data points <c>x</c>. Those values need to be sorted.</param>
/// <param name="y">The output data points <c>y</c>.</param>
/// <param name="lower">
/// The value to be returned for values before the first point in <paramref name="x"/>.</param>
/// <param name="upper">
/// The value to be returned for values after the last point in <paramref name="x"/>.</param>
///
/// <returns>Computes the output for f(value) by using a piecewise linear
/// interpolation of the data points <paramref name="x"/> and <paramref name="y"/>.</returns>
///
public static double Interpolate1D(double value, double[] x, double[] y, double lower, double upper)
{
for (int i = 0; i < x.Length; i++)
{
if (value < x[i])
{
if (i == 0)
return lower;

int start = i - 1;
int next = i;

double m = (value - x[start]) / (x[next] - x[start]);
return y[start] + (y[next] - y[start]) * m;
}
}

return upper;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ public override string ToString()
return ToString(null, System.Globalization.CultureInfo.CurrentCulture);
}

/// <summary>
/// Creates a new linear regression directly from data points.
/// </summary>
///
/// <param name="x">The input vectors <c>x</c>.</param>
/// <param name="y">The output vectors <c>y</c>.</param>
///
/// <returns>A linear regression f(x) that most approximates y.</returns>
///
public static MultipleLinearRegression FromData(double[][] x, double[] y)
{
var regression = new MultipleLinearRegression(x[0].Length);

regression.Regress(x, y);

return regression;
}

#region ILinearRegression Members
double[] ILinearRegression.Compute(double[] inputs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,24 @@ public double[][] Compute(double[][] input)
return output;
}

/// <summary>
/// Creates a new linear regression directly from data points.
/// </summary>
///
/// <param name="x">The input vectors <c>x</c>.</param>
/// <param name="y">The output vectors <c>y</c>.</param>
///
/// <returns>A linear regression f(x) that most approximates y.</returns>
///
public static MultivariateLinearRegression FromData(double[][] x, double[][] y)
{
var regression = new MultivariateLinearRegression(x[0].Length, y[0].Length);

regression.Regress(x, y);

return regression;
}


#region ILinearRegression Members
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ public string ToString(string format, IFormatProvider formatProvider)
return sb.ToString();
}

/// <summary>
/// Creates a new polynomial regression directly from data points.
/// </summary>
///
/// <param name="degree">The polynomial degree to use.</param>
/// <param name="x">The input vectors <c>x</c>.</param>
/// <param name="y">The output vectors <c>y</c>.</param>
///
/// <returns>A polynomial regression f(x) that most approximates y.</returns>
///
public static PolynomialRegression FromData(int degree, double[] x, double[] y)
{
PolynomialRegression regression = new PolynomialRegression(degree);

regression.Regress(x, y);

return regression;
}

#region ILinearRegression Members
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ public string ToString(string format, IFormatProvider formatProvider)
return String.Format("y(x) = {0}x + {1}", a, b);
}

/// <summary>
/// Creates a new linear regression directly from data points.
/// </summary>
///
/// <param name="x">The input vectors <c>x</c>.</param>
/// <param name="y">The output vectors <c>y</c>.</param>
///
/// <returns>A linear regression f(x) that most approximates y.</returns>
///
public static SimpleLinearRegression FromData(double[] x, double[] y)
{
SimpleLinearRegression regression = new SimpleLinearRegression();

regression.Regress(x, y);

return regression;
}


#region ILinearRegression Members
/// <summary>
Expand Down
Loading

0 comments on commit 049fc1d

Please sign in to comment.