GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
mappers.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <cctype>
31
32#include "lo-ieee.h"
33#include "mappers.h"
34#include "oct-specfun.h"
35
36#include "defun.h"
37#include "error.h"
38#include "variables.h"
39
41
42DEFUN (abs, args, ,
43 doc: /* -*- texinfo -*-
44@deftypefn {} {@var{z} =} abs (@var{x})
45Compute the magnitude of @var{x}.
46
47The magnitude is defined as
48@tex
49$|z| = \sqrt{x^2 + y^2}$.
50@end tex
51@ifnottex
52|@var{z}| = @code{sqrt (x^2 + y^2)}.
53@end ifnottex
54
55For example:
56
57@example
58@group
59abs (3 + 4i)
60 @xresult{} 5
61@end group
62@end example
63@seealso{arg}
64@end deftypefn */)
65{
66 if (args.length () != 1)
67 print_usage ();
68
69 return ovl (args(0).abs ());
70}
71
72/*
73%!assert (abs (1), 1)
74%!assert (abs (-3.5), 3.5)
75%!assert (abs (3+4i), 5)
76%!assert (abs (3-4i), 5)
77%!assert (abs ([1.1, 3i; 3+4i, -3-4i]), [1.1, 3; 5, 5])
78
79%!assert (abs (single (1)), single (1))
80%!assert (abs (single (-3.5)), single (3.5))
81%!assert (abs (single (3+4i)), single (5))
82%!assert (abs (single (3-4i)), single (5))
83%!assert (abs (single ([1.1, 3i; 3+4i, -3-4i])), single ([1.1, 3; 5, 5]))
84
85%!error abs ()
86%!error abs (1, 2)
87*/
88
89DEFUN (acos, args, ,
90 doc: /* -*- texinfo -*-
91@deftypefn {} {@var{y} =} acos (@var{x})
92Compute the inverse cosine in radians for each element of @var{x}.
93@seealso{cos, acosd}
94@end deftypefn */)
95{
96 if (args.length () != 1)
97 print_usage ();
98
99 return ovl (args(0).acos ());
100}
101
102/*
103%!shared rt2, rt3
104%! rt2 = sqrt (2);
105%! rt3 = sqrt (3);
106
107%!test
108%! x = [1, rt3/2, rt2/2, 1/2, 0, -1/2, -rt2/2, -rt3/2, -1];
109%! v = [0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi];
110%! assert (acos (x), v, sqrt (eps));
111
112%!test
113%! x = single ([1, rt3/2, rt2/2, 1/2, 0, -1/2, -rt2/2, -rt3/2, -1]);
114%! v = single ([0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi]);
115%! assert (acos (x), v, sqrt (eps ("single")));
116
117## Test values on either side of branch cut
118%!test
119%! rval = 0;
120%! ival = 1.31695789692481671;
121%! obs = acos ([2, 2-i*eps, 2+i*eps]);
122%! exp = [rval + ival*i, rval + ival*i, rval - ival*i];
123%! assert (obs, exp, 3*eps);
124%! rval = pi;
125%! obs = acos ([-2, -2-i*eps, -2+i*eps]);
126%! exp = [rval - ival*i, rval + ival*i, rval - ival*i];
127%! assert (obs, exp, 5*eps);
128%! assert (acos ([2 0]), [ival*i, pi/2], 3*eps);
129%! assert (acos ([2 0i]), [ival*i, pi/2], 3*eps);
130
131## Test large magnitude arguments (bug #45507)
132## Test fails with older versions of libm, solution is to upgrade.
133%!testif ; ! __have_feature__ ("LLVM_LIBCXX") <*45507>
134%! x = [1, -1, i, -i] .* 1e150;
135%! v = [0, pi, pi/2, pi/2];
136%! assert (real (acos (x)), v);
137
138%!testif HAVE_LLVM_LIBCXX <52627>
139%! ## Same test code as above, but intended for test statistics with libc++.
140%! ## Their trig/hyperbolic functions have huge tolerances.
141%! ## LLVM libc++ returns 0 for x(2) and x(4).
142%! x = [1, -1, i, -i] .* 1e150;
143%! v = [0, pi, pi/2, pi/2];
144%! assert (real (acos (x)), v);
145
146%!error acos ()
147%!error acos (1, 2)
148*/
149
150DEFUN (acosh, args, ,
151 doc: /* -*- texinfo -*-
152@deftypefn {} {@var{y} =} acosh (@var{x})
153Compute the inverse hyperbolic cosine for each element of @var{x}.
154@seealso{cosh}
155@end deftypefn */)
156{
157 if (args.length () != 1)
158 print_usage ();
159
160 return ovl (args(0).acosh ());
161}
162
163/*
164%!testif ; ! __have_feature__ ("BSD_LIBC")
165%! x = [1, 0, -1, 0];
166%! v = [0, pi/2*i, pi*i, pi/2*i];
167%! assert (acosh (x), v, sqrt (eps));
168
169%!testif HAVE_BSD_LIBC <52627>
170%! ## Same test code as above, but intended only for test statistics with BSD
171%! ## libc. The trig/hyperbolic functions in that C library used to have huge
172%! ## tolerances.
173%! x = [1, 0, -1, 0];
174%! v = [0, pi/2*i, pi*i, pi/2*i];
175%! assert (acosh (x), v, sqrt (eps));
176
177## FIXME: std::acosh on Windows platforms and with LLVM libc++, returns a
178## result that differs by 1 in the last significant digit. This is ~30*eps
179## which is quite large.
180## The decision now (9/15/2016) is to mark the test with a bug number so
181## it is understood why it is failing, and wait for MinGW and LLVM to improve
182## their std library.
183%!test <49091>
184%! re = 2.99822295029797;
185%! im = pi/2;
186%! assert (acosh (-10i), re - i*im);
187
188%!testif ; ! __have_feature__ ("BSD_LIBC")
189%! x = single ([1, 0, -1, 0]);
190%! v = single ([0, pi/2*i, pi*i, pi/2*i]);
191%! assert (acosh (x), v, sqrt (eps ("single")));
192
193%!testif HAVE_BSD_LIBC <52627>
194%! ## Same test code as above, but intended only for test statistics with BSD
195%! ## libc. The trig/hyperbolic functions in that C library used to have huge
196%! ## tolerances.
197%! x = single ([1, 0, -1, 0]);
198%! v = single ([0, pi/2*i, pi*i, pi/2*i]);
199%! assert (acosh (x), v, sqrt (eps ("single")));
200
201%!test <49091>
202%! re = single (2.99822295029797);
203%! im = single (pi/2);
204%! assert (acosh (single (10i)), re + i*im, 5* eps ("single"));
205%! assert (acosh (single (-10i)), re - i*im, 5* eps ("single"));
206
207## Test large magnitude arguments (bug #45507)
208## Test fails with older versions of libm, solution is to upgrade.
209%!testif ; ! __have_feature__ ("LLVM_LIBCXX") <*45507>
210%! x = [1, -1, i, -i] .* 1e150;
211%! v = [0, pi, pi/2, -pi/2];
212%! assert (imag (acosh (x)), v);
213
214%!testif HAVE_LLVM_LIBCXX <52627>
215%! ## Same test code as above, but intended for test statistics with libc++.
216%! ## Their trig/hyperbolic functions have huge tolerances.
217%! ## LLVM libc++ returns 0 for x(2) and x(4).
218%! x = [1, -1, i, -i] .* 1e150;
219%! v = [0, pi, pi/2, -pi/2];
220%! assert (imag (acosh (x)), v);
221
222%!error acosh ()
223%!error acosh (1, 2)
224*/
225
226DEFUN (angle, args, ,
227 doc: /* -*- texinfo -*-
228@deftypefn {} {@var{theta} =} angle (@var{z})
229@xref{XREFarg,,arg}.
230@seealso{arg}
231@end deftypefn */)
232{
233 if (args.length () != 1)
234 print_usage ();
235
236 return ovl (args(0).arg ());
237}
238
239DEFUN (arg, args, ,
240 doc: /* -*- texinfo -*-
241@deftypefn {} {@var{theta} =} arg (@var{z})
242@deftypefnx {} {@var{theta} =} angle (@var{z})
243Compute the argument, i.e., angle of @var{z}.
244
245This is defined as,
246@tex
247$\theta = atan2 (y, x),$
248@end tex
249@ifnottex
250@var{theta} = @code{atan2 (@var{y}, @var{x})},
251@end ifnottex
252in radians.
253
254For example:
255
256@example
257@group
258arg (3 + 4i)
259 @xresult{} 0.92730
260@end group
261@end example
262@seealso{abs}
263@end deftypefn */)
264{
265 if (args.length () != 1)
266 print_usage ();
267
268 return ovl (args(0).arg ());
269}
270
271/*
272%!assert (arg (1), 0)
273%!assert (arg (i), pi/2)
274%!assert (arg (-1), pi)
275%!assert (arg (-i), -pi/2)
276%!assert (arg ([1, i; -1, -i]), [0, pi/2; pi, -pi/2])
277
278%!assert (arg (single (1)), single (0))
279%!assert (arg (single (i)), single (pi/2))
280%!test
281%! if (ismac ())
282%! ## Avoid failing on a MacOS feature or bug, depending on your view
283%! assert (arg (single (-1)), single (pi), 2* eps (single (1)));
284%! else
285%! assert (arg (single (-1)), single (pi));
286%! endif
287%!assert (arg (single (-i)), single (-pi/2))
288%!assert (arg (single ([1, i; -1, -i])),
289%! single ([0, pi/2; pi, -pi/2]), 2e1* eps ("single"))
290
291%!error arg ()
292%!error arg (1, 2)
293*/
294
295DEFUN (asin, args, ,
296 doc: /* -*- texinfo -*-
297@deftypefn {} {@var{y} =} asin (@var{x})
298Compute the inverse sine in radians for each element of @var{x}.
299@seealso{sin, asind}
300@end deftypefn */)
301{
302 if (args.length () != 1)
303 print_usage ();
304
305 return ovl (args(0).asin ());
306}
307
308/*
309%!shared rt2, rt3
310%! rt2 = sqrt (2);
311%! rt3 = sqrt (3);
312
313%!test
314%! x = [0, 1/2, rt2/2, rt3/2, 1, rt3/2, rt2/2, 1/2, 0];
315%! v = [0, pi/6, pi/4, pi/3, pi/2, pi/3, pi/4, pi/6, 0];
316%! assert (asin (x), v, sqrt (eps));
317
318%!test
319%! x = single ([0, 1/2, rt2/2, rt3/2, 1, rt3/2, rt2/2, 1/2, 0]);
320%! v = single ([0, pi/6, pi/4, pi/3, pi/2, pi/3, pi/4, pi/6, 0]);
321%! assert (asin (x), v, sqrt (eps ("single")));
322
323## Test values on either side of branch cut
324%!test
325%! rval = pi/2;
326%! ival = 1.31695789692481635;
327%! obs = asin ([2, 2-i*eps, 2+i*eps]);
328%! exp = [rval - ival*i, rval - ival*i, rval + ival*i];
329%! if (__have_feature__ ("BSD_LIBC"))
330%! ## BSD libc seems to implement asin with less accuracy.
331%! tol = 6*eps;
332%! else
333%! tol = 2*eps;
334%! endif
335%! assert (obs, exp, tol);
336%! obs = asin ([-2, -2-i*eps, -2+i*eps]);
337%! exp = [-rval + ival*i, -rval - ival*i, -rval + ival*i];
338%! assert (obs, exp, tol);
339%! assert (asin ([2 0]), [rval - ival*i, 0], tol);
340%! assert (asin ([2 0i]), [rval - ival*i, 0], tol);
341
342## Test large magnitude arguments (bug #45507)
343## Test fails with older versions of libm, solution is to upgrade.
344%!testif ; ! __have_feature__ ("LLVM_LIBCXX") <*45507>
345%! x = [1, -1, i, -i] .* 1e150;
346%! v = [pi/2, -pi/2, 0, -0];
347%! assert (real (asin (x)), v);
348
349%!testif HAVE_LLVM_LIBCXX <52627>
350%! ## Same test code as above, but intended for test statistics with libc++.
351%! ## Their trig/hyperbolic functions have huge tolerances.
352%! ## LLVM libc++ returns 0 for x(1) and x(2).
353%! x = [1, -1, i, -i] .* 1e150;
354%! v = [pi/2, -pi/2, 0, -0];
355%! assert (real (asin (x)), v);
356
357%!error asin ()
358%!error asin (1, 2)
359*/
360
361DEFUN (asinh, args, ,
362 doc: /* -*- texinfo -*-
363@deftypefn {} {@var{y} =} asinh (@var{x})
364Compute the inverse hyperbolic sine for each element of @var{x}.
365@seealso{sinh}
366@end deftypefn */)
367{
368 if (args.length () != 1)
369 print_usage ();
370
371 return ovl (args(0).asinh ());
372}
373
374/*
375%!test
376%! v = [0, pi/2*i, 0, -pi/2*i];
377%! x = [0, i, 0, -i];
378%! assert (asinh (x), v, sqrt (eps));
379
380%!test
381%! v = single ([0, pi/2*i, 0, -pi/2*i]);
382%! x = single ([0, i, 0, -i]);
383%! assert (asinh (x), v, sqrt (eps ("single")));
384
385## Test large magnitude arguments (bug #45507)
386## Test fails with older versions of libm, solution is to upgrade.
387%!testif ; ! __have_feature__ ("LLVM_LIBCXX") <*45507>
388%! x = [1, -1, i, -i] .* 1e150;
389%! v = [0, 0, pi/2, -pi/2];
390%! assert (imag (asinh (x)), v);
391
392%!testif HAVE_LLVM_LIBCXX <52627>
393%! ## Same test code as above, but intended for test statistics with libc++.
394%! ## Their trig/hyperbolic functions have huge tolerances.
395%! ## LLVM libc++ returns 0 for x(4).
396%! x = [1, -1, i, -i] .* 1e150;
397%! v = [0, 0, pi/2, -pi/2];
398%! assert (imag (asinh (x)), v);
399
400%!error asinh ()
401%!error asinh (1, 2)
402*/
403
404DEFUN (atan, args, ,
405 doc: /* -*- texinfo -*-
406@deftypefn {} {@var{y} =} atan (@var{x})
407Compute the inverse tangent in radians for each element of @var{x}.
408@seealso{tan, atand}
409@end deftypefn */)
410{
411 if (args.length () != 1)
412 print_usage ();
413
414 return ovl (args(0).atan ());
415}
416
417/*
418%!shared rt2, rt3
419%! rt2 = sqrt (2);
420%! rt3 = sqrt (3);
421
422%!test
423%! v = [0, pi/6, pi/4, pi/3, -pi/3, -pi/4, -pi/6, 0];
424%! x = [0, rt3/3, 1, rt3, -rt3, -1, -rt3/3, 0];
425%! assert (atan (x), v, sqrt (eps));
426
427%!test
428%! v = single ([0, pi/6, pi/4, pi/3, -pi/3, -pi/4, -pi/6, 0]);
429%! x = single ([0, rt3/3, 1, rt3, -rt3, -1, -rt3/3, 0]);
430%! assert (atan (x), v, sqrt (eps ("single")));
431
432## Test large magnitude arguments (bug #44310, bug #45507)
433%!test <*44310>
434%! x = [1, -1, i, -i] .* 1e150;
435%! v = [pi/2, -pi/2, pi/2, -pi/2];
436%! assert (real (atan (x)), v);
437%! assert (imag (atan (x)), [0, 0, 0, 0], eps);
438
439%!error atan ()
440%!error atan (1, 2)
441*/
442
443DEFUN (atanh, args, ,
444 doc: /* -*- texinfo -*-
445@deftypefn {} {@var{y} =} atanh (@var{x})
446Compute the inverse hyperbolic tangent for each element of @var{x}.
447@seealso{tanh}
448@end deftypefn */)
449{
450 if (args.length () != 1)
451 print_usage ();
452
453 return ovl (args(0).atanh ());
454}
455
456/*
457%!test
458%! v = [0, 0];
459%! x = [0, 0];
460%! assert (atanh (x), v, sqrt (eps));
461
462%!test
463%! v = single ([0, 0]);
464%! x = single ([0, 0]);
465%! assert (atanh (x), v, sqrt (eps ("single")));
466
467## Test large magnitude arguments (bug #44310, bug #45507)
468%!test <*44310>
469%! x = [1, -1, i, -i] .* 1e150;
470%! v = [pi/2, pi/2, pi/2, -pi/2];
471%! assert (imag (atanh (x)), v);
472%! assert (real (atanh (x)), [0, 0, 0, 0], eps);
473
474%!error atanh ()
475%!error atanh (1, 2)
476*/
477
478DEFUN (cbrt, args, ,
479 doc: /* -*- texinfo -*-
480@deftypefn {} {@var{y} =} cbrt (@var{x})
481Compute the real-valued cube root of each element of @var{x}.
482
483Unlike @code{@var{x}^(1/3)}, the result will be negative if @var{x} is
484negative.
485
486If any element of @var{x} is complex, @code{cbrt} aborts with an error.
487@seealso{nthroot}
488@end deftypefn */)
489{
490 if (args.length () != 1)
491 print_usage ();
492
493 return ovl (args(0).cbrt ());
494}
495
496/*
497%!assert (cbrt (64), 4)
498%!assert (cbrt (-125), -5)
499%!assert (cbrt (0), 0)
500%!assert (cbrt (Inf), Inf)
501%!assert (cbrt (-Inf), -Inf)
502%!assert (cbrt (NaN), NaN)
503%!assert (cbrt (2^300), 2^100)
504%!assert (cbrt (125*2^300), 5*2^100)
505
506%!error cbrt ()
507%!error cbrt (1, 2)
508*/
509
510DEFUN (ceil, args, ,
511 doc: /* -*- texinfo -*-
512@deftypefn {} {@var{y} =} ceil (@var{x})
513Return the smallest integer not less than @var{x}.
514
515This is equivalent to rounding towards positive infinity.
516
517If @var{x} is complex, return
518@code{ceil (real (@var{x})) + ceil (imag (@var{x})) * I}.
519
520@example
521@group
522ceil ([-2.7, 2.7])
523 @xresult{} -2 3
524@end group
525@end example
526@seealso{floor, round, fix}
527@end deftypefn */)
528{
529 if (args.length () != 1)
530 print_usage ();
531
532 return ovl (args(0).ceil ());
533}
534
535/*
536## double precision
537%!assert (ceil ([2, 1.1, -1.1, -1]), [2, 2, -1, -1])
538
539## complex double precision
540%!assert (ceil ([2+2i, 1.1+1.1i, -1.1-1.1i, -1-i]), [2+2i, 2+2i, -1-i, -1-i])
541
542## single precision
543%!assert (ceil (single ([2, 1.1, -1.1, -1])), single ([2, 2, -1, -1]))
544
545## complex single precision
546%!assert (ceil (single ([2+2i, 1.1+1.1i, -1.1-1.1i, -1-i])),
547%! single ([2+2i, 2+2i, -1-i, -1-i]))
548
549%!error ceil ()
550%!error ceil (1, 2)
551*/
552
553DEFUN (conj, args, ,
554 doc: /* -*- texinfo -*-
555@deftypefn {} {@var{zc} =} conj (@var{z})
556Return the complex conjugate of @var{z}.
557
558The complex conjugate is defined as
559@tex
560$\bar{z} = x - iy$.
561@end tex
562@ifnottex
563@code{conj (@var{z})} = @var{x} - @var{i}@var{y}.
564@end ifnottex
565@seealso{real, imag}
566@end deftypefn */)
567{
568 if (args.length () != 1)
569 print_usage ();
570
571 return ovl (args(0).conj ());
572}
573
574/*
575%!assert (conj (1), 1)
576%!assert (conj (i), -i)
577%!assert (conj (1+i), 1-i)
578%!assert (conj (1-i), 1+i)
579%!assert (conj ([-1, -i; -1+i, -1-i]), [-1, i; -1-i, -1+i])
580
581%!assert (conj (single (1)), single (1))
582%!assert (conj (single (i)), single (-i))
583%!assert (conj (single (1+i)), single (1-i))
584%!assert (conj (single (1-i)), single (1+i))
585%!assert (conj (single ([-1, -i; -1+i, -1-i])), single ([-1, i; -1-i, -1+i]))
586
587%!error conj ()
588%!error conj (1, 2)
589*/
590
591DEFUN (cos, args, ,
592 doc: /* -*- texinfo -*-
593@deftypefn {} {@var{y} =} cos (@var{x})
594Compute the cosine for each element of @var{x} in radians.
595@seealso{acos, cosd, cosh}
596@end deftypefn */)
597{
598 if (args.length () != 1)
599 print_usage ();
600
601 return ovl (args(0).cos ());
602}
603
604/*
605%!shared rt2, rt3
606%! rt2 = sqrt (2);
607%! rt3 = sqrt (3);
608
609%!test
610%! x = [0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi];
611%! v = [1, rt3/2, rt2/2, 1/2, 0, -1/2, -rt2/2, -rt3/2, -1];
612%! assert (cos (x), v, sqrt (eps));
613
614%!test
615%! rt2 = sqrt (2);
616%! rt3 = sqrt (3);
617%! x = single ([0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi]);
618%! v = single ([1, rt3/2, rt2/2, 1/2, 0, -1/2, -rt2/2, -rt3/2, -1]);
619%! assert (cos (x), v, sqrt (eps ("single")));
620
621%!error cos ()
622%!error cos (1, 2)
623*/
624
625DEFUN (cosh, args, ,
626 doc: /* -*- texinfo -*-
627@deftypefn {} {@var{y} =} cosh (@var{x})
628Compute the hyperbolic cosine for each element of @var{x}.
629@seealso{acosh, sinh, tanh}
630@end deftypefn */)
631{
632 if (args.length () != 1)
633 print_usage ();
634
635 return ovl (args(0).cosh ());
636}
637
638/*
639%!test
640%! x = [0, pi/2*i, pi*i, 3*pi/2*i];
641%! v = [1, 0, -1, 0];
642%! assert (cosh (x), v, sqrt (eps));
643
644%!test
645%! x = single ([0, pi/2*i, pi*i, 3*pi/2*i]);
646%! v = single ([1, 0, -1, 0]);
647%! assert (cosh (x), v, sqrt (eps ("single")));
648
649%!error cosh ()
650%!error cosh (1, 2)
651*/
652
653DEFUN (erf, args, ,
654 doc: /* -*- texinfo -*-
655@deftypefn {} {@var{v} =} erf (@var{z})
656Compute the error function.
657
658The error function is defined as
659@tex
660$$
661 {\rm erf} (z) = {2 \over \sqrt{\pi}}\int_0^z e^{-t^2} dt
662$$
663@end tex
664@ifnottex
665
666@example
667@group
668 z
669 2 /
670erf (z) = --------- * | e^(-t^2) dt
671 sqrt (pi) /
672 t=0
673@end group
674@end example
675
676@end ifnottex
677@seealso{erfc, erfcx, erfi, dawson, erfinv, erfcinv}
678@end deftypefn */)
679{
680 if (args.length () != 1)
681 print_usage ();
682
683 return ovl (args(0).erf ());
684}
685
686/*
687%!test
688%! a = -1i* sqrt (-1/(6.4187*6.4187));
689%! assert (erf (a), erf (real (a)));
690
691%!test
692%! x = [0,.5,1];
693%! v = [0, .520499877813047, .842700792949715];
694%! assert (erf (x), v, 1.e-10);
695%! assert (erf (-x), -v, 1.e-10);
696%! assert (erfc (x), 1-v, 1.e-10);
697%! assert (erfinv (v), x, 1.e-10);
698
699%!test
700%! a = -1i* sqrt (single (-1/(6.4187*6.4187)));
701%! assert (erf (a), erf (real (a)));
702
703%!test
704%! x = single ([0,.5,1]);
705%! v = single ([0, .520499877813047, .842700792949715]);
706%! assert (erf (x), v, 1.e-6);
707%! assert (erf (-x), -v, 1.e-6);
708%! assert (erfc (x), 1-v, 1.e-6);
709%! assert (erfinv (v), x, 1.e-6);
710
711%!test
712%! x = [1+2i,-1+2i,1e-6+2e-6i,0+2i];
713%! v = [-0.53664356577857-5.04914370344703i, 0.536643565778565-5.04914370344703i, 0.112837916709965e-5+0.225675833419178e-5i, 18.5648024145755526i];
714%! assert (erf (x), v, -1.e-10);
715%! assert (erf (-x), -v, -1.e-10);
716%! assert (erfc (x), 1-v, -1.e-10);
717
718%!error erf ()
719%!error erf (1, 2)
720*/
721
722DEFUN (erfinv, args, ,
723 doc: /* -*- texinfo -*-
724@deftypefn {} {@var{y} =} erfinv (@var{x})
725Compute the inverse error function.
726
727The inverse error function is defined such that
728
729@example
730erf (@var{y}) == @var{x}
731@end example
732@seealso{erf, erfc, erfcx, erfi, dawson, erfcinv}
733@end deftypefn */)
734{
735 if (args.length () != 1)
736 print_usage ();
737
738 return ovl (args(0).erfinv ());
739}
740
741/*
742## middle region
743%!assert (erf (erfinv ([-0.9 -0.3 0 0.4 0.8])), [-0.9 -0.3 0 0.4 0.8], eps)
744%!assert (erf (erfinv (single ([-0.9 -0.3 0 0.4 0.8]))),
745%! single ([-0.9 -0.3 0 0.4 0.8]), eps ("single"))
746## tail region
747%!assert (erf (erfinv ([-0.999 -0.99 0.9999 0.99999])),
748%! [-0.999 -0.99 0.9999 0.99999], eps)
749%!assert (erf (erfinv (single ([-0.999 -0.99 0.9999 0.99999]))),
750%! single ([-0.999 -0.99 0.9999 0.99999]), eps ("single"))
751## backward - loss of accuracy
752%!assert (erfinv (erf ([-3 -1 -0.4 0.7 1.3 2.8])),
753%! [-3 -1 -0.4 0.7 1.3 2.8], -1e-12)
754%!assert (erfinv (erf (single ([-3 -1 -0.4 0.7 1.3 2.8]))),
755%! single ([-3 -1 -0.4 0.7 1.3 2.8]), -1e-4)
756## exceptional
757%!assert (erfinv ([-1, 1, 1.1, -2.1]), [-Inf, Inf, NaN, NaN])
758%!error erfinv (1+2i)
759
760%!error erfinv ()
761%!error erfinv (1, 2)
762*/
763
764DEFUN (erfcinv, args, ,
765 doc: /* -*- texinfo -*-
766@deftypefn {} {@var{y} =} erfcinv (@var{x})
767Compute the inverse complementary error function.
768
769The inverse complementary error function is defined such that
770
771@example
772erfc (@var{y}) == @var{x}
773@end example
774@seealso{erfc, erf, erfcx, erfi, dawson, erfinv}
775@end deftypefn */)
776{
777 if (args.length () != 1)
778 print_usage ();
779
780 return ovl (args(0).erfcinv ());
781}
782
783/*
784## middle region
785%!assert (erfc (erfcinv ([1.9 1.3 1 0.6 0.2])), [1.9 1.3 1 0.6 0.2], eps)
786%!assert (erfc (erfcinv (single ([1.9 1.3 1 0.6 0.2]))),
787%! single ([1.9 1.3 1 0.6 0.2]), eps ("single"))
788## tail region
789%!assert (erfc (erfcinv ([0.001 0.01 1.9999 1.99999])),
790%! [0.001 0.01 1.9999 1.99999], eps)
791%!assert (erfc (erfcinv (single ([0.001 0.01 1.9999 1.99999]))),
792%! single ([0.001 0.01 1.9999 1.99999]), eps ("single"))
793## backward - loss of accuracy
794%!assert (erfcinv (erfc ([-3 -1 -0.4 0.7 1.3 2.8])),
795%! [-3 -1 -0.4 0.7 1.3 2.8], -1e-12)
796%!testif ; ! ispc ()
797%! assert (erfcinv (erfc (single ([-3 -1 -0.4 0.7 1.3 2.8]))),
798%! single ([-3 -1 -0.4 0.7 1.3 2.8]), -1e-4)
799%!testif ; ispc () <65075>
800%! ## Same test code as above, but intended for test statistics with the UCRT.
801%! ## The deviations are twice as high with it.
802%! assert (erfcinv (erfc (single ([-3 -1 -0.4 0.7 1.3 2.8]))),
803%! single ([-3 -1 -0.4 0.7 1.3 2.8]), -1e-4)
804## exceptional
805%!assert (erfcinv ([2, 0, -0.1, 2.1]), [-Inf, Inf, NaN, NaN])
806%!error erfcinv (1+2i)
807
808%!error erfcinv ()
809%!error erfcinv (1, 2)
810*/
811
812DEFUN (erfc, args, ,
813 doc: /* -*- texinfo -*-
814@deftypefn {} {@var{v} =} erfc (@var{z})
815Compute the complementary error function.
816
817The complementary error function is defined as
818@tex
819$1 - {\rm erf} (z)$.
820@end tex
821@ifnottex
822@w{@code{1 - erf (@var{z})}}.
823@end ifnottex
824@seealso{erfcinv, erfcx, erfi, dawson, erf, erfinv}
825@end deftypefn */)
826{
827 if (args.length () != 1)
828 print_usage ();
829
830 return ovl (args(0).erfc ());
831}
832
833/*
834%!test
835%! a = -1i* sqrt (-1/(6.4187*6.4187));
836%! assert (erfc (a), erfc (real (a)));
837
838%!error erfc ()
839%!error erfc (1, 2)
840*/
841
842DEFUN (erfcx, args, ,
843 doc: /* -*- texinfo -*-
844@deftypefn {} {@var{v} =} erfcx (@var{z})
845Compute the scaled complementary error function.
846
847The scaled complementary error function is defined as
848@tex
849$$
850 e^{z^2} {\rm erfc} (z) \equiv e^{z^2} (1 - {\rm erf} (z))
851$$
852@end tex
853@ifnottex
854
855@example
856exp (z^2) * erfc (z)
857@end example
858
859@end ifnottex
860@seealso{erfc, erf, erfi, dawson, erfinv, erfcinv}
861@end deftypefn */)
862{
863 if (args.length () != 1)
864 print_usage ();
865
866 return ovl (args(0).erfcx ());
867}
868
869/*
870
871%!test
872%! x = [1+2i,-1+2i,1e-6+2e-6i,0+2i];
873%! assert (erfcx (x), exp (x.^2) .* erfc (x), -1.e-10);
874
875%!test
876%! x = [100, 100+20i];
877%! v = [0.0056416137829894329, 0.0054246791754558-0.00108483153786434i];
878%! assert (erfcx (x), v, -1.e-10);
879
880%!error erfcx ()
881%!error erfcx (1, 2)
882*/
883
884DEFUN (erfi, args, ,
885 doc: /* -*- texinfo -*-
886@deftypefn {} {@var{v} =} erfi (@var{z})
887Compute the imaginary error function.
888
889The imaginary error function is defined as
890@tex
891$$
892 -i {\rm erf} (iz)
893$$
894@end tex
895@ifnottex
896
897@example
898-i * erf (i*z)
899@end example
900
901@end ifnottex
902@seealso{erfc, erf, erfcx, dawson, erfinv, erfcinv}
903@end deftypefn */)
904{
905 if (args.length () != 1)
906 print_usage ();
907
908 return ovl (args(0).erfi ());
909}
910
911/*
912
913%!test
914%! x = [-0.1, 0.1, 1, 1+2i,-1+2i,1e-6+2e-6i,0+2i];
915%! assert (erfi (x), -i * erf (i*x), -1.e-10);
916
917%!error erfi ()
918%!error erfi (1, 2)
919*/
920
921DEFUN (dawson, args, ,
922 doc: /* -*- texinfo -*-
923@deftypefn {} {@var{v} =} dawson (@var{z})
924Compute the Dawson (scaled imaginary error) function.
925
926The Dawson function is defined as
927@tex
928$$
929 {\sqrt{\pi} \over 2} e^{-z^2} {\rm erfi} (z) \equiv -i {\sqrt{\pi} \over 2} e^{-z^2} {\rm erf} (iz)
930$$
931@end tex
932@ifnottex
933
934@example
935(sqrt (pi) / 2) * exp (-z^2) * erfi (z)
936@end example
937
938@end ifnottex
939@seealso{erfc, erf, erfcx, erfi, erfinv, erfcinv}
940@end deftypefn */)
941{
942 if (args.length () != 1)
943 print_usage ();
944
945 return ovl (args(0).dawson ());
946}
947
948/*
949
950%!test
951%! x = [0.1, 1, 1+2i,-1+2i,1e-4+2e-4i,0+2i];
952%! v = [0.099335992397852861, 0.53807950691, -13.38892731648-11.828715104i, 13.38892731648-11.828715104i, 0.0001000000073333+0.000200000001333i, 48.160012114291i];
953%! assert (dawson (x), v, -1.e-10);
954%! assert (dawson (-x), -v, -1.e-10);
955
956%!error dawson ()
957%!error dawson (1, 2)
958*/
959
960DEFUN (exp, args, ,
961 doc: /* -*- texinfo -*-
962@deftypefn {} {@var{y} =} exp (@var{x})
963Compute
964@tex
965$e^{x}$
966@end tex
967@ifnottex
968@code{e^x}
969@end ifnottex
970for each element of @var{x}.
971
972To compute the matrix exponential, @pxref{Linear Algebra}.
973@seealso{log}
974@end deftypefn */)
975{
976 if (args.length () != 1)
977 print_usage ();
978
979 return ovl (args(0).exp ());
980}
981
982/*
983%!assert (exp ([0, 1, -1, -1000]), [1, e, 1/e, 0], sqrt (eps))
984%!assert (exp (1+i), e * (cos (1) + sin (1) * i), sqrt (eps))
985%!assert (exp (single ([0, 1, -1, -1000])),
986%! single ([1, e, 1/e, 0]), sqrt (eps ("single")))
987%!assert (exp (single (1+i)),
988%! single (e * (cos (1) + sin (1) * i)), sqrt (eps ("single")))
989
990%!assert (exp ([Inf, -Inf, NaN]), [Inf 0 NaN])
991%!assert (exp (single ([Inf, -Inf, NaN])), single ([Inf 0 NaN]))
992
993%!error exp ()
994%!error exp (1, 2)
995*/
996
997DEFUN (expm1, args, ,
998 doc: /* -*- texinfo -*-
999@deftypefn {} {@var{y} =} expm1 (@var{x})
1000Compute
1001@tex
1002$ e^{x} - 1 $
1003@end tex
1004@ifnottex
1005@code{exp (@var{x}) - 1}
1006@end ifnottex
1007accurately in the neighborhood of zero.
1008@seealso{exp}
1009@end deftypefn */)
1010{
1011 if (args.length () != 1)
1012 print_usage ();
1013
1014 return ovl (args(0).expm1 ());
1015}
1016
1017/*
1018%!assert (expm1 (2*eps), 2*eps, 1e-29)
1019
1020%!assert (expm1 ([Inf, -Inf, NaN]), [Inf -1 NaN])
1021%!assert (expm1 (single ([Inf, -Inf, NaN])), single ([Inf -1 NaN]))
1022
1023%!error expm1 ()
1024%!error expm1 (1, 2)
1025*/
1026
1027DEFUN (isfinite, args, ,
1028 doc: /* -*- texinfo -*-
1029@deftypefn {} {@var{tf} =} isfinite (@var{x})
1030Return a logical array which is true where the elements of @var{x} are
1031finite values and false where they are not.
1032
1033For example:
1034
1035@example
1036@group
1037isfinite ([13, Inf, NA, NaN])
1038 @xresult{} [ 1, 0, 0, 0 ]
1039@end group
1040@end example
1041@seealso{isinf, isnan, isna}
1042@end deftypefn */)
1043{
1044 if (args.length () != 1)
1045 print_usage ();
1046
1047 return ovl (args(0).isfinite ());
1048}
1049
1050/*
1051%!assert (! isfinite (Inf))
1052%!assert (! isfinite (NaN))
1053%!assert (isfinite (rand (1,10)))
1054
1055%!assert (! isfinite (single (Inf)))
1056%!assert (! isfinite (single (NaN)))
1057%!assert (isfinite (single (rand (1,10))))
1058%!assert (isfinite ('a'))
1059
1060%!error isfinite ()
1061%!error isfinite (1, 2)
1062*/
1063
1064DEFUN (fix, args, ,
1065 doc: /* -*- texinfo -*-
1066@deftypefn {} {@var{y} =} fix (@var{x})
1067Truncate fractional portion of @var{x} and return the integer portion.
1068
1069This is equivalent to rounding towards zero. If @var{x} is complex, return
1070@code{fix (real (@var{x})) + fix (imag (@var{x})) * I}.
1071
1072@example
1073@group
1074fix ([-2.7, 2.7])
1075 @xresult{} -2 2
1076@end group
1077@end example
1078@seealso{ceil, floor, round}
1079@end deftypefn */)
1080{
1081 if (args.length () != 1)
1082 print_usage ();
1083
1084 return ovl (args(0).fix ());
1085}
1086
1087/*
1088%!assert (fix ([1.1, 1, -1.1, -1]), [1, 1, -1, -1])
1089%!assert (fix ([1.1+1.1i, 1+i, -1.1-1.1i, -1-i]), [1+i, 1+i, -1-i, -1-i])
1090%!assert (fix (single ([1.1, 1, -1.1, -1])), single ([1, 1, -1, -1]))
1091%!assert (fix (single ([1.1+1.1i, 1+i, -1.1-1.1i, -1-i])),
1092%! single ([1+i, 1+i, -1-i, -1-i]))
1093
1094%!error fix ()
1095%!error fix (1, 2)
1096*/
1097
1098DEFUN (floor, args, ,
1099 doc: /* -*- texinfo -*-
1100@deftypefn {} {@var{y} =} floor (@var{x})
1101Return the largest integer not greater than @var{x}.
1102
1103This is equivalent to rounding towards negative infinity. If @var{x} is
1104complex, return @code{floor (real (@var{x})) + floor (imag (@var{x})) * I}.
1105
1106@example
1107@group
1108floor ([-2.7, 2.7])
1109 @xresult{} -3 2
1110@end group
1111@end example
1112@seealso{ceil, round, fix}
1113@end deftypefn */)
1114{
1115 if (args.length () != 1)
1116 print_usage ();
1117
1118 return ovl (args(0).floor ());
1119}
1120
1121/*
1122%!assert (floor ([2, 1.1, -1.1, -1]), [2, 1, -2, -1])
1123%!assert (floor ([2+2i, 1.1+1.1i, -1.1-1.1i, -1-i]), [2+2i, 1+i, -2-2i, -1-i])
1124%!assert (floor (single ([2, 1.1, -1.1, -1])), single ([2, 1, -2, -1]))
1125%!assert (floor (single ([2+2i, 1.1+1.1i, -1.1-1.1i, -1-i])),
1126%! single ([2+2i, 1+i, -2-2i, -1-i]))
1127
1128%!error floor ()
1129%!error floor (1, 2)
1130*/
1131
1132DEFUN (gamma, args, ,
1133 doc: /* -*- texinfo -*-
1134@deftypefn {} {@var{v} =} gamma (@var{z})
1135Compute the Gamma function.
1136
1137The Gamma function is defined as
1138@tex
1139$$
1140 \Gamma (z) = \int_0^\infty t^{z-1} e^{-t} dt.
1141$$
1142@end tex
1143@ifnottex
1144
1145@example
1146@group
1147 infinity
1148 /
1149gamma (z) = | t^(z-1) exp (-t) dt.
1150 /
1151 t=0
1152@end group
1153@end example
1154
1155@end ifnottex
1156
1157Programming Note: The gamma function can grow quite large even for small
1158input values. In many cases it may be preferable to use the natural
1159logarithm of the gamma function (@code{gammaln}) in calculations to minimize
1160loss of precision. The final result is then
1161@code{exp (@var{result_using_gammaln}).}
1162@seealso{gammainc, gammaln, factorial}
1163@end deftypefn */)
1164{
1165 if (args.length () != 1)
1166 print_usage ();
1167
1168 return ovl (args(0).gamma ());
1169}
1170
1171/*
1172%!test
1173%! a = -1i* sqrt (-1/(6.4187*6.4187));
1174%! assert (gamma (a), gamma (real (a)));
1175
1176%!test
1177%! x = [.5, 1, 1.5, 2, 3, 4, 5];
1178%! v = [sqrt(pi), 1, .5*sqrt(pi), 1, 2, 6, 24];
1179%! assert (gamma (x), v, sqrt (eps));
1180
1181%!test
1182%! a = single (-1i* sqrt (-1/(6.4187*6.4187)));
1183%! assert (gamma (a), gamma (real (a)));
1184
1185%!test
1186%! x = single ([.5, 1, 1.5, 2, 3, 4, 5]);
1187%! v = single ([sqrt(pi), 1, .5*sqrt(pi), 1, 2, 6, 24]);
1188%! assert (gamma (x), v, sqrt (eps ("single")));
1189
1190%!test
1191%! ## Test exceptional values
1192%! x = [-Inf, -1, -0, 0, 1, Inf, NaN];
1193%! v = [Inf, Inf, -Inf, Inf, 1, Inf, NaN];
1194%! assert (gamma (x), v);
1195%! assert (gamma (single (x)), single (v));
1196
1197%!error gamma ()
1198%!error gamma (1, 2)
1199*/
1200
1201DEFUN (imag, args, ,
1202 doc: /* -*- texinfo -*-
1203@deftypefn {} {@var{y} =} imag (@var{z})
1204Return the imaginary part of @var{z} as a real number.
1205@seealso{real, conj}
1206@end deftypefn */)
1207{
1208 if (args.length () != 1)
1209 print_usage ();
1210
1211 return ovl (args(0).imag ());
1212}
1213
1214/*
1215%!assert (imag (1), 0)
1216%!assert (imag (i), 1)
1217%!assert (imag (1+i), 1)
1218%!assert (imag ([i, 1; 1, i]), full (eye (2)))
1219
1220%!assert (imag (single (1)), single (0))
1221%!assert (imag (single (i)), single (1))
1222%!assert (imag (single (1+i)), single (1))
1223%!assert (imag (single ([i, 1; 1, i])), full (eye (2,"single")))
1224
1225%!error imag ()
1226%!error imag (1, 2)
1227*/
1228
1229DEFUNX ("isalnum", Fisalnum, args, ,
1230 doc: /* -*- texinfo -*-
1231@deftypefn {} {@var{tf} =} isalnum (@var{s})
1232Return a logical array which is true where the elements of @var{s} are
1233letters or digits and false where they are not.
1234
1235This is equivalent to (@code{isalpha (@var{s}) | isdigit (@var{s})}).
1236@seealso{isalpha, isdigit, ispunct, isspace, iscntrl}
1237@end deftypefn */)
1238{
1239 if (args.length () != 1)
1240 print_usage ();
1241
1242 return ovl (args(0).xisalnum ());
1243}
1244
1245/*
1246%!test
1247%! charset = char (0:127);
1248%! result = false (1, 128);
1249%! result(double ("A":"Z") + 1) = true;
1250%! result(double ("0":"9") + 1) = true;
1251%! result(double ("a":"z") + 1) = true;
1252%! assert (isalnum (charset), result);
1253%!assert (isalnum(["Ä8Aa?"; "(Uß ;"]), logical ([1 1 1 1 1 0; 0 1 1 1 0 0]))
1254
1255%!error isalnum ()
1256%!error isalnum (1, 2)
1257*/
1258
1259DEFUNX ("isalpha", Fisalpha, args, ,
1260 doc: /* -*- texinfo -*-
1261@deftypefn {} {@var{tf} =} isalpha (@var{s})
1262Return a logical array which is true where the elements of @var{s} are
1263letters and false where they are not.
1264
1265This is equivalent to (@code{islower (@var{s}) | isupper (@var{s})}).
1266@seealso{isdigit, ispunct, isspace, iscntrl, isalnum, islower, isupper}
1267@end deftypefn */)
1268{
1269 if (args.length () != 1)
1270 print_usage ();
1271
1272 return ovl (args(0).xisalpha ());
1273}
1274
1275/*
1276%!test
1277%! charset = char (0:127);
1278%! result = false (1, 128);
1279%! result(double ("A":"Z") + 1) = true;
1280%! result(double ("a":"z") + 1) = true;
1281%! assert (isalpha (charset), result);
1282%!assert (isalpha("Ä8Aa(Uß ;"), logical ([1 1 0 1 1 0 1 1 1 0 0]))
1283
1284%!error isalpha ()
1285%!error isalpha (1, 2)
1286*/
1287
1288DEFUNX ("isascii", Fisascii, args, ,
1289 doc: /* -*- texinfo -*-
1290@deftypefn {} {@var{tf} =} isascii (@var{s})
1291Return a logical array which is true where the elements of @var{s} are
1292ASCII characters (in the range 0 to 127 decimal) and false where they are
1293not.
1294@end deftypefn */)
1295{
1296 if (args.length () != 1)
1297 print_usage ();
1298
1299 return ovl (args(0).xisascii ());
1300}
1301
1302/*
1303%!test
1304%! charset = char (0:127);
1305%! result = true (1, 128);
1306%! assert (isascii (charset), result);
1307
1308%!error isascii ()
1309%!error isascii (1, 2)
1310*/
1311
1312DEFUNX ("iscntrl", Fiscntrl, args, ,
1313 doc: /* -*- texinfo -*-
1314@deftypefn {} {@var{tf} =} iscntrl (@var{s})
1315Return a logical array which is true where the elements of @var{s} are
1316control characters and false where they are not.
1317@seealso{ispunct, isspace, isalpha, isdigit}
1318@end deftypefn */)
1319{
1320 if (args.length () != 1)
1321 print_usage ();
1322
1323 return ovl (args(0).xiscntrl ());
1324}
1325
1326/*
1327%!test
1328%! charset = char (0:127);
1329%! result = false (1, 128);
1330%! result(1:32) = true;
1331%! result(128) = true;
1332%! assert (iscntrl (charset), result);
1333
1334%!error iscntrl ()
1335%!error iscntrl (1, 2)
1336*/
1337
1338DEFUNX ("isdigit", Fisdigit, args, ,
1339 doc: /* -*- texinfo -*-
1340@deftypefn {} {@var{tf} =} isdigit (@var{s})
1341Return a logical array which is true where the elements of @var{s} are
1342decimal digits (0-9) and false where they are not.
1343@seealso{isxdigit, isalpha, isletter, ispunct, isspace, iscntrl}
1344@end deftypefn */)
1345{
1346 if (args.length () != 1)
1347 print_usage ();
1348
1349 return ovl (args(0).xisdigit ());
1350}
1351
1352/*
1353%!test
1354%! charset = char (0:127);
1355%! result = false (1, 128);
1356%! result(double ("0":"9") + 1) = true;
1357%! assert (isdigit (charset), result);
1358%!assert (isdigit("Ä8Aa(Uß ;"), logical ([0 0 1 0 0 0 0 0 0 0 0]))
1359
1360%!error isdigit ()
1361%!error isdigit (1, 2)
1362*/
1363
1364DEFUN (isinf, args, ,
1365 doc: /* -*- texinfo -*-
1366@deftypefn {} {@var{tf} =} isinf (@var{x})
1367Return a logical array which is true where the elements of @var{x} are
1368infinite and false where they are not.
1369
1370For example:
1371
1372@example
1373@group
1374isinf ([13, Inf, NA, NaN])
1375 @xresult{} [ 0, 1, 0, 0 ]
1376@end group
1377@end example
1378@seealso{isfinite, isnan, isna}
1379@end deftypefn */)
1380{
1381 if (args.length () != 1)
1382 print_usage ();
1383
1384 return ovl (args(0).isinf ());
1385}
1386
1387/*
1388%!assert (isinf (Inf))
1389%!assert (! isinf (NaN))
1390%!assert (! isinf (NA))
1391%!assert (isinf (rand (1,10)), false (1,10))
1392%!assert (isinf ([NaN -Inf -1 0 1 Inf NA]),
1393%! [false, true, false, false, false, true, false])
1394
1395%!assert (isinf (single (Inf)))
1396%!assert (! isinf (single (NaN)))
1397%!assert (! isinf (single (NA)))
1398%!assert (isinf (single (rand (1,10))), false (1,10))
1399%!assert (isinf (single ([NaN -Inf -1 0 1 Inf NA])),
1400%! [false, true, false, false, false, true, false])
1401%!assert (! isinf ('a'))
1402
1403%!error isinf ()
1404%!error isinf (1, 2)
1405*/
1406
1407DEFUNX ("isgraph", Fisgraph, args, ,
1408 doc: /* -*- texinfo -*-
1409@deftypefn {} {@var{tf} =} isgraph (@var{s})
1410Return a logical array which is true where the elements of @var{s} are
1411printable characters (but not the space character) and false where they are
1412not.
1413@seealso{isprint}
1414@end deftypefn */)
1415{
1416 if (args.length () != 1)
1417 print_usage ();
1418
1419 return ovl (args(0).xisgraph ());
1420}
1421
1422/*
1423%!test
1424%! charset = char (0:127);
1425%! result = false (1, 128);
1426%! result(34:127) = true;
1427%! assert (isgraph (charset), result);
1428%!assert (isgraph("Ä8Aa(Uß ;"), logical ([1 1 1 1 1 1 1 1 1 0 1]))
1429
1430%!error isgraph ()
1431%!error isgraph (1, 2)
1432*/
1433
1434DEFUNX ("islower", Fislower, args, ,
1435 doc: /* -*- texinfo -*-
1436@deftypefn {} {@var{tf} =} islower (@var{s})
1437Return a logical array which is true where the elements of @var{s} are
1438lowercase letters and false where they are not.
1439@seealso{isupper, isalpha, isletter, isalnum}
1440@end deftypefn */)
1441{
1442 if (args.length () != 1)
1443 print_usage ();
1444
1445 return ovl (args(0).xislower ());
1446}
1447
1448/*
1449%!test
1450%! charset = char (0:127);
1451%! result = false (1, 128);
1452%! result(double ("a":"z") + 1) = true;
1453%! assert (islower (charset), result);
1454%!assert (islower("Ä8Aa(Uß ;"), logical ([0 0 0 0 1 0 0 1 1 0 0]))
1455
1456%!error islower ()
1457%!error islower (1, 2)
1458*/
1459
1460DEFUN (isna, args, ,
1461 doc: /* -*- texinfo -*-
1462@deftypefn {} {@var{tf} =} isna (@var{x})
1463Return a logical array which is true where the elements of @var{x} are
1464NA (missing) values and false where they are not.
1465
1466For example:
1467
1468@example
1469@group
1470isna ([13, Inf, NA, NaN])
1471 @xresult{} [ 0, 0, 1, 0 ]
1472@end group
1473@end example
1474@seealso{isnan, isinf, isfinite}
1475@end deftypefn */)
1476{
1477 if (args.length () != 1)
1478 print_usage ();
1479
1480 return ovl (args(0).isna ());
1481}
1482
1483/*
1484%!assert (! isna (Inf))
1485%!assert (! isna (NaN))
1486%!assert (isna (NA))
1487%!assert (isna (rand (1,10)), false (1,10))
1488%!assert (isna ([NaN -Inf -1 0 1 Inf NA]),
1489%! [false, false, false, false, false, false, true])
1490
1491%!assert (! isna (single (Inf)))
1492%!assert (! isna (single (NaN)))
1493%!testif HAVE_QNAN_WITH_PAYLOAD
1494%! assert (isna (single (NA)));
1495%!testif HAVE_QNAN_WITH_PAYLOAD
1496%! assert (isna (single (rand (1,10))), false (1,10));
1497%!testif HAVE_QNAN_WITH_PAYLOAD
1498%! assert (isna (single ([NaN -Inf -1 0 1 Inf NA])),
1499%! [false, false, false, false, false, false, true])
1500// Duplicate from above. Only for test statistics
1501%!testif ; ! __have_feature__ ("QNAN_WITH_PAYLOAD") <59830>
1502%! assert (isna (single (NA)));
1503%!testif ; ! __have_feature__ ("QNAN_WITH_PAYLOAD") <59830>
1504%! assert (isna (single (rand (1,10))), false (1,10));
1505%!testif ; ! __have_feature__ ("QNAN_WITH_PAYLOAD") <59830>
1506%! assert (isna (single ([NaN -Inf -1 0 1 Inf NA])),
1507%! [false, false, false, false, false, false, true])
1508
1509%!error isna ()
1510%!error isna (1, 2)
1511*/
1512
1513DEFUN (isnan, args, ,
1514 doc: /* -*- texinfo -*-
1515@deftypefn {} {@var{tf} =} isnan (@var{x})
1516Return a logical array which is true where the elements of @var{x} are
1517NaN values and false where they are not.
1518
1519NA values are also considered NaN values. For example:
1520
1521@example
1522@group
1523isnan ([13, Inf, NA, NaN])
1524 @xresult{} [ 0, 0, 1, 1 ]
1525@end group
1526@end example
1527@seealso{isna, isinf, isfinite}
1528@end deftypefn */)
1529{
1530 if (args.length () != 1)
1531 print_usage ();
1532
1533 return ovl (args(0).isnan ());
1534}
1535
1536/*
1537%!assert (! isnan (Inf))
1538%!assert (isnan (NaN))
1539%!assert (isnan (NA))
1540%!assert (isnan (rand (1,10)), false (1,10))
1541%!assert (isnan ([NaN -Inf -1 0 1 Inf NA]),
1542%! [true, false, false, false, false, false, true])
1543
1544%!assert (! isnan (single (Inf)))
1545%!assert (isnan (single (NaN)))
1546%!assert (isnan (single (NA)))
1547%!assert (isnan (single (rand (1,10))), false (1,10))
1548%!assert (isnan (single ([NaN -Inf -1 0 1 Inf NA])),
1549%! [true, false, false, false, false, false, true])
1550%!assert (! isnan ('a'))
1551
1552%!error isnan ()
1553%!error isnan (1, 2)
1554*/
1555
1556DEFUNX ("isprint", Fisprint, args, ,
1557 doc: /* -*- texinfo -*-
1558@deftypefn {} {@var{tf} =} isprint (@var{s})
1559Return a logical array which is true where the elements of @var{s} are
1560printable characters (including the space character) and false where they
1561are not.
1562@seealso{isgraph}
1563@end deftypefn */)
1564{
1565 if (args.length () != 1)
1566 print_usage ();
1567
1568 return ovl (args(0).xisprint ());
1569}
1570
1571/*
1572%!test
1573%! charset = char (0:127);
1574%! result = false (1, 128);
1575%! result(33:127) = true;
1576%! assert (isprint (charset), result);
1577%!assert (isprint("Ä8Aa(Uß ;"), logical ([1 1 1 1 1 1 1 1 1 1 1]))
1578
1579%!error isprint ()
1580%!error isprint (1, 2)
1581*/
1582
1583DEFUNX ("ispunct", Fispunct, args, ,
1584 doc: /* -*- texinfo -*-
1585@deftypefn {} {@var{tf} =} ispunct (@var{s})
1586Return a logical array which is true where the elements of @var{s} are
1587punctuation characters and false where they are not.
1588@seealso{isalpha, isdigit, isspace, iscntrl}
1589@end deftypefn */)
1590{
1591 if (args.length () != 1)
1592 print_usage ();
1593
1594 return ovl (args(0).xispunct ());
1595}
1596
1597/*
1598%!test
1599%! charset = char (0:127);
1600%! result = false (1, 128);
1601%! result(34:48) = true;
1602%! result(59:65) = true;
1603%! result(92:97) = true;
1604%! result(124:127) = true;
1605%! assert (ispunct (charset), result);
1606%!assert (ispunct("Ä8Aa(Uß ;"), logical ([0 0 0 0 0 1 0 0 0 0 1]))
1607
1608%!error ispunct ()
1609%!error ispunct (1, 2)
1610*/
1611
1612DEFUNX ("isspace", Fisspace, args, ,
1613 doc: /* -*- texinfo -*-
1614@deftypefn {} {@var{tf} =} isspace (@var{s})
1615Return a logical array which is true where the elements of @var{s} are
1616whitespace characters (space, formfeed, newline, carriage return, tab, and
1617vertical tab) and false where they are not.
1618@seealso{iscntrl, ispunct, isalpha, isdigit}
1619@end deftypefn */)
1620{
1621 if (args.length () != 1)
1622 print_usage ();
1623
1624 return ovl (args(0).xisspace ());
1625}
1626
1627/*
1628%!test
1629%! charset = char (0:127);
1630%! result = false (1, 128);
1631%! result(double (" \f\n\r\t\v") + 1) = true;
1632%! assert (isspace (charset), result);
1633%!assert (isspace("Ä8Aa(Uß ;"), logical ([0 0 0 0 0 0 0 0 0 1 0]))
1634
1635%!error isspace ()
1636%!error isspace (1, 2)
1637*/
1638
1639DEFUNX ("isupper", Fisupper, args, ,
1640 doc: /* -*- texinfo -*-
1641@deftypefn {} {@var{tf} =} isupper (@var{s})
1642Return a logical array which is true where the elements of @var{s} are
1643uppercase letters and false where they are not.
1644@seealso{islower, isalpha, isletter, isalnum}
1645@end deftypefn */)
1646{
1647 if (args.length () != 1)
1648 print_usage ();
1649
1650 return ovl (args(0).xisupper ());
1651}
1652
1653/*
1654%!test
1655%! charset = char (0:127);
1656%! result = false (1, 128);
1657%! result(double ("A":"Z") + 1) = true;
1658%! assert (isupper (charset), result);
1659%!assert (isupper("Ä8Aa(Uß ;"), logical ([1 1 0 1 0 0 1 0 0 0 0]))
1660
1661%!error isupper ()
1662%!error isupper (1, 2)
1663*/
1664
1665DEFUNX ("isxdigit", Fisxdigit, args, ,
1666 doc: /* -*- texinfo -*-
1667@deftypefn {} {@var{tf} =} isxdigit (@var{s})
1668Return a logical array which is true where the elements of @var{s} are
1669hexadecimal digits (0-9 and @nospell{a-fA-F}).
1670@seealso{isdigit}
1671@end deftypefn */)
1672{
1673 if (args.length () != 1)
1674 print_usage ();
1675
1676 return ovl (args(0).xisxdigit ());
1677}
1678
1679/*
1680%!test
1681%! charset = char (0:127);
1682%! result = false (1, 128);
1683%! result(double ("A":"F") + 1) = true;
1684%! result(double ("0":"9") + 1) = true;
1685%! result(double ("a":"f") + 1) = true;
1686%! assert (isxdigit (charset), result);
1687%!assert (isxdigit("Ä8Aa(Uß ;"), logical ([0 0 1 1 1 0 0 0 0 0 0]))
1688
1689%!error isxdigit ()
1690%!error isxdigit (1, 2)
1691*/
1692
1693DEFUN (lgamma, args, ,
1694 doc: /* -*- texinfo -*-
1695@deftypefn {} {@var{y} =} gammaln (@var{x})
1696@deftypefnx {} {@var{y} =} lgamma (@var{x})
1697Return the natural logarithm of the gamma function of @var{x}.
1698
1699Programming Note: @code{lgamma} is an alias for @code{gammaln} and either name
1700can be used in Octave.
1701@seealso{gamma, gammainc}
1702@end deftypefn */)
1703{
1704 if (args.length () != 1)
1705 print_usage ();
1706
1707 return ovl (args(0).lgamma ());
1708}
1709
1710/*
1711%!test
1712%! a = -1i* sqrt (-1/(6.4187*6.4187));
1713%! assert (gammaln (a), gammaln (real (a)));
1714
1715%!test
1716%! x = [.5, 1, 1.5, 2, 3, 4, 5];
1717%! v = [sqrt(pi), 1, .5*sqrt(pi), 1, 2, 6, 24];
1718%! assert (gammaln (x), log (v), sqrt (eps));
1719
1720%!test
1721%! a = single (-1i* sqrt (-1/(6.4187*6.4187)));
1722%! assert (gammaln (a), gammaln (real (a)));
1723
1724%!test
1725%! x = single ([.5, 1, 1.5, 2, 3, 4, 5]);
1726%! v = single ([sqrt(pi), 1, .5*sqrt(pi), 1, 2, 6, 24]);
1727%! assert (gammaln (x), log (v), sqrt (eps ("single")));
1728
1729%!test
1730%! x = [-1, 0, 1, Inf];
1731%! v = [Inf, Inf, 0, Inf];
1732%! assert (gammaln (x), v);
1733%! assert (gammaln (single (x)), single (v));
1734
1735%!error gammaln ()
1736%!error gammaln (1,2)
1737*/
1738
1739DEFUN (log, args, ,
1740 doc: /* -*- texinfo -*-
1741@deftypefn {} {@var{y} =} log (@var{x})
1742Compute the natural logarithm,
1743@tex
1744$\ln{(x)},$
1745@end tex
1746@ifnottex
1747@code{ln (@var{x})},
1748@end ifnottex
1749for each element of @var{x}.
1750
1751To compute the matrix logarithm, @pxref{Linear Algebra}.
1752@seealso{exp, log1p, log2, log10, logspace}
1753@end deftypefn */)
1754{
1755 if (args.length () != 1)
1756 print_usage ();
1757
1758 return ovl (args(0).log ());
1759}
1760
1761/*
1762%!assert (log ([1, e, e^2]), [0, 1, 2], sqrt (eps))
1763%!assert (log ([-0.5, -1.5, -2.5]), log ([0.5, 1.5, 2.5]) + pi*1i, sqrt (eps))
1764
1765%!assert (log (single ([1, e, e^2])), single ([0, 1, 2]), sqrt (eps ("single")))
1766%!assert (log (single ([-0.5, -1.5, -2.5])),
1767%! single (log ([0.5, 1.5, 2.5]) + pi*1i), 4* eps ("single"))
1768
1769%!error log ()
1770%!error log (1, 2)
1771*/
1772
1773DEFUN (log10, args, ,
1774 doc: /* -*- texinfo -*-
1775@deftypefn {} {@var{y} =} log10 (@var{x})
1776Compute the base-10 logarithm of each element of @var{x}.
1777@seealso{log, log2, logspace, exp}
1778@end deftypefn */)
1779{
1780 if (args.length () != 1)
1781 print_usage ();
1782
1783 return ovl (args(0).log10 ());
1784}
1785
1786/*
1787%!assert (log10 ([0.01, 0.1, 1, 10, 100]), [-2, -1, 0, 1, 2], sqrt (eps))
1788%!assert (log10 (single ([0.01, 0.1, 1, 10, 100])),
1789%! single ([-2, -1, 0, 1, 2]), sqrt (eps ("single")))
1790
1791%!error log10 ()
1792%!error log10 (1, 2)
1793*/
1794
1795DEFUN (log1p, args, ,
1796 doc: /* -*- texinfo -*-
1797@deftypefn {} {@var{y} =} log1p (@var{x})
1798Compute
1799@tex
1800$\ln{(1 + x)}$
1801@end tex
1802@ifnottex
1803@code{log (1 + @var{x})}
1804@end ifnottex
1805accurately in the neighborhood of zero.
1806@seealso{log, exp, expm1}
1807@end deftypefn */)
1808{
1809 if (args.length () != 1)
1810 print_usage ();
1811
1812 return ovl (args(0).log1p ());
1813}
1814
1815/*
1816%!assert (log1p ([0, 2*eps, -2*eps]), [0, 2*eps, -2*eps], 1e-29)
1817%!assert (log1p (single ([0, 2*eps, -2*eps])), ...
1818%! single ([0, 2*eps, -2*eps]), 1e-29)
1819## Compare to result from Wolfram Alpha rounded to 16 significant digits
1820%!assert <*62094> (log1p (0.1i), ...
1821%! 0.004975165426584041 + 0.09966865249116203i, eps (0.2))
1822%!assert <*62094> (log1p (single (0.1i)), ...
1823%! single (0.004975165426584041 + 0.09966865249116203i), ...
1824%! eps (single (0.2)))
1825
1826%!error log1p ()
1827%!error log1p (1, 2)
1828*/
1829
1830DEFUN (real, args, ,
1831 doc: /* -*- texinfo -*-
1832@deftypefn {} {@var{x} =} real (@var{z})
1833Return the real part of @var{z}.
1834@seealso{imag, conj}
1835@end deftypefn */)
1836{
1837 if (args.length () != 1)
1838 print_usage ();
1839
1840 return ovl (args(0).real ());
1841}
1842
1843/*
1844%!assert (real (1), 1)
1845%!assert (real (i), 0)
1846%!assert (real (1+i), 1)
1847%!assert (real ([1, i; i, 1]), full (eye (2)))
1848
1849%!assert (real (single (1)), single (1))
1850%!assert (real (single (i)), single (0))
1851%!assert (real (single (1+i)), single (1))
1852%!assert (real (single ([1, i; i, 1])), full (eye (2, "single")))
1853
1854%!error real ()
1855%!error real (1, 2)
1856*/
1857
1858DEFUN (round, args, ,
1859 doc: /* -*- texinfo -*-
1860@deftypefn {} {@var{y} =} round (@var{x})
1861Return the integer nearest to @var{x}.
1862
1863If @var{x} is complex, return
1864@code{round (real (@var{x})) + round (imag (@var{x})) * I}. If there
1865are two nearest integers, return the one further away from zero.
1866
1867@example
1868@group
1869round ([-2.7, 2.7])
1870 @xresult{} -3 3
1871@end group
1872@end example
1873@seealso{ceil, floor, fix, roundb}
1874@end deftypefn */)
1875{
1876 if (args.length () != 1)
1877 print_usage ();
1878
1879 return ovl (args(0).round ());
1880}
1881
1882/*
1883%!assert (round (1), 1)
1884%!assert (round (1.1), 1)
1885%!assert (round (5.5), 6)
1886%!assert (round (i), i)
1887%!assert (round (2.5+3.5i), 3+4i)
1888%!assert (round (-2.6), -3)
1889%!assert (round ([1.1, -2.4; -3.7, 7.1]), [1, -2; -4, 7])
1890
1891%!assert (round (single (1)), single (1))
1892%!assert (round (single (1.1)), single (1))
1893%!assert (round (single (5.5)), single (6))
1894%!assert (round (single (i)), single (i))
1895%!assert (round (single (2.5+3.5i)), single (3+4i))
1896%!assert (round (single (-2.6)), single (-3))
1897%!assert (round (single ([1.1, -2.4; -3.7, 7.1])), single ([1, -2; -4, 7]))
1898
1899%!error round ()
1900%!error round (1, 2)
1901*/
1902
1903DEFUN (roundb, args, ,
1904 doc: /* -*- texinfo -*-
1905@deftypefn {} {@var{y} =} roundb (@var{x})
1906Return the integer nearest to @var{x}. If there are two nearest
1907integers, return the even one (banker's rounding).
1908
1909If @var{x} is complex,
1910return @code{roundb (real (@var{x})) + roundb (imag (@var{x})) * I}.
1911@seealso{round}
1912@end deftypefn */)
1913{
1914 if (args.length () != 1)
1915 print_usage ();
1916
1917 return ovl (args(0).roundb ());
1918}
1919
1920/*
1921%!assert (roundb (1), 1)
1922%!assert (roundb (1.1), 1)
1923%!assert (roundb (1.5), 2)
1924%!assert (roundb (4.5), 4)
1925%!assert (roundb (i), i)
1926%!assert (roundb (2.5+3.5i), 2+4i)
1927%!assert (roundb (-2.6), -3)
1928%!assert (roundb ([1.1, -2.4; -3.7, 7.1]), [1, -2; -4, 7])
1929
1930%!assert (roundb (single (1)), single (1))
1931%!assert (roundb (single (1.1)), single (1))
1932%!assert (roundb (single (1.5)), single (2))
1933%!assert (roundb (single (4.5)), single (4))
1934%!assert (roundb (single (i)), single (i))
1935%!assert (roundb (single (2.5+3.5i)), single (2+4i))
1936%!assert (roundb (single (-2.6)), single (-3))
1937%!assert (roundb (single ([1.1, -2.4; -3.7, 7.1])), single ([1, -2; -4, 7]))
1938
1939%!error roundb ()
1940%!error roundb (1, 2)
1941*/
1942
1943DEFUN (sign, args, ,
1944 doc: /* -*- texinfo -*-
1945@deftypefn {} {@var{y} =} sign (@var{x})
1946Compute the @dfn{signum} function.
1947
1948This is defined as
1949@tex
1950$$
1951{\rm sign} (@var{x}) = \cases{1,&$x>0$;\cr 0,&$x=0$;\cr -1,&$x<0$.\cr}
1952$$
1953@end tex
1954@ifnottex
1955
1956@example
1957@group
1958 -1, x < 0;
1959sign (x) = 0, x = 0;
1960 1, x > 0.
1961@end group
1962@end example
1963
1964@end ifnottex
1965
1966For complex arguments, @code{sign} returns @code{x ./ abs (@var{x})}.
1967
1968Note that @code{sign (-0.0)} is 0. Although IEEE@tie{}754 floating point
1969allows zero to be signed, 0.0 and -0.0 compare equal. If you must test
1970whether zero is signed, use the @code{signbit} function.
1971@seealso{signbit}
1972@end deftypefn */)
1973{
1974 if (args.length () != 1)
1975 print_usage ();
1976
1977 return ovl (args(0).signum ());
1978}
1979
1980/*
1981%!assert (sign (-2) , -1)
1982%!assert (sign (0), 0)
1983%!assert (sign (3), 1)
1984%!assert (sign ([1, -pi; e, 0]), [1, -1; 1, 0])
1985
1986%!assert (sign (single (-2)) , single (-1))
1987%!assert (sign (single (0)), single (0))
1988%!assert (sign (single (3)), single (1))
1989%!assert (sign (single ([1, -pi; e, 0])), single ([1, -1; 1, 0]))
1990
1991%!error sign ()
1992%!error sign (1, 2)
1993*/
1994
1995DEFUNX ("signbit", Fsignbit, args, ,
1996 doc: /* -*- texinfo -*-
1997@deftypefn {} {@var{y} =} signbit (@var{x})
1998Return logical true if the value of @var{x} has its sign bit set and false
1999otherwise.
2000
2001This behavior is consistent with the other logical functions.
2002@xref{Logical Values}. The behavior differs from the C language function
2003which returns nonzero if the sign bit is set.
2004
2005This is not the same as @code{x < 0.0}, because IEEE@tie{}754 floating point
2006allows zero to be signed. The comparison @code{-0.0 < 0.0} is false,
2007but @code{signbit (-0.0)} will return a nonzero value.
2008@seealso{sign}
2009@end deftypefn */)
2010{
2011 if (args.length () != 1)
2012 print_usage ();
2013
2014 octave_value tmp = args(0).xsignbit ();
2015
2016 return ovl (tmp != 0);
2017}
2018
2019/*
2020%!assert (signbit (1) == 0)
2021%!assert (signbit (-2) != 0)
2022%!assert (signbit (0) == 0)
2023%!assert (signbit (-0) != 0)
2024
2025%!assert (signbit (single (1)) == 0)
2026%!assert (signbit (single (-2)) != 0)
2027%!assert (signbit (single (0)) == 0)
2028%!assert (signbit (single (-0)) != 0)
2029
2030%!error sign ()
2031%!error sign (1, 2)
2032*/
2033
2034DEFUN (sin, args, ,
2035 doc: /* -*- texinfo -*-
2036@deftypefn {} {@var{y} =} sin (@var{x})
2037Compute the sine for each element of @var{x} in radians.
2038@seealso{asin, sind, sinh}
2039@end deftypefn */)
2040{
2041 if (args.length () != 1)
2042 print_usage ();
2043
2044 return ovl (args(0).sin ());
2045}
2046
2047/*
2048%!shared rt2, rt3
2049%! rt2 = sqrt (2);
2050%! rt3 = sqrt (3);
2051
2052%!test
2053%! x = [0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi];
2054%! v = [0, 1/2, rt2/2, rt3/2, 1, rt3/2, rt2/2, 1/2, 0];
2055%! assert (sin (x), v, sqrt (eps));
2056
2057%!test
2058%! x = single ([0, pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi]);
2059%! v = single ([0, 1/2, rt2/2, rt3/2, 1, rt3/2, rt2/2, 1/2, 0]);
2060%! assert (sin (x), v, sqrt (eps ("single")));
2061
2062%!error sin ()
2063%!error sin (1, 2)
2064*/
2065
2066DEFUN (sinh, args, ,
2067 doc: /* -*- texinfo -*-
2068@deftypefn {} {@var{y} =} sinh (@var{x})
2069Compute the hyperbolic sine for each element of @var{x}.
2070@seealso{asinh, cosh, tanh}
2071@end deftypefn */)
2072{
2073 if (args.length () != 1)
2074 print_usage ();
2075
2076 return ovl (args(0).sinh ());
2077}
2078
2079/*
2080%!test
2081%! x = [0, pi/2*i, pi*i, 3*pi/2*i];
2082%! v = [0, i, 0, -i];
2083%! assert (sinh (x), v, sqrt (eps));
2084
2085%!test
2086%! x = single ([0, pi/2*i, pi*i, 3*pi/2*i]);
2087%! v = single ([0, i, 0, -i]);
2088%! assert (sinh (x), v, sqrt (eps ("single")));
2089
2090%!error sinh ()
2091%!error sinh (1, 2)
2092*/
2093
2094DEFUN (sqrt, args, ,
2095 doc: /* -*- texinfo -*-
2096@deftypefn {} {@var{y} =} sqrt (@var{x})
2097Compute the square root of each element of @var{x}.
2098
2099If @var{x} is negative, a complex result is returned.
2100
2101To compute the matrix square root, @pxref{Linear Algebra}.
2102@seealso{realsqrt, nthroot}
2103@end deftypefn */)
2104{
2105 if (args.length () != 1)
2106 print_usage ();
2107
2108 return ovl (args(0).sqrt ());
2109}
2110
2111/*
2112%!assert (sqrt (4), 2)
2113%!assert (sqrt (-1), i)
2114%!assert (sqrt (1+i), exp (0.5 * log (1+i)), sqrt (eps))
2115%!assert (sqrt ([4, -4; i, 1-i]),
2116%! [2, 2i; exp(0.5 * log (i)), exp(0.5 * log (1-i))], sqrt (eps))
2117
2118%!assert (sqrt (single (4)), single (2))
2119%!assert (sqrt (single (-1)), single (i))
2120%!assert (sqrt (single (1+i)),
2121%! single (exp (0.5 * log (1+i))), sqrt (eps ("single")))
2122%!assert (sqrt (single ([4, -4; i, 1-i])),
2123%! single ([2, 2i; exp(0.5 * log (i)), exp(0.5 * log (1-i))]),
2124%! sqrt (eps ("single")))
2125
2126%!error sqrt ()
2127%!error sqrt (1, 2)
2128*/
2129
2130DEFUN (tan, args, ,
2131 doc: /* -*- texinfo -*-
2132@deftypefn {} {@var{y} =} tan (@var{z})
2133Compute the tangent for each element of @var{x} in radians.
2134@seealso{atan, tand, tanh}
2135@end deftypefn */)
2136{
2137 if (args.length () != 1)
2138 print_usage ();
2139
2140 return ovl (args(0).tan ());
2141}
2142
2143/*
2144%!shared rt2, rt3
2145%! rt2 = sqrt (2);
2146%! rt3 = sqrt (3);
2147
2148%!test
2149%! x = [0, pi/6, pi/4, pi/3, 2*pi/3, 3*pi/4, 5*pi/6, pi];
2150%! v = [0, rt3/3, 1, rt3, -rt3, -1, -rt3/3, 0];
2151%! assert (tan (x), v, sqrt (eps));
2152
2153%!test
2154%! x = single ([0, pi/6, pi/4, pi/3, 2*pi/3, 3*pi/4, 5*pi/6, pi]);
2155%! v = single ([0, rt3/3, 1, rt3, -rt3, -1, -rt3/3, 0]);
2156%! assert (tan (x), v, sqrt (eps ("single")));
2157
2158%!error tan ()
2159%!error tan (1, 2)
2160*/
2161
2162DEFUN (tanh, args, ,
2163 doc: /* -*- texinfo -*-
2164@deftypefn {} {@var{y} =} tanh (@var{x})
2165Compute hyperbolic tangent for each element of @var{x}.
2166@seealso{atanh, sinh, cosh}
2167@end deftypefn */)
2168{
2169 if (args.length () != 1)
2170 print_usage ();
2171
2172 return ovl (args(0).tanh ());
2173}
2174
2175/*
2176%!test
2177%! x = [0, pi*i];
2178%! v = [0, 0];
2179%! assert (tanh (x), v, sqrt (eps));
2180
2181%!test
2182%! x = single ([0, pi*i]);
2183%! v = single ([0, 0]);
2184%! assert (tanh (x), v, sqrt (eps ("single")));
2185
2186%!error tanh ()
2187%!error tanh (1, 2)
2188*/
2189
2190DEFUN (lower, args, ,
2191 doc: /* -*- texinfo -*-
2192@deftypefn {} {@var{y} =} lower (@var{s})
2193@deftypefnx {} {@var{y} =} tolower (@var{s})
2194Return a copy of the string or cell string @var{s}, with each uppercase
2195character replaced by the corresponding lowercase one; non-alphabetic
2196characters are left unchanged.
2197
2198For example:
2199
2200@example
2201@group
2202lower ("MiXeD cAsE 123")
2203 @xresult{} "mixed case 123"
2204@end group
2205@end example
2206
2207Programming Note: @code{tolower} is an alias for @code{lower} and either name
2208can be used in Octave.
2209
2210@seealso{upper}
2211@end deftypefn */)
2212{
2213 if (args.length () != 1)
2214 print_usage ();
2215
2216 return ovl (args(0).xtolower ());
2217}
2218
2219DEFALIAS (tolower, lower);
2220
2221/*
2222%!assert (lower ("OCTAVE"), "octave")
2223%!assert (lower ("123OCTave! _&"), "123octave! _&")
2224%!assert (lower ({"ABC", "DEF", {"GHI", {"JKL"}}}),
2225%! {"abc", "def", {"ghi", {"jkl"}}})
2226%!assert (lower (["ABC"; "DEF"]), ["abc"; "def"])
2227%!assert (lower ({["ABC"; "DEF"]}), {["abc";"def"]})
2228%!assert (lower (["ABCÄÖÜSS"; "abcäöüß"]),
2229%! ["abcäöüss"; "abcäöüß"])
2230%!assert (lower (repmat ("ÄÖÜ", 2, 1, 3)), repmat ("äöü", 2, 1, 3))
2231%!assert (lower (68), 68)
2232%!assert (lower ({[68, 68; 68, 68]}), {[68, 68; 68, 68]})
2233%!assert (lower (68i), 68i)
2234%!assert (lower ({[68i, 68; 68, 68i]}), {[68i, 68; 68, 68i]})
2235%!assert (lower (single (68i)), single (68i))
2236%!assert (lower ({single([68i, 68; 68, 68i])}), {single([68i, 68; 68, 68i])})
2237
2238%!test
2239%! classes = {@char, @double, @single, ...
2240%! @int8, @int16, @int32, @int64, ...
2241%! @uint8, @uint16, @uint32, @uint64};
2242%! for i = 1:numel (classes)
2243%! cls = classes{i};
2244%! assert (class (lower (cls (97))), class (cls (97)));
2245%! assert (class (lower (cls ([98, 99]))), class (cls ([98, 99])));
2246%! endfor
2247%!test
2248%! a(3,3,3,3) = "D";
2249%! assert (lower (a)(3,3,3,3), "d");
2250
2251%!test
2252%! charset = char (0:127);
2253%! result = charset;
2254%! result (double ("A":"Z") + 1) = result (double ("a":"z") + 1);
2255%! assert (lower (charset), result);
2256
2257%!error <Invalid call to lower> lower ()
2258%!error <Invalid call to lower> tolower ()
2259%!error lower (1, 2)
2260*/
2261
2262DEFUN (upper, args, ,
2263 doc: /* -*- texinfo -*-
2264@deftypefn {} {@var{y} =} upper (@var{s})
2265@deftypefnx {} {@var{y} =} toupper (@var{s})
2266Return a copy of the string or cell string @var{s}, with each lowercase
2267character replaced by the corresponding uppercase one; non-alphabetic
2268characters are left unchanged.
2269
2270For example:
2271
2272@example
2273@group
2274upper ("MiXeD cAsE 123")
2275 @xresult{} "MIXED CASE 123"
2276@end group
2277@end example
2278
2279Programming Note: @code{toupper} is an alias for @code{upper} and either name
2280can be used in Octave.
2281
2282@seealso{lower}
2283@end deftypefn */)
2284{
2285 if (args.length () != 1)
2286 print_usage ();
2287
2288 return ovl (args(0).xtoupper ());
2289}
2290
2291DEFALIAS (toupper, upper);
2292
2293/*
2294%!assert (upper ("octave"), "OCTAVE")
2295%!assert (upper ("123OCTave! _&"), "123OCTAVE! _&")
2296%!assert (upper ({"abc", "def", {"ghi", {"jkl"}}}),
2297%! {"ABC", "DEF", {"GHI", {"JKL"}}})
2298%!assert (upper (["abc"; "def"]), ["ABC"; "DEF"])
2299%!assert (upper ({["abc"; "def"]}), {["ABC";"DEF"]})
2300%!assert (upper (["ABCÄÖÜSS"; "abcäöüß"]),
2301%! ["ABCÄÖÜSS"; "ABCÄÖÜSS"])
2302%!assert (upper (repmat ("äöü", 2, 1, 3)), repmat ("ÄÖÜ", 2, 1, 3))
2303%!assert (upper (100), 100)
2304%!assert (upper ({[100, 100; 100, 100]}), {[100, 100; 100, 100]})
2305%!assert (upper (100i), 100i)
2306%!assert (upper ({[100i, 100; 100, 100i]}), {[100i, 100; 100, 100i]})
2307%!assert (upper (single (100i)), single (100i))
2308%!assert (upper ({single([100i, 100; 100, 100i])}),
2309%! {single([100i, 100; 100, 100i])})
2310
2311%!test
2312%! classes = {@char, @double, @single, ...
2313%! @int8, @int16, @int32, @int64, ...
2314%! @uint8, @uint16, @uint32, @uint64};
2315%! for i = 1:numel (classes)
2316%! cls = classes{i};
2317%! assert (class (upper (cls (97))), class (cls (97)));
2318%! assert (class (upper (cls ([98, 99]))), class (cls ([98, 99])));
2319%! endfor
2320%!test
2321%! a(3,3,3,3) = "d";
2322%! assert (upper (a)(3,3,3,3), "D");
2323%!test
2324%! charset = char (0:127);
2325%! result = charset;
2326%! result (double ("a":"z") + 1) = result (double ("A":"Z") + 1);
2327%! assert (upper (charset), result);
2328
2329%!error <Invalid call to upper> upper ()
2330%!error <Invalid call to upper> toupper ()
2331%!error upper (1, 2)
2332*/
2333
2334DEFALIAS (gammaln, lgamma);
2335
2336OCTAVE_END_NAMESPACE(octave)
ComplexColumnVector conj(const ComplexColumnVector &a)
octave_value xsignbit() const
Definition ov.h:1520
ColumnVector real(const ComplexColumnVector &a)
ColumnVector imag(const ComplexColumnVector &a)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage()
Definition defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition defun.h:56
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Macro to define a builtin function with certain internal name.
Definition defun.h:85
#define DEFALIAS(alias, name)
Macro to define an alias for another existing function name.
Definition defun.h:160
function gamma(x)
Definition gamma.f:3
octave_value_list Fisascii(const octave_value_list &args, int)
Definition mappers.cc:1294
octave_value_list Fispunct(const octave_value_list &args, int)
Definition mappers.cc:1589
octave_value_list Fisgraph(const octave_value_list &args, int)
Definition mappers.cc:1414
octave_value_list Fisupper(const octave_value_list &args, int)
Definition mappers.cc:1645
octave_value_list Fisspace(const octave_value_list &args, int)
Definition mappers.cc:1619
octave_value_list Fislower(const octave_value_list &args, int)
Definition mappers.cc:1440
octave_value_list Fisdigit(const octave_value_list &args, int)
Definition mappers.cc:1344
octave_value_list Fiscntrl(const octave_value_list &args, int)
Definition mappers.cc:1318
octave_value_list Fisalnum(const octave_value_list &args, int)
Definition mappers.cc:1237
octave_value_list Fisalpha(const octave_value_list &args, int)
Definition mappers.cc:1267
octave_value_list Fisxdigit(const octave_value_list &args, int)
Definition mappers.cc:1671
octave_value_list Fsignbit(const octave_value_list &args, int)
Definition mappers.cc:2009
octave_value_list Fisprint(const octave_value_list &args, int)
Definition mappers.cc:1563
Complex asin(const Complex &x)
Definition mappers.cc:106
bool isna(double x)
Definition mappers.cc:46
Complex acos(const Complex &x)
Definition mappers.cc:84
std::complex< T > ceil(const std::complex< T > &x)
Definition mappers.h:115
Complex atan(const Complex &x)
Definition mappers.h:83
double roundb(double x)
Definition mappers.h:159
std::complex< T > floor(const std::complex< T > &x)
Definition mappers.h:142
bool isfinite(double x)
Definition mappers.h:229
bool isinf(double x)
Definition mappers.h:212
double signum(double x)
Definition mappers.h:255
double round(double x)
Definition mappers.h:148
bool isnan(bool)
Definition mappers.h:192
double fix(double x)
Definition mappers.h:130
double erfinv(double x)
double erfi(double x)
double dawson(double x)
Complex erf(const Complex &x)
double erfcinv(double x)
double erfcx(double x)
Complex expm1(const Complex &x)
Complex log1p(const Complex &x)
Complex erfc(const Complex &x)
double cbrt(double x)
double asinh(double x)
Definition oct-specfun.h:57
double atanh(double x)
Definition oct-specfun.h:62
double acosh(double x)
Definition oct-specfun.h:39
double lgamma(double x)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217
template int8_t abs(int8_t)