How to resolve the algorithm Bernstein basis polynomials step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bernstein basis polynomials step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

Dim Shared b0 As Double, b1 As Double, b2 As Double

Sub tobern2 (Byval a0 As Double, Byval a1 As Double, Byval a2 As Double, _
    Byref b0 As Double, Byref b1 As Double, Byref b2 As Double)
    ' Subprogram (1): transform monomial coefficients
    '        a0, a1, a2 to Bernstein coefficients b0, b1, b2
    
    b0 = a0
    b1 = a0 + ((1/2) * a1)
    b2 = a0 + a1 + a2
End Sub

Function evalbern2 (b0 As Double, b1 As Double, b2 As Double, t As Double) As Double
    ' Subprogram (2): evaluate, at t, the polynomial with
    '        Bernstein coefficients b0, b1, b2. Use de Casteljau's algorithm
    
    Dim As Double s, b01, b12, b012
    s = 1 - t
    b01 = (s * b0) + (t * b1)
    b12 = (s * b1) + (t * b2)
    b012 = (s * b01) + (t * b12)
    Return b012
End Function

Sub tobern3 (Byval a0 As Double, Byval a1 As Double, Byval a2 As Double, Byval a3 As Double, _
    Byref b0 As Double, Byref b1 As Double, Byref b2 As Double, Byref b3 As Double)
    ' Subprogram (3): transform monomial coefficients
    '        a0, a1, a2, a3 to Bernstein coefficients b0, b1, b2, b3
    
    b0 = a0
    b1 = a0 + ((1/3) * a1)
    b2 = a0 + ((2/3) * a1) + ((1/3) * a2)
    b3 = a0 + a1 + a2 + a3
End Sub

Function evalbern3 (b0 As Double, b1 As Double, b2 As Double, _
    b3 As Double, t As Double) As Double
    ' Subprogram (4): evaluate, at t, the polynomial
    '        with Bernstein coefficients b0, b1, b2, b3.
    '        Use de Casteljau's algorithm
    
    Dim As Double s, b01, b12, b23, b012, b123, b0123
    s = 1 - t
    b01 = (s * b0) + (t * b1)
    b12 = (s * b1) + (t * b2)
    b23 = (s * b2) + (t * b3)
    b012 = (s * b01) + (t * b12)
    b123 = (s * b12) + (t * b23)
    b0123 = (s * b012) + (t * b123)
    Return b0123
End Function

Sub bern2to3 (Byval q0 As Double, Byval q1 As Double, Byval q2 As Double, _
    Byref c0 As Double, Byref c1 As Double, Byref c2 As Double, Byref c3 As Double)
    ' Subprogram (5): transform the quadratic Bernstein
    '        coefficients q0, q1, q2 to the cubic Bernstein
    '        coefficients c0, c1, c2, c3
    
    c0 = q0
    c1 = ((1/3) * q0) + ((2/3) * q1)
    c2 = ((2/3) * q1) + ((1/3) * q2)
    c3 = q2
End Sub

Dim As Double p0b2, p1b2, p2b2
Dim As Double q0b2, q1b2, q2b2

Dim As Double p0b3, p1b3, p2b3, p3b3
Dim As Double q0b3, q1b3, q2b3, q3b3
Dim As Double r0b3, r1b3, r2b3, r3b3

Dim As Double pc0, pc1, pc2, pc3
Dim As Double qc0, qc1, qc2, qc3

Dim As Double x, y


Dim As Double p0m = 1, p1m = 0, p2m = 0
Dim As Double q0m = 1, q1m = 2, q2m = 3
Dim As Double r0m = 1, r1m = 2, r2m = 3, r3m = 4

tobern2 (p0m, p1m, p2m, p0b2, p1b2, p2b2)
tobern2 (q0m, q1m, q2m, q0b2, q1b2, q2b2)
Print "Subprogram (1) examples:"
Print Using "  mono (#.##, #.##, #.##) --> bern (#.##, #.##, #.##)"; p0m; p1m; p2m; p0b2; p1b2; p2b2
Print Using "  mono (#.##, #.##, #.##) --> bern (#.##, #.##, #.##)"; q0m; q1m; q2m; q0b2; q1b2; q2b2

Print "Subprogram (2) examples:"
x = 0.25
y = evalbern2 (p0b2, p1b2, p2b2, x)
Print Using "  p (#.##) = ###.##"; x; y
x = 7.50
y = evalbern2 (p0b2, p1b2, p2b2, x)
Print Using "  p (#.##) = ###.##"; x; y
x = 0.25
y = evalbern2 (q0b2, q1b2, q2b2, x)
Print Using "  q (#.##) = ###.##"; x; y
x = 7.50
y = evalbern2 (q0b2, q1b2, q2b2, x)
Print Using "  q (#.##) = ###.##"; x; y

tobern3 (p0m, p1m, p2m, 0, p0b3, p1b3, p2b3, p3b3)
tobern3 (q0m, q1m, q2m, 0, q0b3, q1b3, q2b3, q3b3)
tobern3 (r0m, r1m, r2m, r3m, r0b3, r1b3, r2b3, r3b3)
Print "Subprogram (3) examples:"
Print Using "  mono (#.##, #.##, #.##, 0.00) --> bern (#.##, #.##, #.##, ##.##)"; p0m; p1m; p2m; p0b3; p1b3; p2b3; p3b3
Print Using "  mono (#.##, #.##, #.##, 0.00) --> bern (#.##, #.##, #.##, ##.##)"; q0m; q1m; q2m; q0b3; q1b3; q2b3; q3b3
Print Using "  mono (#.##, #.##, #.##, #.##) --> bern (#.##, #.##, #.##, ##.##)"; r0m; r1m; r2m; r3m; r0b3; r1b3; r2b3; r3b3

Print "Subprogram (4) examples:"
x = 0.25
y = evalbern3 (p0b3, p1b3, p2b3, p3b3, x)
Print Using "  p (#.##) = ####.##"; x; y
x = 7.50
y = evalbern3 (p0b3, p1b3, p2b3, p3b3, x)
Print Using "  p (#.##) = ####.##"; x; y
x = 0.25
y = evalbern3 (q0b3, q1b3, q2b3, q3b3, x)
Print Using "  q (#.##) = ####.##"; x; y
x = 7.50
y = evalbern3 (q0b3, q1b3, q2b3, q3b3, x)
Print Using "  q (#.##) = ####.##"; x; y
x = 0.25
y = evalbern3 (r0b3, r1b3, r2b3, r3b3, x)
Print Using "  r (#.##) = ####.##"; x; y
x = 7.50
y = evalbern3 (r0b3, r1b3, r2b3, r3b3, x)
Print Using "  r (#.##) = ####.##"; x; y

bern2to3 (p0b2, p1b2, p2b2, pc0, pc1, pc2, pc3)
bern2to3 (q0b2, q1b2, q2b2, qc0, qc1, qc2, qc3)
Print "Subprogram (5) examples:"
Print Using "  bern (#.##, #.##, #.##) --> bern (#.##, #.##, #.##, #.##)"; p0b2; p1b2; p2b2; pc0; pc1; pc2; pc3
Print Using "  bern (#.##, #.##, #.##) --> bern (#.##, #.##, #.##, #.##)"; q0b2; q1b2; q2b2; qc0; qc1; qc2; qc3

Sleep

  

You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Erlang programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the TXR programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Gambas programming language
You may also check:How to resolve the algorithm Assertions step by step in the Forth programming language