RustedSciThe 0.4.16

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
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//! Shared helpers for text task parsers (IVP/BVP).
//!
//! This module keeps parser-agnostic utilities in one place to avoid
//! duplicating the same symbolic pre-processing logic across task shells.

use crate::command_interpreter::task_parser::{DocumentMap, Value};
use crate::symbolic::parse_expr::parse_expression_func;
use crate::symbolic::symbolic_engine::Expr;
use crate::symbolic::symexpr_graphs::{Definition, SymbolicSystem};
use std::collections::{BTreeSet, HashMap, HashSet};

type GenericSectionMap = HashMap<String, Option<Vec<Value>>>;

/// Parsed symbolic IVP/BVP equations after task-document parameters and named
/// aliases have been resolved.
///
/// `rhs` is solver-ready: it may reference only [`Self::arg`] and the declared
/// [`Self::unknowns`].
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedEquationSystem {
    pub arg: String,
    pub unknowns: Vec<String>,
    pub rhs: Vec<Expr>,
    pub parameter_names: Vec<String>,
    pub parameter_values: HashMap<String, f64>,
}

/// Typed validation error shared by IVP and BVP task-document parsers.
#[derive(Debug, Clone, PartialEq)]
pub enum SharedEquationParseError {
    MissingSection(&'static str),
    MissingField {
        section: String,
        field: String,
    },
    InvalidField {
        section: String,
        field: String,
        message: String,
    },
    InconsistentEquationCounts {
        unknowns: usize,
        rhs: usize,
    },
    Semantic(String),
}

impl std::fmt::Display for SharedEquationParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingSection(section) => write!(f, "missing section `{section}`"),
            Self::MissingField { section, field } => {
                write!(f, "missing field `{field}` in section `{section}`")
            }
            Self::InvalidField {
                section,
                field,
                message,
            } => write!(
                f,
                "invalid field `{field}` in section `{section}`: {message}"
            ),
            Self::InconsistentEquationCounts { unknowns, rhs } => write!(
                f,
                "number of unknowns ({unknowns}) does not match number of rhs expressions ({rhs})"
            ),
            Self::Semantic(message) => write!(f, "{message}"),
        }
    }
}

impl std::error::Error for SharedEquationParseError {}

/// Parses the solver-facing `equations` section of an IVP or BVP task document.
///
/// The pipeline validates declaration names, expands `where`/`substitute`
/// aliases through [`SymbolicSystem`], substitutes numeric parameters, and
/// rejects unresolved RHS symbols before any numerical solver is constructed.
pub fn parse_symbolic_equation_system(
    document: &DocumentMap,
    default_arg: &str,
) -> Result<ParsedEquationSystem, SharedEquationParseError> {
    let section = get_required_section(document, "equations")?;
    let arg = get_optional_string(section, "arg", "equations")?
        .unwrap_or_else(|| default_arg.to_string());

    let (parameter_names, parameter_values) =
        parse_numeric_parameter_declarations(document, section)?;

    validate_symbol_names(&arg, &parameter_names).map_err(SharedEquationParseError::Semantic)?;

    let (unknowns, rhs_raw) = if section.contains_key("unknowns") || section.contains_key("rhs") {
        let unknowns = get_required_string_list(section, "equations", "unknowns")?;
        let rhs = get_required_string_list(section, "equations", "rhs")?;
        (unknowns, rhs)
    } else {
        parse_pair_style_equations(section)?
    };

    if unknowns.len() != rhs_raw.len() {
        return Err(SharedEquationParseError::InconsistentEquationCounts {
            unknowns: unknowns.len(),
            rhs: rhs_raw.len(),
        });
    }

    validate_unknown_names(&unknowns).map_err(SharedEquationParseError::Semantic)?;
    let unknown_set: HashSet<&str> = unknowns.iter().map(String::as_str).collect();
    if unknown_set.contains(arg.as_str()) {
        return Err(SharedEquationParseError::Semantic(format!(
            "argument `{arg}` cannot also be listed as an unknown"
        )));
    }
    for parameter in &parameter_names {
        if unknown_set.contains(parameter.as_str()) {
            return Err(SharedEquationParseError::Semantic(format!(
                "parameter `{parameter}` cannot also be listed as an unknown"
            )));
        }
        if parameter == &arg {
            return Err(SharedEquationParseError::Semantic(format!(
                "parameter `{parameter}` cannot also be the independent argument"
            )));
        }
    }

    let substitutions = parse_equation_substitutions(document, &parameter_values, &arg, &unknowns)
        .map_err(|message| SharedEquationParseError::InvalidField {
            section: "where/substitute".to_string(),
            field: "*".to_string(),
            message,
        })?;
    let rhs = rhs_raw
        .iter()
        .map(|expr| parse_expr_safe(expr, "equations", "rhs"))
        .collect::<Result<Vec<_>, _>>()?;
    let rhs = apply_symbolic_substitutions_to_vec(rhs, &substitutions)
        .into_iter()
        .map(|expr| expr.set_variable_from_map(&parameter_values))
        .collect::<Vec<_>>();
    validate_equation_rhs_variables(&rhs, &arg, &unknowns)?;

    Ok(ParsedEquationSystem {
        arg,
        unknowns,
        rhs,
        parameter_names,
        parameter_values,
    })
}

/// Parses numeric task parameters declared in either supported document form.
///
/// The compact legacy form stays inside `equations`:
///
/// ```text
/// parameters: rate, offset
/// parameter_values: 2.0, 0.5
/// ```
///
/// The readable form is a dedicated section and is preferable for larger
/// models:
///
/// ```text
/// parameters
/// rate: 2.0
/// offset: 0.5
/// ```
///
/// Both forms are intentionally mutually exclusive. Silently merging them
/// would make a duplicated parameter name depend on parser implementation
/// details instead of the task document's explicit intent.
fn parse_numeric_parameter_declarations(
    document: &DocumentMap,
    equations: &GenericSectionMap,
) -> Result<(Vec<String>, HashMap<String, f64>), SharedEquationParseError> {
    let uses_inline_form =
        equations.contains_key("parameters") || equations.contains_key("parameter_values");
    let section_parameters = document.get("parameters");

    if uses_inline_form && section_parameters.is_some() {
        return Err(SharedEquationParseError::InvalidField {
            section: "parameters".to_string(),
            field: "*".to_string(),
            message: "use either `equations.parameters` with `parameter_values` or the dedicated `parameters` section, not both".to_string(),
        });
    }

    if let Some(parameters) = section_parameters {
        let mut parameter_names = parameters.keys().cloned().collect::<Vec<_>>();
        parameter_names.sort();
        let mut parameter_values = HashMap::with_capacity(parameter_names.len());

        for name in &parameter_names {
            let values = get_required_values(parameters, "parameters", name)?;
            if values.len() != 1 {
                return Err(SharedEquationParseError::InvalidField {
                    section: "parameters".to_string(),
                    field: name.clone(),
                    message: "expected exactly one numeric value".to_string(),
                });
            }
            parameter_values.insert(
                name.clone(),
                value_to_float(&values[0], "parameters", name)?,
            );
        }

        return Ok((parameter_names, parameter_values));
    }

    let parameter_names =
        get_optional_string_list(equations, "parameters", "equations")?.unwrap_or_default();
    let parameter_values_vec =
        get_optional_float_list(equations, "parameter_values", "equations")?.unwrap_or_default();
    if parameter_names.len() != parameter_values_vec.len() {
        return Err(SharedEquationParseError::InvalidField {
            section: "equations".to_string(),
            field: "parameter_values".to_string(),
            message: format!(
                "expected {} parameter values, got {}",
                parameter_names.len(),
                parameter_values_vec.len()
            ),
        });
    }
    let parameter_values = parameter_names
        .iter()
        .cloned()
        .zip(parameter_values_vec)
        .collect();

    Ok((parameter_names, parameter_values))
}

/// Parses `where` and `substitute` definitions and fully resolves aliases.
///
/// This compatibility helper intentionally returns ordinary [`Expr`] trees.
/// IVP/BVP task documents are normally small, so this keeps their existing
/// solver interfaces simple while [`SymbolicSystem`] provides common cycle and
/// duplicate-definition validation.
pub fn parse_symbolic_substitutions(
    document: &DocumentMap,
) -> Result<HashMap<String, Expr>, String> {
    parse_symbolic_substitutions_with_parameter_values(document, &HashMap::new())
}

/// Parses symbolic aliases after substituting numeric task-document parameters
/// into every definition.
///
/// Parameters are applied before dependency resolution so every expanded alias
/// and final equation observes the same numeric values.
pub fn parse_symbolic_substitutions_with_parameter_values(
    document: &DocumentMap,
    parameter_values: &HashMap<String, f64>,
) -> Result<HashMap<String, Expr>, String> {
    parse_symbolic_substitutions_with_context(document, parameter_values, &HashMap::new())
}

/// Resolves task-document aliases while reserving names owned by the equation
/// system. This prevents aliases from silently replacing `arg`, unknowns, or
/// numeric parameters before the solver sees the final RHS expressions.
fn parse_equation_substitutions(
    document: &DocumentMap,
    parameter_values: &HashMap<String, f64>,
    arg: &str,
    unknowns: &[String],
) -> Result<HashMap<String, Expr>, String> {
    let mut reserved_names = HashMap::new();
    reserved_names.insert(arg.to_string(), "independent argument");
    for unknown in unknowns {
        reserved_names.insert(unknown.clone(), "unknown");
    }
    for parameter in parameter_values.keys() {
        reserved_names.insert(parameter.clone(), "parameter");
    }

    parse_symbolic_substitutions_with_context(document, parameter_values, &reserved_names)
}

fn parse_symbolic_substitutions_with_context(
    document: &DocumentMap,
    parameter_values: &HashMap<String, f64>,
    reserved_names: &HashMap<String, &'static str>,
) -> Result<HashMap<String, Expr>, String> {
    let mut definitions = Vec::new();
    for section_name in ["where", "substitute"] {
        if let Some(section) = document.get(section_name) {
            parse_substitution_section(section_name, section, &mut definitions)?;
        }
    }
    if definitions.is_empty() {
        return Ok(HashMap::new());
    }

    for definition in &definitions {
        if let Some(owner) = reserved_names.get(&definition.name) {
            return Err(format!(
                "symbolic alias `{}` conflicts with reserved {owner} name",
                definition.name
            ));
        }
    }

    let definitions = definitions.into_iter().map(|definition| {
        Definition::new(
            definition.name,
            definition
                .expression
                .set_variable_from_map(parameter_values),
        )
    });

    resolve_substitution_definitions(definitions)
}

/// Validates the final solver-facing RHS expressions after parameters and
/// symbolic aliases have been applied. A generic [`SymbolicSystem`] may keep
/// free variables, but an IVP/BVP callback can only evaluate its independent
/// argument and declared state variables.
fn validate_equation_rhs_variables(
    rhs: &[Expr],
    arg: &str,
    unknowns: &[String],
) -> Result<(), SharedEquationParseError> {
    let mut allowed = HashSet::with_capacity(unknowns.len() + 1);
    allowed.insert(arg);
    allowed.extend(unknowns.iter().map(String::as_str));

    for (index, expression) in rhs.iter().enumerate() {
        let unresolved = expression
            .all_arguments_are_variables()
            .into_iter()
            .filter(|name| !allowed.contains(name.as_str()))
            .collect::<BTreeSet<_>>();

        if !unresolved.is_empty() {
            return Err(SharedEquationParseError::Semantic(format!(
                "rhs expression {index} contains undeclared symbol(s): {}",
                unresolved.into_iter().collect::<Vec<_>>().join(", ")
            )));
        }
    }

    Ok(())
}

/// Applies already resolved named substitutions to one expression.
pub fn apply_symbolic_substitutions(expr: Expr, substitutions: &HashMap<String, Expr>) -> Expr {
    let mut expanded = expr;
    for (alias, replacement) in substitutions {
        expanded = expanded.substitute_variable(alias, replacement);
    }
    expanded
}

/// Applies already resolved named substitutions to each expression in order.
pub fn apply_symbolic_substitutions_to_vec(
    exprs: Vec<Expr>,
    substitutions: &HashMap<String, Expr>,
) -> Vec<Expr> {
    exprs
        .into_iter()
        .map(|expr| apply_symbolic_substitutions(expr, substitutions))
        .collect()
}

/// Applies numeric parameter values to an externally supplied substitution map.
///
/// This compatibility helper is intentionally separate from the canonical task
/// document path, which substitutes parameters before graph expansion.
pub fn apply_parameter_values_to_substitutions(
    substitutions: HashMap<String, Expr>,
    parameter_values: &HashMap<String, f64>,
) -> HashMap<String, Expr> {
    substitutions
        .into_iter()
        .map(|(name, expr)| (name, expr.set_variable_from_map(parameter_values)))
        .collect()
}

/// Validates the independent argument and numeric parameter declarations.
pub fn validate_symbol_names(arg: &str, parameter_names: &[String]) -> Result<(), String> {
    if arg.trim().is_empty() {
        return Err("independent argument name cannot be empty".to_string());
    }
    let mut seen = HashSet::new();
    for name in parameter_names {
        if name.trim().is_empty() {
            return Err("parameter name cannot be empty".to_string());
        }
        if !seen.insert(name) {
            return Err(format!("duplicate parameter name `{name}`"));
        }
    }
    Ok(())
}

/// Validates state-variable names before symbolic substitutions or solver
/// construction. An explicit duplicate would otherwise make RHS-to-state
/// alignment ambiguous even when the expression text itself is valid.
fn validate_unknown_names(unknowns: &[String]) -> Result<(), String> {
    let mut seen = HashSet::new();
    for name in unknowns {
        if name.trim().is_empty() {
            return Err("unknown name cannot be empty".to_string());
        }
        if !seen.insert(name) {
            return Err(format!("duplicate unknown name `{name}`"));
        }
    }
    Ok(())
}

fn parse_substitution_section(
    section_name: &str,
    section: &GenericSectionMap,
    definitions: &mut Vec<Definition>,
) -> Result<(), String> {
    for (alias, values_opt) in section {
        let Some(values) = values_opt else {
            return Err(format!(
                "section `{section_name}` key `{alias}` must contain one expression string"
            ));
        };
        if values.len() != 1 {
            return Err(format!(
                "section `{section_name}` key `{alias}` must contain exactly one expression"
            ));
        }
        let value = &values[0];
        let Some(expr_text) = value.as_string() else {
            return Err(format!(
                "section `{section_name}` key `{alias}` must be a string expression"
            ));
        };
        // Preserve every definition until SymbolicSystem validates duplicates.
        // A HashMap here would silently let `substitute` overwrite `where`.
        definitions.push(Definition::new(
            alias.clone(),
            parse_substitution_expr_safe(expr_text, section_name, alias)?,
        ));
    }
    Ok(())
}

fn parse_substitution_expr_safe(
    expr_text: &str,
    section: &str,
    field: &str,
) -> Result<Expr, String> {
    parse_expression_func(0, expr_text).map_err(|message| {
        format!(
            "failed to parse symbolic expression `{expr_text}` in section `{section}` field `{field}`: {message}"
        )
    })
}

fn parse_expr_safe(
    expr_text: &str,
    section: &str,
    field: &str,
) -> Result<Expr, SharedEquationParseError> {
    parse_expression_func(0, expr_text).map_err(|message| SharedEquationParseError::InvalidField {
        section: section.to_string(),
        field: field.to_string(),
        message: format!("failed to parse symbolic expression `{expr_text}`: {message}"),
    })
}

fn parse_pair_style_equations(
    section: &GenericSectionMap,
) -> Result<(Vec<String>, Vec<String>), SharedEquationParseError> {
    let reserved = [
        "arg",
        "parameters",
        "params",
        "parameter_names",
        "parameter_values",
        "params_values",
        "param_values",
        "unknowns",
        "rhs",
    ];
    let mut unknowns = Vec::new();
    let mut rhs = Vec::new();

    for key in section.keys() {
        if reserved.contains(&key.as_str()) {
            continue;
        }
        let expr = get_required_symbolic_expr(section, "equations", key)?;
        unknowns.push(key.clone());
        rhs.push(expr);
    }

    if unknowns.is_empty() {
        return Err(SharedEquationParseError::Semantic(
            "equations section must contain either `unknowns`/`rhs` lists or variable-to-rhs pairs"
                .to_string(),
        ));
    }

    Ok((unknowns, rhs))
}

fn get_required_symbolic_expr(
    section: &GenericSectionMap,
    section_name: &str,
    field: &str,
) -> Result<String, SharedEquationParseError> {
    let values = get_required_values(section, section_name, field)?;
    if values.is_empty() {
        return Err(SharedEquationParseError::MissingField {
            section: section_name.to_string(),
            field: field.to_string(),
        });
    }
    Ok(values
        .iter()
        .map(Value::to_string_value)
        .collect::<Vec<_>>()
        .join(" "))
}

fn get_required_section<'a>(
    document: &'a DocumentMap,
    section: &'static str,
) -> Result<&'a GenericSectionMap, SharedEquationParseError> {
    document
        .get(section)
        .ok_or(SharedEquationParseError::MissingSection(section))
}

fn get_required_values<'a>(
    section: &'a GenericSectionMap,
    section_name: &str,
    field: &str,
) -> Result<&'a Vec<Value>, SharedEquationParseError> {
    section
        .get(field)
        .ok_or_else(|| SharedEquationParseError::MissingField {
            section: section_name.to_string(),
            field: field.to_string(),
        })?
        .as_ref()
        .ok_or_else(|| SharedEquationParseError::MissingField {
            section: section_name.to_string(),
            field: field.to_string(),
        })
}

fn get_optional_string(
    section: &GenericSectionMap,
    field: &str,
    section_name: &str,
) -> Result<Option<String>, SharedEquationParseError> {
    match section.get(field) {
        Some(Some(values)) if !values.is_empty() => {
            if values.len() != 1 {
                return Err(SharedEquationParseError::InvalidField {
                    section: section_name.to_string(),
                    field: field.to_string(),
                    message: "expected a single string value".to_string(),
                });
            }
            Ok(Some(value_to_string(&values[0], section_name, field)?))
        }
        _ => Ok(None),
    }
}

fn get_required_string_list(
    section: &GenericSectionMap,
    section_name: &str,
    field: &str,
) -> Result<Vec<String>, SharedEquationParseError> {
    let values = get_required_values(section, section_name, field)?;
    values
        .iter()
        .map(|value| value_to_string(value, section_name, field))
        .collect()
}

fn get_optional_string_list(
    section: &GenericSectionMap,
    field: &str,
    section_name: &str,
) -> Result<Option<Vec<String>>, SharedEquationParseError> {
    match section.get(field) {
        Some(Some(values)) => values
            .iter()
            .map(|value| value_to_string(value, section_name, field))
            .collect::<Result<Vec<_>, _>>()
            .map(Some),
        _ => Ok(None),
    }
}

fn get_optional_float_list(
    section: &GenericSectionMap,
    field: &str,
    section_name: &str,
) -> Result<Option<Vec<f64>>, SharedEquationParseError> {
    match section.get(field) {
        Some(Some(values)) => values_to_float_list(values, section_name, field).map(Some),
        _ => Ok(None),
    }
}

fn values_to_float_list(
    values: &[Value],
    section_name: &str,
    field: &str,
) -> Result<Vec<f64>, SharedEquationParseError> {
    if values.len() == 1 {
        if let Some(vector) = values[0].as_vector() {
            return Ok(vector.clone());
        }
    }
    values
        .iter()
        .map(|value| value_to_float(value, section_name, field))
        .collect()
}

fn value_to_string(
    value: &Value,
    section_name: &str,
    field: &str,
) -> Result<String, SharedEquationParseError> {
    if let Some(text) = value.as_string() {
        Ok(text.clone())
    } else {
        Err(SharedEquationParseError::InvalidField {
            section: section_name.to_string(),
            field: field.to_string(),
            message: "expected string".to_string(),
        })
    }
}

fn value_to_float(
    value: &Value,
    section_name: &str,
    field: &str,
) -> Result<f64, SharedEquationParseError> {
    if let Some(number) = value.as_float() {
        Ok(number)
    } else if let Some(integer) = value.as_usize() {
        Ok(integer as f64)
    } else {
        Err(SharedEquationParseError::InvalidField {
            section: section_name.to_string(),
            field: field.to_string(),
            message: "expected numeric value".to_string(),
        })
    }
}

/// Resolves definitions through the shared graph implementation used by both
/// IVP and BVP parsers.
fn resolve_substitution_definitions(
    definitions: impl IntoIterator<Item = Definition>,
) -> Result<HashMap<String, Expr>, String> {
    let system = SymbolicSystem::new(definitions)
        .map_err(|error| format!("invalid symbolic substitutions: {error}"))?;

    Ok(system
        .expand_all()
        .map_err(|error| format!("failed to expand symbolic substitutions: {error}"))?
        .into_iter()
        .collect())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::command_interpreter::task_parser::DocumentParser;

    #[test]
    fn where_section_supports_nested_aliases() {
        let doc = r#"
where
a: 2*t
b: a + y
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse where section");
        let map = parser.get_result().expect("document map should exist");
        let substitutions =
            parse_symbolic_substitutions(map).expect("substitution parsing should succeed");
        assert!(substitutions.contains_key("a"));
        assert!(substitutions.contains_key("b"));
        let expr = Expr::parse_expression("b - y");
        let expanded = apply_symbolic_substitutions(expr, &substitutions);
        let f = expanded.lambdify_borrowed_thread_safe(&["t", "y"]);
        let value = f(&[3.0, 10.0]);
        assert!((value - 6.0).abs() < 1e-12);
    }

    #[test]
    fn where_section_reports_bad_expression_without_panic() {
        let doc = r#"
where
bad_alias: sin(
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse where section");
        let map = parser.get_result().expect("document map should exist");
        let error = parse_symbolic_substitutions(map)
            .expect_err("bad symbolic alias should return a parser error");
        assert!(error.contains("failed to parse symbolic expression"));
        assert!(error.contains("bad_alias"));
    }

    #[test]
    fn symbolic_substitutions_receive_numeric_parameters_before_rhs_expansion() {
        let doc = r#"
equations
arg: t
parameters: a
parameter_values: 3.0
unknowns: y
rhs: gain * y

where
base: 2 * t
gain: base + a
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equations section");
        let map = parser.get_result().expect("document map should exist");
        let parsed =
            parse_symbolic_equation_system(map, "t").expect("equation parsing should succeed");
        let rendered = parsed.rhs[0].to_string();
        assert!(rendered.contains('3'));
        assert!(rendered.contains('2'));
        assert!(!rendered.contains('a'));
    }

    #[test]
    fn equation_system_accepts_readable_parameter_section() {
        let doc = r#"
equations
arg: t
unknowns: y
rhs: source - y

parameters
R: 2.0
offset: 0.5

where
source: R * t + offset
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equation document");
        let map = parser.get_result().expect("document map should exist");

        let parsed = parse_symbolic_equation_system(map, "t")
            .expect("dedicated parameters section should parse");

        assert_eq!(parsed.parameter_names, vec!["R", "offset"]);
        assert_eq!(parsed.parameter_values["R"], 2.0);
        assert_eq!(parsed.parameter_values["offset"], 0.5);
        let rhs = parsed.rhs[0].lambdify_borrowed_thread_safe(&["t", "y"]);
        assert!((rhs(&[3.0, 1.0]) - 5.5).abs() < 1e-12);
    }

    #[test]
    fn equation_system_rejects_mixed_parameter_declaration_styles() {
        let doc = r#"
equations
arg: t
parameters: rate
parameter_values: 2.0
unknowns: y
rhs: -rate * y

parameters
rate: 3.0
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equation document");
        let map = parser.get_result().expect("document map should exist");

        let error = parse_symbolic_equation_system(map, "t")
            .expect_err("mixed parameter declaration styles must be rejected");
        assert!(matches!(
            error,
            SharedEquationParseError::InvalidField { .. }
        ));
        assert!(error.to_string().contains("either"));
    }

    #[test]
    fn symbolic_substitutions_reject_duplicate_aliases_across_sections() {
        let doc = r#"
where
gain: 2 * t

substitute
gain: 3 * t
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser
            .parse_document()
            .expect("parse substitution sections");
        let map = parser.get_result().expect("document map should exist");

        let error = parse_symbolic_substitutions(map)
            .expect_err("duplicate aliases must not silently overwrite one another");

        assert!(error.contains("invalid symbolic substitutions"));
        assert!(error.contains("определена более одного раза"));
        assert!(error.contains("gain"));
    }

    #[test]
    fn symbolic_substitutions_report_indirect_cycles() {
        let doc = r#"
where
a: b + 1
b: c + 1
c: a + 1
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser
            .parse_document()
            .expect("parse cyclic substitution section");
        let map = parser.get_result().expect("document map should exist");

        let error =
            parse_symbolic_substitutions(map).expect_err("indirect alias cycle must be reported");

        assert!(error.contains("циклическая зависимость"));
        assert!(error.contains("a"));
        assert!(error.contains("b"));
        assert!(error.contains("c"));
    }

    #[test]
    fn equation_alias_cannot_shadow_an_unknown() {
        let doc = r#"
equations
arg: t
unknowns: y
rhs: y

where
y: 2 * t
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equation document");
        let map = parser.get_result().expect("document map should exist");

        let error = parse_symbolic_equation_system(map, "t")
            .expect_err("an alias must not replace a state variable");

        assert!(matches!(
            &error,
            SharedEquationParseError::InvalidField { .. }
        ));
        assert!(error.to_string().contains("alias `y`"));
        assert!(error.to_string().contains("unknown"));
    }

    #[test]
    fn equation_alias_cannot_shadow_argument_or_parameter() {
        let cases = [
            (
                r#"
equations
arg: t
unknowns: y
rhs: y

where
t: 2 * y
"#,
                "independent argument",
            ),
            (
                r#"
equations
arg: t
parameters: gain
parameter_values: 2.0
unknowns: y
rhs: gain * y

where
gain: t + y
"#,
                "parameter",
            ),
        ];

        for (document, reserved_owner) in cases {
            let mut parser = DocumentParser::new(document.to_string());
            parser.parse_document().expect("parse equation document");
            let map = parser.get_result().expect("document map should exist");

            let error = parse_symbolic_equation_system(map, "t")
                .expect_err("an alias must not replace a solver-owned name");

            assert!(matches!(
                &error,
                SharedEquationParseError::InvalidField { .. }
            ));
            assert!(error.to_string().contains(reserved_owner));
        }
    }

    #[test]
    fn equation_rhs_rejects_undeclared_symbols_after_alias_expansion() {
        let doc = r#"
equations
arg: t
unknowns: y
rhs: source - y

where
source: misspelled_gain * t
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equation document");
        let map = parser.get_result().expect("document map should exist");

        let error = parse_symbolic_equation_system(map, "t")
            .expect_err("undeclared symbolic inputs must be reported before solving");

        assert!(matches!(&error, SharedEquationParseError::Semantic(_)));
        assert!(error.to_string().contains("misspelled_gain"));
    }

    #[test]
    fn equation_system_rejects_duplicate_unknown_names() {
        let doc = r#"
equations
arg: t
unknowns: y, y
rhs: -y, -y
"#;
        let mut parser = DocumentParser::new(doc.to_string());
        parser.parse_document().expect("parse equation document");
        let map = parser.get_result().expect("document map should exist");

        let error = parse_symbolic_equation_system(map, "t")
            .expect_err("duplicate state variables must not reach solver construction");

        assert!(matches!(&error, SharedEquationParseError::Semantic(_)));
        assert!(error.to_string().contains("duplicate unknown name `y`"));
    }
}