With attempts to use Unicode operators where possible to make the Perl 6 code look like the math.
from the jupyter docs \begin{equation*} \left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \end{equation*}
sub cauchy-schwarz(@a,@b) {
( [+] @a Z× @b )² ≤ ( [+] @a»² ) × ( [+] @b»² )
}
cauchy-schwarz( ( ^100 ).pick(5), ( ^100 ).pick(5) )
True
see this discussion and wikipedia \begin{equation*} \Delta_0 = b^2 - 3ac \\ \Delta_1 = 2b^3 - 9abc + 27a^2d \\ C = \sqrt[3]{ \frac{ \Delta_1 \pm \sqrt{\Delta_1^2 - 4 \Delta_0^3 } }{2} } \\ x_k = - \frac{1}{3a}( b + ς^k C + \frac{\Delta_0}{ς^k C}), k ∊ {0,1,2} \end{equation*}
sub cubic(\a,\b,\c,\d) {
my \Δ0 = b² - 3 × a × c;
# note: special case when Δ0 == 0
my \Δ1 = 2 * b³ - 9 × a × b × c + 27 × a² × d;
my \C = ( ( Δ1 + sqrt( Δ1² - 4 × Δ0³ + 0i) ) / 2 ).roots(3)[0];
my \ς = 1.roots(3); # cubic roots of unity
return [0,1,2].map: -> \k {
( -1 / ( 3 × a ) ) × ( b + ς[k] × C + Δ0 / ( C × ς[k] ) )
}
}
my @vals = cubic(1,10,10,-10);
my $f = -> \x { x³ + 10 * x² + 10 * x - 10 };
my $*TOLERANCE = 1e-10;
[ $f( $_ ) ≅ 0 for @vals ]
[True True True]