File: derivative.pl

package info (click to toggle)
libmath-utils-perl 1.14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: perl: 937; makefile: 8
file content (52 lines) | stat: -rwxr-xr-x 825 bytes parent folder | download | duplicates (3)
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
#!/bin/perl
#
#
use Carp;
use Math::Utils qw(:polynomial);
use Math::Complex;
use strict;
use warnings;

while (my $line = prompt("Polynomial: "))
{
	my @polynomial = split(/,? /, $line);
	last unless ($line);

	my $x = prompt("x-value: ");

	d1(\@polynomial, $x);
	d2(\@polynomial, $x);
}
exit(0);

sub d1
{
	my($p_ref, $x) = @_;

	my($r, $d1, $d2) = pl_dxevaluate($p_ref, $x);

	print "Polynomial: $r, First derivative: $d1, Second derivative $d2\n\n";
}

sub d2
{
	my($p_ref, $x) = @_;

	my @d1p = pl_derivative(@$p_ref);
	my @d2p = pl_derivative(@d1p);

	my $r = pl_evaluate($p_ref, $x);
	my $d1 = pl_evaluate(\@d1p, $x);
	my $d2 = pl_evaluate(\@d2p, $x);

	print "Polynomial: $r, First derivative: $d1, Second derivative $d2\n\n";
}

sub prompt
{
	my $pr = shift;
	print $pr;
	my $inp = <>;
	chomp $inp;
	return $inp;
}