示例#1
0
def _append_args_accessors_matrix(method, files, objlabel):
    """
    add all accessors for arguments from method.args_names to the cpp files for
    object with name objlabel. return Matrix<double, n, m>
    """
    title = "\n\n// Acessors to Arguments, return Matrix<double, n, m>\n"
    files['h']['public'] += title
    files['cpp']['public'] += title
    for name in method.args_names:
        arg = getattr(method, name + '_expr')
        dim0 = len(arg)
        if dim0 == 0:
            dim1 = 0
        else:
            dim1 = 1
        files['h']['public'] += \
            '\n{0} {1}() const;'.format(matrix_type(dim0, dim1), name)
        files['cpp']['public'] += \
            "\n{0} {1}::{2}() const".format(matrix_type(dim0, dim1),
                                            objlabel, name) + ' {'
        files['cpp']['public'] += \
            indent('\n{0} m;'.format(matrix_type(dim0, dim1)))
        for i, symb in enumerate(arg):
            files['cpp']['public'] += \
                indent("\nm({0}, 0) = *{1};".format(i, str(symb)))
        files['cpp']['public'] += indent("\nreturn m;")
        files['cpp']['public'] += "\n}"
示例#2
0
def append_init(objlabel, files):
    title = "\n\n// Initialization\n\n"
    files['h']['private'] += "{0}void {1}();".format(title, 'init')
    files['cpp']['private'] += title
    string = 'void {0}::{1}()'.format(objlabel, 'init') + '{\n'
    string += indent(files['cpp']['data'])
    string += indent(files['cpp']['init'])
    string += '\n};'
    files['cpp']['private'] += string
示例#3
0
def _str_mat_func_get_vector(method, name, objlabel):
    mat = sympy.Matrix(getattr(method, name + '_expr'))
    mtype = 'vector<double>'
    get_h = '\n{0} {1}_vector() const;'.format(mtype, name)
    get_cpp = '\n{0} {1}::{2}_vector() const'.format(mtype, objlabel, name) + \
        ' {'
    dim = mat.shape[0]
    get_cpp += indent("\nvector<double> v = vector<double>({0});".format(dim))
    for i in range(dim):
        get_cpp += indent("\nv[{0}] = _{1}({0}, 0);".format(i, name))
    get_cpp += indent("\nreturn v;") + "\n}"
    return get_h, get_cpp
示例#4
0
def _str_mat_op_get_vector(nums, name, objlabel):
    mat = getattr(nums, name)()
    if len(mat.shape) == 1:
        mat = numpy.matrix(mat).T
    mtype = 'vector<double>'
    get_h = '\n{0} {1}_vector() const;'.format(mtype, name)
    get_cpp = \
        '\n{0} {1}::{2}_vector() const'.format(mtype, objlabel, name) + ' {'
    dim = mat.shape[0]
    get_cpp += indent("\nvector<double> v = vector<double>({0});".format(dim))
    for i in range(dim):
        get_cpp += indent("\nv[{0}] = _{1}({0}, 0);".format(i, name))
    get_cpp += indent("\nreturn v;") + "\n}"
    return get_h, get_cpp
示例#5
0
def append_constructor(objlabel, files):
    title = "\n\n// Default Constructor\n\n"
    files['h']['public'] += "{0}{1}();".format(title, objlabel)
    files['cpp']['public'] += title
    string = '{0}::{0}()'.format(objlabel)
    string += '{\n' + indent('init();') + '\n};'
    files['cpp']['public'] += string
示例#6
0
def _str_op_update(nums, name, objlabel):
    op = getattr(nums.method, name + '_op')
    update_h = '\nvoid {0}_update();'.format(name)
    update_cpp = '\nvoid {0}::{1}_update()'.format(objlabel, name) + '{'
    update_cpp += '\n' + indent('_{0} = {1};'.format(name, op2cpp(op)))
    update_cpp += '\n};'
    return update_h, update_cpp
示例#7
0
def _str_mat_func_get(method, name, objlabel):
    mat = sympy.Matrix(getattr(method, name + '_expr'))
    mtype = matrix_type(mat.shape[0], mat.shape[1])
    get_h = '\n{0} {1}() const;'.format(mtype, name)
    get_cpp = '\n{0} {1}::{2}() const'.format(mtype, objlabel, name)
    get_cpp += ' {\n'
    get_cpp += indent("return _{0};".format(name))
    get_cpp += '\n}'
    return get_h, get_cpp
示例#8
0
def append_constructor_init_matrix(method, objlabel, files):
    title = "\n\n// Constructor with matrix state initalization\n\n"
    mtype = matrix_type(method.core.dims.x(), 1)
    files['h']['public'] += "{0}{1}({2} &);".format(title, objlabel, mtype)
    files['cpp']['public'] += title
    string = '{0}::{0}({1} & x0)'.format(objlabel, mtype) + '{\n'
    string += "set_x(x0);"
    string += '\n' + indent('init();') + '\n};'
    files['cpp']['public'] += string
示例#9
0
def iterate(method, actions, res_label, step_label):
    string = ''
    string += "\nunsigned int iter_{0} = 0;".format(res_label)
    string += "\n_{0} = 1;".format(step_label)
    it = "(iter_{0}<{1})".format(res_label, method.config['maxit'])
    res = "({0}()>{1})".format(res_label, method.config['eps'])
    step = "({0}()>{1})".format(step_label, method.config['eps'])
    string += \
        "\nwhile ({0} & {1} & {2})".format(it, res, step) + '{'
    string += \
        indent(execs(actions) + 'iter_{0} += 1;'.format(res_label)) + '\n}'
    return string
示例#10
0
def append_update(method, files, objlabel):
    string_h = ""
    string_cpp = ""
    str_u_cpp = "vector<double> & u_vec"
    str_u_h = "vector<double> &"
    str_coma = ', '
    str_p_cpp = "vector<double> & p_vec"
    str_p_h = "vector<double> &"
    string_h += "\nvoid update(" + str_u_h + str_coma + str_p_h + ");"
    string_cpp += """\n
void """ + objlabel + '::' + """update(""" + str_u_cpp + str_coma +\
        str_p_cpp + """){\n"""
    string_cpp += '\n' + indent('set_u(u_vec);')
    string_cpp += '\n' + indent('set_p(p_vec);')
    for action in method.update_actions:
        if action[0] == 'exec':
            string_cpp += '\n' + indent(execs(action[1]))
        elif action[0] == 'iter':
            string_cpp += '\n' + indent(iterate(method, *action[1]))
    string_cpp += "\n}"
    files['h']['public'] += string_h
    files['cpp']['public'] += string_cpp
示例#11
0
def _str_mat_op_get(nums, name, objlabel):
    mat = getattr(nums, name)()
    if len(mat.shape) == 1:
        mat = numpy.matrix(mat).T
    if not bool(numpy.prod(mat.shape)):
        shape = (0, 0)
    else:
        shape = mat.shape
    mtype = matrix_type(shape[0], shape[1])
    get_h = '\n{0} {1}() const;'.format(mtype, name)
    get_cpp = \
        '\n{0} {1}::{2}() const'.format(mtype, objlabel, name)
    get_cpp += ' {\n' + indent('return _{0};'.format(name)) + '\n}'
    return get_h, get_cpp
示例#12
0
def append_constructor_init_vector(objlabel, files):
    title = "\n\n// Constructor with vector state initalization\n\n"
    files['h']['public'] += "{0}{1}(vector<double> &);".format(title, objlabel)
    files['cpp']['public'] += title
    string = '{0}::{0}(vector<double> & x0)'.format(objlabel) + '{\n'
    string += """
    if (x().size() == x0.size()) {
        set_x(x0);
    }
    else {
        cerr << "Size of x0 does not match size of x" << endl;
        exit(1);
    }"""
    string += '\n' + indent('init();') + '\n};'
    files['cpp']['public'] += string
示例#13
0
def _append_args_accessors_vector(method, files, objlabel):
    """
    add all accessors for arguments from method.args_names to the cpp files for
    object with name objlabel. return vector<double>
    """
    title = "\n\n// Acessors to Arguments, return vector<double>\n"
    files['h']['public'] += title
    files['cpp']['public'] += title
    for name in method.args_names:
        arg = getattr(method, name + '_expr')
        dim = len(arg)
        files['h']['public'] += \
            '\nvector<double> {0}_vector() const;'.format(name)
        files['cpp']['public'] += \
            "\n\nvector<double> " + \
            "{0}::{1}".format(objlabel, name) + \
            "_vector() const {"
        files['cpp']['public'] += \
            indent("\nvector<double> v = vector<double>({0});".format(dim))
        for i, symb in enumerate(arg):
            files['cpp']['public'] += \
                indent("\nv[{0}] = *{1};".format(i, str(symb)))
        files['cpp']['public'] += indent("\nreturn v;")
        files['cpp']['public'] += "\n}"
示例#14
0
def _append_args_mutators_vectors(method, files, objlabel):
    """
    add all mutators for arguments from method.args_names to the cpp files for
    object with name objlabel. input is vector<double>
    """
    title = "\n\n// Mutators for Arguments, type = vector<double>\n"
    files['h']['public'] += title
    files['cpp']['public'] += title
    for name in method.args_names:
        arg = getattr(method, name + '_expr')
        files['h']['public'] += \
            '\nvoid set_{0}(vector<double> &);'.format(name)
        files['cpp']['public'] += \
            "\nvoid {0}::set_{1}(vector<double> & v)".format(objlabel, name) +\
            " {"
        for i, symb in enumerate(arg):
            files['cpp']['public'] += "\n" + \
                indent("*{0} = v[{1}];".format(str(symb), i))
        files['cpp']['public'] += "\n}"
示例#15
0
def _append_args_mutators_matrix(method, files, objlabel):
    """
    add all mutators for arguments from method.args_names to the cpp files for
    object with name objlabel. input is Matrix<double, n, m>
    """
    title = "\n\n// Mutators for Arguments, type = Matrix<double, n, m>\n"
    files['h']['public'] += title
    files['cpp']['public'] += title
    for name in method.args_names:
        arg = getattr(method, name + '_expr')
        dim0 = len(arg)
        if dim0 == 0:
            dim1 = 0
        else:
            dim1 = 1
        files['h']['public'] += '\nvoid set_' + name + \
            '(Matrix<double, {0}, {1}> &);'.format(dim0, dim1)
        files['cpp']['public'] += \
            "\nvoid {0}::set_{1}".format(objlabel, name) + \
            "(Matrix<double, {0}, {1}> & m)".format(dim0, dim1) + " {"
        for i, symb in enumerate(arg):
            files['cpp']['public'] += \
                '\n'+indent("*{0} = m({1}, 0);".format(str(symb), i))
        files['cpp']['public'] += "\n}"
示例#16
0
def _str_scal_func_get(name, objlabel):
    get_h = '\ndouble {0}();'.format(name)
    get_cpp = \
        '\ndouble {0}::{1}() const '.format(objlabel, name) + '{\n'
    get_cpp += indent('return _{0};'.format(name)) + '\n}'
    return get_h, get_cpp
示例#17
0
def numcore2cpp(nums, objlabel=None, path=None, eigen_path=None):
    if objlabel is None:
        objlabel = 'phscore'.upper()
    else:
        objlabel = objlabel.upper()
    if path is None:
        path = os.getcwd() + os.sep + objlabel
    if not os.path.exists(path):
        os.makedirs(path)
    if eigen_path is None:
        eigen_path = config_eigen_path
    files = {}
    exts = ['cpp', 'h']
    for name in exts:
        files.update({
            name: {
                'public': '',
                'private': '',
                'init': '',
                'data': '',
                'starting': str_preamble(objlabel),
                'closing': ''
            }
        })
    files['h']['starting'] += '\n'
    files['h']['starting'] += "\n#ifndef {0}_H".format(objlabel)
    files['h']['starting'] += "\n#define {0}_H".format(objlabel)
    h, cpp = _str_includes(formatPath(eigen_path))
    files['h']['starting'] += h
    files['cpp']['starting'] += cpp
    files['h']['starting'] += _str_namespaces()
    files['h']['starting'] += "\n\nclass {0} ".format(objlabel) + "{"
    files['h']['closing'] += "\n}" + ";\n\n#endif /* {0}_H */\n".format(
        objlabel)
    files['h']['private'] += '\nprivate:'
    files['h']['public'] += '\npublic:'
    append_parameters(nums.method, files)
    append_update(nums.method, files, objlabel)
    append_args(nums.method, files, objlabel)
    append_funcs(nums, files, objlabel)
    append_ops(nums, files, objlabel)
    append_init(objlabel, files)
    append_constructor(objlabel, files)
    append_constructor_init_vector(objlabel, files)
    append_constructor_init_matrix(nums.method, objlabel, files)
    append_destructuor(objlabel, files)
    for e in exts:
        string = files[e]['starting']
        string += '\n\n// PUBLIC'
        string += indent(files[e]['public'])
        string += '\n\n\n// PRIVATE'
        string += indent(files[e]['private'])
        string += files[e]['closing']
        filename = path + os.sep + 'core.{0}'.format(e)
        _file = open(filename, 'w')
        _file.write(string)
        _file.close()
    data_files = data(nums.method.core.subs, objlabel)
    for e in exts:
        string = data_files[e]
        filename = path + os.sep + 'data.{0}'.format(e)
        _file = open(filename, 'w')
        _file.write(string)
        _file.close()