Click here to Skip to main content
15,886,137 members
Articles / Multimedia / GDI+

C# Application to Create and Recognize Mouse Gestures (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (39 votes)
17 Mar 2008CPOL5 min read 221.4K   8.1K   144  
This program can create and recognize mouse gestures.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace NeuralNetworks.ActivationFunctions
{
	public class SigmoidFunction : IActivationFunction
	{
		private double m_Alpha;
		public double Alpha
		{
			get { return m_Alpha; }
			set 
			{
				if (value < 0)
					throw new ArgumentException("Alpha value must be strictly positive", "alpha");

				m_Alpha = value; 
			}
		}

		public SigmoidFunction()
			: this(1)
		{
		}

		public SigmoidFunction(double alpha)
		{
			if (alpha < 0)
				throw new ArgumentException("Alpha value must be strictly positive", "alpha");

			m_Alpha = alpha;
		}

		//Values are mapped in the interval [0,1]
		public double Compute(double value)
		{
			return (1 / (1 + Math.Exp(-m_Alpha * value)));
		}

		public double ComputeDerivateByOutput(double y)
		{
			return m_Alpha * y * (1 - y);
		}

		public double ComputeDerivateByInput(double x)
		{
			double output = Compute(x);
			return m_Alpha * output * (1 - output);
		}

		public override string ToString()
		{
			return "SigmoidFunction [" + m_Alpha.ToString() + "]";
		}

		public override bool Equals(object obj)
		{
			if (this == obj)
				return true;
			else
			{
				SigmoidFunction f = obj as SigmoidFunction;

				if (f == null)
					return false;
				else
				{
					if (m_Alpha != f.m_Alpha)
						return false;
					else
						return true;
				}
			}
		}

		public override int GetHashCode()
		{
			return base.GetHashCode() * m_Alpha.GetHashCode();
		}

		public void SerializeParameters(XmlWriter writer)
		{
			writer.WriteStartElement("parameters");
			writer.WriteAttributeString("alpha", m_Alpha.ToString());
			writer.WriteEndElement();
		}

		public void DeserializeParameters(XmlReader reader)
		{
			try
			{
				while (reader.Read())
				{
					switch (reader.NodeType)
					{
						case XmlNodeType.Element:
							{
								if (reader.Name == "parameters")
								{
									m_Alpha = double.Parse(reader["alpha"]);
									return;
								}
								break;
							}
					}
				}
			}
			catch (Exception exc)
			{
				Console.WriteLine("Exception caught while loading activation function data.\n\tSource: " + exc.Source + "\n\tMessage: " + exc.Message);
				m_Alpha = 1;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Apex s.r.l.
Italy Italy
I got my Computer Science (Engineering) Master's Degree at the Siena University (Italy), but I'm from Rieti (a small town next to Rome).
My hobbies are RPG, MMORGP, programming and 3D graphics.
At the moment I'm employed at Apex s.r.l. (Modena, Italy) as a senior software developer, working for a WPF/WCF project in Rome.

Comments and Discussions