@@ -217,15 +217,17 @@ def from_function(
217
217
function_handle : Callable [[Tuple [int , ...]], np .ndarray ],
218
218
shape : Shape ,
219
219
) -> tensor :
220
- """Construct a :class:`pyttb.tensor` with data generated by function.
220
+ """Construct :class:`pyttb.tensor` with data generated by given function.
221
221
222
222
Parameters
223
223
----------
224
224
function_handle:
225
- A function that can accept an integer length and
226
- return a :class:`numpy.ndarray` vector of that length.
225
+ A function that takes a tuple of integers and returns a
226
+ :class:`numpy.ndarray`. The array should be in Fortran order to avoid
227
+ warnings of data being copied. The data will be reshaped to the shape,
228
+ so returning a vector of length equal to the product of the shape is fine.
227
229
shape:
228
- Shape of the resulting tensor.
230
+ Shape of the resulting tensor; e.g., a tuple of integers .
229
231
230
232
Returns
231
233
-------
@@ -236,46 +238,46 @@ def from_function(
236
238
Create a :class:`pyttb.tensor` with entries drawn from a normal distribution
237
239
using :func:`numpy.random.randn`::
238
240
239
- >>> np.random.seed(0)
240
- >>> T = ttb.tensor.from_function(np.random.randn, (4, 3, 2))
241
+ >>> randn = lambda s : np.random.randn(np.prod(s))
242
+ >>> np.random.seed(0) # reproducibility
243
+ >>> T = ttb.tensor.from_function(randn, (4, 3, 2))
241
244
>>> print(T)
242
245
tensor of shape (4, 3, 2) with order F
243
246
data[:, :, 0] =
244
247
[[ 1.76405235 1.86755799 -0.10321885]
245
- [ 0.40015721 -0.97727788 0.4105985 ]
246
- [ 0.97873798 0.95008842 0.14404357]
247
- [ 2.2408932 -0.15135721 1.45427351]]
248
+ [ 0.40015721 -0.97727788 0.4105985 ]
249
+ [ 0.97873798 0.95008842 0.14404357]
250
+ [ 2.2408932 -0.15135721 1.45427351]]
248
251
data[:, :, 1] =
249
252
[[ 0.76103773 1.49407907 -2.55298982]
250
- [ 0.12167502 -0.20515826 0.6536186 ]
251
- [ 0.44386323 0.3130677 0.8644362 ]
252
- [ 0.33367433 -0.85409574 -0.74216502]]
253
+ [ 0.12167502 -0.20515826 0.6536186 ]
254
+ [ 0.44386323 0.3130677 0.8644362 ]
255
+ [ 0.33367433 -0.85409574 -0.74216502]]
253
256
254
257
Create a :class:`pyttb.tensor` with all entries equal to 1
255
258
using :func:`numpy.ones`::
256
259
257
- >>> T = ttb.tensor.from_function(np.ones, (2, 3, 4))
260
+ >>> T = ttb.tensor.from_function(lambda s: np.ones(s,order='F') , (2, 3, 4))
258
261
>>> print(T)
259
262
tensor of shape (2, 3, 4) with order F
260
263
data[:, :, 0] =
261
264
[[1. 1. 1.]
262
- [1. 1. 1.]]
265
+ [1. 1. 1.]]
263
266
data[:, :, 1] =
264
267
[[1. 1. 1.]
265
- [1. 1. 1.]]
268
+ [1. 1. 1.]]
266
269
data[:, :, 2] =
267
270
[[1. 1. 1.]
268
- [1. 1. 1.]]
271
+ [1. 1. 1.]]
269
272
data[:, :, 3] =
270
273
[[1. 1. 1.]
271
- [1. 1. 1.]]
274
+ [1. 1. 1.]]
272
275
"""
273
276
# Check size
274
277
shape = parse_shape (shape )
275
278
276
279
# Generate data
277
- totalsize = prod (shape )
278
- data = function_handle (totalsize )
280
+ data = function_handle (shape )
279
281
280
282
# Create the tensor
281
283
return cls (data , shape , copy = False )
0 commit comments