All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rinde.ecj.GenericFunctions Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/**
 * 
 */
package rinde.ecj;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

/**
 * 
 * 
 * @author Rinde van Lon 
 * 
 */
public class GenericFunctions {

	public static  If4 newIf4() {
		return new If4();
	}

	public static  Add newAdd() {
		return new Add();
	}

	public static  Sub newSub() {
		return new Sub();
	}

	public static  Mul newMul() {
		return new Mul();
	}

	public static  Div newDiv() {
		return new Div();
	}

	public static  Pow newPow() {
		return new Pow();
	}

	public static  Constant newConstant(double v) {
		return new Constant(v);
	}

	@SuppressWarnings("unchecked")
	public static  List> newConstants(double... vs) {
		final List> list = newArrayList();
		for (final double d : vs) {
			list.add((Constant) newConstant(d));
		}
		return list;
	}

	public static class If4 extends GPFunc {
		private static final long serialVersionUID = -8010536154981009677L;

		public If4() {
			super(4);
		}

		@Override
		public double execute(double[] input, T context) {
			return input[0] < input[1] ? input[2] : input[3];
		}

	}

	public static class Add extends GPFunc {
		private static final long serialVersionUID = 2200299240321191164L;

		public Add() {
			super(2);
		}

		@Override
		public double execute(double[] input, T context) {
			return input[0] + input[1];
		}
	}

	public static class Sub extends GPFunc {
		private static final long serialVersionUID = -1363621468791103104L;

		public Sub() {
			super(2);
		}

		@Override
		public double execute(double[] input, T context) {
			return input[0] - input[1];
		}
	}

	public static class Mul extends GPFunc {
		private static final long serialVersionUID = 537369514239069421L;

		public Mul() {
			super(2);
		}

		@Override
		public double execute(double[] input, T context) {
			return input[0] * input[1];
		}
	}

	public static class Div extends GPFunc {
		private static final long serialVersionUID = 6727402143693804260L;

		public Div() {
			super(2);
		}

		// protected division
		@Override
		public double execute(double[] input, T context) {
			final double val0 = input[0];
			final double val1 = input[1];
			if (val1 == 0d) {
				return 1d;
			}
			return val0 / val1;
		}
	}

	public static class Pow extends GPFunc {
		private static final long serialVersionUID = -1207160233965775202L;

		public Pow() {
			super(2);
		}

		@Override
		public double execute(double[] input, T context) {
			final double res = Math.pow(input[0], input[1]);
			if (Double.isInfinite(res) || Double.isNaN(res)) {
				return 1d;
			}
			return res;
		}

	}

	public static class Constant extends GPFunc {

		private static final long serialVersionUID = -2428773869358609217L;
		private final double value;

		public Constant(double val) {
			super("" + val, 0);
			value = val;
		}

		@Override
		public double execute(double[] input, C context) {
			return value;
		}

		@Override
		public GPFunc create() {
			return new Constant(value);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy