private static void revertIndex(ParseToken token, int index) { if (token instanceof ParseTokenSimpleTensor) { ParseTokenSimpleTensor pToken = (ParseTokenSimpleTensor) token; SimpleIndices indices = pToken.indices; for (int i = 0; i < indices.size(); i++) { if (indices.get(i) == index) { int[] inds = indices.toArray(); inds[i] = IndicesUtils.inverseIndexState(index); pToken.indices = IndicesFactory.createSimple(null, inds); break; } } } else if (token.tokenType == TokenType.Product || token.tokenType == TokenType.Trace || token.tokenType == TokenType.Sum) for (ParseToken c : token.content) revertIndex(c, index); } }
@Override public ParseToken parseToken(String expression, Parser parser) { ParseToken node = super.parseToken(expression, parser); if (node == null || !parser.isAllowSameVariance()) return node; TIntHashSet indices = new TIntHashSet(); for (ParseToken c : node.content) { Indices free = c.getIndices().getFree(); for (int i = 0; i < free.size(); i++) { int ind = free.get(i); if (indices.contains(ind)) revertIndex(c, ind); else indices.add(ind); } } return node; }