Index: /applications/robust/robustlib.h
===================================================================
--- /applications/robust/robustlib.h (revision 1186)
+++ /applications/robust/robustlib.h (revision 1204)
@@ -15,4 +15,5 @@
 using namespace bdm;
 using namespace std;
+using namespace itpp;
 
 const double max_range = numeric_limits<double>::max()/10e-5;
@@ -30,4 +31,10 @@
 	int multiplicity;	
 
+	int split_state;
+
+	int merge_state;
+
+	
+
 public:
 	/// A list of polyhedrons parents within the Hasse diagram.
@@ -49,4 +56,12 @@
 	vector<polyhedron*> neutralchildren;
 
+	vector<polyhedron*> mergechildren;
+
+	polyhedron* positiveparent;
+
+	polyhedron* negativeparent;
+
+	int message_counter;
+
 	/// List of triangulation polyhedrons of the polyhedron given by their relative vertices. 
 	vector<vector<vertex*>> triangulations;
@@ -58,9 +73,11 @@
 	polyhedron()
 	{
-		multiplicity = 1;	
+		multiplicity = 1;
+
+		message_counter = 0;
 	}
 	
 	/// Setter for raising multiplicity
-	void RaiseMultiplicity()
+	void raise_multiplicity()
 	{
 		multiplicity++;
@@ -68,5 +85,5 @@
 
 	/// Setter for lowering multiplicity
-	void LowerMultiplicity()
+	void lower_multiplicity()
 	{
 		multiplicity--;
@@ -83,4 +100,104 @@
 	{
 		return false;
+	}
+
+	void set_state(double state_indicator, actions action)
+	{
+		switch(action)
+		{
+			case MERGE:
+				merge_state = (int)sign(state_indicator);			
+			break;
+			case SPLIT:
+				split_state = (int)sign(state_indicator);
+			break;
+		}
+	}
+
+	int get_state(actions action)
+	{
+		switch(action)
+		{
+			case MERGE:
+				return merge_state;			
+			break;
+			case SPLIT:
+				return split_state;
+			break;
+		}
+	}
+
+	int number_of_children()
+	{
+		return children.size()+positivechildren.size()+negativechildren.size()+neutralchildren.size();
+	}
+
+	void send_state_message(bool shouldsplit, bool shouldmerge)
+	{
+		if(shouldsplit||shouldmerge)
+		{
+			for(vector<polyhedron*>::iterator parent_iterator = parents.begin();parent_iterator<parents.end();parent_iterator++)
+			{
+				polyhedron* current_parent = *parent_iterator;
+
+				current_parent->message_counter++;
+
+				bool is_last = (current_parent->message_counter == current_parent->number_of_children());
+
+				if(shouldmerge)
+				{
+					int child_state  = get_state(MERGE);
+					int parent_state = current_parent->get_state(MERGE);
+
+					if(parent_state == NULL || parent_state == 0)
+					{
+						current_parent->set_state(child_state, MERGE);
+
+						if(child_state == 0)
+						{
+							current_parent->mergechildren.push_back(this);
+						}
+					}
+					else
+					{
+						if(child_state == 0)
+						{
+							if(parent_state > 0)
+							{
+								positiveparent = current_parent;
+							}
+							else
+							{
+								negativeparent = current_parent;
+							}
+						}
+					}
+
+					if(is_last)
+					{
+						if(parent_state > 0)
+						{
+							for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++)
+							{
+								(*merge_child)->positiveparent = current_parent;
+							}
+						}
+
+						if(parent_state < 0)
+						{
+							for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++)
+							{
+								(*merge_child)->negativeparent = current_parent;
+							}
+						}
+
+						current_parent->mergechildren.clear();
+					}
+
+
+				}
+			
+			}
+		}
 	}
 };
@@ -91,7 +208,11 @@
 {
 	/// A dynamic array representing coordinates of the vertex
-	vector<double> coordinates;
+	vec coordinates;	
+
+	enum actions {MERGE, SPLIT};
 
 public:
+
+
 
 	/// Default constructor
@@ -99,5 +220,5 @@
 
 	/// Constructor of a vertex from a set of coordinates
-	vertex(vector<double> coordinates)
+	vertex(vec coordinates)
 	{
 		this->coordinates = coordinates;
@@ -108,20 +229,15 @@
 	void push_coordinate(double coordinate)
 	{
-		coordinates.push_back(coordinate);
+		coordinates = concat(coordinates,coordinate);
 	}
 
 	/// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer 
 	/// (not given by reference), but a new copy is created (they are given by value).
-	vector<double> get_coordinates()
-	{
-		vector<double> returned_vec;
-
-		for(int i = 0;i<coordinates.size();i++)
-		{
-			returned_vec.push_back(coordinates[i]);
-		}
-
-		return returned_vec;
-	}
+	vec get_coordinates()
+	{		
+		return coordinates;
+	}
+
+		
 };
 
@@ -130,8 +246,8 @@
 class toprow : public polyhedron
 {
+	
+public:
 	/// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
-	vector<double> condition;
-
-public:
+	vec condition;
 
 	/// Default constructor
@@ -139,5 +255,5 @@
 
 	/// Constructor creating a toprow from the condition
-	toprow(vector<double> condition)
+	toprow(vec condition)
 	{
 		this->condition = condition;
@@ -146,5 +262,17 @@
 };
 
-
+class condition
+{	
+public:
+	vec value;	
+
+	int multiplicity;
+
+	condition(vec value)
+	{
+		this->value = value;
+		multiplicity = 1;
+	}
+}
 
 
@@ -156,4 +284,33 @@
 	/// of data update from Bayesian estimation or set by the user if this emlig is a prior density
 	vector<vector<polyhedron*>> statistic;
+
+	vector<condition*> conditions;
+
+	double normalization_factor;
+
+	void alter_toprow_conditions(vec condition, bool should_be_added)
+	{
+		for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++)
+		{
+			double product = 0;
+
+			vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();
+
+			do
+			{
+				product = (*vertex_ref)->coordinates*condition;
+			}
+			while(product == 0)
+
+			if((product>0 && should_be_added)||(product<0 && !should_be_added))
+			{
+				((toprow*) (*horiz_ref))->condition += condition;
+			}
+			else
+			{
+				((toprow*) (*horiz_ref))->condition -= condition;
+			}							
+		}
+	}
 	
 public:	
@@ -173,4 +330,78 @@
 	}
 
+	void add_and_remove_condition(vec toremove, vec toadd)
+	{
+		vector<condition*>::iterator toremove_ref = conditions.end();
+		bool condition_should_be_added = false;
+
+		for(vector<condition*>::iterator ref = conditions.begin();ref<conditions.end();ref++)
+		{
+			if(toremove != NULL)
+			{
+				if((*ref)->value == toremove)
+				{
+					if(multiplicity>1)
+					{
+						multiplicity--;
+
+						alter_toprow_conditions(toremove,false);
+
+						toremove = NULL;
+					}
+					else
+					{
+						toremove_ref = ref;							
+					}
+				}
+			}
+
+			if(toadd != NULL)
+			{
+				if((*iterator)->value == toadd)
+				{
+					(*iterator)->multiplicity++;
+
+					alter_toprow_conditions(toadd,true);
+
+					toadd = NULL;
+				}
+				else
+				{
+					condition_should_be_added = true;
+				}
+			}
+		}
+
+		if(toremove_ref!=conditions.end())
+		{
+			conditions.erase(toremove_ref);
+		}
+
+		if(condition_should_be_added)
+		{
+			conditions.push_back(new condition(toadd));
+		}
+
+		vector<vector<polyhedron*>> for_splitting;
+		vector<vector<polyhedron*>> for_merging;
+
+		for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++)
+		{		
+			vertex* current_vertex = (vertex*)horizontal_position;
+			
+			if(toadd != NULL)
+			{
+				current_vertex->set_state(toadd*current_vertex->coordinates,SPLIT);
+			}
+			
+			if(toremove != NULL)
+			{
+				current_vertex->set_state(toremove*current_vertex->coordinates,MERGE);
+			}
+
+			current_vertex->send_state_message(toadd != NULL, toremove != NULL);
+		}
+	}
+
 protected:
 
@@ -179,5 +410,5 @@
 	{
 		// An empty vector of coordinates.
-		vector<double> origin_coord;	
+		vec origin_coord;	
 
 		// We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords.
@@ -205,10 +436,9 @@
 			// of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the 
 			// right amount of zero cooridnates by reading them from the origin
-			vector<double> origin_coord1 = origin->get_coordinates();
-			vector<double> origin_coord2 = origin->get_coordinates();			
+			vec origin_coord = origin->get_coordinates();						
 
 			// And we incorporate the nonzero coordinates into the new cooordinate vectors
-			origin_coord1.push_back(max_range);
-			origin_coord2.push_back(-max_range);
+			vec origin_coord1 = concat(origin_coord,max_range);
+			vec origin_coord2 = concat(origin_coord,-max_range);
 
 			// Now we create the points
@@ -273,6 +503,9 @@
 					// this condition will be a vector of zeros. There are two vectors, because we need two copies of 
 					// the original Hasse diagram.
-					vector<double> vec1(i+2,0);
-					vector<double> vec2(i+2,0);
+					vec vec1(i+2);
+					vec1.zeros();
+
+					vec vec2(i+2);
+					vec2.zeros();
 
 					// We create a new toprow with the previously specified condition.
@@ -378,5 +611,20 @@
 
 //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
-class RARX : public BMEF{
+class RARX : public BM
+{
+private:
+
+	emlig posterior;
+
+public:
+	RARX():BM()
+	{
+	};
+
+	void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)
+	{
+		
+	}
+
 };
 
