v%fPddlZddlmZmZddlmZddlmZmZm Z m Z ddl m Z ddl mZgdZe ZeevrdZd Znd Zd Zd Zd ZdZdZdZdZd\ZZZZZddd ddZdddZdZ e d.dZ!e dddddZ"e dddd Z#e dddd!Z$e d/d"Z%e d0d#Z&e ddd$d%Z'e dd&d'd(d)Z(e d*Z)e d+Z*e d,Z+e,d-k(ryy)1N)inveig)ceil)pyplotticker get_backendrc)Axes3D)cycle)GTK3Agg GTK3CairoMacOSXnbAggQt4AggQt4CairoQt5AggQt5CairoTkAggTkCairoWebAggWXWXAggWXCairog?z#808080z#cab18cz#0096d6z#008367z#E31937z#004065)z#ef7b9dz#fbd349z#ffa500z#a35cffz#731d1dxyg~jt?)angles scale_unitsscalewidthg?皙?) linewidthalphacfd}|S)Nctddttddtddd td d |i|y)Nfontserif)familysizefigure)dpiaxesTr) axisbelow titlesizelinesr)r#)r fontsize)argskwargsfuncs D/Users/m0saan/Cursus/matrix/notes/notebook/../scripts/plot_helper.pywrapperzset_rc..wrapper(s< 6'1 8 6TQ/ 7a  df)r5r7s` r6set_rcr:'s Nr8c Vtj|}|jddk(sJd|.tj|}|jddk(sJdtj|}|jd}|jd}|dk(r|dkDrtj||df}nC|dk(r|dkDrtj||df}n |j|jk(sJd||z}tj tj tj||f}tj|dz}tjddgtz}tj| \}}|j|dddf|dddf|dddf|dddftd d d |j| |g|j| |g|j!d |j#} |j%} | d| dz } | d| dz } t t't)| | d} t+j,| }|j.j1||j2j1||j4dit6|j8dj;d|j8dj;d|j8dj=d|j8dj=dy)a Draw 2d vectors based on the values of the vectors and the position of their tails. Parameters ---------- vectors : list. List of 2-element array-like structures, each represents a 2d vector. tails : list, optional. List of 2-element array-like structures, each represents the coordinates of the tail of the corresponding vector in vectors. If None (default), all tails are set at the origin (0,0). If len(tails) is 1, all tails are set at the same position. Otherwise, vectors and tails must have the same length. Examples -------- >>> v = [(1, 3), (3, 3), (4, 6)] >>> plot_vector(v) # draw 3 vectors with their tails at origin >>> t = [numpy.array((2, 2))] >>> plot_vector(v, t) # draw 3 vectors with their tails at (2,2) >>> t = [[3, 2], [-1, -2], [3, 5]] >>> plot_vector(v, t) # draw 3 vectors with 3 different tails rz#Each vector should have 2 elements.Nz!Each tail should have 2 elements.rz'vectors and tail must have a same shapeg333333?figsizer)colorrrr equal)baseleftcenterbottomrightnonetop)T)numpyarrayshape zeros_liketilemaxabshstackr fig_scalersubplotsquiverdarkblueset_xlimset_ylim set_aspect get_xticks get_yticksintminrMultipleLocatorxaxisset_major_locatoryaxisgrid grid_paramsspines set_position set_color)vectorstailsnvectorsntailsheadslimitr>r+axisxticksyticksdxdyrAlocs r6 plot_vectorrp0s2kk'"G == q G"GG   E"{{1~"G$GG"  )}}QH [[^F1}!**Wvqk2 1A 58Q-0{{gmm+V-VV+ GOE IIeii eU^ <= >E JJus{ #Ekk1Q% 9,G??73LFDKKac E!A#J! gacl(4q:MME65/"MME65/"OOG__ F __ F VAY B VAY B s3r2; #D  d +CJJ  %JJ  % DII"k" KK$$X.KK&&x0KK""6*KK  (r8TF unit_vector unit_circletitlec|jdk(sJdd}tj| |dz}tj||\}} |dddf} |dddf} | d|z| d| zz} | d|z| d| zz} tjd}t |j D]Z}|j| |ddf| |ddffdtit|j| dd|f| dd|ffdtit\|rP|j||| dg| dgfdtit|j||| dg| dgfdtittt t"t$t&t(g}|rK|D]F}t+|}||j-d dz}|j|||dg|dgfd|itH|rtj.dd tj0zd }tj2tj4|tj6|f}||z}|j|d|dtd d}|j8dj;d|j8dj;d|j8dj=d|j8dj=d|j8dj?d|j8dj?d|jA| |g|jC| |g||jE|yy)a A helper function to plot the linear transformation defined by a 2x2 matrix. Parameters ---------- axis : class matplotlib.axes.Axes. The axes to plot on. matrix : class numpy.ndarray. The 2x2 matrix to visualize. *vectors : class numpy.ndarray. The vector(s) to plot along with the linear transformation. Each array denotes a vector's coordinates before the transformation and must have a shape of (2,). Accept any number of vectors. unit_vector : bool, optional. Whether to plot unit vectors of the standard basis, default to True. unit_circle: bool, optional. Whether to plot unit circle, default to False. title: str, optional. Title of the plot. r<r<z+the input matrix must have a shape of (2,2)rNrcr?r<)r"r?lwrrBrCrDg333333?rErFrG)#rJrHarangemeshgridzerosranger*plotgoldr` lightbluerRgreen quiver_paramsredr pinkrSorangepurplebrownnextreshapelinspacepivstackcossinrarb set_linewidthrcrTrU set_title)rjmatrixrrrsrtrd grid_rangexX_Y_IJXYorigini color_cyclevectorr?vector_r$circle circle_transris r6plot_transformation_helperrys4 <<5 O"OO J j[*Q,/A ^^Aa FBqs Aqs A !R!A$r'A !R!A$r'A [[^F166]> !AaC&!AaC&8D8K8 !AaC&!AaC&=I==>  FFQqTFQqTFQ%Q=Q FFQqTFQqTFO#OOx?@K bF%Ev~~b33G DKK wqzl a% aS` a b 1UXX:r2uyy/51ABC  ,q/<?## F EKK$$X.KK&&x0KK%%c*KK'',KK""6*KK  (MME65/"MME65/"  ur8)rrrsctjddgtz}tjdd|\}\}}t |tj dg|||ddt ||g|||ddy) a Plot the linear transformation defined by a 2x2 matrix using the helper function plot_transformation_helper(). It will create 2 subplots to visualize some vectors before and after the transformation. Parameters ---------- matrix : class numpy.ndarray. The 2x2 matrix to visualize. *vectors : class numpy.ndarray. The vector(s) to plot along with the linear transformation. Each array denotes a vector's coordinates before the transformation and must have a shape of (2,). Accept any number of vectors. unit_vector : bool, optional. Whether to plot unit vectors of the standard basis, default to True. unit_circle: bool, optional. Whether to plot unit circle, default to False. rr<rr=Before transformationrqzAfter transformationN)rHrIrPrrQridentity)rrrrsrdr>r+axis1axis2s r6plot_linear_transformationrs,kk1Q% 9,G#__Q7CFNUEuennQ&7T'T{hs|STufHwHK]hqGHr8ct|dz}d}t||z }tjd|zd|zgtz}t j |||\}}t|D]m} | dk(rtjd} d} n3|| dz  z} | dk(rdj| } ndj| } t|| |z| |zf| ||| o||z|kDr|d jd y y ) aJ Plot the linear transformation defined by a sequence of n 2x2 matrices using the helper function plot_transformation_helper(). It will create n+1 subplots to visualize some vectors before and after each transformation. Parameters ---------- *matrices : class numpy.ndarray. The 2x2 matrices to visualize. Accept any number of matrices. unit_vector : bool, optional. Whether to plot unit vectors of the standard basis, default to True. unit_circle: bool, optional. Whether to plot unit circle, default to False. rr<r=rrAfter {} transformationAfter {} transformationsrq)ryryoffN) lenrrHrIrPrrQrrformatrrj) rrrsmatricesnplotsnxnyr>r+r.r matrix_transrts r6plot_linear_transformationsrs$]Q F B fRiBkk1R42,')3G??2r7;LFD 6] C 6 >>!,L+E#AaC=<7LAv188;299!<"42qt #4lP[it}B C C "uv~ U r8c 4|jdk(sJdd\}}}d}|rCd} tj| | dz} tj| | | \} } } |d| z|d| zz|d | zz}|d | z|d | zz|d | zz}|d | z|d| zz|d| zz}t | j D]}t | j D]}|j |dd||f|dd||f|dd||f|||j ||dd|f||dd|f||dd|f|||j |||ddf|||ddf|||ddf|||rtjddtjzd}tjdtjd}dtjtj|tj|z} dtjtj|tj|z} dtjtjtj |tj|z} |d| z|d| zz|d | zz}|d | z|d | zz|d | zz}|d | z|d| zz|d| zz}|j|||ddddd||j|d}|s|r?fD]6}tjtj |}t||}8nd}|j#| ||j%| ||j'| |dD]}|j)|dy)a A helper function to plot the linear transformation defined by a 3x3 matrix. Parameters ---------- axis : class matplotlib.axes.Axes. The axes to plot on. matrix : class numpy.ndarray. The 3x3 matrix to visualize. grid : bool, optional. Whether to plot 3d grid lines, default to True. unit_sphere : bool, optional. Whether to plot unit sphere, default to False. title : str, optional. Title of the plot. )rz+the input matrix must have a shape of (3,3))z#0084b6z#d8a322z#FF3333ffffff?r<rrrrr)rr<rrrr)rr<)r<r)r<rrvN)r?r#rdroceang333333?)rstridecstrider#cmapr$)ryz)rjpad)rJrHr}r~rr*rrrouterrrones plot_surfacerrMrNrTrUset_zlim tick_params)rjrr_ unit_spherertxcolorycolorzcolorr#rrrrZX_newY_newZ_newrjuvrirIlimit_axis_strs r6plot_3d_transformation_helperrs, <<5 O"OO <FFFI  LL*jl 3..1Q'1as A s A -s A =s A s A -s A =s A s A -s A =qvv gA166] g %!A,a!e eAaEl&\e f %!A,a!e eAaEl&\e f %!A,a!e eAaEl&\e f g g  NN1a%((lC 0 NN1ehh ,  EIIaL%))A,7 7  EIIaL%))A,7 7  EJJuzz!}5uyy|D Ds A s A -s A =s A s A -s A =s A s A -s A = %q!qW^fij  u E {UE* 'EYYuyy/0Fv&E 'MM5&% MM5&% MM5&% #0 hB/0r8c&tjddgtz}tj|}|j dddd}|j dddd}t |tjd||d t ||||d y ) a Plot the linear transformation defined by a 3x3 matrix using the helper function plot_3d_transformation_helper(). It will create 2 subplots to visualize some vectors before and after the transformation. Parameters ---------- matrix : class numpy.ndarray. The 3x3 matrix to visualize. grid : bool, optional. Whether to plot 3d grid lines, default to True. unit_sphere : bool, optional. Whether to plot unit sphere, default to False. rr<r=r3d projectionrzbefore transformationr_rrtzafter transformationN)rHrIrPrr+ add_subplotrr)rr_rr>r+rrs r6plot_3d_linear_transformationrGs&kk1Q% 9,G ]]7 +F   q!Q4  8E   q!Q4  8E!%):S^f}~!%d [qrr8)r_rct|dz}d}t||z }tjd|zd|zgtz}t j |}t|D]z}|j|||dzd} |dk(rtjd} d} n3||dz  z} |dk(rd j|} nd j|} t| | ||| |y ) a. Plot the linear transformation defined by a sequence of n 3x3 matrices using the helper function plot_3d_transformation_helper(). It will create n+1 subplots to visualize some vectors before and after each transformation. Parameters ---------- *matrices : class numpy.ndarray. The 3x3 matrices to visualize. Accept any number of matrices. grid : bool, optional. Whether to plot 3d grid lines, default to False. unit_sphere : bool, optional. Whether to plot unit sphere, default to False. rr<r=rrrrrrrrN) rrrHrIrPrr+rrrrr) r_rrrrrr>r+rrjrrts r6plot_3d_linear_transformationsras$]Q F B fRiBkk1R42,')3G ]]7 +F 6] k!!"b!A#$!? 6 >>!,L+E#AaC=<7LAv188;299!<%dLtQ\dij kr8rrrtI_labelJ_labelc td}tj| |dz}tj||\} } |d| z|d| zz} |d| z|d| zz} tjd} |j | | ddd}t |j D]P}|j| |ddf| |ddft| |j| dd|f| dd|ft| Rd }tj|dk(dd}|j| |ddf| |ddft| |j| dd|f| dd|ft| |j| | |dg|dgfd tit|j| | |dg|dgfd titd }|r|D]}tjtj||f}||j!d dz}|j| | |dg|dgfd t"itt%t'tj$tj(||}|j+| |g|j-| |g|j/d||j1|dddd}|j2|d|dz dz dz|d|dz dz dzdj5|fd ti||j2|d|dz dz dz|d|dz dz dzdj5|fd ti|y)a A helper function to plot the 2D coordinate system determined by the basis I,J. Parameters ---------- axis : class matplotlib.axes.Axes. The axes to plot on. I, J: class numpy.ndarray. The coordinates of the basis vector in the standard basis. *vectors : class numpy.ndarray. The vector(s) to plot along with the change of basis. Each array denotes a vector's coordinates in I-J coordinate system (not in the standard basis). Each vector must have a shape of (2,). Accept any number of vectors. I_label, J_label : str, optional. Label of the new basis, default to 'i' and 'j'. title: str, optional. Title of the plot. rwrrblackr)rxsg?N)rxr|rr?rryrrC)havar*r<g?z${}$)rHr}r~rscatterrr*rgreywhererrrRr transposerrrrMrrNrTrUrjrtextr)rjrrrtrrrdrrrrrrrlw_gridrlw_spinezero_idboundrM text_paramss r6plot_basis_helperrs=0J j[*Q,/A ^^Aa FB !R!A$r'A !R!A$r'A[[^FLL7aL0G 166]6 !AaC&!AaC&DW 5 !AaC&!AaC&DW 56 Hkk!Q$"1%GIIa lAgaiLDXI>IIa' lAaiLI(ICDKK11LdLmLDKK11QiQ=Q E CF aU 34A1--F DKK fQi[ ] ]} ]UYYuyy'89:EBE  C MME65/"MME65/"IIe  u"A>K DIIqtAaDy!mC!A$qt)Qs!2GNN74KgSWg[fg DIIqtAaDy!mC!A$qt)Qs!2GNN74KlS\l`klr8ctjddgtz}tj|\}}t |||g|y)a Plot 2d vectors on the coordinates system defined by basis I and J using the helper funtion plot_basis_helper(). Parameters ---------- I, J: class numpy.ndarray. The coordinates of the basis vector in the standard basis. *vectors : class numpy.ndarray. The vector(s) to plot along with the change of basis. Each array denotes a vector's coordinates in I-J coordinate system (not in the standard basis). Each vector must have a shape of (2,). Accept any number of vectors. r<r=N)rHrIrPrrQr)rrrdr>r+rjs r6 plot_basisrs> kk1Q% 9,G??73LFDdAq+7+r8ctjddgtz}tjdd|\}\}}tj tj ||f}t|}|D cgc]} || jddz} } t|tjddgtjddgg|ddit|||g| d d d d y cc} w)aE Create a side-by-side plot of some vectors both on the standard basis and on the new basis defined by I and J, using the helper function plot_basis_helper(). Parameters ---------- I, J: class numpy.ndarray. The coordinates of the basis vector in the standard basis. *vectors : class numpy.ndarray. The vector(s) to plot along with the change of basis. Each array denotes a vector's coordinates in I-J coordinate system (not in the standard basis). Each vector must have a shape of (2,). Accept any number of vectors. rr<rr=ryrrtzstandard basisz new basisabrN) rHrIrPrrQrrrrr) rrrdr>r+rrrM_invrvectors_s r6plot_change_basisrs kk1Q% 9,G#__Q7CFNUE  aU+,A FE=DF6A..FHFeU[[!A/aU1CfgfUefeQZHZKVYZGs6C#ctjddgtz}tjdd|\}}t |\}}|}tj |}t|}tjddtjzd} tjtj| tj| f} t|dtjddgtjddgd t|d |d d df|d d dfd d dt|d|d d df|d d dfdd dt|dtjddgtjddgd || z} |dD]!} | j| d| dtd#|dD]!} | j| d| dtd#y )a Visualize the eigendecomposition of a 2x2 matrix as a combination of changing basis and scaling transformation, using the helper function plot_basis_helper(). Parameters ---------- matrix : class numpy.ndarray. The 2x2 matrix to visualize. rr<r=rrzrrz%coords in standard basis $\mathbf{x}$)rtrNz&change to new basis $C^{-1}\mathbf{x}$rrrrz)scale along new basis $DC^{-1}\mathbf{x}$rz2change back to standard basis $CDC^{-1}\mathbf{x}$r"r{)rHrIrPrrQrdiagrrrrrrrrr) rr>r+r. eigenvalues eigenvectorsCDC_invr$rrrjs r6 plot_eigenrskk1Q% 9,G??1a9LFD #F KA ;A FE ^^Aqz2 .E \\599U+UYYu-=> ?Fd3iaU!3U[[!A5GOwxd3i1Q31Q37`jmwz{d3i1Q31Q37cmpz}~d3iaU!3U[[!A5GPEFF?LQ; &)VAYcc :;QG ,q/<?## FGr8__main__)N)TFN)TF)-rH numpy.linalgrrmathr matplotlibrrrr mpl_toolkits.mplot3dr itertoolsr _int_backends_backendr2rPrrrrrrSryellowrrrrr`r:rprrrrrrrrrr__name__r9r8r6rs !66'I  = }HIHI   &[#fffe $!   F)F)PCGUZbfGGR=AuHH47;$ $ L?0?0Bss238e"k"kH26SDmDmL,,&[[.GG@ zr8