File: 01basic.t

package info (click to toggle)
libmath-symbolic-perl 0.612-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,260 kB
  • sloc: perl: 12,634; makefile: 2
file content (146 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (6)
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
#!perl
use strict;
use warnings;

use Test::More tests => 32;

BEGIN {
	use_ok('Math::Symbolic');
	use_ok('Math::Symbolic::VectorCalculus');
}

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();
ok( ref($var) eq 'Math::Symbolic::Variable', 'Variable prototype' );

my $a = $var->new( 'a' => 2 );
ok(
    ref($a) eq 'Math::Symbolic::Variable'
      && $a->value() == 2
      && $a->name() eq 'a',
    'Variable creation, value(), and name()'
);

my $b = $var->new( 'b' => 3 );
my $c = $var->new( 'c' => 4 );

print "Vars: a="
  . $a->value() . " b="
  . $b->value() . " c="
  . $c->value()
  . " (Values are optional)\n\n";

my $op = Math::Symbolic::Operator->new();
ok( ref($op) eq 'Math::Symbolic::Operator', 'Operator prototype' );

my $add1 = $op->new( '+', $a, $c );
ok( ref($add1) eq 'Math::Symbolic::Operator' && $add1->type() == B_SUM,
    'Operator creation, type()' );

my $mult1 = $op->new( '*', $a,    $b );
my $div1  = $op->new( '/', $add1, $mult1 );

print "Expression: (a+c)/(a*b)\n\n";

print "prefix notation and evaluation:\n";

eval <<'HERE';
print $div1->to_string('prefix') . " = " . $div1->value() . "\n\n";
HERE
ok( !$@, 'to_string("prefix") did not complain' );

print "Now, we derive this partially to a: (prefix again)\n";

my $n_tree;
eval <<'HERE';
$n_tree = $op->new( {
	type => U_P_DERIVATIVE,
	operands => [$div1, $a],
} );
HERE
ok( !$@, 'long-form partial derivative did not complain' );
ok(
    ref($n_tree) eq 'Math::Symbolic::Operator'
      && $n_tree->type() == U_P_DERIVATIVE,
    ,
    'long-form partial derivative returned derivative'
);

print $n_tree->to_string('prefix') . " = " . $n_tree->value() . "\n\n";

print "Now, we apply the derivative to the term: (infix)\n";

$@ = undef;
my $derived;
eval <<'HERE';
$derived = $n_tree->apply_derivatives();
HERE
ok( !$@, 'apply_derivatives() did not complain' );

print "$derived = " . $derived->value() . "\n\n";

print "Finally, we simplify the derived term as much as possible:\n";

$@ = undef;
my $simplified;
eval <<'HERE';
$simplified = $derived->simplify();
HERE
ok( !$@&&defined($simplified), 'simplify() did not complain' );

print "$simplified = " . $derived->value() . "\n\n";

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 0, 0 ) == 1,
    'binomial_coeff(0, 0)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 1, 1 ) == 1,
    'binomial_coeff(1, 1)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 4, 2 ) == 6,
    'binomial_coeff(4, 2)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 5, 2 ) == 10,
    'binomial_coeff(5, 2)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 5, 4 ) == 5,
    'binomial_coeff(5, 4)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 2, 4 ) == 0,
    'binomial_coeff(2, 4)' );

ok( Math::Symbolic::AuxFunctions::binomial_coeff( 2, -1 ) == 0,
    'binomial_coeff(2, -1)' );

ok( !defined( Math::Symbolic::AuxFunctions::bell_number(-1) ),
    'bell_number(-1)' );

my @bell_numbers = ( 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 );

ok( Math::Symbolic::AuxFunctions::bell_number($_) == $bell_numbers[$_],
    "bell_number($_)" )
  for 0 .. $#bell_numbers;

my $special_constant = Math::Symbolic::Constant->zero();
ok(
    (
        ref $special_constant              eq 'Math::Symbolic::Constant'
          and $special_constant->{special} eq 'zero'
    ),
    "Special attribute on constants set correctly."
);

$special_constant->value(1);
ok(
    (
        not defined $special_constant->{special}
          or $special_constant->{special} ne 'zero'
    ),
    "Special attribute on constans unset correctly on change of value."
);