📄 piecewisefn.h
字号:
/****************************************************************************
* Copyright (C) 2001 Piece Wise Functions
* Carlo Galotto
* Italy
* Email: carlo.galotto@libero.it
* cgalotto@hotmail.com
*
*
* This framework is free software; you can redistribute it and/or modify
* it. This framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* The proposed framework is based upon the paper "Expression Templates" by
* Todd Veldhuizen.
****************************************************************************/
// Class: PieceWiseFn
#ifndef __PIECEWISEFN_H //Required for current class
#define __PIECEWISEFN_H
#include "FnTraits.h"
#include "Expr.h"#include "BinExpr.h"#include "UnaryExpr.h"#include "LiteralExpr.h"#include "IdentityExpr.h"
#include "BinaryFn.h"
#include "Variable.h"
#include "Literal.h"
#include "FnOperators.h"
#include <vector> // underlieing objects
using namespace std;
template <class argT, class resT = argT>
class PieceWiseFn : public BinaryFn<argT, resT>
{
public:
typedef typename argT arg_t;
typedef typename resT res_t;
private:
std::vector< BinaryFn< arg_t, res_t> > y;
std::vector< BinaryFn< arg_t, bool> > domain;
private:
enum CheckDomain_t { cdoInDomain, cdoNotInDomain, cdoOverlapping };
// Check if x is in the domain of the function
CheckDomain_t CheckDomain(const argT & x, size_t & index) const
{
CheckDomain_t res = cdoNotInDomain;
size_t n = 0;
for(size_t i = 0; i < domain.size(); ++i)
if( domain[i](x) ) {
++n; index = i;
res = cdoInDomain;
}
if(n > 1) res = cdoOverlapping;
return res;
}
public:
//Default constructor
PieceWiseFn() : BinaryFn<argT, resT>(), y(), domain() {}
// Template constructor
template <class exprT, class domT>
PieceWiseFn(const exprT& e, const domT & d) : y(), domain()
{
y.push_back ( BinaryFn<argT,resT>( &ExprAdapter<exprT>(e)) );
domain.push_back( BinaryFn<argT,bool>( &ExprAdapter<domT>(d)) );
}
// Template constructor domain = {x e argT}
template <class exprT>
PieceWiseFn(const exprT& e) : y(), domain()
{
Literal<argT, bool> alwaysTrue = true;
y.push_back ( BinaryFn<argT,resT>( new ExprAdapter<exprT>(e)) );
domain.push_back( BinaryFn<argT, bool>(FunctionMaker(alwaysTrue)) );
}
// Add another piece wise to the fn
template <class exprT, class domT>
PieceWiseFn & Add(const exprT& e, const domT & d)
{
y.push_back( BinaryFn<argT,resT>( new ExprAdapter<exprT>(e)) );
domain.push_back( BinaryFn<argT,bool>( new ExprAdapter<domT>(d)) );
return *this;
}
// Add another piece wise to the fn with domain = {x e argT}
template <class exprT>
PieceWiseFn & Add(const exprT& e)
{
Literal<argT, bool> alwaysTrue = true;
y.push_back( BinaryFn<argT,resT>( new ExprAdapter<exprT>(e)) );
domain.push_back( BinaryFn<argT,bool>(FunctionMaker(alwaysTrue)) );
return *this;
}
// evaluator
resT operator ()(const argT& x) const
{
size_t i;
CheckDomain_t res = CheckDomain(x, i);
if(res == cdoInDomain || res == cdoOverlapping) return y[i](x);
return resT(0);
}
~PieceWiseFn() {}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -