forked from uvahotspot/HotSpot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
890 lines (751 loc) · 17.6 KB
/
util.c
File metadata and controls
890 lines (751 loc) · 17.6 KB
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
#include <assert.h>
#include "util.h"
#define SWAP(a,b) {temp=(a); (a)=(b); (b)=temp;}
int eq(double x, double y)
{
return (fabs(x-y) < DELTA);
}
int le(double x, double y)
{
return ((x < y) || eq(x,y));
}
int ge(double x, double y)
{
return ((x > y) || eq(x,y));
}
void fatal(char *s)
{
fprintf(stderr, "error: %s", s);
exit(1);
}
void warning(char *s)
{
fprintf(stderr, "warning: %s", s);
}
void swap_ival (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap_dval (double *a, double *b)
{
double t = *a;
*a = *b;
*b = t;
}
int tolerant_ceil(double val)
{
double nearest = floor(val+0.5);
/* numbers close to integers */
if (eq(val, nearest))
return ((int) nearest);
/* all others */
else
return ((int) ceil(val));
}
int tolerant_floor(double val)
{
double nearest = floor(val+0.5);
/* numbers close to integers */
if (eq(val, nearest))
return ((int) nearest);
/* all others */
else
return ((int) floor(val));
}
double *dvector(int n)
{
double *v;
v=(double *)calloc(n, sizeof(double));
if (!v) fatal("allocation failure in dvector()\n");
return v;
}
void free_dvector(double *v)
{
free(v);
}
void dump_dvector (double *v, int n)
{
int i;
for (i=0; i < n; i++)
fprintf(stdout, "%.5f\t", v[i]);
fprintf(stdout, "\n");
}
void copy_dvector (double *dst, double *src, int n)
{
memmove(dst, src, sizeof(double) * n);
}
void zero_dvector (double *v, int n)
{
memset(v, 0, sizeof(double) * n);
}
/* sum of the elements */
double sum_dvector (double *v, int n)
{
double sum = 0;
int i;
for(i=0; i < n; i++)
sum += v[i];
return sum;
}
int *ivector(int n)
{
int *v;
v = (int *)calloc(n, sizeof(int));
if (!v) fatal("allocation failure in ivector()\n");
return v;
}
void free_ivector(int *v)
{
free(v);
}
void dump_ivector (int *v, int n)
{
int i;
for (i=0; i < n; i++)
fprintf(stdout, "%d\t", v[i]);
fprintf(stdout, "\n\n");
}
void copy_ivector (int *dst, int *src, int n)
{
memmove(dst, src, sizeof(int) * n);
}
void zero_ivector (int *v, int n)
{
memset(v, 0, sizeof(int) * n);
}
/*
* Thanks to Greg Link from Penn State University
* for these memory allocators/deallocators
*/
double **dmatrix(int nr, int nc)
{
int i;
double **m;
m = (double **) calloc (nr, sizeof(double *));
assert(m != NULL);
m[0] = (double *) calloc (nr * nc, sizeof(double));
assert(m[0] != NULL);
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
return m;
}
void free_dmatrix(double **m)
{
free(m[0]);
free(m);
}
int **imatrix(int nr, int nc)
{
int i;
int **m;
m = (int **) calloc (nr, sizeof(int *));
assert(m != NULL);
m[0] = (int *) calloc (nr * nc, sizeof(int));
assert(m[0] != NULL);
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
return m;
}
void free_imatrix(int **m)
{
free(m[0]);
free(m);
}
void dump_dmatrix (double **m, int nr, int nc)
{
int i;
for (i=0; i < nr; i++)
dump_dvector(m[i], nc);
fprintf(stdout, "\n");
}
void copy_dmatrix (double **dst, double **src, int nr, int nc)
{
memmove(dst[0], src[0], sizeof(double) * nr * nc);
}
void zero_dmatrix(double **m, int nr, int nc)
{
memset(m[0], 0, sizeof(double) * nr * nc);
}
void resize_dmatrix(double **m, int nr, int nc)
{
int i;
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
}
/* allocate 3-d matrix with 'nr' rows, 'nc' cols,
* 'nl' layers and a tail of 'xtra' elements
*/
double ***dcuboid_tail(int nr, int nc, int nl, int xtra)
{
int i, j;
double ***m;
/* 1-d array of pointers to the rows of the 2-d array below */
m = (double ***) calloc (nl, sizeof(double **));
assert(m != NULL);
/* 2-d array of pointers denoting (layer, row) */
m[0] = (double **) calloc (nl * nr, sizeof(double *));
assert(m[0] != NULL);
/* the actual 3-d data array */
m[0][0] = (double *) calloc (nl * nr * nc + xtra, sizeof(double));
assert(m[0][0] != NULL);
/* remaining pointers of the 1-d pointer array */
for (i = 1; i < nl; i++)
m[i] = m[0] + nr * i;
/* remaining pointers of the 2-d pointer array */
for (i = 0; i < nl; i++)
for (j = 0; j < nr; j++)
/* to reach the jth row in the ith layer,
* one has to cross i layers i.e., i*(nr*nc)
* values first and then j rows i.e., j*nc
* values next
*/
m[i][j] = m[0][0] + (nr * nc) * i + nc * j;
return m;
}
void free_dcuboid(double ***m)
{
free(m[0][0]);
free(m[0]);
free(m);
}
/* mirror the lower triangle to make 'm' fully symmetric */
void mirror_dmatrix(double **m, int n)
{
int i, j;
for(i=0; i < n; i++)
for(j=0; j < i; j++)
m[j][i] = m[i][j];
}
void dump_imatrix (int **m, int nr, int nc)
{
int i;
for (i=0; i < nr; i++)
dump_ivector(m[i], nc);
fprintf(stdout, "\n");
}
void copy_imatrix (int **dst, int **src, int nr, int nc)
{
memmove(dst[0], src[0], sizeof(int) * nr * nc);
}
void resize_imatrix(int **m, int nr, int nc)
{
int i;
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
}
/* initialize random number generator */
void init_rand(void)
{
srand(RAND_SEED);
}
/* random number within the range [0, max-1] */
int rand_upto(int max)
{
return (int) (max * (double) rand() / (RAND_MAX+1.0));
}
/* random number in the range [0, 1) */
double rand_fraction(void)
{
return ((double) rand() / (RAND_MAX+1.0));
}
/*
* reads tab-separated name-value pairs from file into
* a table of size max_entries and returns the number
* of entries read successfully
*/
int read_str_pairs(str_pair *table, int max_entries, char *file)
{
int i=0;
char str[LINE_SIZE], copy[LINE_SIZE];
char name[STR_SIZE];
char *ptr;
FILE *fp = fopen (file, "r");
if (!fp) {
sprintf (str,"error: %s could not be opened for reading\n", file);
fatal(str);
}
while(i < max_entries) {
fgets(str, LINE_SIZE, fp);
if (feof(fp))
break;
strcpy(copy, str);
/* ignore comments and empty lines */
ptr = strtok(str, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
if ((sscanf(copy, "%s%s", name, table[i].value) != 2) || (name[0] != '-'))
fatal("invalid file format\n");
/* ignore the leading "-" */
strcpy(table[i].name, &name[1]);
i++;
}
fclose(fp);
return i;
}
/*
* same as above but from command line instead of a file. the command
* line is of the form <prog_name> <name-value pairs> where
* <name-value pairs> is of the form -<variable> <value>
*/
int parse_cmdline(str_pair *table, int max_entries, int argc, char **argv)
{
int i, count;
for (i=1, count=0; i < argc && count < max_entries; i++) {
if (i % 2) { /* variable name */
if (argv[i][0] != '-')
fatal("invalid command line. check usage\n");
/* ignore the leading "-" */
strncpy(table[count].name, &argv[i][1], STR_SIZE-1);
table[count].name[STR_SIZE-1] = '\0';
} else { /* value */
strncpy(table[count].value, argv[i], STR_SIZE-1);
table[count].value[STR_SIZE-1] = '\0';
count++;
}
}
return count;
}
/* append the table onto a file */
void dump_str_pairs(str_pair *table, int size, char *file, char *prefix)
{
int i;
char str[STR_SIZE];
FILE *fp = fopen (file, "w");
if (!fp) {
sprintf (str,"error: %s could not be opened for writing\n", file);
fatal(str);
}
for(i=0; i < size; i++)
fprintf(fp, "%s%s\t%s\n", prefix, table[i].name, table[i].value);
fclose(fp);
}
/* table lookup for a name */
int get_str_index(str_pair *table, int size, char *str)
{
int i;
if (!table)
fatal("null pointer in get_str_index\n");
for (i = 0; i < size; i++)
if (!strcasecmp(str, table[i].name))
return i;
return -1;
}
/* delete entry at 'at' */
void delete_entry(str_pair *table, int size, int at)
{
int i;
/*
* overwrite this entry using the next and
* shift all later entries once
*/
for (i=at+1; i < size; i++) {
strcpy(table[i-1].name, table[i].name);
strcpy(table[i-1].value, table[i].value);
}
}
/*
* remove duplicate names in the table - the entries later
* in the table are discarded. returns the new size of the
* table
*/
int str_pairs_remove_duplicates(str_pair *table, int size)
{
int i, j;
for(i=0; i < size-1; i++)
for(j=i+1; j < size; j++)
if (!strcasecmp(table[i].name, table[j].name)) {
delete_entry(table, size, j);
size--;
j--;
}
return size;
}
/* debug */
void print_str_pairs(str_pair *table, int size)
{
int i;
fprintf(stdout, "printing string table\n");
for (i=0; i < size; i++)
fprintf(stdout, "%s\t%s\n", table[i].name, table[i].value);
}
/*
* binary search a sorted double array 'arr' of size 'n'. if found,
* the 'loc' pointer has the address of 'ele' and the return
* value is TRUE. otherwise, the return value is FALSE and 'loc'
* points to the 'should have been' location
*/
int bsearch_double(double *arr, int n, double ele, double **loc)
{
if(n < 0)
fatal("wrong index in binary search\n");
if(n == 0) {
*loc = arr;
return FALSE;
}
if(eq(arr[n/2], ele)) {
*loc = &arr[n/2];
return TRUE;
} else if (arr[n/2] < ele) {
return bsearch_double(&arr[n/2+1], (n-1)/2, ele, loc);
} else
return bsearch_double(arr, n/2, ele, loc);
}
/*
* binary search and insert an element into a partially sorted
* double array if not already present. returns FALSE if present
*/
int bsearch_insert_double(double *arr, int n, double ele)
{
double *loc;
int i;
/* element found - nothing more left to do */
if (bsearch_double(arr, n, ele, &loc))
return FALSE;
else {
for(i=n-1; i >= (loc-arr); i--)
arr[i+1] = arr[i];
arr[loc-arr] = ele;
}
return TRUE;
}
/* search if an array contains a value
* return the index if so and -1 otherwise
*/
int contains(int *array, int size, int value)
{
int i;
for(i = 0; i < size; i++)
{
if(array[i] == value)
return i;
}
return -1;
}
/*
* population count of an 8-bit integer - using pointers from
* http://aggregate.org/MAGIC/
*/
unsigned int ones8(register unsigned char n)
{
/* group the bits in two and compute the no. of 1's within a group
* this works because 00->00, 01->01, 10->01, 11->10 or
* n = n - (n >> 1). the 0x55 masking prevents bits flowing across
* group boundary
*/
n -= ((n >> 1) & 0x55);
/* add the 2-bit sums into nibbles */
n = ((n & 0x33) + ((n >> 2) & 0x33));
/* add both the nibbles */
n = ((n + (n >> 4)) & 0x0F);
return n;
}
/*
* find the number of non-empty, non-comment lines
* in a file open for reading
*/
int count_significant_lines(FILE *fp)
{
char str[LINE_SIZE], *ptr;
int count = 0;
fseek(fp, 0, SEEK_SET);
while(!feof(fp)) {
fgets(str, LINE_SIZE, fp);
if (feof(fp))
break;
/* ignore comments and empty lines */
ptr = strtok(str, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
count++;
}
return count;
}
/* Ke's code: Coo2CSC */
struct coo_elem
{
int x;
int y;
double val;
};
int c2c_cmp( const void *a , const void *b )
{
struct coo_elem *c = (struct coo_elem *)a;
struct coo_elem *d = (struct coo_elem *)b;
if(c->y != d->y) return c->y - d->y;
else return c->x - d->x;
}
int coo2csc(int size, int nnz,
int *cooX, int *cooY, double *cooV, // input COO array
int *cscRowInd, int *cscColPtr, double *cscV) //output CSC array
{
int i, j;
int prev_x, prev_y;
// Init struct array
struct coo_elem *cooArray;
cooArray = (struct coo_elem *) calloc (nnz, sizeof(struct coo_elem));
// Copy in
for (i =0; i <nnz; i++) {
cooArray[i].x = cooX[i];
cooArray[i].y = cooY[i];
cooArray[i].val = cooV[i];
}
// Sort in col major
qsort(cooArray, nnz, sizeof(cooArray[0]), c2c_cmp);
// Copy out, check duplicate
j = -1;
prev_x = -1;
prev_y = -1;
for (i =0; i <nnz; i++) {
cscRowInd[i]=cooArray[i].x;
cscV[i]=cooArray[i].val;
while(j<cooArray[i].y){
j++;
cscColPtr[j]=i;
}
if((cooArray[i].x == prev_x) &&
(cooArray[i].y == prev_y))
printf("Warning: Duplicate elements in Matrix!\n");
prev_x = cooArray[i].x;
prev_y = cooArray[i].y;
}
cscColPtr[j+1]=i;
free(cooArray);
return 1;
}
/*
* Gauss-Jordan elimination with full pivoting
* Taken from Numerical Recipes in C
* gaussj is written assuming that b is a vector, but it is trivial to instead
* make b an nxm matrix in order to solve Ax=b for m different values of b
*/
void gaussj(double **a, int n, double *b) {
int *indxc, *indxr, *ipiv;
int i, icol, irow, j, k, l, ll;
double big, dum, pivinv, temp;
indxc = calloc(n, sizeof(int));
indxr = calloc(n, sizeof(int));
ipiv = calloc(n, sizeof(int));
// Main Loop
for(i = 0; i < n; i++) {
big = 0.0;
// Search for a pivot element (choose the largest)
for(j = 0; j < n; j++) {
if (ipiv[j] != 1)
for(k = 0; k < n; k++) {
if(ipiv[k] == 0) {
if(fabs(a[j][k]) >= big) {
big = fabs(a[j][k]);
irow = j;
icol = k;
}
}
}
}
++(ipiv[icol]);
/*
* We've found the pivot, so we interchange rows if necessary to put
* the pivot on the diagonal
* indxc[i] = the column of the ith pivot element
* indxr[i] = the row in which that pivot element was originally located
* if indxr[i] != indxc[i], there is an implied column intercahnge
*/
if(irow != icol) {
for(l = 0; l < n; l++)
SWAP(a[irow][l], a[icol][l])
SWAP(b[irow], b[icol])
}
indxr[i] = irow;
indxc[i] = icol;
if(a[icol][icol] == 0.0)
fatal("gaussj: Singular Pressure Matrix\n");
pivinv = 1.0 / a[icol][icol];
a[icol][icol] = 1.0;
// Divide the pivot row by the pivot element
for(l = 0; l < n; l++)
a[icol][l] *= pivinv;
b[icol] *= pivinv;
// Reduce all rows except the row containing the pivot
for(ll = 0; ll < n; ll++) {
if(ll != icol) {
dum = a[ll][icol];
a[ll][icol] = 0.0;
for(l = 0; l < n; l++)
a[ll][l] -= a[icol][l] * dum;
b[ll] -= b[icol] * dum;
}
}
} // end of Main Loop
// Put columns back in original order
for(l = n-1; l >= 0; l--) {
if(indxr[l] != indxc[l]) {
for(k = 1; k < n; k++) {
SWAP(a[k][indxr[l]], a[k][indxc[l]]);
}
}
}
free(ipiv);
free(indxr);
free(indxc);
}
#if SUPERLU > 0
/*
* computes A = c*diag + A
* NOTE: Assumes that A contains only nonzero elements on its diagonal
*/
int diagonal_add_SparseMatrix(double c, diagonal_matrix_t *diag, SuperMatrix *A) {
NCformat *Astore;
int i, j;
int n;
double *a;
int *asub, *xa;
int flag = 1;
Astore = A->Store;
n = A->ncol;
a = Astore->nzval;
asub = Astore->rowind;
xa = Astore->colptr;
double *diag_vals = diag->vals;
for(i=0; i<n; i++){
flag = 1;
j = xa[i];
while(j<xa[i+1]){
if(asub[j] == i){
a[j] += c*diag_vals[i];
flag = 0;
fprintf(stderr, "A[%d] = %e\n", j, a[j]);
}
j++;
}
if(flag)
fatal("Matrix missing diagonal element! Cannot support that yet\n");
}
return 1;
}
/*
* computes vector = c*diag*vector
*/
int diagonal_mul_vector(double c, diagonal_matrix_t *diag, double **vector) {
int i, n;
double *diag_vals;
n = diag->n;
diag_vals = diag->vals;
for(i = 0; i < n; i++) {
(*vector)[i] *= c*diag_vals[i];
}
return 1;
}
/*
* computes vector2 += vector1
*/
int vector_add_vector(int n, double c1, double *vector1, double c2, double *vector2) {
int i;
for(i = 0; i < n; i++) {
//fprintf(stderr, "vector2[%d] = vector1[%d] + vector[%d] = %e + %e = ", i, i, i, vector1[i], vector2[i]);
vector2[i] = c1*vector1[i] + c2*vector2[i];
//fprintf(stderr, "%e\n", vector2[i]);
}
return 1;
}
/*
* computes A = A*vector
*/
int SparseMatrix_mul_vector(SuperMatrix *A, double *vector) {
NCformat *Astore;
double *result;
int i, j, row_index;
int m, n;
double *a;
int *asub, *xa;
m = A->nrow;
n = A->ncol;
Astore = A->Store;
a = Astore->nzval;
asub = Astore->rowind;
xa = Astore->colptr;
if ( !(result = (double *) calloc(m, sizeof(double))) )
fatal("Malloc fails for result[].\n");
for(i=0; i<m; i++)
result[i] = 0;
for(i=0; i<n; i++){
j = xa[i];
while(j<xa[i+1]){
row_index = asub[j];
result[row_index] += a[j] * vector[i];
j++;
}
}
copy_dvector(vector, result, m);
free(result);
return 1;
}
void cooTocsv(char *filename, int size, int nnz, int *cooX, int *cooY, double *cooV) {
int i, j, k;
double **matrix;
matrix = calloc(size, sizeof(double *));
for(i = 0; i < size; i++)
matrix[i] = calloc(size, sizeof(double));
for(i = 0; i < nnz; i++) {
matrix[cooX[i]][cooY[i]] = cooV[i];
}
FILE *fp = fopen(filename, "w");
fprintf(fp, ",");
for(i = 0; i < size-1; i++)
fprintf(fp, "%d, ", i);
fprintf(fp, "%d\n", size-1);
for(i = 0; i < size; i++) {
fprintf(fp, "%d, ", i);
for(j = 0; j < size-1; j++) {
fprintf(fp, "%e, ", matrix[i][j]);
}
fprintf(fp, "%e\n", matrix[i][size-1]);
}
for(i = 0; i < size; i++)
free(matrix[i]);
free(matrix);
fclose(fp);
}
void diagTocsv(char *filename, diagonal_matrix_t *diag) {
int n = diag->n;
double *vals = diag->vals;
int i, j;
FILE *fp = fopen(filename, "w");
fprintf(fp, ",");
for(i = 0; i < n-1; i++)
fprintf(fp, "%d, ", i);
fprintf(fp, "%d\n", n-1);
for(i = 0; i < n; i++) {
fprintf(fp, "%d, ", i);
for(j = 0; j < n-1; j++) {
if(i != j)
fprintf(fp, "0, ");
else
fprintf(fp, "%e, ", vals[i]);
}
if(i == n-1)
fprintf(fp, "%e\n", vals[n-1]);
else
fprintf(fp, "0\n");
}
fclose(fp);
}
void vectorTocsv(char *filename, int size, double *vector) {
FILE *fp = fopen(filename, "w");
int i;
fprintf(fp, ",0\n");
for(i = 0; i < size; i++)
fprintf(fp, "%d, %e\n", i, vector[i]);
fclose(fp);
}
#endif