]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
adf31860a570a21e98132988182e5f712cb59ae0
[ginac.git] / ginac / inifcns.cpp
1 /** @file inifcns.cpp
2  *
3  *  Implementation of GiNaC's initially known functions. */
4
5 #include <vector>
6 #include <stdexcept>
7
8 #include "ginac.h"
9
10 //////////
11 // dilogarithm
12 //////////
13
14 ex Li2_eval(ex const & x)
15 {
16     if (x.is_zero())
17         return x;
18     if (x.is_equal(exONE()))
19         return power(Pi, 2) / 6;
20     if (x.is_equal(exMINUSONE()))
21         return -power(Pi, 2) / 12;
22     return Li2(x).hold();
23 }
24
25 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
26
27 //////////
28 // trilogarithm
29 //////////
30
31 ex Li3_eval(ex const & x)
32 {
33     if (x.is_zero())
34         return x;
35     return Li3(x).hold();
36 }
37
38 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
39
40 //////////
41 // factorial
42 //////////
43
44 ex factorial_evalf(ex const & x)
45 {
46     return factorial(x).hold();
47 }
48
49 ex factorial_eval(ex const & x)
50 {
51     if (is_ex_exactly_of_type(x, numeric))
52         return factorial(ex_to_numeric(x));
53     else
54         return factorial(x).hold();
55 }
56
57 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
58
59 //////////
60 // binomial
61 //////////
62
63 ex binomial_evalf(ex const & x, ex const & y)
64 {
65     return binomial(x, y).hold();
66 }
67
68 ex binomial_eval(ex const & x, ex const &y)
69 {
70     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
71         return binomial(ex_to_numeric(x), ex_to_numeric(y));
72     else
73         return binomial(x, y).hold();
74 }
75
76 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
77
78 //////////
79 // Order term function (for truncated power series)
80 //////////
81
82 ex Order_eval(ex const & x)
83 {
84         if (is_ex_exactly_of_type(x, numeric)) {
85
86                 // O(c)=O(1)
87                 return Order(exONE()).hold();
88
89         } else if (is_ex_exactly_of_type(x, mul)) {
90
91                 mul *m = static_cast<mul *>(x.bp);
92                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
93
94                         // O(c*expr)=O(expr)
95                         return Order(x / m->op(m->nops() - 1)).hold();
96                 }
97         }
98         return Order(x).hold();
99 }
100
101 ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
102 {
103         // Just wrap the function into a series object
104         epvector new_seq;
105         new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
106         return series(s, point, new_seq);
107 }
108
109 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
110
111 /** linear solve. */
112 ex lsolve(ex eqns, ex symbols)
113 {
114     // solve a system of linear equations
115     if (eqns.info(info_flags::relation_equal)) {
116         if (!symbols.info(info_flags::symbol)) {
117             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
118         }
119         ex sol=lsolve(lst(eqns),lst(symbols));
120         
121         ASSERT(sol.nops()==1);
122         ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
123         
124         return sol.op(0).op(1); // return rhs of first solution
125     }
126     
127     // syntax checks
128     if (!eqns.info(info_flags::list)) {
129         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
130     }
131     for (int i=0; i<eqns.nops(); i++) {
132         if (!eqns.op(i).info(info_flags::relation_equal)) {
133             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
134         }
135     }
136     if (!symbols.info(info_flags::list)) {
137         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
138     }
139     for (int i=0; i<symbols.nops(); i++) {
140         if (!symbols.op(i).info(info_flags::symbol)) {
141             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
142         }
143     }
144     
145     // build matrix from equation system
146     matrix sys(eqns.nops(),symbols.nops());
147     matrix rhs(eqns.nops(),1);
148     matrix vars(symbols.nops(),1);
149
150     for (int r=0; r<eqns.nops(); r++) {
151         ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
152         ex linpart=eq;
153         for (int c=0; c<symbols.nops(); c++) {
154             ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
155             linpart -= co*symbols.op(c);
156             sys.set(r,c,co);
157         }
158         linpart=linpart.expand();
159         rhs.set(r,0,-linpart);
160     }
161     
162     // test if system is linear and fill vars matrix
163     for (int i=0; i<symbols.nops(); i++) {
164         vars.set(i,0,symbols.op(i));
165         if (sys.has(symbols.op(i))) {
166             throw(std::logic_error("lsolve: system is not linear"));
167         }
168         if (rhs.has(symbols.op(i))) {
169             throw(std::logic_error("lsolve: system is not linear"));
170         }
171     }
172     
173     //matrix solution=sys.solve(rhs);
174     matrix solution;
175     try {
176         solution=sys.fraction_free_elim(vars,rhs);
177     } catch (runtime_error const & e) {
178         // probably singular matrix (or other error)
179         // return empty solution list
180         cerr << e.what() << endl;
181         return lst();
182     }
183     
184     // return a list of equations
185     if (solution.cols()!=1) {
186         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
187     }
188     if (solution.rows()!=symbols.nops()) {
189         cout << "symbols.nops()=" << symbols.nops() << endl;
190         cout << "solution.rows()=" << solution.rows() << endl;
191         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
192     }
193     
194     // return list of the form lst(var1==sol1,var2==sol2,...)
195     lst sollist;
196     for (int i=0; i<symbols.nops(); i++) {
197         sollist.append(symbols.op(i)==solution(i,0));
198     }
199     
200     return sollist;
201 }
202
203 /** non-commutative power. */
204 ex ncpower(ex basis, unsigned exponent)
205 {
206     if (exponent==0) {
207         return exONE();
208     }
209
210     exvector v;
211     v.reserve(exponent);
212     for (unsigned i=0; i<exponent; ++i) {
213         v.push_back(basis);
214     }
215
216     return ncmul(v,1);
217 }
218