/** * Allows to parse expressions with repeated indices of the same variance (like T_aa or T_a*T^a which results in T^a_a * and T^a*T_a respactively) * * @param b allow or not to parse repeated indices with same variance */ public void setParserAllowsSameVariance(boolean b) { parseManager.getParser().setAllowSameVariance(b); }
/** * Returns whether repeated indices of the same variance are allowed to be parsed * * @return whether repeated indices of the same variance are allowed to be parsed */ public boolean getParserAllowsSameVariance() { return parseManager.getParser().isAllowSameVariance(); }
protected Tensor[] contentToTensors() { Tensor[] tensors = new Tensor[content.length]; for (int i = 0; i < content.length; ++i) tensors[i] = content[i].toTensor(); return tensors; }
/** * @param expression string expression * @param tensorPreprocessors transformation * @param nodesPreprocessors AST transformers * @return tensor */ public Tensor parse(String expression, Transformation[] tensorPreprocessors, ParseTokenTransformer[] nodesPreprocessors) { ParseToken node = parser.parse(expression); for (ParseTokenTransformer tr : nodesPreprocessors) node = tr.transform(node); Tensor t = node.toTensor(); for (Transformation tr : tensorPreprocessors) if (tr != t) t = tr.transform(t); return t; }
private void modeParser(String expression, Mode mode, Parser parser, List<ParseToken> nodes) { if (mode == Mode.Direct) { nodes.add(parser.parse(expression)); return; } if (mode == Mode.Inverse) nodes.add(inverseOperation(parser.parse(expression))); else throw new ParserException("unrepoted operator parser mode"); }
@Override public ParseToken parseToken(String expression, Parser parser) { if (!expression.contains("=")) return null; if (expression.indexOf('=') != expression.lastIndexOf('=')) throw new ParserException("Several '=' symbols."); String[] parts = expression.split("="); parts[0] = parts[0].trim(); boolean preprocessing = false; if (parts[0].charAt(parts[0].length() - 1) == ':') { preprocessing = true; parts[0] = parts[0].substring(0, parts[0].length() - 1); } ParseToken left = parser.parse(parts[0]); ParseToken right = parser.parse(parts[1]); return new ParseTokenExpression(preprocessing, left, right); }
@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; }
@Override protected ParseToken inverseOperation(ParseToken node) { return new ParseToken(TokenType.Power, node, new ParseTokenNumber(Complex.MINUS_ONE)); }
private static void getAllIndices1(ParseToken node, Set<Integer> set) { if (node instanceof ParseTokenSimpleTensor) { Indices indices = node.getIndices(); for (int i = indices.size() - 1; i >= 0; --i) set.add(IndicesUtils.getNameWithType(indices.get(i))); } else for (ParseToken pn : node.content) if (!(pn instanceof ParseTokenScalarFunction)) getAllIndices1(pn, set); }
/** * @param expression string expression * @return tensor */ public Tensor parse(String expression) { return parse(expression, defaultTensorPreprocessors, defaultParserPreprocessors); }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; final ParseTokenScalarFunction other = (ParseTokenScalarFunction) obj; return Objects.equals(this.function, other.function); } }
/** * @param tokenType node type * @param content child nodes */ public ParseToken(TokenType tokenType, ParseToken... content) { this.tokenType = tokenType; this.content = content; for (ParseToken node : content) node.setParent(this); }
public static Set<Integer> getAllIndices(ParseToken node) { Set<Integer> s = new HashSet<>(); getAllIndices1(node, s); return s; }
/** * Parses string expression and returns indices. * * @param expression string representation of indices * @return integer indices * @throws IllegalArgumentException if string does not represent correct indices object. */ public static SimpleIndices parseSimple(String expression) { return IndicesFactory.createSimple(null, parse(expression)); }
/** * @param expression string expression * @param tensorPreprocessors transformation * @param nodesPreprocessors AST transformers * @return tensor */ public Tensor parse(String expression, List<Transformation> tensorPreprocessors, List<ParseTokenTransformer> nodesPreprocessors) { ParseToken node = parser.parse(expression); for (ParseTokenTransformer tr : nodesPreprocessors) node = tr.transform(node); Tensor t = node.toTensor(); for (Transformation tr : tensorPreprocessors) if (tr != t) t = tr.transform(t); return t; }
@Override protected ParseToken inverseOperation(ParseToken node) { ParseToken[] content; if (node.tokenType == TokenType.Product) { content = new ParseToken[1 + node.content.length]; content[0] = new ParseTokenNumber(Complex.MINUS_ONE); System.arraycopy(node.content, 0, content, 1, node.content.length); } else content = new ParseToken[]{ new ParseTokenNumber(Complex.MINUS_ONE), node }; return new ParseToken(TokenType.Product, content); }
/** * @param expression string expression * @param nodesPreprocessors AST transformers * @return tensor */ public Tensor parse(String expression, ParseTokenTransformer... nodesPreprocessors) { return parse(expression, new Transformation[0], nodesPreprocessors); }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; final ParseTokenSimpleTensor other = (ParseTokenSimpleTensor) obj; if (!Objects.equals(this.indices, other.indices)) return false; return Objects.equals(this.name, other.name); }