1@c Copyright (C) 1996-2019 John W. Eaton 2@c 3@c This file is part of Octave. 4@c 5@c Octave is free software: you can redistribute it and/or modify it 6@c under the terms of the GNU General Public License as published by 7@c the Free Software Foundation, either version 3 of the License, or 8@c (at your option) any later version. 9@c 10@c Octave is distributed in the hope that it will be useful, but 11@c WITHOUT ANY WARRANTY; without even the implied warranty of 12@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13@c GNU General Public License for more details. 14@c 15@c You should have received a copy of the GNU General Public License 16@c along with Octave; see the file COPYING. If not, see 17@c <https://www.gnu.org/licenses/>. 18 19@node Numeric Data Types 20@chapter Numeric Data Types 21@cindex numeric constant 22@cindex numeric value 23 24A @dfn{numeric constant} may be a scalar, a vector, or a matrix, and it may 25contain complex values. 26 27The simplest form of a numeric constant, a scalar, is a single number. Note 28that by default numeric constants are represented within Octave by IEEE 754 29double precision (binary64) floating-point format (complex constants are 30stored as pairs of binary64 values). It is, however, possible to represent 31real integers as described in @ref{Integer Data Types}. 32 33If the numeric constant is a real integer, it can be defined in decimal, 34hexadecimal, or binary notation. Hexadecimal notation starts with @samp{0x} or 35@samp{0X}, binary notation starts with @samp{0b} or @samp{0B}, otherwise 36decimal notation is assumed. As a consequence, @samp{0b} is not a hexadecimal 37number, in fact, it is not a valid number at all. 38 39For better readability, digits may be partitioned by the underscore separator 40@samp{_}, which is ignored by the Octave interpreter. Here are some examples 41of real-valued integer constants, which all represent the same value and are 42internally stored as binary64: 43 44@example 45@group 4642 # decimal notation 470x2A # hexadecimal notation 480b101010 # binary notation 490b10_1010 # underscore notation 50round (42.1) # also binary64 51@end group 52@end example 53 54In decimal notation, the numeric constant may be denoted as decimal fraction 55or even in scientific (exponential) notation. Note that this is not possible 56for hexadecimal or binary notation. Again, in the following example all 57numeric constants represent the same value: 58 59@example 60@group 61.105 621.05e-1 63.00105e+2 64@end group 65@end example 66 67Unlike most programming languages, complex numeric constants are denoted as 68the sum of real and imaginary parts. The imaginary part is denoted by a 69real-valued numeric constant followed immediately by a complex value indicator 70(@samp{i}, @samp{j}, @samp{I}, or @samp{J} which represents 71@tex 72 $\sqrt{-1}$). 73@end tex 74@ifnottex 75 @code{sqrt (-1)}). 76@end ifnottex 77No spaces are allowed between the numeric constant and the complex value 78indicator. Some examples of complex numeric constants that all represent the 79same value: 80 81@example 82@group 833 + 42i 843 + 42j 853 + 42I 863 + 42J 873.0 + 42.0i 883.0 + 0x2Ai 893.0 + 0b10_1010i 900.3e1 + 420e-1i 91@end group 92@end example 93 94@DOCSTRING(double) 95 96@DOCSTRING(complex) 97 98@menu 99* Matrices:: 100* Ranges:: 101* Single Precision Data Types:: 102* Integer Data Types:: 103* Bit Manipulations:: 104* Logical Values:: 105* Promotion and Demotion of Data Types:: 106* Predicates for Numeric Objects:: 107@end menu 108 109@node Matrices 110@section Matrices 111@cindex matrices 112 113@opindex [ 114@opindex ] 115@opindex ; 116@opindex , 117 118It is easy to define a matrix of values in Octave. The size of the 119matrix is determined automatically, so it is not necessary to explicitly 120state the dimensions. The expression 121 122@example 123a = [1, 2; 3, 4] 124@end example 125 126@noindent 127results in the matrix 128@tex 129$$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ 130@end tex 131@ifnottex 132 133@example 134@group 135 136 / \ 137 | 1 2 | 138 a = | | 139 | 3 4 | 140 \ / 141 142@end group 143@end example 144 145@end ifnottex 146 147Elements of a matrix may be arbitrary expressions, provided that the 148dimensions all make sense when combining the various pieces. For 149example, given the above matrix, the expression 150 151@example 152[ a, a ] 153@end example 154 155@noindent 156produces the matrix 157 158@example 159@group 160ans = 161 162 1 2 1 2 163 3 4 3 4 164@end group 165@end example 166 167@noindent 168but the expression 169 170@example 171[ a, 1 ] 172@end example 173 174@noindent 175produces the error 176 177@example 178error: number of rows must match (1 != 2) near line 13, column 6 179@end example 180 181@noindent 182(assuming that this expression was entered as the first thing on line 18313, of course). 184 185Inside the square brackets that delimit a matrix expression, Octave 186looks at the surrounding context to determine whether spaces and newline 187characters should be converted into element and row separators, or 188simply ignored, so an expression like 189 190@example 191@group 192a = [ 1 2 193 3 4 ] 194@end group 195@end example 196 197@noindent 198will work. However, some possible sources of confusion remain. For 199example, in the expression 200 201@example 202[ 1 - 1 ] 203@end example 204 205@noindent 206the @samp{-} is treated as a binary operator and the result is the 207scalar 0, but in the expression 208 209@example 210[ 1 -1 ] 211@end example 212 213@noindent 214the @samp{-} is treated as a unary operator and the result is the 215vector @code{[ 1, -1 ]}. Similarly, the expression 216 217@example 218[ sin (pi) ] 219@end example 220 221@noindent 222will be parsed as 223 224@example 225[ sin, (pi) ] 226@end example 227 228@noindent 229and will result in an error since the @code{sin} function will be 230called with no arguments. To get around this, you must omit the space 231between @code{sin} and the opening parenthesis, or enclose the 232expression in a set of parentheses: 233 234@example 235[ (sin (pi)) ] 236@end example 237 238Whitespace surrounding the single quote character (@samp{'}, used as a 239transpose operator and for delimiting character strings) can also cause 240confusion. Given @code{a = 1}, the expression 241 242@example 243[ 1 a' ] 244@end example 245 246@noindent 247results in the single quote character being treated as a 248transpose operator and the result is the vector @code{[ 1, 1 ]}, but the 249expression 250 251@example 252[ 1 a ' ] 253@end example 254 255@noindent 256produces the error message 257 258@example 259@group 260parse error: 261 262 syntax error 263 264>>> [ 1 a ' ] 265 ^ 266@end group 267@end example 268 269@noindent 270because not doing so would cause trouble when parsing the valid expression 271 272@example 273[ a 'foo' ] 274@end example 275 276For clarity, it is probably best to always use commas and semicolons to 277separate matrix elements and rows. 278 279The maximum number of elements in a matrix is fixed when Octave is compiled. 280The allowable number can be queried with the function @code{sizemax}. Note 281that other factors, such as the amount of memory available on your machine, 282may limit the maximum size of matrices to something smaller. 283 284@DOCSTRING(sizemax) 285 286When you type a matrix or the name of a variable whose value is a 287matrix, Octave responds by printing the matrix in with neatly aligned 288rows and columns. If the rows of the matrix are too large to fit on the 289screen, Octave splits the matrix and displays a header before each 290section to indicate which columns are being displayed. You can use the 291following variables to control the format of the output. 292 293@DOCSTRING(output_precision) 294 295It is possible to achieve a wide range of output styles by using 296different values of @code{output_precision}. Reasonable combinations can be 297set using the @code{format} function. @xref{Basic Input and Output}. 298 299@DOCSTRING(split_long_rows) 300 301Octave automatically switches to scientific notation when values become 302very large or very small. This guarantees that you will see several 303significant figures for every value in a matrix. If you would prefer to 304see all values in a matrix printed in a fixed point format, you can use 305the function @code{fixed_point_format}. But doing so is not 306recommended, because it can produce output that can easily be 307misinterpreted. 308 309@DOCSTRING(fixed_point_format) 310 311@menu 312* Empty Matrices:: 313@end menu 314 315@node Empty Matrices 316@subsection Empty Matrices 317 318A matrix may have one or both dimensions zero, and operations on empty 319matrices are handled as described by @nospell{Carl de Boor} in 320@cite{An Empty Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and 321@nospell{C. N. Nett and W. M. Haddad}, in 322@cite{A System-Theoretic Appropriate Realization of the Empty Matrix Concept}, 323IEEE Transactions on Automatic Control, Volume 38, Number 5, May 1993. 324@tex 325Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, 326and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or 327both dimensions equal to zero), the following are true: 328$$ 329\eqalign{% 330s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr 331[\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr 332[\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr 333M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr 334[\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} 335$$ 336@end tex 337@ifnottex 338Briefly, given a scalar @var{s}, an @var{m} by 339@var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix 340@code{[](mxn)} (with either one or both dimensions equal to zero), the 341following are true: 342 343@example 344@group 345s * [](mxn) = [](mxn) * s = [](mxn) 346 347 [](mxn) + [](mxn) = [](mxn) 348 349 [](0xm) * M(mxn) = [](0xn) 350 351 M(mxn) * [](nx0) = [](mx0) 352 353 [](mx0) * [](0xn) = 0(mxn) 354@end group 355@end example 356 357@end ifnottex 358 359By default, dimensions of the empty matrix are printed along with the 360empty matrix symbol, @samp{[]}. The built-in variable 361@code{print_empty_dimensions} controls this behavior. 362 363@DOCSTRING(print_empty_dimensions) 364 365Empty matrices may also be used in assignment statements as a convenient 366way to delete rows or columns of matrices. 367@xref{Assignment Ops,,Assignment Expressions}. 368 369When Octave parses a matrix expression, it examines the elements of the 370list to determine whether they are all constants. If they are, it 371replaces the list with a single matrix constant. 372 373@node Ranges 374@section Ranges 375@cindex range expressions 376@cindex expression, range 377 378@opindex :, range expressions 379 380A @dfn{range} is a convenient way to write a row vector with evenly 381spaced elements. A range expression is defined by the value of the first 382element in the range, an optional value for the increment between 383elements, and a maximum value which the elements of the range will not 384exceed. The base, increment, and limit are separated by colons (the 385@samp{:} character) and may contain any arithmetic expressions and 386function calls. If the increment is omitted, it is assumed to be 1. 387For example, the range 388 389@example 3901 : 5 391@end example 392 393@noindent 394defines the set of values @code{[ 1, 2, 3, 4, 5 ]}, and the range 395 396@example 3971 : 3 : 5 398@end example 399 400@noindent 401defines the set of values @code{[ 1, 4 ]}. 402 403Although a range constant specifies a row vector, Octave does @emph{not} 404normally convert range constants to vectors unless it is necessary to do so. 405This allows you to write a constant like @code{1 : 10000} without using 40680,000 bytes of storage on a typical 32-bit workstation. 407 408A common example of when it does become necessary to convert ranges into 409vectors occurs when they appear within a vector (i.e., inside square 410brackets). For instance, whereas 411 412@example 413x = 0 : 0.1 : 1; 414@end example 415 416@noindent 417defines @var{x} to be a variable of type @code{range} and occupies 24 418bytes of memory, the expression 419 420@example 421y = [ 0 : 0.1 : 1]; 422@end example 423 424@noindent 425defines @var{y} to be of type @code{matrix} and occupies 88 bytes of 426memory. 427 428This space saving optimization may be disabled using the function 429@dfn{disable_range}. 430 431@DOCSTRING(disable_range) 432 433Note that the upper (or lower, if the increment is negative) bound on 434the range is not always included in the set of values, and that ranges 435defined by floating point values can produce surprising results because 436Octave uses floating point arithmetic to compute the values in the 437range. If it is important to include the endpoints of a range and the 438number of elements is known, you should use the @code{linspace} function 439instead (@pxref{Special Utility Matrices}). 440 441When adding a scalar to a range, subtracting a scalar from it (or subtracting a 442range from a scalar) and multiplying by scalar, Octave will attempt to avoid 443unpacking the range and keep the result as a range, too, if it can determine 444that it is safe to do so. For instance, doing 445 446@example 447a = 2*(1:1e7) - 1; 448@end example 449 450@noindent 451will produce the same result as @code{1:2:2e7-1}, but without ever forming a 452vector with ten million elements. 453 454Using zero as an increment in the colon notation, as @code{1:0:1} is not 455allowed, because a division by zero would occur in determining the number of 456range elements. However, ranges with zero increment (i.e., all elements equal) 457are useful, especially in indexing, and Octave allows them to be constructed 458using the built-in function @code{ones}. Note that because a range must be a 459row vector, @code{ones (1, 10)} produces a range, while @code{ones (10, 1)} 460does not. 461 462When Octave parses a range expression, it examines the elements of the 463expression to determine whether they are all constants. If they are, it 464replaces the range expression with a single range constant. 465 466@node Single Precision Data Types 467@section Single Precision Data Types 468 469Octave includes support for single precision data types, and most of the 470functions in Octave accept single precision values and return single 471precision answers. A single precision variable is created with the 472@code{single} function. 473 474@DOCSTRING(single) 475 476for example: 477 478@example 479@group 480sngl = single (rand (2, 2)) 481 @result{} sngl = 482 0.37569 0.92982 483 0.11962 0.50876 484class (sngl) 485 @result{} single 486@end group 487@end example 488 489Many functions can also return single precision values directly. For 490example 491 492@example 493@group 494ones (2, 2, "single") 495zeros (2, 2, "single") 496eye (2, 2, "single") 497rand (2, 2, "single") 498NaN (2, 2, "single") 499NA (2, 2, "single") 500Inf (2, 2, "single") 501@end group 502@end example 503 504@noindent 505will all return single precision matrices. 506 507@node Integer Data Types 508@section Integer Data Types 509 510Octave supports integer matrices as an alternative to using double 511precision. It is possible to use both signed and unsigned integers 512represented by 8, 16, 32, or 64 bits. It should be noted that most 513computations require floating point data, meaning that integers will 514often change type when involved in numeric computations. For this 515reason integers are most often used to store data, and not for 516calculations. 517 518In general most integer matrices are created by casting 519existing matrices to integers. The following example shows how to cast 520a matrix into 32 bit integers. 521 522@example 523@group 524float = rand (2, 2) 525 @result{} float = 0.37569 0.92982 526 0.11962 0.50876 527integer = int32 (float) 528 @result{} integer = 0 1 529 0 1 530@end group 531@end example 532 533@noindent 534As can be seen, floating point values are rounded to the nearest integer 535when converted. 536 537@DOCSTRING(isinteger) 538 539@DOCSTRING(int8) 540 541@DOCSTRING(uint8) 542 543@DOCSTRING(int16) 544 545@DOCSTRING(uint16) 546 547@DOCSTRING(int32) 548 549@DOCSTRING(uint32) 550 551@DOCSTRING(int64) 552 553@DOCSTRING(uint64) 554 555@DOCSTRING(intmax) 556 557@DOCSTRING(intmin) 558 559@DOCSTRING(flintmax) 560 561@menu 562* Integer Arithmetic:: 563@end menu 564 565@node Integer Arithmetic 566@subsection Integer Arithmetic 567 568While many numerical computations can't be carried out in integers, 569Octave does support basic operations like addition and multiplication 570on integers. The operators @code{+}, @code{-}, @code{.*}, and @code{./} 571work on integers of the same type. So, it is possible to add two 32 bit 572integers, but not to add a 32 bit integer and a 16 bit integer. 573 574When doing integer arithmetic one should consider the possibility of 575underflow and overflow. This happens when the result of the computation 576can't be represented using the chosen integer type. As an example it is 577not possible to represent the result of @math{10 - 20} when using 578unsigned integers. Octave makes sure that the result of integer 579computations is the integer that is closest to the true result. So, the 580result of @math{10 - 20} when using unsigned integers is zero. 581 582When doing integer division Octave will round the result to the nearest 583integer. This is different from most programming languages, where the 584result is often floored to the nearest integer. So, the result of 585@code{int32 (5) ./ int32 (8)} is @code{1}. 586 587@DOCSTRING(idivide) 588 589@node Bit Manipulations 590@section Bit Manipulations 591 592Octave provides a number of functions for the manipulation of numeric 593values on a bit by bit basis. The basic functions to set and obtain the 594values of individual bits are @code{bitset} and @code{bitget}. 595 596@DOCSTRING(bitset) 597 598@DOCSTRING(bitget) 599 600The arguments to all of Octave's bitwise operations can be scalar or 601arrays, except for @code{bitcmp}, whose @var{k} argument must a 602scalar. In the case where more than one argument is an array, then all 603arguments must have the same shape, and the bitwise operator is applied 604to each of the elements of the argument individually. If at least one 605argument is a scalar and one an array, then the scalar argument is 606duplicated. Therefore 607 608@example 609bitget (100, 8:-1:1) 610@end example 611 612@noindent 613is the same as 614 615@example 616bitget (100 * ones (1, 8), 8:-1:1) 617@end example 618 619It should be noted that all values passed to the bit manipulation 620functions of Octave are treated as integers. Therefore, even though the 621example for @code{bitset} above passes the floating point value 622@code{10}, it is treated as the bits @code{[1, 0, 1, 0]} rather than the 623bits of the native floating point format representation of @code{10}. 624 625As the maximum value that can be represented by a number is important 626for bit manipulation, particularly when forming masks, Octave supplies 627two utility functions: @code{flintmax} for floating point integers, and 628@code{intmax} for integer objects (@code{uint8}, @code{int64}, etc.). 629 630Octave also includes the basic bitwise 'and', 'or', and 'exclusive or' 631operators. 632 633@DOCSTRING(bitand) 634 635@DOCSTRING(bitor) 636 637@DOCSTRING(bitxor) 638 639The bitwise 'not' operator is a unary operator that performs a logical 640negation of each of the bits of the value. For this to make sense, the 641mask against which the value is negated must be defined. Octave's 642bitwise 'not' operator is @code{bitcmp}. 643 644@DOCSTRING(bitcmp) 645 646Octave also includes the ability to left-shift and right-shift values bitwise. 647 648@DOCSTRING(bitshift) 649 650Bits that are shifted out of either end of the value are lost. Octave 651also uses arithmetic shifts, where the sign bit of the value is kept 652during a right shift. For example: 653 654@example 655@group 656bitshift (-10, -1) 657@result{} -5 658bitshift (int8 (-1), -1) 659@result{} -1 660@end group 661@end example 662 663Note that @code{bitshift (int8 (-1), -1)} is @code{-1} since the bit 664representation of @code{-1} in the @code{int8} data type is @code{[1, 1, 6651, 1, 1, 1, 1, 1]}. 666 667@node Logical Values 668@section Logical Values 669 670Octave has built-in support for logical values, i.e., variables that 671are either @code{true} or @code{false}. When comparing two variables, 672the result will be a logical value whose value depends on whether or 673not the comparison is true. 674 675The basic logical operations are @code{&}, @code{|}, and @code{!}, 676which correspond to ``Logical And'', ``Logical Or'', and ``Logical 677Negation''. These operations all follow the usual rules of logic. 678 679It is also possible to use logical values as part of standard numerical 680calculations. In this case @code{true} is converted to @code{1}, and 681@code{false} to 0, both represented using double precision floating 682point numbers. So, the result of @code{true*22 - false/6} is @code{22}. 683 684Logical values can also be used to index matrices and cell arrays. 685When indexing with a logical array the result will be a vector containing 686the values corresponding to @code{true} parts of the logical array. 687The following example illustrates this. 688 689@example 690@group 691data = [ 1, 2; 3, 4 ]; 692idx = (data <= 2); 693data(idx) 694 @result{} ans = [ 1; 2 ] 695@end group 696@end example 697 698@noindent 699Instead of creating the @code{idx} array it is possible to replace 700@code{data(idx)} with @w{@code{data( data <= 2 )}} in the above code. 701 702Logical values can also be constructed by 703casting numeric objects to logical values, or by using the @code{true} 704or @code{false} functions. 705 706@DOCSTRING(logical) 707 708@DOCSTRING(true) 709 710@DOCSTRING(false) 711 712@node Promotion and Demotion of Data Types 713@section Promotion and Demotion of Data Types 714 715Many operators and functions can work with mixed data types. For example, 716 717@example 718@group 719uint8 (1) + 1 720 @result{} 2 721@end group 722@end example 723 724@noindent 725where the above operator works with an 8-bit integer and a double precision 726value and returns an 8-bit integer value. Note that the type is demoted 727to an 8-bit integer, rather than promoted to a double precision value as 728might be expected. The reason is that if Octave promoted values in 729expressions like the above with all numerical constants would need to be 730explicitly cast to the appropriate data type like 731 732@example 733@group 734uint8 (1) + uint8 (1) 735 @result{} 2 736@end group 737@end example 738 739@noindent 740which becomes difficult for the user to apply uniformly and might allow 741hard to find bugs to be introduced. The same applies to single precision 742values where a mixed operation such as 743 744@example 745@group 746single (1) + 1 747 @result{} 2 748@end group 749@end example 750 751@noindent 752returns a single precision value. The mixed operations that are valid 753and their returned data types are 754 755@multitable @columnfractions .2 .3 .3 .2 756@headitem @tab Mixed Operation @tab Result @tab 757@item @tab double OP single @tab single @tab 758@item @tab double OP integer @tab integer @tab 759@item @tab double OP char @tab double @tab 760@item @tab double OP logical @tab double @tab 761@item @tab single OP integer @tab integer @tab 762@item @tab single OP char @tab single @tab 763@item @tab single OP logical @tab single @tab 764@end multitable 765 766The same logic applies to functions with mixed arguments such as 767 768@example 769@group 770min (single (1), 0) 771 @result{} 0 772@end group 773@end example 774 775@noindent 776where the returned value is single precision. 777 778In the case of mixed type indexed assignments, the type is not 779changed. For example, 780 781@example 782@group 783x = ones (2, 2); 784x(1, 1) = single (2) 785 @result{} x = 2 1 786 1 1 787@end group 788@end example 789 790@noindent 791where @code{x} remains of the double precision type. 792 793@node Predicates for Numeric Objects 794@section Predicates for Numeric Objects 795 796Since the type of a variable may change during the execution of a 797program, it can be necessary to do type checking at run-time. Doing this 798also allows you to change the behavior of a function depending on the 799type of the input. As an example, this naive implementation of @code{abs} 800returns the absolute value of the input if it is a real number, and the 801length of the input if it is a complex number. 802 803@example 804@group 805function a = abs (x) 806 if (isreal (x)) 807 a = sign (x) .* x; 808 elseif (iscomplex (x)) 809 a = sqrt (real(x).^2 + imag(x).^2); 810 endif 811endfunction 812@end group 813@end example 814 815The following functions are available for determining the type of a 816variable. 817 818@DOCSTRING(isnumeric) 819 820@DOCSTRING(islogical) 821 822@DOCSTRING(isfloat) 823 824@DOCSTRING(isreal) 825 826@DOCSTRING(iscomplex) 827 828@DOCSTRING(ismatrix) 829 830@DOCSTRING(isvector) 831 832@DOCSTRING(isrow) 833 834@DOCSTRING(iscolumn) 835 836@DOCSTRING(isscalar) 837 838@DOCSTRING(issquare) 839 840@DOCSTRING(issymmetric) 841 842@DOCSTRING(ishermitian) 843 844@DOCSTRING(isdefinite) 845 846@DOCSTRING(isbanded) 847 848@DOCSTRING(isdiag) 849 850@DOCSTRING(istril) 851 852@DOCSTRING(istriu) 853 854@DOCSTRING(isprime) 855 856If instead of knowing properties of variables, you wish to know which 857variables are defined and to gather other information about the 858workspace itself, @pxref{Status of Variables}. 859