
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Misc Stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand{\R}{\mathbb{R}}
\renewcommand{\Z}{\mathbb{Z}}

%\newcommand{\ceil}[1]{ \left\lceil #1 \right\rceil }
%\newcommand{\floor}[1]{ \left\lfloor #1 \right\rfloor }
\newcommand{\expt}[2] { \left( #1 \right)^{#2} }
\newcommand{\st}{\colon\,}
\newcommand{\arrows}[1] {\stackrel{#1}{\to}}
\newcommand{\sto}{\arrows{s}}
\newcommand{\ps}{r}
\newcommand{\dist}{\mathrm{dist}}
%\newcommand{\CS}{\mathcal{S}}

\def\etal{\emph{et~al.}}

\newcommand{\red}{{\color{red} red}}
\newcommand{\blue}{{\color{blue} blue}}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tikz stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzstyle vertex=[circle,fill=black!35,inner sep=1pt]
\tikzstyle medium vertex=[vertex,minimum size=4pt]
\tikzstyle fat vertex=[vertex,minimum size=2.5ex]
\tikzstyle print vertex=[circle,inner sep=1pt, minimum size=3.5ex, draw]
%\tikzstyle selected vertex=[vertex, fill=orange!85]
\tikzstyle edge=[draw,line width=1pt]
%\tikzstyle redge=[edge,color=red]
%\tikzstyle bedge=[edge,color=blue]
%\tikzstyle selected edge=[edge,very thick,color=green!40!black]
\tikzstyle selected edge=[edge,line width=2.25pt]
%\tikzstyle directed edge=[edge,->]
%\tikzstyle walk=[edge,rounded corners=10pt,densely dotted]
%\tikzstyle walk=[edge,rounded corners=10pt]
%\tikzstyle directed walk=[walk,->]

%\tikzstyle selected edge=[line width=3pt,->]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Meta Routines: display a tikzpicture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\disptikztwice{global args}{tikz text}
\newcommand{\disptikztwice}[2]%
{%
\begin{center}
\hspace{2cm}
\begin{tikzpicture}[#1]
	#2
\end{tikzpicture}
\hfill
\begin{tikzpicture}[#1]
	#2
\end{tikzpicture}
\hspace{2cm}~
\end{center}
}

%\disptikzonce{global args}{tikz text}
\newcommand{\disptikzonce}[2]%
{
\begin{center}
\hfill
\begin{tikzpicture}[#1]
	#2
\end{tikzpicture}
\hfill~
\end{center}
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Basic Function Library
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\tmpL
\newcount\tmpLA
\newcount\tmpLB
\newcount\outL
\newcommand{\addL}[2]
{
	\outL=#1
	\tmpL=#2
	\advance\outL by\tmpL
}

%modular additioncolor=\c,
%\addmodL{a}{b}{modulus},
% where a and b are assumed to be in range 1..modulus
% \outL will also be in the same range
\newcommand{\addmodL}[3]
{
	\addL{#1}{#2}

	\ifnum\outL>#3 
		\advance\outL by-#3
	\fi
}

\newcommand{\mulL}[2]
{
	\outL=#1
	\tmpL=#2
	\multiply\outL by\tmpL
}
\newcommand{\divL}[2]
{
	\outL=#1
	\tmpL=#2
	\divide\outL by\tmpL
}

\newcommand{\getangleL}[2]
{
	\mulL{360}{#1}
	\tmpLA=\the\outL
	\divL{\the\tmpLA}{#2}
}

\newcount\out
\newcount\tmp
\newcommand{\add}[2]
{
	\out=#1
	\tmp=#2
	\advance\out by\tmp
}

%\newcount\auxA
%\newcount\auxB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Drawing library
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\withrandomcolor}[1]
{
	\pgfmathrandominteger{\c}{0}{1}

	\ifnum \c=0
		\begin{scope}[color=red]
			#1
		\end{scope}
	\else
		\begin{scope}[color=blue]
			#1
		\end{scope}
	\fi
}

\newcommand{\drawpath}[2]
{
	\foreach \i in {1, ..., #2}
	{
		\node[vertex] at (\i, 0) (#1\i) {} ;
	}
	
	\foreach \i in {2, ..., #2}
	{
		\addL{\i}{-1}
		\draw (#1\the\outL) -- (#1\i) ;
	}
}

%\plotcircleverts{name}{num-verts}{radius}
\newcommand{\plotcircleverts}[3]
{
	\foreach \i in {1, ..., #2}
	{
		\getangleL{\i}{#2}
		\node[medium vertex] at (\the\outL:#3) (#1\i) {} ;
	}
}

%\plotcirclecoords{name}{num-verts}{radius}
\newcommand{\plotcirclecoords}[3]
{
	\foreach \i in {1, ..., #2}
	{
		\getangleL{\i}{#2}
		\node[coordinate] at (\the\outL:#3) (#1\i) {} ;
	}
}

%\drawcycle{name}{num-verts}
\newcommand{\drawcycle}[2]
{
	\foreach \i in {2, ..., #2}
	{
		\addL{\i}{-1}
		\draw (#1\i) edge (#1\the\outL) ;
	}
	
	\draw (#11) edge (#1#2) ;
}

%\drawchord{name}{num-verts}{start-vertex}{length}
\newcommand{\drawchordbylength}[4]
{
	\addmodL{#3}{#4}{#2}
	\draw (#1#3) edge[] (#1\the\outL) ;
}

%\drawedge{name}{first vert}{second vert}
\newcommand{\drawedge}[3]
{
	\draw (#1#2) edge (#1#3) ;
}

\newcommand{\plotbiclique}[2]
{
	\begin{scope}[shift={(0,-1)}]
	\foreach \i in {1, ..., #1}
	{
		\node[vertex] at (-1, \i) (X\i) {} ;
		\node[vertex] at (1, \i) (Y\i) {} ;
	}
	\end{scope}
}


%\drawbicliqueedges{V1 name}{V1 size}{V2 name}{V2 size}
\newcommand{\drawbicliqueedges}[4]
{
	\foreach \i in {1, ..., #2}
	{
		\foreach \j in {1, ..., #4}
		{
			\draw (#1\i) edge (#3\j) ;
		}
	}
}
%\drawcliqueedges{name}{num}
\newcommand{\drawcliqueedges}[2]
{
	\foreach \i in {1, ..., #2}
	{{
		\addL{\i}{0}

		\ifnum\outL=#2
		\else
			\addL{\i}{1}
			
			\foreach \j in {\the\outL, ..., #2}
			{
				\draw (#1\i) -- (#1\j) ;
			}
		\fi
	}}
}

%\drawstar{how many leaves}{root node name}
%draws a star centered above (0,0)
\newcommand{\drawstar}[2]
{
	\node[vertex] (#2) at (0,2) {} ;
	\foreach \i/\dist in {1/-2, 2/-1, 3/0, 4/2}
	{
		\path (\dist, 0) node[vertex] (L\i) {} ;
		\draw[edge] (#2) -- (L\i) ;
	}

	\node () at (1,0) {$\ldots$} ;
%	\node () at (0,-0.5) {$K_{1,#1}$} ;
			
	%\draw (#2) ++(-150:0.5cm) arc (-150:-30:0.5cm) +(0.5ex, 0) node[anchor=base west,inner sep=1pt] {$#1$} ;
}

%\drawcube{name}
\newcommand{\drawcube}[1]
{
	\node[vertex] (#1000) at (0,0) {} ;
	\node[vertex] (#1111) at (0,3) {} ;
	\foreach \x/\bits in {-1/100, 0/010, 1/001}
	{
		\node[vertex] (#1\bits) at (\x, 1) {} ;
		\draw[edge] (#1000) -- (#1\bits) ;
	}

	\foreach \x/\bits in {-1/110, 0/101, 1/011}
	{
		\node[vertex] (#1\bits) at (\x, 2) {} ;
		\draw[edge] (#1111) -- (#1\bits) ;
	}

	\draw[edge] (#1100) -- (#1101) -- (#1001) -- (#1011) -- (#1010) -- (#1110) -- (#1100) ;
}

\newcommand{\drawN}%
{%
	\begin{tikzpicture}[scale=0.2,baseline=0pt]
		\begin{scope}[xscale=0.5]
			\foreach \x in {-1,1}
			{
				\node[vertex] (L\x) at (\x,0) {} ;
				\node[vertex] (H\x) at (\x,1.5) {} ;
			}
		\end{scope}

		\draw[edge] (L-1) -- (H-1) -- (L1) -- (H1) ;
	\end{tikzpicture}
}

%\drawstdex{name}{number of pairs}
\newcommand{\drawstdex}[2]
{
	\foreach \x in {1, ..., #2}
	{
		\node[vertex] (#1L\x) at (\x, 0) {} ;
		\node[vertex] (#1H\x) at (\x, 1) {} ;
	}

	\foreach \l in {1, ..., #2}
	{{
		\foreach \u in {1, ..., #2}
		{{
			\ifnum \l=\u
			\else
			{
				\draw[edge] (#1L\l) -- (#1H\u) ;
			}
			\fi
		}}
	}}
}

%\drawstdex{name}{number of pairs}
\newcommand{\drawKrr}[2]
{
	\foreach \x in {1, ..., #2}
	{
		\node[vertex] (#1L\x) at (\x, 0) {} ;
		\node[vertex] (#1H\x) at (\x, 1) {} ;
	}

	\foreach \l in {1, ..., #2}
	{{
		\foreach \u in {1, ..., #2}
		{{
			\draw[edge] (#1L\l) -- (#1H\u) ;
		}}
	}}
}

%\drawstdex{name}{number of pairs}
\newcommand{\drawPTrr}[2]
{
	\node[vertex] (#1M) at (#2/2+0.5,0.5) {} ;
	\foreach \x in {1, ..., #2}
	{
		\node[vertex] (#1L\x) at (\x, 0) {} ;
		\node[vertex] (#1H\x) at (\x, 1) {} ;

		\draw[edge] (#1L\x) -- (#1M) -- (#1H\x) ;
	}
}

\newcommand{\gausscurve}%
{
	\draw (-2, 0) -- plot[id=gauss,samples=100,domain={-2:2}] function{exp(-x*x)} -- (2, 0) ;
}

\newcommand{\drawlattice}%
{
	\begin{scope}[rotate=90,scale=0.5]
	\gausscurve
	\begin{scope}[yscale=-1]
		\gausscurve
	\end{scope}
	\end{scope}
}

%\drawlatticeAt{xscale}{xcoord}{ymin}{ymax}
\newcommand{\drawlatticeAt}[4]
{
	\begin{scope}[xscale={#1},shift={(#2, #3/2 + #4/2)},scale={#4/2 - #3/2}]
		\drawlattice
	\end{scope}
}

%\drawlatticeAt{xscale}{xcoord}{ymin}{ymax}{stuff}
\newcommand{\drawstuffAt}[5]
{
%
% Commented line has order wrong!!  The order that transforms are executed is RIGHT to LEFT
% ALSO!  Be ware, must distribute out or else the computation will fail
%
%	\begin{scope}[xscale=#1, scale={(#4 - #3)/2}, shift={(#2, (#3+#4)/2)}]
%
	\begin{scope}[xscale={#1},shift={(#2, #3/2 + #4/2)},scale={#4/2 - #3/2}]
		#5
	\end{scope}
}


%\LP{n}{level}{horizontal percent}
\newcommand{\LP}[3]
{%
%	({(2*#3 - 1)*exp(-0*0)}*(1,0) + {2*#2/(#1 + 1) - 1}*(0,1)})
	\pgfmathsetmacro{\yval}{2*#2/#1 - 1}%
	\pgfmathsetmacro{\xval}{(2*#3 - 1)*0.5*(exp(-4*\yval*\yval))}%
	%(\xval cm, \yval cm)
}

%\Lplotcoords{name}{n}{id0/level0/xpct0, id1/level1/xpct1, ...}
% Plots points on lattice
\newcommand{\Lplotcoords}[3]
{
	\foreach \id/\l/\x in {#3}
	{{
		\LP{#2}{\l}{\x}	
		\node[coordinate] at (\xval,\yval) (#1\id) {} ;
	}}
}

%\Lploteltslabs{name}{id0/label0, id1/label1, ...}{color}
% Plots points on lattice
\newcommand{\Lploteltslabs}[3]
{
	\foreach \id/\lab in {#2}
	{{
		% Replace the coordinate node with a vertex node
		\node[vertex,fill=#3] (#1\id) at (#1\id) {} ;
		% Add label text
		\node[anchor=west] at (#1\id) {\lab} ;
	}}
}

%\Lplotelts{name}{id0, id1, ...}{color}
% Plots points on lattice
\newcommand{\Lplotelts}[3]
{
	\foreach \id in {#2}
	{{
		% Replace the coordinate node with a vertex node
		\node[vertex,fill=#3] (#1\id) at (#1\id) {} ;
	}}
}


%Displays a lattice walk
%\LW{name}{level start}{level end}{slide start}
\newcommand{\LW}[4]
{
	% Should be one fewer than number of levels}
	\add{#3}{-1}
	\foreach \l in {#2, ..., \the\out}
	{{
		\add{#4}{\l}
		\add{\the\out}{-#2}
		\only<\the\out->
		{{
			\add{\l}{1}
			\draw (#1\l) -- (#1\the\out) ;	
		}}
	}}
	
}

%\drawsimpedge{name}{num a}{num b}{label}
\newcommand{\drawsimpedge}[4]
{
	\draw[edge] (#1#2) edge node[inner sep=2pt,fill=white] {#4} (#1#3) ;
}

%\drawsimpedge{name}{num a}{num b}{range}
\newcommand{\drawSimpEdgeRandomLabel}[4]
{
	\drawsimpedge{#1}{#2}{#3}{\pgfmathrandom{#4}\pgfmathresult}
}

