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 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
|
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
from ./octave.texi on 18 June 1999 -->
<TITLE>GNU Octave - Expressions</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_8.html">previous</A>, <A HREF="octave_10.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
<P><HR><P>
<H1><A NAME="SEC64" HREF="octave_toc.html#TOC64">Expressions</A></H1>
<P>
<A NAME="IDX292"></A>
</P>
<P>
Expressions are the basic building block of statements in Octave. An
expression evaluates to a value, which you can print, test, store in a
variable, pass to a function, or assign a new value to a variable with
an assignment operator.
</P>
<P>
An expression can serve as a statement on its own. Most other kinds of
statements contain one or more expressions which specify data to be
operated on. As in other languages, expressions in Octave include
variables, array references, constants, and function calls, as well as
combinations of these with various operators.
</P>
<H2><A NAME="SEC65" HREF="octave_toc.html#TOC65">Index Expressions</A></H2>
<P>
<A NAME="IDX293"></A>
<A NAME="IDX294"></A>
</P>
<P>
An <STRONG>index expression</STRONG> allows you to reference or extract selected
elements of a matrix or vector.
</P>
<P>
Indices may be scalars, vectors, ranges, or the special operator
<SAMP>`:'</SAMP>, which may be used to select entire rows or columns.
</P>
<P>
Vectors are indexed using a single expression. Matrices require two
indices unless the value of the built-in variable
<CODE>do_fortran_indexing</CODE> is nonzero, in which case matrices may
also be indexed by a single expression.
</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>do_fortran_indexing</B>
<DD><A NAME="IDX295"></A>
If the value of <CODE>do_fortran_indexing</CODE> is nonzero, Octave allows
you to select elements of a two-dimensional matrix using a single index
by treating the matrix as a single vector created from the columns of
the matrix. The default value is 0.
</DL>
</P>
<P>
Given the matrix
</P>
<PRE>
a = [1, 2; 3, 4]
</PRE>
<P>
all of the following expressions are equivalent
</P>
<PRE>
a (1, [1, 2])
a (1, 1:2)
a (1, :)
</PRE>
<P>
and select the first row of the matrix.
</P>
<P>
A special form of indexing may be used to select elements of a matrix or
vector. If the indices are vectors made up of only ones and zeros, the
result is a new matrix whose elements correspond to the elements of the
index vector that are equal to one. For example,
</P>
<PRE>
a = [1, 2; 3, 4];
a ([1, 0], :)
</PRE>
<P>
selects the first row of the matrix <CODE>a</CODE>.
</P>
<P>
This operation can be useful for selecting elements of a matrix based on
some condition, since the comparison operators return matrices of ones
and zeros.
</P>
<P>
This special zero-one form of indexing leads to a conflict with the
standard indexing operation. For example, should the following
statements
</P>
<PRE>
a = [1, 2; 3, 4];
a ([1, 1], :)
</PRE>
<P>
return the original matrix, or the matrix formed by selecting the first
row twice? Although this conflict is not likely to arise very often in
practice, you may select the behavior you prefer by setting the built-in
variable <CODE>prefer_zero_one_indexing</CODE>.
</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>prefer_zero_one_indexing</B>
<DD><A NAME="IDX296"></A>
If the value of <CODE>prefer_zero_one_indexing</CODE> is nonzero, Octave
will perform zero-one style indexing when there is a conflict with the
normal indexing rules. See section <A HREF="octave_9.html#SEC65">Index Expressions</A>. For example, given a
matrix
</P>
<PRE>
a = [1, 2, 3, 4]
</PRE>
<P>
with <CODE>prefer_zero_one_indexing</CODE> is set to nonzero, the
expression
</P>
<PRE>
a ([1, 1, 1, 1])
</PRE>
<P>
results in the matrix <CODE>[ 1, 2, 3, 4 ]</CODE>. If the value of
<CODE>prefer_zero_one_indexing</CODE> set to 0, the result would be
the matrix <CODE>[ 1, 1, 1, 1 ]</CODE>.
</P>
<P>
In the first case, Octave is selecting each element corresponding to a
<SAMP>`1'</SAMP> in the index vector. In the second, Octave is selecting the
first element multiple times.
</P>
<P>
The default value for <CODE>prefer_zero_one_indexing</CODE> is 0.
</DL>
</P>
<P>
Finally, indexing a scalar with a vector of ones can be used to create a
vector the same size as the index vector, with each element equal to
the value of the original scalar. For example, the following statements
</P>
<PRE>
a = 13;
a ([1, 1, 1, 1])
</PRE>
<P>
produce a vector whose four elements are all equal to 13.
</P>
<P>
Similarly, indexing a scalar with two vectors of ones can be used to
create a matrix. For example the following statements
</P>
<PRE>
a = 13;
a ([1, 1], [1, 1, 1])
</PRE>
<P>
create a 2 by 3 matrix with all elements equal to 13.
</P>
<P>
This is an obscure notation and should be avoided. It is better to
use the function <CODE>ones</CODE> to generate a matrix of the appropriate
size whose elements are all one, and then to scale it to produce the
desired result. See section <A HREF="octave_16.html#SEC134">Special Utility Matrices</A>.
</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>prefer_column_vectors</B>
<DD><A NAME="IDX297"></A>
If <CODE>prefer_column_vectors</CODE> is nonzero, operations like
</P>
<PRE>
for i = 1:10
a (i) = i;
endfor
</PRE>
<P>
(for <CODE>a</CODE> previously undefined) produce column vectors. Otherwise, row
vectors are preferred. The default value is 1.
</P>
<P>
If a variable is already defined to be a vector (a matrix with a single
row or column), the original orientation is respected, regardless of the
value of <CODE>prefer_column_vectors</CODE>.
</DL>
</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>resize_on_range_error</B>
<DD><A NAME="IDX298"></A>
If the value of <CODE>resize_on_range_error</CODE> is nonzero, expressions
like
</P>
<PRE>
for i = 1:10
a (i) = sqrt (i);
endfor
</PRE>
<P>
(for <CODE>a</CODE> previously undefined) result in the variable <CODE>a</CODE>
being resized to be just large enough to hold the new value. New
elements that have not been given a value are set to zero. If the value
of <CODE>resize_on_range_error</CODE> is 0, an error message is printed and
control is returned to the top level. The default value is 1.
</DL>
</P>
<P>
Note that it is quite inefficient to create a vector using a loop like
the one shown in the example above. In this particular case, it would
have been much more efficient to use the expression
</P>
<PRE>
a = sqrt (1:10);
</PRE>
<P>
thus avoiding the loop entirely. In cases where a loop is still
required, or a number of values must be combined to form a larger
matrix, it is generally much faster to set the size of the matrix first,
and then insert elements using indexing commands. For example, given a
matrix <CODE>a</CODE>,
</P>
<PRE>
[nr, nc] = size (a);
x = zeros (nr, n * nc);
for i = 1:n
x(:,(i-1)*n+1:i*n) = a;
endfor
</PRE>
<P>
is considerably faster than
</P>
<PRE>
x = a;
for i = 1:n-1
x = [x, a];
endfor
</PRE>
<P>
particularly for large matrices because Octave does not have to
repeatedly resize the result.
</P>
<H2><A NAME="SEC66" HREF="octave_toc.html#TOC66">Calling Functions</A></H2>
<P>
A <STRONG>function</STRONG> is a name for a particular calculation. Because it has
a name, you can ask for it by name at any point in the program. For
example, the function <CODE>sqrt</CODE> computes the square root of a number.
</P>
<P>
A fixed set of functions are <STRONG>built-in</STRONG>, which means they are
available in every Octave program. The <CODE>sqrt</CODE> function is one of
these. In addition, you can define your own functions.
See section <A HREF="octave_12.html#SEC89">Functions and Script Files</A>, for information about how to do this.
</P>
<P>
<A NAME="IDX299"></A>
The way to use a function is with a <STRONG>function call</STRONG> expression,
which consists of the function name followed by a list of
<STRONG>arguments</STRONG> in parentheses. The arguments are expressions which give
the raw materials for the calculation that the function will do. When
there is more than one argument, they are separated by commas. If there
are no arguments, you can omit the parentheses, but it is a good idea to
include them anyway, to clearly indicate that a function call was
intended. Here are some examples:
</P>
<PRE>
sqrt (x^2 + y^2) # One argument
ones (n, m) # Two arguments
rand () # No arguments
</PRE>
<P>
Each function expects a particular number of arguments. For example, the
<CODE>sqrt</CODE> function must be called with a single argument, the number
to take the square root of:
</P>
<PRE>
sqrt (<VAR>argument</VAR>)
</PRE>
<P>
Some of the built-in functions take a variable number of arguments,
depending on the particular usage, and their behavior is different
depending on the number of arguments supplied.
</P>
<P>
Like every other expression, the function call has a value, which is
computed by the function based on the arguments you give it. In this
example, the value of <CODE>sqrt (<VAR>argument</VAR>)</CODE> is the square root of
the argument. A function can also have side effects, such as assigning
the values of certain variables or doing input or output operations.
</P>
<P>
Unlike most languages, functions in Octave may return multiple values.
For example, the following statement
</P>
<PRE>
[u, s, v] = svd (a)
</PRE>
<P>
computes the singular value decomposition of the matrix <CODE>a</CODE> and
assigns the three result matrices to <CODE>u</CODE>, <CODE>s</CODE>, and <CODE>v</CODE>.
</P>
<P>
The left side of a multiple assignment expression is itself a list of
expressions, and is allowed to be a list of variable names or index
expressions. See also section <A HREF="octave_9.html#SEC65">Index Expressions</A>, and section <A HREF="octave_9.html#SEC74">Assignment Expressions</A>.
</P>
<H3><A NAME="SEC67" HREF="octave_toc.html#TOC67">Call by Value</A></H3>
<P>
In Octave, unlike Fortran, function arguments are passed by value, which
means that each argument in a function call is evaluated and assigned to
a temporary location in memory before being passed to the function.
There is currently no way to specify that a function parameter should be
passed by reference instead of by value. This means that it is
impossible to directly alter the value of function parameter in the
calling function. It can only change the local copy within the function
body. For example, the function
</P>
<PRE>
function f (x, n)
while (n-- > 0)
disp (x);
endwhile
endfunction
</PRE>
<P>
displays the value of the first argument <VAR>n</VAR> times. In this
function, the variable <VAR>n</VAR> is used as a temporary variable without
having to worry that its value might also change in the calling
function. Call by value is also useful because it is always possible to
pass constants for any function parameter without first having to
determine that the function will not attempt to modify the parameter.
</P>
<P>
The caller may use a variable as the expression for the argument, but
the called function does not know this: it only knows what value the
argument had. For example, given a function called as
</P>
<PRE>
foo = "bar";
fcn (foo)
</PRE>
<P>
you should not think of the argument as being "the variable
<CODE>foo</CODE>." Instead, think of the argument as the string value,
<CODE>"bar"</CODE>.
</P>
<P>
Even though Octave uses pass-by-value semantics for function arguments,
values are not copied unnecessarily. For example,
</P>
<PRE>
x = rand (1000);
f (x);
</PRE>
<P>
does not actually force two 1000 by 1000 element matrices to exist
<EM>unless</EM> the function <CODE>f</CODE> modifies the value of its
argument. Then Octave must create a copy to avoid changing the
value outside the scope of the function <CODE>f</CODE>, or attempting (and
probably failing!) to modify the value of a constant or the value of a
temporary result.
</P>
<H3><A NAME="SEC68" HREF="octave_toc.html#TOC68">Recursion</A></H3>
<P>
<A NAME="IDX300"></A>
</P>
<P>
With some restrictions<A NAME="DOCF2" HREF="octave_foot.html#FOOT2">(2)</A>, recursive function calls are allowed. A
<STRONG>recursive function</STRONG> is one which calls itself, either directly or
indirectly. For example, here is an inefficient<A NAME="DOCF3" HREF="octave_foot.html#FOOT3">(3)</A> way to compute the factorial of a given integer:
</P>
<PRE>
function retval = fact (n)
if (n > 0)
retval = n * fact (n-1);
else
retval = 1;
endif
endfunction
</PRE>
<P>
This function is recursive because it calls itself directly. It
eventually terminates because each time it calls itself, it uses an
argument that is one less than was used for the previous call. Once the
argument is no longer greater than zero, it does not call itself, and
the recursion ends.
</P>
<P>
The built-in variable <CODE>max_recursion_depth</CODE> specifies a limit to
the recursion depth and prevents Octave from recursing infinitely.
</P>
<P>
<DL>
<DT><U>max_recursion_depth:</U> <B></B>
<DD><A NAME="IDX301"></A>
Limit the number of times a function may be called recursively.
If the limit is exceeded, an error message is printed and control
returns to the top level.
</P>
<P>
The default value is 256.
</DL>
</P>
<H2><A NAME="SEC69" HREF="octave_toc.html#TOC69">Arithmetic Operators</A></H2>
<P>
<A NAME="IDX302"></A>
<A NAME="IDX303"></A>
<A NAME="IDX304"></A>
<A NAME="IDX305"></A>
<A NAME="IDX306"></A>
<A NAME="IDX307"></A>
<A NAME="IDX308"></A>
<A NAME="IDX309"></A>
<A NAME="IDX310"></A>
<A NAME="IDX311"></A>
<A NAME="IDX312"></A>
<A NAME="IDX313"></A>
<A NAME="IDX314"></A>
<A NAME="IDX315"></A>
<A NAME="IDX316"></A>
</P>
<P>
The following arithmetic operators are available, and work on scalars
and matrices.
</P>
<DL COMPACT>
<DT><CODE><VAR>x</VAR> + <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX317"></A>
Addition. If both operands are matrices, the number of rows and columns
must both agree. If one operand is a scalar, its value is added to
all the elements of the other operand.
<DT><CODE><VAR>x</VAR> .+ <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX318"></A>
Element by element addition. This operator is equivalent to <CODE>+</CODE>.
<DT><CODE><VAR>x</VAR> - <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX319"></A>
Subtraction. If both operands are matrices, the number of rows and
columns of both must agree.
<DT><CODE><VAR>x</VAR> .- <VAR>y</VAR></CODE>
<DD>
Element by element subtraction. This operator is equivalent to <CODE>-</CODE>.
<DT><CODE><VAR>x</VAR> * <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX320"></A>
Matrix multiplication. The number of columns of <VAR>x</VAR> must agree
with the number of rows of <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> .* <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX321"></A>
Element by element multiplication. If both operands are matrices, the
number of rows and columns must both agree.
<DT><CODE><VAR>x</VAR> / <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX322"></A>
Right division. This is conceptually equivalent to the expression
<PRE>
(inverse (y') * x')'
</PRE>
but it is computed without forming the inverse of <VAR>y'</VAR>.
If the system is not square, or if the coefficient matrix is singular,
a minimum norm solution is computed.
<DT><CODE><VAR>x</VAR> ./ <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX323"></A>
Element by element right division.
<DT><CODE><VAR>x</VAR> \ <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX324"></A>
Left division. This is conceptually equivalent to the expression
<PRE>
inverse (x) * y
</PRE>
but it is computed without forming the inverse of <VAR>x</VAR>.
If the system is not square, or if the coefficient matrix is singular,
a minimum norm solution is computed.
<DT><CODE><VAR>x</VAR> .\ <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX325"></A>
Element by element left division. Each element of <VAR>y</VAR> is divided
by each corresponding element of <VAR>x</VAR>.
<DT><CODE><VAR>x</VAR> ^ <VAR>y</VAR></CODE>
<DD>
<DT><CODE><VAR>x</VAR> ** <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX326"></A>
<A NAME="IDX327"></A>
Power operator. If <VAR>x</VAR> and <VAR>y</VAR> are both scalars, this operator
returns <VAR>x</VAR> raised to the power <VAR>y</VAR>. If <VAR>x</VAR> is a scalar and
<VAR>y</VAR> is a square matrix, the result is computed using an eigenvalue
expansion. If <VAR>x</VAR> is a square matrix. the result is computed by
repeated multiplication if <VAR>y</VAR> is an integer, and by an eigenvalue
expansion if <VAR>y</VAR> is not an integer. An error results if both
<VAR>x</VAR> and <VAR>y</VAR> are matrices.
The implementation of this operator needs to be improved.
<DT><CODE><VAR>x</VAR> .^ <VAR>y</VAR></CODE>
<DD>
<DT><CODE><VAR>x</VAR> .** <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX328"></A>
<A NAME="IDX329"></A>
Element by element power operator. If both operands are matrices, the
number of rows and columns must both agree.
<DT><CODE>-<VAR>x</VAR></CODE>
<DD>
<A NAME="IDX330"></A>
Negation.
<DT><CODE>+<VAR>x</VAR></CODE>
<DD>
<A NAME="IDX331"></A>
Unary plus. This operator has no effect on the operand.
<DT><CODE><VAR>x</VAR>'</CODE>
<DD>
<A NAME="IDX332"></A>
Complex conjugate transpose. For real arguments, this operator is the
same as the transpose operator. For complex arguments, this operator is
equivalent to the expression
<PRE>
conj (x.')
</PRE>
<DT><CODE><VAR>x</VAR>.'</CODE>
<DD>
<A NAME="IDX333"></A>
Transpose.
</DL>
<P>
Note that because Octave's element by element operators begin with a
<SAMP>`.'</SAMP>, there is a possible ambiguity for statements like
</P>
<PRE>
1./m
</PRE>
<P>
because the period could be interpreted either as part of the constant
or as part of the operator. To resolve this conflict, Octave treats the
expression as if you had typed
</P>
<PRE>
(1) ./ m
</PRE>
<P>
and not
</P>
<PRE>
(1.) / m
</PRE>
<P>
Although this is inconsistent with the normal behavior of Octave's
lexer, which usually prefers to break the input into tokens by
preferring the longest possible match at any given point, it is more
useful in this case.
</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>warn_divide_by_zero</B>
<DD><A NAME="IDX334"></A>
If the value of <CODE>warn_divide_by_zero</CODE> is nonzero, a warning
is issued when Octave encounters a division by zero. If the value is
0, the warning is omitted. The default value is 1.
</DL>
</P>
<H2><A NAME="SEC70" HREF="octave_toc.html#TOC70">Comparison Operators</A></H2>
<P>
<A NAME="IDX335"></A>
<A NAME="IDX336"></A>
<A NAME="IDX337"></A>
<A NAME="IDX338"></A>
<A NAME="IDX339"></A>
<A NAME="IDX340"></A>
<A NAME="IDX341"></A>
<A NAME="IDX342"></A>
<A NAME="IDX343"></A>
</P>
<P>
<STRONG>Comparison operators</STRONG> compare numeric values for relationships
such as equality. They are written using
<EM>relational operators</EM>.
</P>
<P>
All of Octave's comparison operators return a value of 1 if the
comparison is true, or 0 if it is false. For matrix values, they all
work on an element-by-element basis. For example,
</P>
<PRE>
[1, 2; 3, 4] == [1, 3; 2, 4]
=> 1 0
0 1
</PRE>
<P>
If one operand is a scalar and the other is a matrix, the scalar is
compared to each element of the matrix in turn, and the result is the
same size as the matrix.
</P>
<DL COMPACT>
<DT><CODE><VAR>x</VAR> < <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX344"></A>
True if <VAR>x</VAR> is less than <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> <= <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX345"></A>
True if <VAR>x</VAR> is less than or equal to <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> == <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX346"></A>
True if <VAR>x</VAR> is equal to <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> >= <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX347"></A>
True if <VAR>x</VAR> is greater than or equal to <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> > <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX348"></A>
True if <VAR>x</VAR> is greater than <VAR>y</VAR>.
<DT><CODE><VAR>x</VAR> != <VAR>y</VAR></CODE>
<DD>
<DT><CODE><VAR>x</VAR> ~= <VAR>y</VAR></CODE>
<DD>
<DT><CODE><VAR>x</VAR> <> <VAR>y</VAR></CODE>
<DD>
<A NAME="IDX349"></A>
<A NAME="IDX350"></A>
<A NAME="IDX351"></A>
True if <VAR>x</VAR> is not equal to <VAR>y</VAR>.
</DL>
<P>
String comparisons may also be performed with the <CODE>strcmp</CODE>
function, not with the comparison operators listed above.
See section <A HREF="octave_6.html#SEC53">Strings</A>.
</P>
<H2><A NAME="SEC71" HREF="octave_toc.html#TOC71">Boolean Expressions</A></H2>
<P>
<A NAME="IDX352"></A>
<A NAME="IDX353"></A>
<A NAME="IDX354"></A>
<A NAME="IDX355"></A>
<A NAME="IDX356"></A>
<A NAME="IDX357"></A>
<A NAME="IDX358"></A>
<A NAME="IDX359"></A>
<A NAME="IDX360"></A>
<A NAME="IDX361"></A>
<A NAME="IDX362"></A>
</P>
<H3><A NAME="SEC72" HREF="octave_toc.html#TOC72">Element-by-element Boolean Operators</A></H3>
<P>
<A NAME="IDX363"></A>
</P>
<P>
An <STRONG>element-by-element boolean expression</STRONG> is a combination of
comparison expressions using the boolean
operators "or" (<SAMP>`|'</SAMP>), "and" (<SAMP>`&'</SAMP>), and "not" (<SAMP>`!'</SAMP>),
along with parentheses to control nesting. The truth of the boolean
expression is computed by combining the truth values of the
corresponding elements of the component expressions. A value is
considered to be false if it is zero, and true otherwise.
</P>
<P>
Element-by-element boolean expressions can be used wherever comparison
expressions can be used. They can be used in <CODE>if</CODE> and <CODE>while</CODE>
statements. However, if a matrix value used as the condition in an
<CODE>if</CODE> or <CODE>while</CODE> statement is only true if <EM>all</EM> of its
elements are nonzero.
</P>
<P>
Like comparison operations, each element of an element-by-element
boolean expression also has a numeric value (1 if true, 0 if false) that
comes into play if the result of the boolean expression is stored in a
variable, or used in arithmetic.
</P>
<P>
Here are descriptions of the three element-by-element boolean operators.
</P>
<DL COMPACT>
<DT><CODE><VAR>boolean1</VAR> & <VAR>boolean2</VAR></CODE>
<DD>
<A NAME="IDX364"></A>
Elements of the result are true if both corresponding elements of
<VAR>boolean1</VAR> and <VAR>boolean2</VAR> are true.
<DT><CODE><VAR>boolean1</VAR> | <VAR>boolean2</VAR></CODE>
<DD>
<A NAME="IDX365"></A>
Elements of the result are true if either of the corresponding elements
of <VAR>boolean1</VAR> or <VAR>boolean2</VAR> is true.
<DT><CODE>! <VAR>boolean</VAR></CODE>
<DD>
<DT><CODE>~ <VAR>boolean</VAR></CODE>
<DD>
<A NAME="IDX366"></A>
<A NAME="IDX367"></A>
Each element of the result is true if the corresponding element of
<VAR>boolean</VAR> is false.
</DL>
<P>
For matrix operands, these operators work on an element-by-element
basis. For example, the expression
</P>
<PRE>
[1, 0; 0, 1] & [1, 0; 2, 3]
</PRE>
<P>
returns a two by two identity matrix.
</P>
<P>
For the binary operators, the dimensions of the operands must conform if
both are matrices. If one of the operands is a scalar and the other a
matrix, the operator is applied to the scalar and each element of the
matrix.
</P>
<P>
For the binary element-by-element boolean operators, both subexpressions
<VAR>boolean1</VAR> and <VAR>boolean2</VAR> are evaluated before computing the
result. This can make a difference when the expressions have side
effects. For example, in the expression
</P>
<PRE>
a & b++
</PRE>
<P>
the value of the variable <VAR>b</VAR> is incremented even if the variable
<VAR>a</VAR> is zero.
</P>
<P>
This behavior is necessary for the boolean operators to work as
described for matrix-valued operands.
</P>
<H3><A NAME="SEC73" HREF="octave_toc.html#TOC73">Short-circuit Boolean Operators</A></H3>
<P>
<A NAME="IDX368"></A>
</P>
<P>
Combined with the implicit conversion to scalar values in <CODE>if</CODE> and
<CODE>while</CODE> conditions, Octave's element-by-element boolean operators
are often sufficient for performing most logical operations. However,
it is sometimes desirable to stop evaluating a boolean expression as
soon as the overall truth value can be determined. Octave's
<STRONG>short-circuit</STRONG> boolean operators work this way.
</P>
<DL COMPACT>
<DT><CODE><VAR>boolean1</VAR> && <VAR>boolean2</VAR></CODE>
<DD>
<A NAME="IDX369"></A>
The expression <VAR>boolean1</VAR> is evaluated and converted to a scalar
using the equivalent of the operation <CODE>all (all (<VAR>boolean1</VAR>))</CODE>.
If it is false, the result of the overall expression is 0. If it is
true, the expression <VAR>boolean2</VAR> is evaluated and converted to a
scalar using the equivalent of the operation <CODE>all (all
(<VAR>boolean1</VAR>))</CODE>. If it is true, the result of the overall expression
is 1. Otherwise, the result of the overall expression is 0.
<DT><CODE><VAR>boolean1</VAR> || <VAR>boolean2</VAR></CODE>
<DD>
<A NAME="IDX370"></A>
The expression <VAR>boolean1</VAR> is evaluated and converted to a scalar
using the equivalent of the operation <CODE>all (all (<VAR>boolean1</VAR>))</CODE>.
If it is true, the result of the overall expression is 1. If it is
false, the expression <VAR>boolean2</VAR> is evaluated and converted to a
scalar using the equivalent of the operation <CODE>all (all
(<VAR>boolean1</VAR>))</CODE>. If it is true, the result of the overall expression
is 1. Otherwise, the result of the overall expression is 0.
</DL>
<P>
The fact that both operands may not be evaluated before determining the
overall truth value of the expression can be important. For example, in
the expression
</P>
<PRE>
a && b++
</PRE>
<P>
the value of the variable <VAR>b</VAR> is only incremented if the variable
<VAR>a</VAR> is nonzero.
</P>
<P>
This can be used to write somewhat more concise code. For example, it
is possible write
</P>
<PRE>
function f (a, b, c)
if (nargin > 2 && isstr (c))
...
</PRE>
<P>
instead of having to use two <CODE>if</CODE> statements to avoid attempting to
evaluate an argument that doesn't exist. For example, without the
short-circuit feature, it would be necessary to write
</P>
<PRE>
function f (a, b, c)
if (nargin > 2)
if (isstr (c))
...
</PRE>
<P>
Writing
</P>
<PRE>
function f (a, b, c)
if (nargin > 2 & isstr (c))
...
</PRE>
<P>
would result in an error if <CODE>f</CODE> were called with one or two
arguments because Octave would be forced to try to evaluate both of the
operands for the operator <SAMP>`&'</SAMP>.
</P>
<H2><A NAME="SEC74" HREF="octave_toc.html#TOC74">Assignment Expressions</A></H2>
<P>
<A NAME="IDX371"></A>
<A NAME="IDX372"></A>
<A NAME="IDX373"></A>
<A NAME="IDX374"></A>
</P>
<P>
<A NAME="IDX375"></A>
</P>
<P>
An <STRONG>assignment</STRONG> is an expression that stores a new value into a
variable. For example, the following expression assigns the value 1 to
the variable <CODE>z</CODE>:
</P>
<PRE>
z = 1
</PRE>
<P>
After this expression is executed, the variable <CODE>z</CODE> has the value 1.
Whatever old value <CODE>z</CODE> had before the assignment is forgotten.
The <SAMP>`='</SAMP> sign is called an <STRONG>assignment operator</STRONG>.
</P>
<P>
Assignments can store string values also. For example, the following
expression would store the value <CODE>"this food is good"</CODE> in the
variable <CODE>message</CODE>:
</P>
<PRE>
thing = "food"
predicate = "good"
message = [ "this " , thing , " is " , predicate ]
</PRE>
<P>
(This also illustrates concatenation of strings.)
</P>
<P>
<A NAME="IDX376"></A>
Most operators (addition, concatenation, and so on) have no effect
except to compute a value. If you ignore the value, you might as well
not use the operator. An assignment operator is different. It does
produce a value, but even if you ignore the value, the assignment still
makes itself felt through the alteration of the variable. We call this
a <STRONG>side effect</STRONG>.
</P>
<P>
<A NAME="IDX377"></A>
The left-hand operand of an assignment need not be a variable
(see section <A HREF="octave_8.html#SEC59">Variables</A>). It can also be an element of a matrix
(see section <A HREF="octave_9.html#SEC65">Index Expressions</A>) or a list of return values
(see section <A HREF="octave_9.html#SEC66">Calling Functions</A>). These are all called <STRONG>lvalues</STRONG>, which
means they can appear on the left-hand side of an assignment operator.
The right-hand operand may be any expression. It produces the new value
which the assignment stores in the specified variable, matrix element,
or list of return values.
</P>
<P>
It is important to note that variables do <EM>not</EM> have permanent types.
The type of a variable is simply the type of whatever value it happens
to hold at the moment. In the following program fragment, the variable
<CODE>foo</CODE> has a numeric value at first, and a string value later on:
</P>
<PRE>
octave:13> foo = 1
foo = 1
octave:13> foo = "bar"
foo = bar
</PRE>
<P>
When the second assignment gives <CODE>foo</CODE> a string value, the fact that
it previously had a numeric value is forgotten.
</P>
<P>
Assignment of a scalar to an indexed matrix sets all of the elements
that are referenced by the indices to the scalar value. For example, if
<CODE>a</CODE> is a matrix with at least two columns,
</P>
<PRE>
a(:, 2) = 5
</PRE>
<P>
sets all the elements in the second column of <CODE>a</CODE> to 5.
</P>
<P>
Assigning an empty matrix <SAMP>`[]'</SAMP> works in most cases to allow you to
delete rows or columns of matrices and vectors. See section <A HREF="octave_5.html#SEC50">Empty Matrices</A>.
For example, given a 4 by 5 matrix <VAR>A</VAR>, the assignment
</P>
<PRE>
A (3, :) = []
</PRE>
<P>
deletes the third row of <VAR>A</VAR>, and the assignment
</P>
<PRE>
A (:, 1:2:5) = []
</PRE>
<P>
deletes the first, third, and fifth columns.
</P>
<P>
An assignment is an expression, so it has a value. Thus, <CODE>z = 1</CODE>
as an expression has the value 1. One consequence of this is that you
can write multiple assignments together:
</P>
<PRE>
x = y = z = 0
</PRE>
<P>
stores the value 0 in all three variables. It does this because the
value of <CODE>z = 0</CODE>, which is 0, is stored into <CODE>y</CODE>, and then
the value of <CODE>y = z = 0</CODE>, which is 0, is stored into <CODE>x</CODE>.
</P>
<P>
This is also true of assignments to lists of values, so the following is
a valid expression
</P>
<PRE>
[a, b, c] = [u, s, v] = svd (a)
</PRE>
<P>
that is exactly equivalent to
</P>
<PRE>
[u, s, v] = svd (a)
a = u
b = s
c = v
</PRE>
<P>
In expressions like this, the number of values in each part of the
expression need not match. For example, the expression
</P>
<PRE>
[a, b, c, d] = [u, s, v] = svd (a)
</PRE>
<P>
is equivalent to the expression above, except that the value of the
variable <SAMP>`d'</SAMP> is left unchanged, and the expression
</P>
<PRE>
[a, b] = [u, s, v] = svd (a)
</PRE>
<P>
is equivalent to
</P>
<PRE>
[u, s, v] = svd (a)
a = u
b = s
</PRE>
<P>
You can use an assignment anywhere an expression is called for. For
example, it is valid to write <CODE>x != (y = 1)</CODE> to set <CODE>y</CODE> to 1
and then test whether <CODE>x</CODE> equals 1. But this style tends to make
programs hard to read. Except in a one-shot program, you should rewrite
it to get rid of such nesting of assignments. This is never very hard.
</P>
<P>
<A NAME="IDX378"></A>
<A NAME="IDX379"></A>
<A NAME="IDX380"></A>
<A NAME="IDX381"></A>
</P>
<H2><A NAME="SEC75" HREF="octave_toc.html#TOC75">Increment Operators</A></H2>
<P>
<EM>Increment operators</EM> increase or decrease the value of a variable
by 1. The operator to increment a variable is written as <SAMP>`++'</SAMP>. It
may be used to increment a variable either before or after taking its
value.
</P>
<P>
For example, to pre-increment the variable <VAR>x</VAR>, you would write
<CODE>++<VAR>x</VAR></CODE>. This would add one to <VAR>x</VAR> and then return the new
value of <VAR>x</VAR> as the result of the expression. It is exactly the
same as the expression <CODE><VAR>x</VAR> = <VAR>x</VAR> + 1</CODE>.
</P>
<P>
To post-increment a variable <VAR>x</VAR>, you would write <CODE><VAR>x</VAR>++</CODE>.
This adds one to the variable <VAR>x</VAR>, but returns the value that
<VAR>x</VAR> had prior to incrementing it. For example, if <VAR>x</VAR> is equal
to 2, the result of the expression <CODE><VAR>x</VAR>++</CODE> is 2, and the new
value of <VAR>x</VAR> is 3.
</P>
<P>
For matrix and vector arguments, the increment and decrement operators
work on each element of the operand.
</P>
<P>
Here is a list of all the increment and decrement expressions.
</P>
<DL COMPACT>
<DT><CODE>++<VAR>x</VAR></CODE>
<DD>
<A NAME="IDX382"></A>
This expression increments the variable <VAR>x</VAR>. The value of the
expression is the <EM>new</EM> value of <VAR>x</VAR>. It is equivalent to the
expression <CODE><VAR>x</VAR> = <VAR>x</VAR> + 1</CODE>.
<DT><CODE>--<VAR>x</VAR></CODE>
<DD>
<A NAME="IDX383"></A>
This expression decrements the variable <VAR>x</VAR>. The value of the
expression is the <EM>new</EM> value of <VAR>x</VAR>. It is equivalent to the
expression <CODE><VAR>x</VAR> = <VAR>x</VAR> - 1</CODE>.
<DT><CODE><VAR>x</VAR>++</CODE>
<DD>
<A NAME="IDX384"></A>
This expression causes the variable <VAR>x</VAR> to be incremented. The
value of the expression is the <EM>old</EM> value of <VAR>x</VAR>.
<DT><CODE><VAR>x</VAR>--</CODE>
<DD>
<A NAME="IDX385"></A>
This expression causes the variable <VAR>x</VAR> to be decremented. The
value of the expression is the <EM>old</EM> value of <VAR>x</VAR>.
</DL>
<P>
It is not currently possible to increment index expressions. For
example, you might expect that the expression <CODE><VAR>v</VAR>(4)++</CODE> would
increment the fourth element of the vector <VAR>v</VAR>, but instead it
results in a parse error. This problem may be fixed in a future
release of Octave.
</P>
<H2><A NAME="SEC76" HREF="octave_toc.html#TOC76">Operator Precedence</A></H2>
<P>
<A NAME="IDX386"></A>
</P>
<P>
<STRONG>Operator precedence</STRONG> determines how operators are grouped, when
different operators appear close by in one expression. For example,
<SAMP>`*'</SAMP> has higher precedence than <SAMP>`+'</SAMP>. Thus, the expression
<CODE>a + b * c</CODE> means to multiply <CODE>b</CODE> and <CODE>c</CODE>, and then add
<CODE>a</CODE> to the product (i.e., <CODE>a + (b * c)</CODE>).
</P>
<P>
You can overrule the precedence of the operators by using parentheses.
You can think of the precedence rules as saying where the parentheses
are assumed if you do not write parentheses yourself. In fact, it is
wise to use parentheses whenever you have an unusual combination of
operators, because other people who read the program may not remember
what the precedence is in this case. You might forget as well, and then
you too could make a mistake. Explicit parentheses will help prevent
any such mistake.
</P>
<P>
When operators of equal precedence are used together, the leftmost
operator groups first, except for the assignment and exponentiation
operators, which group in the opposite order. Thus, the expression
<CODE>a - b + c</CODE> groups as <CODE>(a - b) + c</CODE>, but the expression
<CODE>a = b = c</CODE> groups as <CODE>a = (b = c)</CODE>.
</P>
<P>
The precedence of prefix unary operators is important when another
operator follows the operand. For example, <CODE>-x^2</CODE> means
<CODE>-(x^2)</CODE>, because <SAMP>`-'</SAMP> has lower precedence than <SAMP>`^'</SAMP>.
</P>
<P>
Here is a table of the operators in Octave, in order of increasing
precedence.
</P>
<DL COMPACT>
<DT><CODE>statement separators</CODE>
<DD>
<SAMP>`;'</SAMP>, <SAMP>`,'</SAMP>.
<DT><CODE>assignment</CODE>
<DD>
<SAMP>`='</SAMP>. This operator groups right to left.
<DT><CODE>logical "or" and "and"</CODE>
<DD>
<SAMP>`||'</SAMP>, <SAMP>`&&'</SAMP>.
<DT><CODE>element-wise "or" and "and"</CODE>
<DD>
<SAMP>`|'</SAMP>, <SAMP>`&'</SAMP>.
<DT><CODE>relational</CODE>
<DD>
<SAMP>`<'</SAMP>, <SAMP>`<='</SAMP>, <SAMP>`=='</SAMP>, <SAMP>`>='</SAMP>, <SAMP>`>'</SAMP>, <SAMP>`!='</SAMP>,
<SAMP>`~='</SAMP>, <SAMP>`<>'</SAMP>.
<DT><CODE>colon</CODE>
<DD>
<SAMP>`:'</SAMP>.
<DT><CODE>add, subtract</CODE>
<DD>
<SAMP>`+'</SAMP>, <SAMP>`-'</SAMP>.
<DT><CODE>multiply, divide</CODE>
<DD>
<SAMP>`*'</SAMP>, <SAMP>`/'</SAMP>, <SAMP>`\'</SAMP>, <SAMP>`.\'</SAMP>, <SAMP>`.*'</SAMP>, <SAMP>`./'</SAMP>.
<DT><CODE>transpose</CODE>
<DD>
<SAMP>`''</SAMP>, <SAMP>`.''</SAMP>
<DT><CODE>unary plus, minus, increment, decrement, and "not"</CODE>
<DD>
<SAMP>`+'</SAMP>, <SAMP>`-'</SAMP>, <SAMP>`++'</SAMP>, <SAMP>`--'</SAMP>, <SAMP>`!'</SAMP>, <SAMP>`~'</SAMP>.
<DT><CODE>exponentiation</CODE>
<DD>
<SAMP>`^'</SAMP>, <SAMP>`**'</SAMP>, <SAMP>`.^'</SAMP>, <SAMP>`.**'</SAMP>.
</DL>
<P><HR><P>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_8.html">previous</A>, <A HREF="octave_10.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
</BODY>
</HTML>
|