Chai Xiangyu | Research, Develop, Projects, Blogs Algorithm Armadillo使用介绍(十):运算符

Armadillo使用介绍(十):运算符

+   −   *   /   %   ==   !=   <=   >=   <   >

类型:运算符

  • Overloaded operators for Mat, Col, Row and Cube classes
  • Meanings:
    + Addition of two objects
    Subtraction of one object from another or negation of an object
    / Element-wise division of an object by another object or a scalar
    * Matrix multiplication of two objects; not applicable to the Cube class unless multiplying a cube by a scalar
    % Schur product: element-wise multiplication of two objects
    == Element-wise equality evaluation of two objects; generates a matrix of type umat with entries that indicate whether at a given position the two elements from the two objects are equal (1) or not equal (0)
    != Element-wise non-equality evaluation of two objects
    >= As for ==, but the check is for “greater than or equal to”
    <= As for ==, but the check is for “less than or equal to”
    > As for ==, but the check is for “greater than”
    < As for ==, but the check is for “less than”
  • Caveat: operators involving an equality comparison (ie. ==, !=, >=, <=) are not recommended for matrices of type mat or fmat, due to the necessarily limited precision of the underlying element types; consider using approx_equal() instead
  • A std::logic_error exception is thrown if incompatible object sizes are used
  • If the +, and % operators are chained, Armadillo will try to avoid the generation of temporaries; no temporaries are generated if all given objects are of the same type and size
  • If the * operator is chained, Armadillo will try to find an efficient ordering of the matrix multiplications

示例代码如下:

mat A = randu<mat>(5,10);
mat B = randu<mat>(5,10);
mat C = randu<mat>(10,5);

mat P = A + B;
mat Q = A - B;
mat R = -B;
mat S = A / 123.0;
mat T = A % B;
mat U = A * C;

// V is constructed without temporaries
mat V = A + B + A + B;

imat AA = "1 2 3; 4 5 6; 7 8 9;";
imat BB = "3 2 1; 6 5 4; 9 8 7;";

// compare elements
umat ZZ = (AA >= BB);

运行结果:

cpp Armadillo version: 9.900.1 (Nocturnal Misbehaviour) A: 0.0013 0.1741 0.9885 0.1662 0.8760 0.7797 0.3759 0.8376 0.5990 0.5171 0.1933 0.7105 0.1191 0.4508 0.9559 0.9968 0.6772 0.4849 0.7350 0.7515 0.5850 0.3040 0.0089 0.0571 0.5393 0.6115 0.0088 0.7437 0.5724 0.1690 0.3503 0.0914 0.5317 0.7833 0.4621 0.2662 0.2759 0.4580 0.1516 0.4919 0.8228 0.1473 0.6018 0.5199 0.8622 0.8401 0.5879 0.7444 0.4252 0.6998 B: 0.1475 0.1533 0.7320 0.8347 0.4717 0.1960 0.5313 0.3141 0.2527 0.1138 0.1416 0.8217 0.2796 0.5170 0.8470 0.8394 0.8430 0.2861 0.0016 0.7522 0.6929 0.1914 0.6822 0.4262 0.4561 0.5009 0.6576 0.1403 0.8062 0.5434 0.4265 0.8172 0.7219 0.9493 0.9829 0.0275 0.8421 0.8346 0.2106 0.4367 0.9666 0.1556 0.1230 0.5495 0.7392 0.5726 0.1099 0.6002 0.5532 0.6962 C: 0.4367 0.9315 0.1843 0.9115 0.5451 0.5779 0.8946 0.5551 0.7277 0.4090 0.6287 0.2273 0.2429 0.6678 0.4656 0.5041 0.4107 0.6047 0.3150 0.1527 0.6958 0.6281 0.5846 0.3058 0.7380 0.1900 0.4516 0.4944 0.1086 0.8267 0.1784 0.5978 0.7407 0.8512 0.8733 0.4575 0.8548 0.6204 0.1549 0.3001 0.0975 0.6248 0.8045 0.0793 0.1272 0.0944 0.5657 0.5760 0.6410 0.7850 P = A + B 0.1488 0.3274 1.7205 1.0009 1.3477 0.9757 0.9072 1.1517 0.8518 0.6309 0.3349 1.5322 0.3987 0.9678 1.8029 1.8362 1.5202 0.7710 0.7366 1.5038 1.2779 0.4953 0.6912 0.4833 0.9954 1.1124 0.6664 0.8840 1.3786 0.7124 0.7769 0.9086 1.2536 1.7327 1.4450 0.2937 1.1180 1.2926 0.3621 0.9286 1.7894 0.3029 0.7248 1.0694 1.6014 1.4127 0.6979 1.3447 0.9784 1.3960 Q = A - B -0.1463 0.0209 0.2565 -0.6684 0.4042 0.5837 -0.1555 0.5235 0.3463 0.4033 0.0517 -0.1112 -0.1605 -0.0662 0.1089 0.1574 -0.1658 0.1988 0.7334 -0.0007 -0.1079 0.1126 -0.6733 -0.3692 0.0832 0.1106 -0.6488 0.6034 -0.2338 -0.3744 -0.0762 -0.7258 -0.1902 -0.1660 -0.5209 0.2387 -0.5662 -0.3766 -0.0590 0.0552 -0.1438 -0.0083 0.4787 -0.0297 0.1230 0.2675 0.4780 0.1442 -0.1280 0.0035 R = -B -0.1475 -0.1533 -0.7320 -0.8347 -0.4717 -0.1960 -0.5313 -0.3141 -0.2527 -0.1138 -0.1416 -0.8217 -0.2796 -0.5170 -0.8470 -0.8394 -0.8430 -0.2861 -0.0016 -0.7522 -0.6929 -0.1914 -0.6822 -0.4262 -0.4561 -0.5009 -0.6576 -0.1403 -0.8062 -0.5434 -0.4265 -0.8172 -0.7219 -0.9493 -0.9829 -0.0275 -0.8421 -0.8346 -0.2106 -0.4367 -0.9666 -0.1556 -0.1230 -0.5495 -0.7392 -0.5726 -0.1099 -0.6002 -0.5532 -0.6962 S = A / 123.0 1.0312e-005 1.4157e-003 8.0367e-003 1.3516e-003 7.1217e-003 6.3387e-003 3.0558e-003 6.8098e-003 4.8703e-003 4.2042e-003 1.5717e-003 5.7764e-003 9.6813e-004 3.6649e-003 7.7715e-003 8.1040e-003 5.5056e-003 3.9425e-003 5.9756e-003 6.1101e-003 4.7561e-003 2.4714e-003 7.2542e-005 4.6387e-004 4.3849e-003 4.9715e-003 7.1684e-005 6.0466e-003 4.6536e-003 1.3740e-003 2.8480e-003 7.4318e-004 4.3225e-003 6.3684e-003 3.7567e-003 2.1643e-003 2.2430e-003 3.7234e-003 1.2322e-003 3.9991e-003 6.6897e-003 1.1977e-003 4.8924e-003 4.2266e-003 7.0099e-003 6.8302e-003 4.7798e-003 6.0522e-003 3.4566e-003 5.6890e-003 T = A % B 0.0002 0.0267 0.7236 0.1388 0.4132 0.1528 0.1997 0.2631 0.1514 0.0588 0.0274 0.5838 0.0333 0.2331 0.8096 0.8367 0.5709 0.1387 0.0012 0.5653 0.4053 0.0582 0.0061 0.0243 0.2460 0.3063 0.0058 0.1043 0.4615 0.0918 0.1494 0.0747 0.3838 0.7436 0.4542 0.0073 0.2323 0.3822 0.0319 0.2148 0.7954 0.0229 0.0740 0.2857 0.6373 0.4810 0.0646 0.4468 0.2352 0.4872 U = A * C 2.1215 2.9597 2.9130 2.0216 2.9102 2.1368 3.7822 3.6100 2.5069 3.4701 1.3705 2.5515 1.9567 1.2862 1.7960 1.6269 2.1905 2.0136 1.7909 1.9403 2.3973 3.8188 3.2559 2.8758 3.5393 V = A + B + A + B 0.2976 0.6548 3.4410 2.0018 2.6954 1.9513 1.8143 2.3034 1.7035 1.2618 0.6698 3.0643 0.7973 1.9356 3.6057 3.6724 3.0404 1.5420 1.4732 3.0075 2.5558 0.9907 1.3823 0.9665 1.9909 2.2248 1.3328 1.7680 2.7573 1.4248 1.5537 1.8172 2.5071 3.4653 2.8900 0.5874 2.2360 2.5852 0.7243 1.8572 3.5789 0.6058 1.4496 2.1388 3.2028 2.8254 1.3957 2.6893 1.9567 2.7919 AA: 1 2 3 4 5 6 7 8 9 BB: 3 2 1 6 5 4 9 8 7 ZZ = (AA >= BB) 0 1 1 0 1 1 0 1 1 请按任意键继续. . .

Leave a Reply

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Related Post