Evaluator Class
The Evaluator class is designed to evaluate mathematical expressions with support for variables and functions. It allows you to perform arithmetic operations, use mathematical constants, and define custom functions and variables for evaluation.
Explore the CLI Expression Evaluator Example to see how the Evaluator class can be used in a real-world project.
This example provides step-by-step guidance for practical implementation.
Features
- Evaluate mathematical expressions with variables and functions.
- Support for standard mathematical functions (e.g.,
sqrt,sin,cos, etc.). - Built-in support for common constants like
piande. - Allow dynamic addition of variables and custom functions.
Installation
To use the Evaluator class in your project, simply import it as follows:
import { Evaluator } from 'samasya';
Class Definition
Evaluator
constructor
constructor(variables: { [key: string]: number } = {});
The constructor accepts an optional variables object, which can be used to initialize the evaluator with predefined variables.
- variables: A key-value object where the key is the variable name (e.g.,
x,y) and the value is its corresponding number.
Methods
evaluate(expression: string): number
Evaluates the given mathematical expression.
- expression: A string representing a mathematical expression to evaluate.
- returns: The result of the evaluated expression.
Example:
const evaluator = new Evaluator({ x: 5, y: 10 });
const result = evaluator.evaluate('x * pi + y'); // Evaluates to 25.70796
setVariable(name: string, value: number): void
Sets a variable for the evaluator.
- name: The name of the variable (e.g., x, y).
- value: The value to assign to the variable.
Example:
const evaluator = new Evaluator();
evaluator.setVariable('x', 5);
setFunction(name: string, func: (...args: number[]) => number): void
Sets a custom function for the evaluator.
name: The name of the function (e.g., customFunc).func: The function to associate with the name.
Example:
const evaluator = new Evaluator();
evaluator.setFunction('customFunc', (a, b) => a + b);
const result = evaluator.evaluate('customFunc(2, 3)'); // Evaluates to 5
Built-in Functions and Constants
The Evaluator class comes with several built-in functions and constants:
Functions
sqrt(x): Returns the square root ofx.sin(x): Returns the sine ofx(in radians).cos(x): Returns the cosine ofx(in radians).log(x): Returns the natural logarithm ofx.abs(x): Returns the absolute value ofx.tan(x): Returns the tangent ofx(in radians).exp(x): Returns e^x (Euler's number raised to the power ofx).round(x): Returns x rounded to the nearest integer.floor(x): Returns the largest integer less than or equal tox.ceil(x): Returns the smallest integer greater than or equal tox.max(a, b, ...): Returns the maximum of the given numbers.min(a, b, ...): Returns the minimum of the given numbers.pow(x, y): Returnsxraised to the power ofy.random(): Returns a random number between0and1.
Constants
pi: The mathematical constant π (approximately3.14159).e: The mathematical constant Euler's number (approximately2.71828).
Example:
const evaluator = new Evaluator({ x: 5, y: 10 });
const result = evaluator.evaluate('x * pi + y'); // Uses the pi constant
Example Usage
Here are a few example usages of the Evaluator class:
const evaluator = new Evaluator({ x: 5, y: 10 });
// Basic evaluation with variables
const result1 = evaluator.evaluate('x * pi + y'); // Expected output: 25.70796
// Using the `e` constant
const result2 = evaluator.evaluate('2 + e * x + y'); // Expected output: 25.5914
// Custom function
evaluator.setFunction('multiply', (a, b) => a * b);
const result3 = evaluator.evaluate('multiply(x, y)'); // Expected output: 50
Error Handling
The Evaluator class provides error handling for the following cases:
- Invalid characters in the expression: If the expression contains invalid characters (such as letters other than variables or functions), an error will be thrown.
- Unrecognized variables: If the expression references variables that have not been defined, an error will be thrown.
- Invalid function calls: If a function is called with incorrect arguments, an error will be thrown.
Example:
try {
const result = evaluator.evaluate('x + z'); // z is not defined
} catch (error) {
console.error(error.message); // Output: Unrecognized variable(s): z
}