package org.matheclipse.core.builtin.function;

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

/**
 * Calculates the depth of an expression (i.e. {x,{y}} --> 3
 */
public class Depth extends AbstractCoreFunctionEvaluator {

	public Depth() {
	}

	@Override
	public IExpr evaluate(final IAST ast) {
		Validate.checkSize(ast, 2);

		final IExpr arg1 = F.eval(ast.arg1());
		if (!(arg1.isAST())) {
			return F.C1;
		}
		return F.integer(depth((IAST) ast.arg1(), 1));
	}

	/**
	 * Calculates the depth of an expression. Atomic expressions (no sublists) have depth 1 Example: the nested list
	 * [x,[y]] has depth 3
	 * 
	 * @param headOffset
	 * 
	 */
	public static int depth(final IAST list, int headOffset) {
		int maxDepth = 1;
		int d;
		for (int i = headOffset; i < list.size(); i++) {
			if (list.get(i).isAST()) {
				d = depth((IAST) list.get(i), headOffset);
				if (d > maxDepth) {
					maxDepth = d;
				}
			}
		}
		return ++maxDepth;
	}

}

 

Ads help maintain this website.