variables.md - Open-CP/OCP GitHub Wiki

variable Class

variable Class is a fundamental component in our graph modeling framework. This class includes information about a variable, including its size, value, and unique identifier, and provides methods for managing and displaying variable properties.


Attributes

  • bitsize: An integer representing the size of the variable in bits.
  • value: The current value of the variable (default is None if not set).
  • ID: A unique identifier string for the variable.

Methods

display_value(representation='binary')

Displays the value of the variable in a specified format.

Inputs:

  • representation (optional): A string specifying the desired representation format.
    Supported values:
    • 'binary': Binary representation, padded to the variable's bit size.
    • 'hexadecimal': Hexadecimal representation, padded to fit the variable's bit size.
    • 'integer': Decimal representation.

Outputs:

  • Returns a string representing the value of the variable in the specified format.
  • If the value is not set or the representation is invalid, returns "Invalid representation".

Example:

var = Variable(bitsize=8, value=10, ID="var1")
print(var.display_value('binary'))      # Output: 00001010
print(var.display_value('hexadecimal')) # Output: 0a
print(var.display_value('integer'))     # Output: 10 

display(representation='binary')

Prints a detailed description of the variable, including:

  • The ID.
  • The bitsize.
  • The value in the specified format.

Inputs:

  • representation (optional): A string specifying the desired representation format, identical to the display_value method.

Outputs:

  • Prints the variable details to the console.

Example:

var = Variable(bitsize=8, value=10, ID="var1")
var.display('binary')
# Output: ID: var1 / bitsize: 8 / value: 00001010

remove_round_from_ID()

Removes round information from the variable’s ID.
This method is useful in scenarios where round-specific identifiers are not required, such as in unrolled computations.

Inputs:

  • None.

Outputs:

  • Returns a string with the round information removed from the ID.

Example:

var = Variable(bitsize=8, value=10, ID="in_1_0_0")
print(var.remove_round_from_ID())  # Output: in_0_0