33#include < fstream>
44#include < iostream>
55#include < iomanip>
6+ #include < ostream>
67#include < utility>
78
89
@@ -11,7 +12,6 @@ using PLMD::SwitchingFunctionAccelerable;
1112
1213
1314struct callSQR {
14- constexpr static auto kind=" sqr_" ;
1515 template <typename T>
1616 static std::pair <double ,double > call (const T& sw, double point) {
1717 double deriv;
@@ -22,72 +22,133 @@ struct callSQR {
2222};
2323
2424struct callplain {
25- constexpr static auto kind=" " ;
2625 template <typename T>
27- static std::pair <double ,double > call (const T& sw, double point) {
26+ static std::pair <double ,double > call (const T& sw, const double point) {
2827 double deriv;
2928 double value = sw.calculate (point,deriv);
3029 return std::make_pair (value,deriv);
3130 }
3231};
3332
34- template <typename caller>
35- void test (const std::string& name, std::string definition,bool stretch=true ) {
33+ template <typename caller,typename SW>
34+ std::ostream& calculateAndSubtract (const SW& sw,
35+ const double point,
36+ const double value,
37+ const double derivative,
38+ std::ostream& out) {
39+ auto [myval,mydev] = caller::call (sw,point);
40+ out << (myval -value)<< " " << (mydev - derivative) << " " ;
41+ return out;
42+ }
43+
44+
45+ void test (const std::string& name, std::string definition,std::string custom,bool stretch=true ) {
3646 if (!stretch) {
3747 definition += " NOSTRETCH " ;
48+ custom += " NOSTRETCH " ;
3849 }
3950
40- std::ofstream os (" out_" +std::string (caller::kind)+ name+(stretch ? " " : " _nostretch" ));
51+ std::ofstream os (" out_" +name+(stretch ? " " : " _nostretch" ));
4152 os <<std::fixed<<std::setprecision (6 );
4253 SwitchingFunction sw;
4354 std::string error;
4455 sw.set (definition,error);
4556 if (!error.empty ()) {
4657 std::cerr<<error<<" \n " ;
4758 }
59+ error.clear ();
4860
61+ SwitchingFunction swc;
62+ swc.set (custom,error);
63+ if (!error.empty ()) {
64+ std::cerr<<error<<" \n " ;
65+ }
4966 error.clear ();
5067 SwitchingFunctionAccelerable swa;
5168 swa.set (definition,error);
5269 if (!error.empty ()) {
5370 std::cerr<<error<<" \n " ;
5471 }
55- os << " point :\t value deriv\t value_acc deriv_acc\t value_delta deriv_delta\n " ;
72+ os << " point : value deriv vsq_delta dsq_delta "
73+ " vAcc_delta dAcc_delta "
74+ " vAccsq_delta dAccsq_delta "
75+ " vcus_delta dcus_delta "
76+ " vsqcus_delta dsqcus_delta\n " ;
5677 for (int i=0 ; i<10 ; i++) {
5778 double point=i/2.50 ;
58- double deriv ;
79+ double derivative ;
5980 double value;
60- os<< point <<" :\t " ;
61- std::tie (value,deriv ) = caller ::call (sw, point);
62- os << value << " " << deriv <<" \t " ;
63- double deriv_acc ;
64- double value_acc ;
65- std::tie (value_acc,deriv_acc) = caller::call (swa, point) ;
66- os << value_acc << " " << deriv_acc << " \t " ;
67- os << value-value_acc << " " << deriv-deriv_acc << " \n " ;
81+ os<< point <<" : " ;
82+ std::tie (value,derivative ) = callplain ::call (sw, point);
83+ os << value << " " << derivative <<" " ;
84+ calculateAndSubtract<callSQR>(sw,point, value, derivative,os) << " " ;
85+ calculateAndSubtract<callplain>(swa,point, value, derivative,os) << " " ;
86+ calculateAndSubtract<callSQR>(swa,point, value, derivative,os) << " " ;
87+ calculateAndSubtract<callplain>(swc,point, value, derivative,os) << " " ;
88+ calculateAndSubtract<callSQR>(swc,point, value, derivative,os) << " \n " ;
6889 }
6990}
7091
7192
7293int main () {
94+ // the test will output the value of the switch along with de difference of it and
95+ // the same calculation under different settings (with squared/non squared input,
96+ // with the "accelerable version and with custom)
7397 for (const auto &x: {
74- std::pair<std::string,std::string> {" COSINUS R_0=2.6" ," cosinus" },
75- {" EXP R_0=0.8 D_0=0.5 D_MAX=2.6" ," exp" },
76- {" GAUSSIAN R_0=1.0 D_0=0.0 D_MAX=2.6" ," fastgaussian" },
77- {" GAUSSIAN R_0=1.0 D_0=0.3 D_MAX=2.6" ," gaussian" },
78- {" RATIONAL R_0=1.3 NN=6 MM=10 D_MAX=2.6" ," fastrational" },
79- {" RATIONAL R_0=1.3 D_MAX=2.6" ," fastrational_MMeq2MM" },
80- {" RATIONAL R_0=1.3 NN=5 MM=11 D_MAX=2.6" ," rational" },
81- {" RATIONAL R_0=1.3 NN=5 D_MAX=2.6" ," rational_MMeq2MM" },
82- {" Q R_0=1.0 D_0=0.3 BETA=1.0 LAMBDA=1.0 REF=1.3 D_MAX=2.6" ," q" },
83- {" SMAP R_0=1.3 A=3 B=2 D_MAX=2.6" ," smap" },
84- {" TANH R_0=1.3 D_MAX=2.6" ," tanh" }
98+ std::tuple<std::string,std::string,std::string> {
99+ " COSINUS R_0=2.6" ,
100+ " CUSTOM FUNC=0.5*(cos(x*pi)+1) R_0=2.6 D_MAX=2.6" ,
101+ " cosinus"
102+ }, {
103+ " EXP R_0=0.8 D_0=0.5 D_MAX=2.6" ,
104+ " CUSTOM FUNC=exp(-x) R_0=0.8 D_0=0.5 D_MAX=2.6" ,
105+ " exp"
106+ }, {
107+ " GAUSSIAN R_0=1.0 D_0=0.0 D_MAX=2.6" ,
108+ " CUSTOM FUNC=exp(-x^2/2) R_0=1.0 D_0=0.0 D_MAX=2.6" ,
109+ " fastgaussian"
110+ }, {
111+ " GAUSSIAN R_0=1.0 D_0=0.3 D_MAX=2.6" ,
112+ " CUSTOM FUNC=exp(-x^2/2) R_0=1.0 D_0=0.3 D_MAX=2.6" ,
113+ " gaussian"
114+ }, {
115+ " RATIONAL R_0=1.3 NN=6 MM=10 D_MAX=2.6" ,
116+ " CUSTOM FUNC=(1-x^6)/(1-x^10) D_MAX=2.6 R_0=1.3" ,
117+ " fastrational"
118+ }, {
119+ " RATIONAL R_0=1.3 D_MAX=2.6" ,
120+ " CUSTOM FUNC=(1-x^6)/(1-x^12) D_MAX=2.6 R_0=1.3" ,
121+ " fastrational_NNeq2MM"
122+ }, {
123+ " RATIONAL R_0=1.3 NN=5 MM=11 D_MAX=2.6" ,
124+ " CUSTOM FUNC=(1-x^5)/(1-x^11) D_MAX=2.6 R_0=1.3" ,
125+ " rational"
126+ }, {
127+ " RATIONAL R_0=1.3 NN=5 D_MAX=2.6" ,
128+ " CUSTOM FUNC=(1-x^5)/(1-x^10) D_MAX=2.6 R_0=1.3" ,
129+ " rational_NNeq2MM"
130+ }, {
131+ " Q R_0=1.0 D_0=0.3 BETA=5.0 LAMBDA=1.0 REF=1.3 D_MAX=2.6" ,
132+ " CUSTOM FUNC=1/(1+exp(5.0*((x+0.3)-1.3))) R_0=1.0 D_0=0.3 D_MAX=2.6" ,
133+ " q"
134+ }, {
135+ " TANH R_0=1.3 D_MAX=2.6" ,
136+ " CUSTOM FUNC=1-tanh(x) R_0=1.3 D_MAX=2.6" ,
137+ " tanh"
138+ }, {
139+ " SMAP R_0=1.3 A=3 B=2 D_MAX=2.6" ,
140+ " CUSTOM FUNC=(1+(2^(3/2)-1)*x^3)^(-2/3) R_0=1.3 D_MAX=2.6" ,
141+ " smap"
142+ }, {
143+ " CUBIC D_MAX=2.6 D_0=0.6" ,
144+ // Here R_O = DMAX-D_0
145+ " CUSTOM FUNC=(x-1)^2*(1+2*x) R_0=2.0 D_0=0.6 D_MAX=2.6" ,
146+ " cubic"
147+ }
85148 }) {
86- auto [definition, name] = x;
87- test<callplain> (name,definition);
88- test<callplain> (name,definition,false );
89- test<callSQR> (name,definition);
90- test<callSQR> (name,definition,false );
149+ auto [definition, custom, name] = x;
150+ test (name,definition,custom);
151+ test (name,definition,custom,false );
91152
92153 }
93154
0 commit comments