Click here to Skip to main content
12,405,543 members (62,993 online)
Click here to Skip to main content

Stats

789.5K views
57.6K downloads
661 bookmarked
Posted

Neural Networks on C#

, 19 Nov 2006 GPL3
The articles describes a C# library for neural network computations, and their application for several problem solving.
neuro_demo
Back Propagation
Approximation
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Approximation.exe
Data Samples
sample1.csv
sample2.csv
TimeSeries
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
TimeSeries.exe
XORProblem
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
XORProblem.exe
Simple
Delta Rule Learning
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Classifier.exe
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
2DOrganizing.exe
AForge.dll
AForge.Neuro.dll
Color
AForge.dll
AForge.Neuro.dll
Color.exe
TSP
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
TSP.exe
neuro_src
Docs
AForge.Core.chm
AForge.Neuro.chm
Release
AForge.Controls.dll
AForge.dll
AForge.Neuro.dll
Samples
Neuro
Back Propagation
Approximation
App.ico
Data Samples
sample1.csv
sample2.csv
TimeSeries
App.ico
Data Samples
exponent.csv
growing sinusoid.csv
parabola.csv
sigmoid.csv
sinusoid.csv
XORProblem
App.ico
Simple
Delta Rule Learning
App.ico
Data Samples
and.csv
cube.csv
or.csv
sample1.csv
sample2.csv
One-Layer Perceptron Classifier
App.ico
Data Samples
sample1.csv
sample2.csv
Perceptron Classifier
App.ico
Data Samples
and.csv
cube.csv
or.csv
SOM
2DOrganizing
App.ico
Color
App.ico
TSP
App.ico
Sources
Controls
Core
Neuro
Activation Functions
Images
sigmoid.bmp
sigmoid_bipolar.bmp
threshold.bmp
Layers
Learning
Networks
Neurons
// AForge Library
//
// Copyright � Andrew Kirillov, 2006
// andrew.kirillov@gmail.com
//

namespace AForge
{
	using System;
	using System.Collections;

	// Quick and dirty implementation of polish expression evaluator

	/// <summary>
	/// Evaluator of expressions written in polish notation
	/// </summary>
	/// 
	/// <remarks>The class evaluates expressions writen in postfix polish notation.
	/// The list of supported functuins is:
	/// <list type="bullet">
	/// <item><b>Arithmetic functions</b>: +, -, *, /;</item>
	/// <item><b>sin</b> - sine;</item>
	/// <item><b>cos</b> - cosine;</item>
	/// <item><b>ln</b> - natural logarithm;</item>
	/// <item><b>exp</b> - exponent;</item>
	/// <item><b>sqrt</b> - square root.</item>
	/// </list>
	/// Arguments for these functions could be as usual constants, written as numbers, as variables,
	/// writen as $&lt;var_number&gt; (<b>$2</b>, for example). The variable number is zero based index
	/// of variables array.
	/// </remarks>
	/// 
	/// <example>The following sample illustrates the usage of <c>PolishExpression</c> class:
	/// <code>
	/// // expression written in polish notation
	/// string expression = "2 $0 / 3 $1 * +";
	/// // variables for the expression
	/// double[] vars = new double[] { 3, 4 };
	/// // expression evaluation
	/// double result = PolishExpression.Evaluate( expression, vars );
	/// </code>
	/// </example>
	/// 
	public class PolishExpression
	{
		/// <summary>
		/// Constructor (the class should not be instantiated at this moment)
		/// </summary>
		private PolishExpression ( ) { }

		/// <summary>
		/// Evaluates specified expression
		/// </summary>
		///
		/// <param name="expression">Expression written in postfix polish notation</param>
		/// <param name="variables">Variables for the expression</param>
		/// 
		/// <returns>Evaluated value of the expression</returns>
		///
		public static double Evaluate( string expression, double[] variables )
		{
			// split expression to separate tokens, which represent functions ans variables
			string[]	tokens = expression.Trim( ).Split( ' ' );
			// arguments stack
			Stack		arguments = new Stack( );

			// walk through all tokens
			foreach ( string token in tokens )
			{
				// check for token type
				if ( char.IsDigit( token[0] ) )
				{
					// the token in numeric argument
					arguments.Push( double.Parse( token ) );
				}
				else if ( token[0] == '$' )
				{
					// the token is variable
					arguments.Push( variables[ int.Parse( token.Substring( 1 ) ) ] );
				}
				else
				{
					// each function has at least one argument, so let's get the top one
					// argument from stack
					double v = (double) arguments.Pop( );

					// check for function
					switch ( token )
					{
						case "+":			// addition
							arguments.Push( (double) arguments.Pop( ) + v );
							break;

						case "-":			// subtraction
							arguments.Push( (double) arguments.Pop( ) - v );
							break;

						case "*":			// multiplication
							arguments.Push( (double) arguments.Pop( ) * v );
							break;

						case "/":			// division
							arguments.Push( (double) arguments.Pop( ) / v );
							break;

						case "sin":			// sine
							arguments.Push( Math.Sin( v ) );
							break;

						case "cos":			// cosine
							arguments.Push( Math.Cos( v ) );
							break;

						case "ln":			// natural logarithm
							arguments.Push( Math.Log( v ) );
							break;

						case "exp":			// exponent
							arguments.Push( Math.Exp( v ) );
							break;

						case "sqrt":		// square root
							arguments.Push( Math.Sqrt( v ) );
							break;

						default:
							// throw exception informing about undefined function
							throw new ArgumentException( "Undefined function: " + token );
					}
				}
			}

			// check stack size
			if ( arguments.Count != 1 )
			{
				throw new ArgumentException( "Incorrect expression" );
			}

			// return the only value from stack
			return (double) arguments.Pop( );
		}
	}
}

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 GNU General Public License (GPLv3)

Share

About the Author

Andrew Kirillov
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET and not so open Computer Vision Sandbox.

Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 19 Nov 2006
Article Copyright 2006 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid