|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// BSD 3-Clause License |
| 3 | +// |
| 4 | +// Copyright (c) 2021, NVIDIA Corporation |
| 5 | +// All rights reserved. |
| 6 | +// |
| 7 | +// Redistribution and use in source and binary forms, with or without |
| 8 | +// modification, are permitted provided that the following conditions are met: |
| 9 | +// |
| 10 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | +// list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | +// this list of conditions and the following disclaimer in the documentation |
| 15 | +// and/or other materials provided with the distribution. |
| 16 | +// |
| 17 | +// 3. Neither the name of the copyright holder nor the names of its |
| 18 | +// contributors may be used to endorse or promote products derived from |
| 19 | +// this software without specific prior written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 25 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +///////////////////////////////////////////////////////////////////////////////// |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | + |
| 36 | +#include "matx/core/type_utils.h" |
| 37 | +#include "matx/operators/base_operator.h" |
| 38 | + |
| 39 | +namespace matx |
| 40 | +{ |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the polynomial evaluated at each point |
| 44 | + */ |
| 45 | + namespace detail { |
| 46 | + template <typename Op, typename Coeffs> |
| 47 | + class PolyvalOp : public BaseOp<PolyvalOp<Op, Coeffs>> |
| 48 | + { |
| 49 | + private: |
| 50 | + Op op_; |
| 51 | + Coeffs coeffs_; |
| 52 | + |
| 53 | + public: |
| 54 | + using matxop = bool; |
| 55 | + using scalar_type = typename Op::scalar_type; |
| 56 | + |
| 57 | + __MATX_INLINE__ std::string str() const { return "polyval()"; } |
| 58 | + __MATX_INLINE__ PolyvalOp(const Op &op, const Coeffs &coeffs) : op_(op), coeffs_(coeffs) { |
| 59 | + MATX_STATIC_ASSERT_STR(coeffs.Rank() == 1, matxInvalidDim, "Coefficient must be rank 1"); |
| 60 | + MATX_STATIC_ASSERT_STR(op.Rank() == 1, matxInvalidDim, "Input operator must be rank 1"); |
| 61 | + }; |
| 62 | + |
| 63 | + __MATX_INLINE__ __MATX_DEVICE__ __MATX_HOST__ auto operator()(index_t idx) const |
| 64 | + { |
| 65 | + // Horner's method for computing polynomial |
| 66 | + scalar_type ttl{coeffs_(0)}; |
| 67 | + for(int i = 1; i < coeffs_.Size(0); i++) { |
| 68 | + ttl = ttl * op_(idx) + coeffs_(i); |
| 69 | + } |
| 70 | + |
| 71 | + return ttl; |
| 72 | + } |
| 73 | + |
| 74 | + static __MATX_INLINE__ constexpr __MATX_HOST__ __MATX_DEVICE__ int32_t Rank() |
| 75 | + { |
| 76 | + return 1; |
| 77 | + } |
| 78 | + constexpr __MATX_INLINE__ __MATX_HOST__ __MATX_DEVICE__ index_t Size([[maybe_unused]] int dim) const |
| 79 | + { |
| 80 | + return op_.Size(0); |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * @brief Evaluate a polynomial |
| 88 | + * |
| 89 | + * Currently only allows 1D input and coefficients |
| 90 | + * |
| 91 | + * @tparam Op Type of input values to evaluate |
| 92 | + * @tparam Coeffs Type of coefficients |
| 93 | + * @param op Input values to evaluate |
| 94 | + * @param coeffs Coefficient values |
| 95 | + * @return polyval operator |
| 96 | + */ |
| 97 | + template <typename Op, typename Coeffs> |
| 98 | + __MATX_INLINE__ auto polyval(const Op &op, const Coeffs &coeffs) { |
| 99 | + return detail::PolyvalOp(op, coeffs); |
| 100 | + } |
| 101 | +} // end namespace matx |
0 commit comments