File: expint.m

package info (click to toggle)
octave 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,396 kB
  • ctags: 45,547
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (266 lines) | stat: -rw-r--r-- 6,919 bytes parent folder | download | duplicates (3)
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
## Copyright (C) 2006, 2013 Sylvain Pelissier
##
## This file is part of Octave.
##
## Octave 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.
##
## Octave 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 Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## Author: Sylvain Pelissier <sylvain.pelissier@gmail.com>

## -*- texinfo -*-
## @deftypefn {Function File} {} expint (@var{x})
## Compute the exponential integral:
## @tex
## $$
## {\rm E_1} (x) = \int_x^\infty {e^{-t} \over t} dt
## $$
## @end tex
## @ifnottex
##
## @example
## @group
##            infinity
##           /
## E_1 (x) = | exp (-t)/t dt
##           /
##          x
## @end group
## @end example
##
## @end ifnottex
## Note: For compatibility, this functions uses the @sc{matlab} definition
## of the exponential integral.  Most other sources refer to this particular
## value as @math{E_1 (x)}, and the exponential integral is
## @tex
## $$
## {\rm Ei} (x) = - \int_{-x}^\infty {e^{-t} \over t} dt.
## $$
## @end tex
## @ifnottex
##
## @example
## @group
##             infinity
##            /
## Ei (x) = - | exp (-t)/t dt
##            /
##          -x
## @end group
## @end example
##
## @end ifnottex
## The two definitions are related, for positive real values of @var{x}, by
## @tex
## $
## E_1 (-x) = -{\rm Ei} (x) - i\pi.
## $
## @end tex
## @ifnottex
## @w{@code{E_1 (-x) = -Ei (x) - i*pi}}.
## @end ifnottex
## @end deftypefn

function y = expint (x)

  if (nargin != 1)
    print_usage ();
  endif

  y = x;  # Copy over all values, including NaNs

  if (isreal (x))
    idx = (x >= 0);
    y(idx) = -expint_Ei (-x(idx));

    idx = (x < 0);
    y(idx) = -expint_Ei (-x(idx)) - i*pi;
  else
    idx = (imag (x) > 0);
    y(idx) = -expint_Ei (-x(idx)) - i*pi;

    idx = (imag (x) < 0);
    y(idx) = -expint_Ei (-x(idx)) + i*pi;

    isreal_idx = (imag (x) == 0);
    idx = (isreal_idx & real (x) >= 0);
    y(idx) = -expint_Ei (-x(idx));

    idx = (isreal_idx & real (x) < 0);
    y(idx) = -expint_Ei (-x(idx)) - i*pi;
  endif

endfunction

## -*- texinfo -*-
## @deftypefn {Function File} {@var{y} =} expint_Ei (@var{x})
## Compute the exponential integral:
## @verbatim
##                       infinity
##                      /
##    expint_Ei (x) = - | exp(-t)/t dt
##                      /
##                    -x
## @end verbatim
## @end deftypefn

function y = expint_Ei (x)

  y = zeros (size (x));
  F = @(x) exp (-x)./x;

  for t = 1:numel (x)
    xt = x(t);
    if (xt < 0 && imag (xt) == 0)
      ## Direct integration for most real inputs
      y(t) = -quad (F, -xt, Inf, [0, 1e-10]);
    elseif (xt > 2 && imag (xt) == 0)
      persistent Ei_2 = 4.954234356001890;
      y(t) = Ei_2 - quad (F, -xt, -2);
    elseif (abs (xt) < 10)
      ## Series Expansion for real (range [0,2]) or complex inputs (r < 10)
      k = 1;
      do
        term = xt^k / (k*factorial (k));
        y(t) += term;
      until (abs (term) < eps (abs (y(t))) / 2 || k++ >= 100)
      y(t) = 0.57721566490153286 + log (xt) + y(t);
    else
      ## FIXME: This expansion is accurate to only 1e-13 at the beginning
      ##        near 10+i, although it becomes more accurate as the magnitude
      ##        of xt grows.
      if (imag (xt) <= 0)
        persistent a1 = 4.03640;
        persistent a2 = 1.15198;
        persistent b1 = 5.03637;
        persistent b2 = 4.19160;
        y(t) = -(xt^2 - a1*xt + a2) ...
               / ((xt^2 - b1*xt + b2) * (-xt) * exp (-xt)) ...
               - i*pi;
      else
        y(t) = conj (expint_Ei (conj (xt)));
      endif;
    endif
  endfor
endfunction


## Test against A&S Table 5.1
%!test
%! x = [5:5:50]'/100;
%! gamma = 0.5772156649;
%! y_exp = [0.9876375971;
%!          0.9755453033;
%!          0.9637156702;
%!          0.9521414833;
%!          0.9408157528;
%!          0.9297317075;
%!          0.9188827858;
%!          0.9082626297;
%!          0.8978650778;
%!          0.8876841584 ];
%! y = (expint (x) + log(x) + gamma) ./ x;
%! assert (y, y_exp, 1e-9);
%!test
%! x = [50:5:95]'/100;
%! y_exp = [0.559773595;
%!          0.503364081;
%!          0.454379503;
%!          0.411516976;
%!          0.373768843;
%!          0.340340813;
%!          0.310596579;
%!          0.284019269;
%!          0.260183939;
%!          0.238737524 ];
%! y = expint (x);
%! assert (y, y_exp, 1e-9);
%!test
%! x = [100:5:145]'/100;
%! y_exp = [0.219383934;
%!          0.201872813;
%!          0.185990905;
%!          0.171555354;
%!          0.158408437;
%!          0.146413373;
%!          0.135450958;
%!          0.125416844;
%!          0.116219313;
%!          0.107777440 ];
%! y = expint (x);
%! assert (y, y_exp, 1e-9);
%!test
%! x = [150:5:200]'/100;
%! y_exp = [0.100019582;
%!          0.092882108;
%!          0.086308334;
%!          0.080247627;
%!          0.074654644;
%!          0.069488685;
%!          0.064713129;
%!          0.060294967;
%!          0.056204378;
%!          0.052414380;
%!          0.048900511 ];
%! y = expint (x);
%! assert (y, y_exp, 1e-9);

## Series expansion (-2 < x < 0)
## Expected values from Mathematica
%!test  
%! x = [-0.1; -0.5; -1; -1.5; -2];
%! y_exp = [ 1.6228128139692767  - i*pi;
%!          -0.45421990486317358 - i*pi;
%!          -1.8951178163559368  - i*pi;
%!          -3.3012854491297978  - i*pi;
%!          -4.9542343560018902  - i*pi];
%! y = expint (x);
%! assert (y, y_exp, eps (real (y_exp)));

## (x < -2, x real)
%!test  
%! x = [-2.5; -3; -10;-15; -25];
%! y_exp = [-7.0737658945786007   - i*pi;
%!          -9.9338325706254165   - i*pi;
%!          -2492.2289762418777   - i*pi;
%!          -234955.85249076830   - i*pi;
%!          -3.0059509065255486e9 - i*pi];
%! y = expint (x);
%! assert (y, y_exp, 8*eps (real (y_exp)));

## Complex values
%!test
%! x = [i; -1-i; 10-i; 10+i];
%! y_exp = [-0.33740392290096813   - i*0.62471325642771360;
%!          -1.7646259855638540    + i*0.75382280207927082;
%!          1.90746381979783120e-6 + i*3.67354374003294739e-6;
%!          1.90746381979783120e-6 - i*3.67354374003294739e-6];
%! y = expint (x);
%! assert (y, y_exp, 1e-12);

## Exceptional values (-Inf, Inf, NaN, 0, 0.37250741078)
%!test  
%! x = [-Inf; Inf; NaN; 0; -0.3725074107813668];
%! y_exp = [-Inf - i*pi;
%!          -Inf;  # should be 0;
%!          NaN;
%!          Inf;
%!          0 - i*pi];
%! y = expint (x);
%! assert (y, y_exp, 5*eps);

## Test input validation
%!error expint ()
%!error expint (1,2)