-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.c
More file actions
809 lines (635 loc) · 19.4 KB
/
tensor.c
File metadata and controls
809 lines (635 loc) · 19.4 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
#include <math.h>
#include "tensor.h"
#include "dynamic_array.h"
#include "utils.h"
struct Tensor
{
DynamicArray *data;
size_t *shape, *strides;
size_t ndim, size;
};
Tensor *tensorCreate(size_t ndim, const size_t *shape)
{
if (ndim > 0 && !shape)
{
fprintf(stderr, "Error: shape pointer is NULL for non-scalar tensor\n");
exit(1);
}
Tensor *t = safeMalloc(sizeof(Tensor), "Failed to allocate tensor");
t->ndim = ndim;
if (ndim == 0)
{
t->shape = NULL;
t->strides = NULL;
t->size = 1;
t->data = daCreate(1);
} else
{
t->shape = safeMalloc(sizeof(size_t) * ndim, "Failed to allocate shape");
t->strides = safeMalloc(sizeof(size_t) * ndim, "Failed to allocate strides");
memcpy(t->shape, shape, sizeof(size_t) * ndim);
tensorComputeStrides(ndim, shape, t->strides);
t->size = tensorComputeSize(ndim, shape);
t->data = daCreate(t->size);
}
return t;
}
Tensor *tensorCreateFromData(size_t ndim, const size_t *shape, const DynamicArray *src)
{
daIsNullCheck(src);
if (ndim > 0 && !shape)
{
fprintf(stderr, "Error: shape pointer is NULL for non-scalar tensor\n");
exit(1);
}
/* ---- scalar case ---- */
if (ndim == 0)
{
if (daSize(src) != 1)
{
fprintf(stderr, "Error: source size does not match scalar tensor\n");
exit(1);
}
Tensor *t = safeMalloc(sizeof(Tensor), "Failed to allocate tensor");
t->ndim = 0;
t->size = 1;
t->shape = NULL;
t->strides = NULL;
t->data = daCreate(1);
daAppend(t->data, daGet(src, 0));
return t;
}
/* ---- non-scalar case ---- */
size_t expectedSize = tensorComputeSize(ndim, shape);
if (daSize(src) != expectedSize)
{
fprintf(stderr, "Error: source size does not match tensor shape\n");
exit(1);
}
Tensor *t = safeMalloc(sizeof(Tensor), "Failed to allocate tensor");
t->ndim = ndim;
t->size = expectedSize;
t->shape = safeMalloc(sizeof(size_t) * ndim, "Failed to allocate shape");
t->strides = safeMalloc(sizeof(size_t) * ndim, "Failed to allocate strides");
memcpy(t->shape, shape, sizeof(size_t) * ndim);
tensorComputeStrides(ndim, shape, t->strides);
t->data = daCreate(expectedSize);
for (size_t i = 0; i < expectedSize; i++)
daAppend(t->data, daGet(src, i));
return t;
}
Tensor *tensorScalar(double value)
{
Tensor *t = safeMalloc(sizeof(Tensor), "Failed to allocate scalar");
t->ndim = 0;
t->size = 1;
t->shape = NULL;
t->strides = NULL;
t->data = daCreate(1);
daSet(t->data, 0, value);
return t;
}
Tensor *tensorVector(size_t n)
{
size_t shape[1] = { n };
return tensorCreate(1, shape);
}
Tensor *tensorVectorFromData(size_t n, const double *data)
{
size_t shape[1] = { n };
DynamicArray *da = daCreate(n);
for (size_t i = 0; i < n; i++) daAppend(da, data[i]);
Tensor *t = tensorCreateFromData(1, shape, da);
daDestroy(da);
return t;
}
Tensor *tensorMatrix(size_t rows, size_t cols)
{
size_t shape[2] = { rows, cols };
return tensorCreate(2, shape);
}
Tensor *tensorMatrixFromData(size_t rows, size_t cols, const double *data)
{
size_t shape[2] = { rows, cols };
DynamicArray *da = daCreate(rows * cols);
for (size_t i = 0; i < rows * cols; i++) daAppend(da, data[i]);
Tensor *t = tensorCreateFromData(2, shape, da);
daDestroy(da);
return t;
}
void tensorDestroy(Tensor *t)
{
if (!t) return;
daDestroy(t->data);
free(t->shape);
free(t->strides);
free(t);
}
double tensorGet(const Tensor *t, const size_t *indices)
{
tensorIsNullCheck(t);
/* ---- scalar case ---- */
if (t->ndim == 0)
{
if (indices)
{
fprintf(stderr, "Error: Scalar tensor accessed with indices\n");
exit(1);
}
return daGet(t->data, 0);
}
if (!indices)
{
fprintf(stderr, "Error: indices pointer is NULL for non-scalar tensor\n");
exit(1);
}
size_t linear = 0;
for (size_t i = 0; i < t->ndim; i++)
{
if (indices[i] >= t->shape[i])
{
fprintf(stderr, "Error: index %zu out of bounds for dimension %zu (size %zu)\n", indices[i], i, t->shape[i]);
exit(1);
}
linear += indices[i] * t->strides[i];
}
return daGet(t->data, linear);
}
void tensorSet(Tensor *t, const size_t *indices, double value)
{
tensorIsNullCheck(t);
/* ---- scalar case ---- */
if (t->ndim == 0)
{
if (indices)
{
fprintf(stderr, "Error: Scalar tensor accessed with indices\n");
exit(1);
}
daAppend(t->data, value);
return;
}
if (!indices)
{
fprintf(stderr, "Error: indices pointer is NULL for non-scalar tensor\n");
exit(1);
}
size_t linear = 0;
for (size_t i = 0; i < t->ndim; i++)
{
if (indices[i] >= t->shape[i])
{
fprintf(stderr, "Error: index %zu out of bounds for dimension %zu (size %zu)\n", indices[i], i, t->shape[i]);
exit(1);
}
linear += indices[i] * t->strides[i];
}
daSet(t->data, linear, value);
}
static void tensorPrintRecursive(const Tensor *t, const size_t *indices, size_t dim)
{
if (dim == t->ndim)
{
printf("%g", tensorGet(t, indices));
return;
}
printf("[");
for (size_t i = 0; i < t->shape[dim]; i++)
{
size_t newIndices[ t->ndim ];
if (indices)
memcpy(newIndices, indices, sizeof(size_t) * t->ndim);
newIndices[dim] = i;
tensorPrintRecursive(t, newIndices, dim + 1);
if (i + 1 < t->shape[dim]) printf(", ");
}
printf("]");
}
void tensorPrint(const Tensor *t)
{
tensorIsNullCheck(t);
if (t->ndim == 0)
{
printf("%g\n", tensorGet(t, NULL));
return;
}
tensorPrintRecursive(t, NULL, 0);
printf("\n");
}
static double add_op(double a, double b) { return a + b; }
static double sub_op(double a, double b) { return a - b; }
static double mul_op(double a, double b) { return a * b; }
static double div_op(double a, double b)
{
if (b == 0.0)
{
fprintf(stderr, "Error: Division by zero\n");
exit(1);
}
return a / b;
}
static Tensor *tensorBinaryOp(const Tensor *a, const Tensor *b, BinaryOp op)
{
tensorIsNullCheck(a);
tensorIsNullCheck(b);
if (a->ndim != b->ndim)
{
fprintf(stderr, "Error: tensor dimensions do not match (%zu vs %zu)\n", a->ndim, b->ndim);
exit(1);
}
// Scalars
if (a->ndim == 0)
return tensorScalar(op(tensorGet(a, NULL), tensorGet(b, NULL)));
// Validate shapes
for (size_t i = 0; i < a->ndim; i++)
{
if (a->shape[i] != b->shape[i])
{
fprintf(stderr, "Error: tensor shapes do not match at dimension %zu (%zu vs %zu)\n",
i, a->shape[i], b->shape[i]);
exit(1);
}
}
Tensor *res = tensorCreate(a->ndim, a->shape);
size_t total = a->size;
for (size_t i = 0; i < total; i++)
{
double va = daGet(a->data, i);
double vb = daGet(b->data, i);
daAppend(res->data, op(va, vb));
}
return res;
}
Tensor *tensorAdd(const Tensor *a, const Tensor *b) { return tensorBinaryOp(a, b, add_op); }
Tensor *tensorSub(const Tensor *a, const Tensor *b) { return tensorBinaryOp(a, b, sub_op); }
Tensor *tensorHadamardMul(const Tensor *a, const Tensor *b) { return tensorBinaryOp(a, b, mul_op); }
Tensor *tensorHadamardDiv(const Tensor *a, const Tensor *b) { return tensorBinaryOp(a, b, div_op); }
static double add_scalar(double a, double s) { return a + s; }
static double sub_scalar(double a, double s) { return a - s; }
static double mul_scalar(double a, double s) { return a * s; }
static double div_scalar(double a, double s)
{
if (s == 0.0)
{
fprintf(stderr, "Error: Division by zero\n");
exit(1);
}
return a / s;
}
static Tensor *tensorScalarOp(const Tensor *t, double scalar, ScalarOp op)
{
tensorIsNullCheck(t);
/* ---- scalar tensor ---- */
if (t->ndim == 0) return tensorScalar(op(tensorGet(t, NULL), scalar));
Tensor *res = tensorCreate(t->ndim, t->shape);
for (size_t i = 0; i < t->size; i++)
{
double v = daGet(t->data, i);
daAppend(res->data, op(v, scalar));
}
return res;
}
Tensor *tensorAddScalar(const Tensor *t, double scalar) { return tensorScalarOp(t, scalar, add_scalar); }
Tensor *tensorSubScalar(const Tensor *t, double scalar) { return tensorScalarOp(t, scalar, sub_scalar); }
Tensor *tensorMulScalar(const Tensor *t, double scalar) { return tensorScalarOp(t, scalar, mul_scalar); }
Tensor *tensorDivScalar(const Tensor *t, double scalar) { return tensorScalarOp(t, scalar, div_scalar); }
Tensor *tensorApply(const Tensor *t, UnaryOp op)
{
tensorIsNullCheck(t);
if (!op)
{
fprintf(stderr, "Error: function pointer is NULL\n");
exit(1);
}
/* ---- scalar tensor ---- */
if (t->ndim == 0) return tensorScalar(op(tensorGet(t, NULL)));
Tensor *res = tensorCreate(t->ndim, t->shape);
for (size_t i = 0; i < t->size; i++)
{
double v = daGet(t->data, i);
daAppend(res->data, op(v));
}
return res;
}
double tensorReduce(const Tensor *t, ReduceOp op, double init)
{
tensorIsNullCheck(t);
if (!op)
{
fprintf(stderr, "Error: reduce operator is NULL\n");
exit(1);
}
/* ---- scalar ---- */
if (t->ndim == 0)
return op(init, tensorGet(t, NULL));
double acc = init;
for (size_t i = 0; i < t->size; i++)
acc = op(acc, daGet(t->data, i));
return acc;
}
static double sum_op(double a, double b) { return a + b; }
static double prod_op(double a, double b) { return a * b; }
static double square(double x) { return x * x; }
static double min_op(double a, double b) { return a < b ? a : b; }
static double max_op(double a, double b) { return a > b ? a : b; }
static double and_op(double a, double b) { return (a != 0.0) && (b != 0.0); }
static double or_op(double a, double b) { return (a != 0.0) || (b != 0.0); }
static double neg(double x) { return -x; }
static double abs_val(double x) { return fabs(x); }
static double exp_func(double x) { return exp(x); }
static double log_func(double x) { return log(x); }
double tensorSum(const Tensor *t) { return tensorReduce(t, sum_op, 0.0); }
double tensorProd(const Tensor *t) { return tensorReduce(t, prod_op, 1.0); }
double tensorMin(const Tensor *t) { return tensorReduce(t, min_op, daGet(t->data, 0)); }
double tensorMax(const Tensor *t) { return tensorReduce(t, max_op, daGet(t->data, 0)); }
double tensorMean(const Tensor *t) { return tensorSum(t) / t->size; }
double tensorAll(const Tensor *t) { return tensorReduce(t, and_op, 1.0); }
double tensorAny(const Tensor *t) { return tensorReduce(t, or_op, 0.0); }
Tensor *tensorNeg(const Tensor *t) { return tensorApply(t, neg); }
Tensor *tensorAbs(const Tensor *t) { return tensorApply(t, abs_val); }
Tensor *tensorExp(const Tensor *t) { return tensorApply(t, exp_func); }
Tensor *tensorLog(const Tensor *t) { return tensorApply(t, log_func); }
size_t tensorArgMin(const Tensor *t)
{
tensorIsNullCheck(t);
size_t idx = 0;
double best = daGet(t->data, 0);
for (size_t i = 1; i < t->size; i++)
{
double v = daGet(t->data, i);
if (v < best)
{
best = v;
idx = i;
}
}
return idx;
}
size_t tensorArgMax(const Tensor *t)
{
tensorIsNullCheck(t);
size_t idx = 0;
double best = daGet(t->data, 0);
for (size_t i = 1; i < t->size; i++)
{
double v = daGet(t->data, i);
if (v > best)
{
best = v;
idx = i;
}
}
return idx;
}
double tensorL2Norm(const Tensor *t)
{
Tensor *tmp = tensorApply(t, square);
double sum = tensorSum(tmp);
tensorDestroy(tmp);
return sqrt(sum);
}
Tensor *tensorMul(const Tensor *a, const Tensor *b)
{
tensorIsNullCheck(a);
tensorIsNullCheck(b);
/* scalar cases */
if (a->ndim == 0)
return tensorScalarOp(b, daGet(a->data,0), mul_scalar);
if (b->ndim == 0)
return tensorScalarOp(a, daGet(b->data,0), mul_scalar);
/* vector dot */
if (a->ndim == 1 && b->ndim == 1)
return tensorDot(a, b);
/* matrix */
if (a->ndim == 2 && b->ndim == 2)
return tensorMatMul(a, b);
/* batched */
return tensorBatchedMatMul(a, b);
}
Tensor *tensorMatMul(const Tensor *a, const Tensor *b)
{
tensorIsNullCheck(a);
tensorIsNullCheck(b);
if (a->ndim != 2 || b->ndim != 2)
{
fprintf(stderr, "Error: matmul requires 2D tensors\n");
exit(1);
}
size_t m = a->shape[0];
size_t k1 = a->shape[1];
size_t k2 = b->shape[0];
size_t n = b->shape[1];
if (k1 != k2)
{
fprintf(stderr, "Error: incompatible shapes for matmul\n");
exit(1);
}
size_t result_shape[2] = {m, n};
Tensor *res = tensorCreate(2, result_shape);
for (size_t i = 0; i < m; i++)
{
for (size_t j = 0; j < n; j++)
{
double sum = 0.0;
for (size_t k = 0; k < k1; k++)
{
size_t ai = i * k1 + k;
size_t bi = k * n + j;
sum += daGet(a->data, ai) * daGet(b->data, bi);
}
daAppend(res->data, sum);
}
}
return res;
}
Tensor *tensorDot(const Tensor *a, const Tensor *b)
{
tensorIsNullCheck(a);
tensorIsNullCheck(b);
if (a->shape[0] != b->shape[0])
{
fprintf(stderr, "Error: dot product size mismatch\n");
exit(1);
}
double sum = 0.0;
for (size_t i = 0; i < a->shape[0]; i++)
sum += daGet(a->data,i) * daGet(b->data,i);
return tensorScalar(sum);
}
static size_t tensorBatchCount(const Tensor *t)
{
size_t count = 1;
for (size_t i = 0; i < t->ndim-2; i++)
count *= t->shape[i];
return count;
}
Tensor *tensorBatchedMatMul(const Tensor *a, const Tensor *b)
{
tensorIsNullCheck(a);
tensorIsNullCheck(b);
if (a->ndim != b->ndim || a->ndim < 3)
{
fprintf(stderr, "Error: invalid ranks for batched matmul\n");
exit(1);
}
size_t ndim = a->ndim;
for (size_t i = 0; i < ndim-2; i++)
if (a->shape[i] != b->shape[i])
{
fprintf(stderr, "Error: batch dimension mismatch\n");
exit(1);
}
size_t m = a->shape[ndim-2];
size_t k1 = a->shape[ndim-1];
size_t k2 = b->shape[ndim-2];
size_t n = b->shape[ndim-1];
if (k1 != k2)
{
fprintf(stderr, "Error: inner dimension mismatch\n");
exit(1);
}
/* build output shape */
size_t *out_shape = safeMalloc(sizeof(size_t)*ndim, "shape alloc");
for (size_t i = 0; i < ndim-2; i++)
out_shape[i] = a->shape[i];
out_shape[ndim-2] = m;
out_shape[ndim-1] = n;
Tensor *res = tensorCreate(ndim, out_shape);
free(out_shape);
size_t batch = tensorBatchCount(a);
size_t Ablock = m * k1;
size_t Bblock = k1 * n;
for (size_t bidx = 0; bidx < batch; bidx++)
{
size_t aBase = bidx * Ablock;
size_t bBase = bidx * Bblock;
for (size_t i = 0; i < m; i++)
for (size_t j = 0; j < n; j++)
{
double sum = 0;
for (size_t p = 0; p < k1; p++)
{
size_t ai = aBase + i*k1 + p;
size_t bi = bBase + p*n + j;
sum += daGet(a->data, ai) *
daGet(b->data, bi);
}
daAppend(res->data, sum);
}
}
return res;
}
Tensor *tensorTranspose(const Tensor *t, size_t axis1, size_t axis2)
{
tensorIsNullCheck(t);
if (axis1 >= t->ndim || axis2 >= t->ndim)
{
fprintf(stderr, "Error: invalid transpose axes\n");
exit(1);
}
if (axis1 == axis2)
return tensorCreateFromData(t->ndim, t->shape, t->data);
/* build new shape */
size_t *newShape = safeMalloc(sizeof(size_t)*t->ndim, "shape alloc");
for (size_t i = 0; i < t->ndim; i++) newShape[i] = t->shape[i];
size_t tmp = newShape[axis1];
newShape[axis1] = newShape[axis2];
newShape[axis2] = tmp;
Tensor *res = tensorCreate(t->ndim, newShape);
free(newShape);
size_t *idx = safeMalloc(sizeof(size_t)*t->ndim, "index buffer");
size_t *swapped = safeMalloc(sizeof(size_t)*t->ndim, "index buffer");
double *temp = safeMalloc(sizeof(double) * t->size, "temp array");
for (size_t i = 0; i < t->size; i++)
{
linearToIndices(i, t->shape, t->strides, t->ndim, idx);
for (size_t k = 0; k < t->ndim; k++) swapped[k] = idx[k];
tmp = swapped[axis1];
swapped[axis1] = swapped[axis2];
swapped[axis2] = tmp;
size_t outLinear = indicesToLinear(swapped, res->strides, res->ndim);
temp[outLinear] = daGet(t->data, i);
}
for (size_t i = 0; i < t->size; i++) {
daAppend(res->data, temp[i]);
}
free(temp);
free(idx);
free(swapped);
return res;
}
Tensor *tensorTranspose2D(const Tensor *t)
{
if (t->ndim != 2)
{
fprintf(stderr, "Error: not a matrix\n");
exit(1);
}
return tensorTranspose(t, 0, 1);
}
Tensor *tensorReshape(const Tensor *t, size_t new_ndim, const size_t *new_shape)
{
tensorIsNullCheck(t);
if (new_ndim > 0 && !new_shape)
{
fprintf(stderr, "Error: new shape is NULL\n");
exit(1);
}
size_t new_size = tensorComputeSize(new_ndim, new_shape);
if (new_size != t->size)
{
fprintf(stderr, "Error: reshape changes total size (%zu -> %zu)\n", t->size, new_size);
exit(1);
}
Tensor *res = tensorCreate(new_ndim, new_shape);
for (size_t i = 0; i < t->size; i++) daAppend(res->data, daGet(t->data, i));
return res;
}
Tensor *tensorReshapeInfer(const Tensor *t, size_t new_ndim, const size_t *shape)
{
tensorIsNullCheck(t);
if (new_ndim > 0 && !shape)
{
fprintf(stderr, "Error: new shape is NULL\n");
exit(1);
}
size_t infer_index = SIZE_MAX;
size_t known_product = 1;
for (size_t i = 0; i < new_ndim; i++)
{
if (shape[i] == SIZE_MAX)
{
if (infer_index != SIZE_MAX)
{
fprintf(stderr, "Error: multiple inferred dimensions\n");
exit(1);
}
infer_index = i;
}
else
{
known_product *= shape[i];
}
}
size_t *final_shape = safeMalloc(sizeof(size_t)*new_ndim, "shape alloc");
memcpy(final_shape, shape, sizeof(size_t)*new_ndim);
if (infer_index != SIZE_MAX)
{
if (known_product == 0 || t->size % known_product != 0)
{
fprintf(stderr, "Error: cannot infer reshape dimension\n");
exit(1);
}
final_shape[infer_index] = t->size / known_product;
}
size_t new_size = tensorComputeSize(new_ndim, final_shape);
if (new_size != t->size)
{
fprintf(stderr, "Error: reshape changes total size (%zu -> %zu)\n", t->size, new_size);
exit(1);
}
Tensor *res = tensorCreate(new_ndim, final_shape);
for (size_t i = 0; i < t->size; i++) daAppend(res->data, daGet(t->data, i));
free(final_shape);
return res;
}