[go: up one dir, main page]

Menu

[r9]: / src / equation.cpp  Maximize  Restore  History

Download this file

673 lines (612 with data), 30.9 kB

  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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/***************************************************************************
equation.cpp - Class for handling equations
-------------------
begin : Sun Oct 21 2001
copyright : (C) 2001 by Jan Rheinlaender
email : jrheinlaender@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "equation.h"
#include "unit.h"
#include "printing.h"
#include "expression.h"
#include "msgdriver.h" // *** added in 0.7
//#include "../config/config.h" // *** added in 1.1
#include <sstream>
#include "utils.h" // *** added in 0.8
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(equation, relational,
print_func<print_context>(&equation::do_print).
print_func<print_latex>(&equation::do_print_ltx));
// *** added in 0.9
int equation::nextlabel = 0;
bool equation_init::called = false;
equation_init equation_init::equation_initializer;
equation_init::equation_init() { // *** added in 0.6
if (called) {
// Removed in 1.3.1, msg_error might not be initialized yet
// msg_error.prio(0) << "Attempt to call equation::init() more than one time!" << endline;
return;
} else {
called = true;
equation::init();
}
} // equation_init::equation_init()
bool equation::initialized = false;
void equation::init() { // *** changed in 1.2
if (initialized) return;
#ifndef _MSC_VER
// *** added in 1.4.1 because MSVC does not initialize Digits before running this code
// The program crashes in numeric_digits& _numeric_digits::operator=(long prec) at for (; it != end; ++it) {
Digits = 9; // This must not be too high, because of things like x^(0.99999999999999995)...
#endif
initialized = true;
} // equation::init()
void equation::clear() { // *** added in 0.8
nextlabel = 0;
Digits = 9;
} // equation::clear()
// constructors
equation::equation() : inherited(numeric(0), numeric(0), relational::equal),
label(""), al(none), type(empty), ltxrepr("") { // *** added in 0.9
// tinfo_key = TINFO_equation; *** removed in 1.2.1
// tinfo_key = &equation::tinfo_static; *** removed in 1.3.1
} // equation::equation()
equation::equation(const expression &le, const expression &ri, const operators op,
const aligntype align, const eqtype t, const std::string &l)
: inherited(le, ri, op), label(l) {
// tinfo_key = TINFO_equation; *** removed in 1.2.1
// tinfo_key = &equation::tinfo_static; *** removed in 1.3.1
type = ((le.is_empty() && ri.is_empty()) ? empty : t);
if ((le.getltxrepr() == "") || (ri.getltxrepr() == "") || (type == empty)) {
ltxrepr = "";
} else {
std::string oper; // The ltxrepr of the two expressions already have ampersands!!
switch(o) {
case relational::equal: oper = "="; break;
case relational::not_equal: oper = "\\neq"; break;
case relational::less: oper = "<"; break;
case relational::less_or_equal: oper = "\\leq"; break;
case relational::greater: oper = ">"; break;
case relational::greater_or_equal: oper = "\\geq"; break;
default: oper = "[invalid operator]";
}
ltxrepr = le.getltxrepr() + oper + ri.getltxrepr();
}
if (le.getampersand()) {
al = ri.getampersand() ? both : onlyleft;
} else if (ri.getampersand()) {
al = le.getampersand() ? both : onlyright;
} else {
al = align; // TODO: In the other cases align is ignored
}
MSG_INFO(3) << "Created equation '" << le << " = " << ri << "' with ltxrepr '" << ltxrepr << "'" << endline;
} // equation::equation()
equation::equation(const equation &eq) : inherited(eq.lh, eq.rh, eq.o),
label(eq.label), al(eq.al), type(eq.type), ltxrepr(eq.ltxrepr) {
// tinfo_key = TINFO_equation; *** removed in 1.2.1
// tinfo_key = &equation::tinfo_static; *** removed in 1.3.1
} // equation::equation(const equation&)
equation &equation::operator=(const equation &other) { // *** added in 0.9
lh = other.lh;
rh = other.rh;
o = other.o;
al = other.al;
label = other.label;
type = other.type;
ltxrepr = other.ltxrepr;
return *this;
} // equation::operator=()
// *** GiNaC standard methods
/* equation::equation(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {
// *** added in 0.9, removed in 1.3.1
n.find_string("label", label);
} // equation::equation(const archive_node&, lst&)
*/
void equation::read_archive(const archive_node &n, lst &sym_lst) { // *** added in 0.9, changed in 1.3.1 to read_archive() format
inherited::read_archive(n, sym_lst);
n.find_string("label", label);
//return (new equation(n, sym_lst))->setflag(status_flags::dynallocated);
} // equation::unarchive()
GINAC_BIND_UNARCHIVER(equation); // *** added in 1.3.1
void equation::archive(archive_node &n) const { // *** added in 0.9
inherited::archive(n);
n.add_string("label", label);
} // equation::archive()
int equation::compare_same_type(const basic &other) const { // *** added in 0.9
return inherited::compare_same_type(other);
} // equation::compare_same_type()
// methods
const bool equation::is_assignment() const {
if (msg::info().checkprio(6)) {
msg::info() << endline;
std::ostringstream os;
os << tree << rh << dflt;
msg::info() << os.str();
msg::info() << endline;
}
if (!is_a<symbol>(lh) || (o != relational::equal)) return false;
// *** changed is_a<symbol> to is_a_symbol in 0.4
// *** added check for equals in 0.9
// parse the expression tree and check that it contains only numerics and Units.
return is_quantity(rh);
}
const bool equation::is_temporary() const { // *** added in 0.7
return ((label == "") || ((label[0] == '{') && (label[label.size() - 1] == '}')));
} // equation::is_temporary()
const equation &equation::settemplabel() {
// *** added in 0.7, changed return type in 0.9, changed to use sprintf() in 1.0, changed to char[10] in 1.3 because of buffer overflow
char l[10]; // Hopefully nobody ever creates more than a million equations...
sprintf(l, "%c%7u%c", '{', nextlabel++, '}');
label = l;
// msg::info().setlevel(1);
// MSG_INFO(0) << "Set equation " << *this << " to label " << label << ". Next label: " << nextlabel << endline;
// msg::info().setlevel(-1);
return *this;
} // settemplabel()
const equation &equation::addalign(const aligntype &align) { // *** added in 0.9
switch(al) {
case none: al = align; break;
case onlyleft: al = ((align == left) ? onlyleft : both); break;
case onlyright: al = ((align == right) ? onlyright : both); break;
default: al = align;
}
return *this;
} // equation::addalign()
void equation::print_oper(const print_context &c) const {
switch(o) {
case relational::equal: c.s << "=="; break;
case relational::not_equal: c.s << "!="; break;
case relational::less: c.s << "<"; break;
case relational::less_or_equal: c.s << "<="; break;
case relational::greater: c.s << ">"; break;
case relational::greater_or_equal: c.s << ">="; break;
default: c.s << "[invalid operator]";
}
} // equation::print_oper()
void equation::print_oper_ltx(const print_context &c) const {
std::string oper;
switch(o) {
case relational::equal: oper = "="; break;
case relational::not_equal: oper = "\\neq"; break;
case relational::less: oper = "<"; break;
case relational::less_or_equal: oper = "\\leq"; break;
case relational::greater: oper = ">"; break;
case relational::greater_or_equal: oper = "\\geq"; break;
default: c.s << "[invalid operator]";
}
switch (optstack::options->get(o_eqalign).align) {
case none: { c.s << oper; break; }
case onlyleft: { c.s << "&" << oper; break; }
case onlyright: { c.s << oper << "&"; break; }
case both: { c.s << "&" << oper << "&"; break; }
default: { c.s << "[invalid alignment]"; }
}
} // equation::print_oper()
void equation::do_print(const print_context &c, unsigned level) const { // *** changed in 0.9
if (type == empty) { // *** added in 0.6
c.s << "[empty equation]";
} else {
c.s << expression(lh).print(); // *** changed in 1.3.1, just doing c.s << lh crashes openoffice
print_oper(c);
c.s << expression(rh).print();
}
} // equation::do_print()
const unsigned countampersand(const std::string &s) { // *** added in 0.9
unsigned n = 0;
unsigned pos = 0;
while ((pos = s.find("&", pos)) < s.size()) { n++; pos++; }
return n;
} // countampersand()
const std::string removeampersand(const std::string &s) {
unsigned pos = 0;
std::string result = s;
while ((pos = result.find("&", pos)) < result.size()) { result.erase(pos, 1); pos--; }
return result;
} // removeampersand()
void equation::do_print_ltx(const print_context &c, unsigned level) const { // *** added in 0.9
MSG_INFO(2) << "Printing equation (do_print_ltx) " << lh << " = " << rh << endline;
if (type == empty) {
c.s << "[empty equation]";
} else {
if (optstack::options->get(o_eqginac).boolean) { // *** added in 1.1 for test purposes
c.s << latex << Unit::subst_units(lh, *optstack::options->get(o_units).vec); // *** added optstack::options->get in 1.4.1
print_oper_ltx(c);
c.s << latex << Unit::subst_units(rh, *optstack::options->get(o_units).vec);
} else if (optstack::options->get(o_eqraw).boolean && (ltxrepr != "")) {
// Note: Automatic alignment occurs even in eqraw mode-use eqalign=none etc. to selectively turn it off
switch (optstack::options->get(o_eqalign).align) {
case none: { c.s << removeampersand(ltxrepr); break; }
case onlyleft: { if (countampersand(ltxrepr) != 1) {
std::string lr = removeampersand(ltxrepr);
c.s << lr.insert(lr.find("="), "&");
} else
c.s << ltxrepr;
break;
}
case onlyright: { if (countampersand(ltxrepr) != 1) {
std::string lr = removeampersand(ltxrepr);
c.s << lr.insert(lr.find("=") - 1, "&");
} else
c.s << ltxrepr;
break;
}
case both: { if (countampersand(ltxrepr) != 2) {
std::string lr = removeampersand(ltxrepr);
lr.insert(lr.find("="), "&"); // *** corrected this in 1.2
c.s << lr.insert(lr.find("=") + 1, "&");
} else
c.s << ltxrepr;
break;
}
default: { c.s << "[invalid alignment]"; }
}
} else {
if (optstack::options->get(o_eqalign).align == none) {
print_ltx(Unit::subst_units(lh, *optstack::options->get(o_units).vec), c.s, true); // *** changed in 1.0, added optstack::options->get in 1.4.1
} else if (!optstack::options->get(o_eqchain).boolean) {
print_ltx(Unit::subst_units(lh, *optstack::options->get(o_units).vec), c.s, true); // *** changed in 1.0
} // else omit the lhs
print_oper_ltx(c);
int pos = optstack::options->get(o_eqsplit).integer;
if (pos > 0) {
optstack::options->save(o_eqsplit);
option_value o;
o.expr = new ex((int)(pos - (lh.nops() == 0?1:lh.nops())));// TODO: status_flags::dyn_allocated?
optstack::options->set(o_eqsplit, o); // Adjust the split position *** added in 1.2
MSG_INFO(2) << "Adjusted split position for rhs to "
<< optstack::options->get(o_eqsplit).integer << endline;
print_ltx(Unit::subst_units(rh, *optstack::options->get(o_units).vec), c.s, true); // *** changed in 1.0, added optstack::options->get in 1.4.1
optstack::options->restore(o_eqsplit);
} else {
print_ltx(Unit::subst_units(rh, *optstack::options->get(o_units).vec), c.s, true);
}
}
}
} // equation::do_print_ltx()
const equation equation::subs(const ex &e, unsigned options) const
throw (std::invalid_argument) { // *** added in 0.9
return equation(expression(lh).subs(e, options), expression(rh).subs(e, options),
o, al, derived);
} // equation::subs(const ex&, unsigned)
ex equation::subs(const exmap &m, unsigned options) const
throw (std::invalid_argument) { // *** added in 1.0
return equation(expression(lh).subs(m, options), expression(rh).subs(m, options),
o, al, derived);
} // equation::subs(const exmap &, unsigned)
const equation equation::csubs(const exmap &m, unsigned options) const { // *** added in 0.9
return equation(expression(lh).csubs(m, options), expression(rh).csubs(m,options), o, al, derived);
} // equation::csubs()
const equation equation::csubs(const ex &e, unsigned options) const
throw(std::invalid_argument) {
return equation(expression(lh).csubs(e, options), expression(rh).csubs(e, options), o, al, derived);
} // equation::csubs(const lst&, unsigned)
ex equation::expand(unsigned options) const {
// *** added in 0.7, changed return type to ex in 0.9
// *** added cast to expression in 1.0 to benefit from expand_real_powers
MSG_INFO(1) << "Expanding " << *this << endline;
return equation(expression(lh).expand(options), expression(rh).expand(options), o, al, derived);
} // equation::expand()
const equation equation::evalf() const { // *** added in 0.7
MSG_INFO(3) << "Evaluating " << *this << endline;
return equation(lh.evalf(), rh.evalf(), o, al, derived);
} // equation::evalf()
ex equation::evalm() const { // *** added in 1.0
MSG_INFO(3) << "Evaluating matrices in " << *this << endline;
equation result = *this;
result.lh = lh.evalm();
result.rh = rh.evalm();
return result;
} // equation::evalm()
const equation equation::evalu() const { // *** added in 0.9
return equation(expression(lh).evalu(), expression(rh).evalu(), o, al, derived);
} // equation::evalu()
const equation &equation::eqadd(const expression &add) throw (std::invalid_argument) {
// *** added in 0.9
if (is_a<equation>(add)) {
lh += ex_to<equation>(add).lh;
rh += ex_to<equation>(add).rh;
if (o != ex_to<equation>(add).o)
throw std::invalid_argument("The two equations must have the same operator sign");
if (al != ex_to<equation>(add).al)
msg::warn(1) << "Warning: Alignment types of the equations mismatch" << endline;
} else {
lh += add;
rh += add;
}
type = derived;
return *this;
} // equation::eqadd()
const equation &equation::eqmul(const expression &mul) throw (std::invalid_argument) {
// *** added in 0.9
if (is_a<equation>(mul))
throw std::invalid_argument("Cannot multiply two equations");
if (o != relational::equal)
msg::warn(0) << "Warning: Operator sign might have changed in multiplication" << endline;
lh *= mul;
rh *= mul;
type = derived;
return *this;
} // equation::eqmul()
const equation equation::normal() const { // *** added in 0.9
return equation(lh.normal(), rh.normal(), o, al, derived);
} // equation::normal()
const expression equation::apply_func(const expression &e) const throw (std::invalid_argument) {
// *** added in 0.9
MSG_INFO(1) << "Applying function " << e << " to " << *this << endline;
if (!is_a<func>(e))
throw std::invalid_argument("Argument must be a function name");
return equation(func(ex_to<func>(e).get_name(), lh),
func(ex_to<func>(e).get_name(), rh), o, al, derived);
} // equation::apply_func()
const expression equation::apply_func(const std::string &fname) const throw (std::invalid_argument) {
// *** added in 1.4.1
MSG_INFO(1) << "Applying function " << fname << " to " << *this << endline;
if (!func::is_a_func(fname))
throw std::invalid_argument("Argument must be a function name");
return equation(func(fname, lh), func(fname, rh), o, al, derived);
} // equation::apply_func()
const expression equation::apply_power(const expression &e) const throw (std::invalid_argument) {
// *** added in 1.4.1
MSG_INFO(1) << "Raising " << *this << " to the power of " << e << endline;
return equation(power(lh, e), power(rh, e), o, al, derived);
} // equation::apply_power()
const expression equation::diff(const ex &var, unsigned nth) const
throw (std::logic_error) { // *** added in 1.0
if (is_a<symbol>(var))
return diff(ex_to<symbol>(var), nth);
else if (is_a<func>(var) && ex_to<func>(var).is_pure())
return equation(diff_to_func(lh, ex_to<func>(var), nth),
diff_to_func(rh, ex_to<func>(var), nth), o, al, derived);
else
throw std::logic_error("Can only differentiate with respect to a variable or a pure function!");
} // equation::diff()
const expression equation::diff(const symbol &var, unsigned nth) const {
// *** added in 0.6, changed in 0.9
MSG_INFO(1) << "Calculating " << nth << "nth derivative of " << *this << " to " << var << endline;
func::replace_function_by_func replace_functions; // get rid of GiNaC functions that might have been introduced *** added in 1.3.0
return equation(replace_functions(lh.diff(var, nth)),
replace_functions(rh.diff(var, nth)), o, al, derived); // *** added nth in 1.3.0, this had been forgotten!
// *** changed this->equals() to equal_sign in 0.7
} // equation::diff()
const equation equation::reverse() const {
MSG_INFO(1) << "Reversed equation " << *this << endline;
return equation(rh, lh, o, al, derived);
// *** changed in_eqnarray to equals in 0.5, added derived in 0.6
// *** changed equals() to getequals() in 0.8
} // equation::reverse()
const equation equation::simplify(const std::vector<std::string> &s) const {
// *** changed to handle a vector of simplifications in 0.9
// *** removed bug in 1.2: result.lhs() was result.lh etc. for diff and sum
equation result(*this);
for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); i++) {
if (*i == "expand") { // Full expansion
MSG_INFO(1) << "Full expanding " << result << endline;
result = ex_to<equation>(result.expand(expand_options::expand_function_args));
} else if (*i == "expandf") { // Do not expand function args
MSG_INFO(1) << "Expanding " << result << endline;
result = ex_to<equation>(result.expand());
} else if (*i == "eval") { // Evaluation *** added in 0.7
MSG_INFO(1) << "Evaluating " << result << endline;
result = result.evalf();
} else if (*i == "normal") { // Normalization *** added in 0.7
MSG_INFO(1) << "Normalizing " << result << endline;
result = result.normal();
} else if (*i == "collect-common") { // Collecting common factors *** added in 0.8
MSG_INFO(1) << "Collecting common factors in " << result << endline;
result = collect_common_factors(result);
} else if (*i == "unsafe") { // Unsafe simplifications *** added in 0.9
MSG_INFO(1) << "Doing unsafe simplifications in " << result << endline;
result = result.evalu();
} else if (*i == "diff") { // Evalute \diff function objects *** added in 1.0
func::expand_partialdiff expand_diff;
result = equation(expand_diff(result.lhs()), expand_diff(result.rhs()), o, al, derived);
} else if (*i == "sum") { // Evalute \sum function objects *** added in 1.0
func::expand_sum expand_s;
result = equation(expand_s(result.lhs()), expand_s(result.rhs()), o, al, derived);
} else if (*i == "gather-sqrt") { // *** added in 1.2
MSG_INFO(1) << "Gathering square roots in " << result << endline;
gather_sqrt gather_sqrts;
result = equation(gather_sqrts(result.lhs()), gather_sqrts(result.rhs()), o, al, derived);
} else if (*i == "integrate") { // *** added in 1.3.1
MSG_INFO(1) << "Symbolically integrating " << result << endline;
result = equation(result.lhs().eval_integ(), result.rhs().eval_integ(), o, al, derived);
MSG_INFO(2) << "Result: " << result << endline;
} else { // Nothing of the above fitted
msg::warn(0) << "Warning: Unknown simplification of type " << *i << endline;
}
}
return result;
} // equation::simplify()
expression qsolve(const std::vector<expression> &coeff, const int num) {
// Helper function for equation::solve()
expression help = coeff[1]/expression((numeric(2) * coeff[2]));
expression det = pow(help, numeric(2)) - coeff[0]/coeff[2];
if (det.is_zero()) {
if (num == 1) msg::warn(1) << "Warning: This equation has only one solution" << endline;
return (-help);
} else {
if (num > 2) msg::warn(1) << "Warning: This equation has only two solutions" << endline;
return ((num == 1) ? -help + pow(det, expression(numeric(1,2)))
: -help - pow(det, expression(numeric(1,2))));
}
} // qsolve()
expression csolve(const std::vector<expression> &coeff, const int num) {
// Helper function for equation::solve() *** added in 1.0
// Use the formulae of Cardan to solve the cubic problem. The solution is done for
// y^3 + 3 p y + 2 q = 0, where y = x + b/(3a)
// Note that sqrt(3) can not be used because it evaluates to a real number immediately
expression q = numeric(1,2) * (numeric(2) * pow(coeff[2], numeric(3)) /
(numeric(27) * pow(coeff[3], numeric(3))) -
coeff[2] * coeff[1] / (numeric(3) * pow(coeff[3], numeric(2))) +
coeff[0]/coeff[3]);
expression p = numeric(1,3) * ((numeric(3) * expression(coeff[3] * coeff[1]) -
pow(coeff[2], numeric(2))) /
(numeric(3) * pow(coeff[3], numeric(2))));
//0) << "Coefficient p : " << p << ", q: " << q << endline;
expression u = pow(-q + pow(pow(q, numeric(2)) + pow(p, numeric(3)), numeric(1,2)), numeric(1,3));
expression v = pow(-q - pow(pow(q, numeric(2)) + pow(p, numeric(3)), numeric(1,2)), numeric(1,3));
expression eps1 = numeric(-1,2) + I * numeric(1,2) * pow(3, numeric(1,2));
expression eps2 = numeric(-1,2) - I * numeric(1,2) * pow(3, numeric(1,2));
//0) << "eps1 = " << eps1 << "; eps2 = " << eps2 << endline;
expression y1 = u + v;
expression y2 = eps1 * u + eps2 * v;
expression y3 = eps2 * u + eps1 * v;
switch (num) {
case 0: // This returns the first solution, also
case 1: return (y1 - expression(coeff[2] / expression(numeric(3) * coeff[3])));
case 2: return (y2 - expression(coeff[2] / expression(numeric(3) * coeff[3])));
case 3: return (y3 - expression(coeff[2] / expression(numeric(3) * coeff[3])));
default:
throw std::invalid_argument("Not more than three solutions exist");
}
} // csolve()
const equation equation::solve(const expression &e, const expression &n) const
throw(std::invalid_argument) {
// *** added in 0.8
MSG_INFO(1) << "Solving " << *this << " for " << e << ", solution #" << n << " requested " <<endline;
if (!is_a<symbol>(e)) throw std::invalid_argument("Solving only works for symbols!");
if (!is_a<numeric>(n)) throw std::invalid_argument("The solution number must be numerical!");
if (!ex_to<numeric>(n).is_pos_integer())
throw std::invalid_argument("The solution number must be a positive integer!");
unsigned num = ex_to<numeric>(n).to_int();
expression problem = (lh - rh).expand().normal(); // *** added normal() and numer() in 1.1
if (!denom(problem).is_equal(1)) {
MSG_WARN(2) << "Warning: The solution may not be defined for some values because the denominator is not 1" << endline;
problem = numer(problem);
}
unsigned degree = problem.degree(e);
if (num > degree) {
msg::warn(0) << "Warning: This equation can only have " << degree
<< " solutions. Returning the last one." << endline;
num = degree;
}
std::vector<expression> coeff;
for (unsigned i = 0; i <= degree; i++) coeff.push_back(problem.coeff(e, i));
// The problem now has the form coeff[0] + coeff[1] * e + ... + coeff[degree] * e^degree
// TODO: What about negative degrees?
if (msg::info().checkprio(2)) {
msg::info() << "Degree of the problem is " << degree << endline << "Coefficients: ";
for (std::vector<expression>::const_iterator i = coeff.begin(); i < coeff.end(); i++)
msg::info() << *i << " ";
msg::info() << endline;
}
switch (degree) {
case 0: return *this;
case 1: return equation (e, -1 * (coeff[0]/coeff[1]), o, al, derived);
case 2: return equation (e, qsolve(coeff, num), o, al, derived);
case 3: {
if (coeff[1].is_zero() && coeff[2].is_zero()) { // added in 1.4.1, since csolve produces division by zero
if (num > 1)
msg::warn(0) << "Warning: This equation can only have one solution. Returning it." << endline;
return equation(e, pow(-1 * (coeff[0]/coeff[3]), numeric(1,3)), o, al, derived);
} else {
return equation (e, csolve(coeff, num), o, al, derived); // *** added in 1.0
}
}
case 4: {
if (coeff[1].is_zero() && coeff[3].is_zero()) {
// The problem has the form coeff[0] + coeff[2] * e^2 + coeff[4] * e^4
std::vector<expression> newcoeff;
newcoeff.push_back(coeff[0]);
newcoeff.push_back(coeff[2]);
newcoeff.push_back(coeff[4]);
switch (num) {
case 1: return equation(e, sqrt(qsolve(newcoeff, 1)), o, al, derived);
case 2: return equation(e, -sqrt(qsolve(newcoeff, 1)), o, al, derived);
case 3: return equation(e, sqrt(qsolve(newcoeff, 2)), o, al, derived);
case 4: return equation(e, -sqrt(qsolve(newcoeff, 2)), o, al, derived);
}
}
}
default: {
throw std::invalid_argument("Solving for higher degrees than 3 is not implemented yet.");
// TODO: Check whether degree() - ldegree() > 1 instead
}
}
} // equation::solve()
const equation collect_common_factors(const equation &eq) {
return equation(GiNaC::collect_common_factors(eq.lhs()),
GiNaC::collect_common_factors(eq.rhs()),
eq.getop(), eq.getalign(), derived);
} // collect_common_factors(const equation &)
const ex operator+(const expression &e, const expression &add) throw(std::invalid_argument) {
// *** changed in 0.9
MSG_INFO(3) << "Adding " << add << " to " << e << endline;
if (is_a<equation>(e)) {
equation result = ex_to<equation>(e);
return result.eqadd(add);
} else if (is_a<equation>(add)) {
equation result = ex_to<equation>(add);
return result.eqadd(e);
} else
return operator+(ex(e), ex(add));
} // operator+()
const ex operator-(const expression &e, const expression &sub) throw(std::invalid_argument) {
// *** changed in 0.9
MSG_INFO(3) << "Subtracting " << sub << " from " << e << endline;
if (is_a<equation>(e)) {
equation result = ex_to<equation>(e);
return result.eqadd(sub * numeric(-1));
} else if (is_a<equation>(sub)) {
equation result = ex_to<equation>(sub * numeric(-1));
return result.eqadd(e);
} else
return operator-(ex(e), ex(sub));
}
const ex operator*(const expression &e, const expression &mul) throw(std::invalid_argument) {
// *** changed in 0.9
MSG_INFO(3) << "Multiplying " << e << " with " << mul << endline;
if (mul == 0) throw std::invalid_argument("Multiplication with zero");
if (is_a<equation>(e)) {
equation result = ex_to<equation>(e);
return result.eqmul(mul);
} else if (is_a<equation>(mul)) {
equation result = ex_to<equation>(mul);
return result.eqmul(e);
} else
return operator*(ex(e), ex(mul));
}
const ex operator/(const expression &e, const expression &divisor) throw(std::invalid_argument) {
// *** changed in 0.9
MSG_INFO(3) << "Dividing " << e << " by " << divisor << endline;
if (divisor == 0) throw std::invalid_argument("Division by zero");
if (is_a<equation>(e)) {
equation result = ex_to<equation>(e);
return result.eqmul(pow(divisor, numeric(-1)));
} else if (is_a<equation>(divisor)) {
equation result = ex_to<equation>(pow(divisor, numeric(-1)));
return result.eqmul(e);
} else
return operator/(ex(e), ex(divisor));
}
const ex pow(const expression &e, const expression &exponent) throw (std::invalid_argument) {
// *** added in 0.9, takes care of equations
MSG_INFO(3) << "Calculating power of " << e << " to " << exponent << endline;
if (is_a<equation>(exponent))
throw std::invalid_argument("Cannot calculate power to exponent that is an equation");
if (is_a<equation>(e)) {
equation eq = ex_to<equation>(e);
if (eq.getop() != relational::equal)
msg::warn(0) << "Warning: Operator sign might have changed in exponentiation" << endline;
return equation(pow(eq.lhs(), exponent), pow(eq.rhs(), exponent), eq.getop(), eq.getalign(), derived) ;
} else
return pow(ex(e), ex(exponent));
}
const ex operator-(const expression &e) { // *** added in 0.9
return e * numeric(-1);
}
// new input/output operators
/*message &operator<<(message &ms, const equation &eq) { // *** changed to message stream in 0.7
std::ostringstream os;
eq.print(print_dflt(os)); // *** changed to use print_dflt in 0.9
ms << os.str();
return (ms);
}
*/