1.1 Basics
description
Transcript of 1.1 Basics
1
1.1 Basics
2
程式架構 (I) program {name}
{declarations}
{other statements}
stop
end program {name}
紅色部分為 Fortran 90的做法
3
程式架構 (II) we now show a program that computes
the surface area of a sphere from the formula A= : program area real r, Aarea
c this program reads a real number r c and prints the surface area of a sphere that c has radius r.
read(*,*) r Aarea = 4. * 3.14159 * r * r write(*,*) Aarea stop end
24 r
4
Column position rules(Fortran77) I
Columns 1 : : 空白
1 : 數字 , 代表行數 c : 當成註解
Columns 6 :第六行若為非零以外字元 (即 &), 表示本行接續上一行程式 EX. a=b-c &
& -d+e
此為 a=b-c-d+e
5
Columns 7-72 : 程式敘述區 (開頭需空六格 ) 程式敘述……… ..
Columns 73-80: 不使用 ,超過部份不被當成程式碼
Fortran 90無以上 columns的規定
Column position rules(Fortran77) II
6
Fortran 77 vs. 90
註解 :- c 以後的字元當成註解 (Fortran 77)- ! 以後的字元當成註解 (Fortran 90)
儲存檔案 :*.for(Fortran 77)*.f90(Fortran 90)
7
Read and Write Read :
read( * , * ) {variables}
代表 UNIT(輸出位置 ) FMT(輸出格式 )
Write :write(UNIT= ,FMT= ) {variables}
包裝字串 : 使用‘ ’單引號 (Fortran 77), “ ”雙引號 (Fortran90)
8
Type and Declaration 整數 :Integer {list-of-variables} 複數 :Complex {list-of-variables} 字元 :Character {list-of-variables} 邏輯變數 :Logical {list-of-variables} 浮點數 (單精準 ):Real {list-of-variables} 浮點數 (雙精準 ):Double precision {list-of-variables}
變數名稱可由 a~z, A~Z, 0~9組成 ,
但須注意 , 要以字母開頭且變數長度不超過 6 個
9
Integer Variables
In a typical computer might be allocated for each variable x with 32bits , , and then
x =
Ex.
3110 ,....,, bbb
}1,0{ , ).....()1( 201293031 ibbbbbb
13
2021212021
0110143210
2
)(
10
Floating Point Variables Numbers that are stored in real or double
precision variables are represented in Floating point style.
A typical 32-bit FP number x might have a 24-bit mantissa m and an 8-bit exponent e ,
64-bits computer has 56bits for mantissa.
2221023 )...... ()1( bbbbm
2243031 )....()1( bbbe emx 2
11
Arithmetic Assignment add(+) , subtract(-), multiply(*) ,
divide(/) :Ex. 7*4-6/3
exponential :Ex. 2**3 (2 的 3次方 =8)
build-function :Ex. sqrt(4) (4開平方 =2.000000)
12
String Manipulation I
宣告 :character * 7 str
字串 str長度為 7(初始設定為 1)
假設 s1 = ‘abcde’ , s2=‘xyz’ :- 連結 : s=s1//s2 s=‘abcdexyz’
- 指定 : s1(2:4)=‘123’ s1=‘a123e’- 抽出 : s=s1(2:5) s=‘bcde’
13
String Manipulation II 內建函數 ( 當 s=‘abcdabcd’) :
- 計算長度 (length): Ex. length(s)=8- 比對內文 (index): Ex. index(s,’da’) => s=abcdabcd => 4
index(s,’dc’) => s=abcdabcd => 0- 輸入數字 , 對照電腦使用的字元集 (char): Ex. char(72)=H- 輸入字元 , 對照電腦使用的字元集 (ichar): Ex. ichar(H)=72
Fortran 90可使用 len求字串長度
14
Type conversion
c-charr-reald-double precisioni-integerx-real,double precision,
integer
Ex. If ksum is an integer and sum is real then sum = real (ksum) converts ksum to a floating representation(real), and stores the result in sum.
15
Parameter Statement 在程式中 ,有時需要固定不變的資料常數 ,如圓周率 ,
重力加速度…等 .為了簡化程式 ,可直接宣告成 parameter.
Ex. program areareal r, area1 ,fourpiparameter (fourpi = 12.566e0)read(*,*) rarea1 = fourpi * r * rwrite(*,*) area1stopend
16
1.2 LOGICAL OPERATIONS
17
Logical Variables
Declaration : logical a,b
Assigned either the value .TRUE. Or .FALSE.Ex. a = .TRUE.
It could be show like “T” to .TRUE.“F” to .FALSE.
It could be also assigned to .true. or .false. with the lower case.
18
Logical Expressions I
We define some relational operators :.LT. Less than.LE. Less than or equal.EQ. Equal.NE. Not equal.GT. Greater than.GE. Greater than or equal
Ex. How to test if i between 3 and 7 ? Ans: a =(3 .LE. i).AND.(i .LT. 10)
19
Logical Variables II
Truth table (And ,Or , Not) :
20
“IF” Constructs I
The simplest ‘if’ statement :if ({logical expression}) {executable
statement}
Ex. The following statement prints count if count is positive.
if (count .GT. 0) write(*,*) count
21
“IF” Constructs II
if ( {logical expression} ) then{statement}
endif
Ex. The following statement prints count if it is positive
if (count .GT. 0) then
write(*,*) count
endif
22
“IF” Constructs III
The most general form of ‘if’ :
if ( {logical expression} ) then {statement}
elseif ( {logical expression} ) then {statement}
: elseif ( {logical expression} ) then
{statement} endif
23
“IF” Constructs IV A program reads three distinct integers and prints the
median:program medianinteger x,y,z,median1read(*,*)x,y,zif ( (x-z)*(x-y) .LT. 0 ) then
median1 = xelseif ( (y-x)*(y-z) .LT. 0 ) then
median1 = yelse
median1 = zendifwrite(*,*) median1stopend
24
Stylistic Considerations
Make the program statements involves indentation to enhances the readability of if-then-else constructs.
An example :
25
1.3 LOOPS
26
While-Loops I
{label} if ( { logical expression }) then{statements}
goto {label}endif
All the labels in a program must be unique, except in two program ,like between main program and subprogram.
The label is a number between 1 and 99999 and must be situated within columns 2 through 5.
27
While-Loops II
Example:Prints Fibonacci numbers that are strictly less than 200.
program fibon integer x, y, z x = 1 y = 1 10 if ( x .LT. 200 ) then write(*,*)x z = x + y y = x x = z goto 10 endif stop end
28
Until-Loops {label} continue
{statements} if ( { logical expression }) goto {label}
Example:Prints Fibonacci numbers that are strictly less than 200.
program fibonacci integer x, y, z x = 1 y = 1
10 continue z = x + y y = x x = z write(*,*)x if ( x .LE. 200 ) goto 10 stop end
29
Do-Loops I
do {label} {var} = { exp.1 },{ exp.2 },{ exp.3 }{statements}
{label} continue
var is the loop index.exp.1 is an initial value of var.exp.2 is the terminating bound.exp.3 defines the increment(or decrement).
label is code used to assign the loop range.
30
Do-Loops II
Example:Prints Fibonacci numbers from x1 to x10.
program fibonacci integer x, y, z x = 1 y = 1
do 10 count = 1, 10 z = x + y y = x x = z write(*,*)x
10 continue stop end
31
Do-Loops III
Example: compute s = n(n-1)…….(n-k).
s = n do 20 j = n-1, n-k, -1 s = s*j
20 continue
The do statement here says “step from n-1 to n-k in steps of –1 ”.
32
Nesting Do-Loops
Example:Prints all integer triplets (i,j,k) with the property that
.
do 10 i = 1, 100 do 5 j = 1, 100 k = int( sqrt( real( i*i + j*j ) ) ) if ( k*k .EQ. i*i + j*j ) then write(*,*)i, j, k endif
5 continue 10 continue
222 i k and 100ji1 j
33
More on “goto” and “continue”
The ‘no operation’ continue statement is useful for specifying the target of the goto. For Example 10 continue
{statement}
goto 10
cause control to return up to statement 10 and continue (downward) form there.
The goto statement should be used only as last resort since the presence of goto’s in a program makes for difficult reading.
A do-loop must be entered ‘from the top’.Never jump into the body of a do-loop.
34
Loops, Indentation, and Labels
Indentation :The body of a loop should be uniformly indented to highlight the program structure and it should be more
readability. Labels :
Labels should be assigned so that the sequence of numbers is increasing,top to bottom.It’s good practice to leave gaps between the value of consecutive label and the subsequence program will be easily accommodated.
35
1.4 ARRAYS
36
One-dimensional Arrays I
Declaration{type} {name} (size)
Ex. real a(100)- one-dimensional- real array of size 100
37
One-dimensional Arrays II
Contentthe content of cell 1 is denoted by a(1)
Declaration
{name} ({first_index}:{last_index})
Ex. Claim a real array of size 100, with index range –41 to 58:
real b(-41:58)
38
Two-dimensional Arrays I
Declaration{name} (number of row, number of column)
Ex. integer A(4,5),identify a two-dimensional integer array of size 4x5:
(1,1) (1,2) (1,3) (1,4) (1,5)(2,1) (2,2) (2,3) (2,4) (2,5)(3,1) (3,2) (3,3) (3,4) (3,5)(4,1) (4,2) (4,3) (4,4) (4,5)
39
Two-dimensional Arrays II
Declaration
The total size of the array is(last_index1 - first_index1 + 1)*(last_index2 - first_index2 + 1)
integer k(20,100) integer k(1:20,1:100)
It’s the programmer’s responsibility to ensure that the index values are meaningful.Do not assume that array entries are automatically initialized to zero by the compiler.
x2}){last_inde:ex2}{first_indx1},{last_inde:dex1}({first_in {name}
40
Space Allocation for Two-dimensional Arrays I
Fortran stores a two-dimensional array as a contiguous, linear sequence of cells, by column.For example: 4x5 array
41
Space Allocation for Two-dimensional Arrays II
Note the storage of the 3-by-3 times table matrix in the 4-by-5 array results in unused array space:(the following matrix elements are not contiguous in memory)
42
Space Allocation for Two-dimensional Arrays III
Address of an array element isaddr[A(i,j)]=addr[A(1,1)]+(j-1)*adim+(i-1)
where A is an m-by-n matrix stored in array A that has row dimension adim.
43
Space Allocation for Two-dimensional Arrays IV
Physical address:Example:A cell may be 4 bytes long for integer arrays and 8 bytes long for double precision arrays.
Then,the physical address is addr[A(i,j)]=addr[A(1,1)]+[(j-1)*adim+(i-1)]*(4 or 8)byte
program frank integer F(15, 15), n, i, j do 25 n = 2, 6, 2 do 10 i = 1, n do 5 j = 1, n if ( i .LE. j ) then F(i, j) = n-j+1 elseif ( i .EQ. j+1 ) then F(i, j) = n-j else F(i, j) = 0 endif 5 continue10 continue do 15 i = 1, n write(*,*)(F(i, j), j=1, n)15 continue do 20 i = 1, n write(*,*)(F(j, i), j=1, n)20 continue25 continue stop end
45
Packed Storage
Packed form:It’s a useful data structure when dealing with
symmetric and triangular matrices,and only store the lower triangular portion in column-by-column fashion in a one-dimensional array.Ex.
k = 1
do 10 j = 1, n do 5 i = j, n c c a(k) = A(i, j) c
a(k) = i*j k = k+1 5 continue
10 continue
16
12
9
8
6
4
4
3
2
1
a
1
A
161284
12963
8642
432
46
Arrays of Higher Dimension It’s possible to manipulate arrays of dimension up to
seven.real A( 2, 3, 6, 2, 8, 7, 4)
means that there are 2x3x6x2x8x7x4=16128 memory locations are reserved.
Ex. Assign (abcd)2 to hcube(a,b,c,d) [binary to decimal] do 40 a = 0 , 1 do 30 b = 0 , 1 do 20 c = 0 , 1 do 10 d = 0 , 1
hcube(a,b,c,d)=8*a+4*b+2*c+d10 continue20 continue30 continue40 continue
47
1.5 SUBPROGRAMS
48
Built-in Functions
SpecificA specific function requires arguments of a particular type and returns a predefined type.
Ex. char(72) and we will have a string “H” Generic
A generic function accepts arguments of any appropriate type ;the return value is determined
by the types of the arguments.Ex. x = cos(y)
49
Functions I
Declaration{type} function {name} ({list-of-variables})
{Declaration}{statements}returnend
50
Functions II
Rules for user-defined function -The value produced by the function is returned to the
calling program via a variable whose name is the function’s name. Ex. f(x) is returned in the variable f
-A function has a type and it must be declared in the calling program.
-The body of a function resembles the body of a main program and the same rules apply.
-Execution begins at the top and flows to the bottom.Control is passed back to calling program when return statement is encountered the subroutine is exited.
-A reference to a defined function can appear anywhere in an expression,subject to type consideration.
51
Functions III
Solve the problem
program min1integer kreal m greater clarity m=-3.0e0do 10 k=1,10 x = real(k) m= min(m,((x-2.0)*x-7.0)*x-3.0)
10 continuewrite(*,*) mstopend
372)(
)}(),....0(min{),(23
xxxxf
nffnfm
real function f(x)real xf = ((x-2.0)*x-7.0)*x-3.0)returnend
program min2integer kreal m ,fm=f(0.0e0)do 10 k=1,10m= min(m,f(real(k) ) )
10 continuewrite(*,*) mstopend
52
Functions with Other Functions as Arguments
Solve the problem
program min3integer kreal m1,m2 m1=f1(0.0e0)do 10 k=1,10 greater clarity m1= min(m1,f1(real(k))
10continue m2=f2(0.0e0)do 20 k=1,20 m2= min(m2,f2(real(k))
20continuewrite(*,*) 'm(f1,10)=',m1,'m(f2,20)=',m2stopend
)20,( and )10,(
)}(),....0(min{),(
21 fmfm
nffnfm
real function fmin(f,n)integer nreal f fmin = f(0.0e0)do 10 k = 1,n fmin = min(fimn,f(real(k)))10 continuereturnend
program min4real m1,m2,f1,f2external f1,f2m1 =fmin(f1,10)m2 =fmin(f2,20)write(*,*) ‘m(f1,10)=’,m1,’m(f2,20)=’,m2stopend
53
Common I
When we want to solve problem for arbitrary n and arbitrary cubic ,we couldn't use fmin to compute m(f,n).The reason is that fmin expect a function with a single argument,not a function of the form f(x,a,b,c,d).So by placing variables “in common” they can be shared between the main program and one or more subprograms.
dcxbxaxxf 23)(
54
Common II
program min5
integer n
real m,a,b,c,d,f
common /coeff/ a,b,c,d
read (*,*)n,a,b,c,d
m = fmin(f,n)
write(*,*) m
stop
end
55
Common III The syntax for a common statement is as follows:
common /{ name } / { list-of-variables }different common block must have different names.
It’s legal for a variable to belong to more than one common block.
The variables listed in a common block are shared by all subprograms that list the block.
The common statement should be placed before any executable statements and declared in every subprogram using the common block.
The common variables don’t have to be named the same in each such routine, but they need to have same type and in the same order.
56
Subroutines I
Declarationsubroutine {name} ({argument list})
declaration{statements}end
A subroutine is called by a statement of the formcall { name } ({ argument list })
Unlike functions, subroutines do not have a type.The name of a subroutine does not return a value.
57
Subroutines II
subroutine fmin(f,n,value,point)integer n, pointreal f, valueinteger ireal temppoint = 0 value = f(0.0e0)do 10 i = 1,n temp = f(real(i)) if (temp .LT. value) then
point = i value = temp
endif 10 continue
returnend
program min6external finteger n,mptreal mvalread(*,*) n call fmin( f, n, mval, mpt)write(*,*)'m(f,n)=',mval,'mpt=',mpt stopend
58
Functions versus Subroutines I Every function can be put into “equivalent” subroutine form.
Ex. Prints program printreal a, b, c, d, x0, value, fread (*,*) a, b, c, d, x0value = f (a, b, c, d, x0)write (*,*) valuestopend
real function f(a, b, c ,d,x)real a, b, c, d, xf = ((a*x + b)*x + c)*x +dreturn end
dcxbxaxxf 23)(
59
Functions versus Subroutines II Change to “subroutine language”
program printreal a, b, c, d, x0, valueread (*,*) a, b, c, d, x0call f(a, b, ,c, d, x0, value)write (*,*) valuestopend
subroutine f(a, b, c ,d, x, value)real a, b, c, d, x, valuevalue= ((a*x + b)*x + c)*x +dreturn end
dcxbxaxxf 23)(
60
Save Ordinarily, the local variable values are lost when control
passed back to calling program.However, we can retain the value if it is named in a save statement.Ex. subroutine print(k)
integer kinteger lastksave lastkif (k .EQ. 0) then write (*,*) k lastk = kelseif (k .NE. lastk) then write (*,*) k lastk = k endifreturnend
61
Further Rules and Guidelines
Local variables exist only in the subprogram except they are named in a save statement.
Subprograms can be invoked by other subprogram as well as by the main program.
All functions used by a subprogram should be declared.This includes all specific build-in function.
To enhance readability ,there should be only one return in a subprogram.
Minimize the use of common.
62
1.6 ARRAYS AND
SUBPROGRAMS
63
Subprogram with Array Arguments I
A subroutine that performs matrix-vector multiplication
subroutine matvec(p, q, c, cdim, v, w ) integer p, q, cdim real v(*),w(*), C(cdim,*) integer i, j do 10 i = 1, p w(i) = 0.0e010 continue do 30 j = 1, q
do 20 i = 1, p w(i)=w(i)+c(i,j)*v(j)
20 continue30 continue return end
64
Subprogram with Array Arguments II
A main program that calls matvec:
integer idim, jdim parameter (idim = 50, jdim=40) integer i,j,m,n real A(idim,jdim), x(jdim),y(idim)
call matvec(m, n, A, idim, x, y)
stop end
65
Subprogram with Array Arguments III
The last dimension of an F77 array is not needed for address computation, so asterisks suffice.
Ex. The above subroutine , we claim some variables ,like
real v(*),w(*), C(cdim,*)
means v and w is one dimension array and C is two dimension array.
66
Different Dimensions I
The dimension of a passed array does not have to conform to its dimension in the calling program.
It’s perfectly legal to pass a two-dimensional array to a subprogram and then to treat it as one-dimensional in the subprogram.
67
Different Dimensions II
Ex.integer function prod(n,x,y)integer n,x(*),y(*)integer iprod = 0.do 10 i = 1, n prod = prod +x(i)*y(i)
10 continue return
end
Then calculate the AtA with A is an m*n matrix by function prodlength = m * nfrob2A = prod( length, A, A)
First of all ,We must sure m and adim have the same value.
68
Passing Submatrices
Ex.
then
161284
151173
141062
13951
A
1511
1410 )4:3 , 3:2(A
69
Stride
subroutine scale2(n , c, v, incv)integer n, incvreal c, v(*)integer j, k k=1do 10 j = 1, n
v(k)= c*v(k)k = k+incv
10 continuereturnend
suppose c and v(1,8) are initialized,then
call scale2(4 ,c ,v ,2) call scale2(4 ,c ,v(2) ,2) call scale2(2 ,c ,v(3) ,4)
scaled v(7) v(3),
scaled v(8)v(6),v(4), v(2),
scaled v(7)v(5),v(3), v(1),
70
A Note on Index Ranges
Declarationsubroutine sub2(x, p1, p2, A, q1, q2, r1, r2 ….)
integer p1, p2, q1, q2, r1, r2
real x(p1:p2), A(q1:q2,r1:r2)
Locationaddr[A(i, j)] = addr[A(q1, r1)] + (j-r1)*(q2-q1+1) +(i-q1)
71
Passing Arrays in Common Blocks Common blocks provide another way to communicate to
subprograms.[Like the global variable in C]
real function f(x)real u(100) , v(100)integer ncommon /fdate/ u, v, nreal s integer ks = 0.
do 10 k = 1 , n s = s + (u(k)*x – v(k)) ** 2
10 continuef = s- 1 returnend
common /fdate/ u, v, n
n integer
v(100) , u(100) real
mainprogram
72
1.7 INPUT AND OUTPUT
73
“ read” and “write” Statement
Declarationread ({unit number} , {format number}) {list-of-variables}write ({unit number} , {format number}) {list-of-variables}
The first argument indicates where the data is coming from or going to.
The second argument indicates the format of the data.
The asterisk(*) invokes certain convenient default options and it is at this simple level that we begin our discussion.
74
List-directed “read”
Declarationread ({unit number} , {format number}) {list-of-variables}
The format of data is “directed”( ) by the list of variables.Ex. read (*,*) i, j, m, x
read (*,*) y, z
if we set 10 , 5 , 6 , 1.0 -1.0 , 2.0
data items 10, 5 , 6 (i, j, m)are integer.data items 1.0, -1.0, 2.0(x, y, z) are real.
75
List-directed “write”
Declarationwrite ({unit number} , {format number}) {list-of-variables}
We can use single quotes(' ' ) to list the names of the variables along with their values.
Ex. suppose i=1,j=2,x=3,y=4,then when we usewrite(*,*) 'i=',i,',j=',j,',x=',x,',y=',y
and shows like this in the screeni = 1 , j = 2, x = 3 , y = 4
76
Formatted “read” read(*,100) i, j, k
100 format(I6, I6, I6)
There are three integer fields per line,each of the form “I6”.
“6” means six spaces wide. The spaces is includes sign bit.It means that we can’t show
-123456 with ‘I6’.
read(*,100) x, y100 format( E9.1, E10.3)
“E9.1” designates a floating number with length 9 bit including 1 bit mantissa.
It will show asterisk(*) when we don’t allow enough spaces.
77
Formatted “write”
write(* , 101)
write(* , 101)
101 format (1x,//, ' Upon termination : ')
102 format (1x,//, 2x, 'x= ', F8.4, 2x, 'y= ',
& F.4, 2X, 'i= ', I5, 2x, 'j= ', I5)
The slash(/) cause a blank line to be printed. Single quotes are used for names. ‘iX’ indicates that i spaces are to be skipped’
78
Location of “format” Statements
Collecting all the format statements in the program and placing them at the end just before and the end statement.
We recommend labels for all the format statements that appear in a program and encourage the reader interested in the formats to look for their specification at the end of the program.
79
Some Shortcuts
100 format (2x, I2, 2x, I2, 2x, I2)
100 format (3 (2x, I2) )
100 format (I5, I5, /E10.2/, /E10.2/)
100 format (2I5, 2(/E10.2), /)
80
The “data” Statement I
Declarationdata {list-of-variables /{list-of -values}/,…..
Ex. Show the following assignment:n=100, m=-5, x=2.0 ,y=2.0 ,z=2.0
data n/100/, m/-5/, x/2.0/, y/2.0/, z/2.0/data n,m/100,-5/, x,y,z/3*2.0/
81
The “data” Statement II
Ex. Write a code to assign 4-by-3 matrix of ones to A,set the first row of B to [1,2], assign a 4-vector of
zeros to c, and sets the second component of d to 1.
integer idim, jdim, kdim
parameter (idim = 4, jdim = 3, kdim = 2)
real A(idim, jdim), B(kdim, kdim), c(idim), d(kdim)
data A/12 *1.0/, B(1,1)/1.0/
data B(1,2)/2.0/, c/4*0.0/ , d(2)/1.0
A/12 *1.0/ means that matrix A has 12 ones in it.
82
Input and Output of Arrays It’s convenient to read in a m*n matrix :
read (*,*) ((A(i,j), i=1,m) , j=1,n)Ex. read (*,10) ((A(i,j), i=1,5) , j=1,6) 10 format (5 I3)
The example is the same with:j= 1,6 i=1,5
write (*, 10) A10 format (6 F7.4)
which results in 6 numbers per line.If A /1,2,3,4,5,6,7,8,9/ , it shows 1 2 3 4 5 6 -line1
7 8 9 -line2
83
1.8 COMPLEX ARITHMETIC
84
Declaring Complex Variables
Just as the complex number z = x+iy (i2 = -1), and ordered pair (x,y) of real numbers.The declaration
complex z Complex arrays are also possible.
Because complex variables take up twice as much space as real variables, memory constraints sometimes pose a problem when large complex array are involved,
85
Useful Built-in Functions
In a typical situation one often has to ‘set up’ a complex number from two real numbers.
z = complex (x,y)
assigns x+iy to z.Likewise,
x = real (z)
y = aimag(z)
86
Using Complex Variables
Complex variable can be manipulated just as can real and double precision variables.Ex. p = z**2 +z
is equivalent to x = real(z)
y = aimag(z)u = x*x – y*y + xv = 2.0e0*x*y+ yp = complex(u,v)
87
Input/Output
If z is complex write(*,100) z
100 format(‘real(z)=’, f10.7, ‘imag(z)=’, f10.7)
is equivalent tox = real(z)
y = aimag(z)
100 format(‘real(z)=’, f10.7, ‘imag(z)=’, f10.7)
where x and y are real.
88
Avoiding Complex Arithmetic Consider the following function that compute the absolute value
of the large root of the real quadratic equation x2+2bx+c = 0.real function maxrt( b, c ) real b, c d = b*b - c if (d .GE. 0.0e0) then maxrt = abs ( -b + sign(sqrt(d),-b)) else maxrt = sqrt ( b*b - d) endif
return end
this is preferable to the coded= csqrt( complex (b*b +cc,0.0e0)) maxrt = max (cabs(-b+d),cabs(-b-d))
csqrt(a) compute the square root of a,and return complex value.
89
1.9 PROGRAMMING TIPS
90
Intermediate Variables for Clarity and Efficiency I
Ex. If d2 = [r sin(b1) cos(a1) - r sin(b2)cos(a2)]2 +
[r sin(b1) sin(a1) - r sin(b2)sin(a2)]2 +
[r cos(b1) - r cos(b2)]2
Here are three ways to compute d. First: a straight encoding of the formula
d = sqrt (
& ( r*sin(b1)*cos(a1) - r*sin(b2)*cos(a2) )**2 +
& ( r*sin(b1)*sin(a1) - r*sin(b2)*sin(a2) )**2 +
& ( r*cos(b1) - r*cos(b2) )**2 )
91
Intermediate Variables for Clarity and Efficiency II
Second : Use some common subexpressions.
s1 = sin(b1)
s2 = sin(b1)
xdist = s1 * cos(a1) - s2*cos(a2)
ydist = s1 * sin(a1) - s2*sin(a2)
zdist = cos(b1) - cos(b2)
d = r*sqrt ( xdist **2 + xdist**2 + xdist**2)
92
Intermediate Variables for Clarity and Efficiency III
Third : Use some trigonometric identities.
t = cos(a1-a2)
d = r*sqrt (1. – t*cos(b1-b2) -
& cos(b1)*cos(b2)*(1. - t))
The third method is more difficult to read than first and second, and it could be addressed with sufficient comments.It is readability and efficiency ,but is difficult to reconcile.
93
Intermediate Variables for Clarity and Efficiency IV
A temporary array can lead to a more efficient computation.
For example, to set up n*n matrix F with fij = exp(-i –0.5j)
do 10 j=1,n
x(j) = exp(-float(j))
y(j) = exp(-.5 *float(j))
10 continue
do 20 j = 1,n
do 15 i = 1,n
F(i,j) = x(i) * y(j)
15 continue
20 continue
94
Checking Input Parameters
Make sure that the range is permissible.mmult(A, adim, ma, na, BB, bdim, mb, nb, C,
cdim)
if ( na .NE. mb) then
write(*,*) 'Product AB not defined'
return
elseif ( cdim .LT. ma)
write(*,*) 'Array C not big enough'
return
endif
95
Test Most Likely Conditions First
Depend on the comparison permutation
do 10 j=1,n do 5 i= 1,n
if (i .LT. j)then A(i,j)=-1elseif (i .GT. j)then
A(i,j)=0elseif (i .EQ. j)
A(i,j)=1 endif 5 continue10 continue
Avoid all ‘i-j’ comparison do 10 j = 2,n do 5 i =1,j-1 A(i,j) = -1 5 continue 10 continue do 20 j= 1,n A(j,j) = 1 20 continue do 30 j = 1,n-1 do 25 i = j+1,n A(i,j) = 0 25 continue 30 continue
Arrange the conditions in decreasing order of likelihood to minimize execution time.
96
Integer Arithmetic In order to reduce the amount of subscripting, we could do
two.1. Build the running sum in a scalar variable to save space.2. Let alphabets substitute into some fixed variable.
do 30 k =0 ,n-1 s(k) = 0. do 10 i = 0,n-k+1
s(k) = s(k) + x(i)*y(i+k) 10 continue
do 20 i = n-k, n-1 s(k) = s(k) +x(i)*y(i-n+k)
20 continue 30 continue
do 30 k =0 ,n-1 t = 0.
do 10 i = 0,n-k+1 t = t + x(i)*y(i+k)
10 continue kmn = k - n do 20 i = n-k, n-1
t = t +x(i)*y(i+kmn)
20 continue
s(k) = t 30 continue
97
Generality versus Efficiency I Efficient case :
real function dot1(n,x,y) integer n real x(*), y(*) integer i real s s = 0 do 10 i=1,n s = s +x(i)*y(i)
10 continue dot1=s return end
98
Generality versus Efficiency II General case :
real function dot2(n, x, incx, y incy) integer n, incx, incy real x(*), y(*) integer i, ix, iy real s ix = 1 iy = 1 s = 0 do 10 i = 1, n s = s + x(ix)*y(iy) ix = ix + incx iy = iy + incy
10 continue dot2 = s return end
99
Generality versus Efficiency III
Clear case :real function dot2(n, x, incx, y incy) integer n, incx, incy , i, ix, iy real x(*), y(*) ,s s = 0 if ( incx .EQ. 1 .AND. incy .EQ. 1 ) then do 5 i = 1, n
s = s + x(ix)*y(iy) continue else ix = 1 iy = 1 do 10 i = 1,n
s = s + x(ix)*y(iy) ix = ix + incx iy = iy + incy
continue endif
dot3 = s
return
end
100
Machine-independent Testing For Roundoff Noise I The unit roundoff u in a floating point system that
has t-bit mantissas is defined by u=21-t.We have some method to check the precision of the machine.
1.
But it only works in 59-bit floating point system.
endif
else
y)then*if(x.LT.u
55))*(*2uparameter(
101
Machine-independent Testing For Roundoff Noise II
2. A better solution is :
endif
else
y)then .GT. if(z
yxz
102
Termination Criteria I
real function exp1(x,tol)real x, tolreal s, terminteger ks = 1term = xdo 10 k = 1,30
s = s + term if ( abs(term) .LT. tol*abs(s) ) exp1 = s return endif term = term*(x/real(k))
10 continueexp1 = sreturnend
103
Termination Criteria II Some things wrong with above program.
1.’indefinite termination’ is better to use the while construct
2.’tol’ may underlie machine precision.
10 if ( k .LE. kmax .AND. big ) then
s = snew
k = k+1
term = term*(x/float(k))
snew = s +term
big = term .GE. tol*s .AND. snew .NE. s
goto 10
endif
exp2 = s
return
end
real function exp1(x,tol)
real x, tol , s, snew
integer kamx, k
parameter (kmax = 30)
logical big
s = 1.
k = 0
term = x
snew = s + term
big = term .GE. tol*s .AND. snew .NE. s
104
Computing Small Corrections
It’s better to compute the midpoint m of a and b,withm = a-(b-a)/2
rather than with the formulam = (a+b)/2
where a and b are nearby floating point numbers.
105
Pay Attention to the Innermost Loops Ex. We want to evaluate the polynomial
p(x) = a0+a1x2+a2x4+…+anx2n
using Horner’s rule.
s= a(n)do 10 k = n-1:-1:0 s = s*z*z + a(k)
10 continue
we add a number ‘w’ by getting the ‘z*z’ computation outside the loop
s= a(n)w = z*zdo 10 k = n-1:-1:0 s = s*w + a(k)
10 continue
106
Guarding against Overflow I Consider the computation of a cosine-sine pair(c,s) such
that –sx+cy=0, where x and y are given real numbers. A naïve solution:
d = sqrt ( x**2 + y **2 ) if d .GT. 0 c = x/d
s = y/d else c = 1 s = 0 endif
Overflow might occur in the computation of d
107
Guarding against Overflow II
A scaled versions of x and y:m = abs(x) + abs(y)
if m .NE. 0 then
x = x/m
y = y/m
d = sqrt( x*x + y*y )
c = x/d
s = y/d
else
c = 1
s = 0
endif
108
Guarding against Overflow III
Simplify scaled versions of x and y:if abs(y) .GT. abs(x) t = x/y s = 1./sqrt(1 + t*t) c = s*t elseif (abs(x) .GT. 0.) t = y/x c = 1./sqrt(1. + t*t) s = t*c else c = 1 s = 0 endif
109
Column-oriented versus Row-oriented Algorithm
Consider the matrix-vector multiplication y=Ax,where A is an m*n matrix and x is an n-vector. Compute y as follows
do 10 i = 1,m
y(i) = 0.
10 continue
do 30 j = 1,n
do 20 i = 1,m
y(i) = y(i) + A(i,j)*x(j)
20 continue
30 continue
The inner loop is row index , because arrays are stored by column in Fortran
110
2.1 BOOKKEEPING OPERATIONS
111
SCOPY and SSWAP SCOPY
call SCOPY (n, x, incx, y, incy)
ex. call SCOPY(15, x(10),1,y(20),1)y(19+1*i) x(9+1*i) i= 1:15
SSWAPcall SSWAP (n, x, incx, y, incy)
ex. call SSWAP(10, x(20),3,y(2),2)x(17+3*i) <--> y(2*i) i= 1:20
n : the loop from 1 to n x(index) : a vector starts from indexincx,incy : increment number
112
Example 2.1-1 MATRIX COPY
subroutine MATCOP ( m, n, B, bdim, A, adim )
integer m, n, bdim, adim
real B(bdim,*), A(adim,*)
integer k
do 10 k = 1,n
call SCOPY( m, B(1,k), 1, A(1,k), 1)
10 continue
return
end
113
Example 2.1-2 MATRIX TRANSPOSE
subroutine TRANSP( n, A, adim )
integer n, adim
real A(admin,*)
integer k
do 10 k= 1, n-1
call SSWAP( n-k, A(k+1,k), 1, A(k,k+1), adim)
10 continue
return
end
114
Example 2.1-3 CIRCULANT MATRIX
subroutine CIRCUL( n, C, cdim, v )
integer n, cdim
real C(cdim,*), v(*)
integer k
call SCOPY (n,v(1),1,C(1,1),1)
do 10 k = 2,n
call SCOPY( n-1, C(2,k-1), 1, C(1,k), 1)
C(n,k) = C(1,k-1)
10 continue
return
end
115
2.2 VECTOR OPERATIONS
116
Scaling Vectors SSCAL
To product of the form a*x, where a is a scalar and x is a vector:
call SSCAL (n, a, x, incx )
ex. call SSCAL (50, a, x(2),2)
x(2i) a*x(2i) i= 1:50
117
Adding a Multiple of One Vector to Another
SAXPYPerform the operation y a*x + y,where a is a scalar and x and y are vectors:
call SAXPY (n, a, x, incx, y, incy)
ex. call SAXPY (5, a, x(3), 2, y(10), 4) y(6+4i) a*x(1+2i) + y(6+4i)
i= 1:5
118
Inner Products SDOT
To compute the value of inner product:w = SDOT (n, a, x, incx, y, incy)
ex. W = call SDOT (50, x, 1, y, 1) w = x(1)y(1) + … + x(50)y(50)
ex. W = call SDOT (50, x, 2, y, 1) w = x(1)y(1) + x(3)y(2) + … + x(50)y(50)
+2
119
Example 2.2-1 Univariate least squares subroutine LS( m, a, b, x ) integer m real a(*), b(*), xreal SDOTx = SDOT( m, a, 1, a, 1 ) if ( x .NE. 0.0) then
x = SDOT( m, a, 1, a, 1 ) call SAXPY( m, -x, a, 1, b, 1) call SSCAL( m, x, a, 1)
endifreturn end
120
Example 2.2-2 Skew and symmetric Parts
subroutine SYMSKU( n, A, adim ) integer n,adim real A(adim, *) real half integer k,kpl,nmk parameter( half = .50e0) do 10 k = 1, n-1
kp1 = k + 1nmk = n – kcall SAXPY( nmk, 1., A(kp1,k), 1, A(k,kp1), adim )call SSCAL( nmk, half, A(k,kpl),adim ) call SAXPY( nmk, -1., A(k,kp1), adim, A(kp1,k), 1 )
10 continue return end
121
Example 2.2-3 Matrix-Vector Multiplicationsubroutine MMULT( m, n, A, adim, x, y, job ) integer m, n, adim, job real A(adim,*), x(*), y(*) real SDOT integer i,j if (job .EQ. 0) then do 10 i = 1,m
y(i) = 0.0 10 continue
do 20 j = 1,n call SAXPY( m, x(j), A(1,j), 1, y(1), 1)
20 continue else
do 30 j = 1,n y(j) = SDOT ( m, A(1,j), 1, x(1), 1)
30 continue endif return end
122
Example 2.2-4 Krylov Matrix
subroutine KRYLOV( n, A, adim, K, kdim, v, j )
integer n, adim, kdim, j
real A(adim,*), K(kdim,*), v(*)
integer p
call SCOPY( n, v(1), 1, K(1,1), 1 )
do 10 p = 2,j
call MMULT( n, n, A, adim, K(1,p-1), k(1,p), 0 )
10 continue
return
end
123
2.3 NORM COMPUTATIONS
124
1-Norms and 2-Norms SNRM2 & SASUM
The 2-norm of a real n-vector x :sw = SNRM2 (n, x, incx)
The 1-norm of a real n-vector x :sw = SASUM (n, a, x, incx)
ex. sw = SNRM2 (20, x, 1) w sqrt( |x(1)|2 + … + |x(20)|2 )
ex. sw = SASUM (20, x, 1) w |x(1)| + … + |x(20)|
125
Maximal Entry ISAMAX Find the index of element having max. absolute value:
imax = ISAMAX(n, x, incx)
ex. If x = (2, -1, 0, 6, -7)
imax = ISAMAX(5, x, 1)
imax = 5
imax = ISAMAX(2, x(2), 2)
imax = 2
126
Example 2.3-1 Vector Infinity-Norm
real function NORMI(x,n)
integer n
real x(*)
integer ISAMAX
real abs
NORMI = abs( x( ISMAX( n, x, 1) ) )
return
end
127
Example 2.3-2 Frobenius Matrix Norm
real function SNORMF( m, n, A, adim ) integer m, n, adim real A(adim,*)real v(2), SNRM2 integer k SNORMF = 0.0 do 10 k = 1,n
v(1) = SNORMF v(2) = SNRM2( m, A(1,k), 1 ) SNORMF =SNRM2 ( 2, v(1), 1 )
10 continue return
end
128
Example 2.3-3 Matrix 1-Norm
real function SNORM1( m, n, A, adim ) integer m, n, adim real A(adim,*) real s, SASUM integer k SNORM1 = 0.0 do 10 k = 1,n s = SASUM( m, A(1,k), 1 ) SNORM1 = max( s, SNORM1)
10 continue return end
129
Example 2.3-4 Householder Vector
subroutine HOUSE( n, x )
integer n
real x(*)
real alfa,beta,x1,SNRM2, abs
if (alfa .NE. 0.0) then
x1 = x(1)
x(1) = x1 + sign(alfa,x1)
beta = sqrt( 2.0 * alfa * (alfa + abs(x1)) )
call SSCAL( n, 1.0/beta, x(1), 1 )
else
x(1) = 1
endif
return
end
130
2.4 GIVENS ROTATIONS
131
Constructing a Givens Rotation I
SROTGIt can be used to construct a Givens rotation for the purpose of zeroing the second component of a prescribed 2-vector.
call SROTG ( a, b, c, s )
a, c ,and s :
b : if (a>b) then b=sesleif (b>a and c<>0) b=1/celse b=1
1sc0
r
b
a
cs
sc22
132
Constructing a Givens Rotation II
Consider an example: x= ( 1, 4, 7, 3 )We have : call SROTG( x(2), x(4), c, s)
5
3s
5
4c
5
34x52x
ba5
3sb
a5s3c4r5
4c
5
3s
1sc
0c3s4
rs3c4
1sc0
r
3
4
cs
sc
22
22
,,)(,)(
)(
)(
,
負不合
133
Reconstructing a Givens Rotation
subroutine ZTOCS( z, c, s ) real z,c,s if ( abs(z) .LT. 1 ) then
s = z c = sqrt(1. - s*s)
elseif ( abs(z) .GT. 1 ) then c = 1./z s = sqrt(1. - c*c)
else c = 0. s = 1.
endif return end
134
Applying a Givens Rotation SROT
call SROT(n, x, incx, y, incy, c, s)
means
call SROT(n, A(p,1), adim, A(q,1), adim, c, s)
call SROT(m, A(1,p), 1, A(1,q), 1, c, s)
cs
scyxyx
AscqpJA ),,,(
),,,( scqpAJA
135
Example 2.4-1 Zeroing an n-Vector
subroutine ZERO(n,x)
integer n
real x(*)
real c,s
integer k
do 10 k = n-1,1,-1
call SROTG(x(k),x(k+1),c,s)
10 continue
return
end
136
Example 2.4-2 Factored Q Times Vector
subroutine APPLY( n, x, y )
integer n
real x(*), y(*)
real c, s
integer k
do 10 k = n-1, 1, -1
call ZTOCS( x(k+1), c, s )
call SROT( 1, y(k), 1, y(k+1), 1, c, s )
10 continue
return
end
137
Example 2.4-3 Hessenberg QR Factorization
subroutine HESS( n, A, adim)integer n, adimreal A(adim,*)
real c, s integer j, jp1 do 20 j = 1, n-1
jp1 = j + 1call SROTG( A(i,j), A(jp1, j), c, s )
call SROT( n-j, A(j,jp1), adim, & A(jp1,jp1), adim, c, s)20 continue
return end
138
2.5 DOUBLE PRECISION ANDCOMPLEX VERSIONS
139
Copying a Vector call SCOPY(n, sx, incx, sy, incy)
call DCOPY(n, dx, incx, dy, incy)call CCOPY(n, cx, incx, cy, incy)call ZCOPY(n, zx, incx, zy, incy)
s : real single precisiond : real double precisionc : complex single precisionz : double precision
140
Interchanging Vectors call SSWAP(n, sx, incx, sy, incy)
call DSWAP(n, dx, incx, dy, incy)call CSWAP(n, cx, incx, cy, incy)call ZSWAP(n, zx, incx, zy, incy)
s : real single precisiond : real double precisionc : complex single precisionz : double precision
141
Multiplying a Vector by a Scalar
call SSCAL(n, sa, sx, incx)call DSCAL(n, da, dx, incx)call CSCAL(n, ca, cx, incx)call ZSCAL(n, za, zx, incx)
Complex casecall CSSCAL(n, sa, cx, incx)call ZDSCAL(n, da, zx, incx)
142
Inner Product sw = SDOT(n, sx, incx, sy, incy)
dw = DDOT(n, dx, incx, dy, incy)
complex casefor xHy :
cw = CDOTC(n, cx, incx, cy, incy)zw = ZDOTC(n, zx, incx, zy, incy)
for xTy : cw = CDOTU(n, cx, incx, cy, incy)zw = ZDOTU(n, zx, incx, zy, incy)
143
Adding a Multiple of One Vector to Another
call SAXPY(n, sa, sx, incx, sy, incy)
call DAXPY(n, da, dx, incx, dy, incy)
call CAXPY(n, ca, cx, incx, cy, incy)
call ZAXPY(n, za, zx, incx, zy, incy)
144
2-Norm sw = SNRM2(n, sx, incx)
dw = DNRM2(n, dx, incx)
sw = SCNRM2(n, cx, incx)
dw = DZNRM2(n, zx, incx)
145
1-Norm sw = SASUM(n, sx, incx)
dw = DASUM(n, dx, incx)
sw = SCASUM(n, cx, incx)
dw = DZASUM(n, zx, incx)
146
Maximal Entry imax = ISAMAX(n, sx, incx)
imax = IDAMAX(n, dx, incx)
imax = ICAMAX(n, cx, incx)
imax = IZAMAX(n, zx, incx)
147
Constructing a Givens Rotation
call SROTG( sa, sb, sc, ss)
call DROTG( da, db, dc, ds)
148
Applying a Givens Rotation call SROT(n, sx, incx, sy, incy, sc, ss)
call DROT(n, dx, incx, dy, incy, dc, ds)
call CSROT(n, cx, incx, cy, incy, sc, ss)
call ZDROT(n, zx, incx, zy, incy, dc, ds)
149
Example 2.5-1 Complex Rotation
subroutine SCROTG( ca, cb, sc1, ss1, sc2, ss2)complex ca,cbreal sc1,ss1,sc2,ss2 real a1, a2, b1, b2
real real aimag a1 = real(ca)a2 = aimag(ca)b1 = real(cb)b2 = aimag(cb) call SROTG( a1, a2, sca, ssa )
call SROTG( b1, b2, scb, ssb )call SROTG( a1, b1, sc1, ss1 )
sc2 = sca*scb + ssa*ssb ss2 = ssa*scb - sca*ssb return end
150
3.1 TRIANGULAR SYSTEMS
151
Solving Triangular Systems I STRSL
It can be used to solve triangular systems of the form Tx=b and Ttx=b,where T is either upper or lower triangular.
call STRSL( T, tdim, n, b, job, info )
[Input]T : The triangular matrix T.tdim : The row dimension of the array T.n : The dimension of matrix T.b : The right-hand side.job : Indicate the type of system to be solved.
[Output]b : If T is nonsingular.Otherwise unchanged.info : Return a singularity flag.
152
Solving Triangular Systems II [job]
00 solve Tx=b, where T is lower triangular.
01 solve Tx=b, where T is upper triangular.10 solve Ttx=b, where T is lower triangular.11 solve Ttx=b, where T is upper triangular.
[info]1. T is nonsingular, info is zero.2. T is singular , info returns the smallest k such that tkk is zero.
153
Solving Triangular Systems III
EX: To solve an upper Triangular system
program ANSWER1 real T(3,3),B(3,1) DATA T /1,0,0,2,1,0,3,-1,2 / DATA B /14,-1,6 /
integer info call strsl(T,3,3,B,01,info) ! info = singular flag & B = T**(-1)*b
write(*,10) info,B10 format (1X,I1,/, G10.3, G10.3, G10.3)
stop end
6
1
14
x
x
x
200
110
321
3
2
1
154
Condition Estimation STRCO
It can predict that a matrix may be close to a singular matrix. If so, it will return a ‘unmeaning and sufficiently small value’ rcond.
call STRCO( T, tdim, n, rcond, z, job )
[Input]T : The triangular matrixtdim : The row dimension of the array T.n : The dimension of matrix T.job : Indicate whether T is upper or lower
triangular( 0=lower, 1=upper ) .
[Output]rcond : Return estimate of 1/k1(T).
z : Return 1-norm z so ||Tz||1 = rcond||z||1.
155
Determinant and Explicit Inverse I
STRDITo solve the inverse or determinant of triangular matrix.
call STRDI( T, tdim, n, d, job, info )
[Job]It contains three-digit integer ABC.A :if A=1, We'll compute the determinant of T. A=0, No determinant computation.B :if B=1, compute the T-1
B=0, No inverse computation.C :if C=1, T is upper triangular. if C=0, T is lower triangular.
156
Determinant and Explicit Inverse II
EX: Solve the Determinant and inverse of
program teststrdi real T(2,2),d(2,1) DATA T /3,0,4,2 /
integer info call strdi(T,2,2,d,111,info)
write(*,10) info ! singular flagwrite(*,20) d(1,1)*10**d(2,1)!determinantwrite(*,30) T ! inverse of T
10 format (1X,I2)20 format (1X,G10.3)30 format (1X,G10.3, G10.3, G10.3, G10.3)
stop end
20
43
157
Example3.1-1 Multiple Right-hand Sides
subroutine TINVB( T, tdim, n, B, bdim, p, info, rcond, z )integer tdim, n, bdim, p, inforeal T(tdim,*), B(bdim,*), rcond, z(*)integer jreal rcond1call STRCO( T, tdim, n, rcond, z, 1 )rcond1 = 1.0e0 + rcondif ( rcond1 .EQ. 1.0e0 ) then
info = 1else
info = 0do 10 j = 1, p
call STRSL( T, tdim, n, B(1,j), 01, info )10 continue
endifreturnend
158
3.2 General SYSTEMS
159
LU Factorization I SGECO
It computes the factorization PA=LU via Gaussian elimination with partial pivoting.
call SGECO( A, adim, n, ipvt, rcond, z )
[Input]A : n*n matrix.adim : The row dimension of A.n : The dimension of the matrix A.
[Output]A : Return L and U.ipvt : In order to compute P.rcond : Return estimate of 1/k1(T).
z : Return 1-norm z so ||Tz||1 = recond||z||1.
160
LU Factorization II SGEFA
If the condition is not required ( factorization PA=LU), we can use SGEFA instead of SGECO to save time.
call SGEFA( A, adim, n, ipvt, info )
[info] = 0 : No zero pivots encountered. Use SGESL , SGEDI safely.= k : Ukk=0, k is the address of the last zero element.
161
Back-substitution/Forward-elimination
SGESLTo solve either the system Ax=b or Atx=b.
call SGESL( A, adim, n, ipvt, b, job )
[Input]A : Contain A’s LU decomposition from SGECO or SGEFA.ipvt : The pivot vector from SGECO.b : The right-hand side.job := 0 , solve Ax=b. = 1 , solve Atx=b.
[Output]b : Return solution to requested system.
162
Explicit Inverse and Determinate I
SGEDICalculate inverse and determinant of A.
call SGEDI( A, adim, n, ipvt, d, work, job )
[Input]
job : = 01 , Only inverse. = 10 , Only determinant. = 11 , inverse and determinant.
[Output]A : if job=0, A=A. Otherwise A= A-1.d : if job=x1,return a two-vector determinant
d=d(1)*10.0**d(2).
163
Explicit Inverse and Determinate II
EX: Solve the inverse of the matrix( use SGECO , SGEDI)
program testLU real A(3,3),rcond,z(3,1),work(3,1),d(2,1)
integer ipvt(3,1) DATA A /1.0, 1.0, 1.0, 3.0, 3.0, 4.0, 3.0, 4.0, 3.0/ call sgeco(A,3,3,ipvt,rcond,z) !PA = LU
call sgedi(A,3,3,ipvt,d,work,11)write(*,10) ((A(i,j),j=1,3),i=1,3) !inverse of Awrite(*,20) ipvtwrite(*,30) d(1,1)*10**d(2,1) !determinant of A
10 format (1X,G20.3, G20.3, G20.3,/,G20.3, G20.3, G20.3,/, G20.3, G20.3, G20.3)
20 format (1X,I1, I1, I1)30 format (1X,G10.3) stop end
341
431
331
164
Example3.2-1 Ax=b Solver with Condition Estimate
subroutine SGSOL( A, adim, n, ipvt, rcond, z, b, info)integer adim, n, ipvt(*), info real A(adim,*), b(*), rcond, z(*) real rcond1 call SGECO( A, adim, n, ipvt, rcond, b ) rcond1 = rcond + 1.0e0 if ( rcond1 .GT. 1.0e0 ) then
info = 0 call SGESL( A, adim, n, ipvt, b, 0 )
else info = 1
endif return end
165
3.3 SYMMETRIC SYSTEMS
166
Cholesky Factorization I SPOCO
To compute the Cholesky factorization A=RtR of a symmetric positive definite matrix.
CALL SPOCO (A, adim, n, rcond, z, info)
[Input]info := 0 , R is found.
> 0 , A is not positive definite.
167
Cholesky Factorization II SPOFA
If the condition is not required(symmetric positive definite matrix), we can use SPOFA instead of SPOCO to save time.
CALL SPOFA (A, adim, n, info)
168
Cholesky Factorization III SPOSL
The output of either SPOCO or SPOFA can be used(info=0) to solve Ax=b. Here we can use SPOSL to solve it.
CALL SPOSL (A, adim, n, b)
[Input]A : Either the matrix of SPOCO or SPOFA.info : = 0 , R is found.
> 0 , A is not positive definite.
169
Example 3.3-1 Symmetric Positive Definite System Solver
subroutine SPOSOL( A, adim, n, b, rcond, z, info )integer n, adim, info real A(adim,*), b(*), rcond, z(*) real rcond1 call SPOCO( A, adim, n, rcond, z, info ) rcond1 = rcond + 1.0e0 if ( info .EQ. 0 .AND. rcond, z, info ) then call SPOSL( A, adim, n, b ) elseif( info .EQ. 0 .AND. rcond1 .GT. 1.0e0) then
info = -1 endif return end
170
Diagonal Pivoting Method I SSICO
To solve symmetric indefinite systems with A =UDUt.
CALL SSICO(A, adim, n, ipvt, rcond, z) [Input]
A : the symmetric matrix. [Output]
A : U and D.ipvt : the pivot information.rcond : 1/k1(A) estimate.
171
Diagonal Pivoting Method II SSIFA
If the condition estimate is not desired then use :
CALL SSIFA( A, adim, n, ipvt, rcond, k)
[Output]k : = 0, A is nonsingular.
> 0, kth diagonal block of D is singular.
172
Diagonal Pivoting Method III SSISL
After the output of either SSICO or SSIFA can be solved the symmetric indefinite systems Ax=b.
CALL SSISL( A, adim, n, ipvt, b)
[Input]A : from SSICO or SSIFA.b : the right-hand side.
[Output]ipvt : the pivot information from SSICO or SSIFA.b : A-1b.
173
Example3.3-2 Symmetric Indefinite System Solver
subroutine SSISOL( A, adim, n, ipvt, rcond, z, b, info)integer adim, n , ipvt(*), info real A(adim,*), b(*), rcond, z(*) real rcond1 call SSICO( A, adim, n, ipvt, rcond, z ) rcond1 = 1.0e0 + rcondif(rcond1 .GT. 1.0e0) then
info = 0 call SSISL( A, adim, n, ipvt, b )
else info = -1
endif return end
174
Inverses,Determinant,Inertia I
SPODIAfter the Cholesky factorization has been calculated via SPOCO or SPOFA, then we can use SPODI to solve the inverse and/or determinant of a symmetric positive matrix.
call SPODI ( A, adim, n, det, job )
[Input] job : =11, inverse and determinant .
=10, determinant . =1, inverse . [Output]
A : If job=1 or 11 , A will return the upper triangular matrix.
175
Inverses,Determinant,Inertia II
SPIDILike the SPODI, but in an indefinite case.
call SPIDI ( A, adim, n, kpvt, det, inert, work, job )
[Output]inert : an integer three-vector
inert (1) = the number of positive eigenvalues.
inert (2) = the number of zero eigenvalues. inert (3) = the number of negative
eigenvalues.
176
Packed Storage We could store upper triangular matrix column by
column . See 1.4
Packed-storage version of SPOFA, SPODI, SSIFA, and SSIFI also exist.
Packed version Conventional version
SPPCO( AP, n, rcond, z, info)SPPSL( AP, n, b)SSPCO( AP, n, ipvt, rcond, z)SSPSL( AP, n, ipvt, b)
SPOCO( A, adim, n, rcond, z, info)SPOSL( A, adim, n, b)SSICO( A, adim, n, ipvt, rcond, z)SSISL( A, adim, n, ipvt, b)
177
Cholesky with Pivoting SCHDC
Decomposition PAPt = GGt, where A is symmetric positive semidefinite and P is a permutation matrix.
call SCHDC ( A, adim, n, work, ipvt, job, info )
[Input]job : =0 without change pivot =1 otherwise
178
Updating and Downdating the Cholesky Factorization
SCHUD . SCHDD . SCHEXSuppose the Cholesky factorization A = GGt and x is n-vector.We can use a fast subroutine SCHUD(or SCHDD) to compute Cholesky factorization.
call SCHUD (R,adim,P,X,Z,LDZ,NZ,Y,RHO,c,s)call SCHDD
(R,adim,P,X,Z,LDZ,NZ,Y,RHO,C,S,INFO)
SCHUD -> A+=A+XXt
SCHUD -> A-=A-XXt
The subroutine SCHEX is frequently used in conjunction with SCHUD and SCHUD.
179
3.4 BANDED SYSTEMS
180
Band Gaussian Elimination Here are the corresponding banded versions(Full
general case in 3.2):
Full : SGECO( A, adim, n, ipvt, rcond, z )Banded: SGBCO( A, adim, n, p, q, ipvt, rcond, z )
Full : SGEFA( A, adim, n, ipvt, info )Banded: SGBFA( A, adim, n, p, q, ipvt, info)
Full : SGESL( A, adim, n, ipvt, b, job )Banded: SGBSL( A, adim, n, p, q, ipvt, b, job )
Full : SGEDI( A, adim, n, ipvt, b, job )Banded: SGBDI( A, adim, n, p, q, ipvt, b, job )
181
Band Cholesky Here are the banded analogs(Full positive
definite system case in 3.3):
Full : SPOCO( A, adim, n, rcond, z, info )Banded: SPBCO ( A, adim, n, q, rcond, z, info )
Full : SPOFA( A, adim, n, info )Banded: SPBFA( A, adim, n, q, info )
Full : SPOSL( A, adim, n, b )Banded: SPBSL( A, adim, n, q, b )
Full : SPODI( A, adim, n, det, job )Banded: SGECO( A, adim, n, q, det )
182
Tridiagonal Systems I SGTSL
Consider the tridiaginal system:
we can invoke SGTSL to solve linear system by using Gaussian elimination with partial pivoting.
call SGTSL( n, c, d, e, b, info )
n
1n
2
1
n
1n
2
1
nn
1n1n1n
22
11
b
b
b
b
x
x
x
x
dc
edc
dc
ed
183
Tridiagonal Systems II SPTSL
If the matrix A is symmetric positive define, then we use SPTSL.
call SPTSL( n, d, e, b)
b is overwritten with the solution to Ax = b.
184
Example 3.4-1 Symmetric Positive Definite Band Solver
subroutine SPBSOL ( A, adim, n, q, rcond, z, b, info )
integer n, q, adim, info
real A(adim,*), b(*), z(*), rcond
real rcond1
call SPBCO( A, adim, n, rcond, z, info )
rcond1 = rcond + 1.0e0
if ( info .EQ. 0 .AND. rcond1 .GT. 1.0e0 ) then
call SPBSL( A, adim, n, q, b )
else
info = -1
endif
return
end
185
3.5 THE QR FACTORIZATION
186
SQRDCTo compute the QR factorization with no column pivoting :call SQRDC( A, adim, m, n, qraus, ipvt, work, job )
SQRSLcall SQRSL( A, adim, m, k, qraus, b, Qb,
QTb, x, rsd, Ax, job, info )
187
Example3.5-1 Full Rank Least Squares Solver
subroutine LS( A, adim, m, n, b, x, rcond, z, info, qraux )
integer adim, m, n, info
real A(adim,*), b(*), x(*), rcond, z(*), qraux(*)
real rcond1
call SQRDC( A, adim, m, n, qraux, 1, z, 0 )
call STRCO( A, adim, n, rcond, z, 1 )
rcond1 = rcond + 1.0e0
if ( rcond .GT. 1.0e0 ) then
call SQRSL( A,adim,m,n,qraux,b,z,b,x,b,z,110,info )
else
info = -1
endif
return
end
188
Example3.5-2 Underdetermined System Solver
subroutine UND(A,adim,m,n,b,x,rcond,z,info,qraux,ipvt)integer adim, m, n, info, ipvt(*)real A(adim,*), b(*), x(*), z(*), qraux(*), rcondreal rcond1integer kcall SQRDC( A, adim, m, n, qraux, ipvt, z, 1 )call STRCO( A, adim, m, rcond, z, 1 )rcond1 = rcond + 1.0e0if (rcond1 .GT. 1.0e0) then
call SQRSL(A,adim,m,m,qraux,b,z,b,b,b,z,100,info)
do 30 k = 1, nx(k) = 0if (ipvt(k) .LE. m) x(k) = b(ipvt(k))
30 continueelse info = -1endifreturnend
189
3.6THE SINGULAR VALUE
DECOMPOSITION
190
SSVDCThe singular value decomposition A = USVt of an m-by-n matrix A can be computed using the subroutine SSVDC :call SSVDC( A, adim, m, n, s, e, U, udim,
V,vdim, work, job, info )
191
Job Identification
A U V
Effect
0000010110112020212121
A A AA U UA A VA A AA U AA U VA U AA A AA U VA U AA A V
No U or V.No U or V. A saved.Matrix V returned in V.Matrix V returned in top of A.Matrix U returned in U. A saved.Matrix U and V returned in U and V. A saved.Matrix U1 returned in U. A savedMatrix U1 returned in A.Matrix U1 and V returned in U and V. A saved.Matrix U1 and V returned in U and A. A saved.Matrix U1 and V returned in A and V.
192
Example3.6-1 Rank Deficient Least Squaressubroutine LSSVD(a,adim,m,n,s,e,V,vdim,b,tol,x,info)integer adim, m, n, vdim, inforeal A(adim,*), s(*), e(*), V(vdim,*), b(*), tol, x(*)integer i, jreal ujtbcall SSVDC( a,asim,m,n,s,e,A,adim,V,vdim,x,21,info )if (info .NE. 0) then
do 5 i = 1, nx(i) = 0
5 continue do 50 j = 1, n if (s(j) .GE. tol) then
ujtb = SDOT( m, a(1,j), 1, b(1), 1 )call SAXPY( n,ujtb/s(j),V(1,j),a,x(a),1 )
info = info – 1 endif50 continue endif return end
193
3.7 DOUBLE PRECISION AND COMPLEX
VERSIONS
194
Triangular SOLVE
STRSL( SA, adim, n, sb, job, info )DTRSL( DA, adim, n, db, job, info )CTRSL( CA, adim, n, cb, job, info )ZTRSL( ZA, adim, n, zb, job, info )
Condition
STRCO( SA, adim, n, srcond, sz, job )DTRCO( DA, adim, n, drcond, dz, job )CTRCO( CA, adim, n, crcond, cz, job )ZTRCO( ZA, adim, n, zrcond, zz, job )
195
General I Factor and Condition
SGECO( SA, adim, n, ipvt, srcond, sz ) DGECO( DA, adim, n, ipvt, drcond, dz ) CGECO( CA, adim, n, ipvt, crcond, cz )
ZGECO( ZA, adim, n, ipvt, zrcond, zz ) Factor
SGEFA( SA, adim, n, ipvt, info ) DGEFA( DA, adim, n, ipvt, info ) CGEFA( CA, adim, n, ipvt, info ) ZGEFA( ZA, adim, n, ipvt, info )
196
General II Solve
SGESL( SA, adim, n, ipvt, sb, job ) DGESL( DA, adim, n, ipvt, db, job ) CGESL( CA, adim, n, ipvt, cb, job ) ZGESL( ZA, adim, n, ipvt, zb, job )
Inverse and DeterminantSGEDI( SA, adim, n, ipvt, sdet, swork, job )
DGEDI( DA, adim, n, ipvt, ddet, dwork, job ) CGEDI( CA, adim, n, ipvt, cdet, cwork, job ) ZGEDI( ZA, adim, n, ipvt, zdet, zwork, job )
197
General Banded I Factor and Condition
SGBCO( SA, adim, n, p, q, ipvt, srcond, sz) DGBCO( DA, adim, n, p, q, ipvt, drcond, dz) CGBCO( CA, adim, n, p, q, ipvt, crcond, cz) ZGBCO( ZA, adim, n, p, q, ipvt, zrcond, zz) Factor
SGBFA( SA, adim, n, p, q, ipvt, info ) DGBFA( DA, adim, n, p, q, ipvt, info ) CGBFA( CA, adim, n, p, q, ipvt, info ) ZGBFA( ZA, adim, n, p, q, ipvt, info )
198
General Banded II Solve
SGBSL( SA, adim, n, p, q, ipvt, sb, job ) DGBSL( DA, adim, n, p, q, ipvt, db, job ) CGBSL( CA, adim, n, p, q, ipvt, cb, job ) ZGBSL( ZA, adim, n, p, q, ipvt, zb, job )
Inverse and DeterminantSGBDI( SA, adim, n, p, q, ipvt, sdet, swork, job )DGBDI( DA, adim, n, p, q, ipvt, ddet, dwork, job )
CGBDI( CA, adim, n, p, q, ipvt, cdet, cwork, job ) ZGBDI( ZA, adim, n, p, q, ipvt, zdet, zwork, job )
199
General Tridiagonal SGTSL( n, sc, sd, se, sb, info )
DGTSL( n, dc, dd, de, db, info )
CGTSL( n, cc, cd, ce, cb, info )
ZGTSL( n, zc, zd, ze, zb, info )
200
Hermitian Positive Definite I Factor and Condition
SPOCO( SA, adim, n, srcond, sz, info ) DPOCO( DA, adim, n, drcond, dz, info ) CPOCO( CA, adim, n, crcond, cz, info ) ZPOCO( ZA, adim, n, zrcond, zz, info ) Factor
SPOFA( SA, adim, n, info ) DPOFA( DA, adim, n, info ) CPOFA( CA, adim, n, info ) ZPOFA( ZA, adim, n, info )
201
Hermitian Positive Definite II Solve SPOSL( SA, adim, n, sb ) DPOSL( DA, adim, n, db ) CPOSL( CA, adim, n, cb ) ZPOSL( ZA, adim, n, zb )
Inverse and DeterminantSPODI( SA, adim, n, sdet, job )
DPODI( DA, adim, n, ddet, job ) CPODI( CA, adim, n, cdet, job ) ZPODI( ZA, adim, n, zdet, job )
202
Banded Hermitian Positive Definite I
Factor and Condition SPBCO( SA, adim, n, q, srcond, sz, info )
DPBCO( DA, adim, n, q, drcond, dz, info ) CPBCO( CA, adim, n, q, crcond, cz, info ) ZPBCO( ZA, adim, n, q, zrcond, zz, info ) Factor
SPBFA( SA, adim, n, q, info ) DPBFA( DA, adim, n, q, info ) CPBFA( CA, adim, n, q, info ) ZPBFA( ZA, adim, n, q, info )
203
Banded Hermitian Positive Definite II
Solve SPBSL( SA, adim, n, q, sb )
DPBSL( DA, adim, n, q, db ) CPBSL( CA, adim, n, q, cb ) ZPBSL( ZA, adim, n, q, zb )
Inverse and DeterminantSPBDI( SA, adim, n, q, sdet, job )
DPBDI( DA, adim, n, q, ddet, job ) CPBDI( CA, adim, n, q, cdet, job ) ZPBDI( ZA, adim, n, q, zdet, job )
204
Hermitian Positive Definite Tridiagonal
SPTSL( n, sd, se, sb, info )
DPTSL( n, dd, se, db, info )
CPTSL( n, cd, se, cb, info )
ZPTSL( n, zd, se, zb, info )
205
Hermitian Indefinite I Factor and Condition
SSICO( SA, adim, n, ipvt, srcond, sz ) DSICO( DA, adim, n, ipvt, drcond, dz ) CSICO( CA, adim, n, ipvt, crcond, cz ) ZSICO( ZA, adim, n, ipvt, zrcond, zz ) Factor SSIFA( SA, adim, n, ipvt, info ) DSIFA( DA, adim, n, ipvt, info ) CSIFA( CA, adim, n, ipvt, info ) ZSIFA( ZA, adim, n, ipvt, info )
206
Hermitian Indefinite II Solve
SSISL( SA, adim, n, ipvt, sb ) DSISL( DA, adim, n, ipvt, db ) CSISL( CA, adim, n, ipvt, cb ) ZSISL( ZA, adim, n, ipvt, zb )
Inverse and DeterminantSSIDI( SA, adim, n, ipvt, sdet, inert, swork, job )
DSIDI( DA, adim, n, ipvt, ddet, inert, dwork, job ) CSIDI( CA, adim, n, ipvt, cdet, inert, cwork, job ) ZSIDI( ZA, adim, n, ipvt, zdet, inert, zwork, job )
207
QR Factorization Factor
SQRDC( SA, adim, m, n, sqraux, ipvt, swork, job ) DQRDC( DA, adim, m, n, dqraux, ipvt, dwork, job ) CQRDC( CA, adim, m, n, cqraux, ipvt, cwork, job ) ZQRDC( ZA, adim, m, n, zqraux, ipvt, zwork, job ) SolveSQRSL(SA,adim,m,n,sqraux,sb,sQb,SQTb,sx,srsd,aAx,job,info)DQRSL(DA,adim,m,n,dqraux,db,dQb,dQTb,dx,drsd,dAx,job,inf
o)CQRSL(CA,adim,m,n,cqraux,cb,cQb,cQTb,cx,crsd,cAx,job,info)ZQRSL(ZA,adim,m,n,zqraux,zb,zQb,zQTb,zx,zrsd,zAx,job,info)
208
Singular Value Decomposition
SSVDC( SA,adim,m,n,ss,se,SU,udim,SV,vdim,swork,job,info)
DSVDC( DA,adim,m,n,ds,de,DU,udim,DV,vdim,dwork,job,info)
CSVDC( CA,adim,m,n,cs,ce,CU,udim,CV,vdim,cwork,job,info)
ZSVDC( ZA,adim,m,n,zs,ze,ZU,udim,SV,vdim,zwork,job,info)
209
4.1 BASICS
210
Setting Up Matrices Assignment a matrix
A = [ {row 1}; {row 2}; ……; {row m} ]
Ex: To set up a 4x4 matrix A ,like
A = [1 1 1 1;1 2 3 4;1 3 6 10;1 4 10 20]
It’s also legal to make input ‘look like’ output.A = [1 1 1 1
1 2 3 41 3 6 101 4 10 20 ]
201041
10631
4321
1111
211
Simple Output If you enter A = [1 2;3 4] without a
semicolon ,then the system responds with
If you already assignment A wit a semicolon, but now you want to display it .You could only type A to display.
43
21A
212
Types and Dimension Complex syntax
c = complex(a,b), it means c = a + bi
Dimension is handled automatically in MATLAB.Suppose you set B = [1 2 3; 4 5 6 ], and then set B = [1 0; 0 1].The system ‘knows enough’ to recognize that B has changed from 2-by-3 matrix to a 2-by-2 matrix.
213
Subscripts The subscripts in MATLAB must be positive. If you type A(1.2 , 3.5) = 10, you will get a error
message . ?? Subscript indices must either be real positive integers or logicals
214
Vectors and Scalars Vector and scalar are ‘special matrices’.
The commands, x = [ 1 ; 2 ; 3 ] ; or x = [1 2 3]’ ; establish x as a column vector while
Assignment an 1-by-1 matrix with c = [3], and it equals to c = 3 .
3
2
1
x
215
Size and LengthIf we declare A = [ 1 2 ; 3 4 ; 5 6 ], v = [7 8 9 10]
Size & Length syntaxsize(A)
ans = [3 2]length(v)
ans = 4
216
Continuation Sometimes it is not possible to put an entire
MATLAB instruction on a single line. We can break the instruction in a ‘natural’ place.
There are some ways to express a instruction, and they are the same.
A = [1 2 3; 4 5 6 ;....... 7 8 9]
A = [1 2 3; 4 5 6 ; 7 8 9]
A = [1 2 3; 4 5 6 ; 7 8 9]
217
Variable Names Variable names in MATLAB must consist of
nineteen or fewer characters.
The name must begin with a letter.Any letter, number, underscore may follow, e.g., sym_part1.
Upper and lower cases are distinguished in MATLAB.
218
Addition, Multiplication, Exponentiation
If A and B are matrices the C = A+B sets C to be the sum while C = A*B is the product.
To raise a square A to power r enterC = A^r
C = A^(-1) assigns the inverse of A to C.
f = c1Ab + c2A2b + c3A3b
f = A*(A*(c(3)*A*b+c(2)*b)c(1)*b)
f = ( c(3)*A^3+ c(2)*A^2+c(1)*A )*b
219
Transpose The conjugate transpose of a matrix can be
obtained using a single quote:Set B = At
B = A’
220
The “Colon Method” for Setting Up Vectors
Colons can be used to prescribe vectors whose components differ by a fixed increment.
v = 1:3 v = [1 2 3] v = 3:-1:0 v = [3 2 1 0] v = 1:2:10 v = [1 3 5 7 9]
221
Logarithmically Spaced Vectors
LOGSPACElogspace(a,b,n) generates n points between decades 10^a and 10^b.
If w = logspace(d1,d2,n), thenw(i) = 10 ^ [ d1 +(i-1)(d2-d1)/(n-1) ]
w = logspace(0,3,4) w = [1 10 100 1000]
222
Generation of Special Matrices
MATLAB has a number of build-in functions that can be used to generate frequently occurring matrices:
A = eye(m , n ) A m-by-n, ones on diagonal, zeros
elsewhere.A = zeros(m , n )
A m-by-n, zeros everywhere.A = ones(m , n )
A m-by-n, ones everywhere.
223
Random Matrices It’s possible to generate matrices and vectors
of random numbers:
A = rand(m , n ) A m-by-n, random element between 0
and 1.
224
Complex Matrices If A and B are matrices with the same
dimension and i2=-1, then the complex matrix C = A+iB can be generated as follows:
C = A + sqrt(-1) * B ; or C = A + i * B ;
225
Pointwise Operations Element operations between two matrices of
the same size are also possible:
C = A.*B cij = aij * bij
C = A.\B cij = bij / aij
C = A./B cij = aij / bij
C = A.^B cij = aijbij
C = A.’ cij = aji
If we have a=[1 2], then type a.*[3 4]We get the answer = [1*3 2*4 ]= [3 8].
226
More On Input/Output I The format for printed output can be controlled
using the following commands:format short 5-digit fixed point
styleformat short e 5-digit floating point
styleformat long 15-digit fixed point
styleformat long e 15-digit floating
point styleformat hex hexadecimal
227
More On Input/Output II format hex hexadecimal
To represent a number into double precise floating point(with hexadecimal form).
Ex. To show 1 in format ‘ hex ’ in matlab1 = + 1.0 * 2^0
sign exponent+1023 mantissa
0011 1111 1111 0000 0000 0000 ..... 0000 3 f f 0 0 0 ..... 0
sign(1 bit)
exponent(11 bit)
mantissa(52 bit)
0 01111111111 000000000.....00+1023
228
Machine Precision At the start of MATLAB session, the variable eps
contains the effective machine precision,i.e.,the smallest floating point number such that if x = 1 + eps then x>1 .
eps = 2.220446049250313e-016
229
Flops This is an obsolete function.
It can estimate the count of the program.
For complex operands, addition and subtraction count as two flops while multiplications and divisions each count as six flops.
230
4.2 LOOPS AND CONDITIONALS
231
For-Loops Syntax
for {var} = {row vector of counter values}{statements}
end
Ex: for i = 1:3 x(i) = i;end
is equivalent to x = [1;2;3];
232
Relations Relation operators
== equals
~= not equals< less than<= less than or equal to> greater than>= greater than or equal to
Ex: A=[1 2;3 4]; B=[1 2;2 4];T = A==BThen, the output is T = 1 1
0 1
233
‘and’, ’or’, ’not’ And(&), or(|), not(~)
The operations are possible between 0-1 matrices of equal dimension.
If T1 = [1 1 ; 0 1] ,T2 = [1 0 ; 0 0]T=T1&T2, T= 1 0
0 0T=T1|T2, T= 1 1
0 1T=~T1, T= 0 0
1 0
234
‘if’ Constructions Syntax
if {relation} {statements}end
Ex:Set a upper triangular matrix that has 1’s on the diagonal and –1’s above the diagonal.for i = 1 : n for j = 1 : n
if i < j A(i,j) = -1; elseif i > j A(i,j) = 0; else A(i,j) = 1; end
end end
235
The ‘any’ and ‘all’ Functions Any
If any component of the vector is nonzero, it returns a “1”.
AllIf all the entries are nonzero, it returns a “1”.
a = [ 0 1 1; 0 0 1; 0 0 1]any(a) ans =[0 1 1]all(a) ans =[0 0 1]
236
While-Loops Syntax
while {relation} {statements}
end
Print a sequence{Ai} of random 2-by-2 with the property that Ai+1>Ai:
A = zeros(2)
B = rand(2,2)
while(B>A)
A=B
B=rand(2,2)
end
237
The ‘break’ Command Break
It can be used to terminate a loop.
Print all Fibonnaci number less than 1000:fib(1) = 1
fib(2) = 1
for j = 3:1000
z = fib(j-1) + fib(j-2)
if z >= 1000
break
end
fib(j) = z
end
238
4.3 WORKING WITH SUBMATRICES
239
Setting Up Block Matrices We can set up the block matrix as follows:
A=[A11 A12 ; A21 A22 ; A31 A32 ]
If we want to make a 6-by-6 matrix with random 2-by-2 block diagonal, like
A = rand(2,2)
for i = 2:3
[m,m] = size(A);
A = [ A zeros(m,2) ; zeros(2,m) rand(2,2) ]
end
lk0000
ji0000
00hg00
00fe00
0000dc
0000ba
240
The Empty Matrix The empty matrix [] is often useful for
initialization of certain matrix computations.B=[] B is empty
To set up a column-reversed version of above example:
B = []
for j = 6:-1:1
B = [ B A(:,j) ];
end
0000kl
0000ij
00gh00
00ef00
cd0000
ab0000
241
Designating Submatrices If A is m-by-n matrix and i,j,p,q are integers.
Then, B= A( i:j , p:q ) B=
B = A ( : , p:q ), the range ( : ) means from 1:n, if the dimension of matrix is n.
jqjp
iqip
aa
aa
242
Assignment to Submatrix
If A = , we can assign the column
3 to [3 3 3]’ by using A(:,3)=3 or A ([7 8 9])=[3 3 3].
Then A =
021
021
021
321
321
321
243
Kronecker Product The Kronecker product of two matrices A and B can
be calculated using the build-in function kron: C = kron(A,B) C = (aijB)
C = []; [m,n] = size(A); for j = 1:n ccol = []; for i = 1:m ccol = [ ccol ; A(i,j)*B ]; end
C = [C ccol] end If C = kron( [ 1 2 ] , [ 1 2 ; 3 4 ] ) then C = 1 2 2 4 3 4 6 8
244
Turning Matrices into Vectors and Vice Versa
If A is a m-by-n matrix and we want to set v is an mn-by-1 vector.
v = A(:)
Set a transpose of a 2-by-2 matrix A = [ 1 2 ; 3 4 ]x = A(:);
x( [2 3] ) = x( [3 2] );
A(:) = x;
245
4.4 BUILD-IN FUNCTIONS
246
‘abs’, ‘real’, ‘imag’, ‘conj’ B = abs(A) B = ( |aij| )
B = real(A) B = ( real(aij) )
B = imag(A) B = ( imag(aij) )
B = conj(A) B = real(A)-i*imag(A), i2=-1
If A=[1-2i 2-i;3-4i 4-3i]; conj(A);Then, ans = 1+2i 2+i
3+4i 4+3i MATLAB’s build-in functions are in lower case,
unless user define.Otherwise, it’s will show ‘Capitalized internal function XXXX; Caps Lock may be on.’
247
Norms t = norm(A) t = || A ||2
t = norm(A,1) t = || A ||1
t = norm(A,’inf’) t = || A ||t = norm(A,'fro') t = || A ||F
If A = [1 2 3 4]norm(A)
ans = 5.47722557505166sqrt(1+4+9+16)ans = 5.47722557505166
248
Largest and Smallest Entries If v = max(A) v = [ max(A(:,1)), … , max(A(:,n)) ]
[v,i] = max(A)v = [ max(A(:,1)), … , max(A(:,n)) ]i = [the row of the max value
in]
A =[ 1 2 86 9 4
7 5 3];and [v,i]=max(A);
v= 7 9 8i = 3 2 1
249
Sums and Products t = sum(A) t =
t = prod(A) t =
A =[ 1 2 9 6 9 4
7 5 3]sum(A)ans =14 16 15prod(A)ans =42 90 96
) )(:,, .... ),(:, ( nA1A
))(:, , .... ),(:, ( nA1A
250
‘sort’ and ’find’ I Sorts each column of A in ascending order by using
sort(A)
[v,i] = sort(A), returns in sorted v and an integer vector i which indicate the permutation of new from old matrix.
A =[ 3 5 92 6 81 4 7]
[B,i]=sort(A); B =[1 4 7 2 5 8 3 6 9]
i =[ 3 3 3 2 1 2 1 2 1]
251
‘sort’ and ’find’ II The find function can be used to locate nonzero
entries.
A =[1 0 2 3 0]find(A)
ans =[1 3 4]find(A == 0)
ans =[2 5]
252
Rounding, Remainders, and Signs I
There are several conversion-to-integer routines:
round(x) round x to nearest integerfix(x) round x to zerofloor(x) round x to -ceil(x) round x to +
A = [ 1.1 -2.2 -0.9 3.8 ]round(A) ans=[1 - 2 -1 4]fix(A) ans=[1 -2 0 3]floor(A) ans=[1 -3 -1 3]ceil(A) ans=[2 -2 0 4]
253
Rounding, Remainders, and Signs II
t = sign(x) t =
t = rem(x,y) t = x-y*fix(x/y)If y = 0, rem(x,0) is NaN. And x and y must be the same dimension.
rem equals mod(x,y) if x and y are the same sign.Otherwise , mod(x,y) = rem(x,y)+ya = mod(-7,4)a = 1a = rem(-7,4) a = -3
0 if x 1
0 if x0
0 if x1
254
Extracting Triangular and Diagonal Parts I
B = tril (A) bij=aij if i j, zero otherwise.B = triu (A) bij=aij if i j, zero otherwise.B = tril (A,k) bij=aij if i+k j, zero otherwise.
B = triu (A,k) bij=aij if i+k j, zero otherwise.
diag(v) puts v on the main diagonal.v=diag(A) return a vector v with A’s diagonal.
255
Extracting Triangular and Diagonal Parts II
T = toeplitz(c,r) is equivalent to
T = zeros(n);
for k = 1:n
j = n-k;
if k<n
T = T + diag( r(k+1)*ones(j,1) , k);
end
T = T + diag( c(k)*ones(j+1,1) , -k+1);
end
256
Extracting Triangular and Diagonal Parts III
Take c=[6 7 8 9 10];r=[1 2 3 4 5];n=5;
into above two function. We can show
T =[ 6 2 3 4 5 7 6 2 3 4 8 7 6 2 3 9 8 7 6 2 10 9 8 7 6]
257
‘rank’ If A is a m-by-n matrix, rank(A)= min{m,n}.
It Is also possible to base the rank decision on an arbitrary tolerance.If tol is a nonnegative scalar then
r = rank(A , tol)
A = [ 1 2 ; 3 4 ; 5 6 ]rank(A)=min{3,2}=2
A = [1 0.1 ; 3 0.2 ; 5 0.3 ]rank(A,1)=1
258
Condition cond(A) can be computed the 2-norm
condition (the ratio of the largest
singular value of X to the smallest) of a matrix A.
min/ 1
259
Determinant The determinant of a square matrix A can be
computed as follows:det(A)
A =[1 2;3 4];det(A)=1*4-2*3
=-2
260
Elementary Functions Trigonometric
sin() cos() tan()
Inverse trigonometricasin() acos() atan()
Exponentialexp() log() log10()
Hyperbolicsinh() cosh() tanh()
261
Functions of Matrices F = expm(A) F = eA
F = logm(A) A = eF
F = sqrtm(A) F2 = A
262
‘roots’ and ‘poly’ If A is an n-by-n matrix then
c = poly(A) det( ) =
The roots are returned in a column vector by root(A), if we want to get the root.
If A=[2 -1;1 4];r=poly(A); r=[1 –6 9]a=roots(r); a=[3 -3]
AI n
1
1n
2n1n cccc
263
Fourier Transforms y = fft(x) y = Fourier transform of x
y = ifft(x) y = inverse Fourier transform of x
B = fft2(x) B = 2DFourier transform of xB = ifft2(x) B = inverse 2DFourier transform of x
264
4.5 FUNCTIONS
265
An Example I The first line in the function is of the form
function {output variable} = {function name} ({arguments})
Comments begin with the ’%’ sign.All functions often begins with ‘how-to-use’ comments.They will be displayed, as we input the help with the function name.
266
An Example II Write a function return a unit 2-norm vector
in the direction of Ax
function y = proAx(A,x)%% Compute K = [ v , Av , A^2 v , . . . , A^(p-1) v ] D,% where A is n-by-n, v is n-by-1, and D is diagonal% chosen so that the column of K have unit 2-norm.%y = A*x;c = norm(y);if c ~= 0
y = y/c;else
y = [ 1 ; zero( length( x-1,1 ) ) ];disp('Matrix-vector product is zero. First
column..of I returned.')end
267
Functions can call other Functions
Compute K = [v,Av,A^2v,…,A^(p-1)v]D,where A is n-by-n,v is n-by-1,and D is diagonal.
function K = kry(A,v,p) [ m,n ] = size(A); nv = length(v) if m ~= n | n ~= nv disp('Dimension do not agree.') else K = v/norm(v) for j = 2:p-1 K = [ K prodAx(A,K(:,j-1) )]; end end return
268
Multiple Input and Output Arguments I
Computes nonnegative matrices P and N, where A(i,j) > EPS , implies P(i,j) = A(i.j) , N(i,j) = 0 A(i,j) < EPS , implies P(i,j) = 0 , N(i,j) = -A(i.j)
| A(i,j) | <= EPS , implies P(i,j) = N(i,j) = 0
function [P N] = pn(A)E = EPS*ones(A);
P = A.*( A > E );
N = -A.*( -A > E );
end
269
Multiple Input and Output Arguments II
If one input argument(decided by nargin), tol = EPS.If one output argument (decided by nargout), P only is returned.
function [P N] = pn(A,tol)
if nargin == 1
tol = EPS;
end
E = tol*ones(A);
P = A.*( A > E );
if nargin == 2
N = -A.*( -A > E );
End
Statement Tolerance
Output
P = pn(A)[P,N]=pn(A)P = pn(A,tol)[P,N]=pn(A,tol)
epsepstoltol
Just PP and
NJust PP and
N
270
Passing Function Names It’s possible to pass the function name of another
function,but it requires some text processing with the eval function.
function F = afun(A,f)
[m,n] = size(A);
for j = 1:m
for i = 1:n
F(i,j) = eval([ f ,'(A(i,j))' ]);
end
end F(i,j) = eval([ f ,‘A(i,j)’ ]);
assign f(A(i,j)) to F(i,j). Ex: afun(A,'cos');
[0.54030230586814 -0.41614683654714 -0.98999249660045 -0.65364362086361]
271
Global Variables The values in all global variables can be
accessed whenever inside a function.
Like the common in Fortran, global should be used sparingly.
272
Recursion I Compute the 1-norm of an n-vector x function normL1 = f(x)
n = length(x);
if n == 1
normL1 = abs(x);
else
normL1 = f( x(1:n-1) ) + abs(x(n));
end
Ex: f([1 2 -3 4]) = 10
273
Recursion II Compute A*B with recursion method(Strassen
matrix), where A,B are square matrices.function C = strass(A,B,nmin)
[ n n ] = size(A); if n <= nmin % n small, get C conventionally C = A*B;
else m = n/2; u = 1:m; v = m+1:n; P1 = strass(A(u,u)+A(v,v), B(u,u)+B(v,v), nmin);
P2 = strass(A(v,u)+A(v,v), B(u,u), nmin); P3 = strass(A(u,u), B(u,v)-B(v,v), nmin);
P4 = strass(A(v,v), B(v,u)-B(u,u), nmin); P5 = strass(A(u,u)+A(u,v), B(v,v), nmin); P6 = strass(A(v,u)-A(u,u), B(u,u)+B(u,v), nmin); P7 = strass(A(u,v)-A(v,v), B(v,u)+B(v,v), nmin); C = [ P1+P4-P5+P7 P3+P5 ; P2+P4 P1+P3-P2+P5 ];end
274
4.6 FACTORIZATION
275
Solving Linear Systems It can be computed the solution to the multiple
tight-hand side linear system AX=B.X = A \ B
A less preferable way to solve AX = B isX = inv(A) * B
276
The LU Factorization The factorization A =LU, where U is upper
triangular and L is a row-permuted unit lower triangular matrix,can be computed as follows:
[ L , U ] = lu(A) Another less efficient method for solving AX =
b.[ L , U ] = lu(A)
y = L \ b;
x = U \ y;
277
The Cholesky Factorization It can be computed an upper triangular R
such that A =RHR, where A is an m-by-n Hermitian, positive definite matrix.
R = chol(A)
There is no build-in function for computing the factorization A = LDLH, where L is unit lower triangular and D is diagonal. How to do it?L = chol(A);d = diag(L);L=L';for k =1:n
L(:,k) = L(:,k)/d(k);endD = diag(d.*d);
278
QR Factorization Compute unitary Q(m-by-m) and upper
triangular R(m-by-n) such that A =QR.[Q , R] = qr(A)
Compute the minimizer of ||Ax - b||2.
[Q , R] = qr(A);
[m , n] = size(A);
b = Q'*b;
x = R(1:n,1:n)\b(1:n);
279
Householder and Givens Transformations
Compute a Householder matrix P such that Px is zero below the first component:
[P , R] = qr(x)
280
QR Factorization with Column Pivoting
Syntax[Q,R,P] = qr(A)
where Q is m-by-m unitary, p is n-by-n permutation, and upper triangular m-by-n matrix R.So, QTAP = R =
Compute the solution of Ax = b.[Q,R,P] = qr(A);
[m,n] = size(A);
y= R(1:m , 1:m) \Q'*b;
x= P* [y; zeros(n-m,1)];
00
RR 1211
281
Range and Null Space Compute an orthonormal basis for the range
space.Q = orth(A)
or[m,n]=size(A);[Q,R]=qr(A);Q=Q(:, 1:n);
A null space of a matrix AQ=null(A)
282
The Singular Value Decomposition I
If A is a m-by-n matrix ,we can compute unitary U(m-by-m), unitary V (n-by-n), and diagonal S(m-by-n):
[ U , S , V ] = svd(A)so, UHAV = S.
If m n then the ‘compact’ SVD in which U is m-by-n and S is n-by-n, use a two-argument version of svd:
[ U , S , V ] = svd(A, 0)
283
The Singular Value Decomposition II
A = [1 2 ;4 5;3 4];[ U , S , V ] = svd(A,0)[ U1 , S1 , V1 ] = svd(A,0)
U = [-0.26118 0.92755 -0.26726 -0.76072 -0.36822 -0.53452 -0.59420 0.06370 0.80178]S = [8.41440 0 0 0.444672 0 0 ]V = [-0.60452 -0.796587 -0.79658 0.60452]
U1 = [-0.26118 0.92755
-0.76072 -0.36822
-0.59420 0.06370]
S1 = [8.41440 0
0 0.444672]
V1 =[-0.60452 -0.796587
-0.79658 0.60452]
284
Pseudoinverse and Least Squares
If A is initialized then the pseudoinverse X can be found using pinv:
X = pinv(A)Then, multiple right-hand side least squares problem can be solved with either of the commands
x=A\B;or
X = pinv(A)*B;
285
The Hessenberg Decomposition
If A is a m-by-n matrix ,we can compute unitary Q(n-by-n), and upper Hessenberg H such that QHAQ = H.
H = hess(A)
A =[1 2 4;3 4 2;1 2 4];hess(A);
[1 -3.16227 3.162277 -3.16227 5.2 -1.6 0 -1.6 2.8 ]
286
The Schur Decomposition I Real SCHUR form
If A is real then Q is orthogonal and T = QTAQ is upper quasitriangular.
Imaginary SCHUR formIf the imaginary part of A is nonzero, then Q is unitary and T = QTAQ is upper triangular.
Syntax[Q,T] = schur(A);
It can be computed the complex Schur form from the real Schur form.[Q,T] = schur(A);[Q,T] = rsf2csf(Q,T);
287
The Schur Decomposition II If A=[7 -4 0
8 -5 0 6 -6 3 ];
T = schur(A);T= [3 13.2 6.18448
0 -1 -1.87408 0 0 3 ] A=[3 4;-2 -1];
[Q,T] = schur(A);[Q,T] = rsf2csf(Q,T);T=[ 1+2i -4.47213; 0 1-2i ]
288
The Eigenvector Decomposition
It can be compute a nonsingular X and diagonal D such that X-1AX = D is diagonal.
[X,D] = eig(A);
If A=[7 -4 0 8 -5 0 6 -6 3 ];
e = eig(A);e=[3 1 3]’
289
Generalized Eigenvalues A two-argument version of eig can be used to
solve the generalized eigenvalue problem Ax = Bx, where A and B are square and of the same dimension.
It can be compute a matrix X and a diagonal matrixD such that AX = BXD.
[X,D] = eig( A, B )
290
4.7 MISCELLANEOUS
291
IEEE Arithmetic In MATLAB, the result of a zero divide is a
special value called “inf”.Example, 1/0.
NaN means not a number.Example, 0/0.
Thus, x = inf and y = NaNa=x+z a = infa=1/x a = 0a=y*z a = NaNa=x/y a = NaNa=[x;y] a(1)=inf, a(2)=NaN
292
Timings We can execute the time which we run a
program.The function clock returns the current time in a row vector(year,month,day,hour,minute,and second).The function etime(t1,t2) returns the time in seconds between vectors t1 and t2.
for k =1:500n=k;A=rand(n);x=rand(n,1);t1=clock;y=fft(x);t2=clocktime=time+etime(t2,t1);end
293
Timed Displays A delay can be build into a MATLAB segment
with the pause statement.
Display the n-by-n magic square matrix on the screen for approximately n seconds for n=1:5.
for n=1:5
A=magic(n)
pause(n)
end
294
Getting Input It can be input a value during execution of a
MATLAB segment.
Write a function which displays magic square of chosen dimension by user. while(n>=1)
A=magic(n)
n=input('Enter dimension')
end
295
Converting Numbers to Strings
Num2str can be used to convert a number to its string format.
s = num2str(2) s = ‘2.0000’s = num2str(2) s = ‘-1.000E+2’s = num2str(2) s = ‘1.2345E+7’