opensrdk_symbolic_computation/expression/
partial_variable.rs

1use crate::{Expression, ExpressionArray};
2use std::collections::HashMap;
3
4pub fn new_partial_variable(v: ExpressionArray) -> Expression {
5    Expression::PartialVariable(v)
6}
7
8impl Expression {
9    pub(crate) fn diff_partial_variable(
10        v: &ExpressionArray,
11        variable_ids: &[&str],
12    ) -> Vec<Expression> {
13        variable_ids
14            .iter()
15            .map(|&variable_id| {
16                new_partial_variable(ExpressionArray::from_factory(
17                    v.sizes().to_vec(),
18                    |indices| v[indices].differential(&[variable_id])[0].clone(),
19                ))
20            })
21            .collect()
22    }
23}
24#[cfg(test)]
25mod tests {
26    use std::collections::HashMap;
27    use std::collections::HashSet;
28
29    use crate::new_partial_variable;
30    use crate::new_variable;
31    use crate::ExpressionArray;
32
33    #[test]
34    fn it_works() {
35        let id_1 = "x";
36        let e_1 = new_variable((id_1).to_string());
37        let id_2 = "y";
38        let e_2 = new_variable((id_2).to_string());
39        let id_3 = "z";
40        let e_3 = new_variable((id_3).to_string());
41
42        let expression_vec = vec![e_1, e_2, e_3];
43
44        let factory = |i: &[usize]| expression_vec[i[0].clone()].clone();
45        let sizes: Vec<usize> = vec![3usize];
46
47        let theta_array_orig = ExpressionArray::from_factory(sizes, factory);
48        let theta_array = new_partial_variable(theta_array_orig);
49        println!("{:?}", theta_array);
50    }
51}