package mikera.vectorz;

import mikera.arrayz.INDArray;
import mikera.matrixx.AMatrix;
import mikera.vectorz.impl.ADenseArrayVector;
import mikera.vectorz.util.ErrorMessages;

/**
 * Abstract class for representing a unary operation
 * 
 * @author Mike
 */
public abstract class Op2 {
	
	public abstract double apply(double x, double y);
	
	public void applyTo(AVector a, AVector b) {
		if (a instanceof ADenseArrayVector) {
			applyTo((ADenseArrayVector)a,b);
		} else {
			a.applyOp(this,b);
		}
	}
	
	public void applyTo(AMatrix a, AMatrix b) {
		a.applyOp(this, b);
	}
		
	public void applyTo(AScalar a, AScalar b) {
		a.set(apply(a.get(),b.get()));
	}
	
	public void applyTo(ADenseArrayVector a, AVector b) {
		applyTo(a.getArray(), a.getArrayOffset(),a.length(),b);
	}
	
	public void applyTo(INDArray a, INDArray b) {
		if (a instanceof AVector) {
			applyTo((AVector)a,b.broadcastLike(a));
		} else if (a instanceof AMatrix) {
			applyTo((AMatrix)a,b.broadcastLike(a));
		} else if (a instanceof AScalar) {
			applyTo((AScalar)a,b.broadcastLike(a));
		} else {
			a.applyOp(this,b);
		}
	}

	public void applyTo(double[] data, int start, int length, AVector b) {
		b.checkLength(length);
		for (int i=0; imax)) return false;
		}
		return true;
	}
	
	/**
	 * Creates a copy of the values of src in dest, constraining them to be within the valid
	 * range of output values from this Op
	 * @param src
	 * @param dest
	 * @param offset
	 * @param length
	 */
	public void constrainValues(double[] src, double[] dest, int offset, int length) {
		if (!isBounded()) {
			System.arraycopy(src, 0, dest, offset, length);
		}
		double min=minValue();
		double max=maxValue();
		
		for (int i=offset; i<(offset+length); i++) {
			double v=src[i];
			if (v>max) {
				dest[i]=max;
			} else if (v=-Double.MAX_VALUE)||(maxValue()<=Double.MAX_VALUE);
	}
	
	@Override public String toString() {
		return getClass().toString();
	}
	
	/**
	 * Method to reduce over a subset of a double[] array
	 */
	public double reduce(double init, double[] data, int offset, int length) {
		double result=init;
		for (int i=0; i 

Ads help maintain this website.