RustedSciThe 0.3.29

Rust framework for symbolic and numerical computing: symbolic calculations, nonlinear systems, IVP and BVP for ODE, optimization, fitting and more
Documentation
// Simple optimization: Pre-allocate with capacity
pub fn calc_jacobian_with_capacity(&mut self) {
    let variable_string_vec = self.variable_string.clone();
    let new_jac: Vec<Vec<Expr>> = self
        .vector_of_functions
        .par_iter()
        .enumerate()
        .map(|(i, function)| {
            // Pre-allocate with exact capacity
            let mut vector_of_partial_derivatives = Vec::with_capacity(self.vector_of_variables.len());
            
            for j in 0..self.vector_of_variables.len() {
                let variable = &variable_string_vec[j];
                let list_of_variables_for_this_eq = &self.variables_for_all_disrete[i];
                
                if list_of_variables_for_this_eq.contains(variable) {
                    let mut partial = Expr::diff(function, variable);
                    partial = partial.symplify();
                    vector_of_partial_derivatives.push(partial);
                } else {
                    vector_of_partial_derivatives.push(Expr::Const(0.0));
                }
            }
            vector_of_partial_derivatives
        })
        .collect();
    
    self.symbolic_jacobian = new_jac;
}

// Even simpler: Just avoid the clone
pub fn calc_jacobian_no_clone(&mut self) {
    let new_jac: Vec<Vec<Expr>> = self
        .vector_of_functions
        .par_iter()
        .enumerate()
        .map(|(i, function)| {
            let mut vector_of_partial_derivatives = Vec::new();
            
            for j in 0..self.vector_of_variables.len() {
                let variable = &self.variable_string[j]; // Direct reference
                let list_of_variables_for_this_eq = &self.variables_for_all_disrete[i];
                
                if list_of_variables_for_this_eq.contains(variable) {
                    let mut partial = Expr::diff(function, variable);
                    partial = partial.symplify();
                    vector_of_partial_derivatives.push(partial);
                } else {
                    vector_of_partial_derivatives.push(Expr::Const(0.0));
                }
            }
            vector_of_partial_derivatives
        })
        .collect();
    
    self.symbolic_jacobian = new_jac;
}