This file is indexed.

/usr/share/perl5/Math/Calculus/Expression.pm is in libmath-calculus-expression-perl 0.2.2.ds-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  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
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 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
 173
 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
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 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
 268
 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
 299
 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
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 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
 410
 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
 442
 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
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 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
 570
 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
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 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
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
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
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# ########################################################################################
# A CALCULUS EXPRESSION OBJECT
# Common algebra routines module by Jonathan Worthington.
# Copyright (C) Jonathan Worthington 2004-2005
# This module may be used and distributed under the same terms as Perl.
# ########################################################################################

package Math::Calculus::Expression;
use strict;
our $VERSION = '0.2.2';

=head1 NAME

Math::Calculus::Expression - Algebraic Calculus Tools Expression Class

=head1 SYNOPSIS

  use Math::Calculus::Expression;

  # Create an expression object.
  my $exp = Math::Calculus::Expression->new;
  
  # Set a variable and expression.
  $exp->addVariable('x');
  $exp->setExpression('x^(2+1) + 6*5*x') or die $exp->getError;
  
  # Simplify
  $exp->simplify or die $exp->getError;;
  
  # Print the result.
  print $exp->getExpression; # Prints x^3 + 30*x
  

=head1 DESCRIPTION

This module can take an algebraic expression, parse it into a tree structure, simplify
the tree, substitute variables and named constants for other variables or constants
(which may be numeric), numerically evaluate the tree and turn the tree back into an
output of the same form as the input.

It supports a wide range of expressions including the +, -, *, / and ^ (raise to
power) operators, bracketed expressions to enable correct precedence and the functions
ln, exp, sin, cos, tan, sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin,
acos, atan, asinh, acosh and atanh.

=head1 EXPORT

None by default.

=head1 METHODS

=cut

# Constructor
# ###########

=over

=item new

  $exp = Math::Calculus::Expression->new;

Creates a new instance of the expression object, which can hold an individual
expression and perform basic operations on it.

=cut

sub new {
	# Get invocant.
	my $invocant = shift;
	
	# Create object.
	my $self = {
		traceback	=> '',
		error		=> '',
		expression	=> 0,
		variables	=> [],
	};
	return bless $self, $invocant;
}


# Add variable.
# #############

=item addVariable

  $exp->addVariable('x');

Sets a certain named value in the expression as being a variable. A named value must be
an alphabetic chracter.

=cut

sub addVariable {
	# Get invocant and parameters.
	my ($self, $var) = @_;
	
	# Provided the variable is just one character and we don't already have it...
	unless (length($var) != 1 || grep { $_ eq $var } @{$self->{'variables'}}) {
		$self->{'variables'}->[@{$self->{'variables'}}] = $var;
		$self->{'error'} = '';
		return 1;
	} else {
		$self->{'error'} = 'Invalid variable or variable already added.';
		return undef;
	}
}


# Set Expression
# ##############

=item setExpression

  $exp->setExpression('x^2 + 5*x);

Takes an expression in human-readable form and stores it internally as a tree structure,
checking it is a valid expression that the module can understand in the process. Note that
the module is strict about syntax. For example, note above that you must write 5*x and not
just 5x. Whitespace is allowed in the expression, but does not have any effect on precedence.
If you require control of precedence, use brackets; bracketed expressions will always be
evaluated first, as you would normally expect. The module follows the BODMAS precedence
convention. Returns undef on failure and a true value on success.

=cut

sub setExpression {
	# Get invocant and parameters.
	my ($self, $expr) = @_;
	
	# Clear up the expression.
	$expr =~ s/\s//g;
	1 while $expr =~ s/--/+/g
	     || $expr =~ s/\+-|-\+/-/g
	     || $expr =~ s/([+\-*\/\^])\+/$1/g
	     || $expr =~ s/^\+//g;
	
	# Build expression tree.
	$self->{'error'} = $self->{'traceback'} = undef;
	$self->{'expression'} = $self->buildTree($expr);
	
	# Return depending on whether there was an error.
	if ($self->{'error'}) {
		return undef;
	} else {
		return 1;
	}
}


# Get Expression
# ##############

=item getExpression

  $expr = $exp->getExpression;

Returns a textaul, human readable representation of the expression that is being stored.

=cut

sub getExpression {
	# Get invocant.
	my $self = shift;
	
	# Walk expression tree and generate something to display.
	$self->{'error'} = '';
	my $text = $self->prettyPrint($self->{'expression'});
	
	# If there was an error, return nothing.
	if ($self->{'error'}) {
		return undef;
	} else {
		return $text;
	}
}


# Simplify.
# #########

=item simplify

  $exp->simplify;

Attempts to simplify the expression that is stored internally.

=cut

sub simplify {
	# Get invocant.
	my ($self) = @_;
	
	# Clear error.
	$self->{'error'} = undef;
	
	# Simplify.
	eval {
		$self->{'expression'} = $self->recSimplify($self->{'expression'}, undef);
	};
	
	# We may have boiled it all down to a numerical constant...
	my $const = $self->numericEvaluation($self->{'expression'});
	if (defined($const)) {
		$self->{'expression'} = $const;
	}
	
	# Return an appropriate value (or lack thereof...).
	if ($self->{'error'}) {
		return undef;
	} else {
		return 1;
	}
}


# Evaluate.
# #########

=item evaluate

  $exp->evaluate(x => 0.5, a => 4);

This method takes a hash mapping any variables and named constants (represented
by letters) in the expression to numerical values, and attempts to evaluate the
expression and return a numerical value. It fails and returns undef if it finds
letters that have no mapping or an error such as division by zero occurs during
the evaluation.

=cut

sub evaluate {
	# Get invocant.
	my ($self, %mapping) = @_;
	
	# Clear error.
	$self->{'error'} = undef;
	
	# Evaluate.
	my $value = undef;
	eval {
		$value = $self->evaluateTree($self->{'expression'}, %mapping);
	} || ($self->{'error'} ||= $@);
	
	# Return value or undef if we there was an error.
	if ($self->{'error'}) {
		return undef;
	} else {
		return $value;
	}
}


# Same representation?
# ####################

=item sameRepresentation

  $same = $exp->sameRepresentation($exp2);

The sameRepresentation method takes another expression object as its parameter
and returns true if that expression has the same internal representation as the
expression the method is invoked on. Be careful - while it can be said that if
two expressions have the same representation they are equal, it would be wrong
to say that if they have different representations they are not equal. It is
clear to see that "x + 2" and "2 + x" are equal, but their internal representation
may well differ.

=cut

sub sameRepresentation {
	# Get invocant.
	my ($self, $exp2) = @_;
	
	# Clear error.
	$self->{'error'} = undef;
	
	# Compare and return result.
	return $self->isIdentical($self->{'expression'}, $exp2->getExpressionTree);
}


# Clone.
# ######

=item clone

  $expCopy = $exp->clone;

The clone method returns a deep copy of the expression object (deep copy meaning
that if the original is modified the copy will not be affected and vice versa).

=cut

sub clone {
	# Get invocant.
	my ($self) = @_;
	
	# Clear error.
	$self->{'error'} = undef;
	
	# Do a deep copy.
	my $tree = $self->deepCopy($self->{'expression'});
	
	# Create new object with copied tree and return.
	my $clone = {
		traceback	=> $self->{'traceback'},
		error		=> $self->{'error'},
		expression	=> $tree,
		variables	=> [ @{$self->{'variables'}} ]
	};
	return bless $clone, 'Math::Calculus::Expression';
}


# Get traceback.
# ##############

=item getTraceback

  $exp->getTraceback;

When setExpression and differentiate are called, a traceback is generated to describe
what these functions did. If an error occurs, this traceback can be extremely useful
in helping track down the source of the error.

=cut

sub getTraceback {
	return $_[0]->{'traceback'};
}


# Get error.
# ##########

=item getError

  $exp->getError;

When any method other than getTraceback is called, the error message stored is cleared, and
then any errors that occur during the execution of the method are stored. If failure occurs,
call this method to get a textual representation of the error.

=cut

sub getError {
	return $_[0]->{'error'};
}


# Any other methods.
# ##################

=item Other Methods

Any other method call is taken to refer to a subclass of Expression. The first letter of the
name of the method invoked is capitalized, then a module by that name is loaded (if it exists)
and the method is called on it. This works for, for example, the Differentiate module; calling
the differentiate method on an Expression will load the Differentiate module and call the
differentiate method. If a module cannot be loaded or the method cannot be called, then this
module will die.

=cut

sub AUTOLOAD {
	# Grab the params to pass on.
	my ($self, @params) = @_;
	
	# Get the name of the method called; skip if it is destroy.
	my $name = our $AUTOLOAD;
	return undef if $name =~ /::DESTROY$/;
	$name =~ s/^.+::([A-Za-z0-9]+)(_\w+)?$/$1$2/;
	my $modName = ucfirst $1;
	
	# Attempt to load the module and call the method.
	if (wantarray) {
		my @result = eval {
			require "Math/Calculus/$modName.pm";
			bless $self, "Math::Calculus::$modName";
			my $meth = eval('\&Math::Calculus::' . $modName . '::' . $name);
			$meth->($self, @params)
		};
		die $@ if $@;
		return @result;
	} else {
		my $result = eval {
			require "Math/Calculus/$modName.pm";
			bless $self, "Math::Calculus::$modName";
			my $meth = eval('\&Math::Calculus::' . $modName . '::' . $name);
			$meth->($self, @params)
		};
		die $@ if $@;
		return $result;
	}
}

=back

=head1 SEE ALSO

The author of this module has a website at L<http://www.jwcs.net/~jonathan/>, which has
the latest news about the module and a web-based frontend to allow you to try out this
module and, more specifically, its subclasses.

=head1 AUTHOR

Jonathan Worthington, E<lt>jonathan@jwcs.netE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2004 by Jonathan Worthington

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.1 or,
at your option, any later version of Perl 5 you may have available.

=cut


# ########################################################################################
# Private Methods
# ########################################################################################


# Get expression tree simply gets the raw expression tree.
# ########################################################################################
sub getExpressionTree { return $_[0]->{'expression'}; }


# Build tree recursively explores the passed expression and generates a tree for it.
# The trees take a structure of an operation (which is +, -, *, /, ^, sin, cos, tan,
# sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin, acos, atan, asinh,
# acosh, atanh, exp or ln) and two operands, which are either constants or references
# to other trees.
# ########################################################################################
sub buildTree {
	# Get invocant and expression.
	my ($self, $expr) = @_;
	
	# Store what we're parsing in the traceback.
	$self->{'traceback'} .= "Parsing $expr\n";
	
	# Clear any brackets around the entire expression.
	my $bracketsRemoved = 1;
	while ($bracketsRemoved && substr($expr, 0, 1) eq '(') {
		# See if there are any brackets to remove.
		my $bracketDepth = 0;
		my $bracketDepthHitZero = 0;
		my $count = 0;
		foreach my $char (split //, $expr) {
			if ($char eq '(') {
				$bracketDepth ++;
			} elsif ($char eq ')') {
				$bracketDepth --;
			}
			if ($bracketDepth == 0 && $count > 0 && $count + 1 < length($expr)) {
				$bracketDepthHitZero = 1;
			}
			$count++;
		}
		
		# If so, remove them.
		if ($bracketDepthHitZero == 0) {
			$expr =~ s/^\((.+)\)$/$1/;
		} else {
			$bracketsRemoved = 0;
		}
	}
	
	# If it's a constant or single variable...
	if ($expr =~ /^ (\-? ( (\d+(\.\d+)?) | [A-Za-z] )) $/x) {
		# No tree to build; just return the expression.
		return $1;
	
	# Otherwise it could be a function.
	} elsif ($expr =~ /^ (\-?) (a?sinh?|a?cosh?|a?tanh?|sech?|cosech?|coth?|ln|exp) \((.+)\) $/x &&
	         $self->isProperlyNested($3)) {
		# Return single operand parse tree.
		return {
			operation	=> "$1$2",
			operand1	=> $self->buildTree($3),
			operand2	=> undef
		};
	} else {
		# Otherwise full analysis needed. Analyse expressiona and try to find a split point.
		my $error = undef;
		my $bestSplitOp = '';
		my $splitOpPos = 0;
		my $bracketDepth = 0;
		
		# Cycle through all characters.
		my $curChar = 1;
		my $lastCharOp = 1;
		foreach my $char (split //, $expr) {
			# Maintain bracket depth.
			if ($char eq '(') {
				$bracketDepth ++;
			} elsif ($char eq ')') {
				$bracketDepth --;
			
			# Do we have a split point?
			} elsif ($curChar > 1 && $bracketDepth == 0 && $char =~ /[\^*\/+\-]/ &&
      	                    ($self->higherPrecedence($bestSplitOp, $char) || !$bestSplitOp)
			         && !$lastCharOp) {
				$splitOpPos = $curChar;
				$bestSplitOp = $char;
			}
			
			# If bracket depth is negative, we've got an error.
			if ($bracketDepth < 0)
			{
				$error = "Brackets not properly nested.";
			}
			
			# Maintain flag for if this character was an operator.
			$lastCharOp = $char =~ /[\^*\/+\-]/ ? 1 : 0;
			
			# Increment character counter.
			$curChar ++;
		}
		
		# Split failure error.	
		if (!$error && !$bestSplitOp) {
			$error = 'Could not split expression ' . $expr;
		}
		
		# If there wasn't an error, split, get operand and parse each subexpression.
		unless ($error) {
			my $operand1 = substr($expr, 0, $splitOpPos - 1);
			my $operand2 = substr($expr, $splitOpPos);
			if ($operand2 ne '') {
				return {
					operation	=> $bestSplitOp,
					operand1	=> $self->buildTree($operand1),
					operand2	=> $self->buildTree($operand2)
				};
			} else {
				$error = 'Could not split expression ' . $expr;
			}
		}
		
		# If we've got an error, store it and return failure.
		if ($error) {
			$self->{'error'} = $error;
			return undef;
		}
 	}
	
	# If we get here, something weird happened.
	$self->{'error'} = "Unknown error parsing $expr.";
	return undef;
}


# Pretty print takes an expression tree and returns a text representation for it.
# #######################################################################################
sub prettyPrint {
	# Get invocant and tree.
	my ($self, $tree, $lastOp) = @_;
	
	# See if the tree actually is a tree. If not, it's a value and just return it.
	unless (ref $tree) {
		return $tree;
	} else {
		# See how many operands we take.
		my $curOp = $tree->{'operation'};
		if ($curOp =~ /^[\^\/*\-+]$/) {
			# Dual operand. Look at last op to see if we need brackets.
			my $brackets = ($curOp eq '^' && $lastOp =~ /[\/*+\-]/ ||
			                $curOp =~ /[\/*]/ && $lastOp =~ /[*+\-]/ ||
			                $curOp =~ /[+\-]/ && $lastOp =~ /[+\-]/ ||
                                  !(defined($lastOp)) || $lastOp eq '(')
					   ? 0 : 1;
			
			# Pretty-print each operand, adding spaces around + and - ops.
			my $pretty = '';
			$pretty .= '(' if $brackets;
			$pretty .= $self->prettyPrint($tree->{'operand1'}, $curOp);
			$pretty .= ($curOp =~ /[+\-]/ ? ' ' : '') . $curOp . ($curOp =~ /[+\-]/ ? ' ' : '');
			$pretty .= $self->prettyPrint($tree->{'operand2'}, $curOp);
			$pretty .= ')' if $brackets;
			return $pretty;
		} else {
			# Single operand, e.g. function.
			return $curOp . '(' . $self->prettyPrint($tree->{'operand1'}, '(') . ')';
		}
	}
}


# recSimplify recursively walks a tree and simplifies the branches, then the current
# node.
# ########################################################################################
sub recSimplify {
	# Get invocant, variable and tree.
	my ($self, $tree) = @_;
	
	# If it's just a node, return it. We can't do a great deal with nodes.
	return $tree unless ref $tree;
	
	# Pull out left and right branches for neatness.
	my ($left, $right) = ($tree->{'operand1'}, $tree->{'operand2'});
	
	## RECURSIVELY SIMPLIFTY TREES
	$left = $self->recSimplify($left);
	$right = $self->recSimplify($right);
	
	## CONSTANT EVALUATION
	
	# Get any available numeric evaluations of the left and right branches.
	my $leftval = $self->numericEvaluation($left);
	my $rightval = $self->numericEvaluation($right);
	
	# If they have a numeric evaluation, assign them to the actual values.
	$left = $leftval if defined($leftval);
	$right = $rightval if defined($rightval);
	
	## SHIFTING NEGATIVES
	## These simplifications are not "the final word", indeed dealing with them
	## allows further simplifications to take place. So we modify the tree "in
	## place".
	
	# x - (-y) = x + y
	if ($tree->{'operation'} eq '-') {
		if (!(ref $right) && $right =~ /^-(.+)$/) {
			$tree->{'operation'} = '+';
			$right = $1;
		} elsif (ref $right && $right->{'operation'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '+';
			$right->{'operation'} = $1;
		}
	}
	
	# x + (-y) = x - y
	elsif ($tree->{'operation'} eq '+') {
		if (!(ref $right) && $right =~ /^-(.+)$/) {
			$tree->{'operation'} = '-';
			$right = $1;
		} elsif (ref $right && $right->{'operation'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '-';
			$right->{'operation'} = $1;
		}
	}
	
	# x - -y*z = x + y*z
	if ($tree->{'operation'} eq '-' && ref $right && $right->{'operation'} eq '*') {
		if (!(ref $right->{'operand1'}) && $right->{'operand1'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '+';
			$right->{'operand1'} = $1;
		} elsif (ref($right->{'operand1'}) && $right->{'operand1'}->{'operation'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '+';
			$right->{'operand1'}->{'operation'} = $1;
		}
	}
	
	# x + -y*z = x - y*z
	elsif ($tree->{'operation'} eq '+' && ref $right && $right->{'operation'} eq '*') {
		if (!(ref $right->{'operand1'}) && $right->{'operand1'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '-';
			$right->{'operand1'} = $1;
		} elsif (ref $right->{'operand1'} && $right->{'operand1'}->{'operation'} =~ /^-(.+)$/) {
			$tree->{'operation'} = '-';
			$right->{'operand1'}->{'operation'} = $1;
		}
	}
	
	## MIGRATE CONSTANTS UP THE TREE
	
	# x * c = c * x
	if ($tree->{'operation'} eq '*' && !ref($right) && $right =~ /^-?\d+(\.\d+)?$/) {
		($left, $right) = ($right, $left);
	
	# x * c * y = c * x * y
	} elsif ($tree->{'operation'} eq '*' && ref $right && $right->{'operation'} eq '*' &&
	    $right->{'operand1'} =~ /^-?\d+(\.\d+)?$/) {
		($left, $right->{'operand1'}) = ($right->{'operand1'}, $left);
	
	# x * y * c = x * c * y
	} elsif ($tree->{'operation'} eq '*' && ref $right && $right->{'operation'} eq '*' &&
	    $right->{'operand2'} =~ /^-?\d+(\.\d+)?$/) {
		($right->{'operand1'}, $right->{'operand2'}) = ($right->{'operand2'}, $right->{'operand1'});
	}
	
	## NULL OPERATORS
	
	# 0 + x = x + 0 = x
	if ($tree->{'operation'} eq '+' && (!(ref $left) && $left eq '0')) {
		return $right;
	}
	if ($tree->{'operation'} eq '+' && (!(ref $right) && $right eq '0')) {
		return $left;
	}
	
	# x - 0 = x
	if ($tree->{'operation'} eq '-' && (!(ref $right) && $right eq '0')) {
		return $left;
	}
	
	# x - 0 + y = x - y
	# x + 0 + y = x + y
	if ($tree->{'operation'} =~ /^[+-]$/ && ref $right && $right->{'operation'} =~ /^[+-]$/ &&
	    !(ref $right->{'operand1'}) && $right->{'operand1'} eq '0') {
		$right = $right->{'operand2'};
	}
	
	# 1 * x = x * 1 = x
	if ($tree->{'operation'} eq '*' && (!(ref $left) && $left eq '1')) {
		return $right;
	}
	if ($tree->{'operation'} eq '*' && (!(ref $right) && $right eq '1')) {
		return $left;
	}
	
	# x / 1 = x
	if ($tree->{'operation'} eq '/' && (!(ref $right) && $right eq '1')) {
		return $left;
	}
	
	# x ^ 1 = x
	if ($tree->{'operation'} eq '^' && (!(ref $right) && $right eq '1')) {
		return $left;
	}
	
	## EFFECTS OF ZERO
	
	# x ^ 0 = 1
	if ($tree->{'operation'} eq '^' && (!(ref $right) && $right eq '0')) {
		return 1;
	}
	
	# 0 * x = x * 0 = 0
	if ($tree->{'operation'} eq '*' && (!(ref $left) && $left eq '0')) {
		return 0;
	}
	if ($tree->{'operation'} eq '*' && (!(ref $right) && $right eq '0')) {
		return 0;
	}
	
	# 0 / x = 0
	if ($tree->{'operation'} eq '/' && (!(ref $left) && $left eq '0')) {
		return 0;
	}
	
	## DIVISION OF AN EXPRESSION BY ITSELF
	
	# x / x = 1
	if ($tree->{'operation'} eq '/' && $self->isIdentical($left, $right)) {
		return 1;
	}
	
	## SUBTRACTION OF AN EXPRESSION FROM ITSELF
	
	# x - x = 0
	if ($tree->{'operation'} eq '-' && $self->isIdentical($left, $right)) {
		return 0;
	}
	
	## DEEP NUMERICAL CONSTANT COMBINATION
	
	# n * (m * x) = (o * x) where o = nm
	if ($tree->{'operation'} eq '*' && ref($right) && $right->{'operation'} eq '*' &&
	    !(ref($left)) && $left =~ /^-?\d+(\.\d+)?$/ && $right->{'operand1'} =~ /^-?\d+(\.\d+)?$/) {
		return {
			operation	=> '*',
			operand1	=> ($left * $right->{'operand1'}),
			operand2	=> $right->{'operand2'}
		};
	
	# n * (x * m) = (o * x) where o = nm
	} elsif ($tree->{'operation'} eq '*' && ref($right) && $right->{'operation'} eq '*' &&
	    !(ref($left)) && $left =~ /^-?\d+(\.\d+)?$/ && $right->{'operand2'} =~ /^-?\d+(\.\d+)?$/) {
		return {
			operation	=> '*',
			operand1	=> ($left * $right->{'operand2'}),
			operand2	=> $right->{'operand1'}
		};
	
	# (m * x) * n = (o * x) where o = nm
	} elsif ($tree->{'operation'} eq '*' && ref($left) && $left->{'operation'} eq '*' &&
	    !(ref($right)) && $right =~ /^-?\d+(\.\d+)?$/ && $left->{'operand1'} =~ /^-?\d+(\.\d+)?$/) {
		return {
			operation	=> '*',
			operand1	=> ($right * $left->{'operand1'}),
			operand2	=> $right->{'operand2'}
		};
	
	# (x * m) * n = (o * x) where o = nm
	} elsif ($tree->{'operation'} eq '*' && ref($left) && $left->{'operation'} eq '*' &&
	    !(ref($right)) && $right =~ /^-?\d+(\.\d+)?$/ && $left->{'operand2'} =~ /^-?\d+(\.\d+)?$/) {
		return {
			operation	=> '*',
			operand1	=> ($right * $left->{'operand2'}),
			operand2	=> $right->{'operand1'}
		};
	}
	
	## NATURAL LOGARITHM AND EXPONENTIATION INVERSTION
	
	# exp(ln(f(x))) = f(x)
	if ($tree->{'operation'} =~ /^-?exp$/ && ref($left) && $left->{'operation'} =~ /^ln$/) {
		if ($tree->{'operation'} =~ /^-/) {
			return {
				operation	=> '*',
				operand1	=> "-1",
				operand2	=> $left->{'operand1'}
			};
		} else {
			return $left->{'operand1'};
		}
	}
	
	# ln(exp(f(x))) = f(x)
	if ($tree->{'operation'} =~ /^-?ln$/ && ref($left) && $left->{'operation'} =~ /^exp$/) {
		if ($tree->{'operation'} =~ /^-/) {
			return {
				operation	=> '*',
				operand1	=> "-1",
				operand2	=> $left->{'operand1'}
			};
		} else {
			return $left->{'operand1'};
		}
	}
	
	## MULTIPLICATION CHAINS BECOME POWERS
	
	# e * e = e^2
	if ($tree->{'operation'} eq '*' && $self->isIdentical($left, $right)) {
		return {
			operation	=> '^',
			operand1	=> $left,
			operand2	=> 2
		};
	}
	
	# -e * e = -(e^2)
	elsif ($tree->{'operation'} eq '*') {
		# Check if left is negative.
		if (ref $left && $left->{'operation'} =~ /^-(.+)$/) {
			$left->{'operation'} = $1;
			if ($self->isIdentical($left, $right)) {
				return {
					operation	=> '-',
					operand1	=> 0,
					operand2	=> {
						operation	=> '^',
						operand1	=> $left,
						operand2	=> 2
					}
				};
			} else {
				$left->{'operation'} = "-$1";
			}	
		} elsif (!(ref $left) && $left =~ /^-(.+)$/) {
			$left = $1;
			if ($self->isIdentical($left, $right)) {
				return {
					operation	=> '-',
					operand1	=> 0,
					operand2	=> {
						operation	=> '^',
						operand1	=> $left,
						operand2	=> 2
					}
				};
			} else {
				$left = "-$1";
			}
		}
	}
	
	## TRIG IDENTITIES
	
	# cos^2 - sin^2 = 1
	if ($tree->{'operation'} eq '-' && ref $left && ref $right &&
	    $left->{'operation'} eq '^' && $right->{'operation'} eq '^' &&
	    (!ref $left->{'operand2'}) && $left->{'operand2'} == 2 &&
	    (!ref $right->{'operand2'}) && $right->{'operand2'} == 2 &&
	    ref $left->{'operand1'} && $left->{'operand1'}->{'operation'} =~ /-?cos$/ &&
	    ref $right->{'operand1'} && $right->{'operand1'}->{'operation'} =~ /-?sin$/ &&
	    $self->isIdentical($left->{'operand1'}->{'operand1'}, $right->{'operand1'}->{'operand1'})) {
		return 1;
	}
	
	## NO SIMPLIFICATION POSSIBLE - BUILD NEW TREE OF SIMPLIFIED SUBTREES
	
	# If we get here, just build and return a new tree, which may have no changes.
	return {
		operation	=> $tree->{'operation'},
		operand1	=> $left,
		operand2	=> $right
	};
}


# Evaluate tree simply subs a list of values in to numerically evaluate the tree.
# ########################################################################################
sub evaluateTree {
	# Get invocant, tree and mappings.
	my $self = shift;
	my $tree = shift;
	my %mapping = @_;
	
	# If we've got a numerical constant, just return it.
	if (!ref($tree) && $tree =~ /^-?\d+(\.\d+)?$/) {
		return $tree;
	
	# If we've got an atom, look it up in the mapping; die if we fail.
	} elsif (!ref($tree)) {
		my $val = $mapping{$tree};
		if (defined($val)) {
			return $val;
		} else {
			die; "No mapping for $tree";
		}
	
	#  +
	} elsif ($tree->{'operation'} eq '+') {
		return $self->evaluateTree($tree->{'operand1'}, %mapping) + $self->evaluateTree($tree->{'operand2'}, %mapping);
	
	#  -
	} elsif ($tree->{'operation'} eq '-') {
		return $self->evaluateTree($tree->{'operand1'}, %mapping) - $self->evaluateTree($tree->{'operand2'}, %mapping);
	
	#  *
	} elsif ($tree->{'operation'} eq '*') {
		return $self->evaluateTree($tree->{'operand1'}, %mapping) * $self->evaluateTree($tree->{'operand2'}, %mapping);
	
	#  /
	} elsif ($tree->{'operation'} eq '/') {
		return $self->evaluateTree($tree->{'operand1'}, %mapping) / $self->evaluateTree($tree->{'operand2'}, %mapping);
	
	#  ^
	} elsif ($tree->{'operation'} eq '^') {
		return $self->evaluateTree($tree->{'operand1'}, %mapping) ** $self->evaluateTree($tree->{'operand2'}, %mapping);
	
	# ln
	} elsif ($tree->{'operation'} =~ /^(-?)ln$/) {
		return ($1 ? -1 : 1) * log($self->evaluateTree($tree->{'operand1'}, %mapping));

	# exp
	} elsif ($tree->{'operation'} =~ /^(-?)exp$/) {
		return ($1 ? -1 : 1) * exp($self->evaluateTree($tree->{'operand1'}, %mapping));		
	
	# sin
	} elsif ($tree->{'operation'} =~ /^(-?)sin$/) {
		return ($1 ? -1 : 1) * sin($self->evaluateTree($tree->{'operand1'}, %mapping));		
	
	# cos
	} elsif ($tree->{'operation'} =~ /^(-?)cos$/) {
		return ($1 ? -1 : 1) * cos($self->evaluateTree($tree->{'operand1'}, %mapping));		
		
	# tan
	} elsif ($tree->{'operation'} =~ /^(-?)tan$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (sin($val) / cos($val));
	
	# sec
	} elsif ($tree->{'operation'} =~ /^(-?)sec$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (1 / cos($val));

	# cosec
	} elsif ($tree->{'operation'} =~ /^(-?)cosec$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (1 / sin($val));
	
	# cot
	} elsif ($tree->{'operation'} =~ /^(-?)cot$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (cos($val) / sin($val));
	
	# asin
	} elsif ($tree->{'operation'} =~ /^(-?)asin$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * atan2($val, sqrt(1 - $val * $val));

	# acos
	} elsif ($tree->{'operation'} =~ /^(-?)acos$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * atan2(sqrt(1 - $val * $val), $val);
	
	# atan
	} elsif ($tree->{'operation'} =~ /^(-?)atan$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * atan2($val, 1);
	
	# sinh
	} elsif ($tree->{'operation'} =~ /^(-?)sinh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * ((exp($val) - exp(-$val)) / 2);
	
	# cosh
	} elsif ($tree->{'operation'} =~ /^(-?)cosh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * ((exp($val) + exp(-$val)) / 2);
	
	# tanh
	} elsif ($tree->{'operation'} =~ /^(-?)tanh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * ((exp($val) - exp(-$val)) / (exp($val) + exp(-$val)));
	
	# sech
	} elsif ($tree->{'operation'} =~ /^(-?)sech$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (2 / (exp($val) + exp(-$val)));

	# cosech
	} elsif ($tree->{'operation'} =~ /^(-?)cosech$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * (2 / (exp($val) - exp(-$val)));
	
	# coth
	} elsif ($tree->{'operation'} =~ /^(-?)coth$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * ((exp($val) + exp(-$val)) / (exp($val) - exp(-$val)));
	
	# asinh
	} elsif ($tree->{'operation'} =~ /^(-?)asinh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * log($val + sqrt($val * $val + 1));

	# acosh
	} elsif ($tree->{'operation'} =~ /^(-?)acosh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -1 : 1) * log($val + sqrt(($val * $val >= 1 ? $val * $val : -($val * $val)) - 1));
	
	# atanh
	} elsif ($tree->{'operation'} =~ /^(-?)atanh$/) {
		my $val = $self->evaluateTree($tree->{'operand1'}, %mapping);
		return ($1 ? -0.5 : 0.5) * (log(1 + $val) + log(1 - $val));
	
	# Otherwise, fail.
	} else {
		die "Cannot evaluate $tree->{'operation'}";
	}
}
	


# higherPrecedence(a, b) returns true if a has higher or equal precedence than b.
# ########################################################################################
sub higherPrecedence {
	# Get invocant and parameters.
	my ($self, $a, $b) = @_;
	
	# Do precedence check.
	if ($a eq '^') {
		return 1;
	} elsif ($a eq '/' && $b =~ /\/|\*|\+|-/) {
		return 1;
	} elsif ($a eq '*' && $b =~ /\*|\+|-/) {
		return 1;
	} elsif ($a eq '+' && $b =~ /\+|-/) {
		return 1;
	} elsif ($a eq '-' && $b eq '-') {
		return 1;
	}
		
	# If we get here, precedence is lower.
	return 0;
}


# isConstant takes a tree and a variable, checks if it's dependent on that variable and
# returns 1 if so and 0 if not.
# ########################################################################################
sub isConstant {
	# Get invocant, variable and tree.
	my ($self, $variable, $tree) = @_;
	
	# If the tree is undefined, we've run off the end of it, which means it was all constant.
	return 1 unless defined($tree);
	
	# If we have a ref...
	if (ref $tree) {
		return ($self->isConstant($variable, $tree->{'operand1'}) && $self->isConstant($variable, $tree->{'operand2'}));
	} else {
		# Atom. But is it the variable?
		return $tree eq $variable || $tree eq "-$variable" ? 0 : 1;
	}
}


# Numeric Evaluation takes a tree and, provided it is constant and all constants are
# numeric, calculates the value of the tree. Returns undef if numeric evaluation is
# not possible.
# ########################################################################################
sub numericEvaluation {
	# Get invocant and tree.
	my ($self, $tree) = @_;
	
	# If the tree is a value...
	unless (ref $tree) {
		# If it's numeric, return it.
		return $tree =~ /^-?\d+(\.\d+)?$/ ? $tree : undef;
	} else {
		# Attempt to numerically evaluate each branch.
		my $leftval = $self->numericEvaluation($tree->{'operand1'});
		my $rightval = $self->numericEvaluation($tree->{'operand2'});
		
		# If it's an addition op and both values are numeric...
		if ($tree->{'operation'} eq '+' && defined($leftval) && defined($rightval)) {
			# Add and return.
			return $leftval + $rightval;
		
		# If it's a subtraction op and both values are numeric...
		} elsif ($tree->{'operation'} eq '-' && defined($leftval) && defined($rightval)) {
			# Subtract and return.
			return $leftval - $rightval;

		# If it's a multiplication op and both values are numeric...
		} elsif ($tree->{'operation'} eq '*' && defined($leftval) && defined($rightval)) {
			# Multiply and return.
			return $leftval * $rightval;
		
		# If it's a power op and both values are numeric...
		} elsif ($tree->{'operation'} eq '^' && defined($leftval) && defined($rightval)) {
			# Multiply and return.
			return $leftval ^ $rightval;
		 
		# Otherwise, we can't do numerical operations. Return undef.
		} else {
			return undef;
		}
	}
}


# isIdentical takes two trees and checks if they are identical. Note that identical might
# not mean equal.
# ########################################################################################
sub isIdentical {
	# Get invocant and trees.
	my ($self, $treeA, $treeB) = @_;
	
	# If both are not references and they are the same...
	if (!ref($treeA) && !ref($treeB) && $treeA eq $treeB) {
		return 1;
	
	# If they are both references and have the same operators...
	} elsif (ref($treeA) && ref($treeB) && $treeA->{'operation'} eq $treeB->{'operation'}) {
		# Recursively compare the subtrees.
		my $leftcomp = $self->isIdentical($treeA->{'operand1'}, $treeB->{'operand1'});
		my $rightcomp = $self->isIdentical($treeA->{'operand2'}, $treeB->{'operand2'});
		return $leftcomp && $rightcomp ? 1 : 0;
	
	# Otherwise, they must not be the same.
	} else {
		return 0;
	}
}


# deepCopy creates a deep copy of an expression tree. You'd never have guessed, huh?
# ########################################################################################
sub deepCopy {
	# Get invocant and what is being copied.
	my ($self, $tree) = @_;
	
	# If it's a reference...
	if (ref $tree) {
		# Copy.
		return {
			operation	=> $tree->{'operation'},
			operand1	=> $self->deepCopy($tree->{'operand1'}),
			operand2	=> $self->deepCopy($tree->{'operand2'}),
		};
	} else {
		# Just a value. Return.
		return $tree;
	}
}


# isProperlyNested checks if the brackets in an expression are properly nested.
# ########################################################################################
sub isProperlyNested {
	# Get invocant and string to check.
	my ($self, $check) = @_;
	
	# Do the check.
	my $valid = 1;
	my $bracketDepth = 0;
	for (split(//, $check)) {
		$bracketDepth++ if /\(/;
		$bracketDepth-- if /\)/;
		return 0 if $bracketDepth < 0;
	}
	return $bracketDepth == 0 ? 1 : 0;
}


1;