Example #1
0
    def quick_pattern_test(self, candidate, test, evaluation):
        if test == "System`NumberQ":
            return isinstance(candidate, Number)
        elif test == "System`NumericQ":
            if isinstance(candidate, Number):
                return True
            # Otherwise, follow the standard evaluation
        elif test == "System`RealNumberQ":
            if isinstance(candidate, (Integer, Rational, Real)):
                return True
            candidate = Expression(SymbolN, candidate).evaluate(evaluation)
            return isinstance(candidate, Real)
            # pass
        elif test == "System`Positive":
            if isinstance(candidate, (Integer, Rational, Real)):
                return candidate.value > 0
            return False
            # pass
        elif test == "System`NonPositive":
            if isinstance(candidate, (Integer, Rational, Real)):
                return candidate.value <= 0
            return False
            # pass
        elif test == "System`Negative":
            if isinstance(candidate, (Integer, Rational, Real)):
                return candidate.value < 0
            return False
            # pass
        elif test == "System`NonNegative":
            if isinstance(candidate, (Integer, Rational, Real)):
                return candidate.value >= 0
            return False
            # pass
        elif test == "System`NegativePowerQ":
            return (candidate.has_form("Power", 2)
                    and isinstance(candidate.leaves[1],
                                   (Integer, Rational, Real))
                    and candidate.leaves[1].value < 0)
        elif test == "System`NotNegativePowerQ":
            return not (candidate.has_form("Power", 2) and isinstance(
                candidate.leaves[1],
                (Integer, Rational, Real)) and candidate.leaves[1].value < 0)
        else:
            from mathics.builtin.base import Test

            builtin = None
            builtin = evaluation.definitions.get_definition(test)
            if builtin:
                builtin = builtin.builtin
            if builtin is not None and isinstance(builtin, Test):
                return builtin.test(candidate)
        return None
Example #2
0
 def get_msg_list(expr):
     if expr.has_form('MessageName', 2):
         expr = Expression('List', expr)
     if expr.get_name() == 'All':
         all = True
         messages = []
     elif expr.get_name() == 'None':
         all = False
         messages = []
     elif expr.has_form('List', None):
         all = False
         messages = []
         for item in expr.leaves:
             if item.has_form('MessageName', 2):
                 symbol = item.leaves[0].get_name()
                 tag = item.leaves[1].get_string_value()
                 if symbol and tag:
                     messages.append((symbol, tag))
                 else:
                     raise ValueError
             else:
                 raise ValueError
     else:
         raise ValueError
     return all, messages
Example #3
0
 def get_msg_list(expr):
     if expr.has_form('MessageName', 2):
         expr = Expression('List', expr)
     if expr.get_name() == 'All':
         all = True
         messages = []
     elif expr.get_name() == 'None':
         all = False
         messages = []
     elif expr.has_form('List', None):
         all = False
         messages = []
         for item in expr.leaves:
             if item.has_form('MessageName', 2):
                 symbol = item.leaves[0].get_name()
                 tag = item.leaves[1].get_string_value()
                 if symbol and tag:
                     messages.append((symbol, tag))
                 else:
                     raise ValueError
             else:
                 raise ValueError
     else:
         raise ValueError
     return all, messages
Example #4
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
Example #5
0
 def post_parse(self, expression):
     leaves = [leaf.post_parse() for leaf in expression.leaves]
     expression = Expression(expression.head.post_parse(), *leaves)
     if expression.has_form('Optional', 2) and expression.leaves[0].get_name():
         sub = expression.leaves[1]
         if sub.has_form(('Pattern', 'Optional'), 2):
             return Expression('Optional', Expression('Pattern', expression.leaves[0], sub.leaves[0]), sub.leaves[1])
         else:
             return Expression('Pattern', *[leaf.post_parse() for leaf in expression.leaves])
     else:
         return expression
Example #6
0
 def post_parse(self, expression):
     leaves = [leaf.post_parse() for leaf in expression.leaves]
     expression = Expression(expression.head.post_parse(), *leaves)
     if expression.has_form('Optional', 2) and expression.leaves[0].get_name():
         sub = expression.leaves[1]
         if sub.has_form(('Pattern', 'Optional'), 2):
             return Expression('Optional', Expression('Pattern', expression.leaves[0], sub.leaves[0]), sub.leaves[1])
         else:
             return Expression('Pattern', *[leaf.post_parse() for leaf in expression.leaves])
     else:
         return expression
Example #7
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
Example #8
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
Example #9
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 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