forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadratic_polynomial_in_terms_of_its_zeros.pl
More file actions
executable file
·60 lines (44 loc) · 1.23 KB
/
quadratic_polynomial_in_terms_of_its_zeros.pl
File metadata and controls
executable file
·60 lines (44 loc) · 1.23 KB
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 09 August 2017
# https://github.com/trizen
# Representation of quadratic polynomials in terms of their zeros.
# Let:
# P(x) = a*x^2 + b*x + c
# Let (m, n) be the solutions to P(x) = 0
# Then:
# P(x) = c * (1 - x/m) * (1 - x/n)
use 5.010;
use strict;
use warnings;
use Math::Bacovia qw(:all);
use Math::AnyNum qw(isqrt);
sub integer_quadratic_formula {
my ($x, $y, $z) = @_;
(
Fraction((-$y + isqrt($y**2 - 4 * $x * $z)), (2 * $x)),
Fraction((-$y - isqrt($y**2 - 4 * $x * $z)), (2 * $x)),
);
}
my @poly = (
[ 3, -15, -42],
[ 20, -97, -2119],
[-43, 29, 14972],
);
my $x = Symbol('x');
foreach my $t (@poly) {
my ($x1, $x2) = integer_quadratic_formula(@$t);
my $expr = $t->[0] * $x**2 + $t->[1] * $x + $t->[2];
my $f1 = (1 - $x / $x1);
my $f2 = (1 - $x / $x2);
printf("%s = %s * %s * %s\n",
$expr->pretty,
$f1->simple->pretty,
$f2->simple->pretty,
$t->[2],
);
}
__END__
((3 * x^2) + (-15 * x) + -42) = (1 - (x/7)) * (1 - (x/-2)) * -42
((20 * x^2) + (-97 * x) + -2119) = (1 - (x/13)) * (1 - (x/(-326/40))) * -2119
((-43 * x^2) + (29 * x) + 14972) = (1 - (x/(-788/43))) * (1 - (x/19)) * 14972