Advanced User Tutorials

These tutorials are for developers and scientists who want to leverage NLSQ’s full API capabilities: custom optimization pipelines, protocol-based design, performance tuning, and extending the library.

Note

Prerequisites: Complete the Routine User Tutorials first. Understanding the 3-workflow system is essential before diving into the API layer.

Who This Guide Is For

  • Developers building custom optimization workflows

  • Scientists with specialized fitting requirements

  • Researchers extending NLSQ for new algorithms

  • Power users wanting maximum control

What You’ll Learn

  • NLSQ’s internal architecture and design patterns

  • How to use core API classes directly

  • Factory functions and dependency injection

  • The v0.6.4 orchestration component system

  • Performance tuning and profiling

  • Creating custom optimizers and extensions

Learning Path

#

Chapter

What You’ll Learn

Time

1

Architecture Overview

Package structure, optimization pipeline, JAX patterns

30 min

2

Core APIs

CurveFit, LeastSquares, TRF classes

45 min

3

Factories and Dependency Injection

Factory functions, protocols, dependency injection

30 min

4

Orchestration Components (v0.6.4)

v0.6.4 decomposed components

45 min

5

Custom Workflows

Build your own optimization pipelines

45 min

6

Performance Optimization

JIT caching, memory management, profiling

30 min

7

Numerical Stability

Numerical guards, SVD fallback, recovery

30 min

8

Extending NLSQ

Custom protocols, plugins, testing

30 min

Prerequisites

  • Completed routine user tutorials

  • Python experience (classes, decorators, type hints)

  • Understanding of optimization concepts

  • Familiarity with JAX (helpful but not required)

Quick Reference: API Levels

NLSQ provides multiple API levels:

High Level    fit() function
     │           │
     │           ▼
     │     CurveFit class
     │           │
     │           ▼
Mid Level  LeastSquares class
     │           │
     │           ▼
Low Level  TrustRegionReflective

Choose based on needs:

  • fit(): Simple, automatic (routine users)

  • CurveFit: Reusable, stateful fitting

  • LeastSquares: Direct optimizer control

  • TRF: Algorithm-level customization

Import Patterns

# High-level API
from nlsq import fit, curve_fit, CurveFit

# Core classes
from nlsq.core.least_squares import LeastSquares
from nlsq.core.trf import TrustRegionReflective

# Factories
from nlsq.core.factories import create_optimizer, configure_curve_fit

# Orchestration (v0.6.4+)
from nlsq.core.orchestration import (
    DataPreprocessor,
    OptimizationSelector,
    CovarianceComputer,
    StreamingCoordinator,
)

# Protocols
from nlsq.interfaces import (
    OptimizerProtocol,
    CurveFitProtocol,
    CacheProtocol,
)

# Facades
from nlsq.facades import (
    OptimizationFacade,
    StabilityFacade,
    DiagnosticsFacade,
)