1 // -*- mode:C++ ; compile-command: "g++ -I.. -g -c rpn.cc" -*-
2 /*
3  *  Copyright (C) 2001,2014 B. Parisse, Institut Fourier, 38402 St Martin d'Heres
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef _GIAC_RPN_H
19 #define _GIAC_RPN_H
20 #include "first.h"
21 
22 #include "gen.h"
23 #include "vecteur.h"
24 #include <string>
25 #include <ctype.h>
26 
27 #undef _ABS // for SunOS
28 
29 #ifndef NO_NAMESPACE_GIAC
30 namespace giac {
31 #endif // ndef NO_NAMESPACE_GIAC
32 
33   std::string printasconstant(const gen & feuille,const char * sommetstr,GIAC_CONTEXT);
34   extern const unary_function_ptr * const  at_INTERSECT;
35   extern const unary_function_ptr * const  at_MINUS;
36   extern const unary_function_ptr * const  at_UNION;
37   extern const unary_function_ptr * const  at_rpn;
38   gen _rpn(const gen & args,GIAC_CONTEXT);
39   extern const unary_function_ptr * const  at_alg;
40   gen _alg(const gen & args,GIAC_CONTEXT);
41   std::string enmajuscule(const std::string & s);
42   gen _PERCENT(const gen & args,GIAC_CONTEXT);
43   extern const unary_function_ptr * const  at_PERCENT;
44   void roll(int i,vecteur & v);
45   void ROLL(int i,GIAC_CONTEXT);
46   gen _ROLL(const gen & args,GIAC_CONTEXT);
47   extern const unary_function_ptr * const  at_ROLL;
48 
49   void rolld(int i,vecteur & v);
50   void ROLLD(int i,GIAC_CONTEXT);
51   gen _ROLLD(const gen & args,GIAC_CONTEXT);
52   extern const unary_function_ptr * const  at_ROLLD;
53 
54   void stack_swap(vecteur & v);
55   void SWAP(GIAC_CONTEXT);
56   gen _SWAP(const gen & args,GIAC_CONTEXT);
57   extern const unary_function_ptr * const  at_SWAP;
58 
59   void dup(vecteur & v);
60   gen _DUP(const gen & args,GIAC_CONTEXT);
61   extern const unary_function_ptr * const  at_DUP;
62 
63   void over(vecteur & v);
64   gen _OVER(const gen & args,GIAC_CONTEXT);
65   extern const unary_function_ptr * const  at_OVER;
66 
67   void pick(int i,vecteur & v);
68   gen _PICK(const gen & args,GIAC_CONTEXT);
69   extern const unary_function_ptr * const  at_PICK;
70 
71   void drop(vecteur & v);
72   gen _DROP(const gen & args,GIAC_CONTEXT);
73   extern const unary_function_ptr * const  at_DROP;
74 
75   gen _NOP(const gen & args,GIAC_CONTEXT);
76   extern const unary_function_ptr * const  at_NOP;
77 
78   gen _IFTE(const gen & args,GIAC_CONTEXT);
79   extern const unary_function_ptr * const  at_IFTE;
80 
81   gen _RPN_LOCAL(const gen & args,GIAC_CONTEXT);
82   extern const unary_function_ptr * const  at_RPN_LOCAL;
83 
84   gen _RPN_FOR(const gen & args,GIAC_CONTEXT);
85   extern const unary_function_ptr * const  at_RPN_FOR;
86 
87   gen _RPN_WHILE(const gen & args,GIAC_CONTEXT);
88   extern const unary_function_ptr * const  at_RPN_WHILE;
89 
90   gen _RPN_UNTIL(const gen & args,GIAC_CONTEXT);
91   extern const unary_function_ptr * const  at_RPN_UNTIL;
92 
93   gen _RPN_CASE(const gen & args,GIAC_CONTEXT);
94   extern const unary_function_ptr * const  at_RPN_CASE;
95 
96   gen _RCL(const gen & args,GIAC_CONTEXT);
97   extern const unary_function_ptr * const  at_RCL;
98 
99   gen _VARS(const gen & args,GIAC_CONTEXT);
100   extern const unary_function_ptr * const  at_VARS;
101 
102   gen purgenoassume(const gen & args,const context * contextptr);
103   gen _purge(const gen & args,GIAC_CONTEXT);
104   extern const unary_function_ptr * const  at_purge;
105 
106   gen _rpn_prog(const gen & args,GIAC_CONTEXT);
107   extern const unary_function_ptr * const  at_rpn_prog;
108   vecteur rpn_eval(const vecteur & prog,vecteur & pile,GIAC_CONTEXT);
109   vecteur rpn_eval(const gen & prog,vecteur & pile,GIAC_CONTEXT);
110 
111   gen _division(const gen & args,GIAC_CONTEXT);
112   extern const unary_function_ptr * const  at_division;
113 
114   gen _binary_minus(const gen & args,GIAC_CONTEXT);
115   extern const unary_function_ptr * const  at_binary_minus;
116   vecteur tab2vecteur(gen tab[]);
117 
118   extern const unary_function_ptr * const  at_SIN ;
119   extern const unary_function_ptr * const  at_COS ;
120   extern const unary_function_ptr * const  at_TAN ;
121   extern const unary_function_ptr * const  at_EXP ;
122   extern const unary_function_ptr * const  at_LN ;
123   extern const unary_function_ptr * const  at_DELTALIST ;
124   extern const unary_function_ptr * const  at_PILIST ;
125   extern const unary_function_ptr * const  at_HPSUM ;
126   extern const unary_function_ptr * const  at_SIGMALIST ;
127   extern const unary_function_ptr * const  at_HPDIFF ;
128   extern const unary_function_ptr * const  at_HPINT ;
129   extern const unary_function_ptr * const  at_Fahrenheit2Celsius;
130   extern const unary_function_ptr * const  at_Celsius2Fahrenheit;
131   extern const unary_function_ptr * const  at_polar_complex;
132   extern const unary_function_ptr * const  at_ggb_ang;
133   extern const unary_function_ptr * const  at_HDigits;
134   extern const unary_function_ptr * const  at_HFormat;
135   extern const unary_function_ptr * const  at_HComplex;
136   extern const unary_function_ptr * const  at_HAngle;
137   extern const unary_function_ptr * const  at_HLanguage;
138   // extern const unary_function_ptr * const  at_testfunc;
139   extern const unary_function_ptr * const  at_FREEZE;
140   extern const unary_function_ptr * const  at_LINE;
141   extern const unary_function_ptr * const  at_RECT;
142   extern const unary_function_ptr * const  at_TEXTOUT;
143   extern const unary_function_ptr * const  at_EDITMAT;
144   extern const unary_function_ptr * const  at_INT;
145 
146 #ifdef GIAC_HAS_STO_38
147   extern const unary_function_ptr * const  at_INVERT;
148   extern const unary_function_ptr * const  at_BLIT;
149   extern const unary_function_ptr * const  at_GETPIX;
150   extern const unary_function_ptr * const  at_DIMGROB;
151   extern const unary_function_ptr * const  at_SUBGROB;
152   extern const unary_function_ptr * const  at_GETPIX_P;
153   extern const unary_function_ptr * const  at_PIXON_P;
154   extern const unary_function_ptr * const  at_PIXOFF_P;
155   extern const unary_function_ptr * const  at_LINE_P;
156   extern const unary_function_ptr * const  at_RECT_P;
157   extern const unary_function_ptr * const  at_INVERT_P;
158   extern const unary_function_ptr * const  at_BLIT_P;
159   extern const unary_function_ptr * const  at_TEXTOUT_P;
160   extern const unary_function_ptr * const  at_DIMGROB_P;
161   extern const unary_function_ptr * const  at_ARC_P;
162   extern const unary_function_ptr * const  at_SUBGROB_P;
163   extern const unary_function_ptr * const  at_GROBH;
164   extern const unary_function_ptr * const  at_GROBW;
165   extern const unary_function_ptr * const  at_GROBH_P;
166   extern const unary_function_ptr * const  at_GROBW_P;
167   extern const unary_function_ptr * const  at_ISKEYDOWN;
168   extern const unary_function_ptr * const  at_STARTAPP;
169   extern const unary_function_ptr * const  at_STARTVIEW;
170 #endif
171 
172   gen _hp38(const gen & args,GIAC_CONTEXT);
173   gen _ABS(const gen & args,GIAC_CONTEXT);
174   gen _MODULO(const gen & args,GIAC_CONTEXT);
175   gen _RANDOM(const gen & g,GIAC_CONTEXT);
176   std::string printasRANDOM(const gen & feuille,const char * s,GIAC_CONTEXT);
177   gen _MAXREAL(const gen & g,GIAC_CONTEXT);
178   gen _MINREAL(const gen & g,GIAC_CONTEXT);
179   gen _EXPM1(const gen & g,GIAC_CONTEXT);
180   gen _LNP1(const gen & g,GIAC_CONTEXT);
181   gen _ADDROW(const gen & args,GIAC_CONTEXT);
182   gen _ADDCOL(const gen & args,GIAC_CONTEXT);
183   gen _SCALE(const gen & g,GIAC_CONTEXT);
184   gen _SCALEADD(const gen & g,GIAC_CONTEXT);
185   gen _SWAPCOL(const gen & args,GIAC_CONTEXT);
186   gen _SUB(const gen & args,GIAC_CONTEXT);
187   gen _RANDMAT(const gen & args,GIAC_CONTEXT);
188   gen _REDIM(const gen & args,GIAC_CONTEXT);
189   extern const unary_function_ptr * const  at_redim;
190   gen _REPLACE(const gen & args,GIAC_CONTEXT);
191   gen _EIGENVAL(const gen & args,GIAC_CONTEXT);
192   gen _EIGENVV(const gen & args,GIAC_CONTEXT);
193   gen _SIZE(const gen& args,GIAC_CONTEXT);
194   gen _INT(const gen & g,GIAC_CONTEXT);
195   gen _HPDIFF(const gen & args,GIAC_CONTEXT);
196   gen _HPINT(const gen & args,GIAC_CONTEXT);
197   gen _HPSUM(const gen & args,GIAC_CONTEXT);
198   gen _TAYLOR(const gen & args,GIAC_CONTEXT);
199   gen _POLYFORM(const gen & args,GIAC_CONTEXT);
200   gen _IS_LINEAR(const gen & args,GIAC_CONTEXT);
201   gen _SVD(const gen & args0,GIAC_CONTEXT);
202   gen _SVL(const gen & args0,GIAC_CONTEXT);
203   gen _SPECRAD(const gen & args0,GIAC_CONTEXT);
204   gen _SPECNORM(const gen & args0,GIAC_CONTEXT);
205   gen _COND(const gen & args0,GIAC_CONTEXT);
206   gen _rank(const gen & args,GIAC_CONTEXT);
207   gen _SCHUR(const gen & args,GIAC_CONTEXT);
208   gen _LQ(const gen & args0,GIAC_CONTEXT);
209   gen _LU(const gen & args0,GIAC_CONTEXT);
210   gen _QR(const gen & args0,GIAC_CONTEXT);
211   gen _XPON(const gen & g,GIAC_CONTEXT);
212   gen _MANT(const gen & g,GIAC_CONTEXT);
213   gen _HMSX(const gen & g0,GIAC_CONTEXT);
214   gen _XHMS(const gen & g0,GIAC_CONTEXT);
215   gen _DEGXRAD(const gen & g,GIAC_CONTEXT);
216   gen _RADXDEG(const gen & g,GIAC_CONTEXT);
217   gen _PERCENT(const gen & g,GIAC_CONTEXT);
218   gen _PERCENTCHANGE(const gen & g,GIAC_CONTEXT);
219   gen _PERCENTTOTAL(const gen & g,GIAC_CONTEXT);
220   gen _ITERATE(const gen & args,GIAC_CONTEXT);
221 //  gen _RECURSE(const gen & args,GIAC_CONTEXT);
222   gen _MAKEMAT(const gen & args,GIAC_CONTEXT);
223   gen _LSQ(const gen & args,GIAC_CONTEXT);
224   gen _idivis(const gen & args0,GIAC_CONTEXT);
225   gen _isprime(const gen & args0,GIAC_CONTEXT);
226   gen _ithprime(const gen & args0,GIAC_CONTEXT);
227   gen _euler(const gen & args0,GIAC_CONTEXT);
228   gen _numer(const gen & args0,GIAC_CONTEXT);
229   gen _denom(const gen & args0,GIAC_CONTEXT);
230   gen _ifactors(const gen & args0,GIAC_CONTEXT);
231   gen _binomial_icdf(const gen & args0,GIAC_CONTEXT);
232   gen _poisson_icdf(const gen & args0,GIAC_CONTEXT);
233 
234   gen symb_RPN_LOCAL(const gen & a,const gen & b);
235   gen symb_RPN_FOR(const gen & a,const gen & b);
236   gen symb_RPN_WHILE(const gen & a,const gen & b);
237   gen symb_RPN_CASE(const gen & a);
238   gen symb_RPN_UNTIL(const gen & a,const gen & b);
239   gen symb_IFTE(const gen & args);
240   gen symb_NOP(const gen & args);
241   gen symb_rpn_prog(const gen & args);
242   gen _NTHROOT(const gen & args,GIAC_CONTEXT);
243   extern const unary_function_ptr * const  at_NTHROOT;
244   extern gen * rpn_ans() ;
245   gen _Ans(const gen & args,GIAC_CONTEXT);
246   extern const unary_function_ptr * const  at_Ans;
247   bool is_Ans(const gen & g);
248   gen _EXPORT(const gen & args,GIAC_CONTEXT);
249   extern const unary_function_ptr * const  at_EXPORT;
250   gen _VIEWS(const gen & args,GIAC_CONTEXT);
251   extern const unary_function_ptr * const  at_VIEWS;
252   extern const unary_function_ptr * const  at_POLYFORM;
253   extern const unary_function_ptr * const  at_colSwap;
254   extern const unary_function_ptr * const  at_replace;
255   extern const unary_function_ptr * const  at_scale;
256   extern const unary_function_ptr * const  at_scaleadd;
257   extern const unary_function_ptr * const  at_schur;
258   extern const unary_function_ptr * const  at_svl;
259   extern const unary_function_ptr * const  at_swapcol;
260   extern const unary_function_ptr * const  at_swaprow;
261   extern const unary_function_ptr * const  at_mantissa;
262   extern const unary_function_ptr * const  at_ldexp;
263   extern const unary_function_ptr * const  at_frexp;
264 
265   int is_known_name_home_38(const char * idname);
266   // 1 and 2 app or program variable, 3 home variable
267   int is_known_name_home_38(const char * name_space,const char * idname);
268   // add progname qualifier to variables in v and replace in g
269   void qualify(gen & g,const vecteur & v,const gen & prog,GIAC_CONTEXT);
270   // parse_program description: result is parsed, should be evaled
271   // assignation_by_equal: warning for probable misuse of = instead of :=
272   // undeclared_global_vars: regular 38 global variables used but not declared
273   // declared_global_vars: regular 38 global variables that were declared
274   // exported_function_names: list of exported function
275   //                          may contain description strings
276   // exported_variable_names: same as above but variables
277   // unknown_exported: function or variable exported but not used
278   // unexported: function local to source, re-qualified with progname namespace
279   // unexported_declared_global_vars: non regular 38 global variables,
280   //                                  re-qualified with progname namespace
281   // views: is a matrix: column1 is the function name (no param) or integer,
282   //                     column2 is a comment string
283   // errors: exported names that are regular 38 home variables,
284   //         and undeclared global variables that are not regular 38 variables
285   // return value is 0: ok, -1: invalid VIEWS, >0: #errors
286   // int parse_program(const wchar_t * source,const wchar_t * progname,vecteur & assignation_by_equal,vecteur & undeclared_global_vars,vecteur & declared_global_vars,vecteur & exported_function_names,vecteur & exported_variable_names,vecteur & unknown_exported,vecteur & unexported,vecteur & unexported_declared_global_vars,vecteur & views,vecteur & errors,gen & parsed,GIAC_CONTEXT);
287 
288   // Prepares app sequence for computing a recurrence relation
289   // Valid if 1 sequence is checked and does not depend on other sequences
290   // Given expr_un, the expression of UK(N) in terms of UK(N-1) and UK(N-2)
291   // write the recurrence relation as UK(N)=subst(expr,vars,[N,UK(N-1),UK(N-2)])
292   // Return 0 if expr_un is invalid, 1 if it does not depend on UK(N-2)
293   // 2 otherwise
294   int seqapp_prepare(const gen & expr_un,gen & expr,vecteur & vars,GIAC_CONTEXT,int seqno=-1);
295   // Compute UK(N) for K=k to m, where UK(k) and UK(k+1) are given
296   // If the recurrence relation does not depend on UK(N-2), set UK(k+1) to undef
297   vecteur seqapp_compute(const gen & expr,const vecteur & vars,const gen & UK_k,const gen &UK_kp1,int k,int m,GIAC_CONTEXT);
298 
299   // Prepares app sequence for computing all recurrences relations
300   // expr_un should contain the expression for U0(N) to UK(N) for K<=9
301   // undef may be used if the sequence is not checked
302   // Rewrite the recurrence relation as [U0(N),...,UK(N)]=subst(expr,vars,[N,U0(N-1),U0(N-2),...])
303   // Return 0 if expr_un is invalid, -10-val if Uval should be checked
304   // Return 1 if it does not depend on UK(N-2), 2 otherwise
305   int seqapp_prepare(const vecteur & expr_un,vecteur & expr,vecteur & vars,GIAC_CONTEXT);
306   // Compute UK(N) for K=k to m, where UK(k) and UK(k+1) are given
307   // If the recurrence relation does not depend on UK(N-2), set UK_kp1 to vecteur(0)
308   vecteur seqapp_compute(const vecteur & expr,const vecteur & vars,const vecteur & UK_k,const vecteur &UK_kp1,int k,int m,GIAC_CONTEXT);
309 
310   // check if a lowercase commandname should be uppercased
311   // returns 0 if not, returns a statically pointer valid up to next call if so
312   char * hp38_display_in_maj(const char * s);
313 
314 #ifndef NO_NAMESPACE_GIAC
315 } // namespace giac
316 #endif // ndef NO_NAMESPACE_GIAC
317 
318 #endif // _GIAC_RPN_H
319