11 #include "ThirdParty/parallelstl/include/pstl/algorithm" 12 #include "ThirdParty/parallelstl/include/pstl/execution" 15 namespace FunctionTree {
19 throw BadParameter(
"Inverse::execute() | Parameter type mismatch!");
23 "Inverse::execute() | Expecting a single parameter or value in list");
29 out = std::make_shared<Value<double>>();
31 auto &result = par->operator()();
39 "Inverse::execute() | Expecting a single parameter or value in list");
43 LOG(ERROR) <<
"Inverse::execute() | Division by zero";
49 throw BadParameter(
"Inverse::execute() | Parameter of type " +
50 std::to_string(
checkType) +
" can not be handled");
56 std::shared_ptr<Parameter> &out) {
58 throw BadParameter(
"Inverse::SquareRoot() | Parameter type mismatch!");
62 "SquareRoot::execute() | Expecting a single parameter in list");
68 out = std::make_shared<Value<double>>();
70 auto &result = par->operator()();
77 throw BadParameter(
"SquareRoot::execute() | Expecting a single parameter " 80 result = std::sqrt(var);
84 throw BadParameter(
"SquareRoot::execute() | Parameter of type " +
85 std::to_string(
checkType) +
" can not be handled");
101 double t = accumulation.
sum + y;
109 throw BadParameter(
"AddAll::SquareRoot() | Parameter type mismatch!");
124 "AddAll::execute() | Expecting at least one multi value.");
132 auto &results = par->
values();
133 if (results.size() != n) {
137 double initial_real(0.0);
139 initial_real += x->value();
140 std::complex<double> initial_value(initial_real, 0.0);
142 initial_value += x->value();
143 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
147 if (dv->values().size() != n)
149 "AddAll::execute() | MCOMPLEX: Size of multi complex " 150 "value does not match!");
151 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
152 dv->values().begin(), results.begin(),
153 std::plus<std::complex<double>>());
156 if (dv->values().size() != n)
157 throw BadParameter(
"AddAll::execute() | MCOMPLEX: Size of multi double " 158 "value does not match!");
159 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
160 dv->values().begin(), results.begin(),
161 std::plus<std::complex<double>>());
164 if (dv->values().size() != n)
165 throw BadParameter(
"AddAll::execute() | MCOMPLEX: Size of multi int " 166 "value does not match!");
167 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
168 dv->values().begin(), results.begin(),
169 std::plus<std::complex<double>>());
176 throw BadParameter(
"AddAll::execute() | Return type is double but " 177 "complex value was found!");
184 "AddAll::execute() | Expecting at least one multi value.");
189 auto &results = par->
values();
190 if (results.size() != n) {
194 double initial_value(0.0);
196 initial_value += x->value();
197 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
201 if (dv->values().size() != results.size())
202 throw BadParameter(
"AddAll::execute() | MDOUBLE: Size of multi double " 203 "value does not match!");
204 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
205 dv->values().begin(), results.begin(),
206 std::plus<double>());
209 if (dv->values().size() != results.size())
210 throw BadParameter(
"AddAll::execute() | MDOUBLE: Size of multi double " 211 "value does not match!");
212 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
213 dv->values().begin(), results.begin(),
214 std::plus<double>());
221 throw BadParameter(
"AddAll::execute() | Return type is double but " 222 "complex value was found!");
224 throw BadParameter(
"AddAll::execute() | Return type is int but " 225 "double value was found!");
230 "AddAll::execute() | Expecting at least one multi value.");
236 auto &results = par->
values();
237 if (results.size() != n) {
240 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
245 if (dv->values().size() != results.size())
246 throw BadParameter(
"AddAll::execute() | MDOUBLE: Size of multi double " 247 "value does not match!");
248 std::transform(pstl::execution::par_unseq, results.begin(), results.end(),
249 dv->values().begin(), results.begin(), std::plus<int>());
257 throw BadParameter(
"AddAll::execute() | COMPLEX: expecting at least " 258 "one single value!");
261 out = std::make_shared<Value<std::complex<double>>>();
263 auto &result = par->
values();
264 result = std::complex<double>(0, 0);
267 result += dv->value();
269 result += dv->value();
271 result += dv->value();
273 result += dv->value();
278 std::accumulate(dv->values().begin(), dv->values().end(), result);
282 std::accumulate(dv->values().begin(), dv->values().end(), result);
285 result += std::accumulate(dv->values().begin(), dv->values().end(), 0);
293 out = std::make_shared<Value<double>>();
295 auto &result = par->
values();
299 result += dv->value();
301 result += dv->value();
303 result += dv->value();
308 auto kaResult = std::accumulate(dv->values().begin(), dv->values().end(),
310 result += kaResult.
sum;
314 auto kaResult = std::accumulate(dv->values().begin(), dv->values().end(),
316 result += kaResult.
sum;
324 out = std::make_shared<Value<int>>();
325 auto par = std::static_pointer_cast<
Value<int>>(out);
326 auto &result = par->
values();
329 result += dv->value();
334 auto kaResult = std::accumulate(dv->values().begin(), dv->values().end(),
336 result += kaResult.
sum;
341 throw BadParameter(
"AddAll::execute() | Parameter of type " +
342 std::to_string(
checkType) +
" can not be handled");
349 throw BadParameter(
"MultAll::execute() | Parameter type mismatch!");
363 if (!(nMC > 0 || (nMD > 0 && nC > 0)))
365 "MultAll::execute() | MCOMPLEX: expecting at least " 366 "one multi complex value or a multi double and a complex scalar!");
368 std::complex<double> result(1., 0.);
370 result *= p->value();
372 result *= p->value();
374 result *= p->value();
376 result *= p->value();
381 if (n == 0 && nMC == 0) {
390 auto &results = par->
values();
391 if (results.size() != n) {
394 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
398 std::transform(pstl::execution::par_unseq, p->values().begin(),
399 p->values().end(), results.begin(), results.begin(),
400 std::multiplies<std::complex<double>>());
403 std::transform(pstl::execution::par_unseq, p->values().begin(),
404 p->values().end(), results.begin(), results.begin(),
405 std::multiplies<std::complex<double>>());
408 std::transform(pstl::execution::par_unseq, p->values().begin(),
409 p->values().end(), results.begin(), results.begin(),
410 std::multiplies<std::complex<double>>());
419 "MultAll::execute() | MDOUBLE: Number and/or types do not match");
423 result *= p->value();
425 result *= p->value();
427 result *= p->value();
434 auto &results = par->
values();
435 if (results.size() != n) {
438 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
442 std::transform(pstl::execution::par_unseq, p->values().begin(),
443 p->values().end(), results.begin(), results.begin(),
444 std::multiplies<double>());
447 std::transform(pstl::execution::par_unseq, p->values().begin(),
448 p->values().end(), results.begin(), results.begin(),
449 std::multiplies<double>());
457 if (!nMI || nMC || nMD)
459 "MultAll::execute() | MDOUBLE: Number and/or types do not match");
463 result *= p->value();
465 size_t n = paras.
mIntValue(0)->values().size();
471 auto &results = par->
values();
472 if (results.size() != n) {
475 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
479 std::transform(pstl::execution::par_unseq, p->values().begin(),
480 p->values().end(), results.begin(), results.begin(),
481 std::multiplies<int>());
489 if (!nC || nMD || nMC || nMI)
490 throw BadParameter(
"MultAll::execute() | COMPLEX: expecting at least " 491 "one multi complex value!");
493 out = std::make_shared<Value<std::complex<double>>>();
495 auto &result = par->
values();
496 result = std::complex<double>(1., 0.);
499 result *= p->value();
501 result *= p->value();
503 result *= p->value();
505 result *= p->value();
510 if (!nD || nC || nMD || nMC || nMI)
511 throw BadParameter(
"MultAll::execute() | DOUBLE: expecting at least " 512 "one multi complex value!");
514 out = std::make_shared<Value<double>>();
516 auto &result = par->
values();
520 result *= p->value();
522 result *= p->value();
524 result *= p->value();
528 if (!nI || nD || nC || nMD || nMC || nMI)
529 throw BadParameter(
"MultAll::execute() | INTEGER: expecting at least " 530 "one multi complex value!");
532 out = std::make_shared<Value<int>>();
533 auto par = std::static_pointer_cast<
Value<int>>(out);
534 auto &result = par->
values();
538 result *= p->value();
542 throw BadParameter(
"MultAll::execute() | Parameter of type " +
543 std::to_string(
checkType) +
" can not be handled");
550 throw BadParameter(
"LogOf::execute() | Parameter type mismatch!");
553 throw BadParameter(
"LogOf::execute() | Expecting only one parameter");
567 "LogOf::execute() | MDOUBLE: Number and/or types do not match");
574 auto &results = par->
values();
575 if (results.size() != n) {
578 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
580 std::transform(pstl::execution::par_unseq,
582 paras.
mDoubleValue(0)->operator()().end(), results.begin(),
583 [](
double x) {
return std::log(x); });
586 size_t n = paras.
mIntValue(0)->values().size();
590 auto &results = par->
values();
591 if (results.size() != n) {
594 std::transform(pstl::execution::par_unseq,
595 paras.
mIntValue(0)->operator()().begin(),
596 paras.
mIntValue(0)->operator()().end(), results.begin(),
597 [](
double x) {
return std::log(x); });
605 "LogOf::execute() | DOUBLE: Number and/or types do not match");
608 out = std::make_shared<Value<double>>();
610 auto &result = par->
values();
618 result = std::log(paras.
intValue(0)->value());
620 throw std::runtime_error(
"LogOf::execute() | DOUBLE: something is wrong. " 621 "We should not arrive here!");
625 throw BadParameter(
"LogOf::execute() | Parameter of type " +
626 std::to_string(
checkType) +
" can not be handled");
633 throw BadParameter(
"Exp::execute() | Parameter type mismatch!");
636 throw BadParameter(
"Exp::execute() | Expecting only one parameter");
650 "Exp::execute() | MDOUBLE: Number and/or types do not match");
657 auto &results = par->
values();
658 if (results.size() != n) {
661 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
663 std::transform(pstl::execution::par_unseq,
665 paras.
mDoubleValue(0)->operator()().end(), results.begin(),
666 [](
double x) {
return std::exp(x); });
669 size_t n = paras.
mIntValue(0)->values().size();
673 auto &results = par->
values();
674 if (results.size() != n) {
677 std::transform(pstl::execution::par_unseq,
678 paras.
mIntValue(0)->operator()().begin(),
679 paras.
mIntValue(0)->operator()().end(), results.begin(),
680 [](
double x) {
return std::exp(x); });
688 "Exp::execute() | DOUBLE: Number and/or types do not match");
691 out = std::make_shared<Value<double>>();
693 auto &result = par->
values();
701 result = std::log(paras.
intValue(0)->value());
703 throw std::runtime_error(
"Exp::execute() | DOUBLE: something is wrong. " 704 "We should not arrive here!");
708 throw BadParameter(
"Exp::execute() | Parameter of type " +
709 std::to_string(
checkType) +
" can not be handled");
716 throw BadParameter(
"Pow::execute() | Parameter type mismatch!");
719 throw BadParameter(
"Pow::execute() | Expecting only one parameter");
728 int powerCopy(power);
734 "Pow::execute() | MDOUBLE: Number and/or types do not match");
741 auto &results = par->
values();
742 if (results.size() != n) {
745 std::fill(pstl::execution::par_unseq, results.begin(), results.end(),
747 std::transform(pstl::execution::par_unseq,
749 paras.
mDoubleValue(0)->operator()().end(), results.begin(),
750 [powerCopy](
double x) {
return std::pow(x, powerCopy); });
753 size_t n = paras.
mIntValue(0)->values().size();
757 auto &results = par->
values();
758 if (results.size() != n) {
761 std::transform(pstl::execution::par_unseq,
762 paras.
mIntValue(0)->operator()().begin(),
763 paras.
mIntValue(0)->operator()().end(), results.begin(),
764 [powerCopy](
double x) {
return std::pow(x, powerCopy); });
772 "Pow::execute() | DOUBLE: Number and/or types do not match");
775 out = std::make_shared<Value<double>>();
777 auto &result = par->
values();
781 result = std::pow(paras.
doubleValue(0)->value(), power);
785 result = std::log(paras.
intValue(0)->value());
787 throw std::runtime_error(
"Pow::execute() | DOUBLE: something is wrong. " 788 "We should not arrive here!");
792 throw BadParameter(
"Pow::execute() | Parameter of type " +
793 std::to_string(
checkType) +
" can not be handled");
799 std::shared_ptr<Parameter> &out) {
801 throw BadParameter(
"Complexify::SquareRoot() | Parameter type mismatch!");
813 if (nMD != 2 || nMC || nMI || nC || nD || nI)
814 throw BadParameter(
"Complexify::execute() | MCOMPLEX: Number and/or " 815 "types do not match");
821 auto &results = par->
values();
822 if (results.size() != n) {
829 pstl::execution::par_unseq, paras.
mDoubleValue(0)->operator()().begin(),
831 paras.
mDoubleValue(1)->operator()().begin(), results.begin(),
832 [](
double r,
double phi) {
return std::polar(std::abs(r), phi); });
838 if (nD != 2 || nMC || nMD || nMI || nC || nI)
839 throw BadParameter(
"Complexify::execute() | COMPLEX: Number and/or " 840 "types do not match");
842 out = std::make_shared<Value<std::complex<double>>>();
844 auto &result = par->
values();
847 result = std::polar(std::abs(paras.
doubleValue(0)->value()),
853 throw std::runtime_error(
"LogOf::execute() | DOUBLE: something is wrong. " 854 "We should not arrive here!");
859 throw BadParameter(
"Complexify::execute() | Parameter of type " +
860 std::to_string(
checkType) +
" can not be handled");
866 std::shared_ptr<Parameter> &out) {
869 "ComplexConjugate::SquareRoot() | Parameter type mismatch!");
878 if (nMD || nMI || nD || nI)
879 throw BadParameter(
"ComplexConjugate::execute() | Real numbers given. This " 880 "is mathematically correct but not necessary and " 881 "therefore not implemented.");
887 throw BadParameter(
"ComplexConjugate::execute() | MCOMPLEX: Number " 888 "and/or types do not match");
894 auto &results = par->
values();
895 if (results.size() != n) {
899 std::transform(pstl::execution::par_unseq,
902 [](std::complex<double> c) {
return std::conj(c); });
908 throw BadParameter(
"ComplexConjugate::execute() | COMPLEX: Number and/or " 909 "types do not match");
911 out = std::make_shared<Value<std::complex<double>>>();
913 auto &result = par->
values();
918 throw BadParameter(
"ComplexConjugate::execute() | Parameter of type " +
919 std::to_string(
checkType) +
" can not be handled");
926 throw BadParameter(
"AbsSquare::SquareRoot() | Parameter type mismatch!");
937 throw std::runtime_error(
"AbsSquare::execute() | Input parameter list " 938 "contains more than one parameter!");
948 auto &results = par->
values();
949 if (results.size() != n) {
952 std::transform(paras.
mDoubleValue(0)->operator()().begin(),
953 paras.
mDoubleValue(0)->operator()().end(), results.begin(),
954 [](
double c) {
return std::norm(c); });
955 }
else if (nMC == 1) {
960 auto &results = par->
values();
961 if (results.size() != n) {
964 std::transform(pstl::execution::par_unseq,
967 [](std::complex<double> c) {
return std::norm(c); });
968 }
else if (nMI == 1) {
969 size_t n = paras.
mIntValue(0)->values().size();
973 auto &results = par->
values();
974 if (results.size() != n) {
977 std::transform(pstl::execution::par_unseq,
978 paras.
mIntValue(0)->operator()().begin(),
979 paras.
mIntValue(0)->operator()().end(), results.begin(),
980 [](
int c) {
return std::norm(c); });
982 throw BadParameter(
"AbsSquare::execute() | MDOUBLE: Number and/or " 983 "types do not match");
989 throw BadParameter(
"AbsSquare::execute() | MINTEGER: Number and/or " 990 "types do not match");
991 size_t n = paras.
mIntValue(0)->values().size();
995 auto &results = par->
values();
996 if (results.size() != n) {
999 std::transform(pstl::execution::par_unseq,
1001 paras.
mDoubleValue(0)->operator()().end(), results.begin(),
1002 [](
int c) {
return std::norm(c); });
1007 throw BadParameter(
"AbsSquare::execute() | INTEGER: Number and/or " 1008 "types do not match");
1009 out = std::shared_ptr<Parameter>(
1016 out->name(), std::norm(paras.
doubleValue(0)->value())));
1022 out->name(), std::norm(paras.
complexValue(0)->value())));
1024 throw BadParameter(
"AbsSquare::execute() | DOUBLE: Number and/or " 1025 "types do not match");
1030 throw BadParameter(
"AbsSquare::execute() | Parameter of type " +
1031 std::to_string(
checkType) +
" can not be handled");
virtual std::shared_ptr< Value< int > > intValue(size_t i)
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
std::shared_ptr< Value< std::vector< double > > > MDouble(std::string name, size_t s, double el=0.)
virtual std::vector< std::shared_ptr< Value< std::vector< int > > > > & mIntValues()
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
This file contains Functions implementing the Strategy interface so they can be used inside a node of...
virtual std::size_t numValues() const
virtual std::shared_ptr< Value< std::vector< std::complex< double > > > > mComplexValue(size_t i) const
virtual std::vector< std::shared_ptr< Value< std::vector< double > > > > & mDoubleValues()
virtual std::size_t numParameters() const
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
std::shared_ptr< Value< std::vector< int > > > MInteger(std::string name, size_t s, int el=0.)
virtual std::vector< std::shared_ptr< Value< std::complex< double > > > > & complexValues()
std::shared_ptr< Value< std::vector< std::complex< double > > > > MComplex(std::string name, size_t s, std::complex< double > el=std::complex< double >(0., 0.))
virtual std::shared_ptr< FitParameter > doubleParameter(size_t i) const
virtual std::vector< std::shared_ptr< Value< std::vector< std::complex< double > > > > > & mComplexValues()
virtual std::vector< std::shared_ptr< Value< double > > > & doubleValues()
virtual std::vector< std::shared_ptr< FitParameter > > & doubleParameters()
virtual std::vector< std::shared_ptr< Value< int > > > & intValues()
virtual std::shared_ptr< Value< std::vector< int > > > mIntValue(size_t i) const
KahanSummation KahanSum(KahanSummation accumulation, double value)
KahanSummation keeps track of lost bits and reduced the uncertainty in the summation of many large/sm...
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual std::shared_ptr< Value< std::complex< double > > > complexValue(size_t i) const
virtual T & values()
Reference on the value.
This class provides a list of parameters and values of different types.
virtual std::shared_ptr< Value< std::vector< double > > > mDoubleValue(size_t i) const
virtual std::shared_ptr< Value< double > > doubleValue(size_t i) const
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Strategy execution.
virtual void execute(ParameterList ¶s, std::shared_ptr< Parameter > &out)
Add all values.