package aalg.algebra;

/**
 * Parametric solution of linear equation system.
 * @author Ing. Petr Ležák
 *
 * @param <F> type of field elements
 */
public class EquationSystemSolution<F extends IElement> {

	// Solution of equations = constantVector + parameterCoeffs * parameters
	private final IVector<F> constantVector;
	private final Matrix<F> parameterCoeffs;
	
	/**
	 * Creates general (parametric) solution of equation system.
	 * Solution of equations = constantVector + parameterCoeffs * parameters
	 * @param constantVector constant vector
	 * @param parameterCoeffs parameter dependent coefficients
	 */
	public EquationSystemSolution (final IVector<F> constantVector, final Matrix<F> parameterCoeffs) {
		if (constantVector.getSize() != parameterCoeffs.getRowCount())
			throw new RuntimeException("Wrong sizes of inputs");
		
		this.constantVector = constantVector;
		this.parameterCoeffs = parameterCoeffs;
	}
	
	/**
	 * Returns number of parameters.
	 * @return number of parameters
	 */
	public int getParameterCount() {
		return parameterCoeffs.getColumnCount();
	}
	
	/**
	 * Returns solution for given parameters.
	 * @param parameters vector of parameters
	 * @return vector of solution
	 */
	public IVector<F> getSolution (final IVector<F> parameters) {
		return constantVector.add(parameterCoeffs.multiply(parameters));
	}
	
	/**
	 * Compares two solutions.
	 */
	public boolean equals (final Object obj) {
		if (!(obj instanceof EquationSystemSolution<?>))
			return false;
		
		final EquationSystemSolution<?> cmp = (EquationSystemSolution<?>) obj;
		
		return constantVector.equals(cmp.constantVector) && parameterCoeffs.equals(cmp.parameterCoeffs);
	}
	
	/**
	 * Converts solution to string.
	 */
	public String toString() {
		return constantVector + " " + parameterCoeffs;
	}
}
