- neuro_src.zip
- neuro_src
- Docs
- AForge.Core.chm
- AForge.Neuro.chm
- Release
- AForge.Controls.dll
- AForge.dll
- AForge.Neuro.dll
- Samples
- Neuro
- Back Propagation
- Approximation
- TimeSeries
- XORProblem
- Simple
- Delta Rule Learning
- One-Layer Perceptron Classifier
- Perceptron Classifier
- SOM
- Sources
- neuro_demo.zip
- neuro_demo
- Back Propagation
- Approximation
- AForge.Controls.dll
- AForge.dll
- AForge.Neuro.dll
- Approximation.exe
- Data Samples
- TimeSeries
- AForge.Controls.dll
- AForge.dll
- AForge.Neuro.dll
- Data Samples
- 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
- One-Layer Perceptron Classifier
- AForge.Controls.dll
- AForge.dll
- AForge.Neuro.dll
- Classifier.exe
- Data Samples
- Perceptron Classifier
- AForge.Controls.dll
- AForge.dll
- AForge.Neuro.dll
- Classifier.exe
- Data Samples
- 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
|
// 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 $<var_number> (<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.
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,
Computer Vision Sandbox,
cam2web,
ANNT, etc.