@@ -196,7 +196,7 @@ def from_tensor_type(
196196 @classmethod
197197 def from_function (
198198 cls ,
199- function_handle : Callable [[Tuple [float , float ]], np .ndarray ],
199+ function_handle : Callable [[Tuple [int , ... ]], np .ndarray ],
200200 shape : Tuple [int , ...],
201201 nonzeros : float ,
202202 ) -> sptensor :
@@ -1049,7 +1049,7 @@ def scale(self, factor: np.ndarray, dims: Union[float, np.ndarray]) -> sptensor:
10491049
10501050 if isinstance (factor , ttb .tensor ):
10511051 shapeArray = np .array (self .shape )
1052- if np .any (factor .shape != shapeArray [dims ]):
1052+ if not np .array_equal (factor .shape , shapeArray [dims ]):
10531053 assert False , "Size mismatch in scale"
10541054 return ttb .sptensor .from_data (
10551055 self .subs ,
@@ -1058,7 +1058,7 @@ def scale(self, factor: np.ndarray, dims: Union[float, np.ndarray]) -> sptensor:
10581058 )
10591059 if isinstance (factor , ttb .sptensor ):
10601060 shapeArray = np .array (self .shape )
1061- if np .any (factor .shape != shapeArray [dims ]):
1061+ if not np .array_equal (factor .shape , shapeArray [dims ]):
10621062 assert False , "Size mismatch in scale"
10631063 return ttb .sptensor .from_data (
10641064 self .subs , self .vals * factor .extract (self .subs [:, dims ]), self .shape
@@ -2585,6 +2585,55 @@ def ttm(
25852585 return ttb .tensor .from_tensor_type (Ynt )
25862586
25872587
2588+ def sptenrand (
2589+ shape : Tuple [int , ...],
2590+ density : Optional [float ] = None ,
2591+ nonzeros : Optional [float ] = None ,
2592+ ) -> sptensor :
2593+ """
2594+ Create sptensor with entries drawn from a uniform distribution on the unit interval
2595+
2596+ Parameters
2597+ ----------
2598+ shape: Shape of resulting tensor
2599+ density: Density of resulting sparse tensor
2600+ nonzeros: Number of nonzero entries in resulting sparse tensor
2601+
2602+ Returns
2603+ -------
2604+ Constructed tensor
2605+
2606+ Example
2607+ -------
2608+ >>> X = ttb.sptenrand((2,2), nonzeros=1)
2609+ >>> Y = ttb.sptenrand((2,2), density=0.25)
2610+ """
2611+ if density is None and nonzeros is None :
2612+ raise ValueError ("Must set either density or nonzeros" )
2613+
2614+ if density is not None and nonzeros is not None :
2615+ raise ValueError ("Must set either density or nonzeros but not both" )
2616+
2617+ if density is not None and not 0 < density <= 1 :
2618+ raise ValueError (f"Density must be a fraction (0, 1] but received { density } " )
2619+
2620+ if isinstance (density , float ):
2621+ valid_nonzeros = float (np .prod (shape ) * density )
2622+ elif isinstance (nonzeros , (int , float )):
2623+ valid_nonzeros = nonzeros
2624+ else : # pragma: no cover
2625+ raise ValueError (
2626+ f"Incorrect types for density:{ density } and nonzeros:{ nonzeros } "
2627+ )
2628+
2629+ # Typing doesn't play nice with partial
2630+ # mypy issue: 1484
2631+ def unit_uniform (pass_through_shape : Tuple [int , ...]) -> np .ndarray :
2632+ return np .random .uniform (low = 0 , high = 1 , size = pass_through_shape )
2633+
2634+ return ttb .sptensor .from_function (unit_uniform , shape , valid_nonzeros )
2635+
2636+
25882637def sptendiag (
25892638 elements : np .ndarray , shape : Optional [Tuple [int , ...]] = None
25902639) -> sptensor :
0 commit comments