19 Func1::Func1(
const Func1& right) :
23 m_parent(right.m_parent)
31 Func1& Func1::operator=(
const Func1& right)
39 m_parent = right.m_parent;
68 cout <<
"derivative error... ERR: ID = " << ID() << endl;
69 cout << write(
"x") << endl;
75 if ((ID() != other.ID()) || (m_c != other.m_c)) {
126 Func1& Func1::func1_dup()
const
132 Func1& Func1::func2_dup()
const
138 Func1* Func1::parent()
const
144 void Func1::setParent(Func1* p)
151 string Sin1::write(
string arg)
const
157 return "\\sin(" + c + arg +
")";
163 Func1* r = &newTimesConstFunction(*c, m_c);
171 Func1* r = &newTimesConstFunction(*s, -m_c);
175 std::string Cos1::write(std::string arg)
const
181 return "\\cos("+c+arg+
")";
190 return newTimesConstFunction(*f, m_c);
196 std::string Exp1::write(std::string arg)
const
202 return "\\exp("+c+arg+
")";
212 }
else if (m_c == 1.0) {
216 r = &newTimesConstFunction(*f, m_c);
221 string Func1::write(std::string arg)
const
223 return "<unknown " +
int2str(ID()) +
">("+arg+
")";
229 string Pow1::write(
string arg)
const
234 return "\\sqrt{" + arg +
"}";
237 return "\\frac{1}{\\sqrt{" + arg +
"}}";
241 return "\\left("+arg+
"\\right)^{"+c+
"}";
248 string Const1::write(
string arg)
const
256 string Ratio1::write(
string arg)
const
259 return "\\frac{" + m_f1->write(arg) +
"}{"
260 + m_f2->write(arg) +
"}";
263 string Product1::write(
string arg)
const
266 string s = m_f1->write(arg);
268 s =
"\\left(" + s +
"\\right)";
270 string s2 = m_f2->write(arg);
272 s2 =
"\\left(" + s2 +
"\\right)";
277 string Sum1::write(
string arg)
const
280 string s1 = m_f1->write(arg);
281 string s2 = m_f2->write(arg);
283 return s1 +
" - " + s2.substr(1,s2.size());
285 return s1 +
" + " + s2;
289 string Diff1::write(
string arg)
const
292 string s1 = m_f1->write(arg);
293 string s2 = m_f2->write(arg);
295 return s1 +
" + " + s2.substr(1,s2.size());
297 return s1 +
" - " + s2;
301 string Composite1::write(
string arg)
const
304 string g = m_f2->write(arg);
305 return m_f1->write(g);
308 string TimesConstant1::write(
string arg)
const
311 string s = m_f1->write(arg);
313 s =
"\\left(" + s +
"\\right)";
322 if (n >=
'0' && n <=
'9') {
323 s =
"\\left(" + s +
"\\right)";
328 string PlusConstant1::write(
string arg)
const
332 return m_f1->write(arg);
334 return m_f1->write(arg) +
" + " +
fp2str(m_c);
337 doublereal Func1::isProportional(TimesConstant1& other)
344 doublereal Func1::isProportional(Func1& other)
353 static bool isConstant(Func1& f)
355 if (f.ID() == ConstFuncType) {
362 static bool isZero(Func1& f)
364 if (f.ID() == ConstFuncType && f.c() == 0.0) {
371 static bool isOne(Func1& f)
373 if (f.ID() == ConstFuncType && f.c() == 1.0) {
380 static bool isTimesConst(Func1& f)
382 if (f.ID() == TimesConstantFuncType) {
389 static bool isExp(Func1& f)
391 if (f.ID() == ExpFuncType) {
398 static bool isPow(Func1& f)
400 if (f.ID() == PowFuncType) {
407 Func1& newSumFunction(Func1& f1, Func1& f2)
409 if (f1.isIdentical(f2)) {
410 return newTimesConstFunction(f1, 2.0);
420 doublereal c = f1.isProportional(f2);
423 return *(
new Const1(0.0));
425 return newTimesConstFunction(f1, c + 1.0);
428 return *(
new Sum1(f1, f2));
431 Func1& newDiffFunction(Func1& f1, Func1& f2)
437 if (f1.isIdentical(f2)) {
440 return *(
new Const1(0.0));
442 doublereal c = f1.isProportional(f2);
445 return *(
new Const1(0.0));
447 return newTimesConstFunction(f1, 1.0 - c);
450 return *(
new Diff1(f1, f2));
453 Func1& newProdFunction(Func1& f1, Func1& f2)
463 if (isZero(f1) || isZero(f2)) {
466 return *(
new Const1(0.0));
468 if (isConstant(f1) && isConstant(f2)) {
469 doublereal c1c2 = f1.
c() * f2.c();
472 return *(
new Const1(c1c2));
474 if (isConstant(f1)) {
475 doublereal c = f1.c();
477 return newTimesConstFunction(f2, c);
479 if (isConstant(f2)) {
480 doublereal c = f2.c();
482 return newTimesConstFunction(f1, c);
485 if (isPow(f1) && isPow(f2)) {
486 Func1& p = *(
new Pow1(f1.c() + f2.c()));
492 if (isExp(f1) && isExp(f2)) {
493 Func1& p = *(
new Exp1(f1.c() + f2.c()));
499 bool tc1 = isTimesConst(f1);
500 bool tc2 = isTimesConst(f2);
503 doublereal c1 = 1.0, c2 = 1.0;
504 Func1* ff1 = 0, *ff2 = 0;
507 ff1 = &f1.func1_dup();
514 ff2 = &f2.func1_dup();
519 Func1& p = newProdFunction(*ff1, *ff2);
522 return newTimesConstFunction(p, c1*c2);
527 return *(
new Product1(f1, f2));
531 Func1& newRatioFunction(Func1& f1, Func1& f2)
537 return *(
new Const1(0.0));
539 if (f1.isIdentical(f2)) {
542 return *(
new Const1(1.0));
544 if (f1.ID() == PowFuncType && f2.ID() == PowFuncType) {
545 return *(
new Pow1(f1.c() - f2.c()));
547 if (f1.ID() == ExpFuncType && f2.ID() == ExpFuncType) {
548 return *(
new Exp1(f1.c() - f2.c()));
550 return *(
new Ratio1(f1, f2));
553 Func1& newCompositeFunction(Func1& f1, Func1& f2)
558 return *(
new Const1(0.0));
560 if (isConstant(f1)) {
564 if (isPow(f1) && f1.c() == 1.0) {
568 if (isPow(f1) && f1.c() == 0.0) {
571 return *(
new Const1(1.0));
573 if (isPow(f1) && isPow(f2)) {
574 doublereal c1c2 = f1.
c() * f2.c();
577 return *(
new Pow1(c1c2));
579 return *(
new Composite1(f1, f2));
582 Func1& newTimesConstFunction(Func1& f, doublereal c)
586 return *(
new Const1(0.0));
591 if (f.ID() == TimesConstantFuncType) {
595 return *(
new TimesConstant1(f, c));
598 Func1& newPlusConstFunction(Func1& f, doublereal c)
604 doublereal cc = f.
c() + c;
606 return *(
new Const1(cc));
608 if (f.ID() == PlusConstantFuncType) {
612 return *(
new PlusConstant1(f, c));