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