Skip to content

jelli.core.custom_basis

CustomBasis

A class to represent a custom parameter basis.

Parameters:

Name Type Description Default
name str

The name of the custom basis.

required
parameters list or dict

The parameters in the custom basis. If a list is provided, all parameters are assumed to be real. If a dict is provided, the keys are the parameter names and the values are the types (e.g., R for real, C for complex).

required

Attributes:

Name Type Description
name str

The name of the custom basis.

parameters dict

The parameters in the custom basis. The keys are the parameter names and the values are the types (e.g., R for real, C for complex).

Methods:

Name Description
get_all_names

Get all custom basis names.

get

Get a custom basis by name.

get_all

Get all custom basis objects.

get_parameter_basis

Get the parameter basis.

Examples:

Initialize a custom basis with real parameters:

>>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])

Initialize a custom basis with mixed parameters:

>>> custom_basis = CustomBasis('example_basis', {'param1': 'R', 'param2': 'C'})

Get all custom basis names:

>>> CustomBasis.get_all_names()

Get a custom basis by name:

>>> custom_basis = CustomBasis.get('example_basis')

Get all custom basis objects:

>>> custom_bases = CustomBasis.get_all()

Get the parameter basis:

>>> parameter_basis = custom_basis.get_parameter_basis()
Source code in jelli/core/custom_basis.py
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class CustomBasis:
    '''
    A class to represent a custom parameter basis.

    Parameters
    ----------
    name : str
        The name of the custom basis.
    parameters : list or dict
        The parameters in the custom basis. If a list is provided, all parameters are assumed to be real. If a dict is provided, the keys are the parameter names and the values are the types (e.g., `R` for real, `C` for complex).

    Attributes
    ----------
    name : str
        The name of the custom basis.
    parameters : dict
        The parameters in the custom basis. The keys are the parameter names and the values are the types (e.g., `R` for real, `C` for complex).

    Methods
    -------
    get_all_names() -> List[str]
        Get all custom basis names.
    get(name: str) -> 'CustomBasis'
        Get a custom basis by name.
    get_all() -> List['CustomBasis']
        Get all custom basis objects.
    get_parameter_basis() -> List
        Get the parameter basis.

    Examples
    --------
    Initialize a custom basis with real parameters:

    >>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])

    Initialize a custom basis with mixed parameters:

    >>> custom_basis = CustomBasis('example_basis', {'param1': 'R', 'param2': 'C'})

    Get all custom basis names:

    >>> CustomBasis.get_all_names()

    Get a custom basis by name:

    >>> custom_basis = CustomBasis.get('example_basis')

    Get all custom basis objects:

    >>> custom_bases = CustomBasis.get_all()

    Get the parameter basis:

    >>> parameter_basis = custom_basis.get_parameter_basis()

    '''

    _custom_bases: Dict[str, 'CustomBasis'] = {}  # Class attribute to store all custom bases

    def __init__(self, name: str, parameters: Union[List[str], Dict[str, str]]):
        """
        Initialize the CustomBasis class.

        Parameters
        ----------
        name : str
            The name of the custom basis.
        parameters : list or dict
            The parameters in the custom basis. If a list is provided, all parameters are assumed to be real. If a dict is provided, the keys are the parameter names and the values are the types (e.g., `R` for real, `C` for complex).

        Returns
        --------
        None

        Examples
        --------
        Initialize a custom basis with real parameters:

        >>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])

        Initialize a custom basis with mixed parameters:

        >>> custom_basis = CustomBasis('example_basis', {'param1': 'R', 'param2': 'C'})
        """
        self.name = name
        if isinstance(parameters, list):
            self.parameters = {param: 'R' for param in parameters}
        elif isinstance(parameters, dict):
            if not all(value in ['R', 'C'] for value in parameters.values()):
                raise ValueError("All parameter types must be either 'R' or 'C'.")
            self.parameters = parameters
        else:
            raise ValueError("Parameters must be a list or a dictionary.")
        self._custom_bases[self.name] = self

    @classmethod
    def get_all_names(cls) -> List[str]:
        """
        Get all custom basis names.

        Returns
        --------
        list
            A list of all custom basis names.

        Examples
        --------
            >>> CustomBasis.get_all_names()
        """
        return sorted(cls._custom_bases.keys())

    @classmethod
    def get(cls, name: str) -> 'CustomBasis':
        """
        Get a custom basis by name.

        Parameters
        ----------
        name : str
            The name of the custom basis.
        Returns
        --------
        CustomBasis
            The custom basis object.

        Examples
        --------
        >>> CustomBasis.get('example_basis')
        """
        return cls._custom_bases.get(name)

    @classmethod
    def get_all(cls) -> List['CustomBasis']:
        """
        Get all custom basis objects.

        Returns
        --------
        list
            A list of all custom basis objects.

        Examples
        --------
            >>> CustomBasis.get_all()
        """
        return list(cls._custom_bases.values())

    def get_parameter_basis(self, split_re_im=True) -> List:
        """
        Get the parameter basis.

        Parameters
        ----------
        split_re_im : bool, optional
            If `True`, split parameters into real and imaginary parts, otherwise return the parameters directly. Default is `True`.

        Returns
        --------
        list
            A list containing the parameter basis.

        Examples
        --------
            >>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])
            >>> custom_basis.get_parameter_basis('example_basis')
        """
        if split_re_im:
            parameter_basis = []
            for parameter, parameter_type in self.parameters.items():
                parameter_basis.append((parameter, 'R'))
                if parameter_type == 'C':
                    parameter_basis.append((parameter, 'I'))
        else:
            parameter_basis = self.parameters.keys()
        return sorted(parameter_basis)

__init__(name, parameters)

Initialize the CustomBasis class.

Parameters:

Name Type Description Default
name str

The name of the custom basis.

required
parameters list or dict

The parameters in the custom basis. If a list is provided, all parameters are assumed to be real. If a dict is provided, the keys are the parameter names and the values are the types (e.g., R for real, C for complex).

required

Returns:

Type Description
None

Examples:

Initialize a custom basis with real parameters:

>>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])

Initialize a custom basis with mixed parameters:

>>> custom_basis = CustomBasis('example_basis', {'param1': 'R', 'param2': 'C'})
Source code in jelli/core/custom_basis.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def __init__(self, name: str, parameters: Union[List[str], Dict[str, str]]):
    """
    Initialize the CustomBasis class.

    Parameters
    ----------
    name : str
        The name of the custom basis.
    parameters : list or dict
        The parameters in the custom basis. If a list is provided, all parameters are assumed to be real. If a dict is provided, the keys are the parameter names and the values are the types (e.g., `R` for real, `C` for complex).

    Returns
    --------
    None

    Examples
    --------
    Initialize a custom basis with real parameters:

    >>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])

    Initialize a custom basis with mixed parameters:

    >>> custom_basis = CustomBasis('example_basis', {'param1': 'R', 'param2': 'C'})
    """
    self.name = name
    if isinstance(parameters, list):
        self.parameters = {param: 'R' for param in parameters}
    elif isinstance(parameters, dict):
        if not all(value in ['R', 'C'] for value in parameters.values()):
            raise ValueError("All parameter types must be either 'R' or 'C'.")
        self.parameters = parameters
    else:
        raise ValueError("Parameters must be a list or a dictionary.")
    self._custom_bases[self.name] = self

get(name) classmethod

Get a custom basis by name.

Parameters:

Name Type Description Default
name str

The name of the custom basis.

required

Returns:

Type Description
CustomBasis

The custom basis object.

Examples:

>>> CustomBasis.get('example_basis')
Source code in jelli/core/custom_basis.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@classmethod
def get(cls, name: str) -> 'CustomBasis':
    """
    Get a custom basis by name.

    Parameters
    ----------
    name : str
        The name of the custom basis.
    Returns
    --------
    CustomBasis
        The custom basis object.

    Examples
    --------
    >>> CustomBasis.get('example_basis')
    """
    return cls._custom_bases.get(name)

get_all() classmethod

Get all custom basis objects.

Returns:

Type Description
list

A list of all custom basis objects.

Examples:

>>> CustomBasis.get_all()
Source code in jelli/core/custom_basis.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@classmethod
def get_all(cls) -> List['CustomBasis']:
    """
    Get all custom basis objects.

    Returns
    --------
    list
        A list of all custom basis objects.

    Examples
    --------
        >>> CustomBasis.get_all()
    """
    return list(cls._custom_bases.values())

get_all_names() classmethod

Get all custom basis names.

Returns:

Type Description
list

A list of all custom basis names.

Examples:

>>> CustomBasis.get_all_names()
Source code in jelli/core/custom_basis.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@classmethod
def get_all_names(cls) -> List[str]:
    """
    Get all custom basis names.

    Returns
    --------
    list
        A list of all custom basis names.

    Examples
    --------
        >>> CustomBasis.get_all_names()
    """
    return sorted(cls._custom_bases.keys())

get_parameter_basis(split_re_im=True)

Get the parameter basis.

Parameters:

Name Type Description Default
split_re_im bool

If True, split parameters into real and imaginary parts, otherwise return the parameters directly. Default is True.

True

Returns:

Type Description
list

A list containing the parameter basis.

Examples:

>>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])
>>> custom_basis.get_parameter_basis('example_basis')
Source code in jelli/core/custom_basis.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def get_parameter_basis(self, split_re_im=True) -> List:
    """
    Get the parameter basis.

    Parameters
    ----------
    split_re_im : bool, optional
        If `True`, split parameters into real and imaginary parts, otherwise return the parameters directly. Default is `True`.

    Returns
    --------
    list
        A list containing the parameter basis.

    Examples
    --------
        >>> custom_basis = CustomBasis('example_basis', ['param1', 'param2'])
        >>> custom_basis.get_parameter_basis('example_basis')
    """
    if split_re_im:
        parameter_basis = []
        for parameter, parameter_type in self.parameters.items():
            parameter_basis.append((parameter, 'R'))
            if parameter_type == 'C':
                parameter_basis.append((parameter, 'I'))
    else:
        parameter_basis = self.parameters.keys()
    return sorted(parameter_basis)