package engineering;

import math.equation.Polynomial;
import math.matrix.Matrix2D;
import exception.IllegalDimensionException;
import exception.NoSquareException;
import exception.SingularException;

public class Beam {
	
	Polynomial w;
	Matrix2D xy;
	
	public Beam(double E, double I, double q){
		integrate(new Polynomial(q).times(1./(E*I)));
	}
	
	public Beam(double E, double I, double... q){
		integrate(new Polynomial(q).times(1./(E*I)));
	}
	
	public Beam(double E, double I, double q, double[][] conditions)
			throws IllegalDimensionException, NoSquareException, SingularException{
		integrate(new Polynomial(q).times(1./(E*I)));
		condition(conditions);
	}
	
	private void integrate(Polynomial w4){
		w = w4.integrate().integrate().integrate().integrate();
	}
	
	public void condition(double[][] conditions) throws IllegalDimensionException, NoSquareException, SingularException{
		w.condition(conditions);
	}
	
	public Polynomial getW(){
		return w;
	}
	
	public double getW(double x){
		return w.evaluate(x);
	}
	
	public void printW(double x){
		System.out.println(getW(x));
	}
	
	public Matrix2D getXY(double start, double increment, double end){
		if(xy == null){
	      	try { xy = (Matrix2D) w.evaluateAsMatrix2D(start, increment, end).times(1); }
	      	catch(IllegalDimensionException e) { e.printStackTrace(); }
		}
		return xy;
	}
	
	@Override
	public String toString(){
		return w.toString();
	}
	
	public void println(){
		System.out.println(toString());
	}
	
	public void plot(){
        w.plot();
	}

	public static void main(String[] args) {
		Beam beam = new Beam(1, 1, -1);
        beam.println();
        try {
			beam.condition(new double[][]{
					{0,0,0},{1,0,0},{2,1,0},{3,1,0}});
		} catch (IllegalDimensionException | NoSquareException
				| SingularException e) {
			e.printStackTrace();
		}
        beam.println();
        beam.plot();
//        beam.getW().derive().plot();
	}

}
