Horner's rule for polynomial evaluation: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 232:
128
</pre>
 
 
=={{header|BBC BASIC}}==
Line 456 ⟶ 455:
(poly 3 P) → 128
</lang>
 
=={{header|Elena}}==
{{trans|C#}}
Line 641:
END
</lang>
 
 
=={{header|FreeBASIC}}==
Line 663 ⟶ 662:
Algoritmo de Horner para el polinomio 6*x^3 - 4*x^2 + 7*x - 19 para x = 3: 128
</pre>
 
 
=={{header|FunL}}==
Line 953 ⟶ 951:
coefficients = { -19, 7, -4, 6 }
print( horners_rule( coefficients, x ) )</lang>
 
 
=={{header|Maple}}==
Line 1,169 ⟶ 1,166:
128
</pre>
 
=={{header|Objeck}}==
<lang objeck>
class Horner {
function : Main(args : String[]) ~ Nil {
coeffs := Collection.FloatVector->New();
coeffs->AddBack(-19.0);
coeffs->AddBack(7.0);
coeffs->AddBack(-4.0);
coeffs->AddBack(6.0);
PolyEval(coeffs, 3)->PrintLine();
}
function : PolyEval(coefficients : Collection.FloatVector , x : Float) ~ Float {
accumulator := coefficients->Get(coefficients->Size() - 1);
for(i := coefficients->Size() - 2; i > -1; i -= 1;) {
accumulator := (accumulator * x) + coefficients->Get(i);
};
return accumulator;
}
}
</lang>
 
=={{header|Objective-C}}==
{{works with|Mac OS X|10.6+}} Using blocks
Line 1,214 ⟶ 1,234:
return 0;
}</lang>
 
=={{header|Objeck}}==
<lang objeck>
class Horner {
function : Main(args : String[]) ~ Nil {
coeffs := Collection.FloatVector->New();
coeffs->AddBack(-19.0);
coeffs->AddBack(7.0);
coeffs->AddBack(-4.0);
coeffs->AddBack(6.0);
PolyEval(coeffs, 3)->PrintLine();
}
function : PolyEval(coefficients : Collection.FloatVector , x : Float) ~ Float {
accumulator := coefficients->Get(coefficients->Size() - 1);
for(i := coefficients->Size() - 2; i > -1; i -= 1;) {
accumulator := (accumulator * x) + coefficients->Get(i);
};
return accumulator;
}
}
</lang>
 
=={{header|OCaml}}==
Line 1,275 ⟶ 1,272:
<pre>128
128</pre>
 
=={{header|Oz}}==
<lang oz>declare
Line 1,359 ⟶ 1,356:
print horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
=={{header|Perl 6}}==
<lang perl6>sub horner ( @coeffs, $x ) {
@coeffs.reverse.reduce: { $^a * $x + $^b };
}
 
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Perl 6 implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.perl6.org/type/Pair Pairs] and the reduction meta-operator:
 
<lang perl6>multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
$c.key + $x * horner( $c.value, $x )
}
say horner( [=>](-19, 7, -4, 6 ), 3 );</lang>
 
We can also use the composition operator:
<lang perl6>sub horner ( @coeffs, $x ) {
([o] map { $_ + $x * * }, @coeffs)(0);
}
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
{{out}}
<pre>128</pre>
 
One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
<lang perl6>sub horner ( @coeffs, $x ) {
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
</lang>
{{out}}
<pre>-0.999999999924349-5.28918515954219e-10i</pre>
 
=={{header|Phix}}==
Line 1,606 ⟶ 1,567:
 
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub horner ( @coeffs, $x ) {
@coeffs.reverse.reduce: { $^a * $x + $^b };
}
 
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Perl 6 implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.perl6.org/type/Pair Pairs] and the reduction meta-operator:
 
<lang perl6>multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
$c.key + $x * horner( $c.value, $x )
}
say horner( [=>](-19, 7, -4, 6 ), 3 );</lang>
 
We can also use the composition operator:
<lang perl6>sub horner ( @coeffs, $x ) {
([o] map { $_ + $x * * }, @coeffs)(0);
}
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
 
{{out}}
<pre>128</pre>
 
One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
<lang perl6>sub horner ( @coeffs, $x ) {
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
</lang>
{{out}}
<pre>-0.999999999924349-5.28918515954219e-10i</pre>
 
=={{header|Rascal}}==
Line 1,767 ⟶ 1,765:
end
p horner([-19, 7, -4, 6], 3) # ==> 128</lang>
 
=={{header|Run BASIC}}==
<lang runbasic>coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coef$,x) '128
print horner("1.2 2.3 3.4 4.5 5.6", 8) '25478.8
print horner("5 4 3 2 1", 10) '12345
print horner("1 0 1 1 1 0 0 1", 2) '157
end
 
function horner(coef$,x)
while word$(coef$, i + 1) <> ""
i = i + 1 ' count the num of values
wend
for j = i to 1 step -1
accum = ( accum * x ) + val(word$(coef$, j))
next
horner = accum
end function</lang>
 
=={{header|Rust}}==
Line 1,799 ⟶ 1,816:
println!("result: {}", output);
}</lang>
 
=={{header|Run BASIC}}==
<lang runbasic>coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coef$,x) '128
print horner("1.2 2.3 3.4 4.5 5.6", 8) '25478.8
print horner("5 4 3 2 1", 10) '12345
print horner("1 0 1 1 1 0 0 1", 2) '157
end
 
function horner(coef$,x)
while word$(coef$, i + 1) <> ""
i = i + 1 ' count the num of values
wend
for j = i to 1 step -1
accum = ( accum * x ) + val(word$(coef$, j))
next
horner = accum
end function</lang>
 
=={{header|Sather}}==
Line 1,965 ⟶ 1,963:
128
</pre>
 
=={{header|VBScript}}==
<lang vb>
Function horners_rule(coefficients,x)
accumulator = 0
For i = UBound(coefficients) To 0 Step -1
accumulator = (accumulator * x) + coefficients(i)
Next
horners_rule = accumulator
End Function
 
WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
</lang>
 
{{Out}}
<pre>128</pre>
 
=={{header|Visual Basic .NET}}==
Line 2,027 ⟶ 2,041:
ENDFUNC
</lang>
 
=={{header|VBScript}}==
<lang vb>
Function horners_rule(coefficients,x)
accumulator = 0
For i = UBound(coefficients) To 0 Step -1
accumulator = (accumulator * x) + coefficients(i)
Next
horners_rule = accumulator
End Function
 
WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
</lang>
 
{{Out}}
<pre>128</pre>
 
=={{header|XPL0}}==
10,333

edits