Polynomial synthetic division: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9: | Line 9: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F extended_synthetic_division(dividend, divisor) |
||
‘Fast polynomial division by using Extended Synthetic Division. Also works with non-monic polynomials.’ |
‘Fast polynomial division by using Extended Synthetic Division. Also works with non-monic polynomials.’ |
||
V out = copy(dividend) |
V out = copy(dividend) |
||
Line 27: | Line 27: | ||
print(‘ #. / #. =’.format(n, D), end' ‘ ’) |
print(‘ #. / #. =’.format(n, D), end' ‘ ’) |
||
V (a, b) = extended_synthetic_division(n, D) |
V (a, b) = extended_synthetic_division(n, D) |
||
print(‘#. remainder #.’.format(a, b))</ |
print(‘#. remainder #.’.format(a, b))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 37: | Line 37: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="cpp">/* |
||
* C++ Polynomial Sythetic Division |
* C++ Polynomial Sythetic Division |
||
* GNU Compile example for filename <synthdiv.cpp> |
* GNU Compile example for filename <synthdiv.cpp> |
||
Line 163: | Line 163: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 215: | Line 215: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 222: | Line 222: | ||
{{Trans|Go}} |
{{Trans|Go}} |
||
Thanks Rudy Velthuis for the [https://github.com/rvelthuis/DelphiBigNumbers Velthuis.BigRationals] library.<br> |
Thanks Rudy Velthuis for the [https://github.com/rvelthuis/DelphiBigNumbers Velthuis.BigRationals] library.<br> |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Polynomial_synthetic_division; |
program Polynomial_synthetic_division; |
||
Line 300: | Line 300: | ||
writeln(result[0].ToString, ' remainder ', result[1].ToString); |
writeln(result[0].ToString, ' remainder ', result[1].ToString); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1 -12 0 -42 ] / [1 -3 ] = [1 -9 -27 ] remainder [-123 ]</pre> |
<pre>[1 -12 0 -42 ] / [1 -3 ] = [1 -9 -27 ] remainder [-123 ]</pre> |
||
Line 307: | Line 307: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 341: | Line 341: | ||
Q, R := div(N, D) |
Q, R := div(N, D) |
||
fmt.Printf("%v / %v = %v remainder %v\n", N, D, Q, R) |
fmt.Printf("%v / %v = %v remainder %v\n", N, D, Q, R) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 348: | Line 348: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List |
||
normalized :: (Eq a, Num a) => [a] -> [a] |
normalized :: (Eq a, Num a) => [a] -> [a] |
||
Line 366: | Line 366: | ||
k = length p1 - length as |
k = length p1 - length as |
||
a:as = normalized p2 |
a:as = normalized p2 |
||
ker = negate <$> (as ++ repeat 0)</ |
ker = negate <$> (as ++ repeat 0)</syntaxhighlight> |
||
<pre>*Main> shortDiv [1,-12,0,-42] [1,1,-3] |
<pre>*Main> shortDiv [1,-12,0,-42] [1,1,-3] |
||
Line 376: | Line 376: | ||
For monic divisors it is possible to perform purely integral computations (without Fractional constraint): |
For monic divisors it is possible to perform purely integral computations (without Fractional constraint): |
||
< |
<syntaxhighlight lang="haskell">isMonic :: (Eq a, Num a) => [a] -> Bool |
||
isMonic = ([1] ==) . take 1 . normalized |
isMonic = ([1] ==) . take 1 . normalized |
||
Line 390: | Line 390: | ||
k = length p1 - length as |
k = length p1 - length as |
||
_:as = normalized p2 |
_:as = normalized p2 |
||
ker = negate <$> as ++ repeat 0</ |
ker = negate <$> as ++ repeat 0</syntaxhighlight> |
||
<pre>shortDivMonic [1,-12,0,-42] [1,1,-3 :: Int] |
<pre>shortDivMonic [1,-12,0,-42] [1,1,-3 :: Int] |
||
Line 399: | Line 399: | ||
Solving this the easy way: |
Solving this the easy way: |
||
< |
<syntaxhighlight lang="j"> psd=: [:(}. ;{.) ([ (] -/@,:&}. (* {:)) ] , %&{.~)^:(>:@-~&#)~</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> (1, (-12), 0, -42) psd (1, -3) |
||
┌────────┬────┐ |
┌────────┬────┐ |
||
│1 _9 _27│_123│ |
│1 _9 _27│_123│ |
||
└────────┴────┘ |
└────────┴────┘ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class Test { |
public class Test { |
||
Line 446: | Line 446: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>[1, -12, 0, -42] / [1, -3] = [[1, -9, -27], [-123]]</pre> |
<pre>[1, -12, 0, -42] / [1, -3] = [[1, -9, -27], [-123]]</pre> |
||
Line 452: | Line 452: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="julia">function divrem(dividend::Vector, divisor::Vector) |
||
result = copy(dividend) |
result = copy(dividend) |
||
quotientlen = length(divisor) - 1 |
quotientlen = length(divisor) - 1 |
||
Line 472: | Line 472: | ||
println("[$n] / [$d] = [$quotient] with remainder [$remainder]") |
println("[$n] / [$d] = [$quotient] with remainder [$remainder]") |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
[[1, -12, 0, -42]] / [[1, -3]] = [[1, -9, -27]] with remainder [[-123]] |
[[1, -12, 0, -42]] / [[1, -3]] = [[1, -9, -27]] with remainder [[-123]] |
||
Line 480: | Line 480: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun extendedSyntheticDivision(dividend: IntArray, divisor: IntArray): Pair<IntArray, IntArray> { |
fun extendedSyntheticDivision(dividend: IntArray, divisor: IntArray): Pair<IntArray, IntArray> { |
||
Line 509: | Line 509: | ||
print("${n2.contentToString()} / ${d2.contentToString()} = ") |
print("${n2.contentToString()} / ${d2.contentToString()} = ") |
||
println("${q2.contentToString()}, remainder ${r2.contentToString()}") |
println("${q2.contentToString()}, remainder ${r2.contentToString()}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 520: | Line 520: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">MakePolynomial[l_List, x_] := FromCoefficientRules[Thread[List /@ Range[Length[l] - 1, 0, -1] -> l], {x}] |
||
num = MakePolynomial[{1, -12, 0, -42}, x]; |
num = MakePolynomial[{1, -12, 0, -42}, x]; |
||
den = MakePolynomial[{1, -3}, x]; |
den = MakePolynomial[{1, -3}, x]; |
||
PolynomialQuotient[num, den, x] |
PolynomialQuotient[num, den, x] |
||
PolynomialRemainder[num, den, x]</ |
PolynomialRemainder[num, den, x]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>-27 - 9 x + x^2 |
<pre>-27 - 9 x + x^2 |
||
Line 531: | Line 531: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
type Polynomial = seq[int] |
type Polynomial = seq[int] |
||
Line 558: | Line 558: | ||
let d2 = @[1, 1, 1, 1] |
let d2 = @[1, 1, 1, 1] |
||
let (q2, r2) = extendedSyntheticDivision(n2, d2) |
let (q2, r2) = extendedSyntheticDivision(n2, d2) |
||
echo &"{n2} / {d2} = {q2}, remainder {r2}"</ |
echo &"{n2} / {d2} = {q2}, remainder {r2}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 567: | Line 567: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">sub synthetic_division { |
||
my($numerator,$denominator) = @_; |
my($numerator,$denominator) = @_; |
||
my @result = @$numerator; |
my @result = @$numerator; |
||
Line 588: | Line 588: | ||
print poly_divide([1, -12, 0, -42], [1, -3]); |
print poly_divide([1, -12, 0, -42], [1, -3]); |
||
print poly_divide([1, 0, 0, 0, -2], [1, 1, 1, 1]);</ |
print poly_divide([1, 0, 0, 0, -2], [1, 1, 1, 1]);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1 -12 0 -42] / [1 -3] = [1 -9 -27], remainder [-123] |
<pre>[1 -12 0 -42] / [1 -3] = [1 -9 -27], remainder [-123] |
||
Line 595: | Line 595: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">extendedSyntheticDivision</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dividend</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">divisor</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">extendedSyntheticDivision</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dividend</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">divisor</span><span style="color: #0000FF;">)</span> |
||
Line 625: | Line 625: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v / %v = %v, remainder %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v / %v = %v, remainder %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 639: | Line 639: | ||
{{works with|Python 2.7}} |
{{works with|Python 2.7}} |
||
{{works with|Python 3.x}} |
{{works with|Python 3.x}} |
||
< |
<syntaxhighlight lang="python">from __future__ import print_function |
||
from __future__ import division |
from __future__ import division |
||
Line 671: | Line 671: | ||
D = [1, -3] |
D = [1, -3] |
||
print(" %s / %s =" % (N,D), " %s remainder %s" % extended_synthetic_division(N, D)) |
print(" %s / %s =" % (N,D), " %s remainder %s" % extended_synthetic_division(N, D)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output: |
Sample output: |
||
Line 682: | Line 682: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(require racket/list) |
(require racket/list) |
||
;; dividend and divisor are both polynomials, which are here simply lists of coefficients. |
;; dividend and divisor are both polynomials, which are here simply lists of coefficients. |
||
Line 715: | Line 715: | ||
(define D '(1 -3)) |
(define D '(1 -3)) |
||
(define-values (Q R) (extended-synthetic-division N D)) |
(define-values (Q R) (extended-synthetic-division N D)) |
||
(printf "~a / ~a = ~a remainder ~a~%" N D Q R))</ |
(printf "~a / ~a = ~a remainder ~a~%" N D Q R))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 727: | Line 727: | ||
{{works with|Rakudo|2018.09}} |
{{works with|Rakudo|2018.09}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub synthetic-division ( @numerator, @denominator ) { |
||
my @result = @numerator; |
my @result = @numerator; |
||
my $end = @denominator.end; |
my $end = @denominator.end; |
||
Line 747: | Line 747: | ||
my %result = synthetic-division( @n, @d ); |
my %result = synthetic-division( @n, @d ); |
||
say "[{@n}] / [{@d}] = [%result<quotient>], remainder [%result<remainder>]"; |
say "[{@n}] / [{@d}] = [%result<quotient>], remainder [%result<remainder>]"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1 -12 0 -42] / [1 -3] = [1 -9 -27], remainder [-123] |
<pre>[1 -12 0 -42] / [1 -3] = [1 -9 -27], remainder [-123] |
||
Line 754: | Line 754: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/* REXX Polynomial Division */ |
||
/* extended to support order of divisor >1 */ |
/* extended to support order of divisor >1 */ |
||
call set_dd '1 0 0 0 -1' |
call set_dd '1 0 0 0 -1' |
||
Line 823: | Line 823: | ||
Return list']' |
Return list']' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1,-12,0,-42] / [1,-3] |
<pre>[1,-12,0,-42] / [1,-3] |
||
Line 834: | Line 834: | ||
===Java Interoperability=== |
===Java Interoperability=== |
||
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/59vpjcQ/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/uUk8yRPnQdGdS1aAUFjhmA Scastie (remote JVM)]. |
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/59vpjcQ/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/uUk8yRPnQdGdS1aAUFjhmA Scastie (remote JVM)]. |
||
< |
<syntaxhighlight lang="scala">import java.util |
||
object PolynomialSyntheticDivision extends App { |
object PolynomialSyntheticDivision extends App { |
||
Line 862: | Line 862: | ||
}%s") |
}%s") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ruby">func extended_synthetic_division(dividend, divisor) { |
||
var end = divisor.end |
var end = divisor.end |
||
var out = dividend.clone |
var out = dividend.clone |
||
Line 889: | Line 889: | ||
var (n, d) = ([1, -12, 0, -42], [1, -3]) |
var (n, d) = ([1, -12, 0, -42], [1, -3]) |
||
print(" %s / %s =" % (n, d)) |
print(" %s / %s =" % (n, d)) |
||
print(" %s remainder %s\n" % extended_synthetic_division(n, d))</ |
print(" %s remainder %s\n" % extended_synthetic_division(n, d))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[1, -12, 0, -42] / [1, -3] = [1, -9, -27] remainder [-123] |
[1, -12, 0, -42] / [1, -3] = [1, -9, -27] remainder [-123] |
||
Line 898: | Line 898: | ||
This uses a common utility proc <tt>range</tt>, and a less common one called <tt>lincr</tt>, which increments elements of lists. The routine for polynomial division is placed in a namespace ensemble, such that it can be conveniently shared with other commands for polynomial arithmetic (eg <tt>polynomial multiply</tt>). |
This uses a common utility proc <tt>range</tt>, and a less common one called <tt>lincr</tt>, which increments elements of lists. The routine for polynomial division is placed in a namespace ensemble, such that it can be conveniently shared with other commands for polynomial arithmetic (eg <tt>polynomial multiply</tt>). |
||
< |
<syntaxhighlight lang="tcl"># range ?start? end+1 |
||
# start defaults to 0: [range 5] = {0 1 2 3 4} |
# start defaults to 0: [range 5] = {0 1 2 3 4} |
||
proc range {a {b ""}} { |
proc range {a {b ""}} { |
||
Line 947: | Line 947: | ||
puts "$top / $btm = $div" |
puts "$top / $btm = $div" |
||
} |
} |
||
test</ |
test</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 955: | Line 955: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-dynamic}} |
{{libheader|Wren-dynamic}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/dynamic" for Tuple |
||
var Solution = Tuple.create("Solution", ["quotient", "remainder"]) |
var Solution = Tuple.create("Solution", ["quotient", "remainder"]) |
||
Line 984: | Line 984: | ||
var sol2 = extendedSyntheticDivision.call(n2, d2) |
var sol2 = extendedSyntheticDivision.call(n2, d2) |
||
System.write("%(n2) / %(d2) = ") |
System.write("%(n2) / %(d2) = ") |
||
System.print("%(sol2.quotient), remainder %(sol2.remainder)")</ |
System.print("%(sol2.quotient), remainder %(sol2.remainder)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 996: | Line 996: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="zkl">fcn extended_synthetic_division(dividend, divisor){ |
||
# Fast polynomial division by using Extended Synthetic Division. Also works with non-monic polynomials. |
# Fast polynomial division by using Extended Synthetic Division. Also works with non-monic polynomials. |
||
# dividend and divisor are both polynomials, which are here simply lists of coefficients. Eg: x^2 + 3x + 5 will be represented as [1, 3, 5] |
# dividend and divisor are both polynomials, which are here simply lists of coefficients. Eg: x^2 + 3x + 5 will be represented as [1, 3, 5] |
||
Line 1,016: | Line 1,016: | ||
separator := -(divisor.len() - 1); |
separator := -(divisor.len() - 1); |
||
return(out[0,separator], out[separator,*]) # return quotient, remainder. |
return(out[0,separator], out[separator,*]) # return quotient, remainder. |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println("POLYNOMIAL SYNTHETIC DIVISION"); |
||
N,D := T(1, -12, 0, -42), T(1, -3); |
N,D := T(1, -12, 0, -42), T(1, -3); |
||
print(" %s / %s =".fmt(N,D)); |
print(" %s / %s =".fmt(N,D)); |
||
println(" %s remainder %s".fmt(extended_synthetic_division(N,D).xplode()));</ |
println(" %s remainder %s".fmt(extended_synthetic_division(N,D).xplode()));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |