Skip to content

Most of the code is now ported#7

Merged
wp-xyz merged 7 commits intowp-xyz:mainfrom
joaopauloschuler:main
Apr 6, 2026
Merged

Most of the code is now ported#7
wp-xyz merged 7 commits intowp-xyz:mainfrom
joaopauloschuler:main

Conversation

@joaopauloschuler
Copy link
Copy Markdown
Contributor

Project Structure

fpc/
  _shared/         ← All reusable units (jpm*.pas)
  <topic>/src/     ← CLI demo projects (.lpr)
  <topic>/gui/src/ ← GUI (LCL/Lazarus) demo projects
  bin/             ← Compiled binaries
original/          ← Original Turbo Pascal source (reference only)

Task List

  • CAT 3 ALLjpmroots.pas: Bisection, Newton, Secant, RegulaFalsi, Brent, Mueller, Steffensen, Aitken, Bairstow ✅ COMMITTED
  • CLI demos: akima, lagrange, romberg, normal, student, prime, primes, appointment, roots
  • GUI demos: normal, appointment
  • CAT 1 ALLjpmstats.pas extended: ChiSquareDist/InvChiSquare, FDist/InvFDist, BinomialProb/CDF, PoissonProb/CDF, Mean/Variance/StdDev/Skewness/Kurtosis/Median ✅ COMMITTED (c158916)
  • CAT 1.3jpmspecial.pas: GammaFunc, LnGamma, BetaFunc, LnBeta, IncompleteBeta, IncompleteGammaP/Q, ErfFunc, ErfcFunc ✅ COMMITTED (13ec193)
  • CAT 2.1jpmderivative.pas: Deriv1/2/3/4, DerivRichardson, DerivRomberg, Gradient, Jacobian ✅ COMMITTED (290ca5d)
  • CAT 2.2jpmintegration.pas extended: TrapezoidIntegral, SimpsonIntegral, AdaptiveSimpson, GaussLegendre ✅ COMMITTED (a49ca43)
  • CAT 2.3jpmchebyshev.pas: ChebFit, ChebEval, ChebInteg, ChebDeriv ✅ COMMITTED (9173acc)
  • CAT 2.4jpmcontinued.pas: FloatToCF, CFToFloat, CFConvergents, GenCFEval, SqrtCF, BestRational ✅ COMMITTED (71f08a6)
  • CAT 4 ALLjpmoptimize.pas: GoldenSection, BrentMin, BracketMin, NelderMead, SteepestDescent, PowellMin ✅ COMMITTED (a10f1d8, fa82776)
  • CAT 5 ALLjpmmatrices.pas: LU decompose/solve/inverse/det, Gauss-Seidel, Cholesky, Thomas tridiagonal, Jacobi eigenvalues ✅ COMMITTED (451132e)
  • CAT 6 ALLjpmlstsqr.pas: LeastSquares, PolyRegression, LinearRegression, MultipleRegression, ChiSquareFit, LevenbergMarquardt ✅ COMMITTED (6074b62, 948cb2d)
  • CAT 7 ALLjpmdiffeq.pas: RK4, RKF45, AdamsBashforth4, AdamsMoulton4, GearStiff (Rosenbrock), BulirschStoer ✅ COMMITTED (273f1f5, 1e698e0, b8c5274)
  • CAT 8 ALLjpmpolynomials.pas: PolyEval/Add/Sub/Mul/Div/Deriv/Integ/GCD, TPolyFrac, PolyFracPartial ✅ COMMITTED (654b419, 102eab5)
  • CAT 9.1–9.5,9.7jpmspecial.pas extended: LegendrePn/Qn, HermiteHn, LaguerreLn, AiryAi/Bi, EllipticK/E, Hypergeometric2F1 ✅ COMMITTED (e86c7c0)
  • CAT 9.6jpmbessel.pas: BesselJ0/J1/Y0/Y1/I0/I1/K0/K1, BesselJn, BesselJ0Zero/J1Zero ✅ COMMITTED (e3dd57d)
  • CAT 10 ALLjpmsort.pas: BubbleSort, InsertionSort, SelectionSort, QuickSort, MergeSort, HeapSort, ShellSort, BinarySearch, RankSort ✅ COMMITTED (7ef74cb)
  • CAT 11 ALLjpmsignal.pas: DFT, FFT (Cooley-Tukey), PowerSpectrum, MovingAverage, SavitzkyGolay5, FourierCoeff ✅ COMMITTED (099ea83)
  • CAT 12 ALLjpmarith.pas: GCD/LCM, ExtGCD, Factorial/BinomCoeff/Permutation, TFraction, IntToBase/BaseToInt, IsPrime, PrimeSieve, Factorize, SolveDiophantine ✅ COMMITTED (e4ad8eb)
  • CAT 13 ALLjpmgeometry.pas: SolveTriangle, SolveArcCircle, PointDistance2D/3D, TriangleArea, PolygonArea, CircleArea/Perimeter/Chord/ArcLen, EllipseArea/Perimeter, HyperbolaAsymptoteAngle ✅ COMMITTED (8fef706)
  • CAT 14.1jpmsimplex.pas: SimplexMaximize, SimplexMinimize ✅ COMMITTED (fcb64d3)
  • CAT 14.3jpmanneal.pas: SimulatedAnnealing (N-dim continuous, geometric cooling) ✅ COMMITTED (679e39d)
  • CAT 16.1jpmcomplex.pas: TComplex record + full arithmetic, transcendentals, trig ✅ COMMITTED (381fcbd)

Coding Conventions (fpc/coding_instructions.md)

  • {$mode objfpc}{$H+} in every unit
  • Float = Double — never use real
  • Dynamic arrays only (array of Float, SetLength)
  • Lowercase keywords (begin, end, var, for, if, ...)
  • No goto, no label, no ReadLn in library units
  • Every unit must have a self_test procedure
  • Unit naming: jpm<topic>.pas
  • Types declared in jpmtypes.pas

Units Implemented

jpmtypes.pas — Shared Types

Core types used by all units:

  • Float = Double
  • TFloatArray, TIntArray, TFloatMatrix, TMatrix
  • TFunction1, TFunction2, TFunction3, TFunctionN, TFuncND, TLMFunc
  • SelfTestCheck, SelfTestFail helpers

jpmroots.pas — Root Finding (CAT 3) ✅

Commit: 5ea95da
9 algorithms: Bisection, Newton-Raphson, Secant, Regula Falsi, Brent, Mueller, Steffensen, Aitken, Bairstow (complex polynomial roots)


jpmmatrices.pas — Linear Algebra (CAT 5) ✅

Commit: 451132e
Functions: LU decomposition, LU solve, LU inverse, LU determinant, Gauss-Seidel iteration, Cholesky decomposition, Thomas algorithm (tridiagonal), Jacobi eigenvalue method


jpmspecial.pas — Special Functions (CAT 1.3 + CAT 9) ✅

Commits: 13ec193, e86c7c0
Functions:

  • Gamma family: GammaFunc, LnGamma (Lanczos), BetaFunc, LnBeta, IncompleteBeta (Lentz CF), IncompleteGammaP, IncompleteGammaQ
  • Error functions: ErfFunc, ErfcFunc (Chebyshev)
  • Orthogonal polynomials: LegendrePn, LegendreQn, HermiteHn, LaguerreLn (3-term recurrence)
  • Airy functions: AiryAi, AiryBi (power series + asymptotic)
  • Elliptic integrals: EllipticK, EllipticE (AGM method)
  • Hypergeometric: Hypergeometric2F1 (Gauss series)

jpmstats.pas — Statistics (CAT 1) ✅

Commits: original + c158916
Functions:

  • Distributions: NormalDist, InvNormalDist, ProbST/StudNT, ChiSquareDist, InvChiSquare, FDist, InvFDist
  • Discrete: BinomialProb, BinomialCDF, PoissonProb, PoissonCDF (log-space via LnGamma)
  • Moments: StatMean, Variance, StdDev, Skewness, Kurtosis, Median

jpmderivative.pas — Numerical Derivatives (CAT 2.1) ✅

Commit: 290ca5d
Functions: Deriv1Deriv4 (finite differences), DerivRichardson (step-halving extrapolation), DerivRomberg (5×5 table with error estimate), Gradient (N-dim central diff), Jacobian (M×N central diff)


jpmintegration.pas — Numerical Integration (CAT 2.2) ✅

Commits: original (Romberg) + a49ca43
Functions: RombergIntegral, TrapezoidIntegral, SimpsonIntegral, AdaptiveSimpson, GaussLegendre (n=2,3,4,5,6,8,10 — Abramowitz & Stegun nodes/weights)


jpmchebyshev.pas — Chebyshev Approximation (CAT 2.3) ✅

Commit: 9173acc
Functions: ChebFit (coefficients via cosine sampling), ChebEval (Clenshaw recurrence), ChebInteg (integral coefficients), ChebDeriv (derivative coefficients)


jpmcontinued.pas — Continued Fractions (CAT 2.4) ✅

Commit: 71f08a6
Functions: FloatToCF, CFToFloat, CFConvergents (p[k]/q[k] recurrence), GenCFEval (Lentz method), SqrtCF (periodic CF for √N), BestRational (best rational approx with bounded denominator)
Example: π → [3;7;15;1;292;…], convergents 22/7 and 355/113


jpmoptimize.pas — Optimization (CAT 4) ✅

Commits: a10f1d8, fa82776
Functions: GoldenSection, BrentMin (parabolic interpolation + golden fallback), BracketMin (automatic downhill bracketing), NelderMead (N-dim downhill simplex: reflection/expansion/contraction/shrink), SteepestDescent (central-diff gradient + BrentMin line search), PowellMin (conjugate-direction method, identity matrix init, replace largest-decrease direction, Rosenbrock → (1,1) in 18 iter)


jpmlstsqr.pas — Least Squares (CAT 6) ✅

Commits: 6074b62, 948cb2d
Functions: LeastSquares (normal equations, Gaussian elim), PolyRegression (Vandermonde matrix), LinearRegression (Pearson r), MultipleRegression (augmented design matrix, R²), ChiSquareFit (weighted poly, 1/σ² weights), LevenbergMarquardt (damped Gauss-Newton, forward-diff Jacobian, adaptive λ, TLMFunc callback)


jpmdiffeq.pas — Differential Equations (CAT 7) ✅

Commits: 273f1f5, 1e698e0, b8c5274
Functions:

  • RK4Step, RK4Integrate (classical fixed-step RK4)
  • RKF45 (adaptive, Cash-Karp coefficients)
  • AdamsBashforth4 (4-step explicit, RK4 startup)
  • AdamsMoulton4 (predictor-corrector)
  • GearStiff (Rosenbrock order-2 L-stable method for stiff ODEs; numerical Jacobian, Gaussian elim; Robertson chemistry test: conservation error ~2e-15)
  • BulirschStoer (Gragg-Bulirsch-Stoer; modified midpoint + Neville polynomial extrapolation; sequence [2,4,6,8,12,16,24,32]; harmonic oscillator error ~5.6e-15)

jpmpolynomials.pas — Polynomials (CAT 8) ✅

Commits: 654b419, 102eab5
Functions: PolyEval (Horner), PolyAdd, PolySub, PolyMul, PolyDiv, PolyDeriv, PolyInteg, PolyGCD (Euclidean, monic), PolyDegree, PolyNormalize, PolyPrint
Polynomial fractions: TPolyFrac record, PolyFracMake, PolyFracEval, PolyFracAdd/Sub/Mul/Div, PolyFracReduce (PolyGCD-based), PolyFracPartial (partial fractions via residues N(r)/D'(r))
Convention: index 0 = constant term


jpmbessel.pas — Bessel Functions (CAT 9.6) ✅

Commit: e3dd57d
Functions: BesselJ0, BesselJ1, BesselY0, BesselY1 (Abramowitz & Stegun poly, split at |x|=8), BesselI0, BesselI1, BesselK0, BesselK1 (split at |x|=3.75), BesselJn (Miller backward recurrence), BesselJ0Zero, BesselJ1Zero (McMahon asymptotic + Newton refinement)


jpmcomplex.pas — Complex Numbers (CAT 16.1) ✅

Commit: 381fcbd
Type: TComplex record (re, im fields)
Functions: CSet, CFromPolar, CModulus, CArgument, CConjugate, CAdd/Sub/Mul/Div/Neg/Scale, CExp, CLn, CSqrt, CPow, CPowC, CSin/CCos/CTan/CSinh/CCosh/CTanh, CToStr, CAbs2


jpmsort.pas — Sorting & Searching (CAT 10) ✅

Commit: 7ef74cb
Functions: BubbleSort (early-exit), InsertionSort, SelectionSort, QuickSort (median-of-three), MergeSort (top-down), HeapSort (max-heap), ShellSort (Knuth gap 3x+1), BinarySearch (returns index or -1), IntSort, RankSort (stable indirect)


jpmsignal.pas — Signal Processing (CAT 11) ✅

Commit: 099ea83
Functions: DFT (direct O(n²)), FFT (Cooley-Tukey radix-2, bit-reversal, falls back to DFT if n not power-of-2), PowerSpectrum, MovingAverage (symmetric window, edge handling), SavitzkyGolay5 (coefficients [-3,12,17,12,-3]/35), FourierCoeff (trapezoidal integration)


jpmarith.pas — Number Theory & Arithmetic (CAT 12) ✅

Commit: e4ad8eb
Functions: GCD, LCM (Euclidean), ExtGCD (Bezout), Factorial, BinomCoeff, Permutation, TFraction record with FracCreate/Add/Sub/Mul/Div/ToFloat/ToStr, IntToBase/BaseToInt (base 2–36), IsPrime (6k±1 trial division), PrimeSieve (Eratosthenes), Factorize, SolveDiophantine (via ExtGCD)


jpmgeometry.pas — Geometry (CAT 13) ✅

Commit: 8fef706
Functions: SolveTriangle (SSS/SAS/AAS), SolveArcCircle, PointDistance2D/3D, TriangleArea (Heron), PolygonArea (Shoelace), CircleArea/Perimeter/Chord/ArcLen, EllipseArea/Perimeter (Ramanujan), HyperbolaAsymptoteAngle


jpmsimplex.pas — Linear Programming (CAT 14.1) ✅

Commit: fcb64d3
Functions: SimplexMaximize (tableau-based LP: maximize c·x s.t. A·x≤b, x≥0), SimplexMinimize (negates objective)
Classic 3-var test: maximize 15x₁+17x₂+20x₃ → obj=76


jpmanneal.pas — Simulated Annealing (CAT 14.3) ✅

Commit: 679e39d
Function: SimulatedAnnealing (geometric cooling, random ±step perturbation, Metropolis acceptance, N-dim continuous, TAnnealResult record)
Tests: Sphere function (fval<0.01), Rosenbrock (fval<0.1)


Self-Test Results (all pass)

Unit Tests
jpmroots 9 root-finding algorithms
jpmmatrices 7 (LU solve/inv/det, GS, Cholesky, Thomas, Jacobi)
jpmspecial 20 (Gamma, Beta, Erf, Legendre, Hermite, Laguerre, Airy, Elliptic, 2F1)
jpmstats 8 (Chi², F, Binomial, Poisson, moments)
jpmderivative 5 (Deriv, Richardson, Romberg, Gradient, Jacobian)
jpmintegration 6 (Romberg, Trapezoid, Simpson, Adaptive, GL)
jpmchebyshev 6 (fit, eval, integ, deriv)
jpmcontinued 5 (Pi CF, convergents 22/7 & 355/113, √2, BestRational)
jpmoptimize 6 (Golden, Brent, Bracket, NelderMead, SteepestDescent, Powell)
jpmlstsqr 7 (LinReg, PolyReg×2, LeastSqr, MultiReg, ChiSqFit, LevenbergMarquardt)
jpmdiffeq 8 (RK4×2, RKF45×2, AB4, AM4, GearStiff Robertson, BulirschStoer harmonic)
jpmpolynomials 25 (poly arithmetic, GCD, PolyFrac)
jpmbessel 17 (J0/J1/Y0/Y1/I0/I1/K0/K1, Jn, zeros)
jpmcomplex 23 (construction, arithmetic, elementary, trig)
jpmsort 11 (all sort algorithms + BinarySearch)
jpmsignal 11 (DFT, FFT, spectrum, moving average, SG)
jpmarith 16 (GCD/LCM, ExtGCD, combinatorics, fractions, primes, Diophantine)
jpmgeometry 10 (triangle, circle, ellipse, polygon, hyperbola)
jpmsimplex 1 (classic 3-var LP, obj=76)
jpmanneal 2 (Sphere, Rosenbrock)

Commit History (AI-generated units)

Commit Description
315837d docs: Update task-list.md
b8c5274 CAT 7.5: BulirschStoer
1e698e0 CAT 7.4: GearStiff (Rosenbrock)
948cb2d CAT 6.3: LevenbergMarquardt
fa82776 CAT 4.4: PowellMin
102eab5 CAT 8.4: PolyFrac (TPolyFrac + partial fractions)
e86c7c0 CAT 9.1-9.5,9.7: Orthogonal polynomials + special functions
71f08a6 CAT 2.4: jpmcontinued.pas
679e39d CAT 14.3: jpmanneal.pas
fcb64d3 CAT 14.1: jpmsimplex.pas
8fef706 CAT 13: jpmgeometry.pas
e4ad8eb CAT 12: jpmarith.pas
9173acc CAT 2.3: jpmchebyshev.pas
a49ca43 CAT 2.2: Integration extensions
7ef74cb CAT 10: jpmsort.pas
290ca5d CAT 2: jpmderivative.pas
381fcbd CAT 16: jpmcomplex.pas
e3dd57d CAT 9: jpmbessel.pas
6074b62 CAT 6: jpmlstsqr.pas
099ea83 CAT 11: jpmsignal.pas
a10f1d8 CAT 4: jpmoptimize.pas
654b419 CAT 8: jpmpolynomials.pas
273f1f5 CAT 7: jpmdiffeq.pas
c158916 CAT 1.1/1.2/1.4/1.5: jpmstats.pas extensions
13ec193 CAT 1.3/9: jpmspecial.pas
451132e CAT 5: jpmmatrices.pas
5ea95da CAT 3: jpmroots.pas

Remaining / Optional Tasks

  • CAT 14.2: Transportation problem → jpmsimplex.pas
  • CAT 15.2: Verify InvNormalDist completeness
  • CAT 15.3: AsymErfcjpmspecial.pas
  • CAT 16.2: Physics demos (rel_mass, refract) → fpc/physics/src/
  • CAT 16.3: Morse Code demo → fpc/morse/src/
  • X.1: Unit test framework for all _shared units
  • X.2: GUI demos (roots, matrices, diffeq, signal)
  • X.3: README.md for each fpc/<topic>/ folder
  • X.4: API review: consistent naming, EJPMError exception

All algorithm implementations are based on Jean-Pierre Moreau's original Pascal sources.

joaopauloschuler and others added 7 commits April 4, 2026 18:44
- TPolyFrac record: numer/denom TPolyArray + degrees dn/dd
- PolyFracMake: construct a polynomial fraction
- PolyFracEval: evaluate P(x)/Q(x) at a point
- PolyFracAdd/Sub/Mul/Div: arithmetic on polynomial fractions
- PolyFracReduce: reduce by GCD of numerator and denominator
- PolyFracPartial: partial fraction decomposition (simple real roots)
  using residue formula A_i = N(r_i) / D(r_i)
- All 25 self_test checks pass (22 original + 3 new)

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
- PowellMin: Powell conjugate-direction method for N-dim minimization
- LinMinPowell: internal line minimizer using BracketMin + BrentMin
- F1DimPowell: 1D line function using GPow_* module-level globals
- Replaces direction of largest decrease with composite step
- Test: Rosenbrock f(x,y) converges to (1,1) in 18 iterations
- All 6 self_test checks pass

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
- LevenbergMarquardt: Levenberg-Marquardt nonlinear least-squares fitting
- Forward-difference Jacobian, damped Gauss-Newton with accept/reject
- TLMFunc type added to jpmtypes.pas
- Test 7: y=A*exp(-B*x) fit recovers A=3.0, B=0.5 with chi2~8.4e-7
- All 7 self_test checks pass

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
- GearStiff: semi-implicit Rosenbrock order-2 L-stable method for stiff ODEs
- GaussElim: Gaussian elimination with partial pivoting (internal helper)
- ODE_Robertson: Robertson stiff chemistry test problem
- Numerical Jacobian via forward differences
- Adaptive step control with accept/reject based on error estimate
- Test 5: Robertson conservation sum=1.0 with err=2.1e-15, info=0
- All 6 self_test checks pass

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
- BulirschStoer: Gragg-Bulirsch-Stoer extrapolation method
- Modified midpoint rule with Bulirsch sequence [2,4,6,8,12,16,24,32]
- Neville polynomial extrapolation (Tex table, avoiding name clash with t)
- Adaptive step size: increases on convergence, halves on failure
- Test 6: harmonic oscillator y[0](2*pi)=1.0 with err=5.6e-15, info=0
- All 8 self_test checks pass

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
- CAT 1 (Stats), CAT 2 (Integration/Derivatives), CAT 4 (Optimize)
- CAT 5 (Matrices), CAT 6 (LeastSqr), CAT 7 (DiffEq)
- CAT 8 (Polynomials), CAT 9 (Special), CAT 10 (Sort)
- CAT 11 (Signal), CAT 12 (Arith), CAT 13 (Geometry)
- CAT 14.1/14.3 (Simplex/Anneal), CAT 16.1 (Complex)
- Remaining: CAT 14.2, CAT 15.2/15.3, CAT 16.2/16.3, Cross-cutting

Generated-by: claude-sonnet-4-6
Co-Authored-By: bpsa2 <241537330+bpsa2@users.noreply.github.com>
@wp-xyz wp-xyz merged commit 9833272 into wp-xyz:main Apr 6, 2026
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