% J. Stolfi's miscellaneous general-purpose math macros
% \newcommand{\lastedit}{2023-06-26 10:11:30 by stolfi}

\makeatletter
\advance\endlinechar by -256

% References to equations (with implicit "~" and auto parentheses):

  \let\eqref=\undef
  \newcommand{\eqref}[1]{~(\ref{#1})}

  \let\eqrefs=\undef
  \newcommand{\eqrefs}[2]{~(\ref{#1}--\ref{#2})}

% Esimo, esima, etc.

  \newcommand{\esimo}{\raisebox{.6ex}{\b{o}}$\,$}
  \newcommand{\esimos}{\raisebox{.6ex}{\b{os}}$\,$}
  \newcommand{\esima}{\raisebox{.6ex}{\b{a}}$\,$}
  \newcommand{\esimas}{\raisebox{.6ex}{\b{as}}$\,$}
  
% Units of measure

  \newcommand{\unitf}[1]{\hbox{\textrm{\textup{#1}}}}

  \newcommand{\um}{\unitf{m}}
  \newcommand{\ucm}{\unitf{cm}}
  \newcommand{\umm}{\unitf{mm}}
  \newcommand{\uum}{\unitf{$\mu$m}}
  \newcommand{\unm}{\unitf{nm}}
  \newcommand{\upm}{\unitf{pm}}
  \newcommand{\ukm}{\unitf{km}}
  
  \newcommand{\us}{\unitf{s}}
  \newcommand{\ums}{\unitf{ms}}
  \newcommand{\uus}{\unitf{$\mu$s}}
  \newcommand{\uns}{\unitf{ns}}

  \newcommand{\ukiB}{\unitf{kiB}}
  \newcommand{\uMiB}{\unitf{MiB}}
  \newcommand{\uGiB}{\unitf{GiB}}
  
  \newcommand{\ug}{\unitf{g}}
  \newcommand{\ukg}{\unitf{kg}}
  \newcommand{\umg}{\unitf{mg}} 
  \newcommand{\uug}{\unitf{$\mu$g}}
  
  \newcommand{\uV}{\unitf{V}}
  \newcommand{\umV}{\unitf{mV}}
  \newcommand{\uuV}{\unitf{$\mu$V}}
  
  \newcommand{\uA}{\unitf{A}}
  \newcommand{\umA}{\unitf{mA}}
  \newcommand{\uuA}{\unitf{$\mu$A}}
  
  \newcommand{\uW}{\unitf{W}}
  \newcommand{\umW}{\unitf{mW}}
  \newcommand{\uuW}{\unitf{$\mu$W}}
  \newcommand{\ukW}{\unitf{kW}}
  \newcommand{\uMW}{\unitf{MW}}
  \newcommand{\uGW}{\unitf{GW}}
  \newcommand{\uTW}{\unitf{TW}}
  
  \newcommand{\uWh}{\unitf{Wh}}
  \newcommand{\ukWh}{\unitf{kWh}}
  \newcommand{\uMWh}{\unitf{MWh}}
  \newcommand{\uGWh}{\unitf{GWh}}
  \newcommand{\uTWh}{\unitf{TWh}}

  \newcommand{\uJ}{\unitf{J}}
  \newcommand{\ukJ}{\unitf{kJ}}
  \newcommand{\uMJ}{\unitf{MJ}}
  \newcommand{\uGJ}{\unitf{GJ}}
  \newcommand{\uTJ}{\unitf{TJ}}

  \newcommand{\uF}{\unitf{F}} 
  \newcommand{\umF}{\unitf{mF}}
  \newcommand{\uuF}{\unitf{$\mu$F}}
  \newcommand{\unF}{\unitf{nF}}
  \newcommand{\upF}{\unitf{pF}}
  
  \newcommand{\uHz}{\unitf{Hz}}
  \newcommand{\ukHz}{\unitf{kHz}}
  \newcommand{\uMHz}{\unitf{MHz}}
  \newcommand{\uGHz}{\unitf{GHz}}

% Currency

  \newcommand{\BRS}{\hbox{\textrm{\textup{R}{\$}}}}
  \newcommand{\USD}{\hbox{\textrm{\textup{US}{\$}}}}
  \newcommand{\CHF}{\hbox{\textrm{\textup{CHF}}}}
  \newcommand{\EUR}{\hbox{\small\rm\raisebox{0.35ex}{\hbox to 2pt{\scriptsize\rm$=$\hss}}\hbox{\textsf{C}}}}

% Mathematics

  % Overlapping math symbols 
  % Usage: \ctrlap{+}{\bullet}{1en}
  
  \newcommand{\ctrlap}[3]{
    \hbox to #3{
      \hss
      \hbox to 0pt{\hss${#1}$\hss}
      \hbox to 0pt{\hss${#2}$\hss}
      \hss
    }
  }
  
  % Operator within a box
  \newcommand{\boxop}[1]{
    % The following must be inside a group to restore the boxes.
    {
      \setbox6\hbox{$#1$}
      \dimen6=\ht6 \advance\dimen6 by\dp6
      \dimen4=\wd6
      \ifdim\dimen4>\dimen6 \dimen6=\dimen4\fi
      \dimen4=\dimen6 \advance\dimen4 by -\dp6
      \hbox{
        \setlength{\fboxsep}{0.5pt}
        \fbox{
          \hbox to \dimen6{
            \vrule width 0pt height \dimen4 depth \dp6
            \hss
            \hbox to 0pt{\hss \box6 \hss}
            \hss
          }
        }
      }
    }
  }

  % Boldface letters:
  \newcommand{\BA}{\mathbf{A}}
  \newcommand{\BB}{\mathbf{B}}
  \newcommand{\BC}{\mathbf{C}}
  \newcommand{\BD}{\mathbf{D}}
  \newcommand{\BE}{\mathbf{E}}
  \newcommand{\BF}{\mathbf{F}}
  \newcommand{\BG}{\mathbf{G}}
  \newcommand{\BH}{\mathbf{H}}
  \newcommand{\BI}{\mathbf{I}}
  \newcommand{\BJ}{\mathbf{J}}
  \newcommand{\BK}{\mathbf{K}}
  \newcommand{\BL}{\mathbf{L}}
  \newcommand{\BM}{\mathbf{M}}
  \newcommand{\BN}{\mathbf{N}}
  \newcommand{\BO}{\mathbf{O}}
  \newcommand{\BP}{\mathbf{P}}
  \newcommand{\BQ}{\mathbf{Q}}
  \newcommand{\BR}{\mathbf{R}}
  \newcommand{\BS}{\mathbf{S}}
  \newcommand{\BT}{\mathbf{T}}
  \newcommand{\BU}{\mathbf{U}}
  \newcommand{\BV}{\mathbf{V}}
  \newcommand{\BW}{\mathbf{W}}
  \newcommand{\BX}{\mathbf{X}}
  \newcommand{\BY}{\mathbf{Y}}
  \newcommand{\BZ}{\mathbf{Z}}
  \newcommand{\Ba}{\mathbf{a}}
  \newcommand{\Bb}{\mathbf{b}}
  \newcommand{\Bc}{\mathbf{c}}
  \newcommand{\Bd}{\mathbf{d}}
  \newcommand{\Be}{\mathbf{e}}
  \newcommand{\Bf}{\mathbf{f}}
  \newcommand{\Bg}{\mathbf{g}}
  \newcommand{\Bh}{\mathbf{h}}
  \newcommand{\Bi}{\mathbf{i}}
  \newcommand{\Bj}{\mathbf{j}}
  \newcommand{\Bk}{\mathbf{k}}
  \newcommand{\Bl}{\mathbf{l}}
  \newcommand{\Bm}{\mathbf{m}}
  \newcommand{\Bn}{\mathbf{n}}
  \newcommand{\Bo}{\mathbf{o}}
  \newcommand{\Bp}{\mathbf{p}}
  \newcommand{\Bq}{\mathbf{q}}
  \newcommand{\Br}{\mathbf{r}}
  \newcommand{\Bs}{\mathbf{s}}
  \newcommand{\Bt}{\mathbf{t}}
  \newcommand{\Bu}{\mathbf{u}}
  \newcommand{\Bv}{\mathbf{v}}
  \newcommand{\Bw}{\mathbf{w}}
  \newcommand{\Bx}{\mathbf{x}}
  \newcommand{\By}{\mathbf{y}}
  \newcommand{\Bz}{\mathbf{z}}

  % Calligraphic letters
  \newcommand{\CA}{\mathcal{A}}
  \newcommand{\CB}{\mathcal{B}}
  \newcommand{\CC}{\mathcal{C}}
  \newcommand{\CD}{\mathcal{D}}
  \newcommand{\CE}{\mathcal{E}}
  \newcommand{\CF}{\mathcal{F}}
  \newcommand{\CG}{\mathcal{G}}
  \newcommand{\CH}{\mathcal{H}}
  \newcommand{\CI}{\mathcal{I}}
  \newcommand{\CJ}{\mathcal{J}}
  \newcommand{\CK}{\mathcal{K}}
  \newcommand{\CL}{\mathcal{L}}
  \newcommand{\CM}{\mathcal{M}}
  \newcommand{\CN}{\mathcal{N}}
  \newcommand{\CO}{\mathcal{O}}
  \newcommand{\CP}{\mathcal{P}}
  \newcommand{\CQ}{\mathcal{Q}}
  \newcommand{\CR}{\mathcal{R}}
  \newcommand{\CS}{\mathcal{S}}
  \newcommand{\CT}{\mathcal{T}}
  \newcommand{\CU}{\mathcal{U}}
  \newcommand{\CV}{\mathcal{V}}
  \newcommand{\CW}{\mathcal{W}}
  \newcommand{\CX}{\mathcal{X}}
  \newcommand{\CY}{\mathcal{Y}}
  \newcommand{\CZ}{\mathcal{Z}}

  % Roman letters
  \newcommand{\MA}{\mathrm{A}}
  \newcommand{\MB}{\mathrm{B}}
  \newcommand{\MC}{\mathrm{C}}
  \newcommand{\MD}{\mathrm{D}}
  \newcommand{\ME}{\mathrm{E}}
  \newcommand{\MF}{\mathrm{F}}
  \newcommand{\MG}{\mathrm{G}}
  \newcommand{\MH}{\mathrm{H}}
  \newcommand{\MI}{\mathrm{I}}
  \newcommand{\MJ}{\mathrm{J}}
  \newcommand{\MK}{\mathrm{K}}
  \newcommand{\ML}{\mathrm{L}}
  \newcommand{\MM}{\mathrm{M}}
  \newcommand{\MN}{\mathrm{N}}
  \newcommand{\MO}{\mathrm{O}}
  \newcommand{\MP}{\mathrm{P}}
  \newcommand{\MQ}{\mathrm{Q}}
  \newcommand{\MR}{\mathrm{R}}
  \newcommand{\MS}{\mathrm{S}}
  \newcommand{\MT}{\mathrm{T}}
  \newcommand{\MU}{\mathrm{U}}
  \newcommand{\MV}{\mathrm{V}}
  \newcommand{\MW}{\mathrm{W}}
  \newcommand{\MX}{\mathrm{X}}
  \newcommand{\MY}{\mathrm{Y}}
  \newcommand{\MZ}{\mathrm{Z}}

  % Blackboard letters

  \newcommand{\RA}{\mathbb{A}}
  \newcommand{\RB}{\mathbb{B}}
  \newcommand{\RC}{\mathbb{C}}
  \newcommand{\RD}{\mathbb{D}}
  \newcommand{\RE}{\mathbb{E}}
  \newcommand{\RF}{\mathbb{F}}
  \newcommand{\RG}{\mathbb{G}}
  \newcommand{\RH}{\mathbb{H}}
  \newcommand{\RI}{\mathbb{I}}
  \newcommand{\RJ}{\mathbb{J}}
  \newcommand{\RK}{\mathbb{K}}
  \newcommand{\RL}{\mathbb{L}}
  \newcommand{\RM}{\mathbb{M}}
  \newcommand{\RN}{\mathbb{N}}
  \newcommand{\RO}{\mathbb{O}}
  \newcommand{\RP}{\mathbb{P}}
  \newcommand{\RQ}{\mathbb{Q}}
  \newcommand{\RR}{\mathbb{R}}
  \newcommand{\RS}{\mathbb{S}}
  \newcommand{\RT}{\mathbb{T}}
  \newcommand{\RU}{\mathbb{U}}
  \newcommand{\RV}{\mathbb{V}}
  \newcommand{\RW}{\mathbb{W}}
  \newcommand{\RX}{\mathbb{X}}
  \newcommand{\RY}{\mathbb{Y}}
  \newcommand{\RZ}{\mathbb{Z}}

  % Formats procedure and variable names:
  \newcommand{\vf}[1]{{\textit{#1}}}

  % Formats program keywords:
  \newcommand{\kw}[1]{{\textit{\textbf{#1}}}}

  % Username in internet:
  \newcommand{\uname}[1]{\texttt{@#1}}

  % Short names for Greek letters
  \newcommand{\alf}{\alpha}
  \newcommand{\bet}{\beta}
  \newcommand{\gam}{\gamma}
  \newcommand{\del}{\delta}
  \newcommand{\eps}{\varepsilon}
  \newcommand{\zet}{\zeta}
  \newcommand{\tht}{\theta}
  \newcommand{\kap}{\kappa}
  \newcommand{\lam}{\lambda}
  \newcommand{\sig}{\sigma}
  \newcommand{\ups}{\upsilon}
  
  % Logic
  \let\implies=\undef
  \newcommand{\implies}{\Rightarrow}
  \let\iff=\undef
  \newcommand{\iff}{\Leftrightarrow}
  \let\land=\undef
  \newcommand{\land}{\wedge}
  \let\lor=\undef
  \newcommand{\lor}{\vee}
  
  % Probability
  \let\Pr=\undef
  \newcommand{\Pr}{\mathop{\mathrm{Pr}}}
  \newcommand{\given}{\mathrel{|}}
  \newcommand{\PR}[1]{\Pr(#1)}
  \newcommand{\CPR}[2]{\Pr(#1\given#2)}
  
  % Average and variance of a sample or random variable:
  \newcommand{\Avg}{\mathop{\CE}}
  \newcommand{\Var}{\mathop{\CV}}

  % Geometry and linear algebra
  \newcommand{\dist}{\mathop{\mathrm{dist}}}
  \newcommand{\rank}{\mathop{\mathrm{rank}}}
  \newcommand{\corank}{\mathop{\mathrm{corank}}}
  \let\det=\undef
  \newcommand{\det}{\mathop{\mathrm{det}}}
  \let\dim=\undef % Defined somewhere
  \newcommand{\dim}{\mathop{\mathrm{dim}}}
  \newcommand{\dir}{\mathop{\mathrm{dir}}}
  % Reversal, inverse, etc:
  \newcommand{\rev}[1]{{\mkern-0.7\thinmuskip\overleftarrow{#1}\mkern-0.7\thinmuskip}}
  
  % Assignments 
  \let\gets=\undef
  \newcommand{\gets}{\mathrel{\leftarrow}}
  \newcommand{\swap}{\mathrel{\leftrightarrow}}

  % Short ellipses, and span for discrete intervals, eg  [0..1]
  \newcommand{\dd}{\mathpunct{..}}
  \newcommand{\cdd}{\mathpunct{\cdot\cdot}}
  \newcommand{\vdd}{\vbox{\baselineskip 4pt\lineskiplimit 0pt\kern 6pt\hbox{.}\hbox{.}}}
  
  % Span for continuous intervals
  \newcommand{\spn}{\mathbin{\_}}
  % \newcommand{\spn}{,}

  % Floor and ceiling
  \newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
  \newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}

  % Broken brackets
  \let\ra=\undef % Defined in amsfont/latexsym/amssymb.
  \newcommand{\la}{\langle}
  \newcommand{\ra}{\rangle}

  % Absolute value '|...|', norm '||...||', pnorm ':|...|:'
  \newcommand{\abs}[1]{\left|#1\right|}
  \newcommand{\norm}[1]{\left\|#1\right\|}
  \newcommand{\pnorm}[1]{\mathopen{{:}\!|}#1\mathclose{|\!{:}}}

  % Variants of the above with fixed-size delimiters, for summations &c:
  \newcommand{\absgg}[1]{\biggl|#1\biggr|}
  \newcommand{\normgg}[1]{\biggl\|#1\biggr\|}
  \newcommand{\pnormgg}[1]{\mathopen{{:}\!{\biggl|}}#1\mathclose{{\biggr|}\!{:}}}
  
  % Sign function
  \newcommand{\sgn}{\mathop{\mathrm{sgn}}}

  % Explicit power
  \newcommand{\pow}[2]{{#1\uparrow#2}}
  
  % Scalar products
  \newcommand{\dprod}[2]{\la#1|#2\ra}
  \newcommand{\dotpr}{\mathbin{\boldsymbol{\cdot}}}
  
  % Convolution
  \newcommand{\conv}{\mathbin{\ast}}

  % Discrete convolution
  \newcommand{\dconv}{\circledast} 
  
  % Dirac pulse train and its Fourier transform
  \newcommand{\trdir}{\mathop{{\sqcup}\kern-1.0ex{\sqcup}}} 
  \newcommand{\Trdir}{\mathop{{\textstyle\bigsqcup}\kern-1.2ex{\textstyle\bigsqcup}}}
  
  % Triangular pulse train and its Fourier series:
  \newcommand{\trtri}{\mathop{{\wedge}\kern-0.2ex{\wedge}}} 
  \newcommand{\Trtri}{\mathop{{\Lambda}\kern-0.2ex{\Lambda}}} 
  
  % Unit rectangular and triangular pulses:
  \newcommand{\recpul}{\mathop{\sqcap}} 
  \newcommand{\tripul}{\mathop{\wedge}} 
  
  % Sinc pulse:
  \newcommand{\sinc}{\mathop{\mathrm{sinc}}} 

  % diamond with dot inside:
  \newcommand{\dotdiam}{\ctrlap{\cdot}{\diamond}{1em}}
  
  % Function arrows
  \newcommand{\fnto}{\rightarrow}
  
  % Divergent and gradient
  \newcommand{\divg}{\mathop{\mathrm{div}}}
  \newcommand{\grad}{\mathop{\mathrm{grad}}}

  % Real and imaginary parts, imaginary unit:
  \renewcommand{\Re}{\mathop{\mathrm{Re}}}
  \renewcommand{\Im}{\mathop{\mathrm{Im}}}
  \newcommand{\img}{\mathbf{i}}
  
  % Infinity
  \newcommand{\oo}{\infty}
  
  % The zero vector
  \newcommand{\zn}{\mathbf{0}}
  
  % Newton's differential symbol
  \newcommand{\dif}{\mathrm{d}}
  
  % Partial derivative, display style and text style:
  \newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}}
  \newcommand{\pderts}[2]{{{\partial #1}/{\partial #2}}}
  
  % Time derivative as a dot superscript:
  \newcommand{\tdif}[1]{{#1}^{\scriptscriptstyle\bullet}}
  \newcommand{\tddif}[1]{{#1}^{\scriptscriptstyle\bullet\bullet}}
  
  % Function continuity classes $\CONT{3}$
  \newcommand{\CONT}[1]{\mathsf{C}_{#1}}
  
  % Combinations
  \newcommand{\comb}[2]{{{#1}\choose{#2}}}
  % Note: some old files may require \let\comb=\choose

  % Sets and sequences
  \newcommand{\card}{\#}
  \newcommand{\set}[1]{\left\lbrace #1 \right\rbrace}
  \newcommand{\seq}[1]{\left\langle #1 \right\rangle}
  \newcommand{\rset}[1]{\left\lbrace\, #1 \,\right\rbrace}
  \newcommand{\rseq}[1]{\left\langle\, #1 \,\right\rangle}
  \newcommand{\ety}{\set{}}
  \newcommand{\suchthat}{\mathrel{:}}
  \newcommand{\such}{\suchthat}
  \newcommand{\st}{\suchthat}
  \newcommand{\inter}{\cap}
  \newcommand{\union}{\cup}

  % Variants of the above with fixed-size delimiters, for summations &c:
  \newcommand{\setgg}[1]{\biggl\lbrace#1\biggr\rbrace}
  \newcommand{\rsetgg}[1]{\biggl\lbrace\, #1 \,\biggr\rbrace}
  
  % Formats subscripts and superscripts in small sans-serif: 
  \newcommand{\sfu}[1]{\hbox{\scalefont{0.45}\textsf{#1}}}
  \newcommand{\sfb}[1]{\raisebox{0.20ex}{\sfu{#1}}}

  % Parenthesized superscripts:
  \newcommand{\pp}[1]{^{(#1)}}

  % Domain, restriction and clipping
  \newcommand{\dom}{\mathop{\mathrm{dom}}}
  \newcommand{\clip}{\mathop{\mathrm{clip}}}
  \newcommand{\restr}{\mathbin{|}}

  % Topological closure, interior, and boundary of set:
  \newcommand{\cls}[1]{{{#1}^\bullet}}
  \newcommand{\itr}[1]{{{#1}^\ast}}
  \newcommand{\bdr}[1]{{{#1}^\circ}}

  % Abbreviations:
  \newcommand{\nn}{\nonumber}
  \newcommand{\ph}{\phantom}
  \newcommand{\tsty}{\textstyle}
  \newcommand{\dsty}{\displaystyle}
  \newcommand{\mcol}{\multicolumn}

  % Graphs and relations:
  \newcommand{\Vg}{\mathop{\CV}}
  \newcommand{\Eg}{\mathop{\CE}}

\advance\endlinechar by 256
\makeatother

% END

