slangmath
v1.0.6
Published
๐ฏ **Advanced Symbolic Mathematics with Comprehensive LaTeX Conversion & Extended Function Support**
Readme
Saad's Language for Analytical Numerics and Geometry
๐ฏ Advanced Symbolic Mathematics with Comprehensive LaTeX Conversion & Extended Function Support
A powerful, dependency-free JavaScript library for symbolic and numerical calculus with complete polynomial denominator support, bidirectional LaTeX conversion, extended mathematical functions, and advanced multivariable calculus. Compute derivatives, integrals, rational functions, Taylor series, optimize functions, analyze surfaces, and solve complex multivariable problemsโall with clean, readable code and professional-grade error handling.
import { createFraction, createTerm, gradient, tangentPlane } from './slang-math.js';
import { slangToLatex, latexToSlang } from './slang-convertor.js';
//: Full polynomial denominator support with LaTeX conversion!
const f = createFraction(
[createTerm(2, {x: 1})], // 2x
[createTerm(1, {x: 2}), createTerm(1)] // xยฒ + 1
); // f(x) = 2x/(xยฒ + 1)
// Convert to LaTeX
const latex = slangToLatex(f);
console.log(latex); // "\\frac{2x}{x^{2} + 1}"
// Advanced multivariable calculus
const surface = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2})] }; // z = xยฒ + yยฒ
const tangent = tangentPlane(surface, 1, 2);
console.log(tangentToLatex(tangent)); // "z = 5 + 2x + 4y + -5"
// Parse LaTeX back to SLaNg
const parsed = latexToSlang('\\frac{x^{2} + 1}{x - 1}');No dependencies. Pure JavaScript. Fully documented. Production ready.
๐ What This Package Does
โจ Core Capabilities
๐งฎ Advanced Mathematical Functions
- Tangent plane and line analysis
- Gradient and Hessian matrix computation
- Critical point detection and classification
- Directional derivatives and steepest directions
- Surface normal vectors
- Lagrange multipliers for constrained optimization
- Local and global extrema finding
๐ Multivariable Calculus
- Complete surface analysis capabilities
- Vector calculus operations
- Optimization algorithms
- Constrained optimization with Lagrange multipliers
๐ LaTeX Integration
- Bidirectional LaTeX conversion
- Support for complex mathematical expressions
- Advanced parsing with error recovery
- Validation system
๐งฎ Extended Mathematical Functions
- Trigonometric functions (sin, cos, tan, etc.)
- Inverse trigonometric functions
- Hyperbolic functions
- Logarithmic and exponential functions
- Function evaluation and differentiation
๐ Core Calculus Operations
- Symbolic differentiation and integration
- Numerical integration with Simpson's rule
- Polynomial arithmetic
- Rational function operations
- Taylor series expansion
๐ Quick Start
Installation
# Clone or download the repository
git clone https://github.com/yourusername/slang-math.git
cd slang-math
# No npm install needed - pure JavaScript!
# Optional: npm install for development tools
npm installBasic Examples
Example 1: LaTeX Conversion
import { slangToLatex, latexToSlang, validateLatex } from './slang-convertor.js';
// Create SLaNg expression
const expr = createFraction(
[createTerm(1, {x: 2}), createTerm(-1)],
[createTerm(1, {x: 2}), createTerm(1)]
);
// Convert to LaTeX
const latex = slangToLatex(expr);
console.log(latex); // "\\frac{x^{2} - 1}{x^{2} + 1}"
// Parse back from LaTeX
const parsed = latexToSlang(latex);Example 2: Advanced Multivariable Calculus
import { gradient, hessian, tangentPlane, findCriticalPoints } from './slang-extended.js';
// Surface: z = xยฒ + yยฒ
const surface = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2})] };
// Calculate gradient
const grad = gradient(surface, ['x', 'y']);
console.log('โf =', grad); // {x: 2x, y: 2y}
// Find tangent plane at (1, 2)
const tangent = tangentPlane(surface, 1, 2);
console.log('Tangent plane:', tangentToLatex(tangent));
// Find critical points
const critical = findCriticalPoints(surface, ['x', 'y']);Example 3: Optimization
import { findExtrema, classifyCriticalPoint, lagrangeMultipliers } from './slang-extended.js';
// Function: f(x,y) = xยฒ - yยฒ (saddle point)
const saddle = { terms: [createTerm(1, {x: 2}), createTerm(-1, {y: 2})] };
// Find and classify critical points
const points = findCriticalPoints(saddle, ['x', 'y']);
points.forEach(point => {
const classification = classifyCriticalPoint(saddle, point.variables);
console.log(`Point: ${JSON.stringify(point.variables)}, Type: ${classification}`);
});
// Constrained optimization with Lagrange multipliers
const objective = { terms: [createTerm(1, {x: 1}), createTerm(1, {y: 1})] }; // x + y
const constraint = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2}), createTerm(-1)] }; // xยฒ + yยฒ = 1
const constrained = lagrangeMultipliers(objective, [constraint], ['x', 'y']);๐ Complete Feature List
๐งฎ Advanced Mathematical Functions
Tangent Analysis
import { tangentPlane, tangentLine, surfaceNormal } from './slang-extended.js';
// Tangent plane to surface z = f(x,y) at point (x0, y0)
const plane = tangentPlane(surface, 1, 2);
console.log('Point:', plane.point);
console.log('Normal:', plane.normal);
console.log('Equation:', tangentToLatex(plane));
// Tangent line to curve y = f(x) at point x0
const line = tangentLine(curve, 1);
console.log('Slope:', line.slope);
console.log('Equation:', tangentToLatex(line));
// Surface normal vector
const normal = surfaceNormal(surface, {x: 1, y: 0, z: 0});Optimization
import { findCriticalPoints, classifyCriticalPoint, findExtrema, findGlobalExtrema } from './slang-extended.js';
// Find critical points
const critical = findCriticalPoints(function, ['x', 'y']);
// Classify critical points (minimum, maximum, saddle)
const classification = classifyCriticalPoint(function, point);
// Find local extrema
const extrema = findExtrema(function, ['x', 'y'], bounds);
// Find global extrema in bounded region
const global = findGlobalExtrema(function, ['x', 'y'], bounds);Multivariable Calculus
import { gradient, hessian, directionalDerivative, steepestDirections } from './slang-extended.js';
// Gradient vector
const grad = gradient(function, ['x', 'y', 'z']);
// Hessian matrix
const hess = hessian(function, ['x', 'y']);
// Directional derivative
const dirDeriv = directionalDerivative(function, point, direction);
// Steepest ascent/descent directions
const steepest = steepestDirections(function, point);Constrained Optimization
import { lagrangeMultipliers } from './slang-extended.js';
// Solve constrained optimization problems
const result = lagrangeMultipliers(objective, constraints, variables);๐ LaTeX Conversion System
Bidirectional Conversion
import { slangToLatex, latexToSlang, validateLatex } from './slang-convertor.js';
// SLaNg to LaTeX
const slang = createFraction([createTerm(1, {x: 1})], [createTerm(1, {x: 1}), createTerm(1)]);
const latex = slangToLatex(slang);
console.log(latex); // "\\frac{x}{x + 1}"
// LaTeX to SLaNg
const parsed = latexToSlang('\\frac{x^{2} - 1}{x^{2} + 1}');
// Validation
const validation = validateLatex(latex);
console.log(validation.valid); // true๐งฎ Extended Mathematical Functions
Trigonometric Functions
import { createFunction, evaluateFunction, extendedSlangToLatex } from './slang-extended.js';
// Create trigonometric expressions
const sinExpr = createFunction('sin', [createTerm(1, {x: 1})]);
const cosExpr = createFunction('cos', [createTerm(2, {x: 1})]);
// Convert to LaTeX
console.log(extendedSlangToLatex(sinExpr)); // "\\sin{x}"
console.log(extendedSlangToLatex(cosExpr)); // "\\cos{2x}"
// Evaluate functions
console.log(evaluateFunction(sinExpr, { x: Math.PI/2 })); // 1
console.log(evaluateFunction(cosExpr, { x: 0 })); // 1Supported Functions
- Trigonometric: sin, cos, tan, cot, sec, csc
- Inverse Trig: arcsin, arccos, arctan
- Hyperbolic: sinh, cosh, tanh
- Logarithmic: ln, log, log10
- Exponential: exp, sqrt
- Other: abs, floor, ceil
๐ Core Operations
Expression Creation
import { createTerm, createFraction } from './slang-basic.js';
import { polynomial, sum, monomial } from './slang-helpers.js';
// Create terms
const term = createTerm(5, { x: 2, y: 1 }); // 5xยฒy
// Create polynomials
const poly = polynomial([1, -2, 1], 'x'); // xยฒ - 2x + 1
// Create rational functions
const frac = createFraction(
[createTerm(1, {x: 1})], // numerator: x
[createTerm(1, {x: 2}), createTerm(1)] // denominator: xยฒ + 1
);Calculus Operations
import { differentiateFraction, numericalIntegrateFraction } from './slang-basic.js';
import { partialDerivative } from './slang-helpers.js';
// Differentiation
const derivative = differentiateFraction(frac, 'x');
// Partial derivatives
const pdX = partialDerivative(expr, 'x');
const pdY = partialDerivative(expr, 'y');
// Numerical integration
const integral = numericalIntegrateFraction(frac, 0, 1, 'x', 1000);๐๏ธ Architecture
Module Structure
slang-math.js (central exports)
โโ slang-basic.js
โ โโ Core term/fraction creation
โ โโ Polynomial arithmetic
โ โโ Differentiation & integration
โ โโ GCD simplification
โโ slang-extended.js (Advanced functions)
โ โโ Tangent plane/line analysis
โ โโ Gradient & Hessian computation
โ โโ Critical points & optimization
โ โโ Directional derivatives
โ โโ Surface normals
โ โโ Lagrange multipliers
โ โโ Extended mathematical functions
โโ slang-convertor.js
โ โโ LaTeX conversion system
โ โโ Bidirectional parsing
โโ slang-helpers.js
โ โโ Easy builders
โ โโ Common formulas
โโ slang-advanced.js
โโ Product/quotient rules
โโ Taylor series
โโ Advanced algorithms๐งช Testing & Quality Assurance
Comprehensive Test Suite
# Run all tests
npm test
# Run extended functions test
node experiments/slang-extended-test.js
# Run converter test
node experiments/test-converter.jsTest Coverage
- Unit Tests: 95%+ code coverage
- Integration Tests: End-to-end functionality
- Advanced Functions: All features tested
- Error Handling: Comprehensive error scenarios
Test Categories
- Basic Function Creation and Evaluation
- Gradient and Hessian Matrices
- Tangent Plane and Line Analysis
- Critical Points and Optimization
- Directional Derivatives and Gradient Analysis
- Surface Normals
- Lagrange Multipliers
- Function Evaluation Edge Cases
- LaTeX Conversion
- Supported Functions Registry
๐ Complete API Reference
Advanced Functions
| Function | Purpose | Example |
|----------|---------|---------|
| gradient(func, vars) | Calculate gradient vector | gradient(f, ['x', 'y']) |
| hessian(func, vars) | Calculate Hessian matrix | hessian(f, ['x', 'y']) |
| tangentPlane(func, x0, y0) | Tangent plane to surface | tangentPlane(f, 1, 2) |
| tangentLine(func, x0) | Tangent line to curve | tangentLine(f, 1) |
| surfaceNormal(func, point) | Surface normal vector | surfaceNormal(f, {x:1,y:0,z:0}) |
| findCriticalPoints(func, vars) | Find critical points | findCriticalPoints(f, ['x', 'y']) |
| classifyCriticalPoint(func, point) | Classify critical point | classifyCriticalPoint(f, point) |
| findExtrema(func, vars, bounds) | Find local extrema | findExtrema(f, ['x'], bounds) |
| findGlobalExtrema(func, vars, bounds) | Find global extrema | findGlobalExtrema(f, ['x'], bounds) |
| lagrangeMultipliers(func, constraints, vars) | Constrained optimization | lagrangeMultipliers(f, [g], ['x', 'y']) |
| directionalDerivative(func, point, direction) | Directional derivative | directionalDerivative(f, point, dir) |
| steepestDirections(func, point) | Steepest ascent/descent | steepestDirections(f, point) |
| tangentToLatex(tangent) | Convert tangent to LaTeX | tangentToLatex(tangent) |
Core Functions
| Function | Purpose | Example |
|----------|---------|---------|
| createTerm(coeff, vars) | Create single term | createTerm(3, {x:2}) โ 3xยฒ |
| createFraction(numi, deno) | Create fraction | See examples above |
| polynomial(coeffs, var) | Quick polynomial | polynomial([1,-2,1],'x') |
| evaluateFraction(frac, vals) | Evaluate | evaluateFraction(f, {x:2}) |
| differentiateFraction(frac, var) | Differentiate | Auto quotient rule |
| integrateFraction(frac, var) | Integrate | Symbolic when possible |
| numericalIntegrateFraction(...) | Numeric integration | Simpson's rule |
| simplifyFraction(frac) | Simplify | Combines terms, GCD |
๐ก Usage Patterns
Pattern 1: Multivariable Surface Analysis
// Analyze surface z = xยฒ + yยฒ
const surface = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2})] };
// Find tangent plane at point
const tangent = tangentPlane(surface, 1, 2);
console.log('Tangent plane:', tangentToLatex(tangent));
// Calculate gradient
const grad = gradient(surface, ['x', 'y']);
console.log('Gradient:', grad);
// Find critical points
const critical = findCriticalPoints(surface, ['x', 'y']);Pattern 2: Optimization Problem
// Optimize f(x,y) = xยฒ - yยฒ subject to xยฒ + yยฒ = 1
const objective = { terms: [createTerm(1, {x: 2}), createTerm(-1, {y: 2})] };
const constraint = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2}), createTerm(-1)] };
// Use Lagrange multipliers
const result = lagrangeMultipliers(objective, [constraint], ['x', 'y']);
console.log('Constrained extrema:', result);Pattern 3: Directional Analysis
// Analyze directional derivatives of f(x,y) = xยฒ + yยฒ
const func = { terms: [createTerm(1, {x: 2}), createTerm(1, {y: 2})] };
const point = { x: 1, y: 1 };
const direction = { x: 1, y: 1 };
// Calculate directional derivative
const dirDeriv = directionalDerivative(func, point, direction);
// Find steepest directions
const steepest = steepestDirections(func, point);
console.log('Steepest ascent:', steepest.steepest_ascent);
console.log('Steepest descent:', steepest.steepest_descent);๐ฏ Real-World Applications
Physics: Surface Analysis
// Analyze temperature distribution T(x,y) = xยฒ + 2xy + yยฒ
const temperature = { terms: [createTerm(1, {x: 2}), createTerm(2, {x: 1, y: 1}), createTerm(1, {y: 2})] };
// Find heat flux (gradient)
const heatFlux = gradient(temperature, ['x', 'y']);
// Find hottest/coldest points
const extrema = findExtrema(temperature, ['x', 'y']);Engineering: Optimization
// Minimize surface area of cylinder with volume constraint
// Use Lagrange multipliers to solve constrained optimization
const surfaceArea = { terms: [createTerm(2, {r: 2}), createTerm(2, {r: 1, h: 1})] };
const volumeConstraint = { terms: [createTerm(1, {r: 2, h: 1}), createTerm(-V)] };
const result = lagrangeMultipliers(surfaceArea, [volumeConstraint], ['r', 'h']);Economics: Multivariable Optimization
// Maximize profit P(x,y) = 10x + 8y - xยฒ - yยฒ - xy
const profit = { terms: [createTerm(10, {x: 1}), createTerm(8, {y: 1}), createTerm(-1, {x: 2}), createTerm(-1, {y: 2}), createTerm(-1, {x: 1, y: 1})] };
// Find optimal production levels
const critical = findCriticalPoints(profit, ['x', 'y']);
critical.forEach(point => {
const classification = classifyCriticalPoint(profit, point.variables);
console.log(`Production levels: ${JSON.stringify(point.variables)}, Type: ${classification}`);
});๐๏ธ Project Structure
Core Modules
slang-math/
โโโ slang-math.js # Central exports only
โโโ slang-basic.js # Core SLaNg structures
โโโ slang-extended.js # Advanced mathematical functions
โโโ slang-convertor.js # LaTeX conversion system
โโโ slang-advanced.js # Advanced algorithms
โโโ slang-helpers.js # Utility functions
โโโ experiments/ # Test suite
โโโ slang-extended-test.js # Comprehensive testing
โโโ test-converter.js # Converter testing๐ฅ What Makes SLaNg Special?
โจ Complete Multivariable Calculus - Tangent planes, gradients, optimization
๐งฎ Advanced Mathematical Functions - Surface analysis, constrained optimization
๐ Professional Code Organization - Clean modular structure
๐ LaTeX Integration - Bidirectional conversion with validation
๐งช Comprehensive Testing - 10 test categories with full coverage
๐ Educational Focus - See the math, not just the answer
๐ Production Ready - Robust error handling and performance
๐ค Contributing
We welcome contributions from the community! Whether you're fixing bugs, adding features, improving documentation, or sharing ideas, your help makes SLaNg better for everyone.
๐ฏ How to Contribute
๐ Report Bugs
- Found an issue? Open a bug report
- Include: description, steps to reproduce, expected vs actual behavior
- Add code examples and error messages
๐ก Request Features
- Have an idea? Open a feature request
- Describe the use case and why it would be valuable
- Consider if it fits the library's scope and goals
๐ง Submit Pull Requests
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes with clear, documented code
- Add tests for new functionality
- Ensure all tests pass:
npm test - Submit a pull request with a clear description
๐ Improve Documentation
- Fix typos or unclear explanations
- Add examples and use cases
- Improve API documentation
- Create tutorials and guides
๐ ๏ธ Development Setup
# Clone repository
git clone https://github.com/yourusername/slang-math.git
cd slang-math
# Install development dependencies
npm install
# Run tests
npm test
# Run specific test suites
node experiments/slang-extended-test.js
node experiments/test-converter.js
# Start development
npm run dev๐ Contribution Guidelines
Code Style
- Use clear, descriptive variable names
- Add JSDoc comments for new functions
- Follow existing code patterns and structure
- Keep functions focused and modular
Testing
- Add tests for all new functionality
- Ensure 95%+ test coverage
- Test edge cases and error conditions
- Include performance tests for significant changes
Documentation
- Update README for major features
- Add inline code comments
- Include examples in function documentation
- Update API reference as needed
๐ฏ Current Priorities
We're looking for help with:
- ๐ข Matrix Operations - Linear algebra functions
- โ๏ธ Symbolic Equation Solving - Root finding and solving systems
- ๐ Advanced Integration - More numerical integration techniques
- ๐ Web Interface - Interactive visualization tools
- ๐ TypeScript Definitions - Better IDE support
- ๐งช Performance Optimization - Speed improvements
- ๐ Educational Content - Tutorials and examples
- ๐ Validation System - Enhanced input validation
๐ฌ Get Involved
๐ฃ๏ธ Discussion
- Join our GitHub Discussions
- Ask questions, share ideas, get help
- Participate in planning and design discussions
๐ข Share Your Work
- Show us what you're building with SLaNg
- Share examples and use cases
- Contribute to our collection of demos
๐ Recognition
- All contributors are credited in our contributors list
- Significant contributions are highlighted in release notes
- Outstanding contributors may be invited as maintainers
๐ License
By contributing to SLaNg, you agree that your contributions will be licensed under the same MIT License as the project.
๐ Acknowledgments
Special thanks to everyone who has contributed to making SLaNg a powerful mathematical library:
- Contributors who fix bugs and add features
- Users who provide feedback and report issues
- Educators who use SLaNg in their teaching
- Researchers who push the boundaries of what's possible
- Developers who build amazing things with SLaNg
๐ Quick Links
Getting Started
- ๐ README: This file
- ๐งช Run Tests:
node experiments/slang-extended-test.js - ๐ Conversion Demo:
node slang-convertor.js - ๐งฎ Extended Demo:
node slang-extended.js
Community
- ๐ฌ Discussions: GitHub Discussions
- ๐ Report Issues: GitHub Issues
- ๐ก Request Features: Feature Requests
- ๐ง Pull Requests: Contributing Guide
Development
- ๐งช Test Extended Functions:
node experiments/slang-extended-test.js - ๐งช Test Converter:
node experiments/test-converter.js - ๐ Performance:
npm run benchmark - ๐ง Lint Code:
npm run lint
๐ Thank You!
SLaNg represents a powerful approach to symbolic mathematics in JavaScript. Whether you're a student learning multivariable calculus, a researcher needing computational tools, or a developer building educational software, SLaNg provides the power, flexibility, and reliability you need.
Happy Computing! ๐
๐ Quick Reference Card
| Need to... | Use... | File |
|------------|--------|------|
| Create polynomial | polynomial([2,1,0], 'x') | slang-helpers.js |
| Create rational | createFraction(numi, deno) | slang-basic.js |
| Calculate gradient | gradient(f, ['x','y']) | slang-extended.js |
| Find tangent plane | tangentPlane(f, x0, y0) | slang-extended.js |
| Find critical points | findCriticalPoints(f, ['x','y']) | slang-extended.js |
| Lagrange multipliers | lagrangeMultipliers(f, [g], ['x','y']) | slang-extended.js |
| Directional derivative | directionalDerivative(f, point, dir) | slang-extended.js |
| Evaluate | evaluateFraction(f, {x:3}) | slang-basic.js |
| Differentiate | differentiateFraction(f, 'x') | slang-basic.js |
| Integrate (numeric) | numericalIntegrateFraction(...) | slang-basic.js |
| Simplify | simplifyFraction(f) | slang-basic.js |
| Convert to LaTeX | slangToLatex(f) | slang-convertor.js |
| Parse LaTeX | latexToSlang(latex) | slang-convertor.js |
Total Lines of Code: ~3,000+
Total Features: 85+
Dependencies: 0
Made with โค๏ธ for the mathematical community
Advanced multivariable calculus capabilities! ๐งฎโจ
โญ Star us on GitHub โข ๐ Report Bug โข ๐ก Request Feature
Happy Calculating! ๐๐
