summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-math-commands.tex
blob: 8a75eaa89ae9c861a607bb087c49dddc8c780bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
% Copyright 2007 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\section{Evaluating Mathematical Operations}

\label{pgfmath-commands}

Instead of parsing and evaluating complex expressions, you can also
use the mathematical engine to evaluate a single mathematical
operation. The macros used for these computations are described in the
following. 


\subsection{Basic Operations and Functions}

\label{pgfmath-operations}

\begin{command}{\pgfmathadd\marg{x}\marg{y}}  
	Defines |\pgfmathresult| as $\meta{x}+\meta{y}$.
\end{command}

\begin{command}{\pgfmathsubtract\marg{x}\marg{y}}      
	Defines |\pgfmathresult| as $\meta{x}-\meta{y}$.                                       
\end{command}

\begin{command}{\pgfmathmultiply\marg{x}\marg{y}}      
	Defines |\pgfmathresult| as $\meta{x}\times\meta{y}$.                                
\end{command}

\begin{command}{\pgfmathdivide\marg{x}\marg{y}}        
	Defines |\pgfmathresult| as $\meta{x}\div\meta{y}$. An error will
	result if \meta{y} is	|0|, or if the result of the division is
	too big for the mathematical engine.
	Please remember	when using this command that accurate (and reasonably 
	quick) division of non-integers is particularly tricky in \TeX{}. 	
	There are three different forms of division used in this command:
	\begin{itemize}
		\item 
		If \meta{y} is an integer then the native |\divide| operation of 
		\TeX{} is used.
		\item
		If \vrule\meta{y}\vrule$<1$, then |\pgfmathreciprocal| is employed.
		\item
		For all other values of \meta{y} an optimised long division 
		algorithm is used. In theory this should be accurate
		to any finite precision, but in practice it is constrained by the
		limits of \TeX{}'s native mathematics.
	\end{itemize}
	                             
\end{command}

\begin{command}{\pgfmathreciprocal\marg{x}}         
	Defines |\pgfmathresult| as $1\div\meta{x}$.                            
\end{command}

\begin{command}{\pgfmathgreaterthan\marg{x}\marg{y}}   
	Defines |\pgfmathresult| as 1.0 if \meta{x} $>$ \meta{y}, but 0.0 otherwise.                 
\end{command}

\begin{command}{\pgfmathlessthan\marg{x}\marg{y}} 
	Defines |\pgfmathresult| as 1.0 if \meta{x} $<$ \meta{y}, but 0.0 otherwise.             
\end{command}
	
\begin{command}{\pgfmathequalto\marg{x}\marg{y}}       
	Defines |\pgfmathresult| 1.0 if \meta{x} $=$ \meta{y}, but 0.0 otherwise.                    
\end{command}

\begin{command}{\pgfmathround\marg{x}}              
	Defines |\pgfmathresult| as $\left\lfloor\textrm{\meta{x}}\right\rceil$.	
	This uses asymmetric	half-up rounding.                          
\end{command}

\begin{command}{\pgfmathfloor\marg{x}}              
	Defines |\pgfmathresult| as $\left\lfloor\textrm{\meta{x}}\right\rfloor$.
\end{command}

\begin{command}{\pgfmathceil\marg{x}}               
	Defines |\pgfmathresult| as $\left\lceil\textrm{\meta{x}}\right\rceil$.                           
\end{command}
	
\begin{command}{\pgfmathpow\marg{x}\marg{y}}         
	Defines |\pgfmathresult| as $\meta{x}^{\meta{y}}$.  For greatest 
	accuracy \mvar{y} should be an integer. If \mvar{y} is not an integer 
	the actual calculation will be an approximation of $e^{y\ln(x)}$.
\end{command}

\begin{command}{\pgfmathmod\marg{x}\marg{y}}           
	Defines |\pgfmathresult| as \meta{x} modulo \meta{y}.                       
\end{command}

\begin{command}{\pgfmathmax\marg{x}\marg{y}}           
	Defines |\pgfmathresult| as the maximum of \meta{x} or \meta{y}.                       
\end{command}

\begin{command}{\pgfmathmin\marg{x}\marg{y}}           
	Defines |\pgfmathresult| as the minimum \meta{x} or \meta{y}.                       
\end{command}
	
\begin{command}{\pgfmathabs\marg{x}}                
	Defines |\pgfmathresult| as  absolute value of \meta{x}.                                 
\end{command}
	
\begin{command}{\pgfmathexp\marg{x}}                
	Defines |\pgfmathresult| as $e^{\meta{x}}$. Here, \meta{x} can be a 
	non-integer. The algorithm	uses a Maclaurin series.               
\end{command}

\begin{command}{\pgfmathln\marg{x}}                
	Defines |\pgfmathresult| as $\ln{\meta{x}}$. This uses an algorithm
	due to Rouben Rostamian, and coefficients suggested by
	Alain Matthes.             
\end{command}
	
\begin{command}{\pgfmathsqrt\marg{x}} 
	Defines |\pgfmathresult| as $\sqrt{\meta{x}}$. 
\end{command}
	
\begin{command}{\pgfmathveclen\marg{x}\marg{y}}        
	Defines |\pgfmathresult| as $\sqrt{\meta{x}^2+\meta{y}^2}$. This uses
	a polynomial approximation, based on ideas due to Rouben Rostamian.                                    
\end{command}

\subsection{Trignometric Functions}

\label{pgfmath-trigonmetry}

\begin{command}{\pgfmathpi}
  	Defines |\pgfmathresult| as $3.14159$.
\end{command}
   
\begin{command}{\pgfmathdeg{\marg{x}}} 
	Defines |\pgfmathresult| as \meta{x} (given in radians) converted to 
	degrees. 
\end{command}

\begin{command}{\pgfmathrad{\marg{x}}} 
	Defines |\pgfmathresult| as \meta{x} (given in degrees) converted to 
	radians. 
\end{command}

\begin{command}{\pgfmathsin{\marg{x}}}  
	Defines |\pgfmathresult| as the sine of \meta{x}.  
\end{command}

\begin{command}{\pgfmathcos{\marg{x}}}
	Defines |\pgfmathresult| as the cosine of \meta{x}.
\end{command}

\begin{command}{\pgfmathtan{\marg{x}}}  
	Defines |\pgfmathresult| as the tangant of \meta{x}.  
\end{command}

\begin{command}{\pgfmathsec{\marg{x}}}
	Defines |\pgfmathresult| as the secant of \meta{x}.
\end{command}

\begin{command}{\pgfmathcosec{\marg{x}}}  
	Defines |\pgfmathresult| as the cosecant of \meta{x}.  
\end{command}

\begin{command}{\pgfmathcot{\marg{x}}}  
	Defines |\pgfmathresult| as the cotangant of \meta{x}.  
\end{command}

\begin{command}{\pgfmathasin{\marg{x}}}
	Defines |\pgfmathresult| as the arcsine of \meta{x}. 
	The result will be in the range $\pm90^\circ$.
\end{command}

\begin{command}{\pgfmathacos{\marg{x}}}
	Defines |\pgfmathresult| as the arccosine of \meta{x}.
	The result will be in the range $\pm90^\circ$.
\end{command}

\begin{command}{\pgfmathatan{\marg{x}}}
 	Defines |\pgfmathresult| as the arctangent of \meta{x}.
\end{command}



\subsection{Pseudo-Random Numbers}

\label{pgfmath-random}


\begin{command}{\pgfmathgeneratepseudorandomnumber}
	Defines |\pgfmathresult| as a pseudo-random integer between 1 and 
	$2^{31}-1$. This uses a linear congruency generator, based on ideas
	due to Erich Janka.
\end{command}

\begin{command}{\pgfmathrnd}
	Defines |\pgfmathresult| as a pseudo-random number between |0| and |1|.
\end{command}

\begin{command}{\pgfmathrand}
	Defines |\pgfmathresult| as a pseudo-random number between |-1| and |1|.
\end{command}

\begin{command}{\pgfmathrandominteger\marg{macro}\marg{maximum}\marg{minimum}}
	This defines \meta{macro} as a pseudo-randomly generated integer from 
	the range \meta{maximum} to \meta{minimum} (inclusive).
	
\begin{codeexample}[]
\begin{pgfpicture}
   \foreach \x in {1,...,50}{
      \pgfmathrandominteger{\a}{1}{50}
      \pgfmathrandominteger{\b}{1}{50}
      \pgfpathcircle{\pgfpoint{+\a pt}{+\b pt}}{+2pt}
      \color{blue!40!white}
      \pgfsetstrokecolor{blue!80!black}
      \pgfusepath{stroke, fill}
   }	  
\end{pgfpicture}
\end{codeexample}
\end{command}

\begin{command}{\pgfmathdeclarerandomlist\marg{list name}\{\marg{item-1}\marg{item 2}...\}}
	This creates a list of items with the name \meta{list name}.
\end{command}

\begin{command}{\pgfmathrandomitem\marg{macro}\marg{list name}}
	Select an item from a random list \meta{list name}. The
	selected item is placed in \meta{macro}.
\end{command}

\begin{codeexample}[]
\begin{pgfpicture}
   \pgfmathdeclarerandomlist{color}{{red}{blue}{green}{yellow}{white}}
   \foreach \a in {1,...,50}{
      \pgfmathrandominteger{\x}{1}{85}
      \pgfmathrandominteger{\y}{1}{85}
      \pgfmathrandominteger{\r}{5}{10}
      \pgfmathrandomitem{\c}{color}
      \pgfpathcircle{\pgfpoint{+\x pt}{+\y pt}}{+\r pt}
      \color{\c!40!white}
      \pgfsetstrokecolor{\c!80!black}
      \pgfusepath{stroke, fill}
   }	  
\end{pgfpicture}
\end{codeexample}

\begin{command}{\pgfmathsetseed\marg{integer}}
  Explicitly set seed for the pseudo-random number generator. By
  default it is set to the value of |\time|$\times$|\year|.
\end{command}


      
\subsection{Conversion Between Bases}
	
\label{pgfmath-bases}

\pgfname{} provides limited support for conversion between 
\emph{representations} of numbers. Currently the numbers must be
positive integers in the range $0$ to $2^{31}-1$, and the bases in the
range $2$ to $36$. All digits representing numbers greater than 9 (in
base ten), are alphabetic, but may be upper or lower case. 

\begin{command}{\pgfmathbasetodec\marg{macro}\marg{number}\marg{base}}
	Defines \meta{macro} as the result of converting \meta{number} from
	base \meta{base} to base 10. Alphabetic digits can be upper or lower
	case.

\medskip{\def\medskip{}

\begin{codeexample}[]
\pgfmathbasetodec\mynumber{107f}{16} \mynumber
\end{codeexample}


\begin{codeexample}[]
\pgfmathbasetodec\mynumber{33FC}{20} \mynumber
\end{codeexample}

}\medskip

\end{command}

\begin{command}{\pgfmathdectobase\marg{macro}\marg{number}\marg{base}}
	Defines \meta{macro} as the result of converting \meta{number} from
	base 10 to base \meta{base}. Any resulting alphabetic digits are in
	\emph{lower case}.
	
\begin{codeexample}[]
\pgfmathdectobase\mynumber{65535}{16} \mynumber
\end{codeexample}

\end{command}

\begin{command}{\pgfmathdectoBase\marg{macro}\marg{number}\marg{base}}
	Defines \meta{macro} as the result of converting \meta{number} from
	base 10 to base \meta{base}. Any resulting alphabetic digits are in
	\emph{upper case}.
	
\begin{codeexample}[]
\pgfmathdectoBase\mynumber{65535}{16} \mynumber
\end{codeexample}

\end{command}

\begin{command}{\pgfmathbasetobase\marg{macro}\marg{number}\marg{base-1}\marg{base-2}}
	Defines \meta{macro} as the result of converting \meta{number} from
	base \meta{base-1} to base \meta{base-2}. Alphabetic digits in 
	\meta{number} can be upper or lower case, but any resulting 
	alphabetic digits are in \emph{lower case}.
	
\begin{codeexample}[]
\pgfmathbasetobase\mynumber{11011011}{2}{16} \mynumber
\end{codeexample}

\end{command}

\begin{command}{\pgfmathbasetoBase\marg{macro}\marg{number}\marg{base-1}\marg{base-2}}
	Defines \meta{macro} as the result of converting \meta{number} from
	base \meta{base-1} to base \meta{base-2}. Alphabetic digits in 
	\meta{number} can be upper or lower case, but any resulting 
	alphabetic digits are in \emph{upper case}.
	
\begin{codeexample}[]
\pgfmathbasetoBase\mynumber{121212}{3}{12} \mynumber
\end{codeexample}

\end{command}


\begin{command}{\pgfmathsetbasenumberlength\marg{integer}}
	Set the number of digits in the result of a base conversion to 
	\meta{integer}. If the result of a conversion has less digits
	than this number it is prefixed with zeros.

\begin{codeexample}[]
\pgfmathsetbasenumberlength{8}
\pgfmathdectobase\mynumber{15}{2} \mynumber
\end{codeexample}

\end{command}