RustedSciThe 0.4.21

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
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
//! Comprehensive guide for solving nonlinear systems of equations
//!
//! This example demonstrates how to use the LM (Levenberg-Marquardt) solver
//! to solve various nonlinear systems using both the traditional API and
//! the new builder pattern.
//!
//! The solver automatically generates analytical Jacobians from symbolic
//! expressions, providing efficient and accurate solutions.
//!
//! run cargo run --example nonlinear_systems_guide
//!

use RustedSciThe::numerical::Nonlinear_systems::least_squares::symbolic_solver::SymbolicLeastSquaresSolver;
use RustedSciThe::symbolic::symbolic_engine::Expr;

fn main() {
    println!("=== Nonlinear Systems Solver Guide ===");
    println!("This guide demonstrates both string-based and native symbolic construction\n");

    // Example 1: Basic usage with builder pattern (string equations)
    example_1_builder_string();

    // Example 2: Builder pattern with Expr objects
    example_2_builder_expr();

    // Example 3: Native symbolic construction (no string parsing)
    example_3_native_symbolic();

    // Example 4: Traditional API (for comparison)
    example_4_traditional_api();

    // Example 5: Rosenbrock function
    example_4_rosenbrock();

    // Example 6: Native symbolic exponential system
    example_6_native_exponential();

    // Example 7: Native symbolic trigonometric system
    example_7_native_trigonometric();

    // Example 8: 3D system with native symbolic
    example_8_native_three_dimensional();

    // Example 9: Parametric system with native symbolic
    example_9_native_parametric();

    // Example 10: Complex native symbolic expression
    example_10_complex_native();

    println!("\n=== All examples completed ===");
}

fn example_1_builder_string() {
    println!("Example 1: Basic Builder Pattern with String Equations");
    println!("-------------------------------------------------------");
    println!("System: x² + y² = 1, x = y");
    println!("Expected solution: x = y = ±√2/2 ≈ ±0.707\n");

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations_str(vec!["x^2 + y^2 - 1".to_string(), "x - y".to_string()])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Verification: x² + y² = {:.6}",
            map["x"].powi(2) + map["y"].powi(2)
        );
    }
    println!();
}

fn example_2_builder_expr() {
    println!("Example 2: Builder Pattern with Expr Objects");
    println!("---------------------------------------------");
    println!("System: x² + y² = 1, x = y\n");

    let eq1 = Expr::parse_expression("x^2 + y^2 - 1");
    let eq2 = Expr::parse_expression("x - y");

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_f_tolerance(1e-10)
        .with_g_tolerance(1e-10)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution: x = {:.6}, y = {:.6}\n", map["x"], map["y"]);
    }
}

fn example_3_native_symbolic() {
    println!("Example 3: Native Symbolic Construction (No String Parsing)");
    println!("-----------------------------------------------------------");
    println!("System: x² + y² = 1, x = y");
    println!("Using Expr::Symbols() and native operations\n");

    // Create symbolic variables
    let vars = Expr::Symbols("x, y");
    let x = vars[0].clone();
    let y = vars[1].clone();

    // Build equations using native symbolic operations
    let eq1 = x.clone().pow(Expr::Const(2.0)) + y.clone().pow(Expr::Const(2.0)) - Expr::Const(1.0);
    let eq2 = x.clone() - y.clone();

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_f_tolerance(1e-10)
        .with_g_tolerance(1e-10)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Verification: x² + y² = {:.6}",
            map["x"].powi(2) + map["y"].powi(2)
        );
    }
    println!();
}

fn example_4_traditional_api() {
    println!("Example 3: Traditional API (for comparison)");
    println!("--------------------------------------------");
    println!("System: x² + y² = 1, x = y\n");

    let mut solver = SymbolicLeastSquaresSolver::new()
        .with_equations_str(vec!["x^2 + y^2 - 1".to_string(), "x - y".to_string()])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution: x = {:.6}, y = {:.6}\n", map["x"], map["y"]);
    }
}

fn example_4_rosenbrock() {
    println!("Example 4: Rosenbrock Function");
    println!("-------------------------------");
    println!("System: 10(y - x²) = 0, 1 - x = 0");
    println!("Expected solution: x = 1, y = 1\n");

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations_str(vec!["10*(y - x^2)".to_string(), "1 - x".to_string()])
        .with_initial_guess(vec![-1.2, 1.0])
        .with_tolerance(1e-8)
        .with_max_iterations(200)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Error from (1,1): {:.2e}",
            ((map["x"] - 1.0).powi(2) + (map["y"] - 1.0).powi(2)).sqrt()
        );
    }
    println!();
}

fn example_6_native_exponential() {
    println!("Example 6: Native Symbolic Exponential System");
    println!("----------------------------------------------");
    println!("System: exp(x) + y = 3, x + exp(y) = 3");
    println!("Using native Expr::exp() construction\n");

    let vars = Expr::Symbols("x, y");
    let x = vars[0].clone();
    let y = vars[1].clone();

    // Build equations with exponentials
    let eq1 = Expr::exp(x.clone()) + y.clone() - Expr::Const(3.0);
    let eq2 = x.clone() + Expr::exp(y.clone()) - Expr::Const(3.0);

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_initial_guess(vec![0.5, 0.5])
        .with_f_tolerance(1e-8)
        .with_g_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Symmetry check |x - y| = {:.2e}",
            (map["x"] - map["y"]).abs()
        );
        println!(
            "  Verification: exp(x) + y = {:.6}",
            map["x"].exp() + map["y"]
        );
    }
    println!();
}

fn example_7_native_trigonometric() {
    println!("Example 7: Native Symbolic Trigonometric System");
    println!("------------------------------------------------");
    println!("System: sin(x) + cos(y) = 1, cos(x) - sin(y) = 0");
    println!("Using native Expr::sin() and Expr::cos()\n");

    let vars = Expr::Symbols("x, y");
    let x = vars[0].clone();
    let y = vars[1].clone();

    // Build equations with trig functions
    let eq1 = Expr::sin(Box::new(x.clone())) + Expr::cos(Box::new(y.clone())) - Expr::Const(1.0);
    let eq2 = Expr::cos(Box::new(x.clone())) - Expr::sin(Box::new(y.clone()));

    let mut solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-7)
        .with_f_tolerance(1e-8)
        .with_g_tolerance(1e-8)
        .with_max_iterations(150)
        .with_loglevel("none".to_string())
        .build();
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!("  Verification:");
        println!(
            "    sin(x) + cos(y) = {:.6}",
            map["x"].sin() + map["y"].cos()
        );
        println!(
            "    cos(x) - sin(y) = {:.6}",
            map["x"].cos() - map["y"].sin()
        );
    }
    println!();
}

fn example_8_native_three_dimensional() {
    println!("Example 8: Native Symbolic Three-Dimensional System");
    println!("----------------------------------------------------");
    println!("System: x² + y² + z² = 1, x·y + z = 0.5, x + y + z = 1\n");

    let vars = Expr::Symbols("x, y, z");
    let x = vars[0].clone();
    let y = vars[1].clone();
    let z = vars[2].clone();

    // Build 3D system
    let eq1 = x.clone().pow(Expr::Const(2.0))
        + y.clone().pow(Expr::Const(2.0))
        + z.clone().pow(Expr::Const(2.0))
        - Expr::Const(1.0);
    let eq2 = x.clone() * y.clone() + z.clone() - Expr::Const(0.5);
    let eq3 = x.clone() + y.clone() + z.clone() - Expr::Const(1.0);

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2, eq3])
        .with_unknowns(vec!["x".to_string(), "y".to_string(), "z".to_string()])
        .with_initial_guess(vec![0.3, 0.3, 0.4])
        .with_tolerance(1e-7)
        .with_max_iterations(200)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!("  z = {:.6}", map["z"]);
        println!("  Verification:");
        println!(
            "    x² + y² + z² = {:.6}",
            map["x"].powi(2) + map["y"].powi(2) + map["z"].powi(2)
        );
        println!("    x·y + z = {:.6}", map["x"] * map["y"] + map["z"]);
        println!("    x + y + z = {:.6}", map["x"] + map["y"] + map["z"]);
    }
    println!();
}

fn example_9_native_parametric() {
    println!("Example 9: Native Symbolic Parametric System");
    println!("---------------------------------------------");
    println!("System: a·x² + b·y² = 1, x = y");
    println!("Using native symbolic parameters\n");

    let vars = Expr::Symbols("x, y");
    let params = Expr::Symbols("a, b");
    let x = vars[0].clone();
    let y = vars[1].clone();
    let a = params[0].clone();
    let b = params[1].clone();

    // Build parametric equations
    let eq1 = a * x.clone().pow(Expr::Const(2.0)) + b * y.clone().pow(Expr::Const(2.0))
        - Expr::Const(1.0);
    let eq2 = x.clone() - y.clone();

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_parameters(vec!["a".to_string(), "b".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve_with_params(vec![1.0, 1.0]);

    if let Some(map) = solver.map_of_solutions {
        println!("Solution with a=1, b=1:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
    }

    // Solve again with different parameters
    let vars2 = Expr::Symbols("x, y");
    let params2 = Expr::Symbols("a, b");
    let x2 = vars2[0].clone();
    let y2 = vars2[1].clone();
    let a2 = params2[0].clone();
    let b2 = params2[1].clone();

    let eq1_2 = a2 * x2.clone().pow(Expr::Const(2.0)) + b2 * y2.clone().pow(Expr::Const(2.0))
        - Expr::Const(1.0);
    let eq2_2 = x2.clone() - y2.clone();

    let solver2 = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1_2, eq2_2])
        .with_unknowns(vec!["x".to_string(), "y".to_string()])
        .with_parameters(vec!["a".to_string(), "b".to_string()])
        .with_initial_guess(vec![0.5, 0.5])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver2 = solver2;
    solver2.solve_with_params(vec![2.0, 0.5]);

    if let Some(map) = solver2.map_of_solutions {
        println!("\nSolution with a=2, b=0.5:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Verification: 2x² + 0.5y² = {:.6}",
            2.0 * map["x"].powi(2) + 0.5 * map["y"].powi(2)
        );
    }
    println!();
}

fn example_10_complex_native() {
    println!("Example 10: Complex Native Symbolic Expression");
    println!("-----------------------------------------------");
    println!("System with logarithms, exponentials, and divisions\n");

    let vars = Expr::Symbols("x, y");
    let x = vars[0].clone();
    let y = vars[1].clone();

    // ln(x) + y = 2, x + ln(y) = 2
    let eq1 = Expr::ln(x.clone()) + y.clone() - Expr::Const(2.0);
    let eq2 = x.clone() + Expr::ln(y.clone()) - Expr::Const(2.0);

    let solver = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1, eq2])
        .with_initial_guess(vec![1.0, 1.0])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver = solver;
    solver.solve();

    if let Some(map) = solver.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Symmetry check |x - y| = {:.2e}",
            (map["x"] - map["y"]).abs()
        );
        println!(
            "  Verification: ln(x) + y = {:.6}",
            map["x"].ln() + map["y"]
        );
    }

    println!("\n--- Division Example ---");
    let vars2 = Expr::Symbols("x, y");
    let x2 = vars2[0].clone();
    let y2 = vars2[1].clone();

    // x/y = 2, x + y = 3
    let eq1_div = x2.clone() / y2.clone() - Expr::Const(2.0);
    let eq2_div = x2.clone() + y2.clone() - Expr::Const(3.0);

    let solver_div = SymbolicLeastSquaresSolver::new()
        .with_equations(vec![eq1_div, eq2_div])
        .with_initial_guess(vec![1.5, 1.0])
        .with_tolerance(1e-8)
        .with_loglevel("none".to_string())
        .build();

    let mut solver_div = solver_div;
    solver_div.solve();

    if let Some(map) = solver_div.map_of_solutions {
        println!("Solution found:");
        println!("  x = {:.6}", map["x"]);
        println!("  y = {:.6}", map["y"]);
        println!(
            "  Verification: x/y = {:.6}, x + y = {:.6}",
            map["x"] / map["y"],
            map["x"] + map["y"]
        );
    }
    println!();
}