Example #1
0
 def generate_method_table(self, println):
     writer = cg.CppCodeWriter(println)
     writer.println('static')
     writer.println('PyMethodDef meth_%s[] = {' % cg.mangle(self.fullname))
     with writer.indent():
         fmt = '{ "%(name)s", (PyCFunction)%(func)s, METH_VARARGS, NULL },'
         for meth in self.methods:
             name = meth.name
             func = meth.c_name
             writer.println(fmt % locals())
         for enumkind in self.enums:
             for enum in enumkind.value_names:
                 name = enum
                 func = enumkind.c_name(enum)
                 writer.println(fmt % locals())
         for attr in self.attrs:
             # getter
             name = attr.getter_name
             func = attr.getter_c_name
             writer.println(fmt % locals())
             # setter
             name = attr.setter_name
             func = attr.setter_c_name
             writer.println(fmt % locals())
         writer.println('{ NULL },')
     writer.println('};')
     writer.println()
Example #2
0
 def generate_method_table(self, println):
     writer = cg.CppCodeWriter(println)
     writer.println('static')
     writer.println('PyMethodDef meth_%s[] = {' % cg.mangle(self.fullname))
     with writer.indent():
         fmt = '{ "%(name)s", (PyCFunction)%(func)s, METH_VARARGS, NULL },'
         for meth in self.methods:
             name = meth.name
             func = meth.c_name
             writer.println(fmt % locals())
         for enumkind in self.enums:
             for enum in enumkind.value_names:
                 name = enum
                 func = enumkind.c_name(enum)
                 writer.println(fmt % locals())
         for attr in self.attrs:
             # getter
             name = attr.getter_name
             func = attr.getter_c_name
             writer.println(fmt % locals())
             # setter
             name = attr.setter_name
             func = attr.setter_c_name
             writer.println(fmt % locals())
         writer.println('{ NULL },')
     writer.println('};')
     writer.println()
Example #3
0
 def generate_submodule_table(self, println, extras=()):
     writer = cg.CppCodeWriter(println)
     writer.println('static')
     name = cg.mangle(self.fullname)
     writer.println('SubModuleEntry submodule_%(name)s[] = {' % locals())
     with writer.indent():
         for cls in self.classes:
             name = cls.name
             table = cg.mangle(cls.fullname)
             writer.println('{ "%(name)s", meth_%(table)s, NULL },' %
                            locals())
         for ns in self.namespaces:
             name = ns.localname
             table = cg.mangle(ns.fullname)
             fmt = '{ "%(name)s", meth_%(table)s, submodule_%(table)s },'
             writer.println(fmt % locals())
         for name, table in extras:
             writer.println('{ "%(name)s", %(table)s, NULL },' % locals())
         writer.println('{ NULL }')
     writer.println('};')
     writer.println('')
Example #4
0
 def generate_submodule_table(self, println, extras=()):
     writer = cg.CppCodeWriter(println)
     writer.println('static')
     name = cg.mangle(self.fullname)
     writer.println('SubModuleEntry submodule_%(name)s[] = {' % locals())
     with writer.indent():
         for cls in self.classes:
             name = cls.name
             table = cg.mangle(cls.fullname)
             writer.println('{ "%(name)s", meth_%(table)s, NULL },' %
                            locals())
         for ns in self.namespaces:
             name = ns.localname
             table = cg.mangle(ns.fullname)
             fmt = '{ "%(name)s", meth_%(table)s, submodule_%(table)s },'
             writer.println(fmt % locals())
         for name, table in extras:
             writer.println('{ "%(name)s", %(table)s, NULL },' % locals())
         writer.println('{ NULL }')
     writer.println('};')
     writer.println('')
Example #5
0
 def setter_c_name(self):
     return cg.mangle('%s_set' % self.fullname)
Example #6
0
 def c_name(self):
     return cg.mangle("%s_%s" % (self.parent, self.name))
Example #7
0
 def c_name(self, enum):
     return cg.mangle("%s_%s_%s" % (self.parent, self.name, enum))
Example #8
0
    def solver(**kwargs):
        if all(valid_args(e) for e in kwargs.values()):
            # args contains *actual* dimensions (for variables) and parameter values
            args = mangle(kwargs)
            
            # only care about the ones that are used
            # args = dict( (k,v) for k,v in mangled.iteritems() if k in set(used) )
            
            # make sure all keys are subset of needed variable list
            if variable_set.issubset(args) and set(params).issubset(args):
                # first, make sure all parameter arguments are sparse matrices
                for k in set(params):
                    if isinstance(args[k], float) or isinstance(args[k], int):
                        args[k] = o.spmatrix(args[k],[0],[0])
                    elif isinstance(args[k], o.matrix):
                        args[k] = o.sparse(args[k])
                                
                # get the size lookup table using actual dimensions
                sizes = codegen.get_variable_sizes(args)
                # build the location of the start indices from the size table
                start_idxs, cum = {}, 0
                for k,v in sizes.iteritems():
                    start_idxs[k] = cum
                    cum += v
                                
                # add parameter sizes to the dictionary (!!!hack)
                for k in codegen.parameters:
                    if k in set(args):
                        sizes[k] = args[k].size[0]
                
                # get objective vector
                c_obj = o.matrix(0, (cum,1), 'd')
                for k,v in c.iteritems():
                    # we ignore constant coefficients
                    if k != '1':
                        idx = start_idxs[k]
                        row_height = sizes[k]
                        c_obj[idx:idx+row_height] = eval_matrix_coeff(v, args, row_height, 1, transpose_output=True)
                
                # get matrices
                A_mat, b_mat = build_matrix(A, b, b_height, args, sizes, start_idxs, cum)
                Gl_mat, hl_vec = build_matrix(Gl, hl, hl_height, args, sizes, start_idxs, cum)
                Gq_mats, hq_vecs = [], []
                
                # matrices in SOC
                for G, h, height in zip(Gq, hq, hq_height):
                    mat, vec = build_matrix(G, h, height, args, sizes, start_idxs, cum)
                    # ensure that sizes agree
                    oldsize = mat.size
                    mat.size = (oldsize[0], cum)
                    Gq_mats.append(mat)
                    hq_vecs.append(vec)
                
                for G, h, height in zip(Gblk, hblk, hblk_blocks):
                    mats, vecs = build_block_matrices(G, h, height, args, sizes, start_idxs, cum)
                    # ensure that sizes agree
                    for m in mats:
                        oldsize = m.size
                        m.size = (oldsize[0], cum)
                    
                    Gq_mats += mats
                    hq_vecs += vecs

                sol = solvers.socp(c_obj, Gl_mat, hl_vec, Gq_mats, hq_vecs, A_mat, b_mat)
                # print sol
                # # Gl_mat, hl_vec
                # 
                # print sizes
                # print c_obj
                # print A_mat
                # print b_mat
                # print Gl_mat
                # print hl_vec
                # 
                #     
                # print c_obj

                solution = recover_variables(sol['x'], start_idxs, sizes, variable_set)
                return solution

            else:
                raise Exception("Not all variable dimensions or parameters have been specified.")
        else:
            raise Exception("Expected integer arguments for variables and matrix or float arguments for params.") 
Example #9
0
 def setter_c_name(self):
     return cg.mangle('%s_set' % self.fullname)
Example #10
0
 def c_name(self):
     return cg.mangle("%s_%s" % (self.parent, self.name))
Example #11
0
 def c_name(self, enum):
     return cg.mangle("%s_%s_%s" % (self.parent, self.name, enum))