# -*- coding: utf-8 -*- """ Created on Mon Apr 11 11:27:02 2016 @author: Miroslav Hrstka, BUT FME Brno Module for performing HSV method from a complex function. The algorithm origines from a program involving symbolic function and it is a modificaton for standard numpy arrays (modified by Miroslav Hrstka). run_hsv=1 if run_hsv==1: #-------------------------------------------------------------------------------- #----- Program for HSV method. Is is necessary to import a module mod_HSV.py ---- #-------------------------------------------------------------------------------- ab=[-0.0,1.0] # limits on the real axis cd=[-0.0,15.0] #limits on the imaginary axis N=400 #unitary resolution, 200 for a nice picture sat=0.8 # set s as global value me=1.0#0.01 # defalut value me = 0.01 ve=1.0#0.2 # defalut value ve = 0.2 pic_out = 'yes' #'no' 'yes' font_type = 'sans-serif' #'lmroman' font_size = 12 file_name = 'Aniso-aniso_fail.pdf' mod_HSV.plot_HSV(detK,ab,cd,N,sat,me,ve,pic_out,font_type,font_size,file_name) """ import numpy as np #import mpmath as mp #from mpmath import mpf from matplotlib.colors import hsv_to_rgb from matplotlib import pyplot as plt #The MATLAB-like interface to matplotlib #------------------------------------------------------------------------------ #------------------ Here starst functions for HSV algorithm ------------------- #------------------------------------------------------------------------------ def func_vals(f, re, im, N): #evaluates the complex function at the nodes of the grid #re and im are tuples, re=(a,b) and im=(c,d), defining the rectangular region #N is the number of nodes per unit interval resL,resH=(N,N) #plot_range(re,im,N) x=np.linspace(re[0], re[1],resL) y=np.linspace(im[0], im[1],resH) x,y=np.meshgrid(x,y) z=x+1j*y w = np.zeros((resH,resL),dtype='complex') for i in range(resH): for j in range(resL): w[i,j] = f(z[i,j]) return w def fH(z):# computes the hue corresponding to the complex number z H=np.angle(z)/(2*np.pi)+1.0 return np.mod(H,1) def fS(s, H):# computes the saturation corresponding to the complex number z S=s*np.ones_like(H) return S def fV(z, me, ve):# computes the value corresponding to the complex number z modul=np.absolute(z) #V = np.ones((np.shape(z))) V = 1.0*(modul/modul)#(1.0-1.0/(1.0+1e23*modul**me))**ve #V = 1.0-0.2**(-modul*1e30)#(1.0-1.0/(1.0+modul**me))**ve #V = (1.0-1.0/(1.0+modul**me))**ve return V def domaincol_c(w, me, ve, s): #Generalized domain coloring #w is the complex array of values f(z) indi=np.where(np.isinf(w))#detects the values w=a+ib, with a or b or both = infinity indn=np.where(np.isnan(w))#detects nans H=fH(w) S=fS(s, H) V=fV(w, me, ve) # the points mapped to infinity are colored with white; hsv_to_rgb(0,0,1)=(1,1,1)=white H[indi]=0.0 S[indi]=0.0 V[indi]=1.0 #hsv_to_rgb(0,0,0.5)=(0.5,0.5, 0.5)=gray H[indn]=0.0 S[indn]=0.0 V[indn]=0.5 HSV = np.dstack((H,S,V)) RGB = hsv_to_rgb(HSV) return RGB def plot_domain(color_func, w, me, ve, re, im, s, Title='', daxis=None): #w=func_vals(f, re, im, N) domc=color_func(w, me, ve, s) plt.xlabel("$\Re(z)$") plt.ylabel("$\Im(z)$") plt.title(Title) if(daxis): plt.imshow(domc, origin="lower", extent=[re[0], re[1], im[0], im[1]],aspect='auto') else: plt.imshow(domc, origin="lower") plt.axis('off') def plot_HSV(f, ab, cd, N, s, me, ve, pic_out, font_type, font_size,file_name): #plt.rcParams['figure.figsize'] = 10, 5 if pic_out=='yes': if font_type=='sans-serif': plt.rcParams['text.latex.preamble']=[r'\usepackage{lmodern}',r'\usepackage{sansmath}' r'\sansmath'] #Options params = {'figure.figsize' : [8.2, 4.1], 'text.usetex' : True, 'font.size' : font_size, 'font.family' : 'sans-serif', 'text.latex.unicode': True, } plt.rcParams.update(params) elif font_type=='lmroman': plt.rcParams['text.latex.preamble']=[r'\usepackage{lmodern}'] #Options params = {'figure.figsize' : [10, 5], 'text.usetex' : True, 'font.size' : font_size, 'font.family' : 'lmodern', 'text.latex.unicode': True, } plt.rcParams.update(params) elif pic_out=='no': plt.rcParams plt.rcParams['figure.figsize'] = 10, 5 plt.rcParams['text.usetex'] = False plt.subplot(1,2,1,aspect='auto') w=func_vals(f, ab, cd, N) plot_domain(domaincol_c, w, me, ve, ab, cd, s, Title='$f(z)$', daxis=True) resL,resH=(N,N)#plot_range(ab,cd,N) x = np.linspace(ab[0], ab[1], resL) #contour vectors y = np.linspace(cd[0], cd[1], resH) #w=func_vals(f, ab, cd, N) plt.subplot(1,2,2,aspect='auto') plt.axis([ab[0], ab[1], cd[0], cd[1]]) cs1=plt.contour(x, y, w.real, [0], colors='b') cs2=plt.contour(x, y, w.imag, [0], colors='r') plt.xlabel("$\Re(z)$") plt.ylabel("$\Im(z)$") plt.title("contour $f(z)=0$") lines = [ cs1.collections[0], cs2.collections[0]] labels = ['$\Re f(z)$','$\Im f(z)$'] plt.legend(lines, labels) plt.tight_layout(2) if pic_out=='yes': plt.savefig(file_name, dpi=600,transparent=True) plt.show() #plt.rcParams['figure.figsize'] = 10, 5 #plt.rcParams['text.usetex'] = False