Skip to content

Commit 63da0cd

Browse files
tgkoldadmdunla
andauthored
Updating documentation for pyttb.tensor.__init__. (#424)
- Giving more explanation of input arguments. - Removing statement about row vector because it shouldn't matter. - Adding more pertinent examples, including constructing a tensor from a 3D numpy array and from a 1D numpy array. - Moved the example of an empty constructor to be last (not sure it's even needed). - Removed 2D conversion example. Co-authored-by: Danny Dunlavy <[email protected]>
1 parent 0415b43 commit 63da0cd

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

pyttb/tensor.py

+57-24
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,70 @@ def __init__(
8787
shape: Optional[Shape] = None,
8888
copy: bool = True,
8989
):
90-
"""Create a :class:`pyttb.tensor` from a :class:`numpy.ndarray`.
91-
92-
Note that 1D tensors (i.e., when len(shape)==1) contains a data
93-
array that follow the Numpy convention of being a row vector.
90+
"""
91+
Create a :class:`pyttb.tensor`.
9492
9593
Parameters
9694
----------
97-
data:
98-
Tensor source data.
99-
shape:
100-
Shape of resulting tensor if not the same as data shape.
101-
copy:
102-
Whether to make a copy of provided data or just reference it.
95+
data : optional
96+
Source data as :class:`numpy.ndarray`
97+
shape : optional
98+
Shape of the tensor as a :class:`tuple` or any iterable array of integers.
99+
A single integer means that the tensor should be a 1D array.
100+
If no shape is given, defaults to :attr:`numpy.ndarray.shape` of ``data``.
101+
Otherwise, the data is reshaped to the specified shape.
102+
copy : optional
103+
Whether to deep copy (versus reference) the data.
104+
By default, the data is deep copied.
103105
104106
Examples
105107
--------
106-
Create an empty :class:`pyttb.tensor`:
107-
108-
>>> T = ttb.tensor()
109-
>>> print(T)
110-
empty tensor of shape ()
111-
data = []
108+
Create a :class:`pyttb.tensor` from a three-way :class:`numpy.ndarray`::
109+
110+
>>> data = np.array([[[1,13],[5,17],[9,21]],
111+
... [[2,14],[6,18],[10,22]],
112+
... [[3,15],[7,19],[11,23]],
113+
... [[4,16],[8,20],[12,24]]])
114+
>>> T = ttb.tensor(data)
115+
>>> print(T)
116+
tensor of shape (4, 3, 2) with order F
117+
data[:, :, 0] =
118+
[[ 1 5 9]
119+
[ 2 6 10]
120+
[ 3 7 11]
121+
[ 4 8 12]]
122+
data[:, :, 1] =
123+
[[13 17 21]
124+
[14 18 22]
125+
[15 19 23]
126+
[16 20 24]]
127+
128+
Create a :class:`pyttb.tensor` from a :class:`numpy.ndarray` vector and
129+
reshape it::
130+
131+
>>> data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
132+
... 17, 18, 19, 20, 21, 22, 23, 24])
133+
>>> T = ttb.tensor(data, shape=(4, 3, 2))
134+
>>> print(T)
135+
tensor of shape (4, 3, 2) with order F
136+
data[:, :, 0] =
137+
[[ 1 5 9]
138+
[ 2 6 10]
139+
[ 3 7 11]
140+
[ 4 8 12]]
141+
data[:, :, 1] =
142+
[[13 17 21]
143+
[14 18 22]
144+
[15 19 23]
145+
[16 20 24]]
146+
147+
Create an empty :class:`pyttb.tensor`::
148+
149+
>>> T = ttb.tensor()
150+
>>> print(T)
151+
empty tensor of shape ()
152+
data = []
112153
113-
Create a :class:`pyttb.tensor` from a :class:`numpy.ndarray`:
114-
115-
>>> T = ttb.tensor(np.array([[1, 2], [3, 4]]))
116-
>>> print(T)
117-
tensor of shape (2, 2) with order F
118-
data[:, :] =
119-
[[1 2]
120-
[3 4]]
121154
"""
122155
if data is None:
123156
# EMPTY / DEFAULT CONSTRUCTOR

0 commit comments

Comments
 (0)