-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bab040
commit ff6e98d
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _polyval_func: | ||
|
||
polyval | ||
======= | ||
|
||
Evaluate a polynomial given an input sequence and coefficients | ||
|
||
.. doxygenfunction:: polyval(const Op &op, const Coeffs &coeffs) | ||
|
||
Examples | ||
~~~~~~~~ | ||
|
||
.. literalinclude:: ../../../test/00_operators/OperatorTests.cu | ||
:language: cpp | ||
:start-after: example-begin polyval-test-1 | ||
:end-before: example-end polyval-test-1 | ||
:dedent: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// BSD 3-Clause License | ||
// | ||
// Copyright (c) 2021, NVIDIA Corporation | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this | ||
// list of conditions and the following disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
///////////////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
|
||
|
||
#include "matx/core/type_utils.h" | ||
#include "matx/operators/base_operator.h" | ||
|
||
namespace matx | ||
{ | ||
|
||
/** | ||
* Returns the polynomial evaluated at each point | ||
*/ | ||
namespace detail { | ||
template <typename Op, typename Coeffs> | ||
class PolyvalOp : public BaseOp<PolyvalOp<Op, Coeffs>> | ||
{ | ||
private: | ||
Op op_; | ||
Coeffs coeffs_; | ||
|
||
public: | ||
using matxop = bool; | ||
using scalar_type = typename Op::scalar_type; | ||
|
||
__MATX_INLINE__ std::string str() const { return "polyval()"; } | ||
__MATX_INLINE__ PolyvalOp(const Op &op, const Coeffs &coeffs) : op_(op), coeffs_(coeffs) { | ||
MATX_STATIC_ASSERT_STR(coeffs.Rank() == 1, matxInvalidDim, "Coefficient must be rank 1"); | ||
MATX_STATIC_ASSERT_STR(op.Rank() == 1, matxInvalidDim, "Input operator must be rank 1"); | ||
}; | ||
|
||
__MATX_INLINE__ __MATX_DEVICE__ __MATX_HOST__ auto operator()(index_t idx) const | ||
{ | ||
scalar_type ttl{0}; | ||
for (int c = 0; c < coeffs_.Size(0); c++) { | ||
ttl += coeffs_(c) * std::pow(op_(idx), coeffs_.Size(0) - c - 1); | ||
} | ||
return ttl; | ||
} | ||
|
||
static __MATX_INLINE__ constexpr __MATX_HOST__ __MATX_DEVICE__ int32_t Rank() | ||
{ | ||
return 1; | ||
} | ||
constexpr __MATX_INLINE__ __MATX_HOST__ __MATX_DEVICE__ index_t Size([[maybe_unused]] int dim) const | ||
{ | ||
return op_.Size(0); | ||
} | ||
}; | ||
} | ||
|
||
|
||
/** | ||
* @brief Evaluate a polynomial | ||
* | ||
* Currently only allows 1D input and coefficients | ||
* | ||
* @tparam Op Type of input values to evaluate | ||
* @tparam Coeffs Type of coefficients | ||
* @param op Input values to evaluate | ||
* @param coeffs Coefficient values | ||
* @return polyval operator | ||
*/ | ||
template <typename Op, typename Coeffs> | ||
__MATX_INLINE__ auto polyval(const Op &op, const Coeffs &coeffs) { | ||
return detail::PolyvalOp(op, coeffs); | ||
} | ||
} // end namespace matx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters