def __init__(self, polys, domain): r""" The Python constructor. See :class:`DynamicalSystem` for details. EXAMPLES:: sage: T.<x,y,w,u> = ProductProjectiveSpaces([1, 1], QQ) sage: DynamicalSystem_projective([x^2, y^2, w^2, u^2], domain=T) Dynamical System of Product of projective spaces P^1 x P^1 over Rational Field Defn: Defined by sending (x : y , w : u) to (x^2 : y^2 , w^2 : u^2). """ DynamicalSystem.__init__(self, polys, domain)
def __init__(self, polys, domain): r""" The Python constructor. See :class:`DynamicalSystem` for details. EXAMPLES:: sage: T.<x,y,w,u> = ProductProjectiveSpaces([1, 1], QQ) sage: DynamicalSystem_projective([x^2, y^2, w^2, u^2], domain=T) Dynamical System of Product of projective spaces P^1 x P^1 over Rational Field Defn: Defined by sending (x : y , w : u) to (x^2 : y^2 , w^2 : u^2). """ DynamicalSystem.__init__(self, polys, domain)
def __init__(self, polys_or_rat_fncts, domain): r""" The Python constructor. See :class:`DynamicalSystem` for details. EXAMPLES:: sage: A.<x,y> = AffineSpace(QQ, 2) sage: DynamicalSystem_affine([3/5*x^2, y^2/(2*x^2)], domain=A) Dynamical System of Affine Space of dimension 2 over Rational Field Defn: Defined on coordinates by sending (x, y) to (3/5*x^2, y^2/(2*x^2)) """ L = polys_or_rat_fncts # Next attribute needed for _fast_eval and _fastpolys self._is_prime_finite_field = is_PrimeFiniteField(L[0].base_ring()) DynamicalSystem.__init__(self, L, domain)
def __init__(self, polys_or_rat_fncts, domain): r""" The Python constructor. See :class:`DynamicalSystem` for details. EXAMPLES:: sage: A.<x,y> = AffineSpace(QQ, 2) sage: DynamicalSystem_affine([3/5*x^2, y^2/(2*x^2)], domain=A) Dynamical System of Affine Space of dimension 2 over Rational Field Defn: Defined on coordinates by sending (x, y) to (3/5*x^2, y^2/(2*x^2)) """ L = polys_or_rat_fncts # Next attribute needed for _fast_eval and _fastpolys self._is_prime_finite_field = is_PrimeFiniteField(L[0].base_ring()) DynamicalSystem.__init__(self, L, domain)
def julia_plot(f=None, **kwds): r""" Plots the Julia set of a given polynomial ``f``. Users can specify whether they would like to display the Mandelbrot side by side with the Julia set with the ``mandelbrot`` argument. If ``f`` is not specified, this method defaults to `f(z) = z^2-1`. The Julia set of a polynomial ``f`` is the set of complex numbers `z` for which the function `f(z)` is bounded under iteration. The Julia set can be visualized by plotting each point in the set in the complex plane. Julia sets are examples of fractals when plotted in the complex plane. ALGORITHM: Let `R_c = \bigl(1 + \sqrt{1 + 4|c|}\bigr)/2` if the polynomial is of the form `f(z) = z^2 + c`; otherwise, let `R_c = 2`. For every `p \in \mathbb{C}`, if `|f^{k}(p)| > R_c` for some `k \geq 0`, then `f^{n}(p) \to \infty`. Let `N` be the maximum number of iterations. Compute the first `N` points on the orbit of `p` under `f`. If for any `k < N`, `|f^{k}(p)| > R_c`, we stop the iteration and assign a color to the point `p` based on how quickly `p` escaped to infinity under iteration of `f`. If `|f^{i}(p)| \leq R_c` for all `i \leq N`, we assume `p` is in the Julia set and assign the point `p` the color black. INPUT: - ``f`` -- input polynomial (optional - default: ``z^2 - 1``). - ``period`` -- list (optional - default: ``None``), returns the Julia set for a random `c` value with the given (formal) cycle structure. - ``mandelbrot`` -- boolean (optional - default: ``True``), when set to ``True``, an image of the Mandelbrot set is appended to the right of the Julia set. - ``point_color`` -- RGB color (optional - default: ``'tomato'``), color of the point `c` in the Mandelbrot set (any valid input for Color). - ``x_center`` -- double (optional - default: ``-1.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 `f(z)`. - ``pixel_count`` -- long (optional - default: ``500``), side length of image in number of pixels. - ``base_color`` -- hex color (optional - default: ``'steelblue'``), color used to determine the coloring of set (any valid input for Color). - ``level_sep`` -- long (optional - default: 1), number of iterations between each color level. - ``number_of_colors`` -- long (optional - default: 30), number of colors used to plot image. - ``interact`` -- boolean (optional - default: ``False``), controls whether plot will have interactive functionality. OUTPUT: 24-bit RGB image of the Julia set in the complex plane. .. TODO:: Implement the side-by-side Mandelbrot-Julia plots for general one-parameter families of polynomials. EXAMPLES: The default ``f`` is `z^2 - 1`:: sage: julia_plot() 1001x500px 24-bit RGB image To display only the Julia set, set ``mandelbrot`` to ``False``:: sage: julia_plot(mandelbrot=False) 500x500px 24-bit RGB image :: sage: R.<z> = CC[] sage: f = z^3 - z + 1 sage: julia_plot(f) 500x500px 24-bit RGB image To display an interactive plot of the Julia set in the Notebook, set ``interact`` to ``True``. (This is only implemented for polynomials of the form ``f = z^2 + c``):: sage: julia_plot(interact=True) interactive(children=(FloatSlider(value=-1.0, description=u'Real c'... :: sage: R.<z> = CC[] sage: f = z^2 + 1/2 sage: julia_plot(f,interact=True) interactive(children=(FloatSlider(value=0.5, description=u'Real c'... To return the Julia set of a random `c` value with (formal) cycle structure `(2,3)`, set ``period = [2,3]``:: sage: julia_plot(period=[2,3]) 1001x500px 24-bit RGB image To return all of the Julia sets of `c` values with (formal) cycle structure `(2,3)`:: sage: period = [2,3] # not tested ....: R.<c> = QQ[] ....: P.<x,y> = ProjectiveSpace(R,1) ....: f = DynamicalSystem([x^2+c*y^2, y^2]) ....: L = f.dynatomic_polynomial(period).subs({x:0,y:1}).roots(ring=CC) ....: c_values = [k[0] for k in L] ....: for c in c_values: ....: julia_plot(c) Polynomial maps can be defined over a polynomial ring or a fraction field, so long as ``f`` is polynomial:: sage: R.<z> = CC[] sage: f = z^2 - 1 sage: julia_plot(f) 1001x500px 24-bit RGB image :: sage: R.<z> = CC[] sage: K = R.fraction_field(); z = K.gen() sage: f = z^2-1 sage: julia_plot(f) 1001x500px 24-bit RGB image Interact functionality is not implemented if the polynomial is not of the form `f = z^2 + c`:: sage: R.<z> = CC[] sage: f = z^3 + 1 sage: julia_plot(f, interact=True) Traceback (most recent call last): ... NotImplementedError: The interactive plot is only implemented for ... """ # extract keyword arguments period = kwds.pop("period", None) mandelbrot = kwds.pop("mandelbrot", True) point_color = kwds.pop("point_color", 'tomato') x_center = kwds.pop("x_center", 0.0) y_center = kwds.pop("y_center", 0.0) image_width = kwds.pop("image_width", 4.0) max_iteration = kwds.pop("max_iteration", 500) pixel_count = kwds.pop("pixel_count", 500) base_color = kwds.pop("base_color", 'steelblue') level_sep = kwds.pop("level_sep", 1) number_of_colors = kwds.pop("number_of_colors", 30) interacts = kwds.pop("interact", False) f_is_default_after_all = None if period: # pick a random c with the specified period R = PolynomialRing(CC, 'c') c = R.gen() x, y = ProjectiveSpace(R, 1, 'x,y').gens() F = DynamicalSystem([x**2 + c * y**2, y**2]) L = F.dynatomic_polynomial(period).subs({x: 0, y: 1}).roots(ring=CC) c = L[randint(0, len(L) - 1)][0] base_color = Color(base_color) point_color = Color(point_color) EPS = 0.00001 if f is not None and period is None: # f user-specified and no period given # try to coerce f to live in a polynomial ring S = PolynomialRing(CC, names='z') z = S.gen() try: f_poly = S(f) except TypeError: R = f.parent() if not (R.is_integral_domain() and (CC.is_subring(R) or CDF.is_subring(R))): raise ValueError('Given `f` must be a complex polynomial.') else: raise NotImplementedError( 'Julia sets not implemented for rational functions.') if (f_poly - z * z) in CC: # f is specified and of the form z^2 + c. f_is_default_after_all = True c = f_poly - z * z else: # f is specified and not of the form z^2 + c if interacts: raise NotImplementedError( "The interactive plot is only implemented for " "polynomials of the form f = z^2 + c.") else: return general_julia(f_poly, x_center, y_center, image_width, max_iteration, pixel_count, level_sep, number_of_colors, base_color) # otherwise we can use fast_julia_plot for z^2 + c if f_is_default_after_all or f is None or period is not None: # specify default c = -1 value if f and period were not specified if not f_is_default_after_all and period is None: c = -1 c = CC(c) c_real = c.real() c_imag = c.imag() if interacts: # set widgets from ipywidgets.widgets import FloatSlider, IntSlider, \ ColorPicker, interact widgets = dict( c_real=FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_real, description="Real c"), c_imag=FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_imag, description="Imag c"), x_center=FloatSlider(min=-1.0, max=1.0, step=EPS, value=x_center, description="Real center"), y_center=FloatSlider(min=-1.0, max=1.0, step=EPS, value=y_center, description="Imag center"), image_width=FloatSlider(min=EPS, max=4.0, step=EPS, value=image_width, description="Width"), max_iteration=IntSlider(min=0, max=1000, value=max_iteration, description="Iterations"), pixel_count=IntSlider(min=10, max=1000, value=pixel_count, description="Pixels"), level_sep=IntSlider(min=1, max=20, value=level_sep, description="Color sep"), color_num=IntSlider(min=1, max=100, value=number_of_colors, description="# Colors"), base_color=ColorPicker(value=base_color.html_color(), description="Base color"), ) if mandelbrot: widgets["point_color"] = ColorPicker( value=point_color.html_color(), description="Point color") return interact(**widgets).widget(julia_helper) else: return interact(**widgets).widget(fast_julia_plot) elif mandelbrot: # non-interactive with mandelbrot return julia_helper(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, level_sep, number_of_colors, base_color, point_color) else: # non-interactive without mandelbrot return fast_julia_plot(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, level_sep, number_of_colors, base_color)
def __classcall_private__(cls, dynamical_system, domain=None, ideal=None): """ Return the appropriate dynamical system on Berkovich space. EXAMPLES:: sage: R.<t> = Qp(3)[] sage: f = DynamicalSystem_affine(t^2 - 3) sage: DynamicalSystem_Berkovich(f) Dynamical system of Affine Berkovich line over Cp(3) of precision 20 induced by the map Defn: Defined on coordinates by sending ((1 + O(3^20))*t) to ((1 + O(3^20))*t^2 + 2*3 + 2*3^2 + 2*3^3 + 2*3^4 + 2*3^5 + 2*3^6 + 2*3^7 + 2*3^8 + 2*3^9 + 2*3^10 + 2*3^11 + 2*3^12 + 2*3^13 + 2*3^14 + 2*3^15 + 2*3^16 + 2*3^17 + 2*3^18 + 2*3^19 + 2*3^20 + O(3^21)) """ if not (is_Berkovich_Cp(domain) or domain is None): raise TypeError( 'domain must be a Berkovich space over Cp, not %s' % domain) if isinstance(domain, Berkovich_Cp_Affine): if not isinstance(dynamical_system, DynamicalSystem_affine): try: dynamical_system = DynamicalSystem_affine(dynamical_system) except: raise TypeError('domain was affine Berkovich space, but dynamical_system did not ' + \ 'convert to an affine dynamical system') if isinstance(domain, Berkovich_Cp_Projective): if not isinstance(dynamical_system, DynamicalSystem_projective): try: dynamical_system = DynamicalSystem_projective( dynamical_system) except: raise TypeError('domain was projective Berkovich space, but dynamical_system did not convert ' + \ 'to a projective dynamical system') if not isinstance(dynamical_system, DynamicalSystem): try: dynamical_system = DynamicalSystem(dynamical_system) except: raise TypeError( 'dynamical_system did not convert to a dynamical system') morphism_domain = dynamical_system.domain() if not isinstance(morphism_domain.base_ring(), pAdicBaseGeneric): if morphism_domain.base_ring() in NumberFields(): if domain is None and ideal is not None: if is_AffineSpace(morphism_domain): domain = Berkovich_Cp_Affine( morphism_domain.base_ring(), ideal) else: domain = Berkovich_Cp_Projective( morphism_domain, ideal) else: if ideal is not None: if ideal != domain.ideal(): raise ValueError( 'conflicting inputs for ideal and domain') else: raise ValueError('base ring of domain of dynamical_system must be p-adic or a number field ' + \ 'not %s' %morphism_domain.base_ring()) if is_AffineSpace(morphism_domain): return DynamicalSystem_Berkovich_affine(dynamical_system, domain) return DynamicalSystem_Berkovich_projective(dynamical_system, domain)
def julia_plot(c=-1, **kwds): r""" Plots the Julia set of a given complex `c` value. Users can specify whether they would like to display the Mandelbrot side by side with the Julia set. The Julia set of a given `c` value is the set of complex numbers for which the function `Q_c(z)=z^2+c` is bounded under iteration. The Julia set can be visualized by plotting each point in the set in the complex plane. Julia sets are examples of fractals when plotted in the complex plane. ALGORITHM: Define the map `Q_c(z) = z^2 + c` for some `c \in \mathbb{C}`. For every `p \in \mathbb{C}`, if `|Q_{c}^{k}(p)| > 2` for some `k \geq 0`, then `Q_{c}^{n}(p) \to \infty`. Let `N` be the maximum number of iterations. Compute the first `N` points on the orbit of `p` under `Q_c`. If for any `k < N`, `|Q_{c}^{k}(p)| > 2`, we stop the iteration and assign a color to the point `p` based on how quickly `p` escaped to infinity under iteration of `Q_c`. If `|Q_{c}^{i}(p)| \leq 2` for all `i \leq N`, we assume `p` is in the Julia set and assign the point `p` the color black. INPUT: - ``c`` -- complex (optional - default: ``-1``), complex point `c` that determines the Julia set. kwds: - ``period`` -- list (optional - default: ``None``), returns the Julia set for a random `c` value with the given (formal) cycle structure. - ``mandelbrot`` -- boolean (optional - default: ``True``), when set to ``True``, an image of the Mandelbrot set is appended to the right of the Julia set. - ``point_color`` -- RGB color (optional - default: ``[255, 0, 0]``), color of the point `c` in the Mandelbrot set. - ``x_center`` -- double (optional - default: ``-1.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. - ``base_color`` -- RGB color (optional - default: ``[40, 40, 40]``), color used to determine the coloring of set. - ``iteration_level`` -- long (optional - default: 1), number of iterations between each color level. - ``number_of_colors`` -- long (optional - default: 30), number of colors used to plot image. - ``interact`` -- boolean (optional - default: ``False``), controls whether plot will have interactive functionality. OUTPUT: 24-bit RGB image of the Julia set in the complex plane. EXAMPLES:: sage: julia_plot() 1001x500px 24-bit RGB image To display only the Julia set, set ``mandelbrot`` to ``False``:: sage: julia_plot(mandelbrot=False) 500x500px 24-bit RGB image To display an interactive plot of the Julia set in the Notebook, set ``interact`` to ``True``:: sage: julia_plot(interact=True) <html>...</html> To return the Julia set of a random `c` value with (formal) cycle structure `(2,3)`, set ``period = [2,3]``:: sage: julia_plot(period=[2,3]) 1001x500px 24-bit RGB image To return all of the Julia sets of `c` values with (formal) cycle structure `(2,3)`:: sage: period = [2,3] # not tested ....: R.<c> = QQ[] ....: P.<x,y> = ProjectiveSpace(R,1) ....: f = DynamicalSystem([x^2+c*y^2, y^2]) ....: L = f.dynatomic_polynomial(period).subs({x:0,y:1}).roots(ring=CC) ....: c_values = [k[0] for k in L] ....: for c in c_values: ....: julia_plot(c) """ x_center = kwds.pop("x_center", 0.0) y_center = kwds.pop("y_center", 0.0) image_width = kwds.pop("image_width", 4.0) max_iteration = kwds.pop("max_iteration", 500) pixel_count = kwds.pop("pixel_count", 500) base_color = kwds.pop("base_color", [50, 50, 50]) iteration_level = kwds.pop("iteration_level", 1) number_of_colors = kwds.pop("number_of_colors", 50) point_color = kwds.pop("point_color", [255, 0, 0]) interacts = kwds.pop("interact", False) mandelbrot = kwds.pop("mandelbrot", True) period = kwds.pop("period", None) if not period is None: R = PolynomialRing(QQ, 'c') c = R.gen() P = ProjectiveSpace(R, 1, 'x,y') x,y = P.gens() f = DynamicalSystem([x**2+c*y**2, y**2]) L = f.dynatomic_polynomial(period).subs({x:0,y:1}).roots(ring=CC) c = L[randint(0,len(L)-1)][0] c_real = CC(c).real() c_imag = CC(c).imag() if interacts: @interact(layout={'bottom':[['real_center'], ['im_center'], ['width']], 'top':[['iterations'], ['level_sep'], ['color_num'], ['mandel'], ['cx'], ['cy']], 'right':[['image_color'], ['pt_color']]}) def _(cx = input_box(c_real, '$Re(c)$'), cy = input_box(c_imag, '$Im(c)$'), real_center=input_box(x_center, 'Real Center'), im_center=input_box(y_center, 'Imaginary Center'), width=input_box(image_width, 'Width of Image'), iterations=input_box(max_iteration, 'Max Number of Iterations'), level_sep=input_box(iteration_level, 'Iterations between Colors'), color_num=input_box(number_of_colors, 'Number of Colors'), image_color=color_selector(default=Color([j/255 for j in base_color]), label="Image Color", hide_box=True), pt_color=color_selector(default=Color([j/255 for j in point_color]), label="Point Color", hide_box=True), mandel=checkbox(mandelbrot, label='Mandelbrot set')): if mandel: return julia_helper(cx, cy, real_center, im_center, width, iterations, pixel_count, level_sep, color_num, image_color, pt_color).show() else: return fast_julia_plot(cx, cy, real_center, im_center, width, iterations, pixel_count, level_sep, color_num, image_color).show() else: if mandelbrot: return julia_helper(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color, point_color) else: return fast_julia_plot(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color)
def julia_plot(c=-1, x_center=0.0, y_center=0.0, image_width=4.0, max_iteration=500, pixel_count=500, base_color='steelblue', iteration_level=1, number_of_colors=50, point_color='yellow', interact=False, mandelbrot=True, period=None): r""" Plots the Julia set of a given complex `c` value. Users can specify whether they would like to display the Mandelbrot side by side with the Julia set. The Julia set of a given `c` value is the set of complex numbers for which the function `Q_c(z)=z^2+c` is bounded under iteration. The Julia set can be visualized by plotting each point in the set in the complex plane. Julia sets are examples of fractals when plotted in the complex plane. ALGORITHM: Define the map `Q_c(z) = z^2 + c` for some `c \in \mathbb{C}`. For every `p \in \mathbb{C}`, if `|Q_{c}^{k}(p)| > 2` for some `k \geq 0`, then `Q_{c}^{n}(p) \to \infty`. Let `N` be the maximum number of iterations. Compute the first `N` points on the orbit of `p` under `Q_c`. If for any `k < N`, `|Q_{c}^{k}(p)| > 2`, we stop the iteration and assign a color to the point `p` based on how quickly `p` escaped to infinity under iteration of `Q_c`. If `|Q_{c}^{i}(p)| \leq 2` for all `i \leq N`, we assume `p` is in the Julia set and assign the point `p` the color black. INPUT: - ``c`` -- complex (optional - default: ``-1``), complex point `c` that determines the Julia set. - ``period`` -- list (optional - default: ``None``), returns the Julia set for a random `c` value with the given (formal) cycle structure. - ``mandelbrot`` -- boolean (optional - default: ``True``), when set to ``True``, an image of the Mandelbrot set is appended to the right of the Julia set. - ``point_color`` -- RGB color (optional - default: ``'tomato'``), color of the point `c` in the Mandelbrot set (any valid input for Color). - ``x_center`` -- double (optional - default: ``-1.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. - ``base_color`` -- RGB color (optional - default: ``'steelblue'``), color used to determine the coloring of set (any valid input for Color). - ``iteration_level`` -- long (optional - default: 1), number of iterations between each color level. - ``number_of_colors`` -- long (optional - default: 30), number of colors used to plot image. - ``interact`` -- boolean (optional - default: ``False``), controls whether plot will have interactive functionality. OUTPUT: 24-bit RGB image of the Julia set in the complex plane. EXAMPLES:: sage: julia_plot() 1001x500px 24-bit RGB image To display only the Julia set, set ``mandelbrot`` to ``False``:: sage: julia_plot(mandelbrot=False) 500x500px 24-bit RGB image To display an interactive plot of the Julia set in the Notebook, set ``interact`` to ``True``:: sage: julia_plot(interact=True) interactive(children=(FloatSlider(value=-1.0, description=u'Real c'... To return the Julia set of a random `c` value with (formal) cycle structure `(2,3)`, set ``period = [2,3]``:: sage: julia_plot(period=[2,3]) 1001x500px 24-bit RGB image To return all of the Julia sets of `c` values with (formal) cycle structure `(2,3)`:: sage: period = [2,3] # not tested ....: R.<c> = QQ[] ....: P.<x,y> = ProjectiveSpace(R,1) ....: f = DynamicalSystem([x^2+c*y^2, y^2]) ....: L = f.dynatomic_polynomial(period).subs({x:0,y:1}).roots(ring=CC) ....: c_values = [k[0] for k in L] ....: for c in c_values: ....: julia_plot(c) """ if period is not None: R = PolynomialRing(QQ, 'c') c = R.gen() x, y = ProjectiveSpace(R, 1, 'x,y').gens() f = DynamicalSystem([x**2 + c * y**2, y**2]) L = f.dynatomic_polynomial(period).subs({x: 0, y: 1}).roots(ring=CC) c = L[randint(0, len(L) - 1)][0] c = CC(c) c_real = c.real() c_imag = c.imag() base_color = Color(base_color) point_color = Color(point_color) if interact: from ipywidgets.widgets import FloatSlider, IntSlider, ColorPicker, interact widgets = dict( c_real = FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_real, description="Real c"), c_imag = FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_imag, description="Imag c"), x_center = FloatSlider(min=-1.0, max=1.0, step=EPS, value=x_center, description="Real center"), y_center = FloatSlider(min=-1.0, max=1.0, step=EPS, value=y_center, description="Imag center"), image_width = FloatSlider(min=EPS, max=4.0, step=EPS, value=image_width, description="Image width"), max_iteration = IntSlider(min=0, max=600, value=max_iteration, description="Iterations"), pixel_count = IntSlider(min=10, max=600, value=pixel_count, description="Pixels"), level_sep = IntSlider(min=1, max=20, value=iteration_level, description="Color sep"), color_num = IntSlider(min=1, max=100, value=number_of_colors, description="# Colors"), base_color = ColorPicker(value=base_color.html_color(), description="Base color"), ) if mandelbrot: widgets["point_color"] = ColorPicker(value=point_color.html_color(), description="Point color") return interact(**widgets).widget(julia_helper) else: return interact(**widgets).widget(fast_julia_plot) if mandelbrot: return julia_helper(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color, point_color) else: return fast_julia_plot(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color)
def julia_plot(c=-1, x_center=0.0, y_center=0.0, image_width=4.0, max_iteration=500, pixel_count=500, base_color='steelblue', iteration_level=1, number_of_colors=50, point_color='yellow', interact=False, mandelbrot=True, period=None): r""" Plots the Julia set of a given complex `c` value. Users can specify whether they would like to display the Mandelbrot side by side with the Julia set. The Julia set of a given `c` value is the set of complex numbers for which the function `Q_c(z)=z^2+c` is bounded under iteration. The Julia set can be visualized by plotting each point in the set in the complex plane. Julia sets are examples of fractals when plotted in the complex plane. ALGORITHM: Define the map `Q_c(z) = z^2 + c` for some `c \in \mathbb{C}`. For every `p \in \mathbb{C}`, if `|Q_{c}^{k}(p)| > 2` for some `k \geq 0`, then `Q_{c}^{n}(p) \to \infty`. Let `N` be the maximum number of iterations. Compute the first `N` points on the orbit of `p` under `Q_c`. If for any `k < N`, `|Q_{c}^{k}(p)| > 2`, we stop the iteration and assign a color to the point `p` based on how quickly `p` escaped to infinity under iteration of `Q_c`. If `|Q_{c}^{i}(p)| \leq 2` for all `i \leq N`, we assume `p` is in the Julia set and assign the point `p` the color black. INPUT: - ``c`` -- complex (optional - default: ``-1``), complex point `c` that determines the Julia set. - ``period`` -- list (optional - default: ``None``), returns the Julia set for a random `c` value with the given (formal) cycle structure. - ``mandelbrot`` -- boolean (optional - default: ``True``), when set to ``True``, an image of the Mandelbrot set is appended to the right of the Julia set. - ``point_color`` -- RGB color (optional - default: ``'tomato'``), color of the point `c` in the Mandelbrot set (any valid input for Color). - ``x_center`` -- double (optional - default: ``-1.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. - ``base_color`` -- RGB color (optional - default: ``'steelblue'``), color used to determine the coloring of set (any valid input for Color). - ``iteration_level`` -- long (optional - default: 1), number of iterations between each color level. - ``number_of_colors`` -- long (optional - default: 30), number of colors used to plot image. - ``interact`` -- boolean (optional - default: ``False``), controls whether plot will have interactive functionality. OUTPUT: 24-bit RGB image of the Julia set in the complex plane. EXAMPLES:: sage: julia_plot() 1001x500px 24-bit RGB image To display only the Julia set, set ``mandelbrot`` to ``False``:: sage: julia_plot(mandelbrot=False) 500x500px 24-bit RGB image To display an interactive plot of the Julia set in the Notebook, set ``interact`` to ``True``:: sage: julia_plot(interact=True) interactive(children=(FloatSlider(value=-1.0, description=u'Real c'... To return the Julia set of a random `c` value with (formal) cycle structure `(2,3)`, set ``period = [2,3]``:: sage: julia_plot(period=[2,3]) 1001x500px 24-bit RGB image To return all of the Julia sets of `c` values with (formal) cycle structure `(2,3)`:: sage: period = [2,3] # not tested ....: R.<c> = QQ[] ....: P.<x,y> = ProjectiveSpace(R,1) ....: f = DynamicalSystem([x^2+c*y^2, y^2]) ....: L = f.dynatomic_polynomial(period).subs({x:0,y:1}).roots(ring=CC) ....: c_values = [k[0] for k in L] ....: for c in c_values: ....: julia_plot(c) """ if period is not None: R = PolynomialRing(QQ, 'c') c = R.gen() x, y = ProjectiveSpace(R, 1, 'x,y').gens() f = DynamicalSystem([x**2 + c * y**2, y**2]) L = f.dynatomic_polynomial(period).subs({x: 0, y: 1}).roots(ring=CC) c = L[randint(0, len(L) - 1)][0] c = CC(c) c_real = c.real() c_imag = c.imag() base_color = Color(base_color) point_color = Color(point_color) if interact: from ipywidgets.widgets import FloatSlider, IntSlider, ColorPicker, interact widgets = dict( c_real=FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_real, description="Real c"), c_imag=FloatSlider(min=-2.0, max=2.0, step=EPS, value=c_imag, description="Imag c"), x_center=FloatSlider(min=-1.0, max=1.0, step=EPS, value=x_center, description="Real center"), y_center=FloatSlider(min=-1.0, max=1.0, step=EPS, value=y_center, description="Imag center"), image_width=FloatSlider(min=EPS, max=4.0, step=EPS, value=image_width, description="Image width"), max_iteration=IntSlider(min=0, max=600, value=max_iteration, description="Iterations"), pixel_count=IntSlider(min=10, max=600, value=pixel_count, description="Pixels"), level_sep=IntSlider(min=1, max=20, value=iteration_level, description="Color sep"), color_num=IntSlider(min=1, max=100, value=number_of_colors, description="# Colors"), base_color=ColorPicker(value=base_color.html_color(), description="Base color"), ) if mandelbrot: widgets["point_color"] = ColorPicker( value=point_color.html_color(), description="Point color") return interact(**widgets).widget(julia_helper) else: return interact(**widgets).widget(fast_julia_plot) if mandelbrot: return julia_helper(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color, point_color) else: return fast_julia_plot(c_real, c_imag, x_center, y_center, image_width, max_iteration, pixel_count, iteration_level, number_of_colors, base_color)