/** * Returns true, if specified tensor is {@code a^(N)}, where {@code N} - a natural number and {@code a} - is a * product of tensors * * @param t tensor * @return true, if specified tensor is {@code a^(N)}, where {@code N} - a natural number and {@code a} - is a * product of tensors */ public static boolean isPositiveIntegerPowerOfProduct(Tensor t) { return isPositiveIntegerPower(t) && t.get(0) instanceof Product; }
/** * Return the resulting tensor. * * @return the resulting tensor */ public Tensor result() { if (currentPointer.previous != null) throw new RuntimeException("Iteration not finished."); return currentPointer.getTensor().get(0); }
/** * Returns true, if specified tensor is a^(-N), where N - a natural number * * @param t tensor * @return true, if specified tensor is a^(-N), where N - a natural number */ public static boolean isNegativeIntegerPower(Tensor t) { return t instanceof Power && TensorUtils.isNegativeNaturalNumber(t.get(1)); }
private static boolean matchSimpleTensors(Tensor a, Tensor b) { if (a.getClass() != b.getClass()) return false; if (a.hashCode() != b.hashCode()) return false; if (TensorUtils.isPositiveIntegerPowerOfSimpleTensor(a)) return TensorUtils.isPositiveIntegerPowerOfSimpleTensor(b) && a.get(1).equals(b.get(1)) && matchSimpleTensors(a.get(0), b.get(0)); if (a instanceof TensorField) for (int i = a.size() - 1; i >= 0; --i) if (!IndexMappings.positiveMappingExists(a.get(i), b.get(i))) return false; return true; }
static boolean powerExpandApplicable1(Tensor power, Indicator<Tensor> indicator) { for (Tensor t : power.get(0)) if (indicator.is(t)) return true; return indicator.is(power); }
public PowerPort(Tensor tensor, int[] initialForbidden, boolean expandSymbolic) { this.expandSymbolic = expandSymbolic; base = tensor.get(0); power = ((Complex) tensor.get(1)).getReal().intValue(); int[] upperBounds = new int[power]; Arrays.fill(upperBounds, base.size()); tuplesPort = new IntTuplesPort(upperBounds); this.initialForbidden = initialForbidden; currentPort = nextPort(); }
@Override public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to) { if (((TensorField) from).getName() != ((TensorField) to).getName()) return IndexMappingProvider.Util.EMPTY_PROVIDER; for (int i = 0; i < from.size(); ++i) { if (!IndexMappings.positiveMappingExists(from.get(i), to.get(i))) return IndexMappingProvider.Util.EMPTY_PROVIDER; } return new ProviderSimpleTensor(opu, (SimpleTensor) from, (SimpleTensor) to); } };
@Override public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to) { if (from.size() != to.size()) return IndexMappingProvider.Util.EMPTY_PROVIDER; for (int i = 0; i < from.size(); ++i) if (from.get(i).hashCode() != to.get(i).hashCode()) return IndexMappingProvider.Util.EMPTY_PROVIDER; return new ProviderSum(opu, from, to); } };
@Override public Tensor create1(Tensor arg) { if (arg instanceof ArcCot) return arg.get(0); if (TensorUtils.isZero(arg)) return Complex.COMPLEX_POSITIVE_INFINITY;//FUTURE complex refactoring if (TensorUtils.isNumeric(arg)) return ComplexUtils.cot((Complex) arg); return new Cot(arg); } }
@Override public Tensor create1(Tensor arg) { if (arg instanceof ArcTan) return arg.get(0); if (TensorUtils.isZero(arg)) return Complex.ZERO; if (TensorUtils.isNumeric(arg)) return ComplexUtils.tan((Complex) arg); return new Tan(arg); } }
@Override public Tensor create1(Tensor arg) { if (arg instanceof Sin) return arg.get(0); if (TensorUtils.isZero(arg)) return Complex.ZERO; if (TensorUtils.isNumeric(arg)) return ComplexUtils.arcsin((Complex) arg); return new ArcSin(arg); } }
@Override public Tensor create1(Tensor arg) { if (arg instanceof ArcCos) return arg.get(0); if (TensorUtils.isZero(arg)) return Complex.ONE; if (TensorUtils.isNumeric(arg)) return ComplexUtils.cos((Complex) arg); return new Cos(arg); } }
private static int match0(final Tensor el, final Tensor patt, final boolean sumPowers) { if (sumPowers && isPositiveIntegerPower(el)) return ((Complex) el.get(1)).intValue() * match0(el.get(0), patt, false); else if (IndexMappings.anyMappingExists(patt, el)) return 1; else if (patt instanceof TensorField && el instanceof TensorField && !((TensorField) patt).isDerivative()) return (((TensorField) el).getParentField().getName() == ((TensorField) patt).getName()) ? 1 : 0; return 0; }
@Override public Tensor create1(Tensor arg) { if (arg instanceof Exp)//TODO Log[Power[E,x]] = x return arg.get(0); if (TensorUtils.isOne(arg)) return Complex.ZERO; if (TensorUtils.isNumeric(arg)) return ComplexUtils.log((Complex) arg); return new Log(arg); } }
OutputPort<Tensor> nextPort() { final int[] tuple = tuplesPort.take(); if (tuple == null) return null; TIntHashSet added = new TIntHashSet(initialForbidden); ProductBuilder builder = new ProductBuilder(); builder.put(base.get(tuple[0])); for (int i = 1; i < tuple.length; ++i) builder.put(ApplyIndexMapping.renameDummy(base.get(tuple[i]), added.toArray(), added)); return createPort(builder.build(), expandSymbolic); }
private boolean match(Tensor t) { if (t instanceof SimpleTensor) return patternsNames.contains(t.hashCode()); else if (TensorUtils.isPositiveIntegerPower(t)) return patternsNames.contains(t.get(0).hashCode()); return false; }
@Override public Tensor create1(Tensor arg) { if (arg instanceof Cot) return arg.get(0); if (TensorUtils.isZero(arg)) return Tensors.parse("pi/2"); if (TensorUtils.isNumeric(arg)) return ComplexUtils.arccot((Complex) arg); return new ArcCot(arg); } }