SIMLIB/C++  3.07
fun.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file fun.cc Function blocks
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // standard continuous function blocks
11 //
12 
13 
14 ////////////////////////////////////////////////////////////////////////////
15 // interface
16 //
17 
18 #include "simlib.h"
19 #include "internal.h"
20 
21 #include <cmath> // functions
22 
23 ////////////////////////////////////////////////////////////////////////////
24 // implementation
25 //
26 
27 namespace simlib3 {
28 
30 
31 ////////////////////////////////////////////////////////////////////////////
32 /// constructor --- functions with single argument
33 //
34 Function1::Function1(Input i, double (*pf)(double))
35  : aContiBlock1(i), f(pf) {
36  Dprintf(("Function1::Function1(in)"));
37 }
38 
39 double Function1::Value() {
40  AlgLoopDetector _(this);
41  double ret = f(InputValue());
42  return ret;
43 }
44 
45 #if 0
46 const char *Function1::Name() const {
47  if(HasName()) return _name;
48  else return SIMLIB_create_tmp_name("Function1{%p}", this);
49 }
50 #endif
51 
52 ////////////////////////////////////////////////////////////////////////////
53 /// constructor --- functions with two arguments
54 //
55 Function2::Function2(Input i1, Input i2, double (*pf)(double,double))
56  : aContiBlock2(i1,i2), f(pf) {
57  Dprintf(("Function2::Function2(in,in)"));
58 }
59 
60 double Function2::Value() {
61  AlgLoopDetector _(this);
62  double ret = f(Input1Value(), Input2Value());
63  return ret;
64 }
65 
66 #if 0
67 const char *Function2::Name() const {
68  if(HasName()) return _name;
69  else return SIMLIB_create_tmp_name("Function2{%p}", this);
70 }
71 #endif
72 
73 
74 ////////////////////////////////////////////////////////////////////////////
75 // SIMLIB local definitions:
76 //
77 
78 static double sign(double x)
79 {
80  if (x == 0.0)
81  return 0.0;
82  if (x > 0.0)
83  return 1.0;
84  else
85  return -1.0;
86 }
87 
88 
89 ////////////////////////////////////////////////////////////////////////////
90 // Implementation of block functions (prototypes are in simlib.h)
91 // using Fun(block expression) creates dynamic object/block, connects it
92 // to block expression, and returns reference to this block
93 
94 Input Abs(Input x) { return new Function1(x, fabs); }
95 
96 Input Sin(Input x) { return new Function1(x, sin); }
97 Input Cos(Input x) { return new Function1(x, cos); }
98 Input Tan(Input x) { return new Function1(x, tan); }
99 
100 Input ASin(Input x) { return new Function1(x, asin); }
101 Input ACos(Input x) { return new Function1(x, acos); }
102 Input ATan(Input x) { return new Function1(x, atan); }
103 Input ATan2(Input y, Input x) { return new Function2(y, x, atan2); }
104 
105 Input Exp(Input x) { return new Function1(x, exp); }
106 Input Log10(Input x) { return new Function1(x, log10); }
107 Input Ln(Input x) { return new Function1(x, log); }
108 Input Pow(Input x, Input y) { return new Function2(x, y, pow); }
109 
110 Input Sign(Input x) { return new Function1(x, sign); }
111 
112 Input Sqrt(Input x) { return new Function1(x, sqrt); }
113 
114 Input Min(Input x, Input y) { return new Function2(x, y, min); }
115 Input Max(Input x, Input y) { return new Function2(x, y, max); }
116 
117 
118 } // namespace
119 
base for continuous blocks with two inputs
Definition: simlib.h:959
Input Tan(Input x)
Definition: fun.cc:98
Input Min(Input x, Input y)
Definition: fun.cc:114
Input Sin(Input x)
Definition: fun.cc:96
Input Max(Input x, Input y)
Definition: fun.cc:115
continuous block connection (transparent reference) wrapper for pointer to objects of aContiBlock der...
Definition: simlib.h:895
Input Sign(Input x)
Definition: fun.cc:110
Input ATan(Input x)
Definition: fun.cc:102
bool HasName() const
Definition: simlib.h:321
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: fun.cc:60
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
double max(double a, double b)
Definition: internal.h:286
class for algebraic loop detection AlgLoopDetector object should be used in Value() method only it ch...
Definition: internal.h:307
Input ASin(Input x)
Definition: fun.cc:100
std::string SIMLIB_create_tmp_name(const char *fmt,...)
printf-like function to create temporary name (the length of temporary names is limited) used only ...
Definition: name.cc:80
Function1(const Function1 &)=delete
double(* f)(double)
Definition: simlib.h:1454
double min(double a, double b)
Definition: internal.h:285
double Input1Value()
Definition: simlib.h:964
double Input2Value()
Definition: simlib.h:965
Input Abs(Input x)
Definition: fun.cc:94
Input Log10(Input x)
Definition: fun.cc:106
Input Cos(Input x)
Definition: fun.cc:97
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
Input ATan2(Input y, Input x)
Definition: fun.cc:103
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Input Pow(Input x, Input y)
Definition: fun.cc:108
double InputValue()
Definition: simlib.h:944
base for continuous blocks with single input and algebraic loop check
Definition: simlib.h:940
double(* f)(double, double)
Definition: simlib.h:1468
#define Dprintf(f)
Definition: internal.h:100
static double sign(double x)
Definition: fun.cc:78
Input Ln(Input x)
Definition: fun.cc:107
Function2(const Function2 &)=delete
Input Sqrt(Input x)
Definition: fun.cc:112
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: fun.cc:39
block parametrized by function with single argument
Definition: simlib.h:1451
Input Exp(Input x)
Definition: fun.cc:105
Input ACos(Input x)
Definition: fun.cc:101