XdmfPostFixCalc.cpp 36.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <stack>
#include <map>
#include <string>
#include <stdlib.h>
#include <math.h>
#include <XdmfArray.hpp>
#include <XdmfArrayType.hpp>
#include "boost/assign.hpp"

double parse(std::string expression, std::map<std::string, double> variables);
double calculation(double val1, double val2, char operation);
double function(std::vector<double> valueVector, std::string functionName);
double sum(std::vector<double> values);
double ave(std::vector<double> values);
shared_ptr<XdmfArray> parse(std::string expression, std::map<std::string, shared_ptr<XdmfArray> > variables);
shared_ptr<XdmfArray> calculation(shared_ptr<XdmfArray> val1, shared_ptr<XdmfArray> val2, char operation);
shared_ptr<XdmfArray> invChunk(shared_ptr<XdmfArray> val1, shared_ptr<XdmfArray> val2);
shared_ptr<XdmfArray> function(std::vector<shared_ptr<XdmfArray> > valueVector, std::string functionName);
shared_ptr<XdmfArray> sum(std::vector<shared_ptr<XdmfArray> > values);
shared_ptr<XdmfArray> ave(std::vector<shared_ptr<XdmfArray> > values);
shared_ptr<XdmfArray> maximum(std::vector<shared_ptr<XdmfArray> > values);

std::string validDigitChars = "1234567890.";
std::string validVariableChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_:.";

//std::map<std::string,  double (*)(std::vector<double>)> functions = boost::assign::map_list_of ("SUM", sum) ("AVE", ave);
std::map<std::string,  double (*)(std::vector<double>)> functions = boost::assign::map_list_of ("SUM", (double (*)(std::vector<double>))sum);

//note, it doesn't handle overloaded functions well. Will generate errors unless typecast
//std::map<std::string,  double (*)(std::vector<double>)> functions = boost::assign::map_list_of ("SUM", sum) ("AVE", ave);
std::map<std::string,  shared_ptr<XdmfArray> (*)(std::vector<shared_ptr<XdmfArray> >)> arrayFunctions = boost::assign::map_list_of ("SUM", (shared_ptr<XdmfArray> (*)(std::vector<shared_ptr<XdmfArray> >))sum) ("MAX", (shared_ptr<XdmfArray> (*)(std::vector<shared_ptr<XdmfArray> >))maximum);



int main(int, char **)
{
	XdmfArray::addFunction("MAX", maximum);
39 40 41 42 43 44
        XdmfArray::addOperation('&', invChunk, 2);
	XdmfArray::addOperation('!', invChunk, 2);
	XdmfArray::addOperation('^', invChunk, 2);
	XdmfArray::addOperation('>', invChunk, 2);
	XdmfArray::addOperation('<', invChunk, 2);
	XdmfArray::addOperation('@', invChunk, 2);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

	functions["AVE"] = ave;
	//sometimes typecasts are required, sometimes they cause errors
	//I think it needs to be cast if it isn't the first variation of the function listed
	arrayFunctions["AVE"] = (shared_ptr<XdmfArray> (*)(std::vector<shared_ptr<XdmfArray> >))ave;

	std::string problemToSolve = "2*twentyfive+2*3+2^(3+8)/3+8/(4+1)+SUM(4, twentyfive*4, 6) + AVE(100, 0, 0, 0)";

	std::map<std::string, double> variableTable;
	variableTable["twentyfive"] = 25;
	variableTable["one"] = 1;
	variableTable["two"] = 2;
	variableTable["three"] = 3;
	variableTable["four"] = 4;
	variableTable["eight"] = 8;

	double answer = parse(problemToSolve, variableTable);

63 64 65 66
	std::cout << answer << std::endl;

	//unless the calculation is fully written out it won't equal properly
	assert(answer == 2*25+2*3+(double)2048/3+(double)8/5+4+100+6+25);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	//std::string arrayExpression = "A|B#C|D";
	std::string arrayExpression = "MAX(2,(AVE(A@B)#AVE(C|D)))";

	shared_ptr<XdmfArray> testArray1 = XdmfArray::New();
	for (int i = 0; i < 10; i++)
	{
		testArray1->pushBack(1);
	}
	shared_ptr<XdmfArray> testArray2 = XdmfArray::New();
	for (int i = 0; i < 10; i++)
	{
		testArray2->pushBack(2);
	}
	shared_ptr<XdmfArray> testArray3 = XdmfArray::New();
	for (int i = 0; i < 10; i++)
	{
		testArray3->pushBack(3);
	}
	shared_ptr<XdmfArray> testArray4 = XdmfArray::New();
	for (int i = 0; i < 10; i++)
	{
		testArray4->pushBack(4);
	}
	shared_ptr<XdmfArray> testArray5 = XdmfArray::New();
	for (int i = 0; i < 13; i++)
	{
		testArray5->pushBack(5);
	}

	std::map<std::string, shared_ptr<XdmfArray> > arrayVariable;
	arrayVariable["A"] = testArray1;
	arrayVariable["B"] = testArray2;
	arrayVariable["C"] = testArray3;
	arrayVariable["D"] = testArray4;
	arrayVariable["E"] = testArray5;

104
	std::cout << "before parsing" << std::endl;
105 106 107 108

	shared_ptr<XdmfArray> answerArray;
	answerArray = parse(arrayExpression, arrayVariable);

109 110 111 112 113 114 115 116 117
	std::cout << "after parsing" << std::endl;

	std::cout << "answer array = " << answerArray->getValuesString() << std::endl;

	assert(answerArray->getValuesString().compare("3.5") == 0);

	std::cout << "array size = " << answerArray->getSize() << std::endl;

	assert(answerArray->getSize() == 1);
118

119 120 121
	std::cout << "interlace" << std::endl;
	answerArray = XdmfArray::evaluateOperation(testArray1, testArray5, '#');
	std::cout << "answer array = " << answerArray->getValuesString() << std::endl;
122

123
	assert(answerArray->getValuesString().compare("1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 5 5 5") == 0);
124

125 126 127
	std::cout << "chunk" << std::endl;
	answerArray = XdmfArray::evaluateOperation(testArray1, testArray5, '|');
	std::cout << "answer array = " << answerArray->getValuesString() << std::endl;
128

129 130 131 132 133 134 135
	assert(answerArray->getValuesString().compare("1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5") == 0);

	std::cout << "inverse chunk" << std::endl;
	answerArray = XdmfArray::evaluateOperation(testArray1, testArray5, '@');
	std::cout << "answer array = " << answerArray->getValuesString() << std::endl;

	assert(answerArray->getValuesString().compare("5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 1") == 0);
136 137 138

	answerArray = XdmfArray::evaluateExpression(arrayExpression, arrayVariable);

139 140 141 142 143 144 145
	std::cout << "after parsing" << std::endl;

	std::cout << "answer array = " << answerArray->getValuesString() << std::endl;

	assert(answerArray->getValuesString().compare("3.5") == 0);

	std::cout << "array size = " << answerArray->getSize() << std::endl;
146

147
	assert(answerArray->getSize() == 1);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	return 0;
}

double parse(std::string expression, std::map<std::string, double> variables)
{
	std::stack<double> valueStack;
	std::stack<char> operationStack;

	std::string validOperationChars = "+-*/()^"; //will be global at some point, all supported operations
	int operationPriority [validOperationChars.size()];
	//list the priorities for the operations, based on the order of operations
	//the index of the corresponding operation in validOperationChars is the same as the index of its priority in this array
	operationPriority[0] = 1;//+
	operationPriority[1] = 1;//-
	operationPriority[2] = 2;//*
	operationPriority[3] = 2;///
	operationPriority[4] = 0;//(
	operationPriority[5] = 0;//)
	operationPriority[6] = 3;//^
	//the higher the value, the earlier the operation is evaluated in the order of operations
	//with the exception of parenthesis which are evaluated as soon as the closing parenthesis is found

	//string is parsed left to right
	//elements of the same priority are evaluated right to left
173
	for (unsigned int i = 0; i < expression.size(); i++)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	{
		if (validDigitChars.find(expression[i]) != std::string::npos)//found to be a digit
		{
			//progress until a non-digit is found
			int valueStart = i;
			while (validDigitChars.find(expression[i + 1]) != std::string::npos)
			{
				i++;
			}
			//push back to the value stack
			valueStack.push(atof(expression.substr(valueStart, i + 1 - valueStart).c_str()));//use this to convert to double
		}
		else if (validVariableChars.find(expression[i]) != std::string::npos)//found to be a variable
		{
			int valueStart = i;
			//progress until a nonvariable value is found
			while (validVariableChars.find(expression[i + 1]) != std::string::npos)
			{
				i++;
			}
			//convert to equivalent
			if (variables.find(expression.substr(valueStart, i + 1 - valueStart)) == variables.end())
			{
				if (functions.find(expression.substr(valueStart, i + 1 - valueStart)) == functions.end())
				{
199
					std::cout << "Error: Invalid Variable or Function: " << expression.substr(valueStart, i + 1 - valueStart) << std::endl;
200 201 202 203 204 205 206 207
					return 0;
				}
				else
				{
					std::string currentFunction = expression.substr(valueStart, i + 1 - valueStart);
					//check if next character is an open parenthesis
					if (expression[i+1] != '(')
					{
208
						std::cout << "Error: No values supplied to function " << expression.substr(valueStart, i + 1 - valueStart) << std::endl;
209 210 211 212 213
						return 0;
					}
					//if it is grab the string between paranthesis
					i = i + 2;
					valueStart = i;
214
					int numOpenParenthesis = 0;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
					while ((expression[i] != ')' || numOpenParenthesis) && i < expression.size())
					{
						if (expression[i] == '(')
						{
							numOpenParenthesis++;
						}
						else if (expression[i] == ')')
						{
							numOpenParenthesis--;
						}
						i++;
					}
					std::string functionParameters = expression.substr(valueStart, i - valueStart);
					std::vector<double> parameterVector;
					//split that string at commas
					size_t parameterSplit = 0;
					while (parameterSplit != std::string::npos)
					{
						parameterSplit = 0;
						parameterSplit = functionParameters.find_first_of(",", parameterSplit);
						//feed the substrings to the parse function
						if (parameterSplit == std::string::npos)
						{
							parameterVector.push_back(parse(functionParameters, variables));
						}
						else
						{
							parameterVector.push_back(parse(functionParameters.substr(0, parameterSplit), variables));
							functionParameters = functionParameters.substr(parameterSplit+1);
						}
					}
					valueStack.push(function(parameterVector, currentFunction));
				}
			}
			else
			{
				//push equivalent to value stack
				valueStack.push(variables.find(expression.substr(valueStart, i + 1 - valueStart))->second);
			}
		}
		else if (validOperationChars.find(expression[i]) != std::string::npos)//found to be an operation
		{
			//pop operations off the stack until one of a lower or equal importance is found
			if (operationStack.size() > 0)
			{
				if (expression[i] == ')')
				{
					//to close a parenthesis pop off all operations until another parentheis is found
					while (operationStack.size() > 0 && operationStack.top() != '(')
					{
						if (valueStack.size() < 2)//must be at least two values for this loop to work properly
						{
							//error, not enough values
268
							std::cout << "Error: Not Enough Values" << std::endl;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
							return 0;
						}
						else
						{
							double val2 = valueStack.top();
							valueStack.pop();
							double val1 = valueStack.top();
							valueStack.pop();
							valueStack.push(calculation(val1, val2, operationStack.top()));
							operationStack.pop();
						}
					}
					operationStack.pop();
				}
				else if (expression[i] == '(')
				{
					//just add it if it's a start parenthesis
					//nothing happens here in that case
					//addition happens after the if statement
				}
				else
				{
					size_t operationLocation = validOperationChars.find(expression[i]);
					size_t topOperationLocation = validOperationChars.find(operationStack.top());
					//see order of operations to determine importance
					while (operationStack.size() > 0 && operationPriority[operationLocation] < operationPriority[topOperationLocation])
					{
						if (valueStack.size() < 2)//must be at least two values for this loop to work properly
						{
							//error, not enough values
299
							std::cout << "Error: Not Enough Values" << std::endl;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
							return 0;
						}
						else
						{
							double val2 = valueStack.top();
							valueStack.pop();
							double val1 = valueStack.top();
							valueStack.pop();
							valueStack.push(calculation(val1, val2, operationStack.top()));
							operationStack.pop();
							if (operationStack.size() == 0) 
							{
								break;
							}
							topOperationLocation = validOperationChars.find(operationStack.top());
						}
					}
				}
			}
			if (expression[i] != ')')
			{
				//add the operation to the operation stack
				operationStack.push(expression[i]);
			}
		}
		//if not a value or operation the character is ignored
	}

	//empty what's left in the stacks before finishing
	while (valueStack.size() > 1)
	{
		if (valueStack.size() < 2)//must be at least two values for this loop to work properly
		{
			//error, not enough values
334
			std::cout << "Error: Not Enough Values" << std::endl;
335 336 337 338 339 340
			return 0;
		}
		else
		{
			if(operationStack.top() == '(')
			{
341
				std::cout << "Warning: Unpaired Parenthesis" << std::endl;
342 343 344 345 346 347 348 349 350 351 352
				operationStack.pop();
			}
			else
			{
				double val2 = valueStack.top();
				valueStack.pop();
				double val1 = valueStack.top();
				valueStack.pop();
				if (operationStack.size() == 0)
				{
					//error, not enough operations
353
					std::cout << "Error: Not Enough Operators" << std::endl;
354 355 356 357 358 359 360 361 362 363 364 365 366 367
					return 0;
				}
				else
				{
					valueStack.push(calculation(val1, val2, operationStack.top()));
					operationStack.pop();
				}
			}
		}
	}

	//throw error if there's extra operations
	if (operationStack.size() > 0)
	{
368
		std::cout << "Warning: Left Over Operators" << std::endl;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	}

	return valueStack.top();
}

double calculation(double val1, double val2, char operation)
{
	//perform the appropriate operation as designated by the string supplied
	if (operation == '+')//addition
	{
		return val1 + val2;
	}
	else if (operation == '-')//subtraction
	{
		return val1 - val2;
	}
	else if (operation == '*')//multiplication
	{
		return val1 * val2;
	}
	else if (operation == '/')//division
	{
		return val1 / val2;
	}
	else if (operation == '^')//exponent
	{
		return pow(val1, val2);
	}
	//insert new operations into this huge if statement
	return 0.0;//return 0 as a default
}

//this is how you use references to functions
double function(std::vector<double> valueVector, std::string functionName)
{
	return (*functions[functionName])(valueVector);
}

double sum(std::vector<double> values)
{
	double total = 0.0;
410
	for (unsigned int i = 0; i < values.size(); i++)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	{
		total += values[i];
	}
	return total;
}

double ave(std::vector<double> values)
{
	double total = sum(values);
	return total/values.size();
}

shared_ptr<XdmfArray> parse(std::string expression, std::map<std::string, shared_ptr<XdmfArray> > variables)
{
	std::stack<shared_ptr<XdmfArray> > valueStack;
	std::stack<char> operationStack;

	std::string validOperationChars = "|#()@"; //will be global at some point, all supported operations
	int operationPriority [validOperationChars.size()];
	//list the priorities for the operations, based on the order of operations
	//the index of the corresponding operation in validOperationChars is the same as the index of its priority in this array
	operationPriority[0] = 2;//|
	operationPriority[1] = 1;//#
	operationPriority[2] = 0;//(
	operationPriority[3] = 0;//)
	operationPriority[4] = 2;//@
	//the higher the value, the earlier the operation is evaluated in the order of operations
	//with the exception of parenthesis which are evaluated as soon as the closing parenthesis is found

	//string is parsed left to right
	//elements of the same priority are evaluated right to left
442
	for (unsigned int i = 0; i < expression.size(); i++)
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	{
		if (validDigitChars.find(expression[i]) != std::string::npos)//found to be a digit
		{
			//progress until a non-digit is found
			int valueStart = i;
			while (validDigitChars.find(expression[i + 1]) != std::string::npos)
			{
				i++;
			}
			//push back to the value stack
			//create a new array to hold the value
			shared_ptr<XdmfArray> valueArray = XdmfArray::New();
			valueArray->insert(0, atof(expression.substr(valueStart, i + 1 - valueStart).c_str()));
			valueStack.push(valueArray);
		}
		else if (validVariableChars.find(expression[i]) != std::string::npos)//found to be a variable
		{
			int valueStart = i;
			//progress until a nonvariable value is found
			while (validVariableChars.find(expression[i + 1]) != std::string::npos)
			{
				i++;
			}
			//convert to equivalent
			if (variables.find(expression.substr(valueStart, i + 1 - valueStart)) == variables.end())
			{
				if (arrayFunctions.find(expression.substr(valueStart, i + 1 - valueStart)) == arrayFunctions.end())
				{
471
					std::cout << "Error: Invalid Variable or Function" << std::endl;
472 473 474 475 476 477 478 479
					return XdmfArray::New();
				}
				else
				{
					std::string currentFunction = expression.substr(valueStart, i + 1 - valueStart);
					//check if next character is an open parenthesis
					if (expression[i+1] != '(')
					{
480
						std::cout << "Error: No Values Supplied to Function " << expression.substr(valueStart, i + 1 - valueStart) << std::endl;
481 482 483 484 485
						return XdmfArray::New();
					}
					//if it is grab the string between paranthesis
					i = i + 2;
					valueStart = i;
486
					int numOpenParenthesis = 0;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
					while ((expression[i] != ')' || numOpenParenthesis) && i < expression.size())
					{
						if (expression[i] == '(')
						{
							numOpenParenthesis++;
						}
						else if (expression[i] == ')')
						{
							numOpenParenthesis--;
						}
						i++;
					}
					std::string functionParameters = expression.substr(valueStart, i - valueStart);
					std::vector<shared_ptr<XdmfArray> > parameterVector;
					//split that string at commas
					size_t parameterSplit = 0;
					while (parameterSplit != std::string::npos)
					{
						parameterSplit = 0;
						parameterSplit = functionParameters.find_first_of(",", parameterSplit);
						//feed the substrings to the parse function
						if (parameterSplit == std::string::npos)
						{
							parameterVector.push_back(parse(functionParameters, variables));
						}
						else
						{
							parameterVector.push_back(parse(functionParameters.substr(0, parameterSplit), variables));
							functionParameters = functionParameters.substr(parameterSplit+1);
						}
					}
					valueStack.push(function(parameterVector, currentFunction));
				}
			}
			else
			{
				//push equivalent to value stack
				valueStack.push(variables.find(expression.substr(valueStart, i + 1 - valueStart))->second);
			}
		}
		else if (validOperationChars.find(expression[i]) != std::string::npos)//found to be an operation
		{
			//pop operations off the stack until one of a lower or equal importance is found
			if (operationStack.size() > 0)
			{
				if (expression[i] == ')')
				{
					//to close a parenthesis pop off all operations until another parentheis is found
					while (operationStack.size() > 0 && operationStack.top() != '(')
					{
						if (valueStack.size() < 2)//must be at least two values for this loop to work properly
						{
							//error, not enough values
							return XdmfArray::New();
						}
						else
						{
							shared_ptr<XdmfArray> val2 = valueStack.top();
							valueStack.pop();
							shared_ptr<XdmfArray> val1 = valueStack.top();
							valueStack.pop();
							valueStack.push(calculation(val1, val2, operationStack.top()));
							operationStack.pop();
						}
					}
					operationStack.pop();
				}
				else if (expression[i] == '(')
				{
					//just add it if it's a start parenthesis
					//nothing happens here in that case
					//addition happens after the if statement
				}
				else
				{
					size_t operationLocation = validOperationChars.find(expression[i]);
					size_t topOperationLocation = validOperationChars.find(operationStack.top());
					//see order of operations to determine importance
					while (operationStack.size() > 0 && operationPriority[operationLocation] < operationPriority[topOperationLocation])
					{
						if (valueStack.size() < 2)//must be at least two values for this loop to work properly
						{
							//error, not enough values
570
							std::cout << "Error: Not Enough Values" << std::endl;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
							return XdmfArray::New();
						}
						else
						{
							shared_ptr<XdmfArray> val2 = valueStack.top();
							valueStack.pop();
							shared_ptr<XdmfArray> val1 = valueStack.top();
							valueStack.pop();
							valueStack.push(calculation(val1, val2, operationStack.top()));
							operationStack.pop();
							if (operationStack.size() == 0)
							{
								break;
							}
							topOperationLocation = validOperationChars.find(operationStack.top());
						}
					}
				}
			}
			if (expression[i] != ')')
			{
				//add the operation to the operation stack
				operationStack.push(expression[i]);
			}
		}
		//if not a value or operation the character is ignored
	}


	//empty what's left in the stacks before finishing
	while (valueStack.size() > 1 && operationStack.size() > 0)
	{
		if(operationStack.top() == '(')
		{
605
			std::cout << "Warning: Unpaired Parenthesis" << std::endl;
606 607 608 609 610
			operationStack.pop();
		}
		else if (valueStack.size() < 2)//must be at least two values for this loop to work properly
		{
			//error, not enough values
611
			std::cout << "Error: Not Enough Values" << std::endl;
612 613 614 615 616 617 618 619 620 621 622
			return XdmfArray::New();
		}
		else
		{
			shared_ptr<XdmfArray> val2 = valueStack.top();
			valueStack.pop();
			shared_ptr<XdmfArray> val1 = valueStack.top();
			valueStack.pop();
			if (operationStack.size() == 0)
			{
				//error, not enough operations
623
				std::cout << "Error: Not Enough Operators" << std::endl;
624 625 626 627 628 629 630 631 632 633 634 635 636
				return XdmfArray::New();
			}
			else
			{
				valueStack.push(calculation(val1, val2, operationStack.top()));
				operationStack.pop();
			}
		}
	}

	//throw error if there's extra operations
	if (operationStack.size() > 0)
	{
637
		std::cout << "Warning: Left Over Operators" << std::endl;
638 639 640 641
	}

	if (valueStack.size() > 1)
	{
642
		std::cout << "Warning: Left Over Values" << std::endl;
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	}

	return valueStack.top();
}

shared_ptr<XdmfArray> calculation(shared_ptr<XdmfArray> val1, shared_ptr<XdmfArray> val2, char operation)
{
	//perform the appropriate operation as designated by the string supplied
	if (operation == '|')//join chunk (add the new array to the end of the first one)
	{
		//joins into new array and returns it
		shared_ptr<XdmfArray> returnArray = XdmfArray::New();
		returnArray->insert(0, val1, 0, val1->getSize(),  1, 1);
		returnArray->insert(val1->getSize(), val2, 0, val2->getSize(), 1, 1);
		return returnArray;
	}
	else if (operation == '@')//inverse chunk
	{
		//joins into new array and returns it
		shared_ptr<XdmfArray> returnArray = XdmfArray::New();
		returnArray->insert(0, val2, 0, val2->getSize(),  1, 1);
		returnArray->insert(val2->getSize(), val1, 0, val1->getSize(), 1, 1);
		return returnArray;
	}
	else if (operation == '#')//join interlace (evenly space the second array within the first one)
	{
		//builds a new array
		shared_ptr<XdmfArray> returnArray = XdmfArray::New();
		//resize to the combined size of both arrays
		//might not be required?
		//what type to class it as?
		shared_ptr<const XdmfArrayType> arrayType1 = val1->getArrayType();
		shared_ptr<const XdmfArrayType> arrayType2 = val2->getArrayType();
		if (arrayType1 == XdmfArrayType::Int8())
		{
			//if floats reclass as floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints reclass as ints of the appropriate size
			else if (arrayType2 == XdmfArrayType::Int64() || arrayType2 == XdmfArrayType::UInt32())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int32() || arrayType2 == XdmfArrayType::UInt16())
			{
				int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int16() || arrayType2 == XdmfArrayType::UInt8())
			{
				short sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else
			{
				char sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
		}
		else if (arrayType1 == XdmfArrayType::Int16())
		{
			//if floats reclass as floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints reclass as ints of the appropriate size
			else if (arrayType2 == XdmfArrayType::Int64() || arrayType2 == XdmfArrayType::UInt32())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int32() || arrayType2 == XdmfArrayType::UInt16())
			{
				int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else
			{
				short sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
		}
		else if (arrayType1 == XdmfArrayType::Int32())
                {
			//if floats reclass as floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints reclass as ints of the appropriate size
			else if (arrayType2 == XdmfArrayType::Int64() || arrayType2 == XdmfArrayType::UInt32())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else
			{
				int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
		else if (arrayType1 == XdmfArrayType::Int64())
                {
			//if floats reclass as floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
                                double sampleValue = 0.0;
                                returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
                        }
			//if uints reclass as ints of the appropriate size
			else
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
                else if (arrayType1 == XdmfArrayType::Float32())
                {
			//use floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
                else if (arrayType1 == XdmfArrayType::Float64())
                {
			//use floats of the appropriate size
			if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
                else if (arrayType1 == XdmfArrayType::UInt8())
                {
			//if int are used reclass as int of the larger size
			if (arrayType2 == XdmfArrayType::Int8() || arrayType2 == XdmfArrayType::Int16())
			{
				short sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int32())
			{
				int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int64())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if floats are used, reclass as floats of the appropriate size
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints are used, adjust size as required
			else if (arrayType2 == XdmfArrayType::UInt8())
			{
				unsigned char sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::UInt16())
			{
				unsigned short sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::UInt32())
			{
				unsigned int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
		}
		else if (arrayType1 == XdmfArrayType::UInt16())
		{
			//if int are used reclass as int of the larger size
                        if (arrayType2 == XdmfArrayType::Int8() || arrayType2 == XdmfArrayType::Int16() || arrayType2 == XdmfArrayType::Int32())
			{
				int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Int64())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if floats are used, reclass as floats of the appropriate size
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints are used, adjust size as required
			else if (arrayType2 == XdmfArrayType::UInt8() || arrayType2 == XdmfArrayType::UInt16())
			{
				unsigned short sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::UInt32())
			{
				unsigned int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
                else if (arrayType1 == XdmfArrayType::UInt32())
                {
			//if int are used reclass as int of the larger size
			if (arrayType2 == XdmfArrayType::Int8() || arrayType2 == XdmfArrayType::Int16() || arrayType2 == XdmfArrayType::Int32() || arrayType2 == XdmfArrayType::Int64())
			{
				long sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if floats are used, reclass as floats of the appropriate size
			else if (arrayType2 == XdmfArrayType::Float32())
			{
				float sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::Float64())
			{
				double sampleValue = 0.0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			//if uints are used, adjust size as required
			else if (arrayType2 == XdmfArrayType::UInt8() || arrayType2 == XdmfArrayType::UInt16() || arrayType2 == XdmfArrayType::UInt32())
			{
				unsigned int sampleValue = 0;
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
			else if (arrayType2 == XdmfArrayType::String())
			{
				//string is the only compatible type here
				std::string sampleValue = "";
				returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
			}
                }
                else if (arrayType1 == XdmfArrayType::String())
                {
			//string is the only compatible type here
			std::string sampleValue = "";
			returnArray->resize(val1->getSize()+val2->getSize(), sampleValue);
                }
		//determine ratio of array sizes
978 979
		int arrayRatio1 = (int)floor(static_cast<double>(val1->getSize())/val2->getSize());
		int arrayRatio2 = (int)floor(static_cast<double>(val2->getSize())/val1->getSize());
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
		if (arrayRatio1 < 1)
		{
			arrayRatio1 = 1;
		}
		if (arrayRatio2 < 1)
		{
			arrayRatio2 = 1;
		}
		//stride is equal to the ratios rounded up and added together
		int stride = arrayRatio1+arrayRatio2;
		int arrayExcess1 = 0;
		int arrayExcess2 = 0;
		for (int i = 0; i < stride; i++)
		{
			//add the values of each array, using strides to interlace and starting index to offset
			//first array gets the first value of the new array
			if (i<arrayRatio1)
			{
				int amountWritten = val1->getSize()/arrayRatio1;
999
				if (((amountWritten * arrayRatio1) + i) < (int)val1->getSize())
1000 1001 1002
				{
					amountWritten++;
				}
1003
				if (amountWritten > (int)floor(val2->getSize()/arrayRatio2))
1004
				{
1005 1006
					arrayExcess1 += amountWritten - (int)floor(val2->getSize()/arrayRatio2);
					amountWritten = (int)floor(val2->getSize()/arrayRatio2);
1007 1008 1009 1010 1011 1012
				}
				returnArray->insert(i, val1, i, amountWritten, stride, arrayRatio1);
			}
			else //second array takes the rest
			{
				int amountWritten = val2->getSize()/arrayRatio2;
1013
				if (((amountWritten * arrayRatio2) + i) < (int)val2->getSize())
1014 1015 1016
				{
					amountWritten++;
				}
1017
				if (amountWritten > (int)floor(val1->getSize()/arrayRatio1))
1018
				{
1019 1020
					arrayExcess2 += amountWritten - (int)floor(val1->getSize()/arrayRatio1);
					amountWritten = (int)floor(val1->getSize()/arrayRatio1);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
				}
				returnArray->insert(i, val2, i-arrayRatio1, amountWritten, stride, arrayRatio2);
			}
			
		}
		if (arrayExcess1 > 0)
		{
			returnArray->insert(val1->getSize()+val2->getSize()-arrayExcess1, val1, 0, arrayExcess1, 1, 1);
		}
		else if (arrayExcess2 > 0)
		{
			returnArray->insert(val1->getSize()+val2->getSize()-arrayExcess2, val2, 0, arrayExcess2, 1, 1);
		}
		returnArray->resize(val1->getSize()+val2->getSize(), 0);
		//after all inserts are done, add the excess values to the end of the array
		return returnArray;
	}
	//insert new operations into this huge if statement
	return XdmfArray::New();//return a blank array as a default
}

shared_ptr<XdmfArray> invChunk(shared_ptr<XdmfArray> val1, shared_ptr<XdmfArray> val2)
{
	//joins into new array and returns it
	shared_ptr<XdmfArray> returnArray = XdmfArray::New();
	returnArray->insert(0, val2, 0, val2->getSize(),  1, 1);
	returnArray->insert(val2->getSize(), val1, 0, val1->getSize(), 1, 1);
	return returnArray;
}

//this is how you use references to functions
shared_ptr<XdmfArray> function(std::vector<shared_ptr<XdmfArray> > valueVector, std::string functionName)
{
	if (arrayFunctions.find(functionName) == arrayFunctions.end())
	{
		return XdmfArray::New();
	}
	else
	{
		return (*arrayFunctions[functionName])(valueVector);
	}
}

shared_ptr<XdmfArray> sum(std::vector<shared_ptr<XdmfArray> > values)
{
        double total = 0.0;
1067
        for (unsigned int i = 0; i < values.size(); i++)
1068
        {
1069
		for (unsigned int j = 0; j < values[i]->getSize(); j++)
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
		{
                	total += values[i]->getValue<double>(j);
		}
        }
	shared_ptr<XdmfArray> returnArray = XdmfArray::New();
	returnArray->insert(0, total);
        return returnArray;
}

shared_ptr<XdmfArray> ave(std::vector<shared_ptr<XdmfArray> > values)
{
	double total = sum(values)->getValue<double>(0);;
	int totalSize = 0;
1083
	for (unsigned int i = 0; i < values.size(); i++)
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	{
		totalSize += values[i]->getSize();
	}
	shared_ptr<XdmfArray> returnArray = XdmfArray::New();
	returnArray->insert(0, total/totalSize);
	return returnArray;
}

shared_ptr<XdmfArray> maximum(std::vector<shared_ptr<XdmfArray> > values)
{
	if (values[0]->getArrayType() == XdmfArrayType::String())
	{
		shared_ptr<XdmfArray> returnArray = XdmfArray::New();
		returnArray->pushBack(values[0]->getValue<std::string>(0));
		return returnArray;
	}
	else
	{
		double maxVal = values[0]->getValue<double>(0);
1103
		for (unsigned int i = 0; i < values.size(); i++)
1104
		{
1105
			for (unsigned int j = 0; j < values[i]->getSize(); j++)
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
			{
				if (maxVal < values[i]->getValue<double>(j))
				{
					maxVal = values[i]->getValue<double>(j);
				}
			}
		}
		shared_ptr<XdmfArray> returnArray = XdmfArray::New();
		returnArray->pushBack(maxVal);
		return returnArray;
	}
}