ó
 ,µ[c           @   s  d  Z  d d l Z d Z d Z d d „ Z d d „ Z d d d „ Z d d	 „ Z d d
 „ Z	 d d „ Z
 d „  Z d d „ Z d „  Z d „  Z e d k rd Z d e GHe e ƒ Z d e e ƒ d e j ƒ  d f GHd GHe j e d d ƒ GHd e e ƒ GHn  d S(   s  
==========================
Iterated Dynamical Systems
==========================

Digraphs from Integer-valued Iterated Functions

Sums of cubes on 3N
-------------------

The number 153 has a curious property.

Let 3N={3,6,9,12,...} be the set of positive multiples of 3.  Define an
iterative process f:3N->3N as follows: for a given n, take each digit
of n (in base 10), cube it and then sum the cubes to obtain f(n).

When this process is repeated, the resulting series n, f(n), f(f(n)),...
terminate in 153 after a finite number of iterations (the process ends
because 153 = 1**3 + 5**3 + 3**3).

In the language of discrete dynamical systems, 153 is the global
attractor for the iterated map f restricted to the set 3N.

For example: take the number 108

f(108) = 1**3 + 0**3 + 8**3 = 513

and

f(513) = 5**3 + 1**3 + 3**3 = 153

So, starting at 108 we reach 153 in two iterations,
represented as:

108->513->153

Computing all orbits of 3N up to 10**5 reveals that the attractor
153 is reached in a maximum of 14 iterations. In this code we
show that 13 cycles is the maximum required for all integers (in 3N)
less than 10,000.

The smallest number that requires 13 iterations to reach 153, is 177, i.e.,

177->687->1071->345->216->225->141->66->432->99->1458->702->351->153

The resulting large digraphs are useful for testing network software.

The general problem
-------------------

Given numbers n, a power p and base b, define F(n; p, b) as the sum of
the digits of n (in base b) raised to the power p. The above example
corresponds to f(n)=F(n; 3,10), and below F(n; p, b) is implemented as
the function powersum(n,p,b). The iterative dynamical system defined by
the mapping n:->f(n) above (over 3N) converges to a single fixed point;
153. Applying the map to all positive integers N, leads to a discrete
dynamical process with 5 fixed points: 1, 153, 370, 371, 407. Modulo 3
those numbers are 1, 0, 1, 2, 2. The function f above has the added
property that it maps a multiple of 3 to another multiple of 3; i.e. it
is invariant on the subset 3N.


The squaring of digits (in base 10) result in cycles and the
single fixed point 1. I.e., from a certain point on, the process
starts repeating itself.

keywords: "Recurring Digital Invariant", "Narcissistic Number",
"Happy Number"

The 3n+1 problem
----------------

There is a rich history of mathematical recreations
associated with discrete dynamical systems.  The most famous
is the Collatz 3n+1 problem. See the function
collatz_problem_digraph below. The Collatz conjecture
--- that every orbit returrns to the fixed point 1 in finite time
--- is still unproven. Even the great Paul Erdos said "Mathematics
is not yet ready for such problems", and offered $500
for its solution.

keywords: "3n+1", "3x+1", "Collatz problem", "Thwaite's conjecture"
iÿÿÿÿNi'  i   i
   c         C   sK   |  d k r d g Sg  } x+ |  d k rF |  | g | } |  | }  q W| S(   s]   Return list of digits comprising n represented in base b.
    n must be a nonnegative integeri    (    (   t   nt   bt   dlist(    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt	   digitsrep[   s    c         C   s8   t  |  | ƒ } d } x | D] } | | | 7} q W| S(   s<   Return sum of digits of n (in base b) raised to the power p.i    (   R   (   R    t   pR   R   t   sumt   k(    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   powersumk   s
    c         C   s    t  j ƒ  } x t d |  d ƒ D]x } | | d k r  | | k r  | } t | | | ƒ } x; | | k r” | j | | ƒ | } t | | | ƒ } q] Wq  q  W| S(   s1   Return digraph of iterations of powersum(n,3,10).i   i    (   t   nxt   DiGrapht   rangeR   t   add_edge(   R    R   t   multipleR   t   GR   t   k1t   knext(    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   attractor153_grapht   s    c         C   s·   t  j ƒ  } x¤ t d |  d ƒ D] } | } | j | ƒ t | d | ƒ } | j | | ƒ xQ | | k r® | } t | d | ƒ } | j | | ƒ | j | ƒ d k r^ Pq^ q^ Wq  W| S(   s1   Return digraph of iterations of powersum(n,2,10).i   i   (   R   R	   R
   t   add_nodeR   R   t
   out_degree(   R    R   R   R   R   R   (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   squaring_cycle_graph_old‚   s    c            s   ‡  f d †  } t  |  | ƒ S(   Nc            s   t  |  d ˆ  ƒ S(   Ni   (   R   (   R    (   R   (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   f•   s    (   t   discrete_dynamics_digraph(   t   nmaxR   R   (    (   R   sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   sum_of_digits_graph”   s    c            s   ‡  f d †  } t  |  | ƒ S(   Nc            s   t  |  d ˆ  ƒ S(   Ni   (   R   (   R    (   R   (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyR   š   s    (   R   (   R   R   R   (    (   R   sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   squaring_cycle_digraph™   s    c         C   s   d „  } t  |  | ƒ S(   Nc         S   s   t  |  d d ƒ S(   Ni   i
   (   R   (   R    (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyR   Ÿ   s    (   R   (   R   R   (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   cubing_153_digraphž   s    	iPÃ  c         C   sµ   t  j ƒ  } x¢ t d |  d ƒ D] } | } | j | ƒ | | ƒ } | j | | ƒ xU | | k r¬ | | >r¬ | } | | ƒ } | j | | ƒ | j | ƒ d k rX PqX qX Wq  W| S(   Ni   (   R   R	   R
   R   R   R   (   R   R   t   itermaxR   R   t   koldt   knew(    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyR   £   s    c         C   s   d „  } t  |  | ƒ S(   Nc         S   s(   |  d d k r |  d Sd |  d Sd  S(   Ni   i    i   i   (    (   R    (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyR   ¶   s    (   R   (   R   R   (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   collatz_problem_digraphµ   s    	c         C   s,   g  |  D]! } |  j  | ƒ d k r | ^ q S(   sf   Return a list of fixed points for the discrete dynamical
    system represented by the digraph G.
    i    (   R   (   R   R    (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   fixed_points¾   s    t   __main__s   Building cubing_153_digraph(%d)s   Resulting digraph hass	   nodes ands    edgess!   Shortest path from 177 to 153 is:i±   i™   s   fixed points are %s(   t   __doc__t   networkxR   R   R   R   R   R   R   R   R   R   R   R   R   t   __name__R   t   lent   sizet   shortest_path(    (    (    sF   share/doc/networkx-2.2/examples/advanced/iterated_dynamical_systems.pyt   <module>S   s,   						