コード例 #1
0
ファイル: plot.py プロジェクト: cjiang/Mathics
 def apply(self, f, x, xstart, xstop, y, ystart, ystop, evaluation, options):
     'DensityPlot[f_, {x_Symbol, xstart_, xstop_}, {y_Symbol, ystart_, ystop_}, OptionsPattern[DensityPlot]]'
     
     x = x.get_name()
     y = y.get_name()
     
     color_function = self.get_option(options, 'ColorFunction', evaluation, pop=True)
     color_function_scaling = self.get_option(options, 'ColorFunctionScaling', evaluation, pop=True)
     
     color_function_min = color_function_max = None
     if color_function.get_name() == 'Automatic':
         color_function = String('LakeColors')
     if color_function.get_string_value():
         func = Expression('ColorData', color_function.get_string_value()).evaluate(evaluation)
         if func.has_form('ColorDataFunction', 4):
             color_function_min = func.leaves[2].leaves[0].get_real_value()
             color_function_max = func.leaves[2].leaves[1].get_real_value()
             color_function = Expression('Function', Expression(func.leaves[3], Expression('Slot', 1)))
         else:
             evaluation.message('DensityPlot', 'color', func)
             return
     if color_function.has_form('ColorDataFunction', 4):
         color_function_min = color_function.leaves[2].leaves[0].get_real_value()
         color_function_max = color_function.leaves[2].leaves[1].get_real_value()
         
     color_function_scaling = color_function_scaling.is_true()
         
     try:
         xstart, xstop, ystart, ystop = [value.to_number(n_evaluation=evaluation) for value in
             (xstart, xstop, ystart, ystop)]
     except NumberError, exc:
         expr = Expression('DensityPlot', f, Expression('List', x, xstart, xstop),
             Expression('List', y, ystart, ystop), *options_to_rules(options))
         evaluation.message('DensityPlot', 'plln', exc.value, expr)
         return
コード例 #2
0
    def apply(self, f, x, xstart, xstop, y, ystart, ystop, evaluation,
              options):
        'DensityPlot[f_, {x_Symbol, xstart_, xstop_}, {y_Symbol, ystart_, ystop_}, OptionsPattern[DensityPlot]]'

        x = x.get_name()
        y = y.get_name()

        color_function = self.get_option(options,
                                         'ColorFunction',
                                         evaluation,
                                         pop=True)
        color_function_scaling = self.get_option(options,
                                                 'ColorFunctionScaling',
                                                 evaluation,
                                                 pop=True)

        color_function_min = color_function_max = None
        if color_function.get_name() == 'Automatic':
            color_function = String('LakeColors')
        if color_function.get_string_value():
            func = Expression(
                'ColorData',
                color_function.get_string_value()).evaluate(evaluation)
            if func.has_form('ColorDataFunction', 4):
                color_function_min = func.leaves[2].leaves[0].get_real_value()
                color_function_max = func.leaves[2].leaves[1].get_real_value()
                color_function = Expression(
                    'Function',
                    Expression(func.leaves[3], Expression('Slot', 1)))
            else:
                evaluation.message('DensityPlot', 'color', func)
                return
        if color_function.has_form('ColorDataFunction', 4):
            color_function_min = color_function.leaves[2].leaves[
                0].get_real_value()
            color_function_max = color_function.leaves[2].leaves[
                1].get_real_value()

        color_function_scaling = color_function_scaling.is_true()

        try:
            xstart, xstop, ystart, ystop = [
                value.to_number(n_evaluation=evaluation)
                for value in (xstart, xstop, ystart, ystop)
            ]
        except NumberError, exc:
            expr = Expression('DensityPlot', f,
                              Expression('List', x, xstart, xstop),
                              Expression('List', y, ystart, ystop),
                              *options_to_rules(options))
            evaluation.message('DensityPlot', 'plln', exc.value, expr)
            return
コード例 #3
0
    def construct_graphics(self, triangles, mesh_points, v_min, v_max,
                           options, evaluation):
        mesh_option = self.get_option(options, 'Mesh', evaluation)
        mesh = mesh_option.to_python()

        color_function = self.get_option(
            options, 'ColorFunction', evaluation, pop=True)
        color_function_scaling = self.get_option(
            options, 'ColorFunctionScaling', evaluation, pop=True)

        color_function_min = color_function_max = None
        if color_function.get_name() == 'Automatic':
            color_function = String('LakeColors')
        if color_function.get_string_value():
            func = Expression(
                'ColorData',
                color_function.get_string_value()).evaluate(evaluation)
            if func.has_form('ColorDataFunction', 4):
                color_function_min = func.leaves[2].leaves[0].get_real_value()
                color_function_max = func.leaves[2].leaves[1].get_real_value()
                color_function = Expression('Function', Expression(
                    func.leaves[3], Expression('Slot', 1)))
            else:
                evaluation.message('DensityPlot', 'color', func)
                return
        if color_function.has_form('ColorDataFunction', 4):
            color_function_min = \
                color_function.leaves[2].leaves[0].get_real_value()
            color_function_max = \
                color_function.leaves[2].leaves[1].get_real_value()

        color_function_scaling = color_function_scaling.is_true()
        v_range = v_max - v_min

        if v_range == 0:
            v_range = 1

        if color_function.has_form('ColorDataFunction', 4):
            color_func = color_function.leaves[3]
        else:
            color_func = color_function
        if (color_function_scaling and      # noqa
            color_function_min is not None and
            color_function_max is not None):
            color_function_range = color_function_max - color_function_min

        colors = {}

        def eval_color(x, y, v):
            v_scaled = (v - v_min) / v_range
            if (color_function_scaling and      # noqa
                color_function_min is not None and
                color_function_max is not None):
                v_color_scaled = color_function_min + \
                    v_scaled * color_function_range
            else:
                v_color_scaled = v

            # Calculate and store 100 different shades max.
            v_lookup = int(v_scaled * 100 + 0.5)

            value = colors.get(v_lookup)
            if value is None:
                value = Expression(color_func, Real(v_color_scaled))
                value = value.evaluate(evaluation)
                colors[v_lookup] = value
            return value

        points = []
        vertex_colors = []
        graphics = []
        for p in triangles:
            points.append(
                Expression('List', *(Expression('List', *x[:2]) for x in p)))
            vertex_colors.append(
                Expression('List', *(eval_color(*x) for x in p)))

        graphics.append(Expression(
            'Polygon', Expression('List', *points),
            Expression('Rule', Symbol('VertexColors'),
                       Expression('List', *vertex_colors))))

        if mesh == 'Full':
            for xi in range(len(mesh_points)):
                line = []
                for yi in range(len(mesh_points[xi])):
                    line.append(Expression('List', mesh_points[xi][yi][0],
                                mesh_points[xi][yi][1]))
                graphics.append(Expression('Line', Expression('List', *line)))
        elif mesh == 'All':
            for p in triangles:
                graphics.append(Expression(
                    'Line',
                    Expression('List', *(from_python(x[:2]) for x in p))))
        return graphics
コード例 #4
0
ファイル: plot.py プロジェクト: rnestler/Mathics
    def construct_graphics(self, triangles, mesh_points, v_min, v_max, options, evaluation):
        mesh_option = self.get_option(options, "Mesh", evaluation)
        mesh = mesh_option.to_python()

        color_function = self.get_option(options, "ColorFunction", evaluation, pop=True)
        color_function_scaling = self.get_option(options, "ColorFunctionScaling", evaluation, pop=True)

        color_function_min = color_function_max = None
        if color_function.get_name() == "Automatic":
            color_function = String("LakeColors")
        if color_function.get_string_value():
            func = Expression("ColorData", color_function.get_string_value()).evaluate(evaluation)
            if func.has_form("ColorDataFunction", 4):
                color_function_min = func.leaves[2].leaves[0].get_real_value()
                color_function_max = func.leaves[2].leaves[1].get_real_value()
                color_function = Expression("Function", Expression(func.leaves[3], Expression("Slot", 1)))
            else:
                evaluation.message("DensityPlot", "color", func)
                return
        if color_function.has_form("ColorDataFunction", 4):
            color_function_min = color_function.leaves[2].leaves[0].get_real_value()
            color_function_max = color_function.leaves[2].leaves[1].get_real_value()

        color_function_scaling = color_function_scaling.is_true()
        v_range = v_max - v_min

        if v_range == 0:
            v_range = 1

        if color_function.has_form("ColorDataFunction", 4):
            color_func = color_function.leaves[3]
        else:
            color_func = color_function
        if color_function_scaling and color_function_min is not None and color_function_max is not None:
            color_function_range = color_function_max - color_function_min

        colors = {}

        def eval_color(x, y, v):
            v_scaled = (v - v_min) / v_range
            if color_function_scaling and color_function_min is not None and color_function_max is not None:
                v_color_scaled = color_function_min + v_scaled * color_function_range
            else:
                v_color_scaled = v
            v_lookup = int(v_scaled * 100 + 0.5)  # calculate and store 100 different shades max.
            value = colors.get(v_lookup)
            if value is None:
                value = Expression(color_func, Real(v_color_scaled))
                value = value.evaluate(evaluation)
                colors[v_lookup] = value
            return value

        points = []
        vertex_colors = []
        graphics = []
        for p1, p2, p3 in triangles:
            c1, c2, c3 = eval_color(*p1), eval_color(*p2), eval_color(*p3)
            points.append(
                Expression(
                    "List", Expression("List", *p1[:2]), Expression("List", *p2[:2]), Expression("List", *p3[:2])
                )
            )
            vertex_colors.append(Expression("List", c1, c2, c3))

        graphics.append(
            Expression(
                "Polygon",
                Expression("List", *points),
                Expression("Rule", Symbol("VertexColors"), Expression("List", *vertex_colors)),
            )
        )

        if mesh == "Full":
            for xi in range(len(mesh_points)):
                line = []
                for yi in range(len(mesh_points[xi])):
                    line.append(Expression("List", mesh_points[xi][yi][0], mesh_points[xi][yi][1]))
                graphics.append(Expression("Line", Expression("List", *line)))
        elif mesh == "All":
            for p1, p2, p3 in triangles:
                line = [from_python(p1[:2]), from_python(p2[:2]), from_python(p3[:2])]
                graphics.append(Expression("Line", Expression("List", *line)))
        return graphics