Bernstein basis polynomials: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: missing space, qualify "mid") |
(Added FreeBasic) |
||
Line 714: | Line 714: | ||
bern (1.000000, 1.000000, 1.000000) --> bern (1.000000, 1.000000, 1.000000, 1.000000) |
bern (1.000000, 1.000000, 1.000000) --> bern (1.000000, 1.000000, 1.000000, 1.000000) |
||
bern (1.000000, 2.000000, 6.000000) --> bern (1.000000, 1.666667, 3.333333, 6.000000)</pre> |
bern (1.000000, 2.000000, 6.000000) --> bern (1.000000, 1.666667, 3.333333, 6.000000)</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|ALGOL 60}} |
|||
<syntaxhighlight lang="vb">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</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Subprogram (1) examples: |
|||
mono (1.00, 0.00, 0.00) --> bern (1.00, 1.00, 1.00) |
|||
mono (1.00, 2.00, 3.00) --> bern (1.00, 2.00, 6.00) |
|||
Subprogram (2) examples: |
|||
p (0.25) = 1.00 |
|||
p (7.50) = 1.00 |
|||
q (0.25) = 1.69 |
|||
q (7.50) = 184.75 |
|||
Subprogram (3) examples: |
|||
mono (1.00, 0.00, 0.00, 0.00) --> bern (1.00, 1.00, 1.00, 1.00) |
|||
mono (1.00, 2.00, 3.00, 0.00) --> bern (1.00, 1.67, 3.33, 6.00) |
|||
mono (1.00, 2.00, 3.00, 4.00) --> bern (1.00, 1.67, 3.33, 10.00) |
|||
Subprogram (4) examples: |
|||
p (0.25) = 1.00 |
|||
p (7.50) = 1.00 |
|||
q (0.25) = 1.69 |
|||
q (7.50) = 184.75 |
|||
r (0.25) = 1.75 |
|||
r (7.50) = 1872.25 |
|||
Subprogram (5) examples: |
|||
bern (1.00, 1.00, 1.00) --> bern (1.00, 1.00, 1.00, 1.00) |
|||
bern (1.00, 2.00, 6.00) --> bern (1.00, 1.67, 3.33, 6.00)</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |