Skip to content

Commit bdc7e61

Browse files
tgkoldadmdunla
andauthored
Updating documentation of pyttb.tensor.from_function. (#425)
* Updating documentation of `pyttb.tensor.from_function`. Added an example showing how to create a tensor with normal random entries, for which there is no current shortcut like `tenones`. Used that example to show how it's arguably best to pass in a vector rather than trying to worry about forcing Fortran ordering. Also updated the example of create an all ones tensor. The previous documentation relied on some function called `fortran_order_one`, and I'm not sure what that is. Instead, I changed it to the `numpy.ones` function and to specify the fortran order via its interface. I think these examples will be more helpful to users. * Fixing ruff error. --------- Co-authored-by: Danny Dunlavy <[email protected]>
1 parent 856b53c commit bdc7e61

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

Diff for: pyttb/tensor.py

+41-18
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ def from_function(
223223
function_handle:
224224
A function that can accept a shape (i.e., :class:`tuple` of
225225
dimension sizes) and return a :class:`numpy.ndarray` of that shape.
226-
`numpy.zeros`, `numpy.ones`.
226+
The array returned by the function should ideally be in Fortran order.
227+
If that is not the case, an expensive reordering of the data will be
228+
required. One way to avoid this reordering is to return a 1D vector.
227229
shape:
228230
Shape of the resulting tensor.
229231
@@ -233,24 +235,45 @@ def from_function(
233235
234236
Examples
235237
--------
236-
Create a :class:`pyttb.tensor` with entries equal to 1:
238+
Create a :class:`pyttb.tensor` with entries drawn from a normal distribution
239+
using :func:`numpy.random.randn`. Observe that we actually generate a vector to
240+
avoid having a C-ordered array (the default if we had provided the shape array)
241+
be rearranged as a F-ordered array::
242+
243+
>>> randn = lambda s : np.random.randn(np.prod(s))
244+
>>> np.random.seed(0) # reproducibility
245+
>>> T = ttb.tensor.from_function(randn, (4, 3, 2))
246+
>>> print(T)
247+
tensor of shape (4, 3, 2) with order F
248+
data[:, :, 0] =
249+
[[ 1.76405235 1.86755799 -0.10321885]
250+
[ 0.40015721 -0.97727788 0.4105985 ]
251+
[ 0.97873798 0.95008842 0.14404357]
252+
[ 2.2408932 -0.15135721 1.45427351]]
253+
data[:, :, 1] =
254+
[[ 0.76103773 1.49407907 -2.55298982]
255+
[ 0.12167502 -0.20515826 0.6536186 ]
256+
[ 0.44386323 0.3130677 0.8644362 ]
257+
[ 0.33367433 -0.85409574 -0.74216502]]
237258
238-
>>> fortran_order_ones = lambda shape: np.ones(shape=shape, order="F")
239-
>>> T = ttb.tensor.from_function(fortran_order_ones, (2, 3, 4))
240-
>>> print(T)
241-
tensor of shape (2, 3, 4) with order F
242-
data[:, :, 0] =
243-
[[1. 1. 1.]
244-
[1. 1. 1.]]
245-
data[:, :, 1] =
246-
[[1. 1. 1.]
247-
[1. 1. 1.]]
248-
data[:, :, 2] =
249-
[[1. 1. 1.]
250-
[1. 1. 1.]]
251-
data[:, :, 3] =
252-
[[1. 1. 1.]
253-
[1. 1. 1.]]
259+
Create a :class:`pyttb.tensor` with all entries equal to 1 using
260+
:func:`numpy.ones`. Observe that we specifically specify Fortran order::
261+
262+
>>> T = ttb.tensor.from_function(lambda s: np.ones(s,order='F'), (2, 3, 4))
263+
>>> print(T)
264+
tensor of shape (2, 3, 4) with order F
265+
data[:, :, 0] =
266+
[[1. 1. 1.]
267+
[1. 1. 1.]]
268+
data[:, :, 1] =
269+
[[1. 1. 1.]
270+
[1. 1. 1.]]
271+
data[:, :, 2] =
272+
[[1. 1. 1.]
273+
[1. 1. 1.]]
274+
data[:, :, 3] =
275+
[[1. 1. 1.]
276+
[1. 1. 1.]]
254277
"""
255278
# Check size
256279
shape = parse_shape(shape)

0 commit comments

Comments
 (0)