Skip to content

Conversation

@paul-hammant
Copy link
Contributor

No description provided.

Created a powerful expression parsing and serialization system with:

Features:
- Multi-format input: S-expressions, infix, prefix, JSON, auto-detect
- Multi-format output: sexp, pretty, JSON, XML, DOT, infix, prefix, AST, tree, rainbow
- Expression evaluation with arithmetic, logical, and mathematical operations
- Expression statistics (depth, node counts, operator frequency)
- Comprehensive test suite with 50+ test cases
- Example data files and documentation

Components:
- cmd/expr.lisp: Main CLI application
- cmd/expr_test.lisp: Comprehensive test suite
- lib/expr/parser.inc: Multi-format parser with tokenizer
- lib/expr/serializer.inc: 10 different output formats
- lib/expr/eval.inc: Expression evaluator with 30+ operations
- lib/expr/stats.inc: Statistical analysis tools
- examples/expr/: Example data files and README

Capabilities:
- Parse infix notation (1 + 2 * 3) to S-expressions
- Convert between all supported formats
- Evaluate expressions with proper precedence
- Generate GraphViz DOT diagrams for visualization
- ASCII art tree representations
- Rainbow colored output with ANSI codes
- Support for factorial, fibonacci, gcd, lcm, min, max, abs, sqrt
- Mathematical constants: pi, e, phi

The implementation follows ChrysaLisp idioms:
- Modular library structure with env-push/pop
- Efficient sequence operations
- Proper use of Fmap for statistics
- Character class support for parsing
- Export pattern for modules
…on parser

Implemented powerful symbolic math capabilities:

NEW FEATURES:
- Symbolic differentiation engine (lib/expr/diff.inc)
  - Power rule, product rule, quotient rule, chain rule
  - Derivatives for: +, -, *, /, ^, sin, cos, tan, exp, ln, log, sqrt, abs
  - Nth-order derivatives
  - Partial derivatives and gradient computation

- Algebraic simplification (lib/expr/simplify.inc)
  - Identity simplifications: x+0=x, x*1=x, x*0=0, x^0=1, x^1=x
  - Constant folding: (+ 1 2 3) = 6
  - Zero/one elimination from sums and products
  - Nested expression simplification
  - Logarithm/exponential simplification
  - Trigonometric simplifications

- Expression expansion
  - Distribute products over sums: (a+b)*c = a*c+b*c

UPDATED cmd/expr.lisp:
- Added --diff (-d) option for differentiation
- Added --nth (-n) option for nth derivatives
- Added --simplify (-p) option for simplification
- Added --expand (-x) option for expansion
- Integrated all transformations into process flow
- Updated multi-format view to show derivatives

UPDATED cmd/expr_test.lisp:
- Added 30+ differentiation tests
- Added 20+ simplification tests
- Tests for power rule, product rule, quotient rule
- Tests for trig, exp, log derivatives
- Tests for identity simplifications
- Tests for constant folding and nested simplification

NEW EXAMPLES:
- derivatives.txt: Basic derivative examples
- calculus.txt: Classic calculus examples (chain rule, product rule)
- simplification.txt: Expressions demonstrating simplification

DOCUMENTATION:
- Updated README with comprehensive differentiation/simplification guide
- Added examples for all new features
- Documented all trigonometric and exponential functions
- Expanded architecture section

This transforms the expression parser into a symbolic math system capable of:
- Computing derivatives d/dx symbolically
- Simplifying complex algebraic expressions
- Expanding and transforming expressions
- Combining differentiation with simplification for clean results

Example usage:
  expr '(* x x)' -d x -p              # d/dx(x*x) = 2*x
  expr '(^ x 3)' -d x -p              # d/dx(x^3) = 3*x^2
  expr '(sin (* 2 x))' -d x -p        # Chain rule
  expr '(^ x 4)' -d x -n 2 -p         # Second derivative
  cat calculus.txt | expr -d x -p    # Batch differentiation
Added "Using as a Library" section demonstrating how to import and use
the expr libraries programmatically in other ChrysaLisp applications.

Examples include:
- Basic calculator function
- Symbolic differentiation wrapper
- Calculator class with eval/analyze/convert methods
- Computer Algebra System (CAS) class with Taylor series
- Expression transformation pipeline
- Complete API reference for all exported functions

Shows that expr is both:
- A standalone command-line tool (cmd/expr.lisp)
- A set of reusable libraries (lib/expr/*.inc)

This clarifies the dual nature and encourages library reuse for:
- GUI calculators
- Graphing tools
- Equation solvers
- Educational math software
- Scientific computing applications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants