Skip to content

Conversation

@ntjohnson1
Copy link
Collaborator

@ntjohnson1 ntjohnson1 commented Dec 15, 2024

Adds a matlab_support category since we are enabling a printing functionality to more closely match MATLAB I expect other features (or at least printing of all our classes). This uses the core functionality that Tammy wrote from the linked issue with some reformating.

This branches off #359 so that should merge first.
Resolves: #350

>>> import numpy as np
>>> import pyttb as ttb
>>> from pyttb.matlab.matlab_support import matlab_print
>>> tensor= ttb.tensor(np.arange(16), shape=(2,2,2,2))
>>> print(tensor)
tensor of shape (2, 2, 2, 2) with order F
data[:, :, 0, 0] =
[[0 2]
 [1 3]]
data[:, :, 1, 0] =
[[4 6]
 [5 7]]
data[:, :, 0, 1] =
[[ 8 10]
 [ 9 11]]
data[:, :, 1, 1] =
[[12 14]
 [13 15]]
>>> matlab_print(tensor)
This is a tensor of shape 2 x 2 x 2 x 2
	data(:,:, 0, 0) =
		0 2
		1 3
	data(:,:, 1, 0) =
		4 6
		5 7
	data(:,:, 0, 1) =
		8 10
		9 11
	data(:,:, 1, 1) =
		12 14
		13 15
>>>matlab_print(tensor[:,:,0,0])
This is a tensor of shape 2 x 2
	data(:,:) =
		0 2
		1 3
>>>matlab_print(tensor[:,0,0,0])
This is a tensor of shape 2
	data(:) =
		0
		1
>>>matlab_print(tensor[:,0,0,0], format="5.1f", name="X")
X is a tensor of shape 2
	X(:) =
		  0.0
		  1.0
# Also support printing the underlying numpy arrays
>>>matlab_print(tensor.data)
ndarray(:,:, 0, 0) =
	0 2
	1 3
ndarray(:,:, 1, 0) =
	4 6
	5 7
ndarray(:,:, 0, 1) =
	8 10
	9 11
>>>matlab_print(tensor.data, format="5.1f", name="X")
X(:,:, 0, 0) =
	  0.0   2.0
	  1.0   3.0
X(:,:, 1, 0) =
	  4.0   6.0
	  5.0   7.0
X(:,:, 0, 1) =
	  8.0  10.0
	  9.0  11.0
X(:,:, 1, 1) =
	 12.0  14.0
	 13.0  15.0

@ntjohnson1 ntjohnson1 marked this pull request as ready for review December 15, 2024 19:41
@tgkolda
Copy link
Collaborator

tgkolda commented Dec 16, 2024

There is a huge indent at each level. Can that be lessened to something smaller and/or customizable?

@ntjohnson1
Copy link
Collaborator Author

The indent size may have been a copy paste issue. I followed the format here where each level is a single tab character.

We can set the indentation at each level to anything. You should be able to checkout my branch to try it, and the indentation level is set via the textwrap calls in matlab_utilities.

@dmdunla dmdunla merged commit 81e5217 into sandialabs:main Jan 6, 2025
9 checks passed
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

Successfully merging this pull request may close these issues.

Pretty print function for dense tensors

3 participants