1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
|
# This file is a part of Julia. License is MIT: https://julialang.org/license
using LinearAlgebra
@test reim(2 + 3im) == (2, 3)
for T in (Int64, Float64)
@test real(T) == T
@test real(Complex{T}) == T
@test complex(T) == Complex{T}
@test complex(Complex{T}) == Complex{T}
end
#show
@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im"
@test sprint(show, complex(true, true)) == "Complex(true,true)"
@testset "arithmetic" begin
@testset for T in (Float16, Float32, Float64, BigFloat)
t = true
f = false
@testset "add and subtract" begin
@test isequal(T(+0.0) + im, Complex(T(+0.0), T(+1.0)))
@test isequal(T(-0.0) + im, Complex(T(-0.0), T(+1.0)))
@test isequal(T(+0.0) - im, Complex(T(+0.0), T(-1.0)))
@test isequal(T(-0.0) - im, Complex(T(-0.0), T(-1.0)))
@test isequal(T(+1.0) + im, Complex(T(+1.0), T(+1.0)))
@test isequal(T(-1.0) + im, Complex(T(-1.0), T(+1.0)))
@test isequal(T(+1.0) - im, Complex(T(+1.0), T(-1.0)))
@test isequal(T(-1.0) - im, Complex(T(-1.0), T(-1.0)))
@test isequal(im + T(+0.0), Complex(T(+0.0), T(+1.0)))
@test isequal(im + T(-0.0), Complex(T(-0.0), T(+1.0)))
@test isequal(im - T(+0.0), Complex(T(+0.0), T(+1.0)))
@test isequal(im - T(-0.0), Complex(T(+0.0), T(+1.0)))
@test isequal(im + T(+1.0), Complex(T(+1.0), T(+1.0)))
@test isequal(im + T(-1.0), Complex(T(-1.0), T(+1.0)))
@test isequal(im - T(+1.0), Complex(T(-1.0), T(+1.0)))
@test isequal(im - T(-1.0), Complex(T(+1.0), T(+1.0)))
@test isequal(T(f) + im, Complex(T(+0.0), T(+1.0)))
@test isequal(T(t) + im, Complex(T(+1.0), T(+1.0)))
@test isequal(T(f) - im, Complex(T(+0.0), T(-1.0)))
@test isequal(T(t) - im, Complex(T(+1.0), T(-1.0)))
@test isequal(im + T(f), Complex(T(+0.0), T(+1.0)))
@test isequal(im + T(t), Complex(T(+1.0), T(+1.0)))
@test isequal(im - T(f), Complex(T(+0.0), T(+1.0)))
@test isequal(im - T(t), Complex(T(-1.0), T(+1.0)))
end
@testset "multiply" begin
@test isequal(T(+0.0) * im, Complex(T(+0.0), T(+0.0)))
@test isequal(T(-0.0) * im, Complex(T(-0.0), T(-0.0)))
@test isequal(T(+1.0) * im, Complex(T(+0.0), T(+1.0)))
@test isequal(T(-1.0) * im, Complex(T(-0.0), T(-1.0)))
@test isequal(im * T(+0.0), Complex(T(+0.0), T(+0.0)))
@test isequal(im * T(-0.0), Complex(T(-0.0), T(-0.0)))
@test isequal(im * T(+1.0), Complex(T(+0.0), T(+1.0)))
@test isequal(im * T(-1.0), Complex(T(-0.0), T(-1.0)))
end
@testset "divide" begin
@test isequal(T(+0.0) / im, Complex(T(+0.0), T(-0.0)))
@test isequal(T(-0.0) / im, Complex(T(-0.0), T(+0.0)))
@test isequal(T(+1.0) / im, Complex(T(+0.0), T(-1.0)))
@test isequal(T(-1.0) / im, Complex(T(-0.0), T(+1.0)))
end
end
@test isequal(true + complex(true,false), complex(true,false) + complex(true,false))
@test isequal(complex(true,false) + true, complex(true,false) + complex(true,false))
@test isequal(true - complex(true,false), complex(true,false) - complex(true,false))
@test isequal(complex(true,false) - true, complex(true,false) - complex(true,false))
@test isequal(true * complex(true,false), complex(true,false) * complex(true,false))
@test isequal(complex(true,false) * true, complex(true,false) * complex(true,false))
end
@testset "basic math functions" begin
# We compare to BigFloat instead of hard-coding
# values, assuming that BigFloat has an independent and independently
# tested implementation.
@testset for T in (Float32, Float64)
x = Complex{T}(1//3 + 1//4*im)
y = Complex{T}(1//2 + 1//5*im)
yi = 4
@testset "Random values" begin
@test x^y ≈ big(x)^big(y)
@test x^yi ≈ big(x)^yi
@test x^true ≈ big(x)^true
@test x^false ≈ big(x)^false
@test x^1 ≈ big(x)^1
@test abs(x) ≈ abs(big(x))
@test abs2(x) ≈ abs2(big(x))
@test acos(x) ≈ acos(big(x))
@test acosh(1+x) ≈ acosh(1+big(x))
@test angle(x) ≈ angle(big(x))
@test asin(x) ≈ asin(big(x))
@test asinh(x) ≈ asinh(big(x))
@test atan(x) ≈ atan(big(x))
@test atanh(x) ≈ atanh(big(x))
@test cis(real(x)) ≈ cis(real(big(x)))
@test cis(x) ≈ cis(big(x))
@test cos(x) ≈ cos(big(x))
@test cosh(x) ≈ cosh(big(x))
@test exp(x) ≈ exp(big(x))
@test exp10(x) ≈ exp10(big(x))
@test exp2(x) ≈ exp2(big(x))
@test expm1(x) ≈ expm1(big(x)) atol=eps(T)
@test log(x) ≈ log(big(x))
@test log10(x) ≈ log10(big(x))
@test log1p(x) ≈ log1p(big(x))
@test log2(x) ≈ log2(big(x))
@test sin(x) ≈ sin(big(x))
@test sinh(x) ≈ sinh(big(x))
@test sqrt(x) ≈ sqrt(big(x))
@test tan(x) ≈ tan(big(x))
@test tanh(x) ≈ tanh(big(x))
end
@testset "Inverses" begin
@test acos(cos(x)) ≈ x
@test acosh(cosh(x)) ≈ x
@test asin(sin(x)) ≈ x
@test asinh(sinh(x)) ≈ x
@test atan(tan(x)) ≈ x
@test atanh(tanh(x)) ≈ x
@test cos(acos(x)) ≈ x
@test cosh(acosh(1+x)) ≈ 1+x
@test exp(log(x)) ≈ x
@test exp10(log10(x)) ≈ x
@test exp2(log2(x)) ≈ x
@test expm1(log1p(x)) ≈ x
@test log(exp(x)) ≈ x
@test log10(exp10(x)) ≈ x
@test log1p(expm1(x)) ≈ x
@test log2(exp2(x)) ≈ x
@test sin(asin(x)) ≈ x
@test sinh(asinh(x)) ≈ x
@test sqrt(x)^2 ≈ x
@test sqrt(x^2) ≈ x
@test tan(atan(x)) ≈ x
@test tanh(atanh(x)) ≈ x
end
@testset "Relations between functions" begin
@test cosh(x) ≈ (exp(x)+exp(-x))/2
@test cosh(x)^2-sinh(x)^2 ≈ 1
@test sin(x)^2+cos(x)^2 ≈ 1
@test sinh(x) ≈ (exp(x)-exp(-x))/2
@test tan(x) ≈ sin(x)/cos(x)
@test tanh(x) ≈ sinh(x)/cosh(x)
end
end
end
@testset "isinf" begin
@test iszero(real(complex(0.0,1.0))) # isimag deprecated
@test !iszero(real(complex(1.0,1.0))) # isimag deprecated
@test isinf(complex(Inf,0))
@test isinf(complex(-Inf,0))
@test isinf(complex(0,Inf))
@test isinf(complex(0,-Inf))
@test !isinf(complex(0,0))
end
@testset "flipsign" begin
@test isequal(complex( 0.0, 0.0 ), flipsign(complex( 0.0, 0.0 ), 1))
@test isequal(complex( -0.0, -0.0 ), flipsign(complex( 0.0, 0.0 ), -1))
@test isequal(complex( Inf, 0.0 ), flipsign(complex( Inf, 0.0 ), 1))
@test isequal(complex( -Inf, -0.0 ), flipsign(complex( Inf, 0.0 ), -1))
@test isequal(complex( 0.0, NaN ), flipsign(complex( 0.0, NaN ), 1.0))
@test isequal(complex( -0.0, NaN ), flipsign(complex( 0.0, NaN ), -1.0))
@test isequal(complex( 5.0, 4.0 ), flipsign(complex(-5.0, -4.0), -1))
@test isequal(complex( 0.5, -0.5 ), flipsign(complex(-0.5, 0.5), -2))
end
@testset "sqrt" begin
# tests special values from csqrt man page
# as well as conj(sqrt(z)) = sqrt(conj(z))
@test isequal(sqrt(complex( 0.0, 0.0)), complex( 0.0, 0.0))
@test isequal(sqrt(complex( 0.0,-0.0)), complex( 0.0,-0.0))
@test isequal(sqrt(complex( 0.0, Inf)), complex( Inf, Inf))
@test isequal(sqrt(complex( 0.0,-Inf)), complex( Inf,-Inf))
@test isequal(sqrt(complex( 0.0, NaN)), complex( NaN, NaN))
@test isequal(sqrt(complex(-0.0, 0.0)), complex( 0.0, 0.0))
@test isequal(sqrt(complex(-0.0,-0.0)), complex( 0.0,-0.0))
@test isequal(sqrt(complex( 5.0, 0.0)), complex(sqrt(5.0), 0.0))
@test isequal(sqrt(complex( 5.0,-0.0)), complex(sqrt(5.0),-0.0))
@test isequal(sqrt(complex(-5.0, 0.0)), complex( 0.0, sqrt(5.0)))
@test isequal(sqrt(complex(-5.0,-0.0)), complex( 0.0,-sqrt(5.0)))
@test isequal(sqrt(complex( Inf, 0.0)), complex( Inf, 0.0))
@test isequal(sqrt(complex( Inf,-0.0)), complex( Inf,-0.0))
@test isequal(sqrt(complex( Inf, 5.0)), complex( Inf, 0.0))
@test isequal(sqrt(complex( Inf,-5.0)), complex( Inf,-0.0))
@test isequal(sqrt(complex( Inf, Inf)), complex( Inf, Inf))
@test isequal(sqrt(complex( Inf,-Inf)), complex( Inf,-Inf))
@test isequal(sqrt(complex( Inf, NaN)), complex( Inf, NaN))
@test isequal(sqrt(complex(-Inf, 0.0)), complex( 0.0, Inf))
@test isequal(sqrt(complex(-Inf,-0.0)), complex( 0.0,-Inf))
@test isequal(sqrt(complex(-Inf, 5.0)), complex( 0.0, Inf))
@test isequal(sqrt(complex(-Inf,-5.0)), complex( 0.0,-Inf))
@test isequal(sqrt(complex(-Inf, Inf)), complex( Inf, Inf))
@test isequal(sqrt(complex(-Inf,-Inf)), complex( Inf,-Inf))
@test isequal(sqrt(complex(-Inf, NaN)), complex( NaN, Inf))
@test isequal(sqrt(complex( NaN, 0.0)), complex( NaN, NaN))
@test isequal(sqrt(complex( NaN, 0.0)), complex( NaN, NaN))
@test isequal(sqrt(complex( NaN, Inf)), complex( Inf, Inf))
@test isequal(sqrt(complex( NaN,-Inf)), complex( Inf,-Inf))
end
@testset "log(conj(z)) == conj(log(z))" begin
@test isequal(log(complex( 0.0, 0.0)), complex(-Inf, 0.0))
@test isequal(log(complex( 0.0,-0.0)), complex(-Inf,-0.0))
@test isequal(log(complex( 0.0, 1.0)), complex( 0.0, pi/2))
@test isequal(log(complex( 0.0,-1.0)), complex( 0.0,-pi/2))
@test isequal(log(complex( 0.0, Inf)), complex( Inf, pi/2))
@test isequal(log(complex( 0.0,-Inf)), complex( Inf,-pi/2))
@test isequal(log(complex( 0.0, NaN)), complex( NaN, NaN))
@test isequal(log(complex(-0.0, 0.0)), complex(-Inf, pi))
@test isequal(log(complex(-0.0,-0.0)), complex(-Inf,-pi))
@test isequal(log(complex( 5.0, 0.0)),complex(log(5.0), 0.0))
@test isequal(log(complex( 5.0,-0.0)),complex(log(5.0),-0.0))
@test isequal(log(complex( Inf, 5.0)), complex( Inf, 0.0))
@test isequal(log(complex( Inf,-5.0)), complex( Inf,-0.0))
@test isequal(log(complex( Inf, Inf)), complex( Inf, pi/4))
@test isequal(log(complex( Inf,-Inf)), complex( Inf,-pi/4))
@test isequal(log(complex( Inf, NaN)), complex( Inf, NaN))
@test isequal(log(complex(-Inf, 5.0)), complex( Inf, pi))
@test isequal(log(complex(-Inf,-5.0)), complex( Inf,-pi))
@test isequal(log(complex(-Inf, Inf)), complex( Inf, 3*pi/4))
@test isequal(log(complex(-Inf,-Inf)), complex( Inf,-3*pi/4))
@test isequal(log(complex(-Inf, NaN)), complex( Inf, NaN))
@test isequal(log(complex( NaN, 0.0)), complex( NaN, NaN))
@test isequal(log(complex( NaN, Inf)), complex( Inf, NaN))
@test isequal(log(complex( NaN,-Inf)), complex( Inf, NaN))
@test isequal(log(complex( NaN, NaN)), complex( NaN, NaN))
end
@testset "exp(conj(z)) == conj(exp(z))" begin
@test isequal(exp(complex( 0.0, 0.0)), complex(1.0, 0.0))
@test isequal(exp(complex( 0.0,-0.0)), complex(1.0,-0.0))
@test isequal(exp(complex( 0.0, Inf)), complex(NaN, NaN))
@test isequal(exp(complex( 0.0,-Inf)), complex(NaN, NaN))
@test isequal(exp(complex( 0.0, NaN)), complex(NaN, NaN))
@test isequal(exp(complex(-0.0, 0.0)), complex(1.0, 0.0))
@test isequal(exp(complex(-0.0,-0.0)), complex(1.0,-0.0))
@test isequal(exp(complex( 5.0, Inf)), complex(NaN, NaN))
@test isequal(exp(complex( Inf, 0.0)), complex(Inf, 0.0))
@test isequal(exp(complex( Inf,-0.0)), complex(Inf,-0.0))
@test isequal(exp(complex( Inf, 5.0)), complex(cos(5.0)*Inf,sin(5.0)* Inf))
@test isequal(exp(complex( Inf,-5.0)), complex(cos(5.0)*Inf,sin(5.0)*-Inf))
@test isequal(exp(complex( Inf, NaN)), complex(-Inf, NaN))
@test isequal(exp(complex( Inf, Inf)), complex(-Inf, NaN))
@test isequal(exp(complex( Inf,-Inf)), complex(-Inf, NaN))
@test isequal(exp(complex(-Inf, 0.0)), complex(0.0, 0.0))
@test isequal(exp(complex(-Inf,-0.0)), complex(0.0,-0.0))
@test isequal(exp(complex(-Inf, 5.0)), complex(cos(5.0)*0.0,sin(5.0)* 0.0))
@test isequal(exp(complex(-Inf,-5.0)), complex(cos(5.0)*0.0,sin(5.0)*-0.0))
@test isequal(exp(complex(-Inf, Inf)), complex(-0.0, 0.0))
@test isequal(exp(complex(-Inf,-Inf)), complex(-0.0,-0.0))
@test isequal(exp(complex(-Inf, NaN)), complex(-0.0, 0.0))
@test isequal(exp(complex( NaN, 0.0)), complex( NaN, 0.0))
@test isequal(exp(complex( NaN,-0.0)), complex( NaN,-0.0))
@test isequal(exp(complex( NaN, 5.0)), complex( NaN, NaN))
@test isequal(exp(complex( NaN, NaN)), complex( NaN, NaN))
end
@testset "expm1(conj(z)) == conj(expm1(z))" begin
@test isequal(expm1(complex( 0.0, 0.0)), complex(0.0, 0.0))
@test isequal(expm1(complex( 0.0,-0.0)), complex(0.0,-0.0))
@test isequal(expm1(complex( 0.0, Inf)), complex(NaN, NaN))
@test isequal(expm1(complex( 0.0,-Inf)), complex(NaN, NaN))
@test isequal(expm1(complex( 0.0, NaN)), complex(NaN, NaN))
@test isequal(expm1(complex(-0.0, 0.0)), complex(-0.0, 0.0))
@test isequal(expm1(complex(-0.0,-0.0)), complex(-0.0,-0.0))
@test isequal(expm1(complex( 5.0, Inf)), complex(NaN, NaN))
@test isequal(expm1(complex( Inf, 0.0)), complex(Inf, 0.0))
@test isequal(expm1(complex( Inf,-0.0)), complex(Inf,-0.0))
@test isequal(expm1(complex( Inf, 5.0)), complex(cos(5.0)*Inf,sin(5.0)* Inf))
@test isequal(expm1(complex( Inf,-5.0)), complex(cos(5.0)*Inf,sin(5.0)*-Inf))
@test isequal(expm1(complex( Inf, NaN)), complex(-Inf, NaN))
@test isequal(expm1(complex( Inf, Inf)), complex(-Inf, NaN))
@test isequal(expm1(complex( Inf,-Inf)), complex(-Inf, NaN))
@test isequal(expm1(complex(-Inf, 0.0)), complex(-1.0, 0.0))
@test isequal(expm1(complex(-Inf,-0.0)), complex(-1.0,-0.0))
@test isequal(expm1(complex(-Inf, 5.0)), complex(-1.0,sin(5.0)* 0.0))
@test isequal(expm1(complex(-Inf,-5.0)), complex(-1.0,sin(5.0)*-0.0))
@test isequal(expm1(complex(-Inf, Inf)), complex(-1.0, 0.0))
@test isequal(expm1(complex(-Inf,-Inf)), complex(-1.0,-0.0))
@test isequal(expm1(complex(-Inf, NaN)), complex(-1.0, 0.0))
@test isequal(expm1(complex( NaN, 0.0)), complex( NaN, 0.0))
@test isequal(expm1(complex( NaN,-0.0)), complex( NaN,-0.0))
@test isequal(expm1(complex( NaN, 5.0)), complex( NaN, NaN))
@test isequal(expm1(complex( NaN, NaN)), complex( NaN, NaN))
@test isequal(expm1(complex( 1e-20, 0.0)), complex(expm1( 1e-20), 0.0))
@test isequal(expm1(complex(-1e-20, 0.0)), complex(expm1(-1e-20), 0.0))
@test expm1(complex( 1e-20, 1e-10)) ≈ complex( 5e-21, 1e-10)
@test expm1(complex( 1e-20,-1e-10)) ≈ complex( 5e-21,-1e-10)
@test expm1(complex(-1e-20, 1e-10)) ≈ complex(-1.5e-20, 1e-10)
@test expm1(complex(-1e-20,-1e-10)) ≈ complex(-1.5e-20,-1e-10)
@test expm1(complex( 10.0, 10.0)) ≈ exp(complex( 10.0, 10.0))-1
@test expm1(complex( 10.0,-10.0)) ≈ exp(complex( 10.0,-10.0))-1
@test expm1(complex(-10.0, 10.0)) ≈ exp(complex(-10.0, 10.0))-1
@test expm1(complex(-10.0,-10.0)) ≈ exp(complex(-10.0,-10.0))-1
end
import Base.Math.@horner
@testset "log1p" begin
@test isequal(log1p(complex(Inf, 3)), complex(Inf, +0.))
@test isequal(log1p(complex(Inf, -3)), complex(Inf, -0.))
@test isequal(log1p(complex(-Inf, 3)), complex(Inf, +pi))
@test isequal(log1p(complex(-Inf, -3)), complex(Inf, -pi))
@test isequal(log1p(complex(Inf, NaN)), complex(Inf, NaN))
@test isequal(log1p(complex(NaN, 0)), complex(NaN, NaN))
@test isequal(log1p(complex(0, NaN)), complex(NaN, NaN))
@test isequal(log1p(complex(-1, +0.)), complex(-Inf, +0.))
@test isequal(log1p(complex(-1, -0.)), complex(-Inf, -0.))
@test isequal(log1p(complex(-2, 1e-10)), log(1 + complex(-2, 1e-10)))
@test isequal(log1p(complex(1, Inf)), complex(Inf, pi/2))
@test isequal(log1p(complex(1, -Inf)), complex(Inf, -pi/2))
for z in (1e-10+1e-9im, 1e-10-1e-9im, -1e-10+1e-9im, -1e-10-1e-9im)
@test log1p(z) ≈ @horner(z, 0, 1, -0.5, 1/3, -0.25, 0.2)
end
for z in (15+4im, 0.2+3im, 0.08-0.9im)
@test log1p(z) ≈ log(1+z)
end
end
@testset "^ (cpow)" begin
# equivalent to exp(y*log(x))
# except for 0^0?
# conj(x)^conj(y) = conj(x^y)
@test isequal(complex( 0.0, 0.0)^complex( 0.0, 0.0), complex(1.0, 0.0))
@test isequal(complex( 0.0, 0.0)^complex( 0.0,-0.0), complex(1.0, 0.0))
@test isequal(complex( 0.0, 0.0)^complex(-0.0, 0.0), complex(1.0,-0.0))
@test isequal(complex( 0.0, 0.0)^complex(-0.0,-0.0), complex(1.0,-0.0))
@test isequal(complex( 0.0,-0.0)^complex( 0.0, 0.0), complex(1.0,-0.0))
@test isequal(complex( 0.0,-0.0)^complex( 0.0,-0.0), complex(1.0,-0.0))
@test isequal(complex( 0.0,-0.0)^complex(-0.0, 0.0), complex(1.0, 0.0))
@test isequal(complex( 0.0,-0.0)^complex(-0.0,-0.0), complex(1.0, 0.0))
@test isequal(complex(-0.0, 0.0)^complex( 0.0, 0.0), complex(1.0, 0.0))
@test isequal(complex(-0.0, 0.0)^complex( 0.0,-0.0), complex(1.0, 0.0))
@test isequal(complex(-0.0, 0.0)^complex(-0.0, 0.0), complex(1.0,-0.0))
@test isequal(complex(-0.0, 0.0)^complex(-0.0,-0.0), complex(1.0,-0.0))
@test isequal(complex(-0.0,-0.0)^complex( 0.0, 0.0), complex(1.0,-0.0))
@test isequal(complex(-0.0,-0.0)^complex( 0.0,-0.0), complex(1.0,-0.0))
@test isequal(complex(-0.0,-0.0)^complex(-0.0, 0.0), complex(1.0, 0.0))
@test isequal(complex(-0.0,-0.0)^complex(-0.0,-0.0), complex(1.0, 0.0))
@test complex(0.0,1.0)^complex(2.0,0) ≈ complex(-1.0, 0.0)
@test complex(1.0,2.0)^complex(3.0,0) ≈ complex(-11.0, -2.0)
@test isequal(complex(0.0,0.0)^false, complex(1.0,0.0))
@test isequal(complex(0.0,0.0)^0, complex(1.0,0.0))
end
@testset "sinh and sin" begin
# sinh: has properties
# sinh(conj(z)) = conj(sinh(z))
# sinh(-z) = -sinh(z)
# sin: defined in terms of sinh
# sin(z) = -i*sinh(i*z)
# i.e. if sinh(a+ib) = x+iy
# then sin(b-ia) = y-ix
# sin(conj(z)) = conj(sin(z))
# sin(-z) = -sin(z)
# @test isequal(sin(complex( 0, 10000)),complex( 0.0, Inf))
# @test isequal(sin(complex( 0,-10000)),complex( 0.0,-Inf))
for (x,y) in [(complex( 0.0, 0.0), complex( 0.0, 0.0)),
(complex( 0.0, Inf), complex( 0.0, NaN)),
(complex( 0.0, NaN), complex( 0.0, NaN)),
(complex( 7.2, Inf), complex( NaN, NaN)),
(complex( 7.2, NaN), complex( NaN, NaN)),
(complex( 7.2, 0.0), complex( sinh(7.2), 0.0)),
(complex( 1e5, 0.0), complex( sinh(1e5), 0.0)),
(complex( Inf, 0.0), complex( Inf, 0.0)),
(complex( Inf, 7.2), Inf*cis(7.2)),
(complex( Inf, Inf), complex( Inf, NaN)),
(complex( Inf, NaN), complex( Inf, NaN)),
(complex( NaN, 0.0), complex( NaN, 0.0)),
(complex( NaN, 7.2), complex( NaN, NaN)),
(complex( NaN, NaN), complex( NaN, NaN)),
]
@test isequal(sinh(x), y)
@test isequal(sinh(conj(x)), conj(y))
@test isequal(sinh(-x), -y)
@test isequal(sinh(-conj(x)), -conj(y))
xx = complex(imag(x),-real(x))
yy = complex(imag(y),-real(y))
@test isequal(sin(xx),yy)
@test isequal(sin(conj(xx)), conj(yy))
@test isequal(sin(-xx), -yy)
@test isequal(sin(-conj(xx)), -conj(yy))
yyy = sin(pi*xx)
@test isequal(sinpi(xx), yyy)
@test isequal(sinpi(conj(xx)),conj(yyy))
@test isequal(sinpi(-xx),-yyy)
@test isequal(sinpi(-conj(xx)),-conj(yyy))
end
end
@testset "cosh and cos" begin
# cosh: has properties
# cosh(conj(z)) = conj(cosh(z))
# coshh(-z) = cosh(z)
# cos
# cos(z) = cosh(iz)
# i.e cos(b-ia) = cosh(a+ib)
# and cos(b+ia) = cosh(a-ib)
# cos(conj(z)) = conj(cos(z))
# cos(-z) = cos(z)
for (x,y) in [(complex( 0.0, 0.0), complex( 1.0, 0.0)),
(complex( 0.0, Inf), complex( NaN, 0.0)),
(complex( 0.0, NaN), complex( NaN, 0.0)),
(complex( 7.2, Inf), complex( NaN, NaN)),
(complex( 7.2, NaN), complex( NaN, NaN)),
(complex( 7.2, 0.0), complex( cosh(7.2), 0.0)),
(complex( 1e5, 0.0), complex( Inf, 0.0)),
(complex( Inf, 0.0), complex( Inf, 0.0)),
(complex( Inf, 7.2), Inf*cis(7.2)),
(complex( Inf, Inf), complex( Inf, NaN)),
(complex( Inf, NaN), complex( Inf, NaN)),
(complex( NaN, 0.0), complex( NaN, 0.0)),
(complex( NaN, 7.2), complex( NaN, NaN)),
(complex( NaN, NaN), complex( NaN, NaN)),
]
undef_sign = isequal(x,complex( NaN, 0.0)) || isequal(x,complex( 0.0, NaN))
@test isequal(cosh(x), y)
if !undef_sign
@test isequal(cosh(conj(x)), conj(y))
@test isequal(cosh(-x), y)
@test isequal(cosh(-conj(x)), conj(y))
end
xx = complex(imag(x),-real(x))
yy = y
@test isequal(cos(xx),yy)
if !undef_sign
@test isequal(cos(conj(xx)), conj(yy))
@test isequal(cos(-xx), yy)
@test isequal(cos(-conj(xx)), conj(yy))
end
yyy = cos(pi*xx)
@test isequal(cospi(xx), yyy)
if !undef_sign
@test isequal(cospi(conj(xx)), conj(yyy))
@test isequal(cospi(-xx), yyy)
@test isequal(cospi(-conj(xx)), conj(yyy))
end
end
end
@testset "tanh(op(z)) == op(tanh(z)) for op in (conj, -)" begin
@test isequal(tanh(complex( 0, 0)),complex(0.0,0.0)) #integer fallback
@test isequal(tanh(complex( 0.0, 0.0)),complex(0.0,0.0))
@test isequal(tanh(complex( 0.0,-0.0)),complex(0.0,-0.0))
@test_throws DomainError tanh(complex( 0.0, Inf))
@test_throws DomainError tanh(complex( 0.0,-Inf))
@test isequal(tanh(complex( 0.0, NaN)),complex(NaN,NaN))
@test isequal(tanh(complex(-0.0, 0.0)),complex(-0.0,0.0))
@test isequal(tanh(complex(-0.0,-0.0)),complex(-0.0,-0.0))
@test_throws DomainError tanh(complex( 5.0, Inf))
@test isequal(tanh(complex( 5.0, NaN)),complex(NaN,NaN))
@test isequal(tanh(complex( Inf, 0.0)),complex(1.0, 0.0))
@test isequal(tanh(complex( Inf,-0.0)),complex(1.0,-0.0))
@test isequal(tanh(complex( Inf, 5.0)),complex(1.0,sin(2*5.0)* 0.0))
@test isequal(tanh(complex( Inf,-5.0)),complex(1.0,sin(2*5.0)*-0.0))
@test isequal(tanh(complex( Inf, Inf)),complex(1.0, 0.0))
@test isequal(tanh(complex( Inf,-Inf)),complex(1.0,-0.0))
@test isequal(tanh(complex( Inf, NaN)),complex(1.0, 0.0))
@test isequal(tanh(complex(-Inf, 0.0)),complex(-1.0, 0.0))
@test isequal(tanh(complex(-Inf,-0.0)),complex(-1.0,-0.0))
@test isequal(tanh(complex(-Inf, 5.0)),complex(-1.0,sin(2*5.0)* 0.0))
@test isequal(tanh(complex(-Inf,-5.0)),complex(-1.0,sin(2*5.0)*-0.0))
@test isequal(tanh(complex(-Inf, Inf)),complex(-1.0, 0.0))
@test isequal(tanh(complex(-Inf,-Inf)),complex(-1.0,-0.0))
@test isequal(tanh(complex(-Inf, NaN)),complex(-1.0, 0.0))
@test isequal(tanh(complex( NaN, 0.0)),complex(NaN, 0.0))
@test isequal(tanh(complex( NaN,-0.0)),complex(NaN,-0.0))
@test isequal(tanh(complex( NaN, 5.0)),complex(NaN, NaN))
@test isequal(tanh(complex( NaN,-5.0)),complex(NaN, NaN))
@test isequal(tanh(complex( NaN, NaN)),complex(NaN, NaN))
end
@testset "tan(z) == -i tanh(iz)" begin
@test isequal(tan(complex( 0.0, Inf)),complex( 0.0, 1.0))
@test isequal(tan(complex( 0.0,-Inf)),complex( 0.0,-1.0))
@test isequal(tan(complex( 0.0, NaN)),complex( 0.0, NaN))
@test isequal(tan(complex(-0.0,-Inf)),complex(-0.0,-1.0))
@test isequal(tan(complex(-0.0, Inf)),complex(-0.0, 1.0))
@test isequal(tan(complex(-0.0, NaN)),complex(-0.0, NaN))
@test isequal(tan(complex( 5.0, Inf)),complex(sin(2*5.0)* 0.0, 1.0))
@test isequal(tan(complex( 5.0,-Inf)),complex(sin(2*5.0)* 0.0,-1.0))
@test isequal(tan(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(tan(complex(-5.0, Inf)),complex(sin(2*5.0)*-0.0, 1.0))
@test isequal(tan(complex(-5.0,-Inf)),complex(sin(2*5.0)*-0.0,-1.0))
@test isequal(tan(complex(-5.0, NaN)),complex( NaN, NaN))
@test_throws DomainError tan(complex( Inf, 5.0))
@test isequal(tan(complex( Inf, Inf)),complex( 0.0, 1.0))
@test isequal(tan(complex( Inf,-Inf)),complex( 0.0,-1.0))
@test isequal(tan(complex(-Inf, Inf)),complex(-0.0, 1.0))
@test isequal(tan(complex(-Inf,-Inf)),complex(-0.0,-1.0))
@test isequal(tan(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(tan(complex( NaN, Inf)),complex( 0.0, 1.0))
@test isequal(tan(complex( NaN,-Inf)),complex( 0.0,-1.0))
@test isequal(tan(complex( NaN, NaN)),complex( NaN, NaN))
end
@testset "acosh(conj(z)) == conj(acosh(z))" begin
@test isequal(acosh(complex( 0.0, 0.0)), complex( 0.0, pi/2))
@test isequal(acosh(complex( 0.0,-0.0)), complex( 0.0,-pi/2))
@test isequal(acosh(complex( 0.0, Inf)), complex( Inf, pi/2))
@test isequal(acosh(complex( 0.0,-Inf)), complex( Inf,-pi/2))
@test isequal(acosh(complex(-0.0, 0.0)), complex( 0.0, pi/2))
@test isequal(acosh(complex(-0.0,-0.0)), complex( 0.0,-pi/2))
@test isequal(acosh(complex( 5.0, Inf)), complex( Inf, pi/2))
@test isequal(acosh(complex( 5.0,-Inf)), complex( Inf,-pi/2))
@test isequal(acosh(complex( 5.0, NaN)), complex( NaN, NaN))
@test isequal(acosh(complex( Inf, 0.0)), complex( Inf, 0.0))
@test isequal(acosh(complex( Inf,-0.0)), complex( Inf,-0.0))
@test isequal(acosh(complex( Inf, 5.0)), complex( Inf, 0.0))
@test isequal(acosh(complex( Inf,-5.0)), complex( Inf,-0.0))
@test isequal(acosh(complex( Inf, Inf)), complex( Inf, pi/4))
@test isequal(acosh(complex( Inf,-Inf)), complex( Inf,-pi/4))
@test isequal(acosh(complex( Inf, NaN)), complex( Inf, NaN))
@test isequal(acosh(complex(-Inf, 0.0)), complex( Inf, pi))
@test isequal(acosh(complex(-Inf,-0.0)), complex( Inf,-pi))
@test isequal(acosh(complex(-Inf, 5.0)), complex( Inf, pi))
@test isequal(acosh(complex(-Inf,-5.0)), complex( Inf,-pi))
@test isequal(acosh(complex(-Inf, Inf)), complex( Inf, 3*pi/4))
@test isequal(acosh(complex(-Inf,-Inf)), complex( Inf,-3*pi/4))
@test isequal(acosh(complex(-Inf, NaN)), complex( Inf, NaN))
@test isequal(acosh(complex( NaN, Inf)), complex( Inf, NaN))
@test isequal(acosh(complex( NaN,-Inf)), complex( Inf, NaN))
@test isequal(acosh(complex( NaN, NaN)), complex( NaN, NaN))
end
@testset "acos(conj(z)) == conj(acos(z))" begin
@test isequal(acos(complex( 0, 0)),complex(pi/2,-0.0)) #integer fallback
@test isequal(acos(complex( 0.0, 0.0)),complex(pi/2,-0.0))
@test isequal(acos(complex( 0.0,-0.0)),complex(pi/2, 0.0))
@test isequal(acos(complex( 0.0, Inf)),complex(pi/2,-Inf))
@test isequal(acos(complex( 0.0,-Inf)),complex(pi/2, Inf))
@test isequal(acos(complex( 0.0, NaN)),complex(pi/2, NaN))
@test isequal(acos(complex(-0.0, 0.0)),complex(pi/2,-0.0))
@test isequal(acos(complex(-0.0,-0.0)),complex(pi/2, 0.0))
@test isequal(acos(complex(-0.0, NaN)),complex(pi/2, NaN))
@test isequal(acos(complex( 5.0, Inf)),complex(pi/2,-Inf))
@test isequal(acos(complex( 5.0,-Inf)),complex(pi/2, Inf))
@test isequal(acos(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(acos(complex( Inf, 0.0)),complex( 0.0,-Inf))
@test isequal(acos(complex( Inf,-0.0)),complex( 0.0, Inf))
@test isequal(acos(complex( Inf, 5.0)),complex( 0.0,-Inf))
@test isequal(acos(complex( Inf,-5.0)),complex( 0.0, Inf))
@test isequal(acos(complex( Inf, Inf)),complex(pi/4,-Inf))
@test isequal(acos(complex( Inf,-Inf)),complex(pi/4, Inf))
@test isequal(acos(complex( Inf, NaN)),complex( NaN, Inf))
@test isequal(acos(complex(-Inf, 0.0)),complex(pi,-Inf))
@test isequal(acos(complex(-Inf,-0.0)),complex(pi, Inf))
@test isequal(acos(complex(-Inf, 5.0)),complex(pi,-Inf))
@test isequal(acos(complex(-Inf,-5.0)),complex(pi, Inf))
@test isequal(acos(complex(-Inf, Inf)),complex(3*pi/4,-Inf))
@test isequal(acos(complex(-Inf,-Inf)),complex(3*pi/4, Inf))
@test isequal(acos(complex(-Inf, NaN)),complex( NaN, Inf))
@test isequal(acos(complex( NaN, 0.0)),complex( NaN, NaN))
@test isequal(acos(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(acos(complex( NaN, Inf)),complex( NaN,-Inf))
@test isequal(acos(complex( NaN,-Inf)),complex( NaN, Inf))
@test isequal(acos(complex( NaN, NaN)),complex( NaN, NaN))
end
@testset "asinh(op(z)) == op(asinh(z)) for op in (conj, -)" begin
@test isequal(asinh(complex( 0.0, 0.0)),complex( 0.0, 0.0))
@test isequal(asinh(complex( 0.0,-0.0)),complex( 0.0,-0.0))
@test isequal(asinh(complex( 0.0, Inf)),complex( Inf, pi/2))
@test isequal(asinh(complex( 0.0,-Inf)),complex( Inf,-pi/2))
@test isequal(asinh(complex( 0.0, NaN)),complex( NaN, NaN))
@test isequal(asinh(complex(-0.0, 0.0)),complex(-0.0, 0.0))
@test isequal(asinh(complex(-0.0,-0.0)),complex(-0.0,-0.0))
@test isequal(asinh(complex(-0.0, Inf)),complex(-Inf, pi/2))
@test isequal(asinh(complex(-0.0,-Inf)),complex(-Inf,-pi/2))
@test isequal(asinh(complex( 5.0, Inf)),complex( Inf, pi/2))
@test isequal(asinh(complex( 5.0,-Inf)),complex( Inf,-pi/2))
@test isequal(asinh(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(asinh(complex(-5.0, Inf)),complex(-Inf, pi/2))
@test isequal(asinh(complex(-5.0,-Inf)),complex(-Inf,-pi/2))
@test isequal(asinh(complex( Inf, Inf)),complex( Inf, pi/4))
@test isequal(asinh(complex( Inf,-Inf)),complex( Inf,-pi/4))
@test isequal(asinh(complex( Inf, NaN)),complex( Inf, NaN))
@test isequal(asinh(complex(-Inf, Inf)),complex(-Inf, pi/4))
@test isequal(asinh(complex(-Inf,-Inf)),complex(-Inf,-pi/4))
@test isequal(asinh(complex(-Inf, NaN)),complex(-Inf, NaN))
@test isequal(asinh(complex( NaN, 0.0)),complex( NaN, 0.0))
@test isequal(asinh(complex( NaN,-0.0)),complex( NaN,-0.0))
@test isequal(asinh(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(asinh(complex( NaN, Inf)),complex( Inf, NaN))
@test isequal(asinh(complex( NaN,-Inf)),complex( Inf, NaN))
@test isequal(asinh(complex( NaN, NaN)),complex( NaN, NaN))
end
@testset "asin(z) == -i*asinh(iz)" begin
@test isequal(asin(complex( 0.0, 0.0)),complex( 0.0, 0.0))
@test isequal(asin(complex( 0.0,-0.0)),complex( 0.0,-0.0))
@test isequal(asin(complex(-0.0, 0.0)),complex(-0.0, 0.0))
@test isequal(asin(complex( 0.0, NaN)),complex( 0.0, NaN))
@test isequal(asin(complex(-0.0,-0.0)),complex(-0.0,-0.0))
@test isequal(asin(complex(-0.0, NaN)),complex(-0.0, NaN))
@test isequal(asin(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(asin(complex( Inf, 0.0)),complex( pi/2, Inf))
@test isequal(asin(complex( Inf,-0.0)),complex( pi/2,-Inf))
@test isequal(asin(complex( Inf, 5.0)),complex( pi/2, Inf))
@test isequal(asin(complex( Inf,-5.0)),complex( pi/2,-Inf))
@test isequal(asin(complex( Inf, Inf)),complex( pi/4, Inf))
@test isequal(asin(complex( Inf,-Inf)),complex( pi/4,-Inf))
@test isequal(asin(complex( Inf, NaN)),complex( NaN, Inf))
@test isequal(asin(complex(-Inf, 0.0)),complex(-pi/2, Inf))
@test isequal(asin(complex(-Inf,-0.0)),complex(-pi/2,-Inf))
@test isequal(asin(complex(-Inf, 5.0)),complex(-pi/2, Inf))
@test isequal(asin(complex(-Inf,-5.0)),complex(-pi/2,-Inf))
@test isequal(asin(complex(-Inf, Inf)),complex(-pi/4, Inf))
@test isequal(asin(complex(-Inf,-Inf)),complex(-pi/4,-Inf))
@test isequal(asin(complex(-Inf, NaN)),complex( NaN, Inf))
@test isequal(asin(complex( NaN, 0.0)),complex( NaN, NaN))
@test isequal(asin(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(asin(complex( NaN, Inf)),complex( NaN, Inf))
@test isequal(asin(complex( NaN,-Inf)),complex( NaN,-Inf))
@test isequal(asin(complex( NaN, NaN)),complex( NaN, NaN))
end
@testset "atanh(op(z)) == op(atanh(z)) for op in (conj, -)" begin
@test isequal(atanh(complex( 0, 0)),complex( 0.0, 0.0)) #integer fallback
@test isequal(atanh(complex( 0.0, 0.0)),complex( 0.0, 0.0))
@test isequal(atanh(complex( 0.0,-0.0)),complex( 0.0,-0.0))
@test isequal(atanh(complex( 0.0, NaN)),complex( 0.0, NaN))
@test isequal(atanh(complex( 0.0, Inf)),complex( 0.0, pi/2))
@test isequal(atanh(complex( 0.0,-Inf)),complex( 0.0,-pi/2))
@test isequal(atanh(complex(-0.0, NaN)),complex(-0.0, NaN))
@test isequal(atanh(complex(-0.0, 0.0)),complex(-0.0, 0.0))
@test isequal(atanh(complex(-0.0,-0.0)),complex(-0.0,-0.0))
@test isequal(atanh(complex(-0.0, Inf)),complex(-0.0, pi/2))
@test isequal(atanh(complex(-0.0,-Inf)),complex(-0.0,-pi/2))
@test isequal(atanh(complex( 1.0, 0.0)),complex( Inf, 0.0))
@test isequal(atanh(complex(-1.0, 0.0)),complex(-Inf, 0.0))
@test isequal(atanh(complex( 5.0, Inf)),complex( 0.0, pi/2))
@test isequal(atanh(complex( 5.0,-Inf)),complex( 0.0,-pi/2))
@test isequal(atanh(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(atanh(complex(-5.0, Inf)),complex(-0.0, pi/2))
@test isequal(atanh(complex(-5.0,-Inf)),complex(-0.0,-pi/2))
@test isequal(atanh(complex(-5.0, NaN)),complex( NaN, NaN))
@test isequal(atanh(complex( Inf, 0.0)),complex(0.0, pi/2))
@test isequal(atanh(complex( Inf,-0.0)),complex(0.0,-pi/2))
@test isequal(atanh(complex( Inf, 5.0)),complex(0.0, pi/2))
@test isequal(atanh(complex( Inf,-5.0)),complex(0.0,-pi/2))
@test isequal(atanh(complex( Inf, Inf)),complex(0.0, pi/2))
@test isequal(atanh(complex( Inf,-Inf)),complex(0.0,-pi/2))
@test isequal(atanh(complex( Inf, NaN)),complex(0.0, NaN))
@test isequal(atanh(complex(-Inf, 0.0)),complex(-0.0, pi/2))
@test isequal(atanh(complex(-Inf,-0.0)),complex(-0.0,-pi/2))
@test isequal(atanh(complex(-Inf, 5.0)),complex(-0.0, pi/2))
@test isequal(atanh(complex(-Inf,-5.0)),complex(-0.0,-pi/2))
@test isequal(atanh(complex(-Inf, Inf)),complex(-0.0, pi/2))
@test isequal(atanh(complex(-Inf,-Inf)),complex(-0.0,-pi/2))
@test isequal(atanh(complex(-Inf, NaN)),complex(-0.0, NaN))
@test isequal(atanh(complex( NaN, 0.0)),complex( NaN, NaN))
@test isequal(atanh(complex( NaN,-0.0)),complex( NaN, NaN))
@test isequal(atanh(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(atanh(complex( NaN,-5.0)),complex( NaN, NaN))
@test isequal(atanh(complex( NaN, Inf)),complex( 0.0, pi/2))
@test isequal(atanh(complex( NaN,-Inf)),complex( 0.0,-pi/2))
@test isequal(atanh(complex( NaN, NaN)),complex( NaN, NaN))
end
@testset "atan(z) == -i*atanh(iz)" begin
@test isequal(atan(complex( 0.0, 0.0)),complex( 0.0, 0.0))
@test isequal(atan(complex( 0.0,-0.0)),complex( 0.0,-0.0))
@test isequal(atan(complex( 0.0, 1.0)),complex( 0.0, Inf))
@test isequal(atan(complex( 0.0, Inf)),complex( pi/2, 0.0))
@test isequal(atan(complex( 0.0,-Inf)),complex( pi/2,-0.0))
@test isequal(atan(complex( 0.0, NaN)),complex( NaN, NaN))
@test isequal(atan(complex(-0.0, 0.0)),complex(-0.0, 0.0))
@test isequal(atan(complex(-0.0,-0.0)),complex(-0.0,-0.0))
@test isequal(atan(complex(-0.0, Inf)),complex(-pi/2, 0.0))
@test isequal(atan(complex(-0.0,-Inf)),complex(-pi/2,-0.0))
@test isequal(atan(complex(-0.0, NaN)),complex( NaN, NaN))
@test isequal(atan(complex( 5.0, Inf)),complex( pi/2, 0.0))
@test isequal(atan(complex( 5.0,-Inf)),complex( pi/2,-0.0))
@test isequal(atan(complex( 5.0, NaN)),complex( NaN, NaN))
@test isequal(atan(complex(-5.0, Inf)),complex(-pi/2, 0.0))
@test isequal(atan(complex(-5.0,-Inf)),complex(-pi/2,-0.0))
@test isequal(atan(complex(-5.0, NaN)),complex( NaN, NaN))
@test isequal(atan(complex( Inf, 0.0)),complex( pi/2, 0.0))
@test isequal(atan(complex( Inf,-0.0)),complex( pi/2,-0.0))
@test isequal(atan(complex( Inf, 5.0)),complex( pi/2, 0.0))
@test isequal(atan(complex( Inf,-5.0)),complex( pi/2,-0.0))
@test isequal(atan(complex( Inf, Inf)),complex( pi/2, 0.0))
@test isequal(atan(complex( Inf,-Inf)),complex( pi/2,-0.0))
@test isequal(atan(complex( Inf, NaN)),complex( pi/2, 0.0))
@test isequal(atan(complex(-Inf, 0.0)),complex(-pi/2, 0.0))
@test isequal(atan(complex(-Inf,-0.0)),complex(-pi/2,-0.0))
@test isequal(atan(complex(-Inf, 5.0)),complex(-pi/2, 0.0))
@test isequal(atan(complex(-Inf,-5.0)),complex(-pi/2,-0.0))
@test isequal(atan(complex(-Inf, Inf)),complex(-pi/2, 0.0))
@test isequal(atan(complex(-Inf,-Inf)),complex(-pi/2,-0.0))
@test isequal(atan(complex(-Inf, NaN)),complex(-pi/2, 0.0))
@test isequal(atan(complex( NaN, 0.0)),complex( NaN, 0.0))
@test isequal(atan(complex( NaN,-0.0)),complex( NaN,-0.0))
@test isequal(atan(complex( NaN, 5.0)),complex( NaN, NaN))
@test isequal(atan(complex( NaN,-5.0)),complex( NaN, NaN))
@test isequal(atan(complex( NaN, Inf)),complex( NaN, 0.0))
@test isequal(atan(complex( NaN,-Inf)),complex( NaN,-0.0))
@test isequal(atan(complex( NaN, NaN)),complex( NaN, NaN))
end
# misc.
@test complex(1//2,1//3)^2 === complex(5//36, 1//3)
@test complex(2,2)^2 === complex(0,8)
let p = -2
@test_throws DomainError complex(2,2)^p
end
@test complex(2,2)^(-2) === complex(2.0,2.0)^(-2) === complex(0.0, -0.125)
@test complex.(1.0, [1.0, 1.0]) == [complex(1.0, 1.0), complex(1.0, 1.0)]
@test complex.([1.0, 1.0], 1.0) == [complex(1.0, 1.0), complex(1.0, 1.0)]
# robust division of Float64
# hard complex divisions from Fig 6 of arxiv.1210.4539
z7 = Complex{Float64}(3.898125604559113300e289, 8.174961907852353577e295)
z9 = Complex{Float64}(0.001953125, -0.001953125)
z10 = Complex{Float64}( 1.02951151789360578e-84, 6.97145987515076231e-220)
harddivs = ((1.0+im*1.0, 1.0+im*2^1023.0, 2^-1023.0-im*2^-1023.0), #1
(1.0+im*1.0, 2^-1023.0+im*2^-1023.0, 2^1023.0+im*0.0), #2
(2^1023.0+im*2^-1023.0, 2^677.0+im*2^-677.0, 2^346.0-im*2^-1008.0), #3
(2^1023.0+im*2^1023.0, 1.0+im*1.0, 2^1023.0+im*0.0), #4
(2^1020.0+im*2^-844., 2^656.0+im*2^-780.0, 2^364.0-im*2^-1072.0), #5
(2^-71.0+im*2^1021., 2^1001.0+im*2^-323.0, 2^-1072.0+im*2^20.0), #6
(2^-347.0+im*2^-54., 2^-1037.0+im*2^-1058.0, z7), #7
(2^-1074.0+im*2^-1074., 2^-1073.0+im*2^-1074., 0.6+im*0.2), #8
(2^1015.0+im*2^-989., 2^1023.0+im*2^1023.0, z9), #9
(2^-622.0+im*2^-1071., 2^-343.0+im*2^-798.0, z10) #10
)
# calculate "accurate bits" in range 0:53 by algorithm given in arxiv.1210.4539
function sb_accuracy(x,expected)
min(logacc(real(x),real(expected)),
logacc(imag(x),imag(expected)))
end
relacc(x,expected) = abs(x-expected)/abs(expected)
function logacc(x::Float64,expected::Float64)
x == expected && (return 53)
expected == 0 && (return 0)
(x == Inf || x == -Inf) && (return 0)
isnan(x) && (return 0)
ra = relacc(BigFloat(x),BigFloat(expected))
max(floor(Int,-log2(ra)),0)
end
# the robust division algorithm should have 53 or 52
# bits accuracy for each of the hard divisions
@test 52 <= minimum([sb_accuracy(h[1]/h[2],h[3]) for h in harddivs])
# division of non-Float64
function cdiv_test(a,b)
c=convert(Complex{Float64},a)/convert(Complex{Float64},b)
50 <= sb_accuracy(c,convert(Complex{Float64},a/b))
end
@test cdiv_test(complex(1//2, 3//4), complex(17//13, 4//5))
@test cdiv_test(complex(1,2), complex(8997,2432))
@testset "inv" begin
@test inv(1e300+0im) == 1e-300 - 0.0im
@test inv(0+1e300im) == 0.0 - 1e-300im
end
@testset "issue #7904" begin
@test log10(10+0im) === 1.0 + 0.0im
@test log2(2+0im) === 1.0 + 0.0im
end
@testset "sign" begin
for T in (Float32, Float64)
z = Complex{T}(1)
@test typeof(sign(z)) == typeof(z)
z = Complex{T}(0)
@test typeof(sign(z)) == typeof(z)
end
for T in (Int32, Int64)
z = Complex{T}(1)
@test typeof(sign(z)) == typeof(float(z))
z = Complex{T}(0)
@test typeof(sign(z)) == typeof(float(z))
end
@test sign(0 + 0im) == 0
@test sign(2 + 0im) == 1
@test sign(-2 + 0im) == -1
@test sign(1 + im) ≈ (1 + im) / sqrt(2)
@test sign(1 - im) ≈ (1 - im) / sqrt(2)
for T in (Float16, Float32, Float64)
z = Complex(zero(T), zero(T))
@test sign(z) === z
@test sign(-z) === -z
@test sign(conj(z)) === conj(z)
@test sign(-conj(z)) === -conj(z)
end
end
@testset "cis" begin
@test cis(0.0+1.0im) ≈ 0.367879441171442321595523770161460867445811131031767834507836+0.0im
@test cis(1.0+0.0im) ≈ 0.54030230586813971740093660744297660373231042061+0.84147098480789650665250232163029899962256306079im
@test cis(pi) ≈ -1.0+0.0im
@test cis(pi/2) ≈ 0.0+1.0im
end
@testset "exp2" begin
@test exp2(0.0+0.0im) == 1.0+0.0im
@test exp2(1.0+0.0im) == 2.0+0.0im
#wolframalpha
@test exp2(1.0+3.0im) ≈ -0.9739888359315627962096198412+1.74681016354974281701922im
@test exp2(im) ≈ 0.7692389013639721 + 0.6389612763136348im
end
@testset "exp10" begin
@test exp10(0.0+0.0im) == 1.0+0.0im
@test exp10(1.0+0.0im) == 10.0+0.0im
#wolframalpha
@test exp10(1.0+2.0im) ≈ -1.0701348355877020772086517528518239460495529361-9.9425756941378968736161937190915602112878340717im
@test exp10(im) ≈ -0.6682015101903132 + 0.7439803369574931im
end
@testset "round and float, PR #8291" begin
@test round(Complex(1.125, 0.875), digits=2) == Complex(1.12, 0.88)
@test round(Complex(1.5, 0.5), RoundDown, RoundUp) == Complex(1.0, 1.0)
@test round.([1:5;] .+ im) == [1:5;] .+ im
@test round.([1:5;] .+ 0.5im) == [1.0:5.0;]
@test float(Complex(1, 2)) == Complex(1.0, 2.0)
@test round(float(Complex(π, ℯ)), digits=3) == Complex(3.142, 2.718)
end
@testset "ComplexF16 arithmetic, PR #10003" begin
@test Float16(1)+Float16(1)im === ComplexF16(1, 1)
@test Float16(1)-Float16(1)im === Float16(1)+Float16(-1)im === ComplexF16(1, -1)
@test Float16(1)*im === ComplexF16(im)
@test Float16(1)/im === ComplexF16(0,-1)
@test Float16(1)^im === ComplexF16(1) === Float16(1)+Float16(0)im
end
# issue/PR #10148
@test typeof(Int8(1) - im) == Complex{Int8}
# issue #10926
@test typeof(π - 1im) == Complex{Float64}
@testset "issue #15969" begin
# specialized muladd for complex types
for x in (3, 3+13im), y in (2, 2+7im), z in (5, 5+11im)
@test muladd(x,y,z) === x*y + z
end
end
@testset "issue #11839" begin
# type stability for Complex{Int64}
let x = 1+im
@inferred sin(x)
@inferred cos(x)
@inferred norm(x)
@inferred opnorm(x)
end
end
@testset "issue #18785" begin
# type stability for exp, expm1 for Complex{Int64}
let x = 2*im
@inferred exp(x)
@inferred expm1(x)
end
end
# issue #19240
@test big(1)/(10+10im) ≈ (5-5im)/big(100) ≈ big"0.05" - big"0.05"*im
@testset "Complex Irrationals, issue #21204" begin
for x in (pi, ℯ, Base.MathConstants.catalan) # No need to test all of them
z = Complex(x, x)
@test typeof(z) == Complex{typeof(x)}
@test exp(z) ≈ exp(x) * cis(x)
@test log1p(z) ≈ log(1 + z)
@test exp2(z) ≈ exp(z * log(2))
@test exp10(z) ≈ exp(z * log(10))
end
end
@testset "expm1 type stability" begin
x = @inferred expm1(0.1im)
@test x isa ComplexF64
x = @inferred expm1(0.1f0im)
@test x isa ComplexF32
end
@testset "array printing with exponent format" begin
a = [1.0 + 1e-10im, 2.0e-15 - 2.0e-5im, 1.0e-15 + 2im, 1.0 + 2e-15im]
@test sprint((io, x) -> show(io, MIME("text/plain"), x), a) ==
join([
"4-element Array{Complex{Float64},1}:",
" 1.0 + 1.0e-10im",
" 2.0e-15 - 2.0e-5im ",
" 1.0e-15 + 2.0im ",
" 1.0 + 2.0e-15im"], "\n")
end
@testset "corner cases of division, issue #22983" begin
# These results abide by ISO/IEC 10967-3:2006(E) and
# mathematical definition of division of complex numbers.
for T in (Float32, Float64, BigFloat)
@test isequal(one(T) / zero(Complex{T}), one(Complex{T}) / zero(Complex{T}))
@test isequal(one(T) / zero(Complex{T}), Complex{T}(NaN, NaN))
@test isequal(one(Complex{T}) / zero(T), Complex{T}(Inf, NaN))
@test isequal(one(Complex{T}) / one(Complex{T}), one(Complex{T}))
@test isequal(one(T) / complex(one(T), zero(T)), Complex(one(T), -zero(T)))
@test isequal(one(T) / complex(one(T), -zero(T)), Complex(one(T), zero(T)))
end
end
@testset "complex^real, issue #14342" begin
for T in (Float32, Float64, BigFloat), p in (T(-21//10), -21//10)
z = T(2)+0im
@test real(z^p) ≈ 2^p
@test signbit(imag(z^p))
end
@test (2+0im)^(-21//10) === (2//1+0im)^(-21//10) === 2^-2.1 - 0.0im
end
@testset "more cpow" begin
# for testing signs of zeros, it is useful to convert ±0.0 to ±1e-15
zero2small(r::Real) = iszero(r) ? copysign(1e-15, r) : r
zero2small(z::Complex) = complex(zero2small(real(z)), zero2small(imag(z)))
≋(x::Real, y::Real) = x*y == 0 ? abs(x) < 1e-8 && abs(y) < 1e-8 && signbit(x)==signbit(y) : isfinite(x) ? x ≈ y : isequal(x, y)
≋(x::Complex, y::Complex) = real(x) ≋ real(y) && imag(x) ≋ imag(y)
≟(x,y) = isequal(x,y)
# test z^p for positive/negative/zero real and imaginary parts of z and p:
v=(-2.7,-3.0,-2.0,-0.0,+0.0,2.0,3.0,2.7)
for zr=v, zi=v, pr=v, pi=v
z = complex(zr,zi)
p = iszero(pi) ? pr : complex(pr,pi)
if isinteger(p)
c = zero2small(z)^Integer(pr)
else
c = exp(zero2small(p) * log(zero2small(z)))
end
if !iszero(z*p) # z==0 or p==0 is tricky, check it separately
@test z^p ≋ c
if isreal(p)
@test z^(p + 1e-15im) ≈ z^(p - 1e-15im) ≈ c
if isinteger(p)
@test isequal(z^Integer(pr), z^p)
end
elseif (zr != 0 || !signbit(zr)) && (zi != 0 || !signbit(zi))
@test isequal((Complex{Int}(z*10)//10)^p, z^p)
end
end
end
@test 2 ^ (0.3 + 0.0im) === 2.0 ^ (0.3 + 0.0im) === conj(2.0 ^ (0.3 - 0.0im)) ≋ 2.0 ^ (0.3 + 1e-15im)
@test 0.2 ^ (0.3 + 0.0im) === conj(0.2 ^ (0.3 - 0.0im)) ≋ 0.2 ^ (0.3 + 1e-15im)
@test (0.0 - 0.0im)^2.0 === (0.0 - 0.0im)^2 === (0.0 - 0.0im)^1.1 === (0.0 - 0.0im) ^ (1.1 + 2.3im) === 0.0 - 0.0im
@test (0.0 - 0.0im)^-2.0 ≟ (0.0 - 0.0im)^-2 ≟ (0.0 - 0.0im)^-1.1 ≟ (0.0 - 0.0im) ^ (-1.1 + 2.3im) ≟ NaN + NaN*im
@test (1.0+0.0)^(1.2+0.7im) === 1.0 + 0.0im
@test (-1.0+0.0)^(2.0+0.7im) ≈ exp(-0.7π)
@test (-4.0+0.0im)^1.5 === (-4.0)^(1.5+0.0im) === (-4)^(1.5+0.0im) === (-4)^(3//2+0im) === 0.0 - 8.0im
# issue #24515:
@test (Inf + Inf*im)^2.0 ≟ (Inf + Inf*im)^2 ≟ NaN + Inf*im
@test (0+0im)^-3.0 ≟ (0+0im)^-3 ≟ NaN + NaN*im
@test (1.0+0.0im)^1e300 === 1.0 + 0.0im
@test Inf^(-Inf + 0.0im) == (Inf + 0.0im)^(-Inf - 0.0im) == (Inf - 0.0im)^(-Inf - 0.0im) == (Inf - 0.0im)^-Inf == 0
# NaN propagation
@test (0 + NaN*im)^1 ≟ (0 + NaN*im)^1.0 ≟ (0 + NaN*im)^(1.0+0im) ≟ 0.0 + NaN*im
@test (0 + NaN*im)^2 ≟ (0 + NaN*im)^2.0 ≟ (0 + NaN*im)^(2.0+0im) ≟ NaN + NaN*im
@test (NaN + 0im)^2.0 ≟ (NaN + 0im)^(2.0+0im) ≟ (2+0im)^NaN ≟ NaN + 0im
@test (NaN + 0im)^2.5 ≟ NaN^(2.5+0im) ≟ (NaN + NaN*im)^2.5 ≟ (-2+0im)^NaN ≟ (2+0im)^(1+NaN*im) ≟ NaN + NaN*im
# more Inf cases:
@test (Inf + 0im)^Inf === Inf^(Inf + 0im) === (Inf + 0im)^(Inf + 0im) == Inf + 0im
@test (-Inf + 0im)^(0.7 + 0im) === (-Inf + 1im)^(0.7 + 0im) === conj((-Inf - 1im)^(0.7 + 0im)) === -Inf + Inf*im
@test (-Inf + 0.0im) ^ 3.1 === conj((-Inf - 0.0im) ^ 3.1) === -Inf - Inf*im
@test (3.0+0.0im)^(Inf + 1im) === (3.0-0.0im)^(Inf + 1im) === conj((3.0+0.0im)^(Inf - 1im)) === Inf + Inf*im
# The following cases should arguably give Inf + Inf*im, but currently
# give partial NaNs instead. Marking as broken for now (since Julia 0.4 at least),
# in the hope that someday we can fix these corner cases. (Python gets them wrong too.)
@test_broken (Inf + 1im)^3 === (Inf + 1im)^3.0 === (Inf + 1im)^(3+0im) === Inf + Inf*im
@test_broken (Inf + 1im)^3.1 === (Inf + 1im)^(3.1+0im) === Inf + Inf*im
# cases where phase angle is non-finite yield NaN + NaN*im:
@test NaN + NaN*im ≟ Inf ^ (2 + 3im) ≟ (Inf + 1im) ^ (2 + 3im) ≟ (Inf*im) ^ (2 + 3im) ≟
3^(Inf*im) ≟ (-3)^(Inf + 0im) ≟ (-3)^(Inf + 1im) ≟ (3+1im)^Inf ≟
(3+1im)^(Inf + 1im) ≟ (1e200+1e-200im)^Inf ≟ (1e200+1e-200im)^(Inf+1im)
@test @inferred(2.0^(3.0+0im)) === @inferred((2.0+0im)^(3.0+0im)) === @inferred((2.0+0im)^3.0) === 8.0+0.0im
end
|