gonum.org/v1/gonum@v0.14.0/blas/testblas/dtrsm.go (about) 1 // Copyright ©2014 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package testblas 6 7 import ( 8 "testing" 9 10 "golang.org/x/exp/rand" 11 12 "gonum.org/v1/gonum/blas" 13 "gonum.org/v1/gonum/floats" 14 ) 15 16 type Dtrsmer interface { 17 Dtrsm(s blas.Side, ul blas.Uplo, tA blas.Transpose, d blas.Diag, m, n int, 18 alpha float64, a []float64, lda int, b []float64, ldb int) 19 } 20 21 func DtrsmTest(t *testing.T, impl Dtrsmer) { 22 rnd := rand.New(rand.NewSource(1)) 23 for i, test := range []struct { 24 s blas.Side 25 ul blas.Uplo 26 tA blas.Transpose 27 d blas.Diag 28 m int 29 n int 30 alpha float64 31 a [][]float64 32 b [][]float64 33 34 want [][]float64 35 }{ 36 { 37 s: blas.Left, 38 ul: blas.Upper, 39 tA: blas.NoTrans, 40 d: blas.NonUnit, 41 m: 3, 42 n: 2, 43 alpha: 2, 44 a: [][]float64{ 45 {1, 2, 3}, 46 {0, 4, 5}, 47 {0, 0, 5}, 48 }, 49 b: [][]float64{ 50 {3, 6}, 51 {4, 7}, 52 {5, 8}, 53 }, 54 want: [][]float64{ 55 {1, 3.4}, 56 {-0.5, -0.5}, 57 {2, 3.2}, 58 }, 59 }, 60 { 61 s: blas.Left, 62 ul: blas.Upper, 63 tA: blas.NoTrans, 64 d: blas.Unit, 65 m: 3, 66 n: 2, 67 alpha: 2, 68 a: [][]float64{ 69 {1, 2, 3}, 70 {0, 4, 5}, 71 {0, 0, 5}, 72 }, 73 b: [][]float64{ 74 {3, 6}, 75 {4, 7}, 76 {5, 8}, 77 }, 78 want: [][]float64{ 79 {60, 96}, 80 {-42, -66}, 81 {10, 16}, 82 }, 83 }, 84 { 85 s: blas.Left, 86 ul: blas.Upper, 87 tA: blas.NoTrans, 88 d: blas.NonUnit, 89 m: 3, 90 n: 4, 91 alpha: 2, 92 a: [][]float64{ 93 {1, 2, 3}, 94 {0, 4, 5}, 95 {0, 0, 5}, 96 }, 97 b: [][]float64{ 98 {3, 6, 2, 9}, 99 {4, 7, 1, 3}, 100 {5, 8, 9, 10}, 101 }, 102 want: [][]float64{ 103 {1, 3.4, 1.2, 13}, 104 {-0.5, -0.5, -4, -3.5}, 105 {2, 3.2, 3.6, 4}, 106 }, 107 }, 108 { 109 s: blas.Left, 110 ul: blas.Upper, 111 tA: blas.NoTrans, 112 d: blas.Unit, 113 m: 3, 114 n: 4, 115 alpha: 2, 116 a: [][]float64{ 117 {1, 2, 3}, 118 {0, 4, 5}, 119 {0, 0, 5}, 120 }, 121 b: [][]float64{ 122 {3, 6, 2, 9}, 123 {4, 7, 1, 3}, 124 {5, 8, 9, 10}, 125 }, 126 want: [][]float64{ 127 {60, 96, 126, 146}, 128 {-42, -66, -88, -94}, 129 {10, 16, 18, 20}, 130 }, 131 }, 132 { 133 s: blas.Left, 134 ul: blas.Lower, 135 tA: blas.NoTrans, 136 d: blas.NonUnit, 137 m: 3, 138 n: 2, 139 alpha: 3, 140 a: [][]float64{ 141 {2, 0, 0}, 142 {3, 4, 0}, 143 {5, 6, 7}, 144 }, 145 b: [][]float64{ 146 {3, 6}, 147 {4, 7}, 148 {5, 8}, 149 }, 150 want: [][]float64{ 151 {4.5, 9}, 152 {-0.375, -1.5}, 153 {-0.75, -12.0 / 7}, 154 }, 155 }, 156 { 157 s: blas.Left, 158 ul: blas.Lower, 159 tA: blas.NoTrans, 160 d: blas.Unit, 161 m: 3, 162 n: 2, 163 alpha: 3, 164 a: [][]float64{ 165 {2, 0, 0}, 166 {3, 4, 0}, 167 {5, 6, 7}, 168 }, 169 b: [][]float64{ 170 {3, 6}, 171 {4, 7}, 172 {5, 8}, 173 }, 174 want: [][]float64{ 175 {9, 18}, 176 {-15, -33}, 177 {60, 132}, 178 }, 179 }, 180 { 181 s: blas.Left, 182 ul: blas.Lower, 183 tA: blas.NoTrans, 184 d: blas.NonUnit, 185 m: 3, 186 n: 4, 187 alpha: 3, 188 a: [][]float64{ 189 {2, 0, 0}, 190 {3, 4, 0}, 191 {5, 6, 7}, 192 }, 193 b: [][]float64{ 194 {3, 6, 2, 9}, 195 {4, 7, 1, 3}, 196 {5, 8, 9, 10}, 197 }, 198 want: [][]float64{ 199 {4.5, 9, 3, 13.5}, 200 {-0.375, -1.5, -1.5, -63.0 / 8}, 201 {-0.75, -12.0 / 7, 3, 39.0 / 28}, 202 }, 203 }, 204 { 205 s: blas.Left, 206 ul: blas.Lower, 207 tA: blas.NoTrans, 208 d: blas.Unit, 209 m: 3, 210 n: 4, 211 alpha: 3, 212 a: [][]float64{ 213 {2, 0, 0}, 214 {3, 4, 0}, 215 {5, 6, 7}, 216 }, 217 b: [][]float64{ 218 {3, 6, 2, 9}, 219 {4, 7, 1, 3}, 220 {5, 8, 9, 10}, 221 }, 222 want: [][]float64{ 223 {9, 18, 6, 27}, 224 {-15, -33, -15, -72}, 225 {60, 132, 87, 327}, 226 }, 227 }, 228 { 229 s: blas.Left, 230 ul: blas.Upper, 231 tA: blas.Trans, 232 d: blas.NonUnit, 233 m: 3, 234 n: 2, 235 alpha: 3, 236 a: [][]float64{ 237 {2, 3, 4}, 238 {0, 5, 6}, 239 {0, 0, 7}, 240 }, 241 b: [][]float64{ 242 {3, 6}, 243 {4, 7}, 244 {5, 8}, 245 }, 246 want: [][]float64{ 247 {4.5, 9}, 248 {-0.30, -1.2}, 249 {-6.0 / 35, -24.0 / 35}, 250 }, 251 }, 252 { 253 s: blas.Left, 254 ul: blas.Upper, 255 tA: blas.Trans, 256 d: blas.Unit, 257 m: 3, 258 n: 2, 259 alpha: 3, 260 a: [][]float64{ 261 {2, 3, 4}, 262 {0, 5, 6}, 263 {0, 0, 7}, 264 }, 265 b: [][]float64{ 266 {3, 6}, 267 {4, 7}, 268 {5, 8}, 269 }, 270 want: [][]float64{ 271 {9, 18}, 272 {-15, -33}, 273 {69, 150}, 274 }, 275 }, 276 { 277 s: blas.Left, 278 ul: blas.Upper, 279 tA: blas.Trans, 280 d: blas.NonUnit, 281 m: 3, 282 n: 4, 283 alpha: 3, 284 a: [][]float64{ 285 {2, 3, 4}, 286 {0, 5, 6}, 287 {0, 0, 7}, 288 }, 289 b: [][]float64{ 290 {3, 6, 6, 7}, 291 {4, 7, 8, 9}, 292 {5, 8, 10, 11}, 293 }, 294 want: [][]float64{ 295 {4.5, 9, 9, 10.5}, 296 {-0.3, -1.2, -0.6, -0.9}, 297 {-6.0 / 35, -24.0 / 35, -12.0 / 35, -18.0 / 35}, 298 }, 299 }, 300 { 301 s: blas.Left, 302 ul: blas.Upper, 303 tA: blas.Trans, 304 d: blas.Unit, 305 m: 3, 306 n: 4, 307 alpha: 3, 308 a: [][]float64{ 309 {2, 3, 4}, 310 {0, 5, 6}, 311 {0, 0, 7}, 312 }, 313 b: [][]float64{ 314 {3, 6, 6, 7}, 315 {4, 7, 8, 9}, 316 {5, 8, 10, 11}, 317 }, 318 want: [][]float64{ 319 {9, 18, 18, 21}, 320 {-15, -33, -30, -36}, 321 {69, 150, 138, 165}, 322 }, 323 }, 324 { 325 s: blas.Left, 326 ul: blas.Lower, 327 tA: blas.Trans, 328 d: blas.NonUnit, 329 m: 3, 330 n: 2, 331 alpha: 3, 332 a: [][]float64{ 333 {2, 0, 0}, 334 {3, 4, 0}, 335 {5, 6, 8}, 336 }, 337 b: [][]float64{ 338 {3, 6}, 339 {4, 7}, 340 {5, 8}, 341 }, 342 want: [][]float64{ 343 {-0.46875, 0.375}, 344 {0.1875, 0.75}, 345 {1.875, 3}, 346 }, 347 }, 348 { 349 s: blas.Left, 350 ul: blas.Lower, 351 tA: blas.Trans, 352 d: blas.Unit, 353 m: 3, 354 n: 2, 355 alpha: 3, 356 a: [][]float64{ 357 {2, 0, 0}, 358 {3, 4, 0}, 359 {5, 6, 8}, 360 }, 361 b: [][]float64{ 362 {3, 6}, 363 {4, 7}, 364 {5, 8}, 365 }, 366 want: [][]float64{ 367 {168, 267}, 368 {-78, -123}, 369 {15, 24}, 370 }, 371 }, 372 { 373 s: blas.Left, 374 ul: blas.Lower, 375 tA: blas.Trans, 376 d: blas.NonUnit, 377 m: 3, 378 n: 4, 379 alpha: 3, 380 a: [][]float64{ 381 {2, 0, 0}, 382 {3, 4, 0}, 383 {5, 6, 8}, 384 }, 385 b: [][]float64{ 386 {3, 6, 2, 3}, 387 {4, 7, 4, 5}, 388 {5, 8, 6, 7}, 389 }, 390 want: [][]float64{ 391 {-0.46875, 0.375, -2.0625, -1.78125}, 392 {0.1875, 0.75, -0.375, -0.1875}, 393 {1.875, 3, 2.25, 2.625}, 394 }, 395 }, 396 { 397 s: blas.Left, 398 ul: blas.Lower, 399 tA: blas.Trans, 400 d: blas.Unit, 401 m: 3, 402 n: 4, 403 alpha: 3, 404 a: [][]float64{ 405 {2, 0, 0}, 406 {3, 4, 0}, 407 {5, 6, 8}, 408 }, 409 b: [][]float64{ 410 {3, 6, 2, 3}, 411 {4, 7, 4, 5}, 412 {5, 8, 6, 7}, 413 }, 414 want: [][]float64{ 415 {168, 267, 204, 237}, 416 {-78, -123, -96, -111}, 417 {15, 24, 18, 21}, 418 }, 419 }, 420 { 421 s: blas.Right, 422 ul: blas.Upper, 423 tA: blas.NoTrans, 424 d: blas.NonUnit, 425 m: 4, 426 n: 3, 427 alpha: 3, 428 a: [][]float64{ 429 {2, 3, 4}, 430 {0, 5, 6}, 431 {0, 0, 7}, 432 }, 433 b: [][]float64{ 434 {10, 11, 12}, 435 {13, 14, 15}, 436 {16, 17, 18}, 437 {19, 20, 21}, 438 }, 439 want: [][]float64{ 440 {15, -2.4, -48.0 / 35}, 441 {19.5, -3.3, -66.0 / 35}, 442 {24, -4.2, -2.4}, 443 {28.5, -5.1, -102.0 / 35}, 444 }, 445 }, 446 { 447 s: blas.Right, 448 ul: blas.Upper, 449 tA: blas.NoTrans, 450 d: blas.Unit, 451 m: 4, 452 n: 3, 453 alpha: 3, 454 a: [][]float64{ 455 {2, 3, 4}, 456 {0, 5, 6}, 457 {0, 0, 8}, 458 }, 459 b: [][]float64{ 460 {10, 11, 12}, 461 {13, 14, 15}, 462 {16, 17, 18}, 463 {19, 20, 21}, 464 }, 465 want: [][]float64{ 466 {30, -57, 258}, 467 {39, -75, 339}, 468 {48, -93, 420}, 469 {57, -111, 501}, 470 }, 471 }, 472 { 473 s: blas.Right, 474 ul: blas.Upper, 475 tA: blas.NoTrans, 476 d: blas.NonUnit, 477 m: 2, 478 n: 3, 479 alpha: 3, 480 a: [][]float64{ 481 {2, 3, 4}, 482 {0, 5, 6}, 483 {0, 0, 7}, 484 }, 485 b: [][]float64{ 486 {10, 11, 12}, 487 {13, 14, 15}, 488 }, 489 want: [][]float64{ 490 {15, -2.4, -48.0 / 35}, 491 {19.5, -3.3, -66.0 / 35}, 492 }, 493 }, 494 { 495 s: blas.Right, 496 ul: blas.Upper, 497 tA: blas.NoTrans, 498 d: blas.Unit, 499 m: 2, 500 n: 3, 501 alpha: 3, 502 a: [][]float64{ 503 {2, 3, 4}, 504 {0, 5, 6}, 505 {0, 0, 8}, 506 }, 507 b: [][]float64{ 508 {10, 11, 12}, 509 {13, 14, 15}, 510 }, 511 want: [][]float64{ 512 {30, -57, 258}, 513 {39, -75, 339}, 514 }, 515 }, 516 { 517 s: blas.Right, 518 ul: blas.Lower, 519 tA: blas.NoTrans, 520 d: blas.NonUnit, 521 m: 4, 522 n: 3, 523 alpha: 3, 524 a: [][]float64{ 525 {2, 0, 0}, 526 {3, 5, 0}, 527 {4, 6, 8}, 528 }, 529 b: [][]float64{ 530 {10, 11, 12}, 531 {13, 14, 15}, 532 {16, 17, 18}, 533 {19, 20, 21}, 534 }, 535 want: [][]float64{ 536 {4.2, 1.2, 4.5}, 537 {5.775, 1.65, 5.625}, 538 {7.35, 2.1, 6.75}, 539 {8.925, 2.55, 7.875}, 540 }, 541 }, 542 { 543 s: blas.Right, 544 ul: blas.Lower, 545 tA: blas.NoTrans, 546 d: blas.Unit, 547 m: 4, 548 n: 3, 549 alpha: 3, 550 a: [][]float64{ 551 {2, 0, 0}, 552 {3, 5, 0}, 553 {4, 6, 8}, 554 }, 555 b: [][]float64{ 556 {10, 11, 12}, 557 {13, 14, 15}, 558 {16, 17, 18}, 559 {19, 20, 21}, 560 }, 561 want: [][]float64{ 562 {435, -183, 36}, 563 {543, -228, 45}, 564 {651, -273, 54}, 565 {759, -318, 63}, 566 }, 567 }, 568 { 569 s: blas.Right, 570 ul: blas.Lower, 571 tA: blas.NoTrans, 572 d: blas.NonUnit, 573 m: 2, 574 n: 3, 575 alpha: 3, 576 a: [][]float64{ 577 {2, 0, 0}, 578 {3, 5, 0}, 579 {4, 6, 8}, 580 }, 581 b: [][]float64{ 582 {10, 11, 12}, 583 {13, 14, 15}, 584 }, 585 want: [][]float64{ 586 {4.2, 1.2, 4.5}, 587 {5.775, 1.65, 5.625}, 588 }, 589 }, 590 { 591 s: blas.Right, 592 ul: blas.Lower, 593 tA: blas.NoTrans, 594 d: blas.Unit, 595 m: 2, 596 n: 3, 597 alpha: 3, 598 a: [][]float64{ 599 {2, 0, 0}, 600 {3, 5, 0}, 601 {4, 6, 8}, 602 }, 603 b: [][]float64{ 604 {10, 11, 12}, 605 {13, 14, 15}, 606 }, 607 want: [][]float64{ 608 {435, -183, 36}, 609 {543, -228, 45}, 610 }, 611 }, 612 { 613 s: blas.Right, 614 ul: blas.Upper, 615 tA: blas.Trans, 616 d: blas.NonUnit, 617 m: 4, 618 n: 3, 619 alpha: 3, 620 a: [][]float64{ 621 {2, 3, 4}, 622 {0, 5, 6}, 623 {0, 0, 8}, 624 }, 625 b: [][]float64{ 626 {10, 11, 12}, 627 {13, 14, 15}, 628 {16, 17, 18}, 629 {19, 20, 21}, 630 }, 631 want: [][]float64{ 632 {4.2, 1.2, 4.5}, 633 {5.775, 1.65, 5.625}, 634 {7.35, 2.1, 6.75}, 635 {8.925, 2.55, 7.875}, 636 }, 637 }, 638 { 639 s: blas.Right, 640 ul: blas.Upper, 641 tA: blas.Trans, 642 d: blas.Unit, 643 m: 4, 644 n: 3, 645 alpha: 3, 646 a: [][]float64{ 647 {2, 3, 4}, 648 {0, 5, 6}, 649 {0, 0, 8}, 650 }, 651 b: [][]float64{ 652 {10, 11, 12}, 653 {13, 14, 15}, 654 {16, 17, 18}, 655 {19, 20, 21}, 656 }, 657 want: [][]float64{ 658 {435, -183, 36}, 659 {543, -228, 45}, 660 {651, -273, 54}, 661 {759, -318, 63}, 662 }, 663 }, 664 { 665 s: blas.Right, 666 ul: blas.Upper, 667 tA: blas.Trans, 668 d: blas.NonUnit, 669 m: 2, 670 n: 3, 671 alpha: 3, 672 a: [][]float64{ 673 {2, 3, 4}, 674 {0, 5, 6}, 675 {0, 0, 8}, 676 }, 677 b: [][]float64{ 678 {10, 11, 12}, 679 {13, 14, 15}, 680 }, 681 want: [][]float64{ 682 {4.2, 1.2, 4.5}, 683 {5.775, 1.65, 5.625}, 684 }, 685 }, 686 { 687 s: blas.Right, 688 ul: blas.Upper, 689 tA: blas.Trans, 690 d: blas.Unit, 691 m: 2, 692 n: 3, 693 alpha: 3, 694 a: [][]float64{ 695 {2, 3, 4}, 696 {0, 5, 6}, 697 {0, 0, 8}, 698 }, 699 b: [][]float64{ 700 {10, 11, 12}, 701 {13, 14, 15}, 702 }, 703 want: [][]float64{ 704 {435, -183, 36}, 705 {543, -228, 45}, 706 }, 707 }, 708 { 709 s: blas.Right, 710 ul: blas.Lower, 711 tA: blas.Trans, 712 d: blas.NonUnit, 713 m: 4, 714 n: 3, 715 alpha: 3, 716 a: [][]float64{ 717 {2, 0, 0}, 718 {3, 5, 0}, 719 {4, 6, 8}, 720 }, 721 b: [][]float64{ 722 {10, 11, 12}, 723 {13, 14, 15}, 724 {16, 17, 18}, 725 {19, 20, 21}, 726 }, 727 want: [][]float64{ 728 {15, -2.4, -1.2}, 729 {19.5, -3.3, -1.65}, 730 {24, -4.2, -2.1}, 731 {28.5, -5.1, -2.55}, 732 }, 733 }, 734 { 735 s: blas.Right, 736 ul: blas.Lower, 737 tA: blas.Trans, 738 d: blas.Unit, 739 m: 4, 740 n: 3, 741 alpha: 3, 742 a: [][]float64{ 743 {2, 0, 0}, 744 {3, 5, 0}, 745 {4, 6, 8}, 746 }, 747 b: [][]float64{ 748 {10, 11, 12}, 749 {13, 14, 15}, 750 {16, 17, 18}, 751 {19, 20, 21}, 752 }, 753 want: [][]float64{ 754 {30, -57, 258}, 755 {39, -75, 339}, 756 {48, -93, 420}, 757 {57, -111, 501}, 758 }, 759 }, 760 { 761 s: blas.Right, 762 ul: blas.Lower, 763 tA: blas.Trans, 764 d: blas.NonUnit, 765 m: 2, 766 n: 3, 767 alpha: 3, 768 a: [][]float64{ 769 {2, 0, 0}, 770 {3, 5, 0}, 771 {4, 6, 8}, 772 }, 773 b: [][]float64{ 774 {10, 11, 12}, 775 {13, 14, 15}, 776 }, 777 want: [][]float64{ 778 {15, -2.4, -1.2}, 779 {19.5, -3.3, -1.65}, 780 }, 781 }, 782 { 783 s: blas.Right, 784 ul: blas.Lower, 785 tA: blas.Trans, 786 d: blas.Unit, 787 m: 2, 788 n: 3, 789 alpha: 3, 790 a: [][]float64{ 791 {2, 0, 0}, 792 {3, 5, 0}, 793 {4, 6, 8}, 794 }, 795 b: [][]float64{ 796 {10, 11, 12}, 797 {13, 14, 15}, 798 }, 799 want: [][]float64{ 800 {30, -57, 258}, 801 {39, -75, 339}, 802 }, 803 }, 804 { 805 s: blas.Right, 806 ul: blas.Lower, 807 tA: blas.Trans, 808 d: blas.Unit, 809 m: 2, 810 n: 3, 811 alpha: 0, 812 a: [][]float64{ 813 {2, 0, 0}, 814 {3, 5, 0}, 815 {4, 6, 8}, 816 }, 817 b: [][]float64{ 818 {10, 11, 12}, 819 {13, 14, 15}, 820 }, 821 want: [][]float64{ 822 {0, 0, 0}, 823 {0, 0, 0}, 824 }, 825 }, 826 } { 827 m := test.m 828 n := test.n 829 na := m 830 if test.s == blas.Right { 831 na = n 832 } 833 for _, lda := range []int{na, na + 3} { 834 for _, ldb := range []int{n, n + 5} { 835 a := make([]float64, na*lda) 836 for i := range a { 837 a[i] = rnd.NormFloat64() 838 } 839 for i := 0; i < na; i++ { 840 for j := 0; j < na; j++ { 841 a[i*lda+j] = test.a[i][j] 842 } 843 } 844 845 b := make([]float64, m*ldb) 846 for i := range b { 847 b[i] = rnd.NormFloat64() 848 } 849 for i := 0; i < m; i++ { 850 for j := 0; j < n; j++ { 851 b[i*ldb+j] = test.b[i][j] 852 } 853 } 854 855 impl.Dtrsm(test.s, test.ul, test.tA, test.d, test.m, test.n, test.alpha, a, lda, b, ldb) 856 857 want := make([]float64, len(b)) 858 copy(want, b) 859 for i := 0; i < m; i++ { 860 for j := 0; j < n; j++ { 861 want[i*ldb+j] = test.want[i][j] 862 } 863 } 864 if !floats.EqualApprox(want, b, 1e-13) { 865 t.Errorf("Case %v: Want %v, got %v.", i, want, b) 866 } 867 } 868 } 869 } 870 }