package org.matheclipse.core.reflection.system;

import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;

/**
 * See Wikipedia - Divergence
 * 
 * Example: Divergence[{f[u,v,w],f[v,w,u],f[w,u,v]}, {u,v,w}].
 */
public class Divergence extends AbstractFunctionEvaluator {
	public Divergence() {
	}

	@Override
	public IExpr evaluate(final IAST ast) {
		Validate.checkSize(ast, 3);
		if ((ast.arg1().isVector() == ast.arg2().isVector())
				&& (ast.arg1().isVector() >= 0)) {
			IAST vector = (IAST) ast.arg1();
			IAST variables = (IAST) ast.arg2();
			IAST divergenceValue = F.Plus();
			for (int i = 1; i < vector.size(); i++) {
				divergenceValue.add(F.D(vector.get(i), variables.get(i)));
			}
			return divergenceValue;
		}

		return null;
	}

}

 

Ads help maintain this website.