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
|
/*
* Copyright (c) 2003 Matteo Frigo
* Copyright (c) 2003 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* $Id: tensor.c,v 1.1 2008/10/17 06:11:29 scuri Exp $ */
#include "ifftw.h"
tensor *X(mktensor)(int rnk)
{
tensor *x;
A(rnk >= 0);
#if defined(STRUCT_HACK_KR)
if (FINITE_RNK(rnk) && rnk > 1)
x = (tensor *)MALLOC(sizeof(tensor) + (rnk - 1) * sizeof(iodim),
TENSORS);
else
x = (tensor *)MALLOC(sizeof(tensor), TENSORS);
#elif defined(STRUCT_HACK_C99)
if (FINITE_RNK(rnk))
x = (tensor *)MALLOC(sizeof(tensor) + rnk * sizeof(iodim),
TENSORS);
else
x = (tensor *)MALLOC(sizeof(tensor), TENSORS);
#else
x = (tensor *)MALLOC(sizeof(tensor), TENSORS);
if (FINITE_RNK(rnk) && rnk > 0)
x->dims = (iodim *)MALLOC(sizeof(iodim) * rnk, TENSORS);
else
x->dims = 0;
#endif
x->rnk = rnk;
return x;
}
void X(tensor_destroy)(tensor *sz)
{
#if !defined(STRUCT_HACK_C99) && !defined(STRUCT_HACK_KR)
X(ifree0)(sz->dims);
#endif
X(ifree)(sz);
}
int X(tensor_sz)(const tensor *sz)
{
int i, n = 1;
if (!FINITE_RNK(sz->rnk))
return 0;
for (i = 0; i < sz->rnk; ++i)
n *= sz->dims[i].n;
return n;
}
void X(tensor_md5)(md5 *p, const tensor *t)
{
int i;
X(md5int)(p, t->rnk);
if (FINITE_RNK(t->rnk)) {
for (i = 0; i < t->rnk; ++i) {
const iodim *q = t->dims + i;
X(md5int)(p, q->n);
X(md5int)(p, q->is);
X(md5int)(p, q->os);
}
}
}
/* treat a (rank <= 1)-tensor as a rank-1 tensor, extracting
appropriate n, is, and os components */
int X(tensor_tornk1)(const tensor *t, int *n, int *is, int *os)
{
A(t->rnk <= 1);
if (t->rnk == 1) {
const iodim *vd = t->dims;
*n = vd[0].n;
*is = vd[0].is;
*os = vd[0].os;
} else {
*n = 1;
*is = *os = 0;
}
return 1;
}
void X(tensor_print)(const tensor *x, printer *p)
{
if (FINITE_RNK(x->rnk)) {
int i;
int first = 1;
p->print(p, "(");
for (i = 0; i < x->rnk; ++i) {
const iodim *d = x->dims + i;
p->print(p, "%s(%d %d %d)",
first ? "" : " ",
d->n, d->is, d->os);
first = 0;
}
p->print(p, ")");
} else {
p->print(p, "rank-minfty");
}
}
|