[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP BASIC for OpenVMS
|
Previous | Contents | Index |
The MAT PRINT statement prints some or all of an array's elements, excluding row and column zero. The MAT PRINT # statement takes values from an array by row, starting with element (1,1), and writes each element to a sequential record in the terminal-format file.
Subscripts are optional in MAT PRINT statements. If you do not specify subscripts, MAT PRINT displays the entire array, excluding row and column zero. If you specify subscripts, MAT PRINT displays the specified subset of the array. In the case of the MAT PRINT # statement, the subscripts determine how many array elements are written to the file. The MAT PRINT [#] statement does not redimension an existing array.
If the last character in the MAT PRINT [#] array list is a semicolon, BASIC begins each array row on a separate line. Data values on each line are packed together with no intermediate spaces. However, if the last character in the MAT PRINT [#] array list is a comma, BASIC begins each array row on a separate line and each data value in a separate print zone.
If there is neither a comma nor a semicolon after the array name, BASIC prints each array element on a separate line. In the following example, the first MAT PRINT statement does not end in a comma or semicolon, so each element is printed on a separate line. The second MAT PRINT statement prints the elements twice, the first time starting each element in a new print zone, and the second time leaving a space before and after each value. The MAT PRINT # statement sends the last two lines of output to a terminal-format file.
MAT INPUT A(5) PRINT MAT PRINT A PRINT MAT PRINT A, A; MAT PRINT #3, A, A; END |
? 5 5 0 0 0 0 5 0 0 0 0 5 0 0 0 0 |
MAT statements do not signal error messages when there are more data items than array elements to contain them or when there are fewer data items than array elements to contain them.
BASIC provides two functions that let you determine how much data the MAT statements transfer: NUM and NUM2.
For two-dimensional arrays, the NUM function returns an integer value specifying the row number of the last data item transferred, and the NUM2 function returns an integer value specifying the column number of the last data item transferred. For one-dimensional arrays, the NUM function returns the number of items entered, and the NUM2 function returns a zero.
With these functions, you can determine the number of items transferred from a terminal-format file. Note, however, that you cannot use the NUM and NUM2 functions to implicitly declare an array. In the following example, the terminal-format file EMP.DAT contains the values 1 to 17, inclusive. When these values are read with the MAT INPUT # statement, NUM and NUM2 represent the row and column number, respectively, of the last value read.
OPEN "EMP.DAT" FOR INPUT AS FILE #3% DIM emp_name$(5,5) MAT INPUT #3%, emp_name$ PRINT NUM, NUM2 END |
4 2 |
BASIC provides a special set of MAT statements for array computations. These statements enable you to add, subtract, and multiply matrices, and to assign values to elements. Note that if you specify an array without subscripts (for example, MAT A), the default is two dimensions.
BASIC also provides matrix functions to transpose and invert matrices and to find the determinant of a matrix you invert.
MAT operators do not operate on elements in row or column zero. |
MAT operators perform matrix assignment, addition, subtraction, and multiplication.
All of these operations use the keyword MAT, followed by an expression. If the array has not been previously dimensioned, these operations create an array. The created output array's dimensions depend on the operation performed but must be (10,10) or smaller.
You can use the MAT operators on arrays larger than (10,10) if the input and output arrays are explicitly created or received as a formal parameter. |
You can assign all values in one array to another array with the MAT statement. In the following example, each element of new_array is set to the corresponding element in old_array. The dimensions of new_array are also redimensioned to the dimensions of old_array.
MAT new_array = old_array |
You can add the elements of two arrays. In the following statement, the two input lists, first_list% and second_list%, must have identical dimensions. The elements of the new list, sum_list%, equal the sum of the corresponding elements in the input lists.
MAT sum_list% = first_list% + second_list% |
You can also subtract the elements of two arrays. The following program subtracts one array from another:
DIM first_array(30,30) DIM second_array(30,30) DIM difference_array(30,30) . . . MAT difference_array = first_array - second_array |
Each element of difference_array is the arithmetic difference
of the corresponding elements of the input arrays.
6.7.1.3 Multiplication
You can multiply the elements of two arrays, provided that the number of columns in the first array equals the number of rows in the second array. The resulting array contains the dot product of the two input arrays.
DIM A(2,2), B(2,2), C(2,2) A(1,1) = 1 A(1,2) = 2 A(2,1) = 3 A(2,2) = 4 B(1,1) = 5 B(1,2) = 6 B(2,1) = 7 B(2,2) = 8 MAT C = A * B MAT PRINT C |
19 22 43 50 |
You can also multiply a matrix by a scalar quantity. BASIC multiplies each element of the input array by the scalar quantity you supply. The output array has the same dimensions as the input array. Enclose the scalar quantity in parentheses. The following example multiplies the elements of inch_array by the inch-to-centimeter conversion factor and places these values in cm_array:
DIM inch_array(5), cm_array(5) MAT READ inch_array DATA 1,12,36,100,39.37 MAT cm_array = (2.54) * inch_array MAT PRINT cm_array, END |
2.54 30.48 91.44 254 99.9998 |
BASIC provides the following matrix functions:
TRN
INV
DET
With these functions, you can transpose and invert matrices and find
the determinant of an inverted matrix.
6.7.2.1 TRN Function
The TRN function transposes a matrix. When you transpose a matrix, BASIC interchanges the array's dimensions. For example, a matrix with n rows and m columns is transposed to a matrix with m rows and n columns. The elements in the first row of the input matrix become the elements in the first column of the output matrix. You cannot transpose a matrix to itself; MAT A = TRN(A) is invalid.
The following example creates a 3-by-5 matrix, transposes it, and prints the results:
DIM B(3,5) MAT READ B MAT A = TRN(B) DATA 1,2,3,4,5 DATA 6,7,8,9,10 DATA 11,12,13,14,15 MAT PRINT B; MAT PRINT A; END |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 6 11 2 7 12 3 8 13 4 9 14 5 10 15 |
The INV function inverts a matrix. BASIC can invert a matrix only if its subscripts are identical and it can be reduced to the identity matrix by elementary row operations. The input matrix multiplied by the output matrix (its inverse) always gives the identity matrix as a result.
MAT INPUT first_array(3,3) MAT PRINT first_array; PRINT MAT inv_array = INV (first_array) MAT PRINT inv_array; PRINT MAT mult_array = first_array * inv_array MAT PRINT mult_array; PRINT D = DET PRINT D |
? 4,0,0,0,0,2,0,8,0 4 0 0 0 0 2 0 8 0 .25 0 0 0 0 .125 0 .5 0 1 0 0 0 1 0 0 0 1 -64 |
The DET function returns the determinant of a matrix. The DET function returns a floating-point number that is the determinant of the last matrix inverted. If you use the DET function before inverting a matrix, the value of DET is zero.
This chapter briefly describes how to define program objects,
explicitly assign data types, and allocate and use data storage.
7.1 Declarative Statements
You use declarative statements to define objects in a HP BASIC program. Objects can be variables, arrays, constants, and user-defined functions within a program module. They can also be routines, variables, and constants external to the program module. Declarative statements always assign names to the objects declared and usually assign other attributes, such as a data type, to them. Declarative statements can also be used to define user-defined data types (RECORD statements). See Chapter 8 for more information about the RECORD statement.
You use declarative statements to assign data types to:
By declaring the objects used in your program, you make the program
easier to understand, modify, and debug.
7.2 Data Types
At its most fundamental level, a data type is a format for information storage. All information is stored in the computer as bit patterns (groups of ones and zeros). Data types specify how the computer should interpret these patterns.
HP BASIC programs allow five general data types: integer, floating-point, string, packed decimal, and record. Each data type is suited for a particular type of task. For example, integers are useful for numeric computations involving whole numbers, strings provide a way to manipulate alphanumeric characters, and packed decimal data is useful for manipulating numeric values that require precise representation.
For more information about HP BASIC data types, see the
HP BASIC for OpenVMS Reference Manual.
7.3 Setting the Default Data Type and Size
There are two ways to set the default data type and size for your program:
The OPTION statement can override the defaults set with qualifiers. For example, the following statement sets the default integer type to be LONG:
OPTION SIZE = INTEGER LONG |
You can have more than one OPTION statement in a program module; however, OPTION statements can be preceded only by a SUB, FUNCTION, REM, or another OPTION statement.
Note that the OPTION statement can also specify the following:
See the HP BASIC for OpenVMS Reference Manual for more information about the OPTION statement.
The OPTION statement in the following example specifies that all program variables must be explicitly typed and that all implicitly typed constants are INTEGER. In addition, any variable typed as INTEGER is a LONG integer and any variable typed as REAL is a DOUBLE floating-point number.
OPTION TYPE = EXPLICIT, ! Variables must be declared & CONSTANT TYPE = INTEGER, ! All implicit constants be integers & SIZE = INTEGER LONG, ! 32-bit integers by default & SIZE = REAL DOUBLE ! 64-bit floating-point ! numbers by default |
You can create variables of other data types by explicitly declaring
them with the DECLARE, COMMON, or MAP statement.
7.4 Declaring Variables
A variable is a named quantity whose value can change during program execution. Variables may be implicitly or explicitly declared. HP BASIC accepts the following types of variables:
For more information about declaring variables, see the HP BASIC for OpenVMS Reference Manual.
7.5 Declaring Named Constants
A constant is a value that does not change during program execution. You can declare named constants within a program unit with the DECLARE statement. You can also refer to constants outside the program unit with the EXTERNAL statement. In addition, BASIC provides notation for binary, octal, decimal, and hexadecimal constants.
For more information about named constants, see the HP BASIC for OpenVMS Reference Manual.
7.6 Operations with Multiple Data Types
When an expression contains operands of different data types, it is called a mixed-mode expression. Before a mixed-mode expression can be evaluated, the operands must be converted, or promoted, to a common data type. The result of the evaluation can also be converted depending on the data type of the variable to which it is assigned.
When assigning values to variables, HP BASIC converts the result of the expression to the data type of the variable. If the value of the expression is outside the allowable range of the variable's data type, HP BASIC signals "Integer error or overflow," "Floating-point error or overflow," or "DECIMAL error or overflow."
In general, HP BASIC promotes operands with different data types
to the lowest data type that can hold the largest and most precise
possible value of either operand's data type. HP BASIC then
performs the operation in that data type, and yields a result of that
data type. If the result of the expression is assigned to a variable,
HP BASIC converts the result to the data type of the variable. For
more information about multiple data types, see the HP BASIC for OpenVMS Reference Manual.
7.7 Allocating Dynamic and Static Storage
HP BASIC programs allocate both dynamic and static storage. Dynamic storage is allocated when the program executes, whereas the size of static storage does not change during program execution.
Variables and arrays declared by the following means use dynamic storage:
Normally, string variables and arrays declared in these ways are dynamic strings, and their length can change during program execution. However, if you declare or dimension an array of a user-defined data type (a RECORD name), then all string variables and arrays are fixed-length strings. See Chapter 8 for more information about the RECORD statement.
Variables and arrays appearing in MAP or COMMON statements use static storage. Hence all string variables appearing in MAP or COMMON statements are fixed-length strings. MAP and COMMON statements create a named storage area called a program section, or PSECT. MAP statements require a map name, but in COMMON statements the name is optional. The PSECT name is the same as the map or common name. If you do not specify a common name, HP BASIC supplies a default PSECT name of $BLANK.
The remainder of this section explains how to use COMMON and MAP
statements for static storage.
7.7.1 COMMON Statement
The COMMON statement defines a named area of storage (called a PSECT). Any HP BASIC subprogram can access the values in a common area by specifying a common with the same name. An item in a COMMON statement can be any one of the following:
The amount of storage reserved for a variable depends on its data type. You can specify a length for string variables and string array elements that appear in a COMMON statement. If you do not specify a length, the default is 16. The following statement specifies 2 bytes for emp.code, 3 bytes for wage.code, and 22 bytes for dep.code:
COMMON (code) STRING emp.code=2, wage.code=3, dep.code=22 |
In a single program module, multiple common areas with the same name allocate storage end-to-end in a single PSECT. That is, HP BASIC concatenates all common areas with the same name in the same program module, in order of appearance. For example, the following statements allocate storage for five LONG integers in a single PSECT named into:
COMMON (into) LONG call_count, sub1_count, sub2_count COMMON (into) LONG sub3_count, sub4_count |
When you explicitly declare an array, HP BASIC allows you to specify both upper and lower bound values. The value you supply as the upper bound determines the maximum subscript value for a given dimension, and the value you supply for the lower bound determines the minimum subscript value for a given dimension.
For more information about specifying bounds with the COMMON statement,
see Chapter 6 and the HP BASIC for OpenVMS Reference Manual.
7.7.2 MAP Statement
The MAP statement, like the COMMON statement, creates a named area of static storage. However, if a program module contains multiple maps with the same name, the maps are overlaid on the same area of storage, rather than being concatenated.
When used with the MAP clause of the OPEN statement, the storage allocated by the MAP statement becomes the record buffer for that file. Variables in the MAP statement correspond to fields in the file's records.
A map item can be one of the following:
When you explicitly declare an array, HP BASIC allows you to specify both upper and lower bound values. The value you supply as the upper bound determines the maximum subscript value for a given dimension, and the value you supply for the lower bound determines the minimum subscript value for a given dimension.
For more information about specifying bounds with the MAP statement,
see Chapter 6 and the HP BASIC for OpenVMS Reference Manual.
7.7.2.1 Single Maps
You associate a map with a record buffer by referencing the map in the OPEN statement.
The MAP statement must appear before any reference to map variables. Changes to map variables do not change the actual records in the file. To transfer the changed variables to the file, you must use the PUT or UPDATE statement. For more information, see Chapter 13.
The following program example uses map variables to access fields in payroll records:
WHEN ERROR USE eof_handler DECLARE INTEGER CONSTANT EOF = 11 MAP (PAYROL) STRING emp_name, LONG wage_class, & STRING sal_rev_date, SINGLE tax_ytd OPEN "payroll.dat" FOR INPUT AS FILE #4% & ,ORGANIZATION SEQUENTIAL & ,ACCESS READ & ,MAP PAYROL OPEN "payrol.new" FOR OUTPUT AS FILE #5% & ,ORGANIZATION SEQUENTIAL & ,ACCESS WRITE & ,MAP payrol PRINT "PAYROLL VERIFICATION" get_loop: WHILE 1% = 1% GET #4 PRINT emp_name, wage_class, sal_rev_date, tax_ytd PRINT "YOU CAN CHANGE:" PRINT "1. EMPLOYEE NAME" PRINT "2. WAGE CLASS" PRINT "3. REVIEW DATE" PRINT "4. TAX YEAR-TO-DATE" PRINT "5. DONE" read_loop: WHILE 1% = 1% INPUT "CHANGES? ANSWER WITH YES OR NO" ; chng$ IF chng$ = "NO" THEN ITERATE get_loop ELSE INPUT "NUMBER" ;number% END IF SELECT number% CASE 1 INPUT "EMPLOYEE NAME"; emp_name CASE 2 INPUT "WAGE CLASS"; wage_class CASE 3 INPUT "REVIEW DATE";sal_rev_date CASE 4 INPUT "TAX YEAR-TO-DATE"; tax_ytd CASE 5 EXIT read_loop CASE ELSE PRINT "Invalid response -- please try again" END SELECT NEXT PUT #5 NEXT END WHEN HANDLER eof_handler IF ERR = EOF THEN PRINT "End of file" ELSE EXIT HANDLER END IF END HANDLER END |
Previous | Next | Contents | Index |