forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.f
183 lines (129 loc) · 4.06 KB
/
main.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
!standard fortran interface
!my function descriptions will be here,
!other lapack cheats show only different interfaces.
subroutine assert_eqi( a, b )
integer a, b
if ( a /= b) then
write(*,*) 'assert_eqi failed'
write(*,*) 'a', a
write(*,*) 'b', b
stop 1
endif
end
subroutine assert_eqr( x, y, err )
real x, y, err
if ( abs( x - y ) >= err ) then
write(*,*) 'assert_eqr failed'
write(*,*) 'y', x
write(*,*) 'x', y
write(*,*) 'err', err
stop 1
endif
end
!euclidean distance |x-y|_2
function dist2( n, x, y )
integer n
real x(n), y(n)
call saxpy( 2, -1.0, x, 1, y, 1 )
dist2 = snrm2( n, y, 1 )
return
end
!assert |x-y|_2 <= err
subroutine assert_eqvr2( n, x, y, err )
integer n
real x(n), y(n), err
real dist2
if ( dist2( n, x, y ) >= err ) then
write(*,*) 'assert_eq_vr_norm2 failed'
write(*,*) 'x', x
write(*,*) 'y', y
write(*,*) 'err', err
stop 1
endif
end
program main
integer n, nrhs, lda, ldb, info, pivots(2)
real a2x2(2,2), b2(2), c2(2), x2(2), x4(4), y2(2), err
err = 10e-6
!#blas
!#common arguments
!most commands start with the array dimension as argument
!ex:
x4(1) = 1.0
x4(2) = 1.0
x4(3) = 1.0
x4(4) = 1.0
call assert_eqr( snrm2(4, x4, 1), 2.0, err )
! ^
! array length
!#indx
!argument present on lots of the level 1 ops.
!takes every nth value only.
!array must be indx times larger
!TODO make this work
x4(1) = 1.0
x4(2) = 1.0
x4(3) = 1.0
x4(4) = 1.0
!call assert_eqr( snrm2(4, x4, 2), sqrt(2.0), err )
! ^
! indx
!#nrm2
!euclidean norm 2
x2(1) = 1.0
x2(2) = -2.0
call assert_eqr( snrm2(2, x2, 1), sqrt(5.0), err )
!#axpy
!y = \alpha * x + y
!x
x2(1) = 1.0
x2(2) = -2.0
!y
y2(1) = 3.0
y2(2) = -4.0
call saxpy(2, 2.0, x2, 1, y2, 1)
! 1 2 3
!1: \alpha
!2: incx
!3: incy
call assert_eqr( y2(1), 5.0, err )
call assert_eqr( y2(2), -8.0, err )
!#scal
!x = \alpha x
x2(1) = 1.0
x2(2) = -2.0
call sscal( 2, 2.0, x2, 1 )
y2(1) = 2.0
y2(2) = -4.0
call assert_eqvr2( 2, x2, y2, err )
!#i.amax
!index of largest absolute value in array
x4(1) = 1.0
x4(2) = -1.0
x4(3) = -2.0
x4(4) = 0.0
call assert_eqi( isamax(4, x4, 1), 3 )
!#lapack
!#gesv
!solve general linear system
n = 2
nrhs = 1 !number of columns of b: if many solves several eqs with same A
a2x2(1,1) = 1.0
a2x2(1,2) = 2.0
a2x2(2,1) = 3.0
a2x2(2,2) = 4.0
lda = 2 !max N,1. Leading Dimenstion A
b2(1) = 5.0
b2(2) = 11.0
ldb = 2 !max 1,N
!result returned inside of b itself:
!return status returned on `info`:
!pivots returned on `pivots`:
call sgesv( n, nrhs, a2x2, lda, pivots, b2, ldb, info )
c2(1) = 1.0
c2(2) = 2.0
call assert_eqi( info, 0 )
call assert_eqvr2( 2, b2, c2, err );
!#
stop
end