2012-07-31

SCIP in C++11 ― 1.3.1節その2


問題1.30Scheme特有の話なのでパス。

問題1.31
aの再帰版のみ示す。反復版はあまり興味ないのでbはパス。
--------
template<typename TermResult, typename TermArgument,
         typename NextResult,typename NextArgument>
const TermResult product
(const function<TermResult(TermArgument)>& term,
 const NextArgument& lower,
 const function<NextResult(NextArgument)>& next,
 const NextArgument& upper)
{
    if(lower>upper){return(1);}
    return(term(lower)*product(term,next(lower),next,upper));
}

const function<int(int)> identity=[](const int x){return(x);};

template<typename ResultType>
const ResultType incrementFun(const ResultType x){return(x+1);}

const int productFactorial(const int upper)
{
    const function<int(int)> increment=incrementFun<int>;
    return(product(identity,1,increment,upper));
}
const double piProduct(const int upper)
{
    const function<double(double)>
        piProductElement=[](const double x){
        return(4.0*x*(x+1.0)/(2.0*x+1.0)/(2.0*x+1.0));
    };
    const function<double(double)> increment=incrementFun<double>;
    return(product(piProductElement,1.0,increment,
                   static_cast<double>(upper))*4.0);
}

int main(int argc, char** argv)
{
    int upper(5);
    cout<<upper<<"!="<<productFactorial(upper)<<endl;

    upper=1000;
    cout<<"pi="<<piProduct(upper)<<endl;

    return(0);
}
------
出力
------
5!=120
pi=3.14238

問題1.32
accumulateと、accumulateを用いたsum,productのみ示す。
------
template<typename TermResult, typename TermArgument,
         typename NextResult,typename NextArgument>
const TermResult accumulate
(const function<TermResult(TermArgument,TermArgument)>& combiner,
 const TermResult& nullValue,
 const function<TermResult(TermArgument)>& term,
 const NextArgument& lower,
 const function<NextResult(NextArgument)>& next,
 const NextArgument& upper)
{
    if(lower>upper){return(nullValue);}
    return(combiner(term(lower),
                    accumulate(combiner,nullValue,term,
                               next(lower),next,upper)));
}

template<typename TermResult, typename TermArgument,
         typename NextResult,typename NextArgument>
const TermResult sum
(const function<TermResult(TermArgument)>& term,
 const NextArgument& lower,
 const function<NextResult(NextArgument)>& next,
 const NextArgument& upper)
{
    const TermResult zero(0);
    const function<TermResult(TermArgument,TermArgument)>
        add=std::plus<TermResult>();
    return(accumulate(add,zero,term,lower,next,upper));
}

template<typename TermResult, typename TermArgument,
         typename NextResult,typename NextArgument>
const TermResult product
(const function<TermResult(TermArgument)>& term,
 const NextArgument& lower,
 const function<NextResult(NextArgument)>& next,
 const NextArgument& upper)
{
    const TermResult one(1);
    const function<TermResult(TermArgument,TermArgument)>
         mul=std::multiplies<TermResult>();
    return(accumulate(mul,one,term,lower,next,upper));
}
------

問題1.33
filter付きaccumulateのみ示す。
a,bは特に実装してないけど・・・。
------
template<typename TermResult, typename TermArgument,
         typename NextResult,typename NextArgument>
const TermResult filteredAccumulate
(const function<TermResult(TermArgument,TermArgument)>& combiner,
 const TermResult& nullValue,
 const function<bool(TermArgument)>& predicate,
 const function<TermResult(TermArgument)>& term,
 const NextArgument& lower,
 const function<NextResult(NextArgument)>& next,
 const NextArgument& upper)
{
    if(lower>upper){return(nullValue);}
    if(predicate(lower)){
        return(combiner(term(lower),
                        filteredAccumulate(combiner,nullValue,predicate,
                                   term,next(lower),next,upper)));
    }
    return(filteredAccumulate(combiner,nullValue,predicate,
                      term,next(lower),next,upper));
}

0 件のコメント :

コメントを投稿