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
|
#!perl
use strict;
use warnings;
use Test::More tests => 34;
BEGIN {
use_ok('Math::Symbolic');
}
if ($ENV{TEST_YAPP_PARSER}) {
require Math::Symbolic::Parser::Yapp;
$Math::Symbolic::Parser = Math::Symbolic::Parser::Yapp->new();
}
use Math::Symbolic::ExportConstants qw/:all/;
my $var = Math::Symbolic::Variable->new();
my $a = $var->new( 'x' => 10 );
print "Vars: x=" . $a->value() . " (Value is optional)\n\n";
print "Expression: x * 2 + 1, x / 2 - 1, x * (2+1)\n\n";
my ( $first, $second, $third );
$@ = undef;
eval <<'HERE';
$first = $a * 2 + 1; # x*2 + 1
HERE
ok( !$@, 'overloaded multiplication and addition' );
my $str = $first->to_string();
$str =~ s/\s+//g;
ok(
$str eq '(x*2)+1' || $str eq '1+(2*x)',
'Correct result of overloaded *,+',
);
ok( $first->value() == 21, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$second = $a / 2 - 1; # x*2 + 1
HERE
ok( !$@, 'overloaded division and subtraction' );
$str = $second->to_string();
$str =~ s/\s+//g;
ok( $str eq '(x/2)-1', 'Correct result of overloaded /,-', );
ok( $second->value() == 4, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$third = $a * "2 + 1"; # x*3
HERE
ok( !$@, 'overloaded multiplication involving auto-parsing' );
$str = $third->to_string();
$str =~ s/\s+//g;
ok(
$str eq 'x*(1+2)'
|| $str eq 'x*(2+1)'
|| $str eq '(2+1)*x'
|| $str eq '(1+2)*x',
'Correct result of overloaded * involving auto-parsing',
);
ok( $third->value() == 30, 'Result evaluates to the correct number' );
my $fourth;
$@ = undef;
eval <<'HERE';
$fourth = 2 ** ($third/$a);
HERE
ok( !$@, 'overloaded ** w/ constant recognition and M::S::Operators' );
ok( $fourth->value() == 2**3, 'Result evaluates to the correct number' );
my $fifth;
$@ = undef;
eval <<'HERE';
$fifth = $fourth ** $fourth;
HERE
ok( !$@, 'overloaded ** w/ two M::S::Operators' );
ok( $fifth->value() == 8**8, 'Result evaluates to the correct number' );
my $sixth;
$@ = undef;
eval <<'HERE';
$sixth = sqrt($third*$third);
HERE
ok( !$@, 'overloaded sqrt, * w/ M::S::Operators' );
ok( $sixth->value() == 30, 'Result evaluates to the correct number' );
my $seventh;
$@ = undef;
eval <<'HERE';
$seventh = -exp(Math::Symbolic::Constant->zero());
HERE
ok( !$@, 'overloaded unary minus, exp w/ M::S::Constant' );
ok( $seventh->value() == -1, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh = log(Math::Symbolic::Constant->one());
HERE
ok( !$@, 'overloaded log w/ M::S::Constant' );
ok( $seventh->value() == 0, 'Result evaluates to the correct number' );
ok( ( $seventh ? 0 : 1 ), 'automatic boolean conversion (Test1)' );
ok( ( $second ? 1 : 0 ), 'automatic boolean conversion (Test2)' );
$@ = undef;
eval <<'HERE';
$seventh = cos(sin(Math::Symbolic::Constant->zero()));
HERE
ok( !$@, 'overloaded sin, cos w/ M::S::Constant' );
ok( $seventh->value() == 1, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh += 2;
HERE
ok( !$@, 'overloaded += w/ M::S::Constant' );
ok( $seventh->value() == 3, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh -= 2;
HERE
ok( !$@, 'overloaded -= w/ M::S::Constant' );
ok( $seventh->value() == 1, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh *= 2;
HERE
ok( !$@, 'overloaded *= w/ M::S::Constant' );
ok( $seventh->value() == 2, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh /= 2;
HERE
ok( !$@, 'overloaded /= w/ M::S::Constant' );
ok( $seventh->value() == 1, 'Result evaluates to the correct number' );
$@ = undef;
eval <<'HERE';
$seventh += 2;
$seventh **= 2;
HERE
ok( !$@, 'overloaded **= w/ M::S::Constant' );
ok( $seventh->value() == 9, 'Result evaluates to the correct number' );
print "prefix notation and evaluation:\n";
print $first->to_string('prefix') . " = " . $first->value() . "\n\n";
print $second->to_string('prefix') . " = " . $second->value() . "\n\n";
print "Now, we derive this partially to x: (prefix again)\n";
my $n_tree = Math::Symbolic::Operator->new(
{
type => U_P_DERIVATIVE,
operands => [ $first, $a ],
}
);
my $n_tree2 = Math::Symbolic::Operator->new(
{
type => U_P_DERIVATIVE,
operands => [ $second, $a ],
}
);
my $n_tree3 = Math::Symbolic::Operator->new(
{
type => U_P_DERIVATIVE,
operands => [ $third, $a ],
}
);
print $n_tree->to_string('prefix') . " = " . $n_tree->value() . "\n\n";
print $n_tree2->to_string('prefix') . " = " . $n_tree2->value() . "\n\n";
print $n_tree3->to_string('prefix') . " = " . $n_tree3->value() . "\n\n";
|