File indexing completed on 2026-08-16 09:21:38
0001 #ifndef TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_IS
0002 #define TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_IS
0003
0004 #include <TMVA/ROperator.hxx>
0005 #include <TMVA/RModel.hxx>
0006 #include <TMVA/SOFIE_common.hxx>
0007 #include <cmath>
0008
0009 namespace TMVA {
0010 namespace Experimental {
0011 namespace SOFIE {
0012
0013 enum class EBasicIsOperator { kIsInf, kIsInfPos, kIsInfNeg, kIsNaN };
0014
0015 template <EBasicIsOperator Op>
0016 struct IsOpTraits {
0017 };
0018 template<>
0019 struct IsOpTraits<EBasicIsOperator::kIsInf> {
0020 static std::string Name() { return "IsInf"; }
0021 static std::string Op(const std::string &x) { return "std::isinf(" + x + ")"; }
0022 static bool Impl(float x) { return std::isinf(x);}
0023 };
0024 template<>
0025 struct IsOpTraits<EBasicIsOperator::kIsInfPos> {
0026 static std::string Name() { return "IsInfPos"; }
0027 static std::string Op(const std::string &x) { return "(std::isinf(" + x + ") && " + x + "> 0)"; }
0028 static bool Impl(float x) { return std::isinf(x) && x > 0;}
0029 };
0030 template<>
0031 struct IsOpTraits<EBasicIsOperator::kIsInfNeg> {
0032 static std::string Name() { return "IsInfNeg"; }
0033 static std::string Op(const std::string &x) { return "(std::isinf(" + x + ") && " + x + "< 0)"; }
0034 static bool Impl(float x) { return std::isinf(x) && x < 0;}
0035 };
0036 template<>
0037 struct IsOpTraits<EBasicIsOperator::kIsNaN> {
0038 static std::string Name() { return "IsInf"; }
0039 static std::string Op(const std::string &x) { return "std::isnan(" + x + ")"; }
0040 static bool Impl(float x) { return std::isnan(x);}
0041 };
0042
0043
0044
0045 template <EBasicIsOperator Op>
0046 class ROperator_Basic_Is final : public ROperator {
0047 private:
0048 std::string fNX;
0049 std::string fNY;
0050
0051 std::vector<Dim> fShapeX;
0052 std::vector<Dim> fShapeY;
0053
0054 public:
0055 ROperator_Basic_Is() {}
0056
0057 ROperator_Basic_Is(std::string nameX, std::string nameY)
0058 : fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
0059 {
0060 fInputTensorNames = { fNX };
0061 fOutputTensorNames = { fNY };
0062 }
0063
0064 void Initialize(RModel& model) override {
0065 if (!model.CheckIfTensorAlreadyExist(fNX)) {
0066 throw std::runtime_error("TMVA::SOFIE - Tensor " + fNX + " not found.");
0067 }
0068 fShapeX = model.GetDimTensorShape(fNX);
0069 fShapeY = fShapeX;
0070 model.AddIntermediateTensor(fNY, ETensorType::BOOL, fShapeY);
0071 }
0072
0073 std::string Generate(std::string opName) override
0074 {
0075 opName = "op_" + opName;
0076 std::stringstream out;
0077
0078 out << SP << "\n//---- Operator" << IsOpTraits<Op>::Name() << " " << opName << "\n";
0079 auto length = ConvertDimShapeToLength(fShapeX);
0080 out << SP << "for (size_t i = 0; i < " << length << "; i++) {\n";
0081 out << SP << SP << "tensor_" << fNY << "[i] = " << IsOpTraits<Op>::Op("tensor_" + fNX + "[i]") << ";\n";
0082 out << SP << "}\n";
0083 return out.str();
0084 }
0085
0086 std::vector<std::string> GetStdLibs() override {
0087 return { std::string("cmath") };
0088 }
0089 };
0090
0091 }
0092 }
0093 }
0094
0095 #endif