optirs-learned 0.3.1

OptiRS learned optimizers and meta-learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
// Second-order optimization methods with automatic differentiation
//
// This module implements advanced second-order optimization algorithms that
// leverage automatic differentiation for computing Hessians and curvature information.

use scirs2_core::ndarray::{Array, Array1, Array2, Axis};
use scirs2_core::numeric::Float;
use std::fmt::Debug;
use std::collections::VecDeque;

use crate::autodiff::{AutodiffEngine, HigherOrderGradients, HessianMatrix, HessianComputationStrategy, JacobianComputationStrategy};
#[allow(unused_imports)]
use crate::error::Result;
use optirs_core::optimizers::{Optimizer, OptimizerState};

/// Configuration for second-order optimization methods
#[derive(Debug, Clone)]
pub struct SecondOrderConfig {
    /// Learning rate
    pub learning_rate: f64,
    
    /// Hessian computation strategy
    pub hessian_strategy: HessianComputationStrategy,
    
    /// Damping parameter for regularization
    pub damping: f64,
    
    /// Trust region radius
    pub trust_region_radius: f64,
    
    /// Maximum number of CG iterations for Hessian inversion
    pub max_cg_iterations: usize,
    
    /// CG tolerance
    pub cg_tolerance: f64,
    
    /// Enable line search
    pub enable_line_search: bool,
    
    /// Line search parameters
    pub line_search_config: LineSearchConfig,
    
    /// Memory size for quasi-Newton methods
    pub memory_size: usize,
    
    /// Update frequency for Hessian computation
    pub hessian_update_frequency: usize,
    
    /// Enable curvature scaling
    pub enable_curvature_scaling: bool}

/// Line search configuration
#[derive(Debug, Clone)]
pub struct LineSearchConfig {
    /// Armijo condition parameter (c1)
    pub c1: f64,
    
    /// Curvature condition parameter (c2)  
    pub c2: f64,
    
    /// Maximum line search iterations
    pub max_iterations: usize,
    
    /// Initial step size
    pub initial_step: f64,
    
    /// Step size reduction factor
    pub reduction_factor: f64,
    
    /// Line search method
    pub method: LineSearchMethod}

/// Line search methods
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineSearchMethod {
    /// Backtracking line search
    Backtracking,
    
    /// Wolfe conditions
    Wolfe,
    
    /// Strong Wolfe conditions
    StrongWolfe,
    
    /// More-Thuente line search
    MoreThuente,
    
    /// Exact line search (for quadratic functions)
    Exact}

/// Newton's method optimizer with automatic differentiation
pub struct NewtonOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: SecondOrderConfig,
    
    /// Higher-order gradient computer
    hog: HigherOrderGradients<T>,
    
    /// Current iteration
    iteration: usize,
    
    /// Convergence history
    convergence_history: VecDeque<ConvergenceMetrics<T>>,
    
    /// Last computed Hessian
    last_hessian: Option<HessianMatrix<T>>,
    
    /// Trust region state
    trust_region_state: TrustRegionState<T>}

/// Quasi-Newton optimizer (BFGS/L-BFGS)
pub struct QuasiNewtonOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: SecondOrderConfig,
    
    /// BFGS memory
    bfgs_memory: BFGSMemory<T>,
    
    /// Higher-order gradient computer
    hog: HigherOrderGradients<T>,
    
    /// Current iteration
    iteration: usize,
    
    /// Convergence history
    convergence_history: VecDeque<ConvergenceMetrics<T>>}

/// K-FAC (Kronecker-Factored Approximate Curvature) optimizer
pub struct KFACOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: SecondOrderConfig,
    
    /// KFAC state
    kfac_state: KFACState<T>,
    
    /// Higher-order gradient computer
    hog: HigherOrderGradients<T>,
    
    /// Current iteration
    iteration: usize,
    
    /// Layer information for neural networks
    layer_info: Vec<LayerInfo>}

/// Natural gradient optimizer
pub struct NaturalGradientOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: SecondOrderConfig,
    
    /// Fisher information matrix
    fisher_matrix: Option<Array2<T>>,
    
    /// Higher-order gradient computer
    hog: HigherOrderGradients<T>,
    
    /// Current iteration
    iteration: usize,
    
    /// Running average of Fisher information
    fisher_ema_decay: T}

/// Trust region state
#[derive(Debug, Clone)]
pub struct TrustRegionState<T: Float + Debug + Send + Sync + 'static> {
    /// Current trust region radius
    pub radius: T,
    
    /// Trust region history
    pub radius_history: VecDeque<T>,
    
    /// Reduction ratio threshold for expanding radius
    pub expand_threshold: T,
    
    /// Reduction ratio threshold for shrinking radius
    pub shrink_threshold: T,
    
    /// Expansion factor
    pub expand_factor: T,
    
    /// Shrinking factor
    pub shrink_factor: T}

/// BFGS memory for quasi-Newton methods
#[derive(Debug, Clone)]
pub struct BFGSMemory<T: Float + Debug + Send + Sync + 'static> {
    /// Memory size
    pub memory_size: usize,
    
    /// Gradient differences (y_k = g_{k+1} - g_k)
    pub y_history: VecDeque<Array1<T>>,
    
    /// Parameter differences (s_k = x_{k+1} - x_k)
    pub s_history: VecDeque<Array1<T>>,
    
    /// Scaling factors (rho_k = 1 / (y_k^T s_k))
    pub rho_history: VecDeque<T>,
    
    /// Initial Hessian approximation scale
    pub initial_scale: T}

/// KFAC state for neural network optimization
#[derive(Debug, Clone)]
pub struct KFACState<T: Float + Debug + Send + Sync + 'static> {
    /// A matrices (input covariances)
    pub a_matrices: Vec<Array2<T>>,
    
    /// G matrices (gradient covariances)
    pub g_matrices: Vec<Array2<T>>,
    
    /// Inverse A matrices
    pub inv_a_matrices: Vec<Array2<T>>,
    
    /// Inverse G matrices
    pub inv_g_matrices: Vec<Array2<T>>,
    
    /// Damping parameter
    pub damping: T,
    
    /// Update frequency
    pub update_frequency: usize,
    
    /// EMA decay for covariance matrices
    pub ema_decay: T}

/// Layer information for KFAC
#[derive(Debug, Clone)]
pub struct LayerInfo {
    /// Layer name
    pub name: String,
    
    /// Input dimension
    pub input_dim: usize,
    
    /// Output dimension
    pub output_dim: usize,
    
    /// Layer type
    pub layer_type: LayerType}

/// Types of neural network layers
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LayerType {
    /// Fully connected layer
    FullyConnected,
    
    /// Convolutional layer
    Convolutional,
    
    /// Batch normalization layer
    BatchNorm,
    
    /// Activation layer
    Activation}

/// Convergence metrics for optimization
#[derive(Debug, Clone)]
pub struct ConvergenceMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Iteration number
    pub iteration: usize,
    
    /// Objective function value
    pub objective_value: T,
    
    /// Gradient norm
    pub gradient_norm: T,
    
    /// Parameter change norm
    pub parameter_change_norm: T,
    
    /// Hessian condition number
    pub condition_number: Option<T>,
    
    /// Trust region radius (if applicable)
    pub trust_region_radius: Option<T>,
    
    /// Line search step size
    pub step_size: T,
    
    /// Convergence status
    pub converged: bool}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> NewtonOptimizer<T> {
    /// Create new Newton optimizer
    pub fn new(
        config: SecondOrderConfig,
        autodiff_engine: AutodiffEngine<T>,
    ) -> Self {
        let hog = HigherOrderGradients::new(
            autodiff_engine,
            2, // Second-order
            config.hessian_strategy,
            JacobianComputationStrategy::Reverse,
        );
        
        let trust_region_state = TrustRegionState {
            radius: scirs2_core::numeric::NumCast::from(config.trust_region_radius).unwrap_or_else(|| T::zero()),
            radius_history: VecDeque::new(),
            expand_threshold: scirs2_core::numeric::NumCast::from(0.75).unwrap_or_else(|| T::zero()),
            shrink_threshold: scirs2_core::numeric::NumCast::from(0.25).unwrap_or_else(|| T::zero()),
            expand_factor: scirs2_core::numeric::NumCast::from(2.0).unwrap_or_else(|| T::zero()),
            shrink_factor: scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero())};
        
        Self {
            config,
            hog,
            iteration: 0,
            convergence_history: VecDeque::new(),
            last_hessian: None,
            trust_region_state}
    }
    
    /// Perform one optimization step
    pub fn step(
        &mut self,
        params: &Array1<T>,
        gradients: &Array1<T>,
        objective_fn: impl Fn(&Array1<T>) -> T,
    ) -> Result<Array1<T>> {
        self.iteration += 1;
        
        // Compute Hessian if needed
        let should_compute_hessian = self.iteration % self.config.hessian_update_frequency == 0 
            || self.last_hessian.is_none();
        
        if should_compute_hessian {
            let output_id = 0; // Simplified
            let input_ids: Vec<usize> = (0..params.len()).collect();
            
            let higher_order_results = self.hog.compute_gradients(output_id, &input_ids, 2)?;
            self.last_hessian = higher_order_results.hessian;
        }
        
        // Solve Newton system: H * p = -g
        let search_direction = self.solve_newton_system(gradients)?;
        
        // Apply trust region constraint
        let constrained_direction = self.apply_trust_region_constraint(&search_direction);
        
        // Line search or trust region step
        let step_size = if self.config.enable_line_search {
            self.line_search(params, &constrained_direction, &objective_fn)?
        } else {
            T::one()
        };
        
        let new_params = params + &(constrained_direction * step_size);
        
        // Update trust region radius
        self.update_trust_region_radius(params, &new_params, &objective_fn)?;
        
        // Record convergence metrics
        self.record_convergence_metrics(params, &new_params, gradients, &objective_fn);
        
        Ok(new_params)
    }
    
    /// Solve Newton system H * p = -g
    fn solve_newton_system(&mut self, gradients: &Array1<T>) -> Result<Array1<T>> {
        if let Some(ref hessian) = self.last_hessian {
            match &hessian.full_matrix {
                Some(h_matrix) => {
                    // Add damping for numerical stability
                    let damped_hessian = h_matrix + &(Array2::eye(h_matrix.nrows()) * scirs2_core::numeric::NumCast::from(self.config.damping).unwrap_or_else(|| T::zero()));
                    
                    // Solve using conjugate gradient method
                    self.solve_cg(&damped_hessian, &(-gradients))
                }
                None => {
                    // Use diagonal approximation or gradient descent
                    if let Some(ref diagonal) = hessian.diagonal {
                        let inv_diag = diagonal.mapv(|x| T::one() / (x + scirs2_core::numeric::NumCast::from(self.config.damping).unwrap_or_else(|| T::zero())));
                        Ok(&(-gradients) * &inv_diag)
                    } else {
                        // Fallback to gradient descent
                        Ok(-gradients * scirs2_core::numeric::NumCast::from(self.config.learning_rate).unwrap_or_else(|| T::zero()))
                    }
                }
            }
        } else {
            // No Hessian available, use gradient descent
            Ok(-gradients * scirs2_core::numeric::NumCast::from(self.config.learning_rate).unwrap_or_else(|| T::zero()))
        }
    }
    
    /// Solve linear system using conjugate gradient method
    fn solve_cg(&self, matrix: &Array2<T>, rhs: &Array1<T>) -> Result<Array1<T>> {
        let n = rhs.len();
        let mut x = Array1::zeros(n);
        let mut r = rhs.clone();
        let mut p = r.clone();
        let mut rsold = r.dot(&r);
        
        for _ in 0..self.config.max_cg_iterations {
            let ap = matrix.dot(&p);
            let alpha = rsold / p.dot(&ap);
            
            x = &x + &(&p * alpha);
            r = &r - &(&ap * alpha);
            
            let rsnew = r.dot(&r);
            
            if rsnew.sqrt() < scirs2_core::numeric::NumCast::from(self.config.cg_tolerance).unwrap_or_else(|| T::zero()) {
                break;
            }
            
            let beta = rsnew / rsold;
            p = &r + &(&p * beta);
            rsold = rsnew;
        }
        
        Ok(x)
    }
    
    /// Apply trust region constraint to search direction
    fn apply_trust_region_constraint(&self, direction: &Array1<T>) -> Array1<T> {
        let direction_norm = direction.dot(direction).sqrt();
        
        if direction_norm <= self.trust_region_state.radius {
            direction.clone()
        } else {
            direction * (self.trust_region_state.radius / direction_norm)
        }
    }
    
    /// Perform line search
    fn line_search(
        &self,
        params: &Array1<T>,
        direction: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) -> Result<T> {
        match self.config.line_search_config.method {
            LineSearchMethod::Backtracking => {
                self.backtracking_line_search(params, direction, objective_fn)
            }
            LineSearchMethod::Wolfe => {
                self.wolfe_line_search(params, direction, objective_fn)
            }
            _ => {
                // Fallback to fixed step size
                Ok(scirs2_core::numeric::NumCast::from(self.config.line_search_config.initial_step).unwrap_or_else(|| T::zero()))
            }
        }
    }
    
    /// Backtracking line search
    fn backtracking_line_search(
        &self,
        params: &Array1<T>,
        direction: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) -> Result<T> {
        let mut step_size = scirs2_core::numeric::NumCast::from(self.config.line_search_config.initial_step).unwrap_or_else(|| T::zero());
        let c1 = scirs2_core::numeric::NumCast::from(self.config.line_search_config.c1).unwrap_or_else(|| T::zero());
        let reduction_factor = scirs2_core::numeric::NumCast::from(self.config.line_search_config.reduction_factor).unwrap_or_else(|| T::zero());
        
        let initial_value = objective_fn(params);
        let directional_derivative = T::zero(); // Would compute actual directional derivative
        
        for _ in 0..self.config.line_search_config.max_iterations {
            let new_params = params + &(direction * step_size);
            let new_value = objective_fn(&new_params);
            
            // Armijo condition
            if new_value <= initial_value + c1 * step_size * directional_derivative {
                return Ok(step_size);
            }
            
            step_size = step_size * reduction_factor;
        }
        
        Ok(step_size)
    }
    
    /// Wolfe line search  
    fn wolfe_line_search(
        &self,
        params: &Array1<T>,
        direction: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) -> Result<T> {
        // Simplified Wolfe line search
        // In practice, would implement full strong Wolfe conditions
        self.backtracking_line_search(params, direction, objective_fn)
    }
    
    /// Update trust region radius based on reduction ratio
    fn update_trust_region_radius(
        &mut self,
        old_params: &Array1<T>,
        new_params: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) -> Result<()> {
        let old_value = objective_fn(old_params);
        let new_value = objective_fn(new_params);
        let actual_reduction = old_value - new_value;
        
        // Compute predicted reduction using quadratic model
        let predicted_reduction = if let Some(ref hessian) = self.last_hessian {
            // Quadratic model: m(p) = f + g^T p + 0.5 p^T H p
            let delta = new_params - old_params;
            let grad_term = T::zero(); // Would use actual gradient
            let hess_term = if let Some(ref h_matrix) = hessian.full_matrix {
                scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero()) * delta.dot(&h_matrix.dot(&delta))
            } else {
                T::zero()
            };
            grad_term + hess_term
        } else {
            T::one() // Fallback
        };
        
        let reduction_ratio = if predicted_reduction != T::zero() {
            actual_reduction / predicted_reduction
        } else {
            T::zero()
        };
        
        // Update radius based on reduction ratio
        if reduction_ratio > self.trust_region_state.expand_threshold {
            self.trust_region_state.radius = self.trust_region_state.radius * self.trust_region_state.expand_factor;
        } else if reduction_ratio < self.trust_region_state.shrink_threshold {
            self.trust_region_state.radius = self.trust_region_state.radius * self.trust_region_state.shrink_factor;
        }
        
        // Record radius history
        self.trust_region_state.radius_history.push_back(self.trust_region_state.radius);
        if self.trust_region_state.radius_history.len() > 100 {
            self.trust_region_state.radius_history.pop_front();
        }
        
        Ok(())
    }
    
    /// Record convergence metrics
    fn record_convergence_metrics(
        &mut self,
        old_params: &Array1<T>,
        new_params: &Array1<T>,
        gradients: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) {
        let objective_value = objective_fn(new_params);
        let gradient_norm = gradients.dot(gradients).sqrt();
        let parameter_change_norm = (new_params - old_params).dot(&(new_params - old_params)).sqrt();
        
        let condition_number = if let Some(ref hessian) = self.last_hessian {
            hessian.condition_number
        } else {
            None
        };
        
        let converged = gradient_norm < scirs2_core::numeric::NumCast::from(1e-6).unwrap_or_else(|| T::zero()) 
            && parameter_change_norm < scirs2_core::numeric::NumCast::from(1e-8).unwrap_or_else(|| T::zero());
        
        let metrics = ConvergenceMetrics {
            iteration: self.iteration,
            objective_value,
            gradient_norm,
            parameter_change_norm,
            condition_number,
            trust_region_radius: Some(self.trust_region_state.radius),
            step_size: T::one(), // Would track actual step size
            converged};
        
        self.convergence_history.push_back(metrics);
        if self.convergence_history.len() > 1000 {
            self.convergence_history.pop_front();
        }
    }
    
    /// Check convergence
    pub fn has_converged(&self) -> bool {
        self.convergence_history.back()
            .map_or(false, |metrics| metrics.converged)
    }
    
    /// Get convergence history
    pub fn convergence_history(&self) -> &VecDeque<ConvergenceMetrics<T>> {
        &self.convergence_history
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> QuasiNewtonOptimizer<T> {
    /// Create new quasi-Newton optimizer
    pub fn new(
        config: SecondOrderConfig,
        autodiff_engine: AutodiffEngine<T>,
    ) -> Self {
        let hog = HigherOrderGradients::new(
            autodiff_engine,
            1, // First-order only
            HessianComputationStrategy::BFGS,
            JacobianComputationStrategy::Reverse,
        );
        
        let bfgs_memory = BFGSMemory {
            memory_size: config.memory_size,
            y_history: VecDeque::new(),
            s_history: VecDeque::new(),
            rho_history: VecDeque::new(),
            initial_scale: T::one()};
        
        Self {
            config,
            bfgs_memory,
            hog,
            iteration: 0,
            convergence_history: VecDeque::new()}
    }
    
    /// Perform BFGS update step
    pub fn step(
        &mut self,
        params: &Array1<T>,
        gradients: &Array1<T>,
        objective_fn: impl Fn(&Array1<T>) -> T,
    ) -> Result<Array1<T>> {
        self.iteration += 1;
        
        // Compute search direction using L-BFGS two-loop recursion
        let search_direction = self.compute_lbfgs_direction(gradients)?;
        
        // Line search
        let step_size = if self.config.enable_line_search {
            self.line_search(params, &search_direction, &objective_fn)?
        } else {
            scirs2_core::numeric::NumCast::from(self.config.learning_rate).unwrap_or_else(|| T::zero())
        };
        
        let new_params = params + &(search_direction * step_size);
        
        // Update BFGS memory
        if self.iteration > 1 {
            let s = &new_params - params;
            // Note: would need previous gradient for y = grad_new - grad_old
            self.update_bfgs_memory(&s, gradients)?;
        }
        
        Ok(new_params)
    }
    
    /// Compute L-BFGS search direction using two-loop recursion
    fn compute_lbfgs_direction(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        if self.bfgs_memory.s_history.is_empty() {
            // No history, use steepest descent
            return Ok(-gradients * scirs2_core::numeric::NumCast::from(self.config.learning_rate).unwrap_or_else(|| T::zero()));
        }
        
        let mut q = gradients.clone();
        let mut alphas = Vec::new();
        
        // First loop (backward)
        for i in (0..self.bfgs_memory.s_history.len()).rev() {
            let rho_i = self.bfgs_memory.rho_history[i];
            let s_i = &self.bfgs_memory.s_history[i];
            let alpha_i = rho_i * s_i.dot(&q);
            alphas.push(alpha_i);
            q = &q - &(&self.bfgs_memory.y_history[i] * alpha_i);
        }
        
        alphas.reverse();
        
        // Scale initial Hessian approximation
        let mut r = &q * self.bfgs_memory.initial_scale;
        
        // Second loop (forward)
        for i in 0..self.bfgs_memory.s_history.len() {
            let rho_i = self.bfgs_memory.rho_history[i];
            let y_i = &self.bfgs_memory.y_history[i];
            let s_i = &self.bfgs_memory.s_history[i];
            let beta = rho_i * y_i.dot(&r);
            r = &r + &(s_i * (alphas[i] - beta));
        }
        
        Ok(-r)
    }
    
    /// Update BFGS memory with new curvature information
    fn update_bfgs_memory(&mut self, s: &Array1<T>, y: &Array1<T>) -> Result<()> {
        let sy = s.dot(y);
        
        // Skip update if curvature condition is not satisfied
        if sy <= scirs2_core::numeric::NumCast::from(1e-10).unwrap_or_else(|| T::zero()) {
            return Ok(());
        }
        
        let rho = T::one() / sy;
        
        // Add to memory
        self.bfgs_memory.s_history.push_back(s.clone());
        self.bfgs_memory.y_history.push_back(y.clone());
        self.bfgs_memory.rho_history.push_back(rho);
        
        // Limit memory size
        if self.bfgs_memory.s_history.len() > self.bfgs_memory.memory_size {
            self.bfgs_memory.s_history.pop_front();
            self.bfgs_memory.y_history.pop_front();
            self.bfgs_memory.rho_history.pop_front();
        }
        
        // Update initial scale (using last curvature pair)
        let yy = y.dot(y);
        if yy > T::zero() {
            self.bfgs_memory.initial_scale = sy / yy;
        }
        
        Ok(())
    }
    
    /// Line search implementation
    fn line_search(
        &self,
        params: &Array1<T>,
        direction: &Array1<T>,
        objective_fn: &impl Fn(&Array1<T>) -> T,
    ) -> Result<T> {
        // Simplified line search
        let mut step_size = T::one();
        let reduction_factor = scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero());
        
        let initial_value = objective_fn(params);
        
        for _ in 0..20 {
            let new_params = params + &(direction * step_size);
            let new_value = objective_fn(&new_params);
            
            if new_value < initial_value {
                return Ok(step_size);
            }
            
            step_size = step_size * reduction_factor;
        }
        
        Ok(step_size)
    }
}

impl Default for SecondOrderConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            hessian_strategy: HessianComputationStrategy::BFGS,
            damping: 1e-4,
            trust_region_radius: 1.0,
            max_cg_iterations: 100,
            cg_tolerance: 1e-8,
            enable_line_search: true,
            line_search_config: LineSearchConfig::default(),
            memory_size: 10,
            hessian_update_frequency: 1,
            enable_curvature_scaling: true}
    }
}

impl Default for LineSearchConfig {
    fn default() -> Self {
        Self {
            c1: 1e-4,
            c2: 0.9,
            max_iterations: 20,
            initial_step: 1.0,
            reduction_factor: 0.5,
            method: LineSearchMethod::Backtracking}
    }
}

/// Utility functions for second-order methods
pub mod utils {
    use super::*;
    
    /// Compute condition number of Hessian
    pub fn compute_condition_number<T: Float + Debug + Send + Sync + 'static>(hessian: &HessianMatrix<T>) -> Option<T> {
        if let Some(ref eigenvalues) = hessian.eigenvalues {
            let max_eigenval = eigenvalues.iter().fold(T::neg_infinity(), |a, &b| a.max(b));
            let min_eigenval = eigenvalues.iter().fold(T::infinity(), |a, &b| a.min(b));
            
            if min_eigenval > T::zero() {
                Some(max_eigenval / min_eigenval)
            } else {
                None
            }
        } else {
            None
        }
    }
    
    /// Check if Hessian is positive definite
    pub fn is_positive_definite<T: Float + Debug + Send + Sync + 'static>(hessian: &HessianMatrix<T>) -> bool {
        if let Some(ref eigenvalues) = hessian.eigenvalues {
            eigenvalues.iter().all(|&val| val > T::zero())
        } else if let Some(ref diagonal) = hessian.diagonal {
            diagonal.iter().all(|&val| val > T::zero())
        } else {
            false
        }
    }
    
    /// Add damping to Hessian for numerical stability
    pub fn add_damping<T: Float + Debug + Send + Sync + 'static>(hessian: &mut Array2<T>, damping: T) {
        for i in 0.._hessian.nrows() {
            hessian[[i, i]] = hessian[[i, i]] + damping;
        }
    }
    
    /// Compute optimal damping parameter using Levenberg-Marquardt heuristic
    pub fn compute_optimal_damping<T: Float + Debug + Send + Sync + 'static>(
        hessian: &HessianMatrix<T>,
        gradient_norm: T,
    ) -> T {
        if let Some(ref diagonal) = hessian.diagonal {
            let max_diag = diagonal.iter().fold(T::neg_infinity(), |a, &b| a.max(b));
            let adaptive_damping = gradient_norm * scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero());
            adaptive_damping.max(max_diag * scirs2_core::numeric::NumCast::from(1e-6).unwrap_or_else(|| T::zero()))
        } else {
            gradient_norm * scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::autodiff::{AutodiffConfig, AutodiffEngine};
    
    #[test]
    fn test_second_order_config_default() {
        let config = SecondOrderConfig::default();
        assert_eq!(config.learning_rate, 1e-3);
        assert!(config.enable_line_search);
        assert_eq!(config.memory_size, 10);
    }
    
    #[test]
    fn test_line_search_config() {
        let config = LineSearchConfig::default();
        assert_eq!(config.c1, 1e-4);
        assert_eq!(config.c2, 0.9);
        assert_eq!(config.method, LineSearchMethod::Backtracking);
    }
    
    #[test]
    fn test_newton_optimizer_creation() {
        let config = SecondOrderConfig::default();
        let autodiff_config = AutodiffConfig::default();
        let engine = AutodiffEngine::<f64>::new(autodiff_config);
        
        let optimizer = NewtonOptimizer::new(config, engine);
        assert_eq!(optimizer.iteration, 0);
    }
    
    #[test]
    fn test_bfgs_memory() {
        let memory = BFGSMemory::<f64> {
            memory_size: 5,
            y_history: VecDeque::new(),
            s_history: VecDeque::new(),
            rho_history: VecDeque::new(),
            initial_scale: 1.0};
        
        assert_eq!(memory.memory_size, 5);
        assert!(memory.y_history.is_empty());
        assert!(memory.s_history.is_empty());
    }
    
    #[test]
    fn test_trust_region_state() {
        let state = TrustRegionState {
            radius: 1.0f64,
            radius_history: VecDeque::new(),
            expand_threshold: 0.75,
            shrink_threshold: 0.25,
            expand_factor: 2.0,
            shrink_factor: 0.5};
        
        assert_eq!(state.radius, 1.0);
        assert_eq!(state.expand_factor, 2.0);
        assert_eq!(state.shrink_factor, 0.5);
    }
}