+/** Contraction of an indexed spinor metric with something else. */
+bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
+{
+ GINAC_ASSERT(is_ex_of_type(*self, indexed));
+ GINAC_ASSERT(is_ex_of_type(*other, indexed));
+ GINAC_ASSERT(self->nops() == 3);
+ GINAC_ASSERT(is_ex_of_type(self->op(0), spinmetric));
+
+ // Contractions between spinor metrics
+ if (is_ex_of_type(other->op(0), spinmetric)) {
+ const idx &self_i1 = ex_to<idx>(self->op(1));
+ const idx &self_i2 = ex_to<idx>(self->op(2));
+ const idx &other_i1 = ex_to<idx>(other->op(1));
+ const idx &other_i2 = ex_to<idx>(other->op(2));
+
+ if (is_dummy_pair(self_i1, other_i1)) {
+ if (is_dummy_pair(self_i2, other_i2))
+ *self = _ex2();
+ else
+ *self = delta_tensor(self_i2, other_i2);
+ *other = _ex1();
+ return true;
+ } else if (is_dummy_pair(self_i1, other_i2)) {
+ if (is_dummy_pair(self_i2, other_i1))
+ *self = _ex_2();
+ else
+ *self = -delta_tensor(self_i2, other_i1);
+ *other = _ex1();
+ return true;
+ } else if (is_dummy_pair(self_i2, other_i1)) {
+ *self = -delta_tensor(self_i1, other_i2);
+ *other = _ex1();
+ return true;
+ } else if (is_dummy_pair(self_i2, other_i2)) {
+ *self = delta_tensor(self_i1, other_i1);
+ *other = _ex1();
+ return true;
+ }
+ }
+
+ // If contracting with the delta tensor, let the delta do it
+ // (don't raise/lower delta indices)
+ if (is_ex_of_type(other->op(0), tensdelta))
+ return false;
+
+ // Try to contract first index
+ const idx *self_idx = &ex_to<idx>(self->op(1));
+ const idx *free_idx = &ex_to<idx>(self->op(2));
+ bool first_index_tried = false;
+ int sign = 1;
+
+again:
+ if (self_idx->is_symbolic()) {
+ for (int i=1; i<other->nops(); i++) {
+ const idx &other_idx = ex_to<idx>(other->op(i));
+ if (is_dummy_pair(*self_idx, other_idx)) {
+
+ // Contraction found, remove metric tensor and substitute
+ // index in second object
+ *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
+ *other = other->subs(other_idx == *free_idx);
+ return true;
+ }
+ }
+ }
+
+ if (!first_index_tried) {
+
+ // No contraction with first index found, try second index
+ self_idx = &ex_to<idx>(self->op(2));
+ free_idx = &ex_to<idx>(self->op(1));
+ first_index_tried = true;
+ sign = -sign;
+ goto again;
+ }
+
+ return false;
+}
+