| 1 |
% \CheckSum{209} |
| 2 |
% \iffalse meta-comment |
| 3 |
% |
| 4 |
% trig.dtx Copyright (C) 1993 1994 1995 1996 1997 1999 David Carlisle |
| 5 |
% Inlined as fitrig.dtc 2005 Lars Hellstr"om |
| 6 |
% |
| 7 |
% This file is part of the fontinst system version 1.9. |
| 8 |
% ----------------------------------------------------- |
| 9 |
% |
| 10 |
% It may be distributed under the terms of the LaTeX Project Public |
| 11 |
% License, as described in lppl.txt in the base LaTeX distribution. |
| 12 |
% Either version 1.1 or, at your option, any later version. |
| 13 |
% |
| 14 |
%%% From file: fitrig.dtx |
| 15 |
% |
| 16 |
%<*driver> |
| 17 |
\documentclass{ltxdoc} |
| 18 |
\usepackage{fisource} |
| 19 |
\title{The \textsf{trig} package inlined into \package{fontinst}} |
| 20 |
\author{David Carlisle\\Edited by Lars Hellstr\"om} |
| 21 |
\begin{document} |
| 22 |
\maketitle |
| 23 |
\DocInput{fitrig.dtx} |
| 24 |
\end{document} |
| 25 |
%</driver> |
| 26 |
% \fi |
| 27 |
% |
| 28 |
% |
| 29 |
% The predecessor of this file is v\,1.09 of \texttt{trig.dtx}, the |
| 30 |
% source for the \package{trig} package in the standard \LaTeX\ |
| 31 |
% ``graphics'' bundle. It has been inlined into the \package{fontinst} |
| 32 |
% source mainly because archive maintainers never seem to tire of |
| 33 |
% questioning the need to provide \texttt{trig.sty} with |
| 34 |
% \package{fontinst}. |
| 35 |
% |
| 36 |
% \changes{1.930}{2005/02/06}{Inlined the \package{trig} package into |
| 37 |
% \texttt{fontinst.sty} and friends. (LH)} |
| 38 |
% |
| 39 |
% |
| 40 |
% \section{Trigonometrical functions} |
| 41 |
% |
| 42 |
% These macros implement the trigonometric functions, sin, cos and tan. |
| 43 |
% In each case two commands are defined. For instance the command |
| 44 |
% |\CalculateSin{33}| may be isued at some point, and then anywhere |
| 45 |
% later in the document, the command |\UseSin{33}| will return the |
| 46 |
% decimal expansion of $\sin(33^\circ)$. |
| 47 |
% |
| 48 |
% The arguments to these macros do not have to be whole numbers, |
| 49 |
% although in the case of whole numbers, \LaTeX\ or plain \TeX\ counters |
| 50 |
% may be used. In \TeX{}Book syntax, arguments must be of type: |
| 51 |
% \meta{optional signs}\meta{factor} |
| 52 |
% |
| 53 |
% Some other examples are:\\ |
| 54 |
% |\CalculateSin{22.5}|, |\UseTan{\int{myvar}}|, |
| 55 |
% |\UseCos{\count@}|. |
| 56 |
% |
| 57 |
% Note that unlike the psfig macros, these save all previously |
| 58 |
% computed values. This could easily be changed, but I thought that in |
| 59 |
% many applications one would want many instances of the |
| 60 |
% same value. (eg rotating all the headings of a table by the |
| 61 |
% \emph{same} amount). |
| 62 |
% |
| 63 |
% I don't really like this need to pre-calculate the values, I |
| 64 |
% originally implemented |\UseSin| so that it automatically calculated |
| 65 |
% the value if it was not pre-stored. This worked fine in testing, until |
| 66 |
% I remembered why one needs these values. You want to be able to say |
| 67 |
% |\dimen2=\UseSin{30}\dimen0|. Which means that |\UseSin| must |
| 68 |
% \emph{expand} to a \meta{factor}. |
| 69 |
% |
| 70 |
% \StopEventually{} |
| 71 |
% |
| 72 |
% |
| 73 |
% \subsection{The Macros} |
| 74 |
% |
| 75 |
% \begin{macrocode} |
| 76 |
%<*pkg> |
| 77 |
% \end{macrocode} |
| 78 |
% |
| 79 |
% \begin{macro}{\nin@ty}\begin{macro}{\@clxxx} |
| 80 |
% \changes{1.930}{2005/02/06}{Renamed this constant. There should be |
| 81 |
% three \texttt{x}s in \cs{romannumeral} 180, but \package{trig} |
| 82 |
% only had two. (LH)} |
| 83 |
% \begin{macro}{\@lxxi}\begin{macro}{\@mmmmlxviii} |
| 84 |
% Some useful constants for converting between degrees and radians. |
| 85 |
% $$ |
| 86 |
% \frac{\pi}{180}\simeq\frac{355}{113\times180}=\frac{71}{4068} |
| 87 |
% $$ |
| 88 |
% \begin{macrocode} |
| 89 |
\chardef\nin@ty=90 |
| 90 |
\chardef\@clxxx=180 |
| 91 |
\chardef\@lxxi=71 |
| 92 |
\mathchardef\@mmmmlxviii=4068 |
| 93 |
% \end{macrocode} |
| 94 |
% \end{macro}\end{macro}\end{macro}\end{macro} |
| 95 |
% |
| 96 |
% The approximation to $\sin$. I experimented with various |
| 97 |
% approximations based on Tchebicheff polynomials, and also some |
| 98 |
% approximations from a SIAM handbook `Computer Approximations' However |
| 99 |
% the standard Taylor series seems sufficiently accurate, and used by |
| 100 |
% far the fewest \TeX\ tokens, as the coefficients are all rational. |
| 101 |
% \begin{eqnarray*} |
| 102 |
% \sin(x)& \simeq& |
| 103 |
% x - (1/3!)x^3 + (1/5!)x^5 - (1/7!)x^7 + (1/9!)x^9\\ |
| 104 |
% &\simeq& |
| 105 |
% \frac{((((7!/9!x^2-7!/7!)x^2+7!/5!)x^2 +7!/3!)x^2+7!/1!)x}{7!}\\ |
| 106 |
% &=& \frac{ ((((1/72x^2-1)x^2+42)x^2 +840)x^2+5040)x }{5040} |
| 107 |
% \end{eqnarray*} |
| 108 |
% The nested form used above reduces the number of operations required. |
| 109 |
% In order to further reduce the number of operations, and more |
| 110 |
% importantly reduce the number of tokens used, we can precompute the |
| 111 |
% coefficients. Note that we cannot use $9!$ as the denominator as |
| 112 |
% this would cause overflow of \TeX's arithmetic. |
| 113 |
% |
| 114 |
% \begin{macro}{\@coeffz} |
| 115 |
% \begin{macro}{\@coeffa} |
| 116 |
% \begin{macro}{\@coeffb} |
| 117 |
% \begin{macro}{\@coeffc} |
| 118 |
% \begin{macro}{\@coeffd} |
| 119 |
% Save the coefficients as |\|(|math|)|char|s. |
| 120 |
% \begin{macrocode} |
| 121 |
\chardef\@coeffz=72 |
| 122 |
%\chardef\@coefa=1 |
| 123 |
\chardef\@coefb=42 |
| 124 |
\mathchardef\@coefc=840 |
| 125 |
\mathchardef\@coefd=5040 |
| 126 |
% \end{macrocode} |
| 127 |
% \end{macro}\end{macro}\end{macro}\end{macro}\end{macro} |
| 128 |
% |
| 129 |
% \begin{macro}{\TG@rem@pt} |
| 130 |
% The standard trick of getting a real number out of a \meta{dimen}. |
| 131 |
% This gives a maximum accuracy of approx.\ 5 decimal places, which |
| 132 |
% should be sufficient. It puts a space after the number, perhaps it |
| 133 |
% shouldn't. |
| 134 |
% \changes{1.930}{2005/02/06}{Using \cs{lose_measure}. (LH)} |
| 135 |
% \begin{macrocode} |
| 136 |
\def\TG@rem@pt#1{\expandafter\lose_measure\the#1\space} |
| 137 |
% \end{macrocode} |
| 138 |
% \end{macro} |
| 139 |
% |
| 140 |
% \begin{macro}{\TG@term} |
| 141 |
% Compute one term of the above nested series. Multiply the previous |
| 142 |
% sum by $x^2$ (stored in |\@tempb|, then add the next coefficient, |
| 143 |
% |#1|. |
| 144 |
% \begin{macrocode} |
| 145 |
\def\TG@term#1{ |
| 146 |
\dimen@\@tempb\dimen@ |
| 147 |
\advance\dimen@ #1\p@ |
| 148 |
} |
| 149 |
% \end{macrocode} |
| 150 |
% \end{macro} |
| 151 |
% |
| 152 |
% \begin{macro}{\TG@series} |
| 153 |
% Compute the above series. The value in degrees will be in |
| 154 |
% |\dimen@| before this is called. |
| 155 |
% \begin{macrocode} |
| 156 |
\def\TG@series{ |
| 157 |
\dimen@\@lxxi\dimen@ |
| 158 |
\divide \dimen@ \@mmmmlxviii |
| 159 |
% \end{macrocode} |
| 160 |
% |\dimen@| now contains the angle in radians, as a \meta{dimen}. |
| 161 |
% We need to remove the units, so store the same value as a |
| 162 |
% \meta{factor} in |\@tempa|. |
| 163 |
% \begin{macrocode} |
| 164 |
\edef\@tempa{\TG@rem@pt\dimen@} |
| 165 |
% \end{macrocode} |
| 166 |
% Now put $x^2$ in |\dimen@| and |\@tempb|. |
| 167 |
% \begin{macrocode} |
| 168 |
\dimen@\@tempa\dimen@ |
| 169 |
\edef\@tempb{\TG@rem@pt\dimen@} |
| 170 |
% \end{macrocode} |
| 171 |
% The first coefficient is $1/72$. |
| 172 |
% \begin{macrocode} |
| 173 |
\divide\dimen@\@coeffz |
| 174 |
\advance\dimen@\m@ne\p@ |
| 175 |
\TG@term\@coefb |
| 176 |
\TG@term{-\@coefc}% |
| 177 |
\TG@term\@coefd |
| 178 |
% \end{macrocode} |
| 179 |
% Now the cubic in $x^2$ is completed, so we need to multiply by |
| 180 |
% $x$ and divide by $7!$. |
| 181 |
% \begin{macrocode} |
| 182 |
\dimen@\@tempa\dimen@ |
| 183 |
\divide\dimen@ \@coefd |
| 184 |
} |
| 185 |
% \end{macrocode} |
| 186 |
% \end{macro} |
| 187 |
% |
| 188 |
% \changes{1.930}{2005/02/06}{Use \cs{x_cs} and \cs{if_undefined} where |
| 189 |
% appropriate. (LH)} |
| 190 |
% |
| 191 |
% \begin{macro}{\CalculateSin} |
| 192 |
% If this angle has already been computed, do nothing, else store |
| 193 |
% the angle, and call |\TG@@sin|. Computed sines are stored in |
| 194 |
% control sequences with names of the form |
| 195 |
% \describecsfamily{sin(\meta{number})}|\sin(|\meta{number}|)|. |
| 196 |
% \begin{macrocode} |
| 197 |
\def\CalculateSin#1{{% |
| 198 |
\if_undefined{sin(\number#1)}\then |
| 199 |
\dimen@=#1\p@ |
| 200 |
\TG@@sin |
| 201 |
\x_cs\xdef{sin(\number#1)}{\TG@rem@pt\dimen@} |
| 202 |
\fi |
| 203 |
}} |
| 204 |
% \end{macrocode} |
| 205 |
% \end{macro} |
| 206 |
% |
| 207 |
% \begin{macro}{\CalculateCos} |
| 208 |
% As above, but use the relation $\cos(x) = \sin(90-x)$. Computed |
| 209 |
% cosines are stored in control sequences with names of the form |
| 210 |
% \describecsfamily{cos(\meta{number})}|\cos(|\meta{number}|)|. |
| 211 |
% \begin{macrocode} |
| 212 |
\def\CalculateCos#1{{% |
| 213 |
\if_undefined{cos(\number#1)}\then |
| 214 |
\dimen@=\nin@ty\p@ |
| 215 |
\advance \dimen@ -#1\p@ |
| 216 |
\TG@@sin |
| 217 |
\x_cs\xdef{cos(\number#1)}{\TG@rem@pt\dimen@} |
| 218 |
\fi |
| 219 |
}} |
| 220 |
% \end{macrocode} |
| 221 |
% \end{macro} |
| 222 |
% |
| 223 |
% \begin{macro}{\TG@@sin} |
| 224 |
% Repeatedly use one of the the relatations |
| 225 |
% $\sin(x)=\sin(180-x)=\sin(-180-x)$ to get $x$ in the range |
| 226 |
% $-90 \leq x\leq 90$. Then call |\TG@series|. |
| 227 |
% \begin{macrocode} |
| 228 |
\def\TG@@sin{% |
| 229 |
\ifdim\TG@reduce>+% |
| 230 |
\else\ifdim\TG@reduce<-% |
| 231 |
\else\TG@series\fi\fi |
| 232 |
} |
| 233 |
% \end{macrocode} |
| 234 |
% \end{macro} |
| 235 |
% |
| 236 |
% \begin{macro}{\TG@reduce} |
| 237 |
% Slightly cryptic, but it seems to work\ldots\space |
| 238 |
% The first line is the condition for an |\ifdim|, the remaining |
| 239 |
% lines constitutes the `then' branch of that conditional. |
| 240 |
% \begin{macrocode} |
| 241 |
\def\TG@reduce#1#2{ |
| 242 |
\dimen@#1#2\nin@ty\p@ |
| 243 |
\advance\dimen@#2-\@clxxx\p@ |
| 244 |
\dimen@-\dimen@ |
| 245 |
\TG@@sin |
| 246 |
} |
| 247 |
% \end{macrocode} |
| 248 |
% \end{macro} |
| 249 |
% |
| 250 |
% \begin{macro}{\UseSin} |
| 251 |
% \begin{macro}{\UseCos} |
| 252 |
% Use a pre-computed value. |
| 253 |
% \begin{macrocode} |
| 254 |
\def\UseSin#1{\csname sin(\number#1)\endcsname} |
| 255 |
\def\UseCos#1{\csname cos(\number#1)\endcsname} |
| 256 |
% \end{macrocode} |
| 257 |
% \end{macro} |
| 258 |
% \end{macro} |
| 259 |
% |
| 260 |
% A few shortcuts to save space. |
| 261 |
% \begin{macrocode} |
| 262 |
\chardef\z@num\z@ |
| 263 |
\x_cs\let{sin(0)} \z@num |
| 264 |
\x_cs\let{cos(0)} \@ne |
| 265 |
\x_cs\let{sin(90)} \@ne |
| 266 |
\x_cs\let{cos(90)} \z@num |
| 267 |
\x_cs\let{sin(-90)}\m@ne |
| 268 |
\x_cs\let{cos(-90)}\z@num |
| 269 |
\x_cs\let{sin(180)}\z@num |
| 270 |
\x_cs\let{cos(180)}\m@ne |
| 271 |
% \end{macrocode} |
| 272 |
% |
| 273 |
% \begin{macro}{\CalculateTan} |
| 274 |
% Originally I coded the Taylor series for tan, but it seems to |
| 275 |
% be more accurate to just take the ratio of the sine and cosine. |
| 276 |
% This is accurate to 4 decimal places for angles up to |
| 277 |
% $50^\circ$, after that the accuracy tails off, giving |
| 278 |
% $57.47894$ instead of $57.2900$ for $89^\circ$. |
| 279 |
% |
| 280 |
% Computed tangents are stored in control sequences with names of the |
| 281 |
% form \describecsfamily{tan(\meta{number})}|\tan(|\meta{number}|)|. |
| 282 |
% \begin{macrocode} |
| 283 |
\def\CalculateTan#1{{% |
| 284 |
\if_undefined{tan(\number#1)}\then |
| 285 |
\CalculateSin{#1}% |
| 286 |
\CalculateCos{#1}% |
| 287 |
\a_dimen\UseCos{#1}\p@ |
| 288 |
\divide \a_dimen \@iv |
| 289 |
\b_dimen\UseSin{#1}\p@ |
| 290 |
\b_dimen\two@fourteen\b_dimen |
| 291 |
\divide\b_dimen\a_dimen |
| 292 |
\x_cs\xdef{tan(\number#1)}{\TG@rem@pt\b_dimen} |
| 293 |
\fi |
| 294 |
}} |
| 295 |
% \end{macrocode} |
| 296 |
% \end{macro} |
| 297 |
% |
| 298 |
% \begin{macro}{\UseTan} |
| 299 |
% Just like |\UseSin|. |
| 300 |
% \begin{macrocode} |
| 301 |
\def\UseTan#1{\csname tan(\number#1)\endcsname} |
| 302 |
% \end{macrocode} |
| 303 |
% \end{macro} |
| 304 |
% |
| 305 |
% \begin{macro}{\two@fourteen} |
| 306 |
% \begin{macro}{\@iv} |
| 307 |
% Two constants needed to keep the division within \TeX's range. |
| 308 |
% \begin{macrocode} |
| 309 |
\mathchardef\two@fourteen=16384 |
| 310 |
\chardef\@iv=4 |
| 311 |
% \end{macrocode} |
| 312 |
% \end{macro} |
| 313 |
% \end{macro} |
| 314 |
% |
| 315 |
% Predefine $\tan(\pm90)$ to be an error. |
| 316 |
% \begin{macrocode} |
| 317 |
\x_cs\def{tan(90)}{\errmessage{Infinite tan !}} |
| 318 |
\expandafter\let |
| 319 |
\csname tan(-90)\expandafter\endcsname \csname tan(90)\endcsname |
| 320 |
%</pkg> |
| 321 |
% \end{macrocode} |
| 322 |
% |
| 323 |
% \Finale |
| 324 |
% |
| 325 |
\endinput |
| 326 |
|