mp-math.mpxl /size: 16 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=mp-math.mpiv,
3%D        version=2019.07.26, % was mp-core: 1999.08.01, anchoring
4%D          title=\CONTEXT\ \METAPOST\ graphics,
5%D       subtitle=extra math functions,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14if known metafun_loaded_math : endinput ; fi ;
15
16newinternal boolean metafun_loaded_math ; metafun_loaded_math := true ; immutable metafun_loaded_math ;
17
18% draw textext(decimal runscript("mp.numeric(xmath.gamma(.12))")) ;
19
20newscriptindex mfid_m_acos       ; mfid_m_acos       := scriptindex "m_acos"       ; def m_acos      = runscript mfid_m_acos      enddef ;
21newscriptindex mfid_m_acosh      ; mfid_m_acosh      := scriptindex "m_acosh"      ; def m_acosh     = runscript mfid_m_acosh     enddef ;
22newscriptindex mfid_m_asin       ; mfid_m_asin       := scriptindex "m_asin"       ; def m_asin      = runscript mfid_m_asin      enddef ;
23newscriptindex mfid_m_asinh      ; mfid_m_asinh      := scriptindex "m_asinh"      ; def m_asinh     = runscript mfid_m_asinh     enddef ;
24newscriptindex mfid_m_atan       ; mfid_m_atan       := scriptindex "m_atan"       ; def m_atan      = runscript mfid_m_atan      enddef ;
25newscriptindex mfid_m_atantwo    ; mfid_m_atantwo    := scriptindex "m_atan2"      ; def m_atantwo   = runscript mfid_m_atantwo   enddef ; % atan2
26newscriptindex mfid_m_atanh      ; mfid_m_atanh      := scriptindex "m_atanh"      ; def m_atanh     = runscript mfid_m_atanh     enddef ;
27newscriptindex mfid_m_cbrt       ; mfid_m_cbrt       := scriptindex "m_cbrt"       ; def m_cbrt      = runscript mfid_m_cbrt      enddef ;
28newscriptindex mfid_m_ceil       ; mfid_m_ceil       := scriptindex "m_ceil"       ; def m_ceil      = runscript mfid_m_ceil      enddef ;
29newscriptindex mfid_m_copysign   ; mfid_m_copysign   := scriptindex "m_copysign"   ; def m_copysign  = runscript mfid_m_copysign  enddef ;
30newscriptindex mfid_m_cos        ; mfid_m_cos        := scriptindex "m_cos"        ; def m_cos       = runscript mfid_m_cos       enddef ;
31newscriptindex mfid_m_cosh       ; mfid_m_cosh       := scriptindex "m_cosh"       ; def m_cosh      = runscript mfid_m_cosh      enddef ;
32newscriptindex mfid_m_deg        ; mfid_m_deg        := scriptindex "m_deg"        ; def m_deg       = runscript mfid_m_deg       enddef ;
33newscriptindex mfid_m_erf        ; mfid_m_erf        := scriptindex "m_erf"        ; def m_erf       = runscript mfid_m_erf       enddef ;
34newscriptindex mfid_m_erfc       ; mfid_m_erfc       := scriptindex "m_erfc"       ; def m_erfc      = runscript mfid_m_erfc      enddef ;
35newscriptindex mfid_m_exp        ; mfid_m_exp        := scriptindex "m_exp"        ; def m_exp       = runscript mfid_m_exp       enddef ;
36newscriptindex mfid_m_exptwo     ; mfid_m_exptwo     := scriptindex "m_exp2"       ; def m_exptwo    = runscript mfid_m_exptwo    enddef ; % exp2
37newscriptindex mfid_m_expm       ; mfid_m_expm       := scriptindex "m_expm1"      ; def m_expm      = runscript mfid_m_expm      enddef ; % expm1
38newscriptindex mfid_m_fabs       ; mfid_m_fabs       := scriptindex "m_fabs"       ; def m_fabs      = runscript mfid_m_fabs      enddef ;
39newscriptindex mfid_m_fdim       ; mfid_m_fdim       := scriptindex "m_fdim"       ; def m_fdim      = runscript mfid_m_fdim      enddef ;
40newscriptindex mfid_m_floor      ; mfid_m_floor      := scriptindex "m_floor"      ; def m_floor     = runscript mfid_m_floor     enddef ;
41newscriptindex mfid_m_fma        ; mfid_m_fma        := scriptindex "m_fma"        ; def m_fma       = runscript mfid_m_fma       enddef ;
42newscriptindex mfid_m_fmax       ; mfid_m_fmax       := scriptindex "m_fmax"       ; def m_fmax      = runscript mfid_m_fmax      enddef ;
43newscriptindex mfid_m_fmin       ; mfid_m_fmin       := scriptindex "m_fmin"       ; def m_fmin      = runscript mfid_m_fmin      enddef ;
44newscriptindex mfid_m_fmod       ; mfid_m_fmod       := scriptindex "m_fmod"       ; def m_fmod      = runscript mfid_m_fmod      enddef ;
45newscriptindex mfid_m_frexp      ; mfid_m_frexp      := scriptindex "m_frexp"      ; def m_frexp     = runscript mfid_m_frexp     enddef ;
46newscriptindex mfid_m_gamma      ; mfid_m_gamma      := scriptindex "m_gamma"      ; def m_gamma     = runscript mfid_m_gamma     enddef ;
47newscriptindex mfid_m_hypot      ; mfid_m_hypot      := scriptindex "m_hypot"      ; def m_hypot     = runscript mfid_m_hypot     enddef ;
48newscriptindex mfid_m_isfinite   ; mfid_m_isfinite   := scriptindex "m_isfinite"   ; def m_isfinite  = runscript mfid_m_isfinite  enddef ;
49newscriptindex mfid_m_isinf      ; mfid_m_isinf      := scriptindex "m_isinf"      ; def m_isinf     = runscript mfid_m_isinf     enddef ;
50newscriptindex mfid_m_isnan      ; mfid_m_isnan      := scriptindex "m_isnan"      ; def m_isnan     = runscript mfid_m_isnan     enddef ;
51newscriptindex mfid_m_isnormal   ; mfid_m_isnormal   := scriptindex "m_isnormal"   ; def m_isnormal  = runscript mfid_m_isnormal  enddef ;
52newscriptindex mfid_m_jz         ; mfid_m_jz         := scriptindex "m_j0"         ; def m_jz        = runscript mfid_m_jz        enddef ; % j0
53newscriptindex mfid_m_j          ; mfid_m_j          := scriptindex "m_j1"         ; def m_j         = runscript mfid_m_j         enddef ; % j1
54newscriptindex mfid_m_jn         ; mfid_m_jn         := scriptindex "m_jn"         ; def m_jn        = runscript mfid_m_jn        enddef ;
55newscriptindex mfid_m_ldexp      ; mfid_m_ldexp      := scriptindex "m_ldexp"      ; def m_ldexp     = runscript mfid_m_ldexp     enddef ;
56newscriptindex mfid_m_lgamma     ; mfid_m_lgamma     := scriptindex "m_lgamma"     ; def m_lgamma    = runscript mfid_m_lgamma    enddef ;
57newscriptindex mfid_m_log        ; mfid_m_log        := scriptindex "m_log"        ; def m_log       = runscript mfid_m_log       enddef ;
58newscriptindex mfid_m_logten     ; mfid_m_logten     := scriptindex "m_log10"      ; def m_logten    = runscript mfid_m_logten    enddef ; % log10
59newscriptindex mfid_m_logp       ; mfid_m_logp       := scriptindex "m_log1p"      ; def m_logp      = runscript mfid_m_logp      enddef ; % log1p
60newscriptindex mfid_m_logtwo     ; mfid_m_logtwo     := scriptindex "m_log2"       ; def m_logtwo    = runscript mfid_m_logtwo    enddef ; % log2
61newscriptindex mfid_m_logb       ; mfid_m_logb       := scriptindex "m_logb"       ; def m_logb      = runscript mfid_m_logb      enddef ;
62newscriptindex mfid_m_modf       ; mfid_m_modf       := scriptindex "m_modf"       ; def m_modf      = runscript mfid_m_modf      enddef ;
63newscriptindex mfid_m_nearbyint  ; mfid_m_nearbyint  := scriptindex "m_nearbyint"  ; def m_nearbyint = runscript mfid_m_nearbyint enddef ;
64newscriptindex mfid_m_nextafter  ; mfid_m_nextafter  := scriptindex "m_nextafter"  ; def m_nextafter = runscript mfid_m_nextafter enddef ;
65newscriptindex mfid_m_pow        ; mfid_m_pow        := scriptindex "m_pow"        ; def m_pow       = runscript mfid_m_pow       enddef ;
66newscriptindex mfid_m_rad        ; mfid_m_rad        := scriptindex "m_rad"        ; def m_rad       = runscript mfid_m_rad       enddef ;
67newscriptindex mfid_m_remainder  ; mfid_m_remainder  := scriptindex "m_remainder"  ; def m_remainder = runscript mfid_m_remainder enddef ;
68newscriptindex mfid_m_remquo     ; mfid_m_remquo     := scriptindex "m_remquo"     ; def m_remquo    = runscript mfid_m_remquo    enddef ;
69newscriptindex mfid_m_round      ; mfid_m_round      := scriptindex "m_round"      ; def m_round     = runscript mfid_m_round     enddef ;
70newscriptindex mfid_m_scalbn     ; mfid_m_scalbn     := scriptindex "m_scalbn"     ; def m_scalbn    = runscript mfid_m_scalbn    enddef ;
71newscriptindex mfid_m_sin        ; mfid_m_sin        := scriptindex "m_sin"        ; def m_sin       = runscript mfid_m_sin       enddef ;
72newscriptindex mfid_m_sinh       ; mfid_m_sinh       := scriptindex "m_sinh"       ; def m_sinh      = runscript mfid_m_sinh      enddef ;
73newscriptindex mfid_m_sqrt       ; mfid_m_sqrt       := scriptindex "m_sqrt"       ; def m_sqrt      = runscript mfid_m_sqrt      enddef ;
74newscriptindex mfid_m_tan        ; mfid_m_tan        := scriptindex "m_tan"        ; def m_tan       = runscript mfid_m_tan       enddef ;
75newscriptindex mfid_m_tanh       ; mfid_m_tanh       := scriptindex "m_tanh"       ; def m_tanh      = runscript mfid_m_tanh      enddef ;
76newscriptindex mfid_m_tgamma     ; mfid_m_tgamma     := scriptindex "m_tgamma"     ; def m_tgamma    = runscript mfid_m_tgamma    enddef ;
77newscriptindex mfid_m_trunc      ; mfid_m_trunc      := scriptindex "m_trunc"      ; def m_trunc     = runscript mfid_m_trunc     enddef ;
78newscriptindex mfid_m_yz         ; mfid_m_yz         := scriptindex "m_y0"         ; def m_yz        = runscript mfid_m_yz        enddef ; % y0
79newscriptindex mfid_m_y          ; mfid_m_y          := scriptindex "m_y1"         ; def m_y         = runscript mfid_m_y         enddef ; % y1
80newscriptindex mfid_m_yn         ; mfid_m_yn         := scriptindex "m_yn"         ; def m_yn        = runscript mfid_m_yn        enddef ;
81
82newscriptindex mfid_c_sin        ; mfid_c_sin        := scriptindex "c_sin"        ; def c_sin       = runscript mfid_c_sin       enddef ;
83newscriptindex mfid_c_cos        ; mfid_c_cos        := scriptindex "c_cos"        ; def c_cos       = runscript mfid_c_cos       enddef ;
84newscriptindex mfid_c_tan        ; mfid_c_tan        := scriptindex "c_tan"        ; def c_tan       = runscript mfid_c_tan       enddef ;
85newscriptindex mfid_c_sinh       ; mfid_c_sinh       := scriptindex "c_sinh"       ; def c_sinh      = runscript mfid_c_sinh      enddef ;
86newscriptindex mfid_c_cosh       ; mfid_c_cosh       := scriptindex "c_cosh"       ; def c_cosh      = runscript mfid_c_cosh      enddef ;
87newscriptindex mfid_c_tanh       ; mfid_c_tanh       := scriptindex "c_tanh"       ; def c_tanh      = runscript mfid_c_tanh      enddef ;
88
89newscriptindex mfid_c_asin       ; mfid_c_asin       := scriptindex "c_asin"       ; def c_asin      = runscript mfid_c_asin      enddef ;
90newscriptindex mfid_c_acos       ; mfid_c_acos       := scriptindex "c_acos"       ; def c_acos      = runscript mfid_c_acos      enddef ;
91newscriptindex mfid_c_atan       ; mfid_c_atan       := scriptindex "c_atan"       ; def c_atan      = runscript mfid_c_atan      enddef ;
92newscriptindex mfid_c_asinh      ; mfid_c_asinh      := scriptindex "c_asinh"      ; def c_asinh     = runscript mfid_c_asinh     enddef ;
93newscriptindex mfid_c_acosh      ; mfid_c_acosh      := scriptindex "c_acosh"      ; def c_acosh     = runscript mfid_c_acosh     enddef ;
94newscriptindex mfid_c_atanh      ; mfid_c_atanh      := scriptindex "c_atanh"      ; def c_atanh     = runscript mfid_c_atanh     enddef ;
95
96newscriptindex mfid_c_sqrt       ; mfid_c_sqrt       := scriptindex "c_sqrt"       ; def c_sqrt      = runscript mfid_c_sqrt      enddef ;
97newscriptindex mfid_c_abs        ; mfid_c_abs        := scriptindex "c_abs"        ; def c_abs       = runscript mfid_c_abs       enddef ;
98newscriptindex mfid_c_arg        ; mfid_c_arg        := scriptindex "c_arg"        ; def c_arg       = runscript mfid_c_arg       enddef ;
99newscriptindex mfid_c_conj       ; mfid_c_conj       := scriptindex "c_conj"       ; def c_conj      = runscript mfid_c_conj      enddef ;
100newscriptindex mfid_c_exp        ; mfid_c_exp        := scriptindex "c_exp"        ; def c_exp       = runscript mfid_c_exp       enddef ;
101newscriptindex mfid_c_log        ; mfid_c_log        := scriptindex "c_log"        ; def c_log       = runscript mfid_c_log       enddef ;
102newscriptindex mfid_c_proj       ; mfid_c_proj       := scriptindex "c_proj"       ; def c_proj      = runscript mfid_c_proj      enddef ;
103
104newscriptindex mfid_c_erf        ; mfid_c_erf        := scriptindex "c_erf"        ; def c_erf       = runscript mfid_c_erf       enddef ;
105newscriptindex mfid_c_erfc       ; mfid_c_erfc       := scriptindex "c_erfc"       ; def c_erfc      = runscript mfid_c_erfc      enddef ;
106newscriptindex mfid_c_erfcx      ; mfid_c_erfcx      := scriptindex "c_erfcx"      ; def c_erfcx     = runscript mfid_c_erfcx     enddef ;
107newscriptindex mfid_c_erfi       ; mfid_c_erfi       := scriptindex "c_erfi"       ; def c_erfi      = runscript mfid_c_erfi      enddef ;
108
109%              mfid_c_imag       ; mfid_c_acos       := scriptindex "c_imag"       ; def c_imag      = runscript mfid_c_imag      enddef ;
110%              mfid_c_real       ; mfid_c_acos       := scriptindex "c_real"       ; def c_real      = runscript mfid_c_real      enddef ;
111%              mfid_c_neg        ; mfid_c_neg        := scriptindex "c_neg"        ; def c_neg       = runscript mfid_c_neg       enddef ;
112
113newscriptindex mfid_c_pow        ; mfid_c_pow        := scriptindex "c_pow"        ; def c_pow (expr a,b) = runscript mfid_c_pow a b enddef ;
114%              mfid_c_add        ; mfid_c_add        := scriptindex "c_add"        ; def c_add (expr a,b) = runscript mfid_c_add a b enddef ;
115%              mfid_c_sub        ; mfid_c_sub        := scriptindex "c_sub"        ; def c_sub (expr a,b) = runscript mfid_c_sub a b enddef ;
116newscriptindex mfid_c_mul        ; mfid_c_mul        := scriptindex "c_mul"        ; def c_mul (expr a,b) = runscript mfid_c_mul a b enddef ;
117newscriptindex mfid_c_div        ; mfid_c_div        := scriptindex "c_div"        ; def c_div (expr a,b) = runscript mfid_c_div a b enddef ;
118
119newscriptindex mfid_c_voigt      ; mfid_c_voigt      := scriptindex "c_voigt"      ; def c_voigt     (expr a,b,c) = runscript mfid_c_voigt      a b c enddef ;
120newscriptindex mfid_c_voigt_hwhm ; mfid_c_voigt_hwhm := scriptindex "c_voigt_hwhm" ; def c_voigt_hwhm(expr a,b)   = runscript mfid_c_voigt_hwhm a b   enddef ;
121
122vardef c_add (expr a, b) = a + b   enddef ;
123vardef c_sub (expr a, b) = a + b   enddef ;
124vardef c_imag(expr a)    = ypart a enddef ;
125vardef c_real(expr a)    = xpart a enddef ;
126vardef c_neg (expr a)    = -a      enddef ;
127
128if (numbersystem == "scaled") or (numbersystem == "double") :
129
130  % vardef sqrt primary x = m_sqrt x                            enddef ;
131
132  % vardef sqr  primary x = x*x                                 enddef ;
133  % vardef log  primary x = if x=0: 0 else: mlog(x)/mlog(10) fi enddef ;
134  % vardef ln   primary x = if x=0: 0 else: mlog(x)/256 fi      enddef ;
135  % vardef exp  primary x = (mexp 256)**x                       enddef ;
136  % vardef inv  primary x = if x=0: 0 else: x**-1 fi            enddef ;
137
138    vardef sin  primary x = m_sin  x enddef ; vardef sinh  primary x = m_sinh  x enddef ;
139    vardef cos  primary x = m_cos  x enddef ; vardef cosh  primary x = m_cosh  x enddef ;
140    vardef tan  primary x = m_tan  x enddef ; vardef tanh  primary x = m_tanh  x enddef ;
141    vardef asin primary x = m_asin x enddef ; vardef asinh primary x = m_asinh x enddef ;
142    vardef acos primary x = m_acos x enddef ; vardef acosh primary x = m_acosh x enddef ;
143    vardef atan primary x = m_atan x enddef ; vardef atanh primary x = m_atanh x enddef ;
144
145    vardef invsin primary x = (m_asin(x))/radian enddef ;
146    vardef invcos primary x = (m_acos(x))/radian enddef ;
147    vardef invtan primary x = (m_atan(x))/radian enddef ;
148
149  % vardef sind primary x = angle(m_sin x) enddef ;
150  % vardef cosd primary x = angle(m_cos x) enddef ;
151  % vardef tand primary x = angle(m_tan x) enddef ;
152
153    vardef asind primary x = angle(m_asin x) enddef ;
154    vardef acosd primary x = angle(m_acos x) enddef ;
155    vardef atand primary x = angle(m_atan x) enddef ;
156
157  % vardef tand primary x = sind(x)/cosd(x)  enddef ;
158  % vardef cotd primary x = cosd(x)/sind(x)  enddef ;
159
160else : % decimal
161
162    vardef sin    primary x = sind(x*radian)   enddef ;
163    vardef cos    primary x = cosd(x*radian)   enddef ;
164    vardef tan    primary x = sin(x)/cos(x)    enddef ;
165    vardef asin   primary x = angle((1+-+x,x)) enddef ;
166    vardef acos   primary x = angle((x,1+-+x)) enddef ;
167    vardef atan   primary x = angle(1,x)       enddef ;
168
169    vardef invsin primary x = (asin(x))/radian enddef ;
170    vardef invcos primary x = (acos(x))/radian enddef ;
171    vardef invtan primary x = (atan(x))/radian enddef ;
172
173fi ;
174
175permanent
176    m_acos, m_acosh, m_asin, m_asinh, m_atan, m_atantwo, m_atanh, m_cbrt, m_ceil,
177    m_copysign, m_cos, m_cosh, m_deg, m_erf, m_erfc, m_exp, m_exptwo, m_expm, m_fabs,
178    m_fdim, m_floor, m_fma, m_fmax, m_fmin, m_fmod, m_frexp, m_gamma, m_hypot,
179    m_isfinite, m_isinf, m_isnan, m_isnormal, m_jz, m_j, m_jn, m_ldexp, m_lgamma,
180    m_log, m_logten, m_logp, m_logtwo, m_logb, m_modf, m_nearbyint, m_nextafter,
181    m_pow, m_rad, m_remainder, m_remquo, m_round, m_scalbn, m_sin, m_sinh, m_sqrt,
182    m_tan, m_tanh, m_tgamma, m_trunc, m_yz, m_y, m_yn,
183
184    c_sin, c_cos, c_tan, c_sinh, c_cosh, c_tanh, c_asin, c_acos, c_atan, c_asinh,
185    c_acosh, c_atanh, c_sqrt, c_abs, c_arg, c_conj, c_exp, c_log, c_proj, c_erf,
186    c_erfc, c_erfcx, c_erfi, c_imag, c_real, c_neg, c_pow, c_add, c_sub, c_mul,
187    c_div, c_voigt, c_voigt_hwhm, c_add, c_sub, c_imag, c_real, c_neg,
188
189    % sqrt, sind, cosd, % these are primitives
190
191    sqr, log, ln, exp, inv,
192    sin, cos, tan, asin, acos, atan, invsin, invcos, invtan,
193    tand, cotd,
194    sinh, cosh, tanh, asinh, acosh,
195    atanh, asind, acosd, atand
196;
197
198