IBM/simulai

Error when trying to perform internal boundary product with numpy array

TarikKaanKoc opened this issue · 3 comments

NameError: name 'tag' is not defined

vector_ = np.array(vector)[:, tag]

The error occurs when the function tries to access the tag variable in the line vector_ = np.array(vector)[:, tag] tag variable is not defined anywhere in the code.

In this version, I have removed the tag variable and added a check to ensure that the input is a 1-D array or a tuple. If the input is a multi-dimensional array, it will raise an error indicating that the input must be a 1-D array or a tuple.

def internal_boundary_product(self, vector):
    try:
        vector_ = np.array(vector)
        if len(vector_.shape) > 1:
            raise Exception("The input must be a 1-D array or a tuple.")
        if isinstance(vector_, np.ndarray):
            product_list = (vector_,)
        elif isinstance(vector_, tuple):
            product_list = vector_
        else:
            raise Exception("The input must be a numpy array or a tuple.")
        return list(product(*product_list))
    except Exception as e:
        print("An error occurred: ", e)
    def internal_boundary_product(self, vector):
        """
        Calculates the internal boundary product of a given vector.
        
        Parameters
        ----------
        vector : numpy.ndarray or tuple
            The input vector for which the internal boundary product is to be calculated.
        
        Returns
        -------
        list
            The list of internal boundary product of the input vector.
        
        Raises
        ------
        Exception
            If the input is not a 1-D numpy array or a tuple.
        
        Examples
        --------
        # Example 1: Using a 1-D numpy array
        >>> vector = np.array([1, 2, 3])
        >>> obj.internal_boundary_product(vector)
        [(1,), (2,), (3,)]

        # Example 2: Using a tuple
        >>> vector = (1, 2, 3)
        >>> obj.internal_boundary_product(vector)
        [(1,), (2,), (3,)]

        # Example 3: Using a 2-D numpy array
        >>> vector = np.array([[1, 2, 3], [4, 5, 6]])
        >>> obj.internal_boundary_product(vector)
        An error occurred: The input must be a 1-D array or a tuple.
        """
        try:
            vector_ = np.array(vector)
            if len(vector_.shape) > 1:
                raise Exception("The input must be a 1-D array or a tuple.")
            if isinstance(vector_, np.ndarray):
                product_list = (vector_,)
            elif isinstance(vector_, tuple):
                product_list = vector_
            else:
                raise Exception("The input must be a numpy array or a tuple.")
            return list(product(*product_list))
        except Exception as e:
            print("An error occurred: ", e)