def pppexport(ppp, filename="pppdata"):
    """Export a proposition preserving partition to a C file

    Restrictions:
        - Each region is assumed to be a convex polytope.
        - If not, then only the first polytope inside the region is
          exported. Hence, the MPC may be infeasible

    @param ppp: tulip.abstract.prop2partition.PropPreservingPartition
    @param filename: a string of the export filename
    @rtype: None
    """
    numregions = len(ppp.regions)
    # generate the .c file
    f = open("sources/" + filename + ".c", "w")

    f.write('#include "' + filename + '.h"\n\n')

    f.write("idxint nRegion = " + str(numregions) + ";\n")
    f.write("Polytope* regions[" + str(numregions) + "];\n")

    for index in range(0, numregions):
        region = ppp.regions[index]
        p = region.list_poly[0]
        s = polytope2str(p, 'p' + str(index))
        f.write("/****Polytope " + str(index) + " ****/\n")
        f.write(s + "\n")

    f.write("void init_region()\n{\n")
    for index in range(0, numregions):
        f.write("   regions[" + str(index) + "]=create_poly(p" + str(index) +
                "k,p" + str(index) + "l,p" + str(index) + "A,p" + str(index) +
                "b,p" + str(index) + "center);\n")

    f.write("}\n\n")

    f.write("""\
void free_region()\n{
    int i;
    for (i=0;i<nRegion;i++)
    {
        free(regions[i]);
    }
}""")

    f.close()
def exportSysData(ctrl, pwa, initState, initRegion, filename="data"):
    """Export the data.c file

    @param ctrl: tulip.transys.machines.MealyMachine
    @param pwa: tulip.abstract,discretization.AbstractPwa
    @param initState: the initial state of the continuous system (numpy.array)
    @param initRegion: the initial region which the initial state belongs to
    @param filename: a string of the export filename
    @rtype: None
    """
    f = open("sources/" + filename + ".c", 'w')
    A = pwa.pwa.A
    B = pwa.pwa.B
    n = B.shape[0]
    # assuming full state observation, hence m = n
    m = B.shape[0]
    p = B.shape[1]
    f.write('#include "data.h"\n')
    f.write('idxint n = ' + str(n) + ';\n')
    f.write('idxint m = ' + str(m) + ';\n')
    f.write('idxint p = ' + str(p) + ';\n')
    f.write('pfloat A[] = {' + matrix2str(A) + '};\n')
    f.write('pfloat B[] = {' + matrix2str(B) + '};\n')
    f.write('idxint totalSteps = ' + str(pwa.disc_params['N']) + ';\n')
    f.write('Polytope *input_bound;\n')

    f.write(polytope2str(pwa.pwa.Uset, 'pu'))
    f.write('\nvoid init_input_bound(void)\n{\n')
    f.write('   input_bound=create_poly(puk,pul,puA,pub,pucenter);\n')
    f.write('}\n\n')

    f.write('void free_input_bound(void)\n{\n')
    f.write('		free(input_bound);\n')
    f.write('}\n')

    f.write('pfloat x0[] = {' + matrix2str(initState) + '};\n')
    f.write('idxint dRegion0 = ' + str(initRegion) + ';\n')
    f.close()