BALL 1.5.0
function.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_MATHS_FUNCTION_H
6#define BALL_MATHS_FUNCTION_H
7
8#ifndef BALL_COMMON_H
9# include <BALL/common.h>
10#endif
11
12namespace BALL
13{
14
19 template <int constant_template>
21 {
22 public:
23
25
26
29
30
35 float operator () (float /* x */) const
36 {
37 return constant_template;
38 }
40
41 };
42
43
48 template <typename DataType = float>
50 {
51 public:
52
54
55
58
59
62
66
69 MutableConstant(DataType constant);
70
73 virtual ~MutableConstant();
74
76
79
82 MutableConstant<DataType>& operator = (const MutableConstant<DataType>& constant);
83
85
88
91 bool operator == (const MutableConstant<DataType>& constant) const;
92
94
97
103 DataType operator () (const DataType& /* x */) const
104 {
105 return constant_;
106 }
107
109
112
115 void setConstant(DataType constant)
116 {
117 constant_ = constant;
118 }
119
123 const DataType& getConstant() const
124 {
125 return constant_;
126 }
127
129
130 protected:
131
132 /*_ the constant
133 */
134 DataType constant_;
135
136 };
137
138
142 template <typename First, typename Second, typename DataType = float>
144 {
145 public:
146
148
149
152
153
155 Addition();
156
160
163 virtual ~Addition();
164
166
169
173
175
178
181 bool operator == (const Addition<First, Second, DataType>& addition) const;
182
184
187
193 DataType operator () (const DataType& x) const
194 {
195 return (first_(x) + second_(x));
196 }
197
199
202
205 void setFirst(const First& first)
206 {
207 first_ = first;
208 }
209
213 First& getFirst()
214 {
215 return first_;
216 }
217
220 const First& getFirst() const
221 {
222 return first_;
223 }
224
227 void setSecond(const Second& second)
228 {
229 second_ = second;
230 }
231
235 Second& getSecond()
236 {
237 return second_;
238 }
239
242 const Second& getSecond() const
243 {
244 return second_;
245 }
246
248
249 protected:
250
251 /*_ the first argument of the addition
252 */
253 First first_;
254
255 /*_ the second argument of the addition
256 */
257 Second second_;
258
259 };
260
261
264 template <typename First, typename Second, typename DataType = float>
266 {
267 public:
268
270
271
274
275
277 Subtraction();
278
281 Subtraction(const Subtraction& subtraction);
282
285 virtual ~Subtraction();
286
288
291
295
297
300
303 bool operator == (const Subtraction<First, Second, DataType>& subtraction) const;
304
306
309
315 DataType operator () (const DataType& x) const
316 {
317 return (first_(x) - second_(x));
318 }
319
321
324
327 void setFirst(const First& first)
328 {
329 first_ = first;
330 }
331
335 First& getFirst()
336 {
337 return first_;
338 }
339
342 void setSecond(const Second& second)
343 {
344 second_ = second;
345 }
346
350 Second& getSecond()
351 {
352 return second_;
353 }
354
356
357 protected:
358
359 /*_ the first argument of the subtraction
360 */
361 First first_;
362
363 /*_ the second argument of the subtraction
364 */
365 Second second_;
366 };
367
368
371 template <typename First, typename Second, typename DataType = float>
373 {
374 public:
375
377
378
381
382
384 Product();
385
388 Product(const Product& product);
389
392 virtual ~Product();
393
395
398
402
404
407
410 bool operator == (const Product<First, Second, DataType>& product) const;
411
413
416
422 DataType operator () (const DataType& x) const
423 {
424 return (first_(x) * second_(x));
425 }
426
428
431
434 void setFirst(const First& first)
435 {
436 first_ = first;
437 }
438
442 First& getFirst()
443 {
444 return first_;
445 }
446
450 const First& getFirst() const
451 {
452 return first_;
453 }
454
458 void setSecond(const Second& second)
459 {
460 second_ = second;
461 }
462
466 Second& getSecond()
467 {
468 return second_;
469 }
470
472
473 protected:
474
475 /*_ the first argument of the product
476 */
477 First first_;
478
479 /*_ the second argument of the product
480 */
481 Second second_;
482
483 };
484
485
488 template <typename First, typename Second, typename DataType = float>
490 {
491 public:
492
494
495
498
499
501 Division();
502
505 Division(const Division& division);
506
509 virtual ~Division();
510
512
515
519
521
524
527 bool operator == (const Division<First, Second, DataType>& division) const;
528
530
533
540 DataType operator () (const DataType& x) const
541 {
542 DataType val = second_(x);
543 if (val != 0.0)
544 {
545 return (first_(x) / val);
546 }
547 else
548 {
549 throw Exception::DivisionByZero(__FILE__, __LINE__);
550 }
551 }
552
554
557
560 void setFirst(const First& first)
561 {
562 first_ = first;
563 }
564
568 First& getFirst()
569 {
570 return first_;
571 }
572
575 void setSecond(const Second& second)
576 {
577 second_ = second;
578 }
579
583 Second& getSecond()
584 {
585 return second_;
586 }
587
589
590 protected:
591
592 /*_ the first argument of the division
593 */
594 First first_;
595
596 /*_ the second argument of the division
597 */
598 Second second_;
599
600 };
601
602
605 template <typename Function, typename DataType = float>
607 {
608 public:
609
611
612
615
616
618 Reciprocal();
619
622 Reciprocal(const Reciprocal& reciprocal);
623
626 virtual ~Reciprocal();
627
629
632
636
638
641
644 bool operator == (const Reciprocal<Function, DataType>& reciprocal) const;
645
647
650
657 DataType operator () (const DataType& x) const
658 {
659 DataType val = function_(x);
660 if (val != 0)
661 {
662 return (1 / val);
663 }
664 else
665 {
666 throw Exception::DivisionByZero(__FILE__, __LINE__);
667 }
668 }
669
671
674
677 void setFunction(const Function& function)
678 {
679 function_ = function;
680 }
681
685 const Function& getFunction() const
686 {
687 return function_;
688 }
689
691
692 protected:
693
694 /*_ the argument of the reciprocal
695 */
697
698 };
699
700
703 template <typename Function, typename DataType = float>
705 {
706 public:
707
709
710
713
714
717
720 SquareFunction(const SquareFunction& square);
721
724 virtual ~SquareFunction();
725
727
730
734
736
739
742 bool operator == (const SquareFunction<Function, DataType>& square) const;
743
745
748
754 DataType operator () (const DataType& x) const
755 {
756 DataType val = function_(x);
757 return val * val;
758 }
759
761
764
767 void setFunction(const Function& function)
768 {
769 function_ = function;
770 }
771
775 const Function& getFunction() const
776 {
777 return function_;
778 }
779
781
782 protected:
783
784 /*_ the argument of the square
785 */
787
788 };
789
790
793 template <typename Function, typename DataType = float>
795 {
796 public:
797
799
800
803
804
807
810 CubicFunction(const CubicFunction& cubic);
811
814 virtual ~CubicFunction();
815
817
820
824
826
829
832 bool operator == (const CubicFunction<Function, DataType>& cubic) const;
833
835
838
844 DataType operator () (const DataType& x) const
845 {
846 DataType val = function_(x);
847 return val * val * val;
848 }
849
851
854
857 void setFunction(const Function& function)
858 {
859 function_ = function;
860 }
861
865 const Function& getFunction() const
866 {
867 return function_;
868 }
869
871
872 protected:
873
874 /*_ the argument of the cubic
875 */
877
878 };
879
880
883 template <typename Function, typename DataType = float>
885 {
886 public:
887
889
890
893
894
896 MutablePower();
897
900 MutablePower(const MutablePower& power);
901
904 virtual ~MutablePower();
905
907
910
914
916
919
922 bool operator == (const MutablePower<Function, DataType>& power) const;
923
925
928
934 DataType operator () (const DataType& x) const
935 {
936 return pow(function_(x), exponent_);
937 }
938
940
943
946 void setFunction(const Function& function)
947 {
948 function_ = function;
949 }
950
954 const Function& getFunction() const
955 {
956 return function_;
957 }
958
962 void setExponent(DataType exp);
963
967 DataType getExponent() const;
968
970
971 protected:
972
973 /*_ the argument of the power
974 */
976
977 /*_ the exponent
978 */
979 DataType exponent_;
980 };
981
982
983 template <typename DataType>
986 : constant_(0)
987 {
988 }
989
990 template <typename DataType>
993 (const MutableConstant<DataType>& constant)
994 : constant_(constant.constant_)
995 {
996 }
997
998 template <typename DataType>
1001 : constant_(constant)
1002 {
1003 }
1004
1005 template <typename DataType>
1008 {
1009 }
1010
1011 template <typename DataType>
1014 (const MutableConstant<DataType>& constant)
1015 {
1016 constant_ = constant.constant_;
1017 return *this;
1018 }
1019
1020 template <typename DataType>
1023 (const MutableConstant<DataType>& constant) const
1024 {
1025 return (constant_ == constant.constant_);
1026 }
1027
1028
1029 template <typename First, typename Second, typename DataType>
1032 : first_(),
1033 second_()
1034 {
1035 }
1036
1037 template <typename First, typename Second, typename DataType>
1040 : first_(addition.first_),
1041 second_(addition.second_)
1042 {
1043 }
1044
1045 template <typename First, typename Second, typename DataType>
1048 {
1049 }
1050
1051 template <typename First, typename Second, typename DataType>
1054 {
1055 first_ = addition.first_;
1056 second_ = addition.second_;
1057 return *this;
1058 }
1059
1060 template <typename First, typename Second, typename DataType>
1063 {
1064 return ((first_ == addition.first_) && (second_ == addition.second_));
1065 }
1066
1067
1068 template <typename First, typename Second, typename DataType>
1071 : first_(),
1072 second_()
1073 {
1074 }
1075
1076 template <typename First, typename Second, typename DataType>
1079 : first_(subtraction.first_),
1080 second_(subtraction.second_)
1081 {
1082 }
1083
1084 template <typename First, typename Second, typename DataType>
1087 {
1088 }
1089
1090 template <typename First, typename Second, typename DataType>
1093 {
1094 first_ = subtraction.first_;
1095 second_ = subtraction.second_;
1096 return *this;
1097 }
1098
1099 template <typename First, typename Second, typename DataType>
1102 {
1103 return ((first_ == subtraction.first_) && (second_ == subtraction.second_));
1104 }
1105
1106
1107 template <typename First, typename Second, typename DataType>
1110 : first_(),
1111 second_()
1112 {
1113 }
1114
1115 template <typename First, typename Second, typename DataType>
1118 : first_(product.first_),
1119 second_(product.second_)
1120 {
1121 }
1122
1123 template <typename First, typename Second, typename DataType>
1126 {
1127 }
1128
1129 template <typename First, typename Second, typename DataType>
1132 {
1133 first_ = product.first_;
1134 second_ = product.second_;
1135 return *this;
1136 }
1137
1138 template <typename First, typename Second, typename DataType>
1141 {
1142 return ((first_ == product.first_) && (second_ == product.second_));
1143 }
1144
1145
1146 template <typename First, typename Second, typename DataType>
1149 : first_(),
1150 second_()
1151 {
1152 }
1153
1154 template <typename First, typename Second, typename DataType>
1157 : first_(division.first_),
1158 second_(division.second_)
1159 {
1160 }
1161
1162 template <typename First, typename Second, typename DataType>
1165 {
1166 }
1167
1168 template <typename First, typename Second, typename DataType>
1171 {
1172 first_ = division.first_;
1173 second_ = division.second_;
1174 return *this;
1175 }
1176
1177 template <typename First, typename Second, typename DataType>
1180 {
1181 return ((first_ == division.first_) && (second_ == division.second_));
1182 }
1183
1184
1185 template <typename Function, typename DataType>
1188 : function_()
1189 {
1190 }
1191
1192 template <typename Function, typename DataType>
1195 : function_(square.function_)
1196 {
1197 }
1198
1199 template <typename Function, typename DataType>
1202 {
1203 }
1204
1205 template <typename Function, typename DataType>
1208 {
1209 function_ = square.function_;
1210 return *this;
1211 }
1212
1213 template <typename Function, typename DataType>
1216 {
1217 return (function_ == square.function_);
1218 }
1219
1220
1221 template <typename Function, typename DataType>
1224 : function_(0)
1225 {
1226 }
1227
1228 template <typename Function, typename DataType>
1231 : function_(cubic.function_)
1232 {
1233 }
1234
1235 template <typename Function, typename DataType>
1238 {
1239 }
1240
1241 template <typename Function, typename DataType>
1244 {
1245 function_ = cubic.function_;
1246 return *this;
1247 }
1248
1249 template <typename Function, typename DataType>
1252 {
1253 return (function_ == cubic.function_);
1254 }
1255
1256
1257 template <typename Function, typename DataType>
1260 : function_()
1261 {
1262 }
1263
1264 template <typename Function, typename DataType>
1267 : function_(reciprocal.function_)
1268 {
1269 }
1270
1271 template <typename Function, typename DataType>
1274 {
1275 }
1276
1277 template <typename Function, typename DataType>
1280 {
1281 function_ = reciprocal.function_;
1282 return *this;
1283 }
1284
1285 template <typename Function, typename DataType>
1288 {
1289 return (function_ == reciprocal.function_);
1290 }
1291
1292
1293 template <typename Function, typename DataType>
1296 : function_(0),
1297 exponent_(0)
1298 {
1299 }
1300
1301 template <typename Function, typename DataType>
1304 : function_(power.function_),
1305 exponent_(power.exponent_)
1306 {
1307 }
1308
1309 template <typename Function, typename DataType>
1312 {
1313 }
1314
1315 template <typename Function, typename DataType>
1318 {
1319 function_ = power.function_;
1320 exponent_ = power.exponent_;
1321 return *this;
1322 }
1323
1324 template <typename Function, typename DataType>
1327 {
1328 return ((exponent_ == power.exponent_)
1329 && (function_ == power.function_));
1330 }
1331
1332 template <typename Function, typename DataType>
1335 {
1336 return exponent_;
1337 }
1338
1339 template <typename Function, typename DataType>
1342 {
1343 exponent_ = exp;
1344 }
1345} // namespace BALL
1346
1347#endif // BALL_MATHS_FUNCTION_H
#define BALL_INLINE
Definition: debug.h:15
#define BALL_CREATE(name)
Definition: create.h:62
Definition: constants.h:13
BALL_EXPORT bool operator==(const String &s1, const String &s2)
const DataType & getConstant() const
Definition: function.h:123
void setConstant(DataType constant)
Definition: function.h:115
virtual ~MutableConstant()
Definition: function.h:1007
First first_
Definition: function.h:253
void setFirst(const First &first)
Definition: function.h:205
const Second & getSecond() const
Definition: function.h:242
Second second_
Definition: function.h:257
void setSecond(const Second &second)
Definition: function.h:227
First & getFirst()
Definition: function.h:213
const First & getFirst() const
Definition: function.h:220
Addition< First, Second, DataType > & operator=(const Addition< First, Second, DataType > &addition)
Definition: function.h:1053
Second & getSecond()
Definition: function.h:235
bool operator==(const Addition< First, Second, DataType > &addition) const
Definition: function.h:1062
virtual ~Addition()
Definition: function.h:1047
bool operator==(const Subtraction< First, Second, DataType > &subtraction) const
Definition: function.h:1101
Subtraction< First, Second, DataType > & operator=(const Subtraction< First, Second, DataType > &subtraction)
Definition: function.h:1092
virtual ~Subtraction()
Definition: function.h:1086
First & getFirst()
Definition: function.h:335
void setSecond(const Second &second)
Definition: function.h:342
void setFirst(const First &first)
Definition: function.h:327
Second & getSecond()
Definition: function.h:350
First first_
Definition: function.h:477
void setSecond(const Second &second)
Definition: function.h:458
Second second_
Definition: function.h:481
virtual ~Product()
Definition: function.h:1125
Second & getSecond()
Definition: function.h:466
bool operator==(const Product< First, Second, DataType > &product) const
Definition: function.h:1140
const First & getFirst() const
Definition: function.h:450
Product< First, Second, DataType > & operator=(const Product< First, Second, DataType > &product)
Definition: function.h:1131
void setFirst(const First &first)
Definition: function.h:434
First & getFirst()
Definition: function.h:442
void setSecond(const Second &second)
Definition: function.h:575
Division< First, Second, DataType > & operator=(const Division< First, Second, DataType > &division)
Definition: function.h:1170
Second second_
Definition: function.h:598
void setFirst(const First &first)
Definition: function.h:560
First first_
Definition: function.h:594
bool operator==(const Division< First, Second, DataType > &division) const
Definition: function.h:1179
Second & getSecond()
Definition: function.h:583
First & getFirst()
Definition: function.h:568
virtual ~Division()
Definition: function.h:1164
Reciprocal< Function, DataType > & operator=(const Reciprocal< Function, DataType > &reciprocal)
Definition: function.h:1279
Function function_
Definition: function.h:696
void setFunction(const Function &function)
Definition: function.h:677
bool operator==(const Reciprocal< Function, DataType > &reciprocal) const
Definition: function.h:1287
virtual ~Reciprocal()
Definition: function.h:1273
const Function & getFunction() const
Definition: function.h:685
void setFunction(const Function &function)
Definition: function.h:767
const Function & getFunction() const
Definition: function.h:775
virtual ~SquareFunction()
Definition: function.h:1201
Function function_
Definition: function.h:786
SquareFunction< Function, DataType > & operator=(const SquareFunction< Function, DataType > &square)
Definition: function.h:1207
bool operator==(const SquareFunction< Function, DataType > &square) const
Definition: function.h:1215
void setFunction(const Function &function)
Definition: function.h:857
Function function_
Definition: function.h:876
const Function & getFunction() const
Definition: function.h:865
bool operator==(const CubicFunction< Function, DataType > &cubic) const
Definition: function.h:1251
virtual ~CubicFunction()
Definition: function.h:1237
CubicFunction< Function, DataType > & operator=(const CubicFunction< Function, DataType > &cubic)
Definition: function.h:1243
MutablePower< Function, DataType > & operator=(const MutablePower< Function, DataType > &power)
Definition: function.h:1317
bool operator==(const MutablePower< Function, DataType > &power) const
Definition: function.h:1326
const Function & getFunction() const
Definition: function.h:954
DataType getExponent() const
Definition: function.h:1334
Function function_
Definition: function.h:975
void setExponent(DataType exp)
Definition: function.h:1341
virtual ~MutablePower()
Definition: function.h:1311
DataType exponent_
Definition: function.h:979
void setFunction(const Function &function)
Definition: function.h:946