-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleGraph.c
699 lines (670 loc) · 17.7 KB
/
SimpleGraph.c
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/**
* @author ChangKaiyan ([email protected])
* @brief A Computational Graph for Deep learning from UESTC Software Engineering
* @version 0.1
* @date 2019-05-15
* @copyright Copyright 2018 Kaiyan Chang
*
*/
#include<stdio.h>
#include<math.h>
#include<assert.h>
#include<malloc.h>
#include"SimpleGraph.h"
#include<stdbool.h>
#define ERRORFUNC fprintf(stderr,"In function %s , in line %d ,"\
"an error occurs.\nCheck it for the details:\n",\
__FUNCTION__,__LINE__)
#ifdef DEBUG
#define DEBUGSHOW printf("Running in function %s, in line %d:\n",__FUNCTION__,__LINE__)
#endif
#ifndef DEBUGSHOW
#define DEBUGSHOW
#endif
static Node mul(Node*a, Node*b);
static Node add(Node*a, Node*b);
static Node sub(Node*a, Node*b);
static void backward(int node);
static void cleargrad();
static Node matgraph[512];//
static int graphpoint = 0;//
static Node grad[512];//
bool has_forward = false;
/**
* @brief
*
* @param a
* @param b
* @return Node
*/
static Node add(Node*a, Node*b)
{
assert(a->m == b->m&&a->n == b->n);
Node temp;
temp.m = a->m;
temp.n = a->n;
for (int i = 0; i < a->m; ++i)
for (int j = 0; j < a->n; ++j)
{
temp.data[i][j] = a->data[i][j] + b->data[i][j];
}
return temp;
}
/**
* @brief
*
* @param a
* @param b
* @return Node
*/
static Node sub(Node*a, Node*b)
{
assert(a->m == b->m&&a->n == b->n);
Node temp;
temp.m = a->m;
temp.n = a->n;
for (int i = 0; i < a->m; ++i)
for (int j = 0; j < a->n; ++j)
{
temp.data[i][j] = a->data[i][j] - b->data[i][j];
}
return temp;
}
/**
* @brief
*
* @param a
* @param b
* @return Node
*/
static Node mul(Node*a, Node*b)
{
assert(a->n == b->m);
Node temp;
temp.m = a->m;
temp.n = b->n;
for (int i = 0; i < a->m; ++i)
for (int j = 0; j < b->n; ++j)
{
temp.data[i][j] = 0;
for (int k = 0; k < a->n; ++k)
temp.data[i][j] += a->data[i][k] * b->data[k][j];
}
return temp;
}
/**
* @brief
*
* @param x Which must have specific shape and data
* @return int
*/
int matrix_constant(Node x)
{
matgraph[graphpoint] = x;
grad[graphpoint].type = matgraph[graphpoint].type = CONSTANT;
matgraph[graphpoint].lnode = matgraph[graphpoint].rnode = -1;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].m = matgraph[graphpoint].m;
grad[graphpoint].n = matgraph[graphpoint].n;
return graphpoint++;
}
/**
* @brief
*
* @param x
* @return int
*/
int matrix_variable(Node x)
{
matgraph[graphpoint] = x;
grad[graphpoint].type = matgraph[graphpoint].type = VARIABLE;
matgraph[graphpoint].lnode = matgraph[graphpoint].rnode = -1;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].m = matgraph[graphpoint].m;
grad[graphpoint].n = matgraph[graphpoint].n;
return graphpoint++;
}
/**
* @brief
*
* @param m
* @param n
* @return int
*/
int matrix_placeholder(int m, int n)//需要输入矩阵型号,确保前向传播矩阵型号一致统一,构建计算图时,反向传播统一分配所有内存
{
grad[graphpoint].m = matgraph[graphpoint].m = m;
grad[graphpoint].n = matgraph[graphpoint].n = n;
grad[graphpoint].type = matgraph[graphpoint].type = PLACEHOLDER;
matgraph[graphpoint].lnode = matgraph[graphpoint].rnode = -1;
matgraph[graphpoint].data = NULL;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_ = 0;
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
return graphpoint++;
}
/**
* @brief
*
* @param lchild
* @param rchild
* @return int
*/
int matrix_add(int lchild, int rchild)
{
grad[graphpoint].m = matgraph[graphpoint].m = matgraph[lchild].m;
grad[graphpoint].n = matgraph[graphpoint].n = matgraph[rchild].n;
matgraph[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
matgraph[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].type = matgraph[graphpoint].type = ADD;
matgraph[graphpoint].lnode = lchild;
matgraph[graphpoint].rnode = rchild;
grad[lchild].parentGrad++;
grad[rchild].parentGrad++;
grad[lchild].parentGrad_++;
grad[rchild].parentGrad_++;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
return graphpoint++;
}
/**
* @brief
*
* @param lchild
* @param rchild
* @return int
*/
int matrix_mul(int lchild, int rchild)
{
DEBUGSHOW;
grad[graphpoint].m = matgraph[graphpoint].m = matgraph[lchild].m;
grad[graphpoint].n = matgraph[graphpoint].n = matgraph[rchild].n;
matgraph[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
matgraph[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].type = matgraph[graphpoint].type = MULTIPLY;
matgraph[graphpoint].lnode = lchild;
matgraph[graphpoint].rnode = rchild;
grad[lchild].parentGrad++;
grad[rchild].parentGrad++;
grad[lchild].parentGrad_++;
grad[rchild].parentGrad_++;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
return graphpoint++;
}
Node matrix_zero(int m, int n)
{
Node matrix;
matrix.m = m;
matrix.n = n;
matrix.data = (double**)calloc(m, sizeof(double*));
for (int i = 0; i < m; ++i)
matrix.data[i] = (double*)calloc(n, sizeof(double));
return matrix;
}
/**
* @brief
*
* @param lchild
* @param rchild
* @return int
*/
int matrix_sub(int lchild, int rchild)
{
grad[graphpoint].m = matgraph[graphpoint].m = matgraph[lchild].m;
grad[graphpoint].n = matgraph[graphpoint].n = matgraph[rchild].n;
matgraph[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
matgraph[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].type = matgraph[graphpoint].type = SUB;
matgraph[graphpoint].lnode = lchild;
matgraph[graphpoint].rnode = rchild;
grad[lchild].parentGrad++;
grad[rchild].parentGrad++;
grad[lchild].parentGrad_++;
grad[rchild].parentGrad_++;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
return graphpoint++;
}
int matrix_relu(int lchild)
{
grad[graphpoint].m = matgraph[graphpoint].m = matgraph[lchild].m;
grad[graphpoint].n = matgraph[graphpoint].n = matgraph[lchild].n;
matgraph[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
matgraph[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].type = matgraph[graphpoint].type = RELU;
matgraph[graphpoint].lnode = lchild;
matgraph[graphpoint].rnode = -1;
grad[lchild].parentGrad++;
grad[lchild].parentGrad_++;
grad[graphpoint].parentGrad = grad[graphpoint].parentGrad_=0;
return graphpoint++;
}
/**
* @brief
*
* @param lchild
* @return int
*/
int matrix_meanSquar(int lchild)
{
grad[graphpoint].m = matgraph[graphpoint].m = 1;
grad[graphpoint].n = matgraph[graphpoint].n = 1;
matgraph[graphpoint].data = (double**)calloc(1, sizeof(double*));
matgraph[graphpoint].data[0] = (double*)calloc(1, sizeof(double));
grad[graphpoint].type = matgraph[graphpoint].type = MEANSQUAR;
matgraph[graphpoint].lnode = lchild;
matgraph[graphpoint].rnode = -1;
grad[lchild].parentGrad++;
grad[lchild].parentGrad_++;
grad[graphpoint].parentGrad = 0;
grad[graphpoint].parentGrad_ = 0;
grad[graphpoint].data = (double**)calloc(matgraph[graphpoint].m, sizeof(double*));
for (int j = 0; j < matgraph[graphpoint].m; ++j)
{
grad[graphpoint].data[j] = (double*)calloc(matgraph[graphpoint].n, sizeof(double));
}
grad[graphpoint].m = matgraph[graphpoint].m;
grad[graphpoint].n = matgraph[graphpoint].n;
return graphpoint++;
}
/**
* @brief
*
* @param node
* @param x
*/
void matrix_fillIn(int node, Node x)
{
if (matgraph[node].type != PLACEHOLDER)
{
ERRORFUNC;
fprintf(stderr, "Can not fill in Data to a not-placeholder node!");
}
else
matgraph[node].data = x.data;
}
/**
* @brief
*
*/
void deletegraph()
{
for (int i = 0; i < graphpoint; ++i)
{
for (int j = 0; j < matgraph[i].m; ++j)
{
free(matgraph[i].data[j]);
}
free(matgraph[i].data);
for (int j = 0; j < grad[i].m; ++j)
{
free(grad[i].data[j]);
}
free(grad[i].data);
}
}
/**
* @brief Make the graph run, caculate the forward.Only one optimized node and a fixed shape graph.
*
*/
void matrix_forwardFlow()
{
cleargrad();
has_forward = true;
for (int index = 0; index < graphpoint; ++index)
{
switch (matgraph[index].type)
{
case ADD:
{
for (int i = 0; i < matgraph[index].m; ++i)
for (int j = 0; j < matgraph[index].n; ++j)
matgraph[index].data[i][j] = matgraph[matgraph[index].lnode].data[i][j] + matgraph[matgraph[index].rnode].data[i][j];
break;
}
case MULTIPLY:
{
for (int i = 0; i < matgraph[index].m; ++i)
for (int j = 0; j < matgraph[index].n; ++j)
for (int k = 0; k < matgraph[matgraph[index].lnode].n; ++k)
matgraph[index].data[i][j] = matgraph[matgraph[index].lnode].data[i][k] * matgraph[matgraph[index].rnode].data[k][j];
break;
}
case SUB:
{
for (int i = 0; i < matgraph[index].m; ++i)
for (int j = 0; j < matgraph[index].n; ++j)
matgraph[index].data[i][j] = matgraph[matgraph[index].lnode].data[i][j] - matgraph[matgraph[index].rnode].data[i][j];
break;
}
case MEANSQUAR:
{
matgraph[index].data[0][0] = 0;
for (int i = 0; i < matgraph[matgraph[index].lnode].m; ++i)
for (int j = 0; j < matgraph[matgraph[index].lnode].n; ++j)
matgraph[index].data[0][0] += 0.5* matgraph[matgraph[index].lnode].data[i][j] * matgraph[matgraph[index].lnode].data[i][j];
matgraph[index].data[0][0] /= matgraph[matgraph[index].lnode].m*matgraph[matgraph[index].lnode].n;
break;
}
case PLACEHOLDER:
{
if (matgraph[index].data == NULL)
{
ERRORFUNC;
fprintf(stderr, "Must assign to placeholder before putting forward!\n");
assert(matgraph[index].data != NULL);
}
break;
}
case RELU:
{
for (int i = 0; i < matgraph[index].m; ++i)
for (int j = 0; j < matgraph[index].n; ++j)
matgraph[index].data[i][j] = matgraph[matgraph[index].lnode].data[i][j] > 0 ? matgraph[matgraph[index].lnode].data[i][j] : 0;
break;
}
default:;//默认情况下是叶子节点,什么也不需要做
}
}
}
/**
* @brief
*
* @param node
*/
void matrix_backFlow(int node)
{
if (matgraph[node].type != MEANSQUAR)
{
ERRORFUNC;
fprintf(stderr, "Error in backFlow. The original node must output scaler!\n");
assert(matgraph[node].type == MEANSQUAR);
}
else if (has_forward == false)
{
ERRORFUNC;
fprintf(stderr, "On every epoch, forward flow must before back flow.\n");
assert(has_forward != false);
}
else
{
backward(node);
}
}
/**
* @brief
*
* @param node
*/
static void backward(int node)
{
DEBUGSHOW;
if (matgraph[node].type==MEANSQUAR)//梯度源点,标量对矩阵求导,这个节点仅有左孩子
{
for (int i = 0; i < grad[matgraph[node].lnode].m; ++i)
for (int j = 0; j < grad[matgraph[node].lnode].n; ++j)
grad[matgraph[node].lnode].data[i][j] = matgraph[matgraph[node].lnode].data[i][j];//均方误差导数
grad[matgraph[node].lnode].parentGrad--;
backward(matgraph[node].lnode);
}
else if (grad[node].parentGrad >= 1)//仅余一个父节点没有传递导数,或者两个父节点都没有传递导数
{
return;//等待剩余父节点传递导数
}
else//所有父节点导数均传递到此
{
if (matgraph[node].lnode == -1 && matgraph[node].rnode == -1)//是叶子节点
{
return;//梯度求解完成
}
else//非叶子节点
{
switch (grad[node].type)
{
case ADD:
{
for (int i = 0; i < matgraph[node].m; ++i)
for (int j = 0; j < matgraph[node].n; ++j)
{
grad[matgraph[node].lnode].data[i][j] += grad[node].data[i][j];//更新左孩子的梯度
grad[matgraph[node].rnode].data[i][j] += grad[node].data[i][j];//更新右孩子的梯度
}
grad[matgraph[node].lnode].parentGrad--;
grad[matgraph[node].rnode].parentGrad--;
backward(matgraph[node].lnode);
backward(matgraph[node].rnode);
break;
}
case MULTIPLY:
{
for (int i = 0; i < grad[node].m; ++i)
for (int j = 0; j < matgraph[matgraph[node].rnode].m; ++j)
{
for (int k = 0; k < grad[node].n; ++k)
grad[matgraph[node].lnode].data[i][j] += grad[node].data[i][k] * matgraph[matgraph[node].rnode].data[j][k];//更新左孩子的梯度
}
for (int i = 0; i < matgraph[matgraph[node].lnode].n; ++i)
for (int j = 0; j < grad[node].n; ++j)
{
for (int k = 0; k < grad[node].m; ++k)
grad[matgraph[node].rnode].data[i][j] += grad[node].data[k][j] * matgraph[matgraph[node].lnode].data[k][i];//更新右孩子的梯度
}
grad[matgraph[node].lnode].parentGrad--;
grad[matgraph[node].rnode].parentGrad--;
backward(matgraph[node].lnode);
backward(matgraph[node].rnode);
break;
}
case SUB:
{
for (int i = 0; i < matgraph[node].m; ++i)
for (int j = 0; j < matgraph[node].n; ++j)
{
grad[matgraph[node].lnode].data[i][j] += grad[node].data[i][j];//更新左孩子的梯度
grad[matgraph[node].rnode].data[i][j] -= grad[node].data[i][j];//更新右孩子的梯度
}
grad[matgraph[node].lnode].parentGrad--;
grad[matgraph[node].rnode].parentGrad--;
backward(matgraph[node].lnode);
backward(matgraph[node].rnode);
break;
}
case RELU:
{
for (int i = 0; i < matgraph[node].m; ++i)
for (int j = 0; j < matgraph[node].n; ++j)
{
grad[matgraph[node].lnode].data[i][j] += grad[node].data[i][j] * (matgraph[matgraph[node].lnode].data[i][j] > 0 ? 1 : 0);//更新左孩子的梯度
}
grad[matgraph[node].lnode].parentGrad--;
backward(matgraph[node].lnode);
break;
}
}
}
}
}
/**
* @brief Clear the gradient of all the node and reset the count which has
* not been derived in its parent nodes.
*
*/
static void cleargrad()//清除所有节点的梯度以及重置双亲节点中尚未求导的个数
{
DEBUGSHOW;
for (int i = 0; i < graphpoint; ++i)
{
for (int index = 0; index < grad[i].m; ++index)
{
for (int j = 0; j < grad[i].n; ++j)
{
grad[i].data[index][j] = 0.0;
}
}
grad[i].parentGrad = grad[i].parentGrad_;
}
}
/**
* @brief Print data matrix
*
* @param node The index of a node
*/
void matrix_printData(int node)
{
DEBUGSHOW;
for (int i = 0; i < matgraph[node].m; ++i)
{
for (int j = 0; j < matgraph[node].n; ++j)
printf("%7.3lf ", matgraph[node].data[i][j]);
putchar('\n');
}
}
void matrix_optimize(int vari_node, double learningrate)
{
DEBUGSHOW;
if (matgraph[vari_node].type != VARIABLE && learningrate >= 0)
{
ERRORFUNC;
fprintf(stderr, "The optimize node is not a Variable node!!\n");
assert(matgraph[vari_node].type == VARIABLE);
}
double sum = 0.0;
for (int i = 0; i < matgraph[vari_node].m; ++i)
for (int j = 0; j < matgraph[vari_node].n; ++j)
{
sum += grad[vari_node].data[i][j] * grad[vari_node].data[i][j];
}
sum = sqrt(sum);
if (sum != 0)
{
for (int i = 0; i < matgraph[vari_node].m; ++i)
for (int j = 0; j < matgraph[vari_node].n; ++j)
matgraph[vari_node].data[i][j] -= learningrate * (grad[vari_node].data[i][j] / sum);
}
}
/**
* @brief Print gradiend matrix
*
* @param node The index of the graph
*/
void matrix_printGrad(int node)
{
for (int i = 0; i < grad[node].m; ++i)
{
for (int j = 0; j < grad[node].n; ++j)
printf("%7.3f ", grad[node].data[i][j]);
putchar('\n');
}
}
/**
* @brief Input matrix data and convert it to Node type.
*
* @param m Row numbers
* @param n Col numbers
* @return Node Data matrix
*/
Node matrix_scanData(int m, int n)
{
DEBUGSHOW;
Node temp;
temp.data = (double**)malloc(sizeof(double*)*m);
for (int i = 0; i < m; ++i)
{
temp.data[i] = (double*)malloc(sizeof(double)*n);
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
scanf("%lf", &temp.data[i][j]);
temp.m = m;
temp.n = n;
return temp;
}
Node matrix_creatNode (int origin)
{
Node no;
no=matgraph[origin];
return no;
}
Node matrix_scanDataFromCsv(const char* filename,int m,int n)
{
Node temp;
FILE* file=fopen(filename,"r");
if(file==NULL)
{
ERRORFUNC;
fprintf(stderr,"File errors!\n");
return temp;
}
temp.data = (double**)malloc(sizeof(double*)*m);
for (int i = 0; i < m; ++i)
{
temp.data[i] = (double*)malloc(sizeof(double)*n);
}
if(n>1)
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n-1; ++j)
fscanf(file," %lf,", &temp.data[i][j]);
fscanf(file," %lf",&temp.data[i][n-1]);
}
}
else if(n==1)
{
for (int i = 0; i < m; ++i)
{
fscanf(file," %lf",&temp.data[i][n-1]);
}
}
else
{
ERRORFUNC;
fprintf(stderr,"Error in the input format! Cols must more than 1. \n");
}
temp.m = m;
temp.n = n;
return temp;
}