/** * Returns index type enum value. * * @param index index * @return index type enum value */ public static IndexType getTypeEnum(int index) { return IndexType.getType(getType(index)); }
/** * Returns all non metric types that present in specified indices. * * @param indices indices * @return all non metric types that present in specified indices */ public static EnumSet<IndexType> nonMetricTypes(final Indices indices) { EnumSet<IndexType> types = EnumSet.noneOf(IndexType.class); for (int i = 0; i < indices.size(); ++i) { int index = indices.get(i); if (!CC.isMetric(getType(index))) types.add(getTypeEnum(index)); } return types; } }
private static void checkLeviCivita(SimpleTensor LeviCivita) { SimpleIndices indices = LeviCivita.getIndices(); if (indices.size() <= 1) throw new IllegalArgumentException("Levi-Civita cannot be a scalar."); byte type = getType(indices.get(0)); for (int i = 1; i < indices.size(); ++i) if (type != getType(indices.get(i))) throw new IllegalArgumentException("Levi-Civita have indices with different types."); } }
public IndexGeneratorFromData(int[] allowedIndices) { if (allowedIndices.length > 0) { Arrays.sort(allowedIndices); byte type = getType(allowedIndices[0]); allowedIndices[0] = getNameWithoutType(allowedIndices[0]); int prevIndex = 0; for (int i = 1; i < allowedIndices.length; ++i) { if (getType(allowedIndices[i]) != type) { generators.put(type, new IntProvider(Arrays.copyOfRange(allowedIndices, prevIndex, i))); prevIndex = i; type = getType(allowedIndices[i]); } allowedIndices[i] = getNameWithoutType(allowedIndices[i]); } generators.put(type, new IntProvider(Arrays.copyOfRange(allowedIndices, prevIndex, allowedIndices.length))); } }
private static void checkIndices(SimpleIndices upper, SimpleIndices lower) { if (upper.size() != lower.size()) throw new IllegalArgumentException("Upper indices size not equal to lower indices size."); int size = upper.size(); for (int i = 0; i < size; ++i) { if (!IndicesUtils.getState(upper.get(i)) || IndicesUtils.getState(lower.get(i))) throw new IllegalArgumentException(); if (IndicesUtils.getType(upper.get(i)) != IndicesUtils.getType(lower.get(i))) throw new IllegalArgumentException(); if (i != 0) if (IndicesUtils.getType(upper.get(i - 1)) != IndicesUtils.getType(upper.get(i))) throw new IllegalArgumentException("Many types."); } }
void update(int pointer, TByteObjectHashMap<MaxType> maxTypeValues) { int previousPointer = 0, current; byte type; while (previousPointer < dummies.length) { type = IndicesUtils.getType(dummies[previousPointer]); current = binarySearchAbs(Arrays.binarySearch(dummies, previousPointer, dummies.length, (type + 1) << 24)); final int count = current - previousPointer; final MaxType typeInfo = maxTypeValues.get(type); if (typeInfo == null) maxTypeValues.put(type, new MaxType(count, pointer)); else if (typeInfo.count < count) { typeInfo.count = count; typeInfo.pointer = pointer; } previousPointer = current; } }
/** * Returns {@code true} if specified indices contain any index with non metric type. * * @param indices indices * @return {@code true} if specified indices contain any index with non metric type */ public static boolean containsNonMetric(final Indices indices) { for (int i = 0; i < indices.size(); ++i) { if (!CC.isMetric(getType(indices.get(i)))) return true; } return false; }
/** * Returns true if index contains in engaged data or already was generated. * * @param index index * @return true if index contains in engaged data or already was generated */ public boolean contains(int index) { byte type = getType(index); IntGenerator intGen; if ((intGen = generators.get(type)) == null) return false; return intGen.contains(getNameWithoutType(index)); }
@Override public void apply(IndexMapper indexMapper, IGWrapper generator, int[] upper, int[] lower) { int i, j; int[] tempUpper = upper.clone(), tempLower = new int[upper.length]; for (i = 0; i < transformers.length - 1; ++i) { for (j = 0; j < upper.length; ++j) tempLower[j] = generator.next(IndicesUtils.getType(lower[j])); transformers[i].apply(indexMapper, generator, tempUpper, tempLower); System.arraycopy(tempLower, 0, tempUpper, 0, tempUpper.length); } transformers[i].apply(indexMapper, generator, tempUpper, lower); } }
protected static SimpleTensor setMatrixIndices(SimpleTensor gamma, int matrixUpper, int matrixLower) { int[] indices = gamma.getIndices().getAllIndices().copy(); for (int i = indices.length - 1; i >= 0; --i) if (!CC.isMetric(getType(indices[i]))) { indices[i] = getState(indices[i]) ? createIndex(matrixUpper, getType(indices[i]), getState(indices[i])) : createIndex(matrixLower, getType(indices[i]), getState(indices[i])); } return simpleTensor(gamma.getName(), IndicesFactory.createSimple(null, indices)); }
/** * 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 SimpleTensor createMetric(int index1, int index2) { byte type; if ((type = IndicesUtils.getType(index1)) != IndicesUtils.getType(index2) || !IndicesUtils.haveEqualStates(index1, index2) || !metricTypesBits.get(type)) throw new IllegalArgumentException("Not metric indices."); SimpleIndices indices = IndicesFactory.createSimple(null, index1, index2); NameDescriptor nd = nameManager.mapNameDescriptor(nameManager.getMetricName(), StructureOfIndices.create(indices)); int name = nd.getId(); return Tensors.simpleTensor(name, indices); }
/** * 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 SimpleTensor createKronecker(int index1, int index2) { byte type; if ((type = IndicesUtils.getType(index1)) != IndicesUtils.getType(index2) || IndicesUtils.getRawStateInt(index1) == IndicesUtils.getRawStateInt(index2)) throw new IllegalArgumentException("This is not kronecker indices!"); if (!isMetric(type) && IndicesUtils.getState(index2)) { int t = index1; index1 = index2; index2 = t; } SimpleIndices indices = IndicesFactory.createSimple(null, index1, index2); NameDescriptor nd = nameManager.mapNameDescriptor(nameManager.getKroneckerName(), StructureOfIndices.create(indices)); int name = nd.getId(); return Tensors.simpleTensor(name, indices); }
private int withMomentum(int gamma, ProductContent pc, StructureOfContractions sc) { Indices indices = pc.get(gamma).getIndices(); int j = 0; for (; j < indices.size(); ++j) if (metricType.getType() == getType(indices.get(j))) break; return getToTensorIndex(sc.contractions[gamma][j]); }
PrimitiveSubstitution(Tensor from, Tensor to) { this.from = ApplyIndexMapping.optimizeDummies(from); this.to = ApplyIndexMapping.optimizeDummies(to); int[] typesCounts = new int[IndexType.TYPES_COUNT]; TIntIterator iterator = TensorUtils.getAllDummyIndicesIncludingScalarFunctionsT(to).iterator(); while (iterator.hasNext()) ++typesCounts[IndicesUtils.getType(iterator.next())]; iterator = TensorUtils.getAllDummyIndicesT(from).iterator(); while (iterator.hasNext()) --typesCounts[IndicesUtils.getType(iterator.next())]; boolean possiblyAddsDummies = false; for (int i : typesCounts) if (i > 0) { possiblyAddsDummies = true; break; } this.possiblyAddsDummies = possiblyAddsDummies; this.toIsSymbolic = TensorUtils.isSymbolic(to); }
private Element getContraction(int gIndex, int gamma, ProductContent pc, StructureOfContractions sc) { Indices indices = pc.get(gamma).getIndices(); int j = 0; for (; j < indices.size(); ++j) if (metricType.getType() == getType(indices.get(j))) break; int to = getToTensorIndex(sc.contractions[gamma][j]); if (to == -1) return null; return new Element(to, gIndex); }
protected static SimpleTensor setMetricIndex(SimpleTensor gamma, int metricIndex) { int[] indices = gamma.getIndices().getAllIndices().copy(); for (int i = indices.length - 1; i >= 0; --i) if (CC.isMetric(getType(indices[i]))) indices[i] = metricIndex; return simpleTensor(gamma.getName(), IndicesFactory.createSimple(null, indices)); }
private Tensor getContraction(int gamma, ProductContent pc, StructureOfContractions sc) { Indices indices = pc.get(gamma).getIndices(); int j = 0; for (; j < indices.size(); ++j) if (metricType.getType() == getType(indices.get(j))) break; int to = getToTensorIndex(sc.contractions[gamma][j]); if (to == -1) return null; return pc.get(to); }
@Override public boolean tryMap(int from, int to) { int fromState = IndicesUtils.getStateInt(from); if (fromState != IndicesUtils.getStateInt(to) && !CC.isMetric(IndicesUtils.getType(from))) return false; int fromName = IndicesUtils.getNameWithType(from); IndexMappingBufferRecord record = map.get(fromName); if (record == null) { record = new IndexMappingBufferRecord(from, to); map.put(fromName, record); return true; } return record.tryMap(from, to); }
private SimpleTensor setMatrixIndices0(SimpleTensor tensor, int... indices) { int[] newIndices = new int[tensor.getIndices().size()]; int j = 0; for (int i = 0; i < tensor.getIndices().size(); ++i) if (getType(tensor.getIndices().get(i)) == matrixType.getType()) newIndices[i] = indices[j++]; else newIndices[i] = tensor.getIndices().get(i); return setIndices(tensor, createSimple(null, newIndices)); }
public static Tensor applyIndexMappingAndRenameAllDummies(Tensor tensor, Mapping mapping, int[] allowedDummies) { if (TensorUtils.isZero(tensor)) return tensor; int[] freeIndicesNames = IndicesUtils.getIndicesNames(tensor.getIndices().getFree()); Arrays.sort(freeIndicesNames); if (!mapping.getFromNames().equalsToArray(freeIndicesNames)) throw new IllegalArgumentException("From indices names does not match free indices names of tensor. Tensor: " + tensor + " mapping: " + mapping); final int[] dummies = TensorUtils.getAllDummyIndicesT(tensor).toArray(); int[] from = new int[mapping.size() + dummies.length]; int[] to = new int[mapping.size() + dummies.length]; ArraysUtils.arraycopy(mapping.getFromNames(), 0, from, 0, mapping.size()); ArraysUtils.arraycopy(mapping.getToData(), 0, to, 0, mapping.size()); System.arraycopy(dummies, 0, from, mapping.size(), dummies.length); IndexGeneratorFromData generator = new IndexGeneratorFromData(allowedDummies); for (int i = mapping.size() + dummies.length - 1, mappingSize = mapping.size(); i >= mappingSize; --i) to[i] = generator.generate(IndicesUtils.getType(from[i])); ArraysUtils.quickSort(from, to); tensor = applyIndexMapping(tensor, new IndexMapper(from, to)); if (mapping.getSign()) tensor = Tensors.negate(tensor); return tensor; }