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
|
#!perl
use strict;
use warnings;
use Test::More tests => 48;
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 qw/:all/;
use Math::Symbolic::ExportConstants qw/:all/;
my $x = Math::Symbolic::parse_from_string('1');
ok( $x->is_constant(), 'is_constant true for constants' );
$x = Math::Symbolic::parse_from_string('a');
ok( !$x->is_constant(), 'is_constant false for vars' );
$x = Math::Symbolic::parse_from_string('1+1/5*log(2,3)^5');
ok( $x->is_constant(), 'is_constant true for constant expressions' );
$x = Math::Symbolic::parse_from_string('1+1/5*log(2,a)^5');
ok( !$x->is_constant(), 'is_constant false for non-constant expressions' );
$x =
Math::Symbolic::parse_from_string('partial_derivative(1+1/5*log(2,2)^5-a,a)');
ok( $x->is_constant(),
'is_constant true for expressions that become constant after del/delx' );
$x =
Math::Symbolic::parse_from_string('total_derivative(1+1/5*log(2,2)^5-a,a)');
ok( $x->is_constant(),
'is_constant true for expressions that become constant after d/dx' );
$x =
Math::Symbolic::parse_from_string(
'total_derivative(b(a)+1/5*log(2,2)^5-a,a)');
ok( !$x->is_constant(),
'is_constant true for expressions that become constant after d/dx' );
$x = Math::Symbolic::parse_from_string('a');
ok( !$x->is_integer(), 'is_integer false for vars' );
$x = Math::Symbolic::parse_from_string('1.5');
ok( !$x->is_integer(), 'is_integer false for fractions' );
$x = Math::Symbolic::parse_from_string('2000');
ok( $x->is_integer(), 'is_integer true for integers' );
$x = Math::Symbolic::parse_from_string('0');
ok( $x->is_integer(), 'is_integer true for zero' );
$x = Math::Symbolic::parse_from_string('2000*2000');
ok( !$x->is_integer(), 'is_integer false for operators' );
$x = Math::Symbolic::parse_from_string('1');
ok( $x->is_sum(), 'is_sum true for constant' );
$x = Math::Symbolic::parse_from_string('1+2');
ok( $x->is_sum(), 'is_sum true for constant sum' );
$x = Math::Symbolic::parse_from_string('1*a');
ok( $x->is_sum(), 'is_sum true for constant times variable' );
$x = Math::Symbolic::parse_from_string('1*a');
ok( $x->is_sum(), 'is_sum true for integer constant times variable' );
$x = Math::Symbolic::parse_from_string('1.5*a');
ok( !$x->is_sum(), 'is_sum false for non-integer constant times variable' );
$x = Math::Symbolic::parse_from_string('1*a+(-b)-3*sin(2)');
ok( $x->is_sum(), 'is_sum true for sum of variables and constant terms' );
$x =
Math::Symbolic::parse_from_string(
'partial_derivative(10*a^2+(-1/b)-3*sin(2),a)');
ok( $x->is_sum(), 'is_sum true for del/delx that evaluates to a sum' );
my $y =
Math::Symbolic::parse_from_string(
'partial_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
$x =
Math::Symbolic::parse_from_string(
'partial_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
ok( $x->is_identical($y), 'is_identical true involved term' );
$y =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(c(d,f,g,i,a)-2),a)');
$x =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(c(d,f,g,i,a)-2),a)');
ok( $x->is_identical($y), 'is_identical true involved term' );
$y =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(a))-3*sin(2),a)');
$x =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
ok( !$x->is_identical($y),
'is_identical false involved term differing in signature' );
$y =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-2/b(x))-3*sin(2),a)');
$x =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
ok( !$x->is_identical($y),
'is_identical false involved term differing in constant' );
$y =
Math::Symbolic::parse_from_string(
'total_derivative(10*x^2+(-1/b(x))-3*sin(2),a)');
$x =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
ok( !$x->is_identical($y),
'is_identical false involved term differing in variable' );
$y =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1*b(x))-3*sin(2),a)');
$x =
Math::Symbolic::parse_from_string(
'total_derivative(10*a^2+(-1/b(x))-3*sin(2),a)');
ok( !$x->is_identical($y),
'is_identical false involved term differing in operator' );
ok(
(
$x->can('descend')
and defined( ref( $x->can('descend') ) )
and ref( $x->can('descend') ) eq 'CODE'
),
'can() returns code ref for builtin method.'
);
ok(
(
$x->can('is_constant')
and defined( ref( $x->can('is_constant') ) )
and ref( $x->can('is_constant') ) eq 'CODE'
),
'can() returns code ref for delegated method.'
);
ok( !$x->can('bdasjkhdsajhdsakasjlh'),
'can() returns false for non-existant builtin method.' );
ok( !$x->can('is_ashdgsajhgdasjhg'),
'can() returns false for non-existant delegated method.' );
ok( parse_from_string('x*y')->is_identical_base('x*y'),
'is_identical_base trivial' );
ok( parse_from_string('(x*y)^2')->is_identical_base('x*y'),
'is_identical_base simple' );
ok( parse_from_string('(x*y)^(a*b)')->is_identical_base('x*y'),
'more is_identical_base tests' );
ok( parse_from_string('(x*y)^(a*b)')->is_identical_base('(x*y)^3'),
'more is_identical_base tests' );
ok( parse_from_string('(x*y^3)')->is_identical_base('(x*y^3)^3'),
'more is_identical_base tests' );
ok( not( parse_from_string('(y)^(a*b)')->is_identical_base('(x*y)^3') ),
'more is_identical_base tests' );
ok( parse_from_string('1')->is_one(), '1 is_one' );
ok( !parse_from_string('0')->is_one(), '!0 is_one' );
ok( !parse_from_string('4-3')->is_one(), '!4-3 is_one' );
ok( !parse_from_string('a')->is_one(), '!a is_one' );
ok( !parse_from_string('1')->is_zero(), '!1 is_zero' );
ok( parse_from_string('0')->is_zero(), '!0 is_zero' );
ok( !parse_from_string('4-4')->is_zero(), '!4-4 is_zero' );
ok( !parse_from_string('a')->is_zero(), '!a is_zero' );
ok( parse_from_string('1')->is_zero_or_one(), '1 is_zero_or_one' );
ok( parse_from_string('0')->is_zero_or_one(), '0 is_zero_or_one' );
ok( !parse_from_string('4-4')->is_zero_or_one(), '!4-4 is_zero_or_one' );
ok( !parse_from_string('a')->is_zero_or_one(), '!a is_zero_or_one' );
|