Bernstein basis polynomials: Difference between revisions

Content added Content deleted
(Added ALGOL 60 in what hopefully is the reference language. In lieu of pseudocode.)
Line 20: Line 20:


[[#ALGOL_60|ALGOL 60]] and [[#Python|Python]] implementations are provided as initial examples. The latter does the optional monomial-basis evaluations.
[[#ALGOL_60|ALGOL 60]] and [[#Python|Python]] implementations are provided as initial examples. The latter does the optional monomial-basis evaluations.

Here is ALGOL 60 more nicely printed:
'''procedure''' ''tobern2'' (''a0'', ''a1'', ''a2'', ''b0'', ''b1'', ''b2'');
'''value''' ''a0'', ''a1'', ''a2''; '''comment''' pass by value;
'''real''' ''a0'', ''a1'', ''a2'';
'''real''' ''b0'', ''b1'', ''b2'';
'''comment''' ''Subprogram (1)'': transform monomial coefficients ''a0'', ''a1'', ''a2'' to Bernstein coefficients ''b0'', ''b1'', ''b2'';
'''begin'''
''b0'' := ''a0'';
''b1'' := ''a0'' + ((1/2) × ''a1'');
''b2'' := ''a0'' + ''a1'' + ''a2''
'''end''' ''tobern2'';

'''real''' '''procedure''' ''evalbern2'' (''b0'', ''b1'', ''b2'', ''t'');
'''value''' ''b0'', ''b1'', ''b2'', ''t'';
'''real''' ''b0'', ''b1'', ''b2'', ''t'';
'''comment''' ''Subprogram (2)'': evaluate, at ''t'', the polynomial with Bernstein coefficients ''b0'', ''b1'', ''b2''. Use de Casteljau’s algorithm;
'''begin'''
'''real''' s, ''b01'', ''b12'', ''b012'';
''s'' := 1 - ''t'';
''b01'' := (''s'' × ''b0'') + (''t'' × ''b1'');
''b12'' := (''s'' × ''b1'') + (''t'' × ''b2'');
''b012'' := (''s'' × ''b01'') + (''t'' × ''b12'');
''evalbern2'' := ''b012''
'''end''' ''evalbern2'';

'''procedure''' ''tobern3'' (''a0'', ''a1'', ''a2'', ''a3'', ''b0'', ''b1'', ''b2'', ''b3'');
'''value ''a0'', ''a1'', ''a2'', ''a3'';
'''real''' ''a0'', ''a1'', ''a2'', ''a3'';
'''real''' ''b0'', ''b1'', ''b2'', ''b3'';
'''comment''' ''Subprogram (3)'': transform monomial coefficients ''a0'', ''a1'', ''a2'', ''a3'' to Bernstein coefficients ''b0'', ''b1'', ''b2'', ''b3'';
'''begin'''
''b0'' := ''a0'';
''b1'' := ''a0'' + ((1/3) × ''a1'');
''b2'' := ''a0'' + ((2/3) × ''a1'') + ((1/3) × ''a2'');
''b3'' := ''a0'' + ''a1'' + ''a2'' + ''a3''
'''end''' ''tobern3'';

'''real''' '''procedure''' ''evalbern3'' (''b0'', ''b1'', ''b2'', ''b3'', ''t'');
'''value''' ''b0'', ''b1'', ''b2'', ''b3'', ''t'';
'''real''' ''b0'', ''b1'', ''b2'', ''b3'', ''t'';
'''comment''' ''Subprogram (4)'': evaluate, at ''t'', the polynomial with Bernstein coefficients ''b0'', ''b1'', ''b2'', ''b3''. Use de Casteljau's algorithm;
'''begin'''
'''real''' ''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'');
''evalbern3'' := ''b0123''
'''end''' ''evalbern3'';

'''procedure''' bern2to3 (''q0'', ''q1'', ''q2'', ''c0'', ''c1'', ''c2'', ''c3'');
'''value''' ''q0'', ''q1'', ''q2'';
'''real''' ''q0'', ''q1'', ''q2;
'''real''' ''c0'', ''c1'', ''c2'', ''c3'';
'''comment''' ''Subprogram (5)'': transform the quadratic Bernstein coefficients ''q0'', ''q1'', ''q2'' to the cubic Bernstein coefficients ''c0'', ''c1'', ''c2'', ''c3'';
'''begin'''
''c0'' := ''q0'';
''c1'' := ((1/3) × ''q0'') + ((2/3) × ''q1'');
''c2'' := ((2/3) × ''q1'') + ((1/3) × ''q2'');
''c3'' := ''q2''
'''end''' ''bern2to3'';


=={{header|ALGOL 60}}==
=={{header|ALGOL 60}}==