| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Public License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
% This file defines the mathematical functions and operators. |
| 11 |
% |
| 12 |
% Version 1.414213 29/9/2007 |
| 13 |
|
| 14 |
% veclen function adapted from ideas due to Rouben Rostamian. |
| 15 |
% |
| 16 |
% Calculate the Euclidean length of a 2D vector. |
| 17 |
% |
| 18 |
% |
| 19 |
\pgfmathdeclarefunction{veclen}{2}{% |
| 20 |
\begingroup% |
| 21 |
\pgfmath@x#1pt\relax% |
| 22 |
\pgfmath@y#2pt\relax% |
| 23 |
\ifdim\pgfmath@x<0pt\relax% |
| 24 |
\pgfmath@x-\pgfmath@x% |
| 25 |
\fi% |
| 26 |
\ifdim\pgfmath@y<0pt\relax% |
| 27 |
\pgfmath@y-\pgfmath@y% |
| 28 |
\fi% |
| 29 |
\ifdim\pgfmath@x=0pt\relax% |
| 30 |
\pgfmath@x\pgfmath@y% |
| 31 |
\else% |
| 32 |
\ifdim\pgfmath@y=0pt\relax% |
| 33 |
\else% |
| 34 |
\ifdim\pgfmath@x>\pgfmath@y% |
| 35 |
\pgfmath@xa\pgfmath@x% |
| 36 |
\pgfmath@x\pgfmath@y% |
| 37 |
\pgfmath@y\pgfmath@xa% |
| 38 |
\fi% |
| 39 |
% We use a scaling factor to reduce errors. |
| 40 |
\ifdim\pgfmath@y>10000pt\relax% |
| 41 |
\c@pgfmath@counta1500\relax% |
| 42 |
\else% |
| 43 |
\ifdim\pgfmath@y>1000pt\relax% |
| 44 |
\c@pgfmath@counta150\relax% |
| 45 |
\else% |
| 46 |
\ifdim\pgfmath@y>100pt\relax% |
| 47 |
\c@pgfmath@counta50\relax% |
| 48 |
\else% |
| 49 |
\c@pgfmath@counta1\relax% |
| 50 |
\fi% |
| 51 |
\fi% |
| 52 |
\fi% |
| 53 |
\divide\pgfmath@x\c@pgfmath@counta\relax% |
| 54 |
\divide\pgfmath@y\c@pgfmath@counta\relax% |
| 55 |
\pgfmathreciprocal@{\pgfmath@tonumber{\pgfmath@y}}% |
| 56 |
\pgfmath@x\pgfmathresult\pgfmath@x% |
| 57 |
\pgfmath@xa\pgfmath@tonumber{\pgfmath@x}\pgfmath@x% |
| 58 |
\edef\pgfmath@temp{\pgfmath@tonumber{\pgfmath@xa}}% |
| 59 |
% |
| 60 |
% Use A+x^2*(B+x^2*(C+x^2*(D+E*x^2))) |
| 61 |
% where |
| 62 |
% A = +1.000012594 |
| 63 |
% B = +0.4993615349 |
| 64 |
% C = -0.1195159052 |
| 65 |
% D = +0.04453994279 |
| 66 |
% E = -0.01019210944 |
| 67 |
% |
| 68 |
\[email protected]\pgfmath@xa% |
| 69 |
\advance\[email protected]\relax% |
| 70 |
\pgfmath@x\pgfmath@temp\pgfmath@x% |
| 71 |
\advance\[email protected]\relax% |
| 72 |
\pgfmath@x\pgfmath@temp\pgfmath@x% |
| 73 |
\advance\[email protected]\relax% |
| 74 |
\pgfmath@x\pgfmath@temp\pgfmath@x% |
| 75 |
\advance\[email protected]\relax% |
| 76 |
\ifdim\pgfmath@y<0pt\relax% |
| 77 |
\pgfmath@y-\pgfmath@y% |
| 78 |
\fi% |
| 79 |
\pgfmath@x\pgfmath@tonumber{\pgfmath@y}\pgfmath@x% |
| 80 |
% Invert the scaling factor. |
| 81 |
\multiply\pgfmath@x\c@pgfmath@counta\relax% |
| 82 |
\fi% |
| 83 |
\fi% |
| 84 |
\pgfmath@returnone\pgfmath@x% |
| 85 |
\endgroup% |
| 86 |
} |
| 87 |
|
| 88 |
% Hyperbolic functions contributed by Martin Heller. |
| 89 |
% |
| 90 |
\pgfmathdeclarefunction{cosh}{1}{% |
| 91 |
\begingroup |
| 92 |
\edef\pgfmath@arg{#1}% |
| 93 |
\pgfmathexp@{\pgfmath@arg}% |
| 94 |
\pgfmath@x=\pgfmathresult pt\relax% |
| 95 |
\pgfmathexp@{-\pgfmath@arg}% |
| 96 |
\advance\pgfmath@x by\pgfmathresult pt\relax% |
| 97 |
\divide\pgfmath@x by2\relax% |
| 98 |
\pgfmath@returnone\pgfmath@x% |
| 99 |
\endgroup} |
| 100 |
|
| 101 |
\pgfmathdeclarefunction{sinh}{1}{% |
| 102 |
\begingroup |
| 103 |
\edef\pgfmath@arg{#1}% |
| 104 |
\pgfmathexp@{\pgfmath@arg}% |
| 105 |
\pgfmath@x=\pgfmathresult pt\relax% |
| 106 |
\pgfmathexp@{-\pgfmath@arg}% |
| 107 |
\advance\pgfmath@x by-\pgfmathresult pt\relax% |
| 108 |
\divide\pgfmath@x by2\relax% |
| 109 |
\pgfmath@returnone\pgfmath@x% |
| 110 |
\endgroup |
| 111 |
}% |
| 112 |
|
| 113 |
\pgfmathdeclarefunction{tanh}{1}{% |
| 114 |
\begingroup |
| 115 |
\edef\pgfmath@arg{#1}% |
| 116 |
\pgfmathsinh@{\pgfmath@arg}% |
| 117 |
\pgfmath@x=\pgfmathresult pt\relax% |
| 118 |
\pgfmathcosh@{\pgfmath@arg}% |
| 119 |
\pgfmath@y=\pgfmathresult pt\relax% |
| 120 |
\pgfmathdivide@{\pgfmath@tonumber{\pgfmath@x}}{\pgfmath@tonumber{\pgfmath@y}}% |
| 121 |
\pgfmath@x=\pgfmathresult pt\relax% |
| 122 |
\pgfmath@returnone\pgfmath@x% |
| 123 |
\endgroup |
| 124 |
}% |
| 125 |
|
| 126 |
% \pgfmathscientific |
| 127 |
% |
| 128 |
% Return the value of #1e#2 |
| 129 |
% |
| 130 |
% e.g. \pgfmathscientific{1.23456789123}{4} |
| 131 |
% |
| 132 |
% defines \pgfmathresult as 12345.67891 |
| 133 |
% |
| 134 |
% NB These arguments *are not parsed*, as the long mantissa would be lost. |
| 135 |
% |
| 136 |
\def\pgfmathscientific#1#2{% |
| 137 |
\begingroup% |
| 138 |
\edef\pgfmath@sci@exponent{#2}% |
| 139 |
\pgfmath@x#1pt\relax% |
| 140 |
\ifdim\pgfmath@x<0pt\relax% |
| 141 |
\pgfmath@x-\pgfmath@x% |
| 142 |
\edef\pgfmath@sci@sign{-}% |
| 143 |
\expandafter\edef\expandafter\pgfmath@temp\expandafter{\expandafter\pgfmath@gobbleone#1}% |
| 144 |
\else% |
| 145 |
\edef\pgfmath@sci@sign{+}% |
| 146 |
\edef\pgfmath@temp{#1}% |
| 147 |
\fi% |
| 148 |
\expandafter\pgfmath@in@\expandafter.\expandafter{#1}% |
| 149 |
\ifpgfmath@in@% |
| 150 |
\else% |
| 151 |
\edef\pgfmath@temp{\[email protected]}% |
| 152 |
\fi% |
| 153 |
\expandafter\pgfmath@scientific@@\pgfmath@temp00000000000\pgfmath@} |
| 154 |
|
| 155 |
\def\pgfmath@scientific@@#1.#2#3#4#5#6{% |
| 156 |
\edef\pgfmath@sci@int{#1}% |
| 157 |
\edef\pgfmath@sci@mantissaA{#2#3#4#5#6}% |
| 158 |
\pgfmath@scientific@@@} |
| 159 |
|
| 160 |
\def\pgfmath@scientific@@@#1#2#3#4#5#6\pgfmath@{% |
| 161 |
\edef\pgfmath@sci@mantissaB{#1#2#3#4#5}% |
| 162 |
\c@pgfmath@counta\pgfmath@sci@exponent\relax% |
| 163 |
\ifnum\c@pgfmath@counta<-4\relax% |
| 164 |
\pgfmath@x0pt\relax% |
| 165 |
\else% |
| 166 |
\c@pgfmath@countb\c@pgfmath@counta% |
| 167 |
\ifnum\c@pgfmath@counta<0\relax% |
| 168 |
\c@pgfmath@counta-\c@pgfmath@counta% |
| 169 |
\fi% |
| 170 |
\pgfmathpow@{10}{\the\c@pgfmath@counta}% |
| 171 |
\afterassignment\pgfmath@gobbletilpgfmath@ |
| 172 |
\c@pgfmath@countc\pgfmathresult\relax\pgfmath@ |
| 173 |
\edef\pgfmath@sci@factor{\the\c@pgfmath@countc}% |
| 174 |
\ifnum\c@pgfmath@countb<0\relax% |
| 175 |
% xE-y: easy... |
| 176 |
\pgfmath@x\pgfmath@sci@int.\pgfmath@sci@mantissaA pt\relax% |
| 177 |
\divide\pgfmath@x\pgfmath@sci@factor\relax% |
| 178 |
\else% |
| 179 |
% xE+y: |
| 180 |
% Must do this way so as not lose digits in a long mantissa. Sigh... |
| 181 |
\c@pgfmath@counta\pgfmath@sci@int% |
| 182 |
\c@pgfmath@countb\pgfmath@sci@mantissaA% |
| 183 |
\multiply\c@pgfmath@counta\pgfmath@sci@factor\relax% |
| 184 |
\multiply\c@pgfmath@countb\pgfmath@sci@factor\relax% |
| 185 |
\c@pgfmath@countc\c@pgfmath@countb% |
| 186 |
\divide\c@pgfmath@countb100000\relax% |
| 187 |
\advance\c@pgfmath@counta\c@pgfmath@countb% |
| 188 |
\multiply\c@pgfmath@countb100000\relax% |
| 189 |
\advance\c@pgfmath@countc-\c@pgfmath@countb% |
| 190 |
\c@pgfmath@countb\pgfmath@sci@mantissaB\relax% |
| 191 |
\multiply\c@pgfmath@countb\pgfmath@sci@factor\relax% |
| 192 |
\divide\c@pgfmath@countb100000\relax% |
| 193 |
\advance\c@pgfmath@countc\c@pgfmath@countb% |
| 194 |
\advance\c@pgfmath@countc100000\relax% |
| 195 |
\edef\pgfmath@sci@result{\pgfmath@sci@sign\the\c@pgfmath@counta.\expandafter\pgfmath@gobbleone\the\c@pgfmath@countc pt}% |
| 196 |
\pgfmath@x\pgfmath@sci@result\relax% |
| 197 |
\fi\fi% |
| 198 |
\pgfmath@returnone\pgfmath@x% |
| 199 |
\endgroup} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
% width function. |
| 204 |
% |
| 205 |
\pgfmathdeclarefunction{width}{1}{% |
| 206 |
\begingroup% |
| 207 |
\setbox\pgfmath@box=\hbox{{\pgfmath@selectfont{#1}}}% |
| 208 |
\pgfmath@x=\wd\pgfmath@box% |
| 209 |
\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}% |
| 210 |
\pgfmath@smuggleone\pgfmathresult% |
| 211 |
\endgroup% |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
% height function. |
| 216 |
% |
| 217 |
\pgfmathdeclarefunction{height}{1}{% |
| 218 |
\begingroup% |
| 219 |
\setbox\pgfmath@box=\hbox{{\pgfmath@selectfont{#1}}}% |
| 220 |
\pgfmath@x=\ht\pgfmath@box% |
| 221 |
\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}% |
| 222 |
\pgfmath@smuggleone\pgfmathresult% |
| 223 |
\endgroup% |
| 224 |
} |
| 225 |
|
| 226 |
% depth function. |
| 227 |
% |
| 228 |
\pgfmathdeclarefunction{depth}{1}{% |
| 229 |
\begingroup% |
| 230 |
\setbox\pgfmath@box=\hbox{{\pgfmath@selectfont{#1}}}% |
| 231 |
\pgfmath@x=\dp\pgfmath@box% |
| 232 |
\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}% |
| 233 |
\pgfmath@smuggleone\pgfmathresult% |
| 234 |
\endgroup% |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
% array function. |
| 239 |
% |
| 240 |
\pgfmathdeclarefunction{array}{2}{% |
| 241 |
\begingroup% |
| 242 |
\afterassignment\pgfmath@gobbletilpgfmath@\pgfmath@count=#2\relax\pgfmath@% |
| 243 |
\expandafter\pgfmatharray@@#1\pgfmath@stop% |
| 244 |
} |
| 245 |
|
| 246 |
\def\pgfmath@token@stop{\pgfmath@stop}% |
| 247 |
\def\pgfmatharray@@#1{% |
| 248 |
\def\pgfmath@temp{#1}% |
| 249 |
\advance\pgfmath@count by-1\relax% |
| 250 |
\ifx\pgfmath@temp\pgfmath@token@stop% |
| 251 |
\pgfmath@error{Array index out of bounds.}{}% |
| 252 |
\def\pgfmathresult{0}% |
| 253 |
\pgfmath@smuggleone\pgfmathresult\endgroup% |
| 254 |
\let\pgfmath@next=\relax% |
| 255 |
\else% |
| 256 |
\ifnum\pgfmath@count=-1\relax% |
| 257 |
\def\pgfmathresult{#1}% |
| 258 |
\let\pgfmath@next=\pgfmatharray@@@% |
| 259 |
\else% |
| 260 |
\let\pgfmath@next=\pgfmatharray@@% |
| 261 |
\fi% |
| 262 |
\fi% |
| 263 |
\pgfmath@next} |
| 264 |
|
| 265 |
\def\pgfmatharray@@@#1\pgfmath@stop{% |
| 266 |
\pgfmath@smuggleone\pgfmathresult\endgroup% |
| 267 |
} |
| 268 |
|
| 269 |
% dim function: return dimension of an array |
| 270 |
% dim({1,2,3}) return 3 |
| 271 |
% dim({{1,2,3},{4,5,6}}) return 2 |
| 272 |
\pgfmathdeclarefunction{dim}{1}{% |
| 273 |
\begingroup |
| 274 |
\pgfmath@count=0\relax |
| 275 |
\expandafter\pgfmath@dim@i\pgfutil@firstofone#1{\pgfmath@token@stop}% |
| 276 |
\edef\pgfmathresult{\the\pgfmath@count}% |
| 277 |
\pgfmath@smuggleone\pgfmathresult% |
| 278 |
\endgroup} |
| 279 |
|
| 280 |
\def\pgfmath@dim@i#1#{% |
| 281 |
\pgfutil@ifempty{#1}{}{\advance\pgfmath@count by 1}% |
| 282 |
\pgfmath@dim@ii{#1}} |
| 283 |
|
| 284 |
\def\pgfmath@dim@ii#1#2{% |
| 285 |
\ifx\pgfmath@token@stop#2% |
| 286 |
\else |
| 287 |
\advance\pgfmath@count by 1\relax |
| 288 |
\expandafter\pgfmath@dim@i |
| 289 |
\fi} |
| 290 |
|
| 291 |
|
| 292 |
% max function. |
| 293 |
% |
| 294 |
\pgfmathdeclarefunction{max}{...}{% |
| 295 |
\begingroup% |
| 296 |
\pgfmath@x=-16383pt\relax% |
| 297 |
\pgfmathmax@@#1{}} |
| 298 |
|
| 299 |
\def\pgfmathmax@@#1{% |
| 300 |
\def\pgfmath@temp{#1}% |
| 301 |
\ifx\pgfmath@temp\pgfmath@empty% |
| 302 |
\let\pgfmath@next=\pgfmathmax@@@% |
| 303 |
\else% |
| 304 |
\ifdim#1pt>\pgfmath@x% |
| 305 |
\pgfmath@x=#1pt\relax% |
| 306 |
\fi% |
| 307 |
\let\pgfmath@next=\pgfmathmax@@% |
| 308 |
\fi% |
| 309 |
\pgfmath@next} |
| 310 |
|
| 311 |
\def\pgfmathmax@@@{\pgfmath@returnone\pgfmath@x\endgroup}% |
| 312 |
|
| 313 |
% For computability with old code. |
| 314 |
\def\pgfmathmax#1#2{% |
| 315 |
\pgfmathparse{#1,#2}% |
| 316 |
\expandafter\pgfmathmax@\expandafter{\pgfmathresult}% |
| 317 |
} |
| 318 |
|
| 319 |
% min function. |
| 320 |
% |
| 321 |
\pgfmathdeclarefunction{min}{...}{% |
| 322 |
\begingroup% |
| 323 |
\pgfmath@x=16383pt\relax% |
| 324 |
\pgfmathmin@@#1{}} |
| 325 |
|
| 326 |
\def\pgfmathmin@@#1{% |
| 327 |
\def\pgfmath@temp{#1}% |
| 328 |
\ifx\pgfmath@temp\pgfmath@empty% |
| 329 |
\let\pgfmath@next=\pgfmathmin@@@% |
| 330 |
\else% |
| 331 |
\ifdim#1pt<\pgfmath@x% |
| 332 |
\pgfmath@x=#1pt\relax% |
| 333 |
\fi% |
| 334 |
\let\pgfmath@next=\pgfmathmin@@% |
| 335 |
\fi% |
| 336 |
\pgfmath@next} |
| 337 |
|
| 338 |
\def\pgfmathmin@@@{\pgfmath@returnone\pgfmath@x\endgroup}% |
| 339 |
|
| 340 |
% For computability with old code. |
| 341 |
\def\pgfmathmin#1#2{% |
| 342 |
\pgfmathparse{#1,#2}% |
| 343 |
\expandafter\pgfmathmin@\expandafter{\pgfmathresult}% |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
% scalar function. |
| 349 |
% |
| 350 |
\pgfmathdeclarefunction{scalar}{1}{% |
| 351 |
\def\pgfmathresult{#1}% |
| 352 |
\global\pgfmathunitsdeclaredfalse% |
| 353 |
} |