14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 Data Structures 157<br />

Matrices<br />

Find the solution to this system using the following script:<br />

A=[1 2 2, 2 4 4, 1 1 1];<br />

B=[6,12,1];<br />

Show(GInverse(A)*B);<br />

G Inverse(A)*B=[-4,2.5,2.5]<br />

Solve<br />

The Solve() function solves a system of linear equations. Solve() finds the vector x so that x = A – 1 b<br />

where A equals a square, nonsingular matrix and b is a vector. The matrix A and the vector b must have the<br />

same number of rows. Solve(A,b) is the same as Inverse(A)*b.<br />

A=[1 -4 2, 3 3 2, 0 4 -1];<br />

b=[1, 2, 1];<br />

x=solve(A,b);<br />

[-16.9999999999999, 4.99999999999998, 18.9999999999999]<br />

A*x;<br />

[1, 2, 0.999999999999997]<br />

Note: There can be small discrepancies in the results due to floating point limitations, as illustrated in the<br />

example.<br />

Sweep<br />

The Sweep() function inverts parts (or pivots) of a square matrix. If you sequence through all of the pivots,<br />

you are left with the matrix inverse. Normally the matrix must be positive definite (or negative definite) so<br />

that the diagonal pivots never go to zero. Sweep() does not check whether the matrix is positive definite. If<br />

the matrix is not positive definite, then it still works, as long as no zero pivot diagonals are encountered. If<br />

zero (or near-zero) pivot diagonals are encountered on a full sweep, then the result is a g2 generalized inverse<br />

if the zero pivot row and column are zeroed.<br />

About the Sweep Function<br />

Suppose matrix E consists of smaller matrix partitions, A, B, C, and D:<br />

E<br />

=<br />

AB<br />

CD<br />

The syntax for Sweep() appears as follows:<br />

Sweep(E, [...]); // where [...] indicates partition A<br />

This produces the matrix result equivalent to the following:<br />

A 1 A 1 B<br />

– CA 1 D – CA 1 B

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!