Bernstein basis polynomials: Difference between revisions
→{{header|ALGOL 68}}: Simplify a little using features of Algol 68 not in Algol 60
(Added Go) |
(→{{header|ALGOL 68}}: Simplify a little using features of Algol 68 not in Algol 60) |
||
Line 337:
# Note the "Subrptoghram (n) designations are as specified in the #
# task #
MODE BERNTWO = STRUCT( STRING label, REAL b0, b1, b2 );
MODE BERNTHREE = STRUCT( STRING label, REAL b0, b1, b2, b3 );
PRIO EVAL = 5; # set the priority of the dyadic EVAL #
# Subprogram (1): transform monomial coefficients #
# a0, a1, a2 to Bernstein coefficients b0, b1, b2 #
PROC tobern2 = ( REAL a0, a1, a2, REF
BEGIN
b0 OF b := a0;
b1 OF b := a0 + ((1/2) * a1);
b2 OF b := a0 + a1 + a2
END # tobern2 # ;
Line 350 ⟶ 355:
# Bernstein coefficients b0, b1, b2. Use de Casteljau's #
# algorithm #
BEGIN
REAL s = 1 - t;
REAL b01 = (s * b0 OF b) + (t * b1 OF b);
REAL b12 = (s * b1 OF b) + (t * b2 OF b);
(s * b01) + (t * b12)
END #
# Subprogram (3): transform monomial coefficients #
# a0, a1, a2, a3 to Bernstein coefficients b0, b1, #
# b2, b3 #
PROC tobern3 = ( REAL a0, a1, a2, a3, REF
BEGIN
b0 OF b := a0;
b1 OF b := a0 + ((1/3) * a1);
b2 OF b := a0 + ((2/3) * a1) + ((1/3) * a2);
b3 OF b := a0 + a1 + a2 + a3
END # tobern3 # ;
Line 372 ⟶ 377:
# with Bernstein coefficients b0, b1, b2, b3. Use #
# de Casteljau's algorithm #
BEGIN
REAL s = 1 - t;
REAL b01 = (s * b0 OF b) + (t * b1 OF b);
REAL b12 = (s * b1 OF b) + (t * b2 OF b);
REAL b23 = (s * b2 OF b) + (t * b3 OF b);
REAL b012 = (s * b01) + (t * b12);
REAL b123 = (s * b12) + (t * b23);
(s * b012) + (t * b123)
END #
# Subprogram (5): transform the quadratic Bernstein #
# coefficients q0, q1, q2 to the cubic Bernstein #
# coefficients c0, c1, c2, c3; #
PROC bern2to3 = (
BEGIN
c0 :=
c1 := ((1/3) *
c2 := ((2/3) *
c3 :=
END # bern2to3 # ;
Line 414 ⟶ 419:
END # f # ;
OP SHOWEVAL = ( BERNTWO p, REAL x )VOID:
BERNTHREE p3 := BERNTHREE( "p", 0, 0, 0, 0 );
BERNTHREE q3 := BERNTHREE( "q", 0, 0, 0, 0 );
BERNTHREE r3 := BERNTHREE( "r", 0, 0, 0, 0 );
REAL pc0 := 0, pc1 := 0, pc2 := 0, pc3 := 0;
REAL qc0 := 0, qc1 := 0, qc2 := 0, qc3 := 0;
▲ REAL x, y;
REAL p0m = 1, p1m = 0, p2m = 0;
Line 430 ⟶ 441:
REAL r0m = 1, r1m = 2, r2m = 3, r3m = 4;
tobern2( p0m, p1m, p2m,
tobern2( q0m, q1m, q2m,
print( ( "Subprogram (1) examples:", newline ) );
print( ( " mono ( ", f( p0m ), ", ", f( p1m ), ", ", f( p2m )
, " ) --> bern ( ", f(
, " )", newline
)
);
print( ( " mono ( ", f( q0m ), ", ", f( q1m ), ", ", f( q2m )
, " ) --> bern ( ", f(
, " )", newline
)
Line 445 ⟶ 456:
print( ( "Subprogram (2) examples:", newline ) );
▲ print( ( " p ( ", f( x ), " ) = ", f( y ), newline ) );
▲ y := evalbern2( p0b2, p1b2, p2b2, x );
▲ x := 0.25;
▲ y := evalbern2( q0b2, q1b2, q2b2, x );
x := 7.50;▼
tobern3( p0m, p1m, p2m, 0,
tobern3( q0m, q1m, q2m, 0,
tobern3( r0m, r1m, r2m, r3m,
print( ( "Subprogram (3) examples:", newline ) );
print( ( " mono ( ", f( p0m ), ", ", f( p1m ), ", ", f( p2m ), ", ", f( 0 )
, " ) --> bern ( ", f(
, " )", newline
)
);
print( ( " mono ( ", f( q0m ), ", ", f( q1m ), ", ", f( q2m ), ", ", f( 0 )
, " ) --> bern ( ", f(
, " )", newline
)
);
print( ( " mono ( ", f( r0m ), ", ", f( r1m ), ", ", f( r2m ), ", ", f( r3m )
, " ) --> bern ( ", f(
, " )", newline
)
Line 479 ⟶ 482:
print( ( "Subprogram (4) examples:", newline ) );
▲ x := 0.25;
▲ x := 7.50;
▲ x := 0.25;
bern2to3(
bern2to3(
print( ( "Subprogram (5) examples:", newline ) );
print( ( " bern ( ", f(
, " ) --> bern ( ", f( pc0 ), ", ", f( pc1 ), ", ", f( pc2 ), ", ", f( pc3 )
, " )", newline
)
);
print( ( " bern ( ", f(
, " ) --> bern ( ", f( qc0 ), ", ", f( qc1 ), ", ", f( qc2 ), ", ", f( qc3 )
, " )", newline
|