File: 04deep_derivatives.t

package info (click to toggle)
libmath-symbolic-perl 0.613-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: perl: 12,638; makefile: 9
file content (64 lines) | stat: -rw-r--r-- 1,518 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
#!perl
use strict;
use warnings;

use Test::More tests => 4;

#use lib 'lib';

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( 'a' => 2 );

my $c   = Math::Symbolic::Constant->zero();
my $e   = $c->euler();
my $two = $c->new(2);

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

my $op   = Math::Symbolic::Operator->new();
my $mul1 = $op->new( '*', $two, $a );
my $exp1 = $op->new( '^', $e, $mul1 );

print "prefix notation and evaluation:\n";
print $exp1->to_string('prefix') . " = " . $exp1->value() . "\n\n";

print "Now, we derive this partially to 'a' (10 times): (infix)\n";

my $n_tree = $op->new(
    {
        type     => U_P_DERIVATIVE,
        operands => [ $exp1, $a ],
    }
);
foreach ( 1 .. 10 ) {
    print "$_\n";
    $n_tree = $op->new(
        {
            type     => U_P_DERIVATIVE,
            operands => [ $n_tree, $a ],
        }
    );
    $n_tree = $n_tree->apply_derivatives();
    $n_tree = $n_tree->simplify();
}

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

ok( abs($n_tree->op1()->value()-2048)<1e-10 , 'Large coefficient and op1() method' );
ok( $n_tree->op2()->op2()->op1()->value() == 2, 'op2() method' );
ok(
    $n_tree->op2()->op1()->{special} eq 'euler',
    'op2() method, special euler trait'
);