fill#
Generate an operator whose elements are all the same caller-supplied value.
fill() has a shape-taking form (rank inferred from the shape) and a
shapeless form. The shapeless form is preferred in contexts where the shape
can be deduced by the surrounding expression, e.g. as a broadcastable scalar
inside an operator expression. fill<T>({}, value) produces a rank-0
operator whose single element is value.
Unlike ones and zeros, fill has no
default for T: the value type is either deduced from value or
specified explicitly so that an int literal does not silently collapse a
floating-point fill to an integer operator.
-
template<typename T, typename ShapeType>
inline auto matx::fill(ShapeType &&s, T value)# Return value for all elements
Creates an operator that returns the same value at every index. It can be used in place of memset to fill all elements with a chosen value, or as a zero-storage stand-in for a scalar in operator expressions and kernel arguments that expect a MatX op.
- Template Parameters:
T – Data type. Has no default; it is either deduced from
valueor specified explicitly. Omitting a default forces deduction so thatfill(shape, 3.14f)produces a float-valued operator rather than collapsing the value to an unrelated default type.- Parameters:
s – Shape of tensor
value – Value to fill
-
template<typename T, int RANK>
inline auto matx::fill(const index_t (&s)[RANK], T value)# Return value for all elements
Array-shape overload. See the ShapeType-taking overload for details.
- Template Parameters:
T – Data type
- Parameters:
s – Shape of tensor
value – Value to fill
-
template<typename T>
inline auto matx::fill(const std::initializer_list<detail::no_size_t> s, T value)# Return value for all elements
Rank-0 overload.
fill<T>({}, value)produces a rank-0 ConstVal whose single element isvalue. Mirrors the empty-bracemake_tensor<T>({})pattern.- Template Parameters:
T – Data type
- Parameters:
s – Empty initializer list
{}; unused, present to enable braced-init overload resolution for the rank-0 case.value – Value to fill
-
template<typename T>
inline auto matx::fill(T value)# Return value for all elements
Shapeless form. The resulting operator has Rank() == matxNoRank and can be used in contexts where the shape can be deduced (e.g. broadcast in an operator expression).
- Template Parameters:
T – Data type
- Parameters:
value – Value to fill
Examples#
Shape-taking form, fills a 1D tensor:
// Note: for trivial "set every element to value", a bare scalar works too:
// (t1 = value).run(exec);
// fill() is shown here for the shape-taking API surface; its real value is
// in slots that require an actual MatX operator (see fill-gen-test-2).
const index_t count = 100;
auto t1 = make_tensor<TestType>({count});
const TestType value = static_cast<TestType>(7);
(t1 = fill<TestType>({count}, value)).run(exec);
fill() used in a slot that requires a MatX operator. zipvec is
templated on operator types, so a bare scalar in the third slot fails to
compile; fill<float>(5.0f) is the zero-storage adapter that satisfies
the slot:
const index_t H = 8, W = 8;
auto x = clone<2>(linspace<float>(-1.0f, 1.0f, W), {H, matxKeepDim});
auto y = clone<2>(linspace<float>(-1.0f, 1.0f, H), {matxKeepDim, W});
// matx::zipvec(x, y, 5.0f) does not compile: zipvec expects MatX ops on
// every slot, and 5.0f has no Rank() / value_type. fill<float>({H, W}, 5.0f)
// is a zero-storage rank-2 op that satisfies the slot without allocating
// an H*W tensor of constants.
auto voxels = zipvec(x, y, fill<float>({H, W}, 5.0f));
auto out = make_tensor<float3>({H, W});
(out = voxels).run(exec);
Rank-0 fill via the {} shape, for APIs that specifically expect a 0D
operator:
// Rank-0 fill via the empty-brace shape. Note: for plain assignment a
// scalar `(t = value).run(exec)` works too; the rank-0 fill form is for
// APIs that specifically expect a 0D MatX operator (e.g. sar_bp's
// range_to_mcp parameter).
auto t = make_tensor<TestType>({});
const TestType value = static_cast<TestType>(17);
(t = fill<TestType>({}, value)).run(exec);