1 // Transpose tensor indices
11 if (cddr(p1
) == symbol(NIL
)) {
26 int i
, j
, k
, l
, m
, ndim
, nelem
, t
;
27 int ai
[MAXDIM
], an
[MAXDIM
];
38 stop("transpose: tensor expected, 1st arg is not a tensor");
44 ndim
= p1
->u
.tensor
->ndim
;
45 nelem
= p1
->u
.tensor
->nelem
;
61 if (l
< 1 || l
> ndim
|| m
< 1 || m
> ndim
)
62 stop("transpose: index out of range");
67 p2
= alloc_tensor(nelem
);
69 p2
->u
.tensor
->ndim
= ndim
;
71 for (i
= 0; i
< ndim
; i
++)
72 p2
->u
.tensor
->dim
[i
] = p1
->u
.tensor
->dim
[i
];
74 p2
->u
.tensor
->dim
[l
] = p1
->u
.tensor
->dim
[m
];
75 p2
->u
.tensor
->dim
[m
] = p1
->u
.tensor
->dim
[l
];
77 a
= p1
->u
.tensor
->elem
;
78 b
= p2
->u
.tensor
->elem
;
82 for (i
= 0; i
< ndim
; i
++) {
84 an
[i
] = p1
->u
.tensor
->dim
[i
];
87 // copy components from a to b
89 for (i
= 0; i
< nelem
; i
++) {
91 // swap indices l and m
93 t
= ai
[l
]; ai
[l
] = ai
[m
]; ai
[m
] = t
;
94 t
= an
[l
]; an
[l
] = an
[m
]; an
[m
] = t
;
96 // convert tensor index to linear index k
99 for (j
= 0; j
< ndim
; j
++)
100 k
= (k
* an
[j
]) + ai
[j
];
104 t
= ai
[l
]; ai
[l
] = ai
[m
]; ai
[m
] = t
;
105 t
= an
[l
]; an
[l
] = an
[m
]; an
[m
] = t
;
111 // increment tensor index
113 // Suppose the tensor dimensions are 2 and 3.
114 // Then the tensor index ai increments as follows:
122 for (j
= ndim
- 1; j
>= 0; j
--) {
143 "transpose(((a,b),(c,d)))",
146 "transpose(((a,b),(c,d)),1,2)",
149 "transpose(((a,b,c),(d,e,f)),1,2)",
150 "((a,d),(b,e),(c,f))",
152 "transpose(((a,d),(b,e),(c,f)),1,2)",
155 "transpose((a,b,c))",
162 test(__FILE__
, s
, sizeof s
/ sizeof (char *));