Z = gn.complex_grid(re_lim, re_num, im_lim, im_num)

# generate rational functions
p = np.array([1.0, 0.0, -2.0, 2.0])
q = np.array([2, 0, -10])
dp = tf.poly_der(p)
dq = tf.poly_der(q)
params = {'p': p, 'dp': dp, 'q': q, 'dq': dq}
f_val = tf.poly_fun
df_val = tf.d_poly_fun
max_iter = 50

# run newton's method
roots, con_root, con_num = gn.newton_method(Z,
                                            f_val,
                                            df_val,
                                            params,
                                            max_iter=max_iter)

# print computed roots
print "Computed " + str(len(roots)) + " roots of " + f_val.__name__
if len(roots) < 8:
    for root in roots:
        print "%.4f \t+\t%.4fi" % (root.real, root.imag)

# plot newton fractal
col_source = 'colourlovers'
# col_params = {'lover': 'joy_of_summer', 'keywords': 'I like your Smile', 'cmap':'spring', 'col_num':len(p)}
# col_params = {'lover': 'QitsuneQage', 'keywords': 'Almost Together', 'cmap':'jet', 'col_num':len(p)}
# col_params = {'lover': 'joy_of_summer', 'keywords': 'See The Light', 'cmap':'spectral', 'col_num':len(p)}
# col_params = {'lover': 'pseudo.cyborg', 'keywords': 'math music', 'cmap':'summer', 'col_num':len(p)}
# colors
colors = [(0, 255, 255), (128, 128, 255), (255, 0, 255), (255, 128, 128)]

# generalized newton parameter, a
a_seq = np.linspace(1.5, 0.6, 600)

# create image sequence
i = 1
for a in a_seq:
    # print progress
    img_file_name = directory + '/' + imagename + '%05d' % i + '.png'
    print 'Creating frame ' + str(i) + ' of ' + str(a_seq.size)
    i += 1

    # newton's method
    roots, con_root, con_num = gn.newton_method(Z,
                                                f_val,
                                                df_val,
                                                params,
                                                a=a,
                                                disp_time=False,
                                                known_roots=known_roots)

    # create image in folder
    gn.newton_plot(con_root, con_num, colors, save_path=img_file_name)

# create the movie
ctrlStr = 'ffmpeg -r %d -i %s%%05d.png -c:v libx264 -preset slow -crf %d %s' % (
    frame_ps, directory + '/' + imagename, quality, filename)
subprocess.call(ctrlStr, shell=True)
    print 'Creating frame ' + str(i + 1) + ' of ' + str(num_times)

    # create polynomial from roots
    known_roots = param_roots[i, :]
    p = np.flipud(poly.polyfromroots(known_roots))
    dp = tf.poly_der(p)

    # update param dictionary
    params['p'] = p
    params['dp'] = dp

    # newton's method
    roots, con_root, con_num = gn.newton_method(Z,
                                                f_val,
                                                df_val,
                                                params,
                                                disp_time=False,
                                                known_roots=known_roots,
                                                max_iter=max_iter)

    # create image in folder
    gn.newton_plot(con_root,
                   con_num,
                   colors,
                   save_path=img_file_name,
                   max_shade=max_iter)

# create the movie
ctrlStr = 'ffmpeg -r %d -i %s%%05d.png -c:v libx264 -preset slow -crf %d %s' % (
    frame_ps, directory + '/' + imagename, quality, filename)
subprocess.call(ctrlStr, shell=True)
im_lim = [-0.2, 0.2]
im_num = 1000
Z = gn.complex_grid(re_lim, re_num, im_lim, im_num)

# generate rational functions
p = np.array([1.0, 0.0, -2.0, 2.0])
q = np.array([2, 0, -10])
dp = tf.poly_der(p)
dq = tf.poly_der(q)
params = {'p': p, 'dp': dp, 'q':q, 'dq':dq}
f_val = tf.poly_fun
df_val = tf.d_poly_fun
max_iter = 50

# run newton's method
roots, con_root, con_num = gn.newton_method(Z, f_val, df_val, params, max_iter=max_iter)

# print computed roots
print "Computed " + str(len(roots)) + " roots of " + f_val.__name__
if len(roots) < 8:
    for root in roots:
        print "%.4f \t+\t%.4fi" %(root.real, root.imag)

# plot newton fractal
col_source = 'colourlovers'
# col_params = {'lover': 'joy_of_summer', 'keywords': 'I like your Smile', 'cmap':'spring', 'col_num':len(p)}
# col_params = {'lover': 'QitsuneQage', 'keywords': 'Almost Together', 'cmap':'jet', 'col_num':len(p)}
# col_params = {'lover': 'joy_of_summer', 'keywords': 'See The Light', 'cmap':'spectral', 'col_num':len(p)}
# col_params = {'lover': 'pseudo.cyborg', 'keywords': 'math music', 'cmap':'summer', 'col_num':len(p)}
# col_params = {'lover': 'silentHue', 'keywords': 'Mute Math EP', 'cmap':'winter', 'col_num':len(p)}
# col_params = {'lover': 'yakotta', 'keywords': 'blue hour', 'cmap':'gnuplot', 'col_num':len(p)}
# get parameterized roots
speeds = np.array([1,4,1,4])
num_times = vid_len * frame_ps
param_roots = tf.parameterized_roots1(speeds, num_times)
max_iter = 50

# create image sequence
for i in range(num_times):
    # print progress
    img_file_name = directory + '/' + imagename + '%05d' % (i+1) + '.png'
    print 'Creating frame ' + str(i+1) + ' of ' + str(num_times)

    # create polynomial from roots
    known_roots = param_roots[i,:]
    p = np.flipud(poly.polyfromroots(known_roots))
    dp = tf.poly_der(p)

    # update param dictionary
    params['p'] = p
    params['dp'] = dp

    # newton's method
    roots, con_root, con_num = gn.newton_method(Z, f_val, df_val, params, disp_time=False, known_roots=known_roots, max_iter = max_iter)

    # create image in folder
    gn.newton_plot(con_root, con_num, colors, save_path=img_file_name, max_shade=max_iter)

# create the movie
ctrlStr = 'ffmpeg -r %d -i %s%%05d.png -c:v libx264 -preset slow -crf %d %s' %(frame_ps, directory + '/' + imagename, quality, filename)
subprocess.call(ctrlStr, shell=True)
df_val = tf.d_poly_fun
known_roots = np.roots(p)

# frame parameters
vid_len = 5  # length of gif
frame_ps = 18  # number of frames per second
quality = 22 # the quality of the encoding

# colors
colors = [(0, 255, 255), (128, 128, 255), (255, 0, 255), (255, 128, 128)]

# generalized newton parameter, a
a_seq = np.linspace(1.5, 0.6, 600)

# create image sequence
i = 1
for a in a_seq:
    # print progress
    img_file_name = directory + '/' + imagename + '%05d' % i + '.png'
    print 'Creating frame ' + str(i) + ' of ' + str(a_seq.size)
    i += 1

    # newton's method
    roots, con_root, con_num = gn.newton_method(Z, f_val, df_val, params, a=a, disp_time=False, known_roots=known_roots)

    # create image in folder
    gn.newton_plot(con_root, con_num, colors, save_path=img_file_name)

# create the movie
ctrlStr = 'ffmpeg -r %d -i %s%%05d.png -c:v libx264 -preset slow -crf %d %s' %(frame_ps, directory + '/' + imagename, quality, filename)
subprocess.call(ctrlStr, shell=True)