-
Notifications
You must be signed in to change notification settings - Fork 0
/
coeffs.h
254 lines (198 loc) · 3.91 KB
/
coeffs.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//Function declaration
double **createMat(int m,int n);
void readMat(double **p, int m,int n);
void print(double **p,int m,int n);
double **loadtxt(char *str,int m,int n);
double linalg_norm(double **a, int m);
double **linalg_sub(double **a, double **b, int m, int n);
double **linalg_inv(double **mat, int m);
double **matmul(double **a, double **b, int m, int n, int p);
double **transpose(double **a, int m, int n);
double **linspace(double a, double l, int n);
void savetxt(double **a, char *str,int m,int n);
double **line_gen(double**A,double**B);
//End function declaration
//Defining the function for matrix creation
double **createMat(int m,int n)
{
int i;
double **a;
//Allocate memory to the pointer
a = (double **)malloc(m * sizeof( *a));
for (i=0; i<m; i++)
a[i] = (double *)malloc(n * sizeof( *a[i]));
return a;
}
//End function for matrix creation
//Defining the function for reading matrix
void readMat(double **p, int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%lf",&p[i][j]);
}
}
}
//End function for reading matrix
//Read matrix from file
double **loadtxt(char *str,int m,int n)
{
FILE *fp;
double **a;
int i,j;
a = createMat(m,n);
fp = fopen(str, "r");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
fscanf(fp,"%lf",&a[i][j]);
}
}
//End function for reading matrix from file
fclose(fp);
return a;
}
//Defining the function for printing
void print(double **p, int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%lf ",p[i][j]);
printf("\n");
}
}
//End function for printing
//Defining the function for norm
double linalg_norm(double **a, int m)
{
int i;
double norm=0.0;
for(i=0;i<m;i++)
{
norm = norm + a[i][0]*a[i][0];
}
return sqrt(norm);
}
//End function for norm
//Defining the function for difference of matrices
double **linalg_sub(double **a, double **b, int m, int n)
{
int i, j;
double **c;
c = createMat(m,n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]= a[i][j]-b[i][j];
}
}
return c;
}
//End function for difference of matrices
//Defining the function for inverse of 2x2 matrix
double **linalg_inv(double **mat, int m)
{
double **c, det;
c = createMat(m,m);
det = mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
c[0][0] = mat[1][1]/det;
c[0][1] = -mat[1][0]/det;
c[1][0] = -mat[0][1]/det;
c[1][1] = mat[0][0]/det;
return c;
}
// End function for inverse of 2x2 matrix
//Defining the function for difference of matrices
double **matmul(double **a, double **b, int m, int n, int p)
{
int i, j, k;
double **c, temp =0;
c = createMat(m,p);
for(i=0;i<m;i++)
{
for(k=0;k<p;k++)
{
for(j=0;j<n;j++)
{
temp= temp+a[i][j]*b[j][k];
}
c[i][k]=temp;
temp = 0;
}
}
return c;
}
//End function for difference of matrices
//Defining the function for transpose of matrix
double **transpose(double **a, int m, int n)
{
int i, j;
double **c;
//printf("I am here");
c = createMat(n,m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j]= a[j][i];
// printf("%lf ",c[i][j]);
}
}
return c;
}
//End function for transpose of matrix
//Linspace function
double **linspace(double a, double l, int m)
{
//Variable declarations
double d, **ap;
int i,j;
ap = createMat(m,1);
//Common difference
d = (l-a)/(m-1);
for(i=0;i<1;i++)
{
for(j=0;j<m;j++)
{
ap[j][i] = a+j*d;
}
}
//Returning the address of the first memory block
return ap;
}
//End linspace function
//Write matrix into file
void savetxt(double **a, char *str,int m,int n)
{
FILE *fp;
int i,j;
fp = fopen(str, "w");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
fprintf(fp,"%lf ",a[i][j]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
//End function for writing matrix into file
//Line generating function
double** line_gen(double**A,double**B){
int len=100;
double**x_AB=createMat(2,len);
double**lam=linspace(0,1,len);
for (int i=0; i<len; i++){
x_AB[0][i]=*A[0]+*lam[i]*(*B[0]-*A[0]);
x_AB[1][i]=*A[1]+*lam[i]*(*B[1]-*A[1]);
}
return x_AB;
}