Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Mandelbrot and Julia sets (Cython helper)
This is the helper file providing functionality for mandel_julia.py.
AUTHORS:
- Ben Barros
"""
#***************************************************************************** # Copyright (C) 2017 BEN BARROS <bbarros@slu.edu> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from __future__ import absolute_import, division from cysignals.signals cimport sig_check
long max_iteration, long pixel_count, long level_sep, long color_num, base_color):
r""" Plots the Mandelbrot set in the complex plane for the map `Q_c(z) = z^2 + c`.
INPUT:
- ``x_center`` -- double, real part of the center point in the complex plane.
- ``y_center`` -- double, imaginary part of the center point in the complex plane.
- ``image_width`` -- double, width of the image in the complex plane.
- ``max_iteration`` -- long, maximum number of iterations the map `Q_c(z)` considered.
- ``pixel_count`` -- long, side length of image in number of pixels.
- ``level_sep`` -- long, number of iterations between each color level.
- ``color_num`` -- long, number of colors used to plot image.
- ``base_color`` -- list, RGB color used to determine the coloring of set.
OUTPUT:
24-bit RGB image of the Mandelbrot set in the complex plane
EXAMPLES:
Plot the Mandelbrot set with the center point `-1 + 0i`::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_mandelbrot_plot sage: fast_mandelbrot_plot(-1, 0, 4, 500, 600, 1, 20, [40, 40, 40]) # long time 600x600px 24-bit RGB image
We can focus on smaller parts of the set by adjusting image_width::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_mandelbrot_plot sage: fast_mandelbrot_plot(-1.11, 0.2283, 1/128, 2000, 500, 1, 500, [40, 100, 100]) # long time 500x500px 24-bit RGB image """
cdef long i, j, col, row, level, color_value, iteration cdef double k, x_corner, y_corner, step_size, x_coor, y_coor, new_x, new_y cdef M, pixel, color_list
# Make sure image_width is positive
# Initialize an image to the color black and access the pixels
# Take the given base color and create a list of evenly spaced # colors between the given base color and white. The number of # colors in the list depends on the variable color_num. # Convert Color to RGB list base_color = [int(k*255) for k in base_color]
# First, we determine the complex coordinates of the point in the top left # corner of the image. Then, we loop through each pixel in the image and # assign it complex coordinates relative to the image's top left corner.
# We compute the orbit of 0 under the map Q(z) = z^2 + c # until we either reach the maximum number of iterations # or find a point in the orbit with modulus greater than 2
# If the point escapes to infinity, assign the point a color # based on how fast it escapes. The more iterations it takes for # a point to escape to infinity, the lighter its color will be. # Otherwise, assume the point is in the Mandelbrot set and leave # it black. # Assign each point a level based on its number of iterations. # Assign the pixel a color based on it's level. If we run out # of colors, assign it the last color in the list. else:
cpdef fast_external_ray(double theta, long D=30, long S=10, long R=100, long pixel_count=500, double image_width=4, long prec=300): r""" Returns a list of points that approximate the external ray for a given angle.
INPUT:
- ``theta`` -- double, angle between 0 and 1 inclusive.
- ``D`` -- long (optional - default: ``25``) depth of the approximation. As ``D`` increases, the external ray gets closer to the boundary of the Mandelbrot set.
- ``S`` -- long (optional - default: ``10``) sharpness of the approximation. Adjusts the number of points used to approximate the external ray (number of points is equal to ``S*D``).
- ``R`` -- long (optional - default: ``100``) radial parameter. If ``R`` is sufficiently large, the external ray reaches enough close to infinity.
- ``pixel_count`` -- long (optional - default: ``500``) side length of image in number of pixels.
- ``image_width`` -- double (optional - default: ``4``) width of the image in the complex plane.
- ``prec`` -- long (optional - default: ``300``) specifies the bits of precision used by the Complex Field when using Newton's method to compute points on the external ray.
OUTPUT:
List of tuples of Real Interval Field Elements
EXAMPLES::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_external_ray sage: fast_external_ray(0,S=1,D=1) [(100.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000), (9.51254777713729174697578576623132297117784691109499464854806785133621315075854778426714908, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)]
::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_external_ray sage: fast_external_ray(1/3,S=1,D=1) [(-49.9999999999999786837179271969944238662719726562500000000000000000000000000000000000000000, 86.6025403784438765342201804742217063903808593750000000000000000000000000000000000000000000), (-5.50628047023173006234970878097113901879832542655926629309001652388544528575532346900138516, 8.64947510053972513843999918917106032664030380426885745306040284140385975750462108180377187)]
::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_external_ray sage: fast_external_ray(0.75234,S=1,D=1) [(1.47021239172637052661229972727596759796142578125000000000000000000000000000000000000000000, -99.9891917935294287644865107722580432891845703125000000000000000000000000000000000000000000), (-0.352790406744857508500937144524776555433184352559852962308757189778284058275081335121601384, -9.98646630765023514178761177926164047797465369576787921409326037870837930920646860774032363)] """
cdef: c_0, r_m, t_m, temp_c, C_k, D_k, old_c, x, y, dist int k, j, t double difference, m
# initialize list with c_0
# Loop through each subinterval and approximate point on external ray.
# Repeat Newton's method until points are close together. # Recursive formula for iterates of q(z) = z^2 + c
# Convert Complex Field elements into tuples.
cpdef convert_to_pixels(point_list, double x_0, double y_0, double width, long number_of_pixels): r""" Converts cartesian coordinates to pixels within a specified window.
INPUT:
- ``point_list`` -- list of tuples, points in cartesian coordinates.
- ``x_0`` -- double, x-coordinate of the center of the image.
- ``y_0`` -- double, y-coordinate of the center of the image.
- ``width`` -- double, width of visible window in caresian coordinates.
- ``number_of_pixels`` -- long, width of image in pixels.
OUTPUT:
List of tuples of integers representing pixels.
EXAMPLES::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import convert_to_pixels sage: convert_to_pixels([(-1,3),(0,-4),(5,0)], 0, 0, 12, 100) [(42, 25), (50, 83), (92, 50)] """ cdef: k, pixel_list, x_corner, y_corner, step_size long x_pixel, y_pixel
# Compute top left corner of window and step size
# Convert each point in list to pixel coordinates
cpdef get_line(start, end): r""" Produces a list of pixel coordinates approximating a line from a starting point to an ending point using the Bresenham's Line Algorithm.
REFERENCE:
[Br2016]_
INPUT:
- ``start`` -- tuple, starting point of line.
- ``end`` -- tuple, ending point of line.
OUTPUT:
List of tuples of integers approximating the line between two pixels.
EXAMPLES::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import get_line sage: get_line((0, 0), (3, 4)) [(0, 0), (1, 1), (1, 2), (2, 3), (3, 4)]
::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import get_line sage: get_line((3, 4), (0, 0)) [(3, 4), (2, 3), (1, 2), (1, 1), (0, 0)] """ # Setup initial conditions cdef: long x1, x2, y1, y2, dx, dy, error, ystep, y is_steep, swapped, points
# Determine how steep the line is
# Rotate line
# Swap start and end points if necessary and store swap state
# Recalculate differentials
# Calculate error
# Iterate over bounding box generating points between start and end
# Reverse the list if the coordinates were swapped
double x_center=0, double y_center=0, double image_width=4, long max_iteration=500, long pixel_count=500, long level_sep=2, r""" Plots the Julia set for a given `c` value in the complex plane for the map `Q_c(z) = z^2 + c`.
INPUT:
- ``c_real`` -- double, Real part of `c` value that determines Julia set.
- ``c_imag`` -- double, Imaginary part of `c` value that determines Julia set.
- ``x_center`` -- double (optional - default: ``0.0``), Real part of center point.
- ``y_center`` -- double (optional - default: ``0.0``), Imaginary part of center point.
- ``image_width`` -- double (optional - default: ``4.0``), width of image in the complex plane.
- ``max_iteration`` -- long (optional - default: ``500``), maximum number of iterations the map ``Q_c(z)``.
- ``pixel_count`` -- long (optional - default: ``500``), side length of image in number of pixels.
- ``level_sep`` -- long (optional - default: ``2``), number of iterations between each color level.
- ``color_num`` -- long (optional - default: ``40``), number of colors used to plot image.
- ``base_color`` -- RGB color (optional - default: ``[50, 50, 50]``), color used to determine the coloring of set.
OUTPUT:
24-bit RGB image of the Julia set in the complex plane.
EXAMPLES:
Plot the Julia set for `c=-1+0i`::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_julia_plot sage: fast_julia_plot(-1, 0) 500x500px 24-bit RGB image
We can focus on smaller parts of the set by adjusting ``image_width``::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import fast_julia_plot sage: fast_julia_plot(-0.7, 0.3, x_center=.75, image_width=0.01, color_num=100) 500x500px 24-bit RGB image """
cdef long i, j, col, row, level, color_value, iteration cdef double k, x_corner, y_corner, step_size, x_coor, y_coor, new_x, new_y cdef M, pixel, color_list
# Make sure image_width is positive
# Initialize an image to the color black and access the pixels
# Take the given base color and create a list of evenly spaced # colors between the given base color and white. The number of # colors in the list depends on the variable color_num. # Convert Color to RGB list base_color = [int(k*255) for k in base_color]
# First, we determine the complex coordinates of the point in the top left # corner of the image. Then, we loop through each pixel in the image and # assign it complex coordinates relative to the image's top left corner.
# We compute the orbit of each pixel under the map Q(z) = z^2 + c # until we either reach the maximum number of iterations # or find a point in the orbit with modulus greater than 2
# If the point escapes to infinity, assign the point a color # based on how fast it escapes. The more iterations it takes for # a point to escape to infinity, the lighter its color will be. # Otherwise, assume the point is in the Julia set and leave # it black. # Assign each point a level based on its number of iterations. # Assign the pixel a color based on it's level. If we run out # of colors, assign it the last color in the list. else:
double y_center=0, double image_width=4, long max_iteration=500, long pixel_count=500, long level_sep=2, long color_num=40,
r""" Helper function that returns the image of a Julia set for a given `c` value side by side with the Mandelbrot set with a point denoting the `c` value.
INPUT:
- ``c_real`` -- double, Real part of `c` value that determines Julia set.
- ``c_imag`` -- double, Imaginary part of `c` value that determines Julia set.
- ``x_center`` -- double (optional - default: ``0.0``), Real part of center point.
- ``y_center`` -- double (optional - default: ``0.0``), Imaginary part of center point.
- ``image_width`` -- double (optional - default: ``4.0``), width of image in the complex plane.
- ``max_iteration`` -- long (optional - default: ``500``), maximum number of iterations the map ``Q_c(z)``.
- ``pixel_count`` -- long (optional - default: ``500``), side length of image in number of pixels.
- ``level_sep`` -- long (optional - default: ``2``), number of iterations between each color level.
- ``color_num`` -- long (optional - default: ``40``), number of colors used to plot image.
- ``base_color`` -- RGB color (optional - default: ``[50, 50, 50]``), color used to determine the coloring of set.
- ``point_color`` -- RGB color (optional - default: ``[255, 0, 0]``), color of the point `c` in the Mandelbrot set.
OUTPUT:
24-bit RGB image of the Julia and Mandelbrot sets in the complex plane.
EXAMPLES:
Plot the Julia set for `c=-1+0i`::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import julia_helper sage: julia_helper(-1,0) 1001x500px 24-bit RGB image
We can focus on smaller parts of the set by adjusting ``image_width``::
sage: from sage.dynamics.complex_dynamics.mandel_julia_helper import julia_helper sage: julia_helper(-.5, .6, y_center=0.178, image_width=0.01) # long time 1001x500px 24-bit RGB image """
cdef: int i, j M, Mp, G, Gp, J, Jp, CP
# Initialize the Julia set max_iteration, pixel_count, level_sep, color_num, base_color)
# Initialize the image with Julia set on left side # Add white border between images
# Plot the Mandelbrot set on the right side
# Convert Color to RGB list if necessary point_color = [int(k*255) for k in point_color]
# Add a cross representing c-value to the Mandelbrot set. # Loop through x and y coordinates and check if they are in image
|