@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; } }
/** * This method initializes and sets current session context from * the specified {@code context settings} ({@link ContextSettings}). * After invocation of this method, all the tensors that exist in * the current thread will be invalidated. * * @return created context */ public static Context initializeNew(ContextSettings contextSettings) { Context context = new Context(contextSettings); threadLocalContainer.get().context = context; return context; }
/** * Returns a delta function with specified arguments * * @param a tensor * @param b tensor * @return DiracDelta[a, b] */ public static TensorField createDiracDelta(Tensor a, Tensor b) { return CC.current().createDeltaFunction(a, b); }
/** * Returns a string representation of a tensor according to the default * {@link cc.redberry.core.context.OutputFormat} defined in * {@link cc.redberry.core.context.CC#getDefaultOutputFormat()}. * * @return a string representation of a tensor */ @Override public final String toString() { return toString(Context.get().getDefaultOutputFormat()); }
public static String toString(int index, OutputFormat mode) { return (getState(index) ? "^" : "_") + Context.get().getIndexConverterManager().getSymbol(index, mode) + ""; }
/** * Converts string expression into tensor. * * @param expression string to be parsed * @return result of parsing * @throws cc.redberry.core.parser.ParserException if expression does not satisfy correct Redberry * input notation for tensors */ public static Tensor parse(String expression) { return CC.current().getParseManager().parse(expression); }
/** * Returns metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states. * * @param index1 first index * @param index2 second index * @return metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states * @throws IllegalArgumentException if indices have different types * @throws IllegalArgumentException if indices have same states and non metric types */ public SimpleTensor createMetricOrKronecker(int index1, int index2) { if (IndicesUtils.getRawStateInt(index1) == IndicesUtils.getRawStateInt(index2)) return createMetric(index1, index2); return createKronecker(index1, index2); }
@Override public SimpleTensor take() { return generateNewSymbol(); } }
/** * Returns metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states. * * @param index1 first index * @param index2 second index * @return metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states * @throws IllegalArgumentException if indices have different types * @throws IllegalArgumentException if indices have same states and non metric types */ public static SimpleTensor createMetricOrKronecker(int index1, int index2) { return CC.current().createMetricOrKronecker(index1, index2); }
/** * Returns the current context of Redberry session. * * @return the current context of Redberry session. */ public static Context current() { return Context.get(); }
/** * Returns Kronecker tensor with specified upper and lower indices. * * @param index1 first index * @param index2 second index * @return Kronecker tensor with specified upper and lower indices * @throws IllegalArgumentException if indices have same states * @throws IllegalArgumentException if indices have different types */ public static SimpleTensor createKronecker(int index1, int index2) { return CC.current().createKronecker(index1, index2); }
/** * Returns current default output format. * * @return current default output format */ public static OutputFormat getDefaultOutputFormat() { return current().getDefaultOutputFormat(); }
/** * Return output port which generates new symbol via {@link #generateNewSymbol()} at each {@code take()} invocation. * * @return output port which generates new symbol via {@link #generateNewSymbol()} at each {@code take()} invocation. */ public static OutputPort<SimpleTensor> getParametersGenerator() { return current().getDefaultParametersGenerator(); }
/** * Returns metric tensor with specified indices. * * @param index1 first index * @param index2 second index * @return metric tensor with specified indices * @throws IllegalArgumentException if indices have different states * @throws IllegalArgumentException if indices have different types * @throws IllegalArgumentException if indices have non metric types */ public static SimpleTensor createMetric(int index1, int index2) { return CC.current().createMetric(index1, index2); }
/** * Parse single index. * * @param string string representation of index * @return integer representation of index */ public static int parseIndex(String string) { string = string.trim(); boolean state = string.charAt(0) == '^'; int start = 0; if (string.charAt(0) == '^' || string.charAt(0) == '_') start = 1; int nameWithType; if (string.charAt(start) == '{') nameWithType = Context.get().getIndexConverterManager().getCode(string.substring(start + 1, string.length() - 1)); else nameWithType = Context.get().getIndexConverterManager().getCode(string.substring(start)); return state ? (0x80000000 ^ nameWithType) : nameWithType; }
/** * Converts string expression into tensor, additionally transforming AST according to specified * AST transformers. * * @param expression string to be parsed * @param preprocessors AST transformers * @return result of parsing * @throws cc.redberry.core.parser.ParserException if expression does not satisfy correct Redberry * input notation for tensors */ public static Tensor parse(String expression, ParseTokenTransformer... preprocessors) { return CC.current().getParseManager().parse(expression, preprocessors); }
@Override public final String toString() { return toString(Context.get().getDefaultOutputFormat()); }
/** * Generates a new symbol which never used before during current session. * * @return new symbol which never used before during current session */ public static SimpleTensor generateNewSymbol() { return current().generateNewSymbol(); }
/** * Returns metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states. * * @param indices indices * @return metric tensor if specified indices have same states and * Kronecker tensor if specified indices have different states * @throws IllegalArgumentException if indices have different types * @throws IllegalArgumentException if indices have same states and non metric types * @throws IllegalArgumentException if indices.size() != 2 */ public static SimpleTensor createMetricOrKronecker(Indices indices) { if (indices.size() != 2) throw new IllegalArgumentException("Inconsistent indices for metric: " + indices); return CC.current().createMetricOrKronecker(indices.get(0), indices.get(1)); }
private static String toStringIndex(int index, OutputFormat mode) { return (IndicesUtils.getState(index) ? "^" : "_") + Context.get().getIndexConverterManager().getSymbol(index, mode); }