File: gfunc.h

package info (click to toggle)
fityk 0.4.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,472 kB
  • ctags: 2,617
  • sloc: cpp: 19,705; sh: 5,965; xml: 2,325; yacc: 356; makefile: 183; lex: 178
file content (253 lines) | stat: -rw-r--r-- 8,348 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// This file is part of fityk program. Copyright (C) Marcin Wojdyr
// $Id: gfunc.h,v 1.2 2004/04/06 08:59:54 wojdyr Exp $

#ifndef GFUNC__H__
#define GFUNC__H__

        /*   g-functions (parameters for f-functions) defined here
         */

#include <math.h>
#include <string>
#include <vector>
#include "common.h"
#include "pag.h"
#include "fzgbase.h"

inline int sign(fp x) { return x >= 0 ? (x != 0 ? 1 : 0) : -1; }

class V_g : public V_fzg
{
public:
    static const z_names_type g_names[];

    V_g (Sum* sum, char t, const std::vector<Pag> &v);
    virtual ~V_g() {}
    One_of_fzg fzg_of() const { return gType; }
    fp g_value (const std::vector<fp>& A, const std::vector<V_g*>& G); 
    std::vector<int_fp> get_derivatives (const std::vector<fp>& A, 
                                         const std::vector<V_g*>& G);
    static std::vector<char> all_types();
    static V_g* factory (Sum* sum, char type, const std::vector<Pag> &p);

protected:
    virtual void prepare_deriv () = 0;
    virtual fp g_val () = 0; 
};


/********  sign change  ***********/
class Gsign : public V_g  // -a0
{
    Gsign (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return -av[0]; }
    void prepare_deriv () { der[0] = -1.; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) 
        { return "-" + ps(0,A,G); }
};   

/********  sum  ***********/
class Gsum : public V_g  // a0 + a1
{
    Gsum (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return av[0] + av[1]; }
    void prepare_deriv () { der[0] = der[1] = 1.; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) 
        { return ps(0,A,G) + "+" + ps(1,A,G); }
};   

/********  product  ***********/
class Gproduct : public V_g  // a0 * a1
{
    Gproduct (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return av[0] * av[1]; }
    void prepare_deriv () { der[0] = av[1]; der[1] = av[0]; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) 
        { return ps(0,A,G) + "*" + ps(1,A,G); }
};   

/********  division  ***********/
class Gdivision : public V_g  // a0 / a1
{
    Gdivision (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return av[0] / av[1]; }
    void prepare_deriv () { 
        der[0] = 1. / av[1];
        der[1] = - av[0] / (av[1] * av[1]); 
    }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return ps(0,A,G) + "/" + ps(1,A,G); }
};   

/********  sqrt  ***********/
class Gsqrt : public V_g  // sqrt (a0)
{
    Gsqrt (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return sqrt (fabs(av[0])); }
    void prepare_deriv() { der[0] = sign(av[0]) * 0.5 / sqrt(fabs(av[0])); }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return "sqrt(" + ps(0,A,G) + ")"; }
};   

/********  cosinus  ***********/
class Gcosinus : public V_g  // cos (a0*a1)
{
    Gcosinus (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return cos (av[0] * av[1]); }
    void prepare_deriv() { fp t = - sin (av[0] * av[1]); 
                           der[0] = av[1] * t; der[1] = av[0] * t; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return "cos(" + ps(0,A,G) + "*" + ps(0,A,G) + ")"; }
};   

/********  tangent  ***********/
class Gtangent : public V_g  // tan (a0*a1)
{
    Gtangent (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return tan (av[0] * av[1]); }
    void prepare_deriv() { fp c = cos(av[0] * av[1]); 
                           der[0] = av[1] / (c*c); der[1] = av[0] / (c*c); }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return "tan(" + ps(0,A,G) + "*" + ps(1,A,G) + ")"; }
};   

/********  sum of products  ***********/
class Gsum_of_prod : public V_g  // a0 * a1 + a2 * a3
{
    Gsum_of_prod (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return av[0] * av[1] + av[2] * av[3]; }
    void prepare_deriv () 
            { der[0] = av[1]; der[1] = av[0]; der[2] = av[3]; der[3] = av[2]; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) 
       {return ps(0,A,G) + "*" + ps(1,A,G) + "+" + ps(2,A,G) + "*" + ps(3,A,G);}
};   

/********  polynomial  ***********/
class Gpolynomial : public V_g  // a1 + a2*a0 + a3*a0^2 + a4*a0^3 
{
    Gpolynomial (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val ();
    void prepare_deriv ();
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G);
};   

/********  rational  ***********/
class Grational : public V_g  // a1*a0^2 + a2*a0 + a3 + a4/a0 + a5/a0^2
{
    Grational (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val ();
    void prepare_deriv (); 
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G); 
};   

/********  2 * ArcSin(a0 / 2 * a1) [DEG] ***********/
class Gtwo_arcsin_a_half_a_in_deg : public V_g  
{
    Gtwo_arcsin_a_half_a_in_deg (Sum *sum, char t, const std::vector<Pag>&g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return 360/M_PI * asin (av[0] / 2 * av[1]); }
    void prepare_deriv (); 
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) 
        { return "360/pi*asin(" + ps(0,A,G) + "/2*" + ps(1,A,G) + ")"; }
};   

/************  sqrt(a0/a1^2+a2/a3^2+a4/a5^2) *************/
class Gsqrt_a0_a1a1_a2_a3a3_a4_a5a5 : public V_g  
{
    Gsqrt_a0_a1a1_a2_a3a3_a4_a5a5 (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () {
        return sqrt (av[0] / (av[1] * av[1]) + av[2] / (av[3] * av[3]) 
                     + av[4] / (av[5] * av[5]));
    }
    void prepare_deriv (); 
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G);
};   

/********  TCHZ-FWHM  ***********/
  // (a0^5 + p0 a0^4 a1 + p1 a0^3 a1^2 + p2 a0^2 a1^3 + p3 a0 a1^4 + a1^5)^0.2
  // p0, p1, p2, p3 = 2.69269, 2.42843, 4.47163, 0.07842
class Gtchz_fwhm : public V_g  
{
    Gtchz_fwhm (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val ();
    void prepare_deriv ();
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G);
private:
    static const fp p[4];
};   

/********  minimum  *********/
class Gminimum : public V_g  // a0^2 + a1
{
    Gminimum (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return av[0] * av[0] + av[1]; }
    void prepare_deriv () { der[0] = 2 * av[0]; der[1] = 1.; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return ps(0,A,G) + "**2+" + ps(1,A,G); }
};   

/********  maximum  *********/
class Gmaximum : public V_g  // -a0^2 + a1
{
    Gmaximum (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return - av[0] * av[0] + av[1]; }
    void prepare_deriv () { der[0] = -2 * av[0]; der[1] = 1.; }
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) { return "-(" + ps(0,A,G) + ")**2+" + ps(0,A,G); }
};   


/********  bounds  ***********/
class Gbounds : public V_g  // (a2-a1) * (sin(a0)+1)/2 + a1
{
    Gbounds (Sum *sum, char t, const std::vector<Pag>& g)
        : V_g(sum, t, g) {}
    friend class V_g;
public:
    fp g_val () { return (av[2] - av[1]) * (sin(av[0]) + 1) / 2. + av[1]; }
    void prepare_deriv ();
    std::string formula (const std::vector<fp>& A, const std::vector<V_g*>& G) {
        return "(" + ps(2,A,G) + "-" + ps(1,A,G) +")*(sin("+ ps(0,A,G) + ")+1)/2+" + ps(1,A,G);
    }
};   

#endif