Bernstein basis polynomials: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Added optional part.)
Line 1,124: Line 1,124:
{{trans|ALGOL 60}}
{{trans|ALGOL 60}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
Note that the library method, ''Math.evalPoly'', evaluates polynomials of any degree using Horner's rule but requires the coefficients to be presented in highest to lowest degree order.
<syntaxhighlight lang="ecmascript">import "./fmt" for Fmt
<syntaxhighlight lang="ecmascript">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]] }
var toBern2 = Fn.new { |a| [a[0], a[0] + a[1] / 2, a[0] + a[1] + a[2]] }
Line 1,170: Line 1,173:
var x
var x
var y
var y
var m


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


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


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


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


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


{{out}}
{{out}}
<pre>
<pre>
Subprogram(1) examples:
Subprogram(1) examples:
mono[1, 0, 0] --> bern[1, 1, 1]
mono [1, 0, 0] --> bern [1, 1, 1]
mono[1, 2, 3] --> bern[1, 2, 6]
mono [1, 2, 3] --> bern [1, 2, 6]


Subprogram(2) examples:
Subprogram(2) examples:
p(0.25) = 1
p(0.25) = 1 (mono 1)
p(7.50) = 1
p(7.50) = 1 (mono 1)
q(0.25) = 1.6875
q(0.25) = 1.6875 (mono 1.6875)
q(7.50) = 184.75
q(7.50) = 184.75 (mono 184.75)


Subprogram(3) examples:
Subprogram(3) examples:
mono[1, 0, 0, 0] --> bern[1, 1, 1, 1]
mono [1, 0, 0, 0] --> bern [1, 1, 1, 1]
mono[1, 2, 3, 0] --> bern[1, 1.6666666666667, 3.3333333333333, 6]
mono [1, 2, 3, 0] --> bern [1, 1.6666666666667, 3.3333333333333, 6]
mono[1, 2, 3, 4] --> bern[1, 1.6666666666667, 3.3333333333333, 10]
mono [1, 2, 3, 4] --> bern [1, 1.6666666666667, 3.3333333333333, 10]


Subprogram(4) examples:
Subprogram(4) examples:
p(0.25) = 1
p(0.25) = 1 (mono 1)
p(7.50) = 1
p(7.50) = 1 (mono 1)
q(0.25) = 1.6875
q(0.25) = 1.6875 (mono 1.6875)
q(7.50) = 184.75
q(7.50) = 184.75 (mono 184.75)
r(0.25) = 1.75
r(0.25) = 1.75 (mono 1.75)
r(7.50) = 1872.25
r(7.50) = 1872.25 (mono 1872.25)


Subprogram(5) examples:
Subprogram(5) examples:
mono[1, 1, 1] --> bern[1, 1, 1, 1]
mono [1, 1, 1] --> bern [1, 1, 1, 1]
mono[1, 2, 6] --> bern[1, 1.6666666666667, 3.3333333333333, 6]
mono [1, 2, 6] --> bern [1, 1.6666666666667, 3.3333333333333, 6]
</pre>
</pre>