def test_validate_references_nested_raises_value_error(): with pytest.raises(ValueError, match="Expected type"): optplan.validate_references( optplan.Sum(functions=[ optplan.Power( function=optplan.Sum(functions=[ optplan.make_constant(2), optplan.SimulationSpace() ])), optplan.make_constant(2) ]))
def test_prod_fun_and_const_obj(): var1 = optplan.Parameter() const1 = optplan.make_constant(2) prod1 = var1 * const1 assert isinstance(prod1, optplan.Product) assert len(prod1.functions) == 2 assert prod1.functions[0] == var1 assert prod1.functions[1] == const1
def test_sum_fun_and_const_obj(): var1 = optplan.Parameter() const1 = optplan.make_constant(2) sum1 = var1 + const1 assert isinstance(sum1, optplan.Sum) assert len(sum1.functions) == 2 assert sum1.functions[0] == var1 assert sum1.functions[1] == const1
def __add__(self, obj) -> "optplan.Sum": if isinstance(obj, optplan.Sum): return obj.__add__(self) if isinstance(obj, optplan.Constant): return optplan.Sum(functions=[self, obj]) if isinstance(obj, (numbers.Number, optplan.ComplexNumber)): return optplan.Sum(functions=[self, optplan.make_constant(obj)]) if isinstance(obj, Function): return optplan.Sum(functions=[self, obj]) raise TypeError("Attempting to add node with type {}".format(type(obj)))
def __mul__(self, obj) -> "optplan.Product": if isinstance(obj, optplan.Product): return obj.__mul__(self) if isinstance(obj, optplan.Constant): return optplan.Product(functions=[self, obj]) if isinstance(obj, (numbers.Number, optplan.ComplexNumber)): return optplan.Product(functions=[self, optplan.make_constant(obj)]) if isinstance(obj, Function): return optplan.Product(functions=[self, obj]) raise TypeError("Attempting to multiply node with type {}".format( type(obj)))
def test_constant_standard(): """Tests `make_constant` creation using standard procedure.""" constant = optplan.make_constant( value=optplan.ComplexNumber(real=2, imag=3.4)) assert constant.value.real == 2 assert constant.value.imag == 3.4
def test_power_constant(): var1 = optplan.Parameter() power1 = var1**optplan.make_constant(2) assert isinstance(power1, optplan.Power) assert power1.function == var1 assert power1.exp == 2
def test_constant_short(value): """Tests `make_constant` creation using the short form (e.g. `Constant(6)`).""" constant = optplan.make_constant(value) complex_value = complex(value) assert constant.value.real == complex_value.real assert constant.value.imag == complex_value.imag