summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/pgfplots/pgfmathlog.sty
blob: 9e9b57ea13755b400cb373caa27cacfd78370eb7 (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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Package pgfmathlog.sty. 
%
% This file provides the public functions
% 
%   \pgfmathnormalisedfloatingpoint#1\to#2\powten#3
% like
%   \pgfmathnormalisedfloatingpoint 412.02451e-5to\M\powten\E
%
% and
%   \pgfmathlog
%   \pgfmathlog@
% which both assign \pgfmathresult to the natural number of a given
% number (without units).
%
% It is based on Till Tantaus pgfmath package which comes as part of
% the PGF bundle, but it provides its own number parsing routines to
% circumvent TeXs limited real number representations.
%
% Copyright 2007/2008 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{pgfmathlog}[2008/02/17 Version 0.9.4]
\RequirePackage{pgf}[2007/01/01]
\RequirePackage{sciformat}[2008/02/17]
\RequirePackage{pgfplotshelpers}[2008/02/17]

% See \sciparse docs
\def\pgfmathnormalisedfloatingpoint#1\to#2\powten#3{%
	\sciparse#1\to#2\powten{#3}%
}

% Evaluates the natural logarithm, log(x) for 0.1<=x<=1.
% @see \pgfmathlog@ for the general method.
% NON ACCURATE ENOUGH!
\def\pgfmathlogzeroone#1{%
	\begingroup%
		\message{EVAL log(#1) STARTS:}%
		\pgfmath@x=#1pt\relax
		\pgfmath@y=\pgfmath@x
		\advance\pgfmath@x by-1pt
		\advance\pgfmath@y by 1pt
		\pgfmathdivide{\pgfmath@x}{\pgfmath@y}%
		% pgfmath@ya = tmp := (#1 -1)/ (#1+1)
		\expandafter\pgfmath@ya\pgfmathresult pt
		% pgfmath@xa := [ (#1 -1)/ (#1+1) ]^2
		\pgfmathmultiply{\pgfmathresult}{\pgfmathresult}%
		\expandafter\pgfmath@xa\pgfmathresult pt
		% returnvalue:
		\pgfmath@y=0pt
		% loop counter:
		\c@pgfmath@counta=0
		% j:
		\c@pgfmath@countb=1
		\loop
		\ifnum\c@pgfmath@counta<6
			\let\multmp=\pgfmath@ya
			\divide\pgfmath@ya by\c@pgfmath@countb
			\advance\pgfmath@y by\pgfmath@ya
			\let\pgfmath@ya=\multmp
			\advance\c@pgfmath@countb by2
			\message{[tmp *= x*x  tmp =\the\pgfmath@ya, x*x=\the\pgfmath@xa]}%
			\pgfmathmultiply{\pgfmath@ya}{\pgfmath@xa}%
			\expandafter\pgfmath@ya\pgfmathresult pt
			\advance\c@pgfmath@counta by1
			\message{[cur/2 = \the\pgfmath@y]}%
		\repeat
		\multiply\pgfmath@y by2
		\message{fertiges Resultat fuer log(#1): \the\pgfmath@y}%
		\pgfmath@returnone\pgfmath@y%
	\endgroup%
}


% Computes a good guess for log(X.XXXX) where the input number should
% be between 0 < x < 10.
%
% If it is not in this range, the returned value will be bad.
%
% For use in the newton implementation of log(x). Please note that we
% first compute a normalized floating point representation of x, and
% the mantisse is always between 0 and 10.
\def\pgfmathlog@smaller@ten@newton@startval#1#2#3\to#4{%
	\ifnum0<#1\relax
		\def\axis@TMP{#2}%
		\def\axis@TMPB{.}%
		\ifx\axis@TMP\axis@TMPB
			\ifcase#1
			\errmessage{pgfmathlog.sty: INTERNAL ERROR: should not happen! 
				[\#1=0 should have been caught before!]}%
			\or#4=0.53062825106217pt
			\or#4=0.993pt
			\or#4=1.308pt
			\or#4=1.5pt
			\or#4=1.71pt
			\or#4=1.872pt
			\or#4=2.01pt
			\or#4=2.145pt
			\or#4=2.24pt
			\fi
%			\message{Newton: Start value \the#4 assigned}%
		\else
%			\message{Kein Komma an zweiter stelle (statt dessen: #2); Nehme standardstartwert.}%
			#4=0.1pt
		\fi
	\else
%		\message{Argument #1 <= 0. Nehme standardstartwert.}%
		#4=0.1pt
	\fi
}

% Computes log(x) into \pgfmathresult.
%
% REMARK
% This method does NOT use TeX Registers to represent the numbers! The
% computation is COMPLETELY STRING BASED.
% This allows numbers such at 10000000 or 5.23e-10 to be represented
% properly, although TeX-registers would produce overflow/underflow
% errors in these cases. 
%
% The natural logarithm is computed using log(X*10^Y) = log(X) + log(10)*Y
\def\pgfmathlog@#1{%
	\pgfmathlog@newton{#1}%
}

% Public interface method. This is expected to use \pgfmathparse but
% it DOESN'T do that. \pgfmathparse naturally can't handle numbers
% which exceed TeX's numerical capabilities. However, exponentials are
% typically either too large or too small for TeX.
%
% @see \pgfmathnormalisedfloatingpoint for how we parse arguments.
% @see \pgfmathlog@
\def\pgfmathlog#1{%
	\pgfmathlog@newton{#1}%
}

% This implementation of log(X) is done with a newton method applied to x-exp(y) = 0.
% The implementation \pgfmathexp is used for exp(y).
%
% see \pgfmathlog@
\def\pgfmathlog@newton#1{%
	\begingroup%
		% compute #1 = M*10^E with normalised mantisse M = [+-]*[1-9].XXXXX
		\sciparse#1\to\newtoninput@mantisse\powten\newtoninput@exponent
		%
		% Now, compute log(#1) = log(M) + E*log(10)
%		\message{EVAL log(#1) = log(\newtoninput@mantisse) + \newtoninput@exponent*log(10) mit newton STARTS:}%
		% input argument into \pgfmath@x
		\expandafter\pgfmath@x\newtoninput@mantisse pt
		\ifdim\pgfmath@x>0pt
			% return value:
			\expandafter\pgfmathlog@smaller@ten@newton@startval\newtoninput@mantisse\to\pgfmath@y
			% loop counter:
			\c@pgfmath@counta=0
			\loop
	%\message{Newton iter \#\the\c@pgfmath@counta: log(\newtoninput@mantisse) = \the\pgfmath@y }%
			\ifnum\c@pgfmath@counta<2
	%			\pgfmathexp\pgfmath@y%
				\pgfmathlog@invoke@expanded\pgfmathexp@{{\pgf@sys@tonumber{\pgfmath@y}}}%
				\let\tmp=\pgfmathresult
				%\pgfmathdivide\pgfmath@x\tmp%
				\pgfmathlog@invoke@expanded\pgfmathdivide@{{\pgf@sys@tonumber{\pgfmath@x}}{\tmp}}%
				\expandafter\pgfmath@ya\pgfmathresult pt
				\advance\pgfmath@y by\pgfmath@ya
				\advance\pgfmath@y by-1pt
				\advance\c@pgfmath@counta by1
			\repeat
			\pgfmath@x=2.302585pt% = log(10)
			\multiply\expandafter\pgfmath@x\newtoninput@exponent
			\advance\pgfmath@y by\pgfmath@x
%\message{Newton final result: log(\newtoninput@mantisse)+\newtoninput@exponent*log(10) = \the\pgfmath@y [ nur \newtoninput@exponent * log(10) = \the\pgfmath@x]}%
			\pgfmath@returnone\pgfmath@y%
		\else
			\global\let\pgfmathresult=\empty%
		\fi
	\endgroup%
}

% Usage:
% \pgfmathapproxequalto@macro\argone\argtwo\result
%
% -> will set \resulttrue or \resultfalse
% -> check with \ifresult
\def\pgfmathlog@approxequalto@macro#1#2#3{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\expandafter\pgfmath@y#2pt\relax%
		\advance\pgfmath@x-\pgfmath@y%
		\ifdim\pgfmath@x<0pt
			\multiply\pgfmath@x by-1
		\fi
		\ifdim\pgfmath@x<0.0001pt\relax%
			\expandafter\aftergroup\csname #3true\endcsname
		\else%
			\expandafter\aftergroup\csname #3false\endcsname
		\fi%
	\endgroup%
}