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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "./fmt" for Fmt
import "./math" for Math

var toBern2 = Fn.new { |a| [a[0], a[0] + a[1] / 2, a[0] + a[1] + a[2]] }

// uses de Casteljau's algorithm
var evalBern2 = Fn.new { |b, t|
    var s = 1 - t
    var b01 = s * b[0] + t * b[1]
    var b12 = s * b[1] + t * b[2]
    return s * b01 + t * b12
}

var toBern3 = Fn.new { |a|
    var b = List.filled(4, 0)
    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
var evalBern3 = Fn.new { |b, t|
    var s = 1 - t
    var b01  = s * b[0] + t * b[1]
    var b12  = s * b[1] + t * b[2]
    var b23  = s * b[2] + t * b[3]
    var b012 = s * b01  + t * b12
    var b123 = s * b12  + t * b23
    return s * b012 + t * b123
}

var bern2to3 = Fn.new { |q|
    var c = List.filled(4, 0)
    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
}

var pm = [1, 0, 0]
var qm = [1, 2, 3]
var rm = [1, 2, 3, 4]
var x
var y
var m

System.print("Subprogram(1) examples:")
var pb2 = toBern2.call(pm)
var qb2 = toBern2.call(qm)
Fmt.print("mono $n --> bern $n", pm, pb2)
Fmt.print("mono $n --> bern $n", qm, qb2)

System.print("\nSubprogram(2) examples:")
x = 0.25
y = evalBern2.call(pb2, x)
m = Math.evalPoly(pm[-1..0], x)
Fmt.print("p($4.2f) = $j (mono $j)", x, y, m)
x = 7.5
y = evalBern2.call(pb2, x)
m = Math.evalPoly(pm[-1..0], x)
Fmt.print("p($4.2f) = $j (mono $j)", x, y, m)
x = 0.25
y = evalBern2.call(qb2, x)
m = Math.evalPoly(qm[-1..0], x)
Fmt.print("q($4.2f) = $j (mono $j)", x, y, m)
x = 7.5
y = evalBern2.call(qb2, x)
m = Math.evalPoly(qm[-1..0], x)
Fmt.print("q($4.2f) = $j (mono $j)", x, y, m)

System.print("\nSubprogram(3) examples:")
pm.add(0)
qm.add(0)
var pb3 = toBern3.call(pm)
var qb3 = toBern3.call(qm)
var rb3 = toBern3.call(rm)
Fmt.print("mono $n --> bern $n", pm, pb3)
Fmt.print("mono $n --> bern $n", qm, qb3)
Fmt.print("mono $n --> bern $n", rm, rb3)

System.print("\nSubprogram(4) examples:")
x = 0.25
y = evalBern3.call(pb3, x)
m = Math.evalPoly(pm[-1..0], x)
Fmt.print("p($4.2f) = $j (mono $j)", x, y, m)
x = 7.5
y = evalBern3.call(pb3, x)
m = Math.evalPoly(pm[-1..0], x)
Fmt.print("p($4.2f) = $j (mono $j)", x, y, m)
x = 0.25
y = evalBern3.call(qb3, x)
m = Math.evalPoly(qm[-1..0], x)
Fmt.print("q($4.2f) = $j (mono $j)", x, y, m)
x = 7.5
y = evalBern3.call(qb3, x)
m = Math.evalPoly(qm[-1..0], x)
Fmt.print("q($4.2f) = $j (mono $j)", x, y, m)
x = 0.25
y = evalBern3.call(rb3, x)
m = Math.evalPoly(rm[-1..0], x)
Fmt.print("r($4.2f) = $j (mono $j)", x, y, m)
x = 7.5
y = evalBern3.call(rb3, x)
m = Math.evalPoly(rm[-1..0], x)
Fmt.print("r($4.2f) = $j (mono $j)", x, y, m)

System.print("\nSubprogram(5) examples:")
var pc = bern2to3.call(pb2)
var qc = bern2to3.call(qb2)
Fmt.print("mono $n --> bern $n", pb2, pc)
Fmt.print("mono $n --> bern $n", qb2, qc)


  

You may also check:How to resolve the algorithm Call an object method step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Mayan calendar step by step in the Python programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the 11l programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Oforth programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the Java programming language