[linalg.scaled] # 29 Numerics library [[numerics]](./#numerics) ## 29.9 Basic linear algebra algorithms [[linalg]](linalg#scaled) ### 29.9.8 Scaled in-place transformation [linalg.scaled] #### [29.9.8.1](#intro) Introduction [[linalg.scaled.intro]](linalg.scaled.intro) [1](#intro-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12572) The scaled function takes a value alpha and an mdspan x, and returns a new read-only mdspan that represents the elementwise product of alpha with each element of x[.](#intro-1.sentence-1) [*Example [1](#intro-example-1)*: using Vec = mdspan>; // z = alpha * x + yvoid z_equals_alpha_times_x_plus_y(double alpha, Vec x, Vec y, Vec z) { add(scaled(alpha, x), y, z);}// z = alpha * x + beta * yvoid z_equals_alpha_times_x_plus_beta_times_y(double alpha, Vec x, double beta, Vec y, Vec z) { add(scaled(alpha, x), scaled(beta, y), z);} — *end example*] #### [29.9.8.2](#scaledaccessor) Class template scaled_accessor [[linalg.scaled.scaledaccessor]](linalg.scaled.scaledaccessor) [1](#scaledaccessor-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12596) The class template scaled_accessor is an mdspan accessor policy which upon access produces scaled elements[.](#scaledaccessor-1.sentence-1) It is part of the implementation of scaled ([[linalg.scaled.scaled]](#scaled "29.9.8.3 Function template scaled"))[.](#scaledaccessor-1.sentence-2) namespace std::linalg {templateclass [scaled_accessor](#lib:scaled_accessor "29.9.8.2 Class template scaled_­accessor [linalg.scaled.scaledaccessor]") {public:using element_type = add_const_t() * declval())>; using reference = remove_const_t; using data_handle_type = NestedAccessor::data_handle_type; using offset_policy = scaled_accessor; constexpr scaled_accessor() = default; templateexplicit(!is_convertible_v)constexpr scaled_accessor(const scaled_accessor& other); constexpr scaled_accessor(const ScalingFactor& s, const NestedAccessor& a); constexpr reference access(data_handle_type p, size_t i) const; constexpr offset_policy::data_handle_type offset(data_handle_type p, size_t i) const; constexpr const ScalingFactor& scaling_factor() const noexcept { return *scaling-factor*; }constexpr const NestedAccessor& nested_accessor() const noexcept { return *nested-accessor*; }private: ScalingFactor *scaling-factor*{}; // *exposition only* NestedAccessor *nested-accessor*{}; // *exposition only*};} [2](#scaledaccessor-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12631) *Mandates*: - [(2.1)](#scaledaccessor-2.1) element_type is valid and denotes a type, - [(2.2)](#scaledaccessor-2.2) is_copy_constructible_v is true, - [(2.3)](#scaledaccessor-2.3) is_reference_v is false, - [(2.4)](#scaledaccessor-2.4) ScalingFactor models [semiregular](concepts.object#concept:semiregular "18.6 Object concepts [concepts.object]"), and - [(2.5)](#scaledaccessor-2.5) NestedAccessor meets the accessor policy requirements ([[mdspan.accessor.reqmts]](mdspan.accessor.reqmts "23.7.3.5.2 Requirements"))[.](#scaledaccessor-2.sentence-1) [🔗](#lib:scaled_accessor,constructor) `template explicit(!is_convertible_v) constexpr scaled_accessor(const scaled_accessor& other); ` [3](#scaledaccessor-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12654) *Constraints*: is_constructible_v is true[.](#scaledaccessor-3.sentence-1) [4](#scaledaccessor-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12658) *Effects*: - [(4.1)](#scaledaccessor-4.1) Direct-non-list-initializes *scaling-factor* with other.scaling_factor(), and - [(4.2)](#scaledaccessor-4.2) direct-non-list-initializes *nested-accessor* with other.nested_accessor()[.](#scaledaccessor-4.sentence-1) [🔗](#lib:scaled_accessor,constructor_) `constexpr scaled_accessor(const ScalingFactor& s, const NestedAccessor& a); ` [5](#scaledaccessor-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12676) *Effects*: - [(5.1)](#scaledaccessor-5.1) Direct-non-list-initializes *scaling-factor* with s, and - [(5.2)](#scaledaccessor-5.2) direct-non-list-initializes *nested-accessor* with a[.](#scaledaccessor-5.sentence-1) [🔗](#lib:scaled_accessor,access) `constexpr reference access(data_handle_type p, size_t i) const; ` [6](#scaledaccessor-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12692) *Returns*: scaling_factor() * NestedAccessor::element_type(*nested-accessor*.access(p, i)) [🔗](#lib:scaled_accessor,offset) `constexpr offset_policy::data_handle_type offset(data_handle_type p, size_t i) const; ` [7](#scaledaccessor-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12705) *Returns*: *nested-accessor*.offset(p, i) #### [29.9.8.3](#scaled) Function template scaled [[linalg.scaled.scaled]](linalg.scaled.scaled) [1](#scaled-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12712) The scaled function template takes a scaling factor alpha and an mdspan x, and returns a new read-only mdspan with the same domain as x, that represents the elementwise product of alpha with each element of x[.](#scaled-1.sentence-1) [🔗](#lib:scaled) ` template constexpr auto scaled(ScalingFactor alpha, mdspan x); ` [2](#scaled-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12728) Let SA be scaled_accessor[.](#scaled-2.sentence-1) [3](#scaled-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12731) *Returns*: mdspan(x.data_handle(), x.mapping(), SA(alpha, x.accessor())) [4](#scaled-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L12739) [*Example [1](#scaled-example-1)*: void test_scaled(mdspan> x){auto x_scaled = scaled(5.0, x); for (int i = 0; i < x.extent(0); ++i) { assert(x_scaled[i] == 5.0 * x[i]); }} — *end example*]