SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
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
27namespace simlib3 {
28
30
31////////////////////////////////////////////////////////////////////////////
32/// constructor --- functions with single argument
33//
34Function1::Function1(Input i, double (*pf)(double))
35 : aContiBlock1(i), f(pf) {
36 Dprintf(("Function1::Function1(in)"));
37}
38
40 AlgLoopDetector _(this);
41 double ret = f(InputValue());
42 return ret;
43}
44
45#if 0
46const 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//
55Function2::Function2(Input i1, Input i2, double (*pf)(double,double))
56 : aContiBlock2(i1,i2), f(pf) {
57 Dprintf(("Function2::Function2(in,in)"));
58}
59
61 AlgLoopDetector _(this);
62 double ret = f(Input1Value(), Input2Value());
63 return ret;
64}
65
66#if 0
67const 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
78static 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
94Input Abs(Input x) { return new Function1(x, fabs); }
95
96Input Sin(Input x) { return new Function1(x, sin); }
97Input Cos(Input x) { return new Function1(x, cos); }
98Input Tan(Input x) { return new Function1(x, tan); }
99
100Input ASin(Input x) { return new Function1(x, asin); }
101Input ACos(Input x) { return new Function1(x, acos); }
102Input ATan(Input x) { return new Function1(x, atan); }
103Input ATan2(Input y, Input x) { return new Function2(y, x, atan2); }
104
105Input Exp(Input x) { return new Function1(x, exp); }
106Input Log10(Input x) { return new Function1(x, log10); }
107Input Ln(Input x) { return new Function1(x, log); }
108Input Pow(Input x, Input y) { return new Function2(x, y, pow); }
109
110Input Sign(Input x) { return new Function1(x, sign); }
111
112Input Sqrt(Input x) { return new Function1(x, sqrt); }
113
114Input Min(Input x, Input y) { return new Function2(x, y, min); }
115Input Max(Input x, Input y) { return new Function2(x, y, max); }
116
117
118} // namespace
119
class for algebraic loop detection AlgLoopDetector object should be used in Value() method only it ch...
Definition: internal.h:307
block parametrized by function with single argument
Definition: simlib.h:1451
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: fun.cc:39
Function1(const Function1 &)=delete
double(* f)(double)
Definition: simlib.h:1454
block parametrized by function with two arguments
Definition: simlib.h:1465
double(* f)(double, double)
Definition: simlib.h:1468
Function2(const Function2 &)=delete
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: fun.cc:60
continuous block connection (transparent reference) wrapper for pointer to objects of aContiBlock der...
Definition: simlib.h:895
bool HasName() const
Definition: simlib.h:321
virtual std::string Name() const
get object name
Definition: object.cc:134
base for continuous blocks with single input and algebraic loop check
Definition: simlib.h:940
double InputValue()
Definition: simlib.h:944
base for continuous blocks with two inputs
Definition: simlib.h:959
double Input2Value()
Definition: simlib.h:965
double Input1Value()
Definition: simlib.h:964
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
Input Cos(Input x)
Definition: fun.cc:97
Input Max(Input x, Input y)
Definition: fun.cc:115
Input ATan(Input x)
Definition: fun.cc:102
Input Min(Input x, Input y)
Definition: fun.cc:114
Input Sign(Input x)
Definition: fun.cc:110
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Input Abs(Input x)
Definition: fun.cc:94
Input Ln(Input x)
Definition: fun.cc:107
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
static double sign(double x)
Definition: fun.cc:78
Input Exp(Input x)
Definition: fun.cc:105
Input ATan2(Input y, Input x)
Definition: fun.cc:103
Input Tan(Input x)
Definition: fun.cc:98
Input Pow(Input x, Input y)
Definition: fun.cc:108
double min(double a, double b)
Definition: internal.h:285
double max(double a, double b)
Definition: internal.h:286
Input Log10(Input x)
Definition: fun.cc:106
Input Sqrt(Input x)
Definition: fun.cc:112
Input Sin(Input x)
Definition: fun.cc:96
Input ASin(Input x)
Definition: fun.cc:100
Input ACos(Input x)
Definition: fun.cc:101
Main SIMLIB/C++ interface.