[docs]defdimension(dim:int)->None:""" Check whether an input dimension is a positive integer and raise an error otherwise. """ifnotisinstance(dim,int):raiseTypeError(f"The dimension needs to be an integer, but is {type(dim)}!")elifdim<1:raiseValueError(f"The dimension needs to be positive, but is {dim}!")
[docs]defdimension_for_algorithm(dim:int,possible:list,algorithm:str="")->None:""" Check whether an input dimension is a member of list and raise an error otherwise. Parameters ---------- dim: Dimension to be checked possible: Values that are allowed algorithm: Name of the algorithm for the error message """ifdimnotinpossible:iflen(possible)==1:in_str=str(possible[0])else:in_str=f"in {possible}"raiseValueError("For `"+algorithm+"` the dimension needs to be "+in_str+", "f"but is {dim}!")
[docs]defintegrality_and_range(value:int,name:str,min_val:int=0,max_val:int=math.inf)->None:""" Check whether an input parameter ``value`` is an integer in a certain range and raise an error otherwise. Parameters ---------- value: Value to be checked name: Name of the parameter min_val: Lower limit for the value max_val: Upper limit for the value """ifnotisinstance(value,int):raiseTypeError("The "+name+f" has to be an integer, but is {type(value)}!")ifvalue<min_valorvalue>max_val:raiseValueError("The "+name+f" has to be an integer in [{min_val},{max_val}], "f"but is {value}!")
[docs]defequal(val1:int|float,val2:int|float,name1:str,name2:str="")->None:""" Check whether an input parameter ``val1`` is equal to ``val2`` and raise an error otherwise. Parameters ---------- val1, val2: Values that shall be equal name1: Name of the parameter ``val1`` name2: Name of the parameter ``val2`` """ifname2=="":str2=""else:str2=name2+", i.e. "ifval1!=val2:raiseValueError("The "+name1+" needs to be ""equal to "+str2+f"{val2}, "f"but is {val1}!")
[docs]defgreater_equal(val1:int|float,val2:int|float,name1:str,name2:str="")->None:""" Check whether an input parameter ``val1`` is greater than or equal to ``val2`` and raise an error otherwise. Parameters ---------- val1: Value that shall be greater/equal val2: Value that shall be smaller name1: Name of the parameter ``val1`` name2: Name of the parameter ``val2`` """ifname2=="":str2=""else:str2=name2+", i.e. "ifval1<val2:raiseValueError("The "+name1+" needs to be ""greater than or equal to "+str2+f"{val2}, "f"but is {val1}!")
[docs]defgreater(val1:int|float,val2:int|float,name1:str,name2:str="")->None:""" Check whether an input parameter ``val1`` is greater than or equal to ``val2`` and raise an error otherwise. Parameters ---------- val1: Value that shall be greater val2: Value that shall be smaller/equal name1: Name of the parameter ``val1`` name2: Name of the parameter ``val2`` """ifname2=="":str2=""else:str2=name2+", i.e. "ifval1<=val2:raiseValueError("The "+name1+" needs to be ""greater than "+str2+f"{val2}, "f"but is {val1}!")
[docs]defsmaller_equal(val1:int|float,val2:int|float,name1:str,name2:str="")->None:""" Check whether an input parameter ``val1`` is smaller than or equal to ``val2`` and raise an error otherwise. Parameters ---------- val1: Value that shall be smaller/equal val2: Value that shall be greater name1: Name of the parameter ``val1`` name2: Name of the parameter ``val2`` """ifname2=="":str2=""else:str2=name2+", i.e. "ifval1>val2:raiseValueError("The "+name1+" needs to be ""smaller than or equal to "+str2+f"{val2}, "f"but is {val1}!")
[docs]defpebble_values(K:int,L:int)->None:""" Check if ``K`` and ``L`` satisfy the pebble conditions ``K > 0`` and ``0 <= L < 2K`` and raise an error otherwise. """# Check that K and L are integers and rangeintegrality_and_range(K,"K",min_val=1)integrality_and_range(L,"L",min_val=0)# Check the conditions on relationgreater(2*K,L,"value 2*K","L")