예제 #1
0
파일: other.py 프로젝트: ppurka/sagelib
    def _eval_(self, x):
        """
        EXAMPLES::

            sage: arg(3+i)
            arctan(1/3)
            sage: arg(-1+i)
            3/4*pi
            sage: arg(2+2*i)
            1/4*pi
            sage: arg(2+x)
            arg(x + 2)
            sage: arg(2.0+i+x)
            arg(x + 2.00000000000000 + 1.00000000000000*I)
            sage: arg(-3)
            pi
            sage: arg(3)
            0
            sage: arg(0)
            0
            sage: arg(sqrt(2)+i)
            arg(sqrt(2) + I)

        """
        if not isinstance(x,Expression):
            # x contains no variables
            if is_inexact(x):
                # inexact complex numbers, e.g. 2.0+i
                return self._evalf_(x, parent(x))
            else:
                # exact complex numbers, e.g. 2+i
                return arctan2(imag_part(x),real_part(x))
        else:
            # x contains variables, e.g. 2+i+y or 2.0+i+y
            # or x involves an expression such as sqrt(2)
            return None
예제 #2
0
    def _eval_(self, x):
        """
        EXAMPLES::

            sage: arg(3+i)
            arctan(1/3)
            sage: arg(-1+i)
            3/4*pi
            sage: arg(2+2*i)
            1/4*pi
            sage: arg(2+x)
            arg(x + 2)
            sage: arg(2.0+i+x)
            arg(x + 2.00000000000000 + 1.00000000000000*I)
            sage: arg(-3)
            pi
            sage: arg(3)
            0
            sage: arg(0)
            0
            sage: arg(sqrt(2)+i)
            arg(sqrt(2) + I)

        """
        if not isinstance(x, Expression):
            # x contains no variables
            if is_inexact(x):
                # inexact complex numbers, e.g. 2.0+i
                return self._evalf_(x, parent(x))
            else:
                # exact complex numbers, e.g. 2+i
                return arctan2(imag_part(x), real_part(x))
        else:
            # x contains variables, e.g. 2+i+y or 2.0+i+y
            # or x involves an expression such as sqrt(2)
            return None
예제 #3
0
파일: hypergraph.py 프로젝트: CETHop/sage
    def _latex_(self):
        r"""
        Return a TikZ representation of the hypergraph.

        EXAMPLES::

            sage: H = Hypergraph([{1,2,3},{2,3,4},{3,4,5},{4,5,6}]); H
            Hypergraph on 6 vertices containing 4 sets
            sage: view(H) # not tested

        With sets of size 4::

            sage: g = graphs.Grid2dGraph(5,5)
            sage: C4 = graphs.CycleGraph(4)
            sage: sets = Set(map(Set,list(g.subgraph_search_iterator(C4))))
            sage: H = Hypergraph(sets)
            sage: view(H) # not tested
        """
        from sage.rings.integer import Integer
        from sage.functions.trig import arctan2

        from sage.misc.misc import warn
        warn("\nThe hypergraph is drawn as a set of closed curves. The curve "
             "representing a set S go **THROUGH** the vertices contained "
             "in S.\n A vertex which is encircled by a curve but is not located "
             "on its boundary is **NOT** included in the corresponding set.\n"
             "\n"
             "The colors are picked for readability and have no other meaning.")

        latex.add_package_to_preamble_if_available("tikz")

        if not latex.has_file("tikz.sty"):
            raise RuntimeError("You must have TikZ installed in order "
                               "to draw a hypergraph.")

        domain = self.domain()
        pos = self._spring_layout()
        tex = "\\begin{tikzpicture}[scale=3]\n"

        colors = ["black", "red", "green", "blue", "cyan", "magenta", "yellow","pink","brown"]
        colored_sets = [(s,i) for i,S in enumerate(self.edge_coloring()) for s in S]

        # Prints each set with its color
        for s,i in colored_sets:
            current_color = colors[i%len(colors)]

            if len(s) == 2:
                s = list(s)
                tex += ("\\draw[color="+str(current_color)+","+
                        "line width=.1cm,opacity = .6] "+
                        str(pos[s[0]])+" -- "+str(pos[s[1]])+";\n")
                continue

            tex += ("\\draw[color="+str(current_color)+","
                    "line width=.1cm,opacity = .6,"
                    "line cap=round,"
                    "line join=round]"
                    "plot [smooth cycle,tension=1] coordinates {")

            # Reorders the vertices of s according to their angle with the
            # "center", i.e. the vertex representing the set s
            cx,cy = pos[s]
            s = map(lambda x:pos[x],s)
            s = sorted(s, key = lambda (x,y) : arctan2(x-cx,y-cy))

            for x in s:
                tex += str(x)+" "
            tex += "};\n"

        # Prints each vertex
        for v in domain:
            tex += "\\draw node[fill,circle,scale=.5,label={90:$"+latex(v)+"$}] at "+str(pos[v])+" {};\n"

        tex += "\\end{tikzpicture}"
        return tex
예제 #4
0
    def _latex_(self):
        r"""
        Return a TikZ representation of the hypergraph.

        EXAMPLES::

            sage: H = Hypergraph([{1,2,3},{2,3,4},{3,4,5},{4,5,6}]); H
            Hypergraph on 6 vertices containing 4 sets
            sage: view(H) # not tested

        With sets of size 4::

            sage: g = graphs.Grid2dGraph(5,5)
            sage: C4 = graphs.CycleGraph(4)
            sage: sets = Set(map(Set,list(g.subgraph_search_iterator(C4))))
            sage: H = Hypergraph(sets)
            sage: view(H) # not tested
        """
        from sage.rings.integer import Integer
        from sage.functions.trig import arctan2

        from sage.misc.misc import warn
        warn(
            "\nThe hypergraph is drawn as a set of closed curves. The curve "
            "representing a set S go **THROUGH** the vertices contained "
            "in S.\n A vertex which is encircled by a curve but is not located "
            "on its boundary is **NOT** included in the corresponding set.\n"
            "\n"
            "The colors are picked for readability and have no other meaning.")

        latex.add_package_to_preamble_if_available("tikz")

        if not latex.has_file("tikz.sty"):
            raise RuntimeError("You must have TikZ installed in order "
                               "to draw a hypergraph.")

        domain = self.domain()
        pos = self._spring_layout()
        tex = "\\begin{tikzpicture}[scale=3]\n"

        colors = [
            "black", "red", "green", "blue", "cyan", "magenta", "yellow",
            "pink", "brown"
        ]
        colored_sets = [(s, i) for i, S in enumerate(self.edge_coloring())
                        for s in S]

        # Prints each set with its color
        for s, i in colored_sets:
            current_color = colors[i % len(colors)]

            if len(s) == 2:
                s = list(s)
                tex += ("\\draw[color=" + str(current_color) + "," +
                        "line width=.1cm,opacity = .6] " + str(pos[s[0]]) +
                        " -- " + str(pos[s[1]]) + ";\n")
                continue

            tex += ("\\draw[color=" + str(current_color) + ","
                    "line width=.1cm,opacity = .6,"
                    "line cap=round,"
                    "line join=round]"
                    "plot [smooth cycle,tension=1] coordinates {")

            # Reorders the vertices of s according to their angle with the
            # "center", i.e. the vertex representing the set s
            cx, cy = pos[s]
            s = map(lambda x: pos[x], s)
            s = sorted(s, key=lambda (x, y): arctan2(x - cx, y - cy))

            for x in s:
                tex += str(x) + " "
            tex += "};\n"

        # Prints each vertex
        for v in domain:
            tex += "\\draw node[fill,circle,scale=.5,label={90:$" + latex(
                v) + "$}] at " + str(pos[v]) + " {};\n"

        tex += "\\end{tikzpicture}"
        return tex