Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty printing of tensor #356

Closed
tgkolda opened this issue Dec 12, 2024 · 1 comment
Closed

Pretty printing of tensor #356

tgkolda opened this issue Dec 12, 2024 · 1 comment

Comments

@tgkolda
Copy link
Collaborator

tgkolda commented Dec 12, 2024

I would like to propose prettier printing of tensors. Here is an example of the code that I would suggest for a dense tensor. Ideally, this would be the default print. It allows the user to indicate the formatting and the name. It always prints the frontal slices, regardless of the underlying storage format.

def pretty_print_tensor(X, fmt="10.4f", name="Slice"):
    if not isinstance(X, ttb.tensor):
        raise ValueError("Input must be a pyttb tensor")

    # Get the shape of the tensor
    shape = X.shape

    if name == "Slice":
        print("Tensor is of shape "+ " x ".join(map(str, shape)))
    else:
        print(f"{name} is a tensor of shape " + " x ".join(map(str, shape)))


    # Iterate over all possible slices (in Fortran order)
    for index in np.ndindex(shape[2:][::-1]): # Skip the first two dimensions and reverse the order
        index = index[::-1] # Reverse the order back to the original
        # Construct the slice indices
        slice_indices = (slice(None), slice(None)) + index
        slice_data = X[slice_indices]
        print(f"{name}(:, :, {', '.join(map(str, index))}) =")
        array = slice_data.data
        for row in array:
            print(" ".join(f"{val:{fmt}}" for val in row))
@ntjohnson1
Copy link
Collaborator

This is a duplicate of #350 correct?

@ntjohnson1 ntjohnson1 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants