4 // n is the total number of things on the stack. The first thing on the stack
5 // is the object to be indexed, followed by the indices themselves.
10 int i
, k
, m
, ndim
, nelem
, t
;
26 ndim
= p1
->u
.tensor
->ndim
;
31 stop("too many indices for tensor");
35 for (i
= 0; i
< m
; i
++) {
38 if (t
< 1 || t
> p1
->u
.tensor
->dim
[i
])
39 stop("index out of range");
40 k
= k
* p1
->u
.tensor
->dim
[i
] + t
- 1;
45 push(p1
->u
.tensor
->elem
[k
]);
50 for (i
= m
; i
< ndim
; i
++)
51 k
= k
* p1
->u
.tensor
->dim
[i
] + 0;
55 for (i
= m
; i
< ndim
; i
++)
56 nelem
*= p1
->u
.tensor
->dim
[i
];
58 p2
= alloc_tensor(nelem
);
60 p2
->u
.tensor
->ndim
= ndim
- m
;
62 for (i
= m
; i
< ndim
; i
++)
63 p2
->u
.tensor
->dim
[i
- m
] = p1
->u
.tensor
->dim
[i
];
65 for (i
= 0; i
< nelem
; i
++)
66 p2
->u
.tensor
->elem
[i
] = p1
->u
.tensor
->elem
[k
+ i
];
73 //-----------------------------------------------------------------------------
75 // Input: n Number of args on stack
77 // tos-n Right-hand value
79 // tos-n+1 Left-hand value
81 // tos-n+2 First index
89 // Output: Result on stack
91 //-----------------------------------------------------------------------------
100 int i
, k
, m
, ndim
, t
;
106 stop("error in indexed assign");
114 if (!istensor(LVALUE
))
115 stop("error in indexed assign");
117 ndim
= LVALUE
->u
.tensor
->ndim
;
122 stop("error in indexed assign");
126 for (i
= 0; i
< m
; i
++) {
129 if (t
< 1 || t
> LVALUE
->u
.tensor
->dim
[i
])
130 stop("error in indexed assign");
131 k
= k
* p1
->u
.tensor
->dim
[i
] + t
- 1;
134 for (i
= m
; i
< ndim
; i
++)
135 k
= k
* p1
->u
.tensor
->dim
[i
] + 0;
139 TMP
= alloc_tensor(LVALUE
->u
.tensor
->nelem
);
141 TMP
->u
.tensor
->ndim
= LVALUE
->u
.tensor
->ndim
;
143 for (i
= 0; i
< p1
->u
.tensor
->ndim
; i
++)
144 TMP
->u
.tensor
->dim
[i
] = LVALUE
->u
.tensor
->dim
[i
];
146 for (i
= 0; i
< p1
->u
.tensor
->nelem
; i
++)
147 TMP
->u
.tensor
->elem
[i
] = LVALUE
->u
.tensor
->elem
[i
];
152 if (istensor(RVALUE
))
153 stop("error in indexed assign");
154 LVALUE
->u
.tensor
->elem
[k
] = RVALUE
;
161 // see if the rvalue matches
163 if (!istensor(RVALUE
))
164 stop("error in indexed assign");
166 if (ndim
- m
!= RVALUE
->u
.tensor
->ndim
)
167 stop("error in indexed assign");
169 for (i
= 0; i
< RVALUE
->u
.tensor
->ndim
; i
++)
170 if (LVALUE
->u
.tensor
->dim
[m
+ i
] != RVALUE
->u
.tensor
->dim
[i
])
171 stop("error in indexed assign");
175 for (i
= 0; i
< RVALUE
->u
.tensor
->nelem
; i
++)
176 LVALUE
->u
.tensor
->elem
[k
+ i
] = RVALUE
->u
.tensor
->elem
[i
];
189 "A=((A11,A12),(A21,A22))",
214 "((B11,B12),(A21,A22))",
220 "((B11,B12),(B21,B22))",
228 // index of scalar ok
237 test(__FILE__
, s
, sizeof s
/ sizeof (char *));