@Override protected Tensor select1(int[] positions) { Tensor[] newData = new Tensor[positions.length]; int i = -1; for (int position : positions) newData[++i] = data[position]; return new Sum(newData, indices); }
@Override protected Tensor remove1(final int[] positions) { Tensor[] newData = new Tensor[data.length - positions.length]; int pointer = 0, counter = -1; for (int i = 0; i < data.length; ++i) if (pointer < positions.length && i == positions[pointer]) ++pointer; else newData[++counter] = data[i]; if (newData.length == 1) return newData[0]; return new Sum(newData, indices); }
@Override public Tensor remove(int i) { if (i >= data.length || i < 0) throw new IndexOutOfBoundsException(); if (data.length == 2) return data[1 - i]; Tensor[] newData = new Tensor[data.length - 1]; System.arraycopy(data, 0, newData, 0, i); if (i < data.length - 1) System.arraycopy(data, i + 1, newData, i, data.length - i - 1); return new Sum(newData, indices); }
private static Tensor multiplyWithFactory(Sum sum, Tensor factor, Transformation... transformations) { final ArrayList<Tensor> newSumData = new ArrayList<>(sum.size()); Tensor temp; boolean reduced = false; for (int i = sum.size() - 1; i >= 0; --i) { temp = Transformation.Util.applySequentially(multiply(factor, sum.get(i)), transformations); if (!TensorUtils.isZero(temp)) { newSumData.add(temp); if (!reduced && isReduced(sum.get(i), factor, temp)) reduced = true; } } if (newSumData.size() == 0) return Complex.ZERO; if (newSumData.size() == 1) return newSumData.get(0); final Tensor[] data = newSumData.toArray(new Tensor[newSumData.size()]); if (reduced) return SumFactory.FACTORY.create(data); return new Sum(data, IndicesFactory.create(newSumData.get(0).getIndices().getFree())); }
for (int i = sumData.length - 1; i >= 0; --i) sumData[i] = Tensors.negate(sumData[i]); return new Sum(s.indices, sumData, s.hashCode());
for (int i = sumData.length - 1; i >= 0; --i) sumData[i] = Tensors.negate(sumData[i]); return new Sum(s.indices, sumData, s.hashCode());
/** * @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())); }
@Override public Tensor build() { if (complex.isNaN() || complex.isInfinite()) return complex; List<Tensor> sum = new ArrayList<>(); final boolean isNumeric = complex.isNumeric(); for (List<FactorNode> nodes : summands.valueCollection()) for (FactorNode node : nodes) { if (isNumeric) { Tensor summand = Tensors.multiply(toNumeric(node.build()), toNumeric(node.factor)); if (summand instanceof Complex) complex = complex.add((Complex) summand); else sum.add(summand); } else { Tensor summand = Tensors.multiply(node.build(), node.factor); if (!TensorUtils.isZero(summand)) sum.add(summand); } } if (sum.isEmpty()) return complex; if (!complex.isZero()) sum.add(complex); if (sum.size() == 1) return sum.get(0); return new Sum(sum.toArray(new Tensor[sum.size()]), indices); }
@Override public Tensor set(int i, Tensor tensor) { // return super.set(i, tensor); if (i >= data.length || i < 0) throw new IndexOutOfBoundsException(); Tensor old = data[i]; if (old == tensor) return this; if (TensorUtils.equalsExactly(old, tensor)) return this; if (TensorUtils.isIndeterminate(tensor)) return tensor; if (TensorUtils.isZero(tensor)) return remove(i); Tensor[] newData = data.clone(); newData[i] = tensor; if (TensorUtils.equals(old, tensor)) return new Sum(newData, indices); return Tensors.sum(newData); }
for (i = sumData.length - 1; i >= 0; --i) sumData[i] = Tensors.negate(sumData[i]); return new Sum(s.indices, sumData, s.hashCode());
for (int i = sumData.length - 1; i >= 0; --i) sumData[i] = Tensors.negate(sumData[i]); summand = new Sum(s.indices, sumData, s.hashCode()); } else
for (int i = sumData.length - 1; i >= 0; --i) sumData[i] = Tensors.negate(sumData[i]); summand = new Sum(s.indices, sumData, s.hashCode()); } else summand = new Product(product.factor, product.indexlessData, new Tensor[0], ProductContent.EMPTY_INSTANCE, IndicesFactory.EMPTY_INDICES);
return new Sum(newData, IndicesFactory.create(newData[0].getIndices().getFree()));
return new Sum(s.indices, data, s.hash);
return new Sum(newData, IndicesFactory.create(newData[0].getIndices().getFree()));
return t; return t instanceof Sum ? new Sum(newData, t.getIndices()) : new Expression(t.getIndices(), newData[0], newData[1]);