How to resolve the algorithm Polynomial long division step by step in the Go programming language
How to resolve the algorithm Polynomial long division step by step in the Go programming language
Table of Contents
Problem Statement
Let us suppose a polynomial is represented by a vector,
x
{\displaystyle x}
(i.e., an ordered collection of coefficients) so that the
i
{\displaystyle i}
th element keeps the coefficient of
x
i
{\displaystyle x^{i}}
, and the multiplication by a monomial is a shift of the vector's elements "towards right" (injecting ones from left) followed by a multiplication of each element by the coefficient of the monomial. Then a pseudocode for the polynomial long division using the conventions described above could be: Note: vector * scalar multiplies each element of the vector by the scalar; vectorA - vectorB subtracts each element of the vectorB from the element of the vectorA with "the same index". The vectors in the pseudocode are zero-based.
Example for clarification
This example is from Wikipedia, but changed to show how the given pseudocode works.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Polynomial long division step by step in the Go programming language
The code snippet you provided is an implementation of the polynomial long division algorithm in Go.
The pld
function takes two input polynomials, nn
and dd
, and returns the quotient q
, the remainder r
, and a boolean indicating whether the division was successful (ok
).
The first step in the algorithm is to check if the degree of the divisor polynomial dd
is negative. If it is, the function returns with all ok
set to false
to indicate that the division cannot be performed.
If the degree of dd
is non-negative, the function appends the remainder r
to the dividend nn
and checks if the degree of nn
is greater than or equal to the degree of dd
. If it is, the function creates a slice q
to store the quotient and iterates through the terms of nn
until the degree of nn
is less than the degree of dd
.
In each iteration, the function creates a slice d
to hold the terms of the divisor polynomial, copies the terms of dd
into d
, and divides the leading coefficient of nn
by the leading coefficient of d
. The result of the division is stored in the corresponding element of q
.
The function then subtracts the product of d
and q
from nn
. This effectively removes the leading term of nn
from the polynomial and reduces its degree by one.
The loop continues until the degree of nn
is less than the degree of dd
. At this point, the remaining terms of nn
are the remainder r
.
The function returns the quotient q
, the remainder r
, and a boolean ok
set to true
indicating that the division was successful.
Here is an example of how to use the pld
function:
package main
import "fmt"
func main() {
n := []float64{-42, 0, -12, 1}
d := []float64{-3, 1}
q, r, ok := pld(n, d)
if ok {
fmt.Println("Q:", q)
fmt.Println("R:", r)
} else {
fmt.Println("error")
}
}
This will print the following output:
Q: [14 4]
R: [0]
Source code in the go programming language
package main
import "fmt"
func main() {
n := []float64{-42, 0, -12, 1}
d := []float64{-3, 1}
fmt.Println("N:", n)
fmt.Println("D:", d)
q, r, ok := pld(n, d)
if ok {
fmt.Println("Q:", q)
fmt.Println("R:", r)
} else {
fmt.Println("error")
}
}
func degree(p []float64) int {
for d := len(p) - 1; d >= 0; d-- {
if p[d] != 0 {
return d
}
}
return -1
}
func pld(nn, dd []float64) (q, r []float64, ok bool) {
if degree(dd) < 0 {
return
}
nn = append(r, nn...)
if degree(nn) >= degree(dd) {
q = make([]float64, degree(nn)-degree(dd)+1)
for degree(nn) >= degree(dd) {
d := make([]float64, degree(nn)+1)
copy(d[degree(nn)-degree(dd):], dd)
q[degree(nn)-degree(dd)] = nn[degree(nn)] / d[degree(d)]
for i := range d {
d[i] *= q[degree(nn)-degree(dd)]
nn[i] -= d[i]
}
}
}
return q, nn, true
}
You may also check:How to resolve the algorithm UPC step by step in the Phix programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the Maple programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the AWK programming language
You may also check:How to resolve the algorithm Function composition step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the D programming language