<commit_before>package org.openforis.idm.model.expression.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.jxpath.ExpressionContext;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class MathFunctions extends CustomFunctions {

	public MathFunctions(String namespace) {
		super(namespace);
		register("PI", new CustomFunction(0) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return PI();
			}
		});
		register("abs", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return abs(number);
			}
		});
		register("pow", new CustomFunction(2) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return pow(objects[0], objects[1]);
			}
		});
		register("min", new CustomFunction(1) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return min(objects[0]);
			}
		});
		register("max", new CustomFunction(1) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return max(objects[0]);
			}
		});
		register("rad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return rad(number);
			}
		});
		register("deg", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return deg(number);
			}
		});
		register("sin", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return sin(number);
			}
		});
		register("sinrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return sinrad(number);
			}
		});
		register("cos", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return cos(number);
			}
		});
		register("cosrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return cosrad(number);
			}
		});
		register("tan", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tan(number);
			}
		});
		register("tanrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tanrad(number);
			}
		});
		register("tanrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tanrad(number);
			}
		});
		
		register("log", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return log(number);
			}
		});
		
		register("log10", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return log10(number);
			}
		});
	}

	private static double PI() {
		return Math.PI;
	}

	private static Double abs(Number value) {
		return Math.abs(value.doubleValue());
	}

	@SuppressWarnings("unchecked")
	private static Object pow(Object base, Object exponent) {
		if (base == null || exponent == null) {
			return null;
		}
		if (!(exponent instanceof Number)) {
			throw new IllegalArgumentException("Exponent not a number");
		}
		Number exponentValue = (Number) exponent;
		return base instanceof Iterable
				? powForCollectionOfBases((Iterable<Object>) base, exponentValue)
				: powForSingleBase(base, exponentValue);
	}

	private static Iterable<Number> powForCollectionOfBases(Iterable<Object> base, Number exponent) {
		List<Number> result = new ArrayList<Number>();
		for (Object number : base) {
			result.add(
					powForSingleBase(number, exponent)
			);
		}
		return result;
	}

	private static Double powForSingleBase(Object base, Number exponent) {
		if (!(base instanceof Number)) {
			throw new IllegalArgumentException("base is not a number");
		}
		Number baseNumber = (Number) base;
		return Math.pow(baseNumber.doubleValue(), exponent.doubleValue());
	}

	private static Object min(Object values) {
		return compare(values, true);
	}

	private static Object max(Object values) {
		return compare(values, false);<commit_msg>Add square root XPath math function<commit_after>package org.openforis.idm.model.expression.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.jxpath.ExpressionContext;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class MathFunctions extends CustomFunctions {

	public MathFunctions(String namespace) {
		super(namespace);
		register("PI", new CustomFunction(0) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return PI();
			}
		});
		register("abs", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return abs(number);
			}
		});
		register("pow", new CustomFunction(2) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return pow(objects[0], objects[1]);
			}
		});
		register("sqrt", new CustomFunction(1) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return sqrt(objects[1]);
			}
		});
		register("min", new CustomFunction(1) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return min(objects[0]);
			}
		});
		register("max", new CustomFunction(1) {
			public Object invoke(ExpressionContext expressionContext, Object[] objects) {
				return max(objects[0]);
			}
		});
		register("rad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return rad(number);
			}
		});
		register("deg", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return deg(number);
			}
		});
		register("sin", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return sin(number);
			}
		});
		register("sinrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return sinrad(number);
			}
		});
		register("cos", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return cos(number);
			}
		});
		register("cosrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return cosrad(number);
			}
		});
		register("tan", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tan(number);
			}
		});
		register("tanrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tanrad(number);
			}
		});
		register("tanrad", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return tanrad(number);
			}
		});
		register("log", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return log(number);
			}
		});
		register("log10", new SingleArgMathFunction() {
			public Number execute(Number number) {
				return log10(number);
			}
		});
	}

	private static double PI() {
		return Math.PI;
	}

	private static Double abs(Number value) {
		return Math.abs(value.doubleValue());
	}

	@SuppressWarnings("unchecked")
	private static Object pow(Object base, Object exponent) {
		if (base == null || exponent == null) {
			return null;
		}
		if (!(exponent instanceof Number)) {
			throw new IllegalArgumentException("Exponent not a number");
		}
		Number exponentValue = (Number) exponent;
		return base instanceof Iterable
				? powForCollectionOfBases((Iterable<Object>) base, exponentValue)
				: powForSingleBase(base, exponentValue);
	}

	private static Iterable<Number> powForCollectionOfBases(Iterable<Object> base, Number exponent) {
		List<Number> result = new ArrayList<Number>();
		for (Object number : base) {
			result.add(
					powForSingleBase(number, exponent)
			);
		}
		return result;
	}

	private static Double powForSingleBase(Object base, Number exponent) {
		if (!(base instanceof Number)) {
			throw new IllegalArgumentException("base is not a number");
		}
		Number baseNumber = (Number) base;
		return Math.pow(baseNumber.doubleValue(), exponent.doubleValue());
	}
	
	private static Object sqrt(Object value) {
		return pow(value, 0.5);
	}

	private static Object min(Object values) {
		return compare(values, true);
	}

	private static Object max(Object values) {
		return compare(values, false);
{'fraction_non_alphanumeric': 0.12421519143173704, 'fraction_numerical': 0.00369321679182568, 'mean_word_length': 4.261658031088083, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093907', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2315, 'n_words': 607}