-
Notifications
You must be signed in to change notification settings - Fork 0
/
t3f2.cpp
103 lines (97 loc) · 2.17 KB
/
t3f2.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "t3.h"
double myF(const int i, const int j) {
return (double) 1 / (i + j + 1);
}
int setA(const int n, const int m, double *myM) {
if(m == 1) {
char tIn[] = "matrix.txt";
FILE* fIn = fopen(tIn, "rt");
if(fIn == NULL) return -4;
int i = 0;
while(i < n) {
int j = 0;
while(j < n) {
if(fscanf(fIn, "%lf", &myM[j + n * i]) < 1) {
fclose(fIn);
return -2;
}
++j;
}
++i;
}
fclose(fIn);
} else {
int i = 0;
while(i < n) {
int j = 0;
while(j < n) {
myM[j + n * i] = myF(i, j);
++j;
}
++i;
}
}
return 0;
}
int setB(const int n, double *myM) {
int i = 0;
while(i < n) {
int j = 0;
while(j < n) {
if(i == j) myM[i + n * j] = 1.; else myM[i + n * j] = 0.;
++j;
}
++i;
}
return 0;
}
int printM(const int n, const int m, double *myM, const char *str) {
if(printf("%s", str) < 1) return -1;
int i = 0;
int k = n;
if(m < n) k = m;
while(i < k) {
int j = 0;
while(j < k) {
if(printf("%.1g\t", myM[j + n * i]) < 1) return -1;
++j;
}
if(printf("\n") < 1) return -1;
++i;
}
return 0;
}
double getDelta(const int n, double *A, double *B) {
double mDelta = 0.;
int i = 0;
while(i < n) {
int j = 0;
double vDelta = 0.;
while(j < n) {
double tmp = 0.;
int k = 0;
while(k < n) {
tmp += A[k + n * i] * B[j + n * k];
++k;
}
if(i == j) tmp -= 1.;
vDelta += fabs(tmp);
// printf("DELTA:::: %g\t%g\t%d\t%d\n", tmp, mDelta, i, j);
++j;
}
if(mDelta < vDelta) mDelta = vDelta;
++i;
}
return mDelta;
}
long int getTime() {
struct timeval buf;
gettimeofday(&buf, 0);
return 1000000 * buf.tv_sec + buf.tv_usec;
}