def code2latex(G, partitions, formdata): "TODO: Document me" bfn = formdata.argument_names V, E = G Vout = extract_outgoing_vertex_connections(G) # Sort dependency sets in a sensible way (preclude to a good # quadrature code generator) deplistlist = dependency_sorting(list(partitions.keys()), len(bfn)) def format_v(i): return "s_{%d}" % i pieces = [] for deplist in deplistlist: for dep in deplist: lines = [] for iv in partitions[dep]: v = V[iv] vout = Vout[iv] vl = format_v(iv) args = ", ".join(format_v(i) for i in vout) if args: el = r"{\mbox{%s}}(%s)" % (v._ufl_class_.__name__, args) else: # terminal el = r"{\mbox{%s}}" % (repr(v),) lines.append((vl, "= " + el)) pieces.extend(("\n", deps2latex(dep), align(lines))) # Add final variable representing integrand vl = format_v(len(V)-1) pieces.append("\n") pieces.append("Variable representing integrand: %s" % vl) # Could also return list of (title, body) parts for subsections if # wanted body = "\n".join(pieces) return body
def form2latex(form, formdata): formname = formdata.name argument_names = formdata.argument_names coefficient_names = formdata.coefficient_names # List of sections to make latex document from sections = [] # Define elements lines = [] for i, f in enumerate(formdata.original_arguments): lines.append(r"\mathcal{P}_{%d} = \{%s\} " % (i, element2latex(f.ufl_element()))) for i, f in enumerate(formdata.original_coefficients): lines.append(r"\mathcal{Q}_{%d} = \{%s\} " % (i, element2latex(f.ufl_element()))) if lines: sections.append(("Finite elements", align(lines))) # Define function spaces lines = [] for i, f in enumerate(formdata.original_arguments): lines.append("V_h^{%d} = \\{v : v \\vert_K \\in \\mathcal{P}_{%d}(K) \\quad \\forall K \\in \\mathcal{T}\\} " % (i, i)) for i, f in enumerate(formdata.original_coefficients): lines.append("W_h^{%d} = \\{v : v \\vert_K \\in \\mathcal{Q}_{%d}(K) \\quad \\forall K \\in \\mathcal{T}\\} " % (i, i)) if lines: sections.append(("Function spaces", align(lines))) # Define arguments and coefficients lines = [] for f in formdata.original_arguments: i = f.number() p = f.part() lines.append("%s = %s \\in V_h^{%d} " % (argument_names[(i, p)], bfname(i, p), i)) # FIXME: Handle part in V_h for i, f in enumerate(formdata.original_coefficients): lines.append("%s = %s \\in W_h^{%d} " % (coefficient_names[i], cfname(i), i)) if lines: sections.append(("Form arguments", align(lines))) # TODO: Wrap ListTensors, ComponentTensor and Conditionals in # expression as variables before transformation # Define variables handled_variables = set() integrals = form.integrals() lines = [] for itg in integrals: variables = _extract_variables(itg.integrand()) for v in variables: l = v._label # noqa: E741 if l not in handled_variables: handled_variables.add(l) exprlatex = expression2latex(v._expression, formdata.argument_names, formdata.coefficient_names) lines.append(("s_{%d}" % l._count, "= %s" % exprlatex)) if lines: sections.append(("Variables", align(lines))) # Join form arguments for signature "a(...) =" b = ", ".join(formdata.argument_names) c = ", ".join(formdata.coefficient_names) arguments = "; ".join((b, c)) signature = "%s(%s) = " % (formname, arguments, ) # Define form as sum of integrals lines = [] a = signature p = "" for itg in integrals: # TODO: Get list of expression strings instead of single # expression! integrand_string = expression2latex(itg.integrand(), formdata.argument_names, formdata.coefficient_names) integral_type = itg.integral_type() dstr = domain_strings[integral_type] # domain = itg.ufl_domain() # TODO: Render domain description subdomain_id = itg.subdomain_id() if isinstance(subdomain_id, int): dstr += "_{%d}" % subdomain_id elif subdomain_id == "everywhere": pass elif subdomain_id == "otherwise": dstr += "_{\text{oth}}" elif isinstance(subdomain_id, tuple): dstr += "_{%s}" % subdomain_id b = p + "\\int_{%s}" % (dstr,) dxstr = ufl.measure.integral_type_to_measure_name[integral_type] c = "{ %s } \\,%s" % (integrand_string, dxstr) lines.append((a, b, c)) a = "{}" p = "{}+ " sections.append(("Form", align(lines))) return sections