"""
CustomMetricCalculator
Generated by Eden via recursive self-improvement
2025-11-01 04:43:21.470553
"""

class CustomMetricCalculator:
    """
    A class to create and compute custom metrics using user-provided mathematical expressions.
    
    Methods:
        - __init__(formula): Initializes the calculator with a formula string.
        - set_formula(new_formula): Updates the metric formula.
        - calculate(values: list) -> float: Computes the result of the current formula given input values.
    """
    
    def __init__(self, formula):
        self.formula = formula
    
    def set_formula(self, new_formula):
        """Set a new custom metric formula."""
        self.formula = new_formula

    def calculate(self, values: list) -> float:
        """Calculate the result of the current formula with provided input values.
        
        Args:
            values (list): A list of numerical inputs for the formula.

        Returns:
            float: The computed value based on the formula and input values.
        """
        try:
            # Evaluate the formula using eval() which is a powerful but potentially risky function
            result = eval(self.formula, {"__builtins__": None}, dict(zip("x0 x1 x2 x3 x4".split(), values)))
        except Exception as e:
            raise ValueError(f"Invalid formula or input: {e}")
        return result

# Example usage
if __name__ == "__main__":
    # Create a CustomMetricCalculator instance with the formula "x0 * x1 + 5"
    calculator = CustomMetricCalculator("x0 * x1 + 5")
    
    # Calculate the metric value for given inputs [2, 3]
    print(calculator.calculate([2, 3]))  # Expected output: 11.0
    
    # Update the formula to "x0^2 - 2*x1"
    calculator.set_formula("x0**2 - 2*x1")
    
    # Recalculate with new inputs [4, 5]
    print(calculator.calculate([4, 5]))  # Expected output: 6.0