protected Tensor[] contentToTensors() { Tensor[] tensors = new Tensor[content.length]; for (int i = 0; i < content.length; ++i) tensors[i] = content[i].toTensor(); return tensors; }
@Override protected ParseToken compile(List<ParseToken> nodes) { return new ParseToken(TokenType.Sum, nodes.toArray(new ParseToken[nodes.size()])); }
/** * Converts this AST to tensor. * * @return resulting tensor */ public Tensor toTensor() { switch (tokenType) { case Sum: return Tensors.sum(contentToTensors()); case Power: assert content.length == 2; return Tensors.pow(content[0].toTensor(), content[1].toTensor()); case Trace: case Product: return Tensors.multiplyAndRenameConflictingDummies(contentToTensors()); } throw new ParserException("Unknown tensor type: " + tokenType); }
@Override public Indices getIndices() { return content[0].getIndices().getFree(); }
&& simpleTensorNode.indices.size() == 0 && arguments.size() == 1 && arguments.get(0).getIndices().getFree().size() == 0) return new ParseToken(TokenType.Power, new ParseToken[]{arguments.get(0), new ParseTokenNumber(Complex.ONE_HALF)}); return new ParseToken(TokenType.Trace, arguments.toArray(new ParseToken[arguments.size()]));
@Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; final ParseTokenScalarFunction other = (ParseTokenScalarFunction) obj; return Objects.equals(this.function, other.function); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(super.toString()).append('['); for (ParseToken node : content) sb.append(node.toString()).append(", "); sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1).append(']'); return sb.toString(); }
/** * @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); }
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); }
@Override public ParseToken transform(ParseToken node) { TokenType type = node.tokenType; switch (type) { case SimpleTensor: ParseTokenSimpleTensor st = (ParseTokenSimpleTensor) node; NameAndStructureOfIndices ds = st.getIndicesTypeStructureAndName(); return new ParseTokenSimpleTensor(transformIndices(st.getIndices(), ds), transformer.newName(ds.getName(), ds)); case TensorField: ParseTokenTensorField tf = (ParseTokenTensorField) node; ParseToken[] newContent = transformContent(tf.content); SimpleIndices[] newArgsIndices = new SimpleIndices[tf.argumentsIndices.length]; for (int i = newArgsIndices.length - 1; i >= 0; --i) newArgsIndices[i] = IndicesFactory.createSimple(null, newContent[i].getIndices()); NameAndStructureOfIndices dsf = tf.getIndicesTypeStructureAndName(); return new ParseTokenTensorField(transformIndices(tf.getIndices(), dsf), transformer.newName(dsf.getName(), dsf), newContent, newArgsIndices); case Number: return node; case ScalarFunction: return new ParseTokenScalarFunction(((ParseTokenScalarFunction) node).function, transformContent(node.content)); case Expression: ParseToken[] nContent = transformContent(node.content); return new ParseTokenExpression(((ParseTokenExpression) node).preprocess, nContent[0], nContent[1]); default: return new ParseToken(node.tokenType, transformContent(node.content)); } }
@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); }
public String toString(OutputFormat mode) { StringBuilder sb = new StringBuilder(); switch (tokenType) { case Trace: sb.append("Tr["); case Product: char operatorChar = mode.is(OutputFormat.LaTeX) ? ' ' : '*'; for (int i = 0; ; ++i) { sb.append(content[i].toString(mode)); if (i == content.length - 1) return tokenType == TokenType.Trace ? sb.append("]").toString() : sb.toString(); sb.append(operatorChar); } //throw new RuntimeException(); case Sum: sb.append("("); String temp; for (int i = 0; ; ++i) { temp = content[i].toString(mode); if ((temp.charAt(0) == '-' || temp.charAt(0) == '+') && sb.length() != 0) sb.deleteCharAt(sb.length() - 1); sb.append(content[i].toString(mode)); if (i == content.length - 1) return sb.append(")").toString(); sb.append('+'); } //throw new RuntimeException(); } throw new RuntimeException("Unsupported token type."); }
DiracSimplify1(DiracOptions options) { super(options, IDENTITY); ParseToken[] ss = {s1, s2, s3, s4}; this.subs = new Expression[ss.length]; for (int i = 0; i < ss.length; ++i) subs[i] = (Expression) deltaTrace.transform(tokenTransformer.transform(ss[i]).toTensor()); }
/** * Returns {@link Indices} of the corresponding mathematical expression. * * @return {@link Indices} of the corresponding mathematical expression */ public Indices getIndices() { switch (tokenType) { case Product: case Trace: IndicesBuilder builder = new IndicesBuilder(); for (ParseToken node : content) builder.append(node.getIndices()); return builder.getIndices(); case Sum: return IndicesFactory.create(content[0].getIndices()); case Power: return IndicesFactory.EMPTY_INDICES; } throw new ParserException("Unknown tensor type: " + tokenType); }
@Override protected ParseToken compile(List<ParseToken> nodes) { return new ParseToken(TokenType.Product, nodes.toArray(new ParseToken[nodes.size()])); }
UnitarySimplifyTransformation(final Tensor dimension, ChangeIndicesTypesAndTensorNames tokenTransformer) { //simplifications with SU(N) combinations ArrayList<Transformation> unitarySimplifications = new ArrayList<>(); if (dimension instanceof Complex) { Transformation nSub = parseExpression("N = " + dimension); for (ParseToken substitution : unitarySimplificationsTokens) unitarySimplifications.add((Transformation) nSub.transform(tokenTransformer.transform(substitution).toTensor())); } else for (ParseToken substitution : unitarySimplificationsTokens) unitarySimplifications.add((Transformation) tokenTransformer.transform(substitution).toTensor()); //all simplifications ArrayList<Transformation> simplifications = new ArrayList<>(); simplifications.add(EliminateMetricsTransformation.ELIMINATE_METRICS); simplifications.addAll(unitarySimplifications); this.unitarySimplifications = new TransformationCollection(simplifications); }
public ParseTokenDerivative(TokenType tokenType, ParseToken... content) { super(tokenType, content); IndicesBuilder ib = new IndicesBuilder(); ib.append(content[0].getIndices().getFree()); for (int i = content.length - 1; i >= 1; --i) ib.append(content[i].getIndices().getInverted().getFree()); indices = ib.getIndices(); }
@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); }
@Override public Tensor toTensor() { Tensor expression = Tensors.expression(content[0].toTensor(), content[1].toTensor()); if (preprocess) { for (Transformation tr : Context.get().getParseManager().defaultTensorPreprocessors) expression = tr.transform(expression); Context.get().getParseManager().defaultTensorPreprocessors.add((Transformation) expression); } return expression; } }
private static void getAllIndicesT1(ParseToken node, TIntSet 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)) getAllIndicesT1(pn, set); } }