@Override public void put(Tensor tensor) { if (pointer == data.length) throw new IllegalStateException("No more arguments in field."); if (tensor == null) throw new NullPointerException(); if (!tensor.getIndices().getFree().equalsRegardlessOrder(field.getArgIndices(pointer))) if (TensorUtils.isZeroOrIndeterminate(tensor)) this.changedSignature = true; else throw new IllegalArgumentException( "Free indices of putted tensor " + tensor.getIndices().getFree() + " differs from field argument binding indices " + field.getArgIndices(pointer) + "!"); data[pointer++] = tensor; }
if (!descriptor.getStructuresOfIndices()[i + 1].isStructureOf(argIndices[i])) throw new IllegalArgumentException("Arguments indices are inconsistent with field signature."); if (!arguments[i].getIndices().getFree().equalsRegardlessOrder(argIndices[i])) throw new IllegalArgumentException("Arguments indices are inconsistent with arguments.");
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()); }
/** * Returns new tensor field with specified string name, indices, arguments * list and explicit argument indices bindings. * * @param name int name of the field * @param indices indices * @param argIndices argument indices bindings * @param arguments arguments list * @return new instance of {@link TensorField} object */ public static TensorField field(String name, SimpleIndices indices, SimpleIndices[] argIndices, Tensor[] arguments) { if (argIndices.length != arguments.length) throw new IllegalArgumentException("Argument indices array and arguments array have different length."); if (arguments.length == 0) throw new IllegalArgumentException("No arguments in field."); for (int i = 0; i < argIndices.length; ++i) if (!arguments[i].getIndices().getFree().equalsRegardlessOrder(argIndices[i])) throw new IllegalArgumentException("Arguments indices are inconsistent with arguments."); StructureOfIndices[] structures = new StructureOfIndices[argIndices.length + 1]; structures[0] = indices.getStructureOfIndices(); for (int i = 0; i < argIndices.length; ++i) structures[i + 1] = argIndices[i].getStructureOfIndices(); NameDescriptor descriptor = CC.getNameManager().mapNameDescriptor(name, structures); return new TensorField(descriptor.getId(), UnsafeIndicesFactory.createOfTensor(descriptor.getSymmetries(), indices), arguments, argIndices); }
@Override public Expression create(Tensor... tensors) { if (tensors.length != 2) throw new IllegalArgumentException("Wrong number of arguments."); if (tensors[0] == null || tensors[1] == null) throw new NullPointerException(); if (!TensorUtils.isZero(tensors[1]) && !TensorUtils.isIndeterminate(tensors[1]) && !tensors[0].getIndices().getFree().equalsRegardlessOrder(tensors[1].getIndices().getFree())) throw new TensorException(String.format("Inconsistent indices in expression: %s != %s", tensors[0].getIndices().getFree(), tensors[1].getIndices().getFree())); return new Expression(IndicesFactory.create(tensors[0].getIndices().getFree()), tensors[0], tensors[1]); } }
@Override public void put(Tensor tensor) { if (tensor == null) throw new NullPointerException(); else if (left == null) { left = tensor; indices = IndicesFactory.create(left.getIndices().getFree()); } else if (right == null) { right = tensor; if (!TensorUtils.isZero(right) && !TensorUtils.isIndeterminate(right) && !indices.equalsRegardlessOrder(right.getIndices().getFree())) throw new TensorException("Inconsistent indices in expression.", tensor); } else throw new TensorException("Expression have only two parts."); }
@Override public Tensor create(Tensor... tensors) { if (tensors.length != field.size()) throw new IllegalArgumentException("Wrong arguments count."); boolean changedSignature = false; for (int i = tensors.length - 1; i >= 0; --i) { if (tensors[i] == null) throw new NullPointerException(); if (!tensors[i].getIndices().getFree().equalsRegardlessOrder(field.getArgIndices(i))) { if (TensorUtils.isZeroOrIndeterminate(tensors[i])) changedSignature = true; else throw new IllegalArgumentException( "Free indices of putted tensor " + tensors[i].getIndices().getFree() + " differs from field argument binding indices " + field.getArgIndices(i) + "!"); } } if (changedSignature) return Tensors.field(field.getStringName(), field.getIndices(), tensors); else return new TensorField(field, tensors); } }
throw new IllegalArgumentException("No arguments in field."); for (int i = 0; i < argIndices.length; ++i) if (!arguments[i].getIndices().getFree().equalsRegardlessOrder(argIndices[i])) throw new IllegalArgumentException("Arguments indices are inconsistent with arguments.");
/** * 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(); }
/** * Returns {@code true} if tensor u mathematically (not programming) equals to tensor v. * * @param u tensor * @param v tensor * @return {@code true} if specified tensors are mathematically (not programming) equal */ public static boolean equals(Tensor u, Tensor v) { if (u == v) return true; Indices freeIndices = u.getIndices().getFree(); if (!freeIndices.equalsRegardlessOrder(v.getIndices().getFree())) return false; int[] free = freeIndices.getAllIndices().copy(); IndexMappingBuffer tester = new IndexMappingBufferTester(free, false); OutputPort<IndexMappingBuffer> mp = IndexMappings.createPortOfBuffers(tester, u, v); IndexMappingBuffer buffer; while ((buffer = mp.take()) != null) if (!buffer.getSign()) return true; return false; }
public void set(Tensor tensor, boolean supposeIndicesAreAdded) { Tensor oldTensor = innerIterator.current(); if (oldTensor == tensor) return; if (TensorUtils.isZeroOrIndeterminate(tensor)) { innerIterator.set(tensor); return; } if (!tensor.getIndices().getFree().equalsRegardlessOrder(oldTensor.getIndices().getFree())) throw new RuntimeException(String.format("Substituting tensor %s with different free indices (%s != %s).", tensor, oldTensor.getIndices().getFree(), tensor.getIndices().getFree() )); if (supposeIndicesAreAdded) { StackPosition<ForbiddenContainer> previous = innerIterator.currentStackPosition().previous(); if (previous != null) { TIntHashSet oldDummyIndices = TensorUtils.getAllDummyIndicesT(oldTensor); TIntHashSet newDummyIndices = TensorUtils.getAllDummyIndicesT(tensor); TIntHashSet added = new TIntHashSet(newDummyIndices); added.removeAll(oldDummyIndices); if (!added.isEmpty() || previous.isPayloadInitialized()) { ForbiddenContainer fc = previous.getPayload(); TIntHashSet removed = new TIntHashSet(oldDummyIndices); removed.removeAll(newDummyIndices); fc.submit(removed, added); } } } innerIterator.set(tensor); }
sortedFreeIndices = indices.getAllIndices().copy(); Arrays.sort(sortedFreeIndices); } else if (!indices.equalsRegardlessOrder(tensor.getIndices().getFree())) throw new TensorException("Inconsistent indices in sum. " + "Expected: " + indices + " Actual: " + tensor.getIndices().getFree(), tensor);//TODO improve message