private static void checkConsistent(Tensor tensor, final int[] from) { int[] freeIndices = tensor.getIndices().getFree().getAllIndices().copy(); Arrays.sort(freeIndices); if (!Arrays.equals(freeIndices, from)) throw new IllegalArgumentException("From indices are not equal to free indices of tensor."); }
/** * Inverts indices of tensor * * @param tensor tensor * @return tensor with inverted indices */ public static Tensor invertIndices(Tensor tensor) { Indices indices = tensor.getIndices().getFree(); if (indices.size() == 0) return tensor; return applyIndexMapping(tensor, new Mapping(indices.toArray(), indices.getInverted().toArray())); } }
/** * Returns an array of contracted indices between specified indices. * * @param u indices * @param v indices * @return an array of contracted indices */ public static int[] getIntersections(Indices u, Indices v) { if (u.size() == 0 || v.size() == 0) return new int[0]; Indices freeU = u.getFree(), freeV = v.getFree(); if (freeU.size() == 0 || freeV.size() == 0) return new int[0]; return getIntersections(((AbstractIndices) freeU).data, ((AbstractIndices) freeV).data); }
/** * Returns an array of indices names (with types), presented in specified {@code Indices} * object with the same ordering. * * @param indices * @return array of indices names (with types) */ public static int[] getIndicesNames(Indices indices) { int a[] = new int[indices.size()]; for (int i = indices.size() - 1; i >= 0; --i) a[i] = getNameWithType(indices.get(i)); return a; }
int[] lowCounts = new int[samples.length + 1]; for (i = 0; i < samples.length; ++i) lowCounts[i] = samples[i].getIndices().getFree().getLower().size(); lowCounts[i] = totalLowCount; int[] upCounts = new int[samples.length + 1]; for (i = 0; i < samples.length; ++i) upCounts[i] = samples[i].getIndices().getFree().getUpper().size(); upCounts[i] = totalUpCount; Tensor temp = samples[i]; Indices termLow = temp.getIndices().getFree().getLower(); Indices termUp = temp.getIndices().getFree().getUpper(); int[] oldIndices = new int[termUp.size() + termLow.size()], newIndices = oldIndices.clone(); for (k = 0; k < termUp.size(); ++k) { oldIndices[k] = termUp.get(k); newIndices[k] = upperArray[u++]; for (k = 0; k < termLow.size(); ++k) { oldIndices[k + termUp.size()] = termLow.get(k); newIndices[k + termUp.size()] = lowerArray[l++];
public static Tensor symmetrizeUpperLowerIndices(Tensor tensor, boolean multiplyOnSymmetryFactor) { Indices indices = IndicesFactory.create(tensor.getIndices().getFree()); int[] indicesArray = indices.getAllIndices().copy(); List<Permutation> symmetries = TensorUtils.getIndicesSymmetriesForIndicesWithSameStates(indicesArray, tensor); int lowerCount = indices.getLower().size(), upperCount = indices.getUpper().size();
/** * Returns true if at least one free index of {@code u} is contracted * with some free index of {@code v}. * * @param u indices * @param v indices * @return true if at least one free index of {@code u} is contracted * with some free index of {@code v} */ public static boolean haveIntersections(Indices u, Indices v) { //todo can be improved Indices uFree = u.getFree(), vFree = v.getFree(); //micro optimization if (uFree.size() > vFree.size()) { Indices temp = uFree; uFree = vFree; vFree = temp; } for (int i = 0; i < uFree.size(); ++i) for (int j = 0; j < vFree.size(); ++j) if (vFree.get(j) == inverseIndexState(uFree.get(i))) return true; return false; }
leftSubIndices = leftIndices.getOfType(type); leftUpperCount = leftSubIndices.getUpper().size(); leftLowerCount = leftSubIndices.getLower().size(); rightSubIndices = rightIndices.getOfType(type); rightUpperCount = rightSubIndices.getUpper().size(); rightLowerCount = rightSubIndices.getLower().size();
public static Tensor[] allStatesCombinations(Tensor st) { Indices indices = st.getIndices().getFree(); int[] indicesArray = indices.getAllIndices().copy(); for (int i = 0; i < indices.size(); ++i) { if (CC.isMetric(getType(indices.get(i)))) metricIndices.add(getNameWithType(indices.get(i))); else nonMetricIndices.add(indices.get(i));
/** * Returns {@code true} if tensor u mathematically (not programming) equals to tensor v, * {@code false} if they they differ only in the sign and {@code null} otherwise. * * @param u tensor * @param v tensor * @return {@code true} {@code false} if tensor u mathematically (not programming) equals to tensor v, * {@code true} if they they differ only in the sign and {@code null} otherwise */ public static Boolean compare1(Tensor u, Tensor v) { Indices freeIndices = u.getIndices().getFree(); if (!freeIndices.equalsRegardlessOrder(v.getIndices().getFree())) return null; int[] free = freeIndices.getAllIndices().copy(); IndexMappingBuffer tester = new IndexMappingBufferTester(free, false); IndexMappingBuffer buffer = IndexMappings.createPortOfBuffers(tester, u, v).take(); if (buffer == null) return null; return buffer.getSign(); }
private static Tensor invertIndices(Tensor t) { Indices free = t.getIndices().getFree(); Mapping mapping = new Mapping(free.toArray(), free.getInverted().toArray()); return ApplyIndexMapping.applyIndexMapping(t, mapping); }
/** * Returns {@code indices.size() == 0}. * * @param indices indices to testing similarity * @return {@code indices.size() == 0} */ @Override public boolean equalsRegardlessOrder(Indices indices) { return indices.size() == 0; }
private void transformInsideFieldsAndScalarFunctions(ParseToken pn) { if (pn.tokenType == TokenType.TensorField) { ParseTokenTensorField pntf = (ParseTokenTensorField) pn; for (int i = 0; i < pn.content.length; ++i) { ParseToken newArgNode = transform(pntf.content[i]); pntf.content[i] = newArgNode; newArgNode.parent = pntf; SimpleIndices oldArgIndices = pntf.argumentsIndices[i]; if (oldArgIndices != null) { IntArrayList newArgIndices = new IntArrayList(oldArgIndices.getAllIndices().copy()); Indices newIndices = newArgNode.getIndices(); for (byte j = 0; j < TYPES_COUNT; ++j) { if (oldArgIndices.size(IndexType.getType(j)) < newIndices.size(IndexType.getType(j))) { if (oldArgIndices.size(IndexType.getType(j)) != 0) throw new IllegalArgumentException("Error in field arg indices."); newArgIndices.addAll(newIndices.getOfType(IndexType.getType(j)).getAllIndices()); } } pntf.argumentsIndices[i] = IndicesFactory.createSimple(null, newArgIndices.toArray()); } } } if (pn.tokenType == TokenType.Power || pn.tokenType == TokenType.ScalarFunction) { for (int i = 0; i < pn.content.length; ++i) pn.content[i] = transform(pn.content[i]); } for (int i = 0; i < pn.content.length; ++i) transformInsideFieldsAndScalarFunctions(pn.content[i]); }
@Override public Indices getIndices() { return content[0].getIndices().getFree(); }
private static void checkConsistence(Tensor from, Tensor to) { if (!TensorUtils.isZeroOrIndeterminate(to)) if (!from.getIndices().getFree().equalsRegardlessOrder(to.getIndices().getFree())) throw new IllegalArgumentException("Tensor from free indices not equal to tensor to free indices: " + from.getIndices().getFree() + " " + to.getIndices().getFree()); }
/** * Creates with generator with specified engaged data. * * @param indices forbidden indices */ public IndexGeneratorImpl(Indices indices) { this(indices.getAllIndices().copy()); }
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(); }