/** * Returns index converter manager of current session. * * @return index converter manager of current session */ public static IndexConverterManager getIndexConverterManager() { return current().getIndexConverterManager(); }
@Override public String toString() { return toString(CC.getDefaultOutputFormat()); } }
public TensorException(String message, Tensor... tensors) { super("\"" + message + "\" in tensors " + Arrays.toString(tensors) + "\n nmseed: " + CC.getNameManager().getSeed()); this.tensors = tensors; }
private StructureOfIndices() { this.size = 0; for (int i = 0; i < IndexType.TYPES_COUNT; ++i) if (!CC.isMetric((byte) i)) this.states[i] = BitArray.EMPTY; }
/** * Adds new insertion rule to this transformer. <p/> <p>After rule is added you can omit indices of specified type * in specified simple tensors, when this transformer is passed to {@link cc.redberry.core.tensor.Tensors#parse(String, * cc.redberry.core.parser.ParseTokenTransformer...)} method or somehow added to default parse nodes * preprocessor.</p> * * @param tensor simple tensor * @param omittedIndexType type of indices that may be omitted */ public void addInsertionRule(SimpleTensor tensor, IndexType omittedIndexType) { addInsertionRule(CC.getNameDescriptor(tensor.getName()), omittedIndexType); }
static final IndexType[] extractTypesFromMatrix(SimpleTensor matrix) { if (matrix.getIndices().size() != 3) throw new IllegalArgumentException("Not a matrix: " + matrix + "."); NameDescriptor descriptor = CC.getNameDescriptor(matrix.getName()); StructureOfIndices typeStructure = descriptor.getStructureOfIndices(); byte metricType = -1, matrixType = -1; int typeCount; for (byte type = 0; type < IndexType.TYPES_COUNT; ++type) { typeCount = typeStructure.typeCount(type); if (typeCount == 0) continue; else if (typeCount == 2) { if (matrixType != -1) throw new IllegalArgumentException("Not a matrix: " + matrix + "."); matrixType = type; if (CC.isMetric(matrixType)) throw new IllegalArgumentException("Not a matrix: " + matrix + "."); } else if (typeCount == 1) { if (metricType != -1) throw new IllegalArgumentException("Not a matrix: " + matrix + "."); metricType = type; if (!CC.isMetric(metricType)) throw new IllegalArgumentException("Not a matrix: " + matrix + "."); } else throw new IllegalArgumentException("Not a matrix: " + matrix + "."); } return new IndexType[]{IndexType.getType(metricType), IndexType.getType(matrixType)}; }
/** * Returns uniformly distributed random permutation from this group. * * @return uniformly distributed random permutation from this group */ public Permutation randomElement() { return randomElement(CC.getRandomGenerator()); }
/** * @deprecated very unsafe method without checks */ @Deprecated public static Tensor multiplySumElementsOnFactors(Sum sum) { final Tensor[] newSumData = new Tensor[sum.size()]; for (int i = newSumData.length - 1; i >= 0; --i) newSumData[i] = multiply(CC.generateNewSymbol(), sum.get(i)); return new Sum(newSumData, IndicesFactory.create(newSumData[0].getIndices().getFree())); }
/** * Generates a set of replacement rules for all scalar (but not symbolic) sub-tensors appearing in the specified * tensor. * * @param tensor tensor * @return set of replacement rules for all scalar (but not symbolic) sub-tensors appearing in the specified * tensor */ public static Expression[] generateReplacementsOfScalars(Tensor tensor) { return generateReplacementsOfScalars(tensor, CC.getParametersGenerator()); }
static void parse(String expression, IntArrayList indices, int state) { Matcher matcher = pattern.matcher(expression); String singleIndex; while (matcher.find()) { singleIndex = matcher.group(); indices.add(CC.getIndexConverterManager().getCode(singleIndex) | state); } String remainder = matcher.replaceAll(""); remainder = remainder.replaceAll("[\\{\\}\\s]*", ""); if (remainder.length() != 0) throw new ParserException("Incorrect indices: " + expression); } }
/** * Creates structure of indices, which contains indices only of specified metric type. * * @param type index type * @param count number of indices * @throws IllegalArgumentException if type is non metric */ private StructureOfIndices(byte type, int count) { if (!CC.isMetric(type)) throw new IllegalArgumentException("No states information provided for non metric type."); typesCounts[type] = count; size = count; for (int i = 0; i < IndexType.TYPES_COUNT; ++i) if (!CC.isMetric((byte) i)) states[i] = BitArray.EMPTY; }
private static String newCoefficientName(Tensor... tensors) { Set<SimpleTensor> simpleTensors = TensorUtils.getAllSymbols(tensors); List<Character> forbidden = new ArrayList<>(); for (SimpleTensor tensor : simpleTensors) { String name = CC.getNameDescriptor(tensor.getName()).getName(tensor.getIndices()); try { Integer.parseInt(name.substring(1)); forbidden.add(name.charAt(0)); } catch (NumberFormatException e) { } } Collections.sort(forbidden); char c = 'a'; for (int i = 0; i < forbidden.size(); ++i) { if (c != forbidden.get(i).charValue()) break; else { ++c; } } return String.valueOf(c); }
/** * Randomly permutes the specified array. * * @param a - the array to be shuffled. */ public static void shuffle(int[] a) { shuffle(a, CC.getRandomGenerator()); }
tensorSubstitutions.put(scalar, CC.generateNewSymbol());
@Override public String toString() { return toString(CC.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(); }
/** * Creates structure of indices from specified data about metric indices. * * @param types array of types * @param count array of sizes of indices of specified types * @throws IllegalArgumentException if any type in type is non metric */ private StructureOfIndices(final byte[] types, int[] count) { for (int i = 0; i < types.length; ++i) if (count[i] != 0 && !CC.isMetric(types[i])) throw new IllegalArgumentException("No states information provided for non metric type."); int size = 0; for (int i = 0; i < types.length; ++i) { typesCounts[types[i]] = count[i]; size += count[i]; } this.size = size; for (int i = 0; i < IndexType.TYPES_COUNT; ++i) if (!CC.isMetric((byte) i)) states[i] = BitArray.EMPTY; }
public boolean isKroneckerOrMetric() { return name.equals(CC.getNameManager().getKroneckerName()) || name.equals(CC.getNameManager().getMetricName()); }
String toString0(OutputFormat mode) { //Initializing StringBuilder StringBuilder sb = new StringBuilder(); sb.append(CC.getNameDescriptor(name).getName(indices, mode)); //If there are no indices return builder content if (indices.size() == 0) return sb.toString(); //Writing indices boolean external = mode.is(OutputFormat.WolframMathematica) || mode.is(OutputFormat.Maple); if (external) sb.append("["); sb.append(indices.toString(mode)); if (external) sb.append("]"); return sb.toString(); }
/** * Randomly permute the specified list using the specified source of randomness. * * @param a - the array to be shuffled. */ public static void shuffle(Object[] a) { shuffle(a, CC.getRandomGenerator()); }