How to resolve the algorithm Bernstein basis polynomials step by step in the Go programming language
How to resolve the algorithm Bernstein basis polynomials step by step in the Go programming language
Table of Contents
Problem Statement
The
n + 1
{\displaystyle n+1}
Bernstein basis polynomials of degree
n
{\displaystyle n}
are defined as Any real polynomial can written as a linear combination of such Bernstein basis polynomials. Let us call the coefficients in such linear combinations Bernstein coefficients. The goal of this task is to write subprograms for working with degree-2 and degree-3 Bernstein coefficients. A programmer is likely to have to deal with such representations. For example, OpenType fonts store glyph outline data as as either degree-2 or degree-3 Bernstein coefficients. The task is as follows: ALGOL 60 and Python implementations are provided as initial examples. The latter does the optional monomial-basis evaluations. You can use the following algorithms. They are written in unambiguous Algol 60 reference language instead of a made up pseudo-language. The ALGOL 60 example was necessary to check my work, but these reference versions are in the actual standard language designed for the printing of algorithms.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bernstein basis polynomials step by step in the Go programming language
The provided Go program demonstrates the conversion between monomial and Bernstein polynomials and evaluates them at various points. Here's a detailed explanation:
-
Subprogram(1): Converting Monomial to Bernstein Polynomials:
toBern2
: Converts a monomial polynomial represented as a slice of coefficientsa
to a quadratic Bernstein polynomial by returning a slice of Bernstein coefficientsb
. It inserts zeros at the end to ensure the correct degree.toBern3
: Similar totoBern2
, but converts to a cubic Bernstein polynomial.
-
Subprogram(2): Evaluating Bernstein Polynomials:
evalBern2
andevalBern3
: Use de Casteljau's algorithm to evaluate quadratic and cubic Bernstein polynomials, respectively, at a given parameter valuet
.
-
Subprogram(3): Converting Monomial to Bernstein Polynomials with Horner's Rule:
toBern2
andtoBern3
: Similar to the previous subprogram, but they convert monomial polynomials to Bernstein polynomials using Horner's rule, which provides an alternative method for calculating Bernstein coefficients.
-
Subprogram(4): Evaluating Monomial Polynomials with Horner's Rule:
evalMono2
andevalMono3
: Use Horner's rule to evaluate quadratic and cubic monomial polynomials, respectively, at a given parameter valuet
.
-
Subprogram(5): Converting Between Bernstein Polynomials:
bern2to3
: Converts a quadratic Bernstein polynomial to a cubic Bernstein polynomial.
Main Function:
- Defines monomial and Bernstein polynomial coefficients for quadratic and cubic polynomials.
- Demonstrates the conversion from monomial to Bernstein polynomials and their evaluation at different parameter values.
The output shows the converted Bernstein coefficients and the evaluation results of both monomial and Bernstein polynomials at specified parameter values. It compares the results to illustrate the equivalence between the two representations.
Source code in the go programming language
package main
import "fmt"
func toBern2(a []float64) []float64 {
return []float64{a[0], a[0] + a[1]/2, a[0] + a[1] + a[2]}
}
// uses de Casteljau's algorithm
func evalBern2(b []float64, t float64) float64 {
s := 1.0 - t
b01 := s*b[0] + t*b[1]
b12 := s*b[1] + t*b[2]
return s*b01 + t*b12
}
func toBern3(a []float64) []float64 {
b := make([]float64, 4)
b[0] = a[0]
b[1] = a[0] + a[1]/3
b[2] = a[0] + a[1]*2/3 + a[2]/3
b[3] = a[0] + a[1] + a[2] + a[3]
return b
}
// uses de Casteljau's algorithm
func evalBern3(b []float64, t float64) float64 {
s := 1.0 - t
b01 := s*b[0] + t*b[1]
b12 := s*b[1] + t*b[2]
b23 := s*b[2] + t*b[3]
b012 := s*b01 + t*b12
b123 := s*b12 + t*b23
return s*b012 + t*b123
}
func bern2to3(q []float64) []float64 {
c := make([]float64, 4)
c[0] = q[0]
c[1] = q[0]/3 + q[1]*2/3
c[2] = q[1]*2/3 + q[2]/3
c[3] = q[2]
return c
}
// uses Horner's rule
func evalMono2(a []float64, t float64) float64 {
return a[0] + (t * (a[1] + (t * a[2])))
}
// uses Horner's rule
func evalMono3(a []float64, t float64) float64 {
return a[0] + (t * (a[1] + (t * (a[2] + (t * a[3])))))
}
func main() {
pm := []float64{1, 0, 0}
qm := []float64{1, 2, 3}
rm := []float64{1, 2, 3, 4}
var x, y, m float64
fmt.Println("Subprogram(1) examples:")
pb2 := toBern2(pm)
qb2 := toBern2(qm)
fmt.Printf("mono %v --> bern %v\n", pm, pb2)
fmt.Printf("mono %v --> bern %v\n", qm, qb2)
fmt.Println("\nSubprogram(2) examples:")
x = 0.25
y = evalBern2(pb2, x)
m = evalMono2(pm, x)
fmt.Printf("p(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 7.5
y = evalBern2(pb2, x)
m = evalMono2(pm, x)
fmt.Printf("p(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 0.25
y = evalBern2(qb2, x)
m = evalMono2(qm, x)
fmt.Printf("q(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 7.5
y = evalBern2(qb2, x)
m = evalMono2(qm, x)
fmt.Printf("q(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
fmt.Println("\nSubprogram(3) examples:")
pm = append(pm, 0)
qm = append(qm, 0)
pb3 := toBern3(pm)
qb3 := toBern3(qm)
rb3 := toBern3(rm)
f := "mono $%v --> bern %0.14v\n"
fmt.Printf(f, pm, pb3)
fmt.Printf(f, qm, qb3)
fmt.Printf(f, rm, rb3)
fmt.Println("\nSubprogram(4) examples:")
x = 0.25
y = evalBern3(pb3, x)
m = evalMono3(pm, x)
fmt.Printf("p(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 7.5
y = evalBern3(pb3, x)
m = evalMono3(pm, x)
fmt.Printf("p(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 0.25
y = evalBern3(qb3, x)
m = evalMono3(qm, x)
fmt.Printf("q(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 7.5
y = evalBern3(qb3, x)
m = evalMono3(qm, x)
fmt.Printf("q(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 0.25
y = evalBern3(rb3, x)
m = evalMono3(rm, x)
fmt.Printf("r(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
x = 7.5
y = evalBern3(rb3, x)
m = evalMono3(rm, x)
fmt.Printf("r(%4.2f) = %0.14g (mono %0.14g)\n", x, y, m)
fmt.Println("\nSubprogram(5) examples:")
pc := bern2to3(pb2)
qc := bern2to3(qb2)
fmt.Printf("mono %v --> bern %0.14v\n", pb2, pc)
fmt.Printf("mono %v --> bern %0.14v\n", qb2, qc)
}
You may also check:How to resolve the algorithm Eban numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the 8th programming language
You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the J programming language