return target;
}
private Tensor getDerivative(Tensor target, SimpleTensor var) {
if (target instanceof Sum) {
Sum sum = (Sum) target;
TensorIterator it = sum.iterator();
Tensor current, derivative;
Sum res = new Sum();
while (it.hasNext()) {
current = it.next();
derivative = getDerivative(current, var);
if (derivative == null)
continue;// it.remove();
else
res.add(derivative);//if (derivative != current)
//it.set(derivative);
}
if (res.isEmpty())
return null;
return res.equivalent();
} else if (target instanceof Product) {
Product product = (Product) target;
Tensor derivative;
List<Tensor> resultProducts = new ArrayList<>();
for (int i = 0; i < product.size(); ++i) {
derivative = getDerivative(product.getElements().get(i), var);
if (derivative == null)
continue;
Product clone = (Product) product.clone();
clone.getElements().remove(i);
if (!isOne(derivative))
clone.add(derivative);
resultProducts.add(clone.equivalent());
}
if (resultProducts.isEmpty())
return null;
if (resultProducts.size() == 1)
return resultProducts.get(0);
return new Sum(resultProducts);
} else if (target.getClass() == SimpleTensor.class) {
SimpleTensor sp = (SimpleTensor) target;
if (sp.getName() != var.getName())
return null;
if (sp.getIndices().size() == 0)