$ show wojdyr/fityk
Fityk is a cross-platform scientific data fitting program focused on nonlinear curve fitting, particularly peak fitting in spectroscopy and crystallography. It provides a core library (libfityk) with multiple fitting algorithms (Levenberg-Marquardt, MPFIT, Nelder-Mead, Genetic Algorithm, NLopt), a rich set of built-in peak functions (Gaussian, Lorentzian, Voigt, Pearson VII, etc.), symbolic derivatives via AST, and a bytecode VM for expression evaluation. The architecture is modular: a core library, a CLI with readline and gnuplot integration, a wxWidgets GUI, and scripting support via Lua and SWIG bindings for Python, Perl, Ruby, and Java. Key entry points include the Fityk class API, the command parser (cparser.cpp), the runner (runner.cpp), and the GUI main frame (frame.cpp). Data flows involve loading data from files (via xylib), defining models as sums of functions, fitting using least-squares optimization, and outputting parameters and statistics. External dependencies include wxWidgets, Boost, Lua, NLopt, xylib, readline, gnuplot, zlib, and SWIG. The codebase is well-structured with extensive documentation and tests, though it shows some age with pre-C++11 idioms and platform-specific workarounds.
The LLM identified 5 files worth highlighting for reuse:
Implements a stack-based bytecode virtual machine for efficient evaluation of mathematical expressions. The VM compiles parsed expressions into bytecode and executes them with support for variables, built-in functions, and user-defined functions. This component is self-contained and can be extracted for use in any project requiring fast expression evaluation, such as formula calculators or scripting engines.
Provides symbolic differentiation and simplification of mathematical expressions represented as an AST. Includes rules for simplifying expressions (e.g., sinΒ²+cosΒ²β1, tanΒ·cosβsin) and converting between AST and bytecode. This module is reusable for any application needing symbolic math, such as automatic differentiation or formula manipulation.
Implements automatic initial parameter estimation for peak fitting. Uses heuristics to detect peaks in data (e.g., local maxima, inflection points) and estimate parameters like center, height, and width. This algorithm is domain-agnostic and can be reused in any peak-fitting or signal-processing project.
A standalone C implementation of the Levenberg-Marquardt algorithm (MINPACK-1) for weighted least-squares minimization. Supports constraints, user-provided derivatives, and robust convergence criteria. This is a well-documented, battle-tested optimizer that can be extracted and used in any C/C++ project requiring nonlinear curve fitting.
A custom wxWidgets text input control that emulates readline keybindings (e.g., Ctrl+A, Ctrl+E, history navigation). It is explicitly designed as a reusable component and can be extracted for use in any wxWidgets application needing a command-line-like input field with advanced editing capabilities.