Back to index

texmacs  1.0.7.15
Functions
math_tree.cpp File Reference
#include "math_tree.hpp"

Go to the source code of this file.

Functions

tree neg (tree t)
tree add (tree t1, tree t2)
tree sub (tree t1, tree t2)
tree mul (tree t1, tree t2)
tree div (tree t1, tree t2)
tree sqrt (tree t)
tree exp (tree t)
tree log (tree t)
tree pow (tree t1, tree t2)
tree cos (tree t)
tree sin (tree t)
tree tan (tree t)
static void math_print (string &s, tree t, int level)
string as_math_string (tree t)

Function Documentation

tree add ( tree  t1,
tree  t2 
)

Definition at line 25 of file math_tree.cpp.

                       {
  if (is_double (t1) && is_double (t2))
    return as_tree (as_double (t1) + as_double (t2));
  if (t1 == "0") return t2;
  if (t2 == "0") return t1;
  return tree (PLUS, t1, t2);
}

Here is the call graph for this function:

Definition at line 181 of file math_tree.cpp.

                        {
  string r;
  math_print (r, t, 0);
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree cos ( tree  t)

Definition at line 90 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (cos (as_double (t)));
  return tree (COS, t);
}

Here is the call graph for this function:

tree div ( tree  t1,
tree  t2 
)

Definition at line 53 of file math_tree.cpp.

                       {
  if (is_double (t1) && is_double (t2))
    return as_tree (as_double (t1) / as_double (t2));
  if (t1 == "0") return "0";
  if (t2 == "1") return t1;
  return tree (OVER, t1, t2);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree exp ( tree  t)

Definition at line 68 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (exp (as_double (t)));
  return tree (EXP, t);
}

Here is the call graph for this function:

tree log ( tree  t)

Definition at line 74 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (log (as_double (t)));
  return tree (LOG, t);
}

Here is the call graph for this function:

static void math_print ( string s,
tree  t,
int  level 
) [static]

Definition at line 112 of file math_tree.cpp.

                                          {
  if (level <= 0 && is_func (t, PLUS, 2)) {
    math_print (s, t[0], 0);
    s << " + ";
    math_print (s, t[1], 0);
  }
  else if (level <= 0 && is_func (t, MINUS, 2)) {
    math_print (s, t[0], 0);
    s << " - ";
    math_print (s, t[1], 1);
  }
  else if (level <= 1 && is_func (t, TIMES, 2)) {
    math_print (s, t[0], 1);
    s << " * ";
    math_print (s, t[1], 1);
  }
  else if (level <= 1 && is_func (t, OVER, 2)) {
    math_print (s, t[0], 1);
    s << " / ";
    math_print (s, t[1], 2);
  }
  else if (level <= 2 && is_func (t, POW, 2)) {
    math_print (s, t[0], 3);
    s << "^";
    math_print (s, t[1], 2);
  }
  else if (level <= 3 && is_func (t, MINUS, 1)) {
    s << "-";
    math_print (s, t[0], 3);
  }
  else if (level <= 4 && is_func (t, RSUB, 2)) {
    math_print (s, t[0], 4);
    s << "[";
    math_print (s, t[1], 0);
    s << "]";
  }
  else if (is_func (t, PLUS, 2) || is_func (t, MINUS, 2) ||
          is_func (t, TIMES, 2) || is_func (t, OVER, 2) ||
          is_func (t, POW, 2) || is_func (t, MINUS, 1) ||
          is_func (t, RSUB, 2))
    {
      s << "(";
      math_print (s, t, 0);
      s << ")";
    }
  else if (is_atomic (t)) s << t->label;
  else if (is_tuple (t)) {
    int i, n= N(t);
    s << "[ ";
    for (i=0; i<n; i++) {
      if (i != 0) s << ", ";
      math_print (s, t[i], 0);
    }
    if (n != 0) s << " ";
    s << "]";
  }
  else {
    int i, n= N(t);
    if (L(t) == MATH_SQRT) s << "sqrt (";
    else s << as_string (L(t)) << " (";
    for (i=0; i<n; i++) {
      if (i != 0) s << ", ";
      math_print (s, t[i], 0);
    }
    s << ")";
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree mul ( tree  t1,
tree  t2 
)

Definition at line 43 of file math_tree.cpp.

                       {
  if (is_double (t1) && is_double (t2))
    return as_tree (as_double (t1) * as_double (t2));
  if (t1 == "0" || t2 == "0") return "0";
  if (t1 == "1") return t2;
  if (t2 == "1") return t1;
  return tree (TIMES, t1, t2);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree neg ( tree  t)

Definition at line 19 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (-as_double (t));
  return tree (MINUS, t);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree pow ( tree  t1,
tree  t2 
)

Definition at line 80 of file math_tree.cpp.

                       {
  if (is_double (t1) && is_double (t2))
    return as_tree (pow (as_double (t1), as_double (t2)));
  if (t1 == "0" || t1 == "1") return t1;
  if (t2 == "0") return "1";
  if (t2 == "1") return t1;
  return tree (POW, t1, t2);
}

Here is the call graph for this function:

tree sin ( tree  t)

Definition at line 96 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (sin (as_double (t)));
  return tree (SIN, t);
}

Here is the call graph for this function:

tree sqrt ( tree  t)

Definition at line 62 of file math_tree.cpp.

              {
  if (is_double (t)) return as_tree (sqrt (as_double (t)));
  return tree (MATH_SQRT, t);
}

Here is the call graph for this function:

tree sub ( tree  t1,
tree  t2 
)

Definition at line 34 of file math_tree.cpp.

                       {
  if (is_double (t1) && is_double (t2))
    return as_tree (as_double (t1) - as_double (t2));
  if (t1 == "0") return tree (MINUS, t2);
  if (t2 == "0") return t1;
  return tree (MINUS, t1, t2);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree tan ( tree  t)

Definition at line 102 of file math_tree.cpp.

             {
  if (is_double (t)) return as_tree (tan (as_double (t)));
  return tree (TAN, t);
}

Here is the call graph for this function: