# Path: sdopt/nodes/attributes.py
# class NodeAttr:
#     bounds    =  1
#     con_num   =  2
#     d_term    =  3
#     display   =  4
#     input_ord =  5
#     n_term    =  6
#     name      =  7
#     node_id   =  8
#     number    =  9
#     operation = 10
#     type      = 11
#     var_num   = 12
# 
# Path: sdopt/representation/dag_util.py
# def get_pretty_type_str(dag, n):
#     return dag.node[n][NodeAttr.type] + '_node'
# 
# Path: sdopt/util/stringIO.py

# 
# Path: sdopt/util/redirect_stdout.py
# @contextmanager
# def redirect_stdout(ostream):
#     oldstdout = sys.stdout
#     sys.stdout = ostream
#     try:
#         yield ostream
#     finally:
#         sys.stdout = oldstdout


import math
from operator import itemgetter
from string import Template
from contextlib import closing
from six.moves import zip as izip
from ..nodes.attributes import NodeAttr
from ..representation.dag_util import get_pretty_type_str
from ..util.stringIO import stringIO
from ..util.redirect_stdout import redirect_stdout
    # body <+ d_term as string>, this function gives the latter
    #  0 -> ''
    #  3 -> ' + 3'
    # -2 -> ' - 2'
    if   d_term == 0:
        return ''
    s = ' + '
    if d_term < 0:
        s = ' - '
    s += str(math.fabs(d_term))
    return s

def lmul_d_term_str(d_term):
    # <d_term as string> * body, this function gives the former
    #  1 -> ''
    # -1 -> '-'
    #  3 -> '3*'
    # -3 -> '-3*
    assert d_term!=0.0
    if d_term ==  1.0:
        return ''
    if d_term == -1.0:
        return '-'
    return '%s * ' % str(d_term)

def idx_str(i, base_vars, con_dag):
    if i in base_vars:
        return 'v[%d]' % base_vars[i]
    # a number
    d = con_dag.node[i]

# Path: sdopt/nodes/attributes.py
# class NodeAttr:
#     bounds    =  1
#     con_num   =  2
#     d_term    =  3
#     display   =  4
#     input_ord =  5
#     n_term    =  6
#     name      =  7
#     node_id   =  8
#     number    =  9
#     operation = 10
#     type      = 11
#     var_num   = 12
# 
# Path: sdopt/representation/dag_util.py
# def get_pretty_type_str(dag, n):
#     return dag.node[n][NodeAttr.type] + '_node'
# 
# Path: sdopt/util/stringIO.py

# 
# Path: sdopt/util/redirect_stdout.py
# @contextmanager
# def redirect_stdout(ostream):
#     oldstdout = sys.stdout
#     sys.stdout = ostream
#     try:
#         yield ostream
#     finally:
#         sys.stdout = oldstdout


# Path: sdopt/nodes/reverse_ad.py
import math
from operator import itemgetter
from string import Template
from contextlib import closing
from six.moves import zip as izip
from ..nodes.attributes import NodeAttr
from ..representation.dag_util import get_pretty_type_str
from ..util.stringIO import stringIO
from ..util.redirect_stdout import redirect_stdout

    # body <+ d_term as string>, this function gives the latter
    #  0 -> ''
    #  3 -> ' + 3'
    # -2 -> ' - 2'
    if   d_term == 0:
        return ''
    s = ' + '
    if d_term < 0:
        s = ' - '
    s += str(math.fabs(d_term))
    return s

def lmul_d_term_str(d_term):
    # <d_term as string> * body, this function gives the former
    #  1 -> ''
    # -1 -> '-'
    #  3 -> '3*'
    # -3 -> '-3*
    assert d_term!=0.0
    if d_term ==  1.0:
        return ''
    if d_term == -1.0:
        return '-'
    return '%s * ' % str(d_term)

def idx_str(i, base_vars, con_dag):
    if i in base_vars:
        return 'v[%d]' % base_vars[i]
    # a number
    d = con_dag.node[i]

    if NodeAttr.number in d: