-
Notifications
You must be signed in to change notification settings - Fork 1
/
bernstein_poly_01.py
200 lines (174 loc) · 4.36 KB
/
bernstein_poly_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#! /usr/bin/env python
#
def bernstein_poly_01 ( n, x ):
#*****************************************************************************80
#
## BERNSTEIN_POLY_01 evaluates the Bernstein polynomials defined on [0,1].
#
# Discussion:
#
# The Bernstein polynomials are assumed to be based on [0,1].
#
# Formula:
#
# B(N,I)(X) = [N!/(I!*(N-I)!)] * (1-X)^(N-I) * X^I
#
# First values:
#
# B(0,0)(X) = 1
#
# B(1,0)(X) = 1-X
# B(1,1)(X) = X
#
# B(2,0)(X) = (1-X)^2
# B(2,1)(X) = 2 * (1-X) * X
# B(2,2)(X) = X^2
#
# B(3,0)(X) = (1-X)^3
# B(3,1)(X) = 3 * (1-X)^2 * X
# B(3,2)(X) = 3 * (1-X) * X^2
# B(3,3)(X) = X^3
#
# B(4,0)(X) = (1-X)^4
# B(4,1)(X) = 4 * (1-X)^3 * X
# B(4,2)(X) = 6 * (1-X)^2 * X^2
# B(4,3)(X) = 4 * (1-X) * X^3
# B(4,4)(X) = X^4
#
# Special values:
#
# B(N,I)(X) has a unique maximum value at X = I/N.
#
# B(N,I)(X) has an I-fold zero at 0 and and N-I fold zero at 1.
#
# B(N,I)(1/2) = C(N,K) / 2^N
#
# For a fixed X and N, the polynomials add up to 1:
#
# Sum ( 0 <= I <= N ) B(N,I)(X) = 1
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 03 December 2015
#
# Author:
#
# John Burkardt
#
# Parameters:
#
# Input, integer N, the degree of the Bernstein polynomials to be
# used. For any N, there is a set of N+1 Bernstein polynomials,
# each of degree N, which form a basis for polynomials on [0,1].
#
# Input, real X, the evaluation point.
#
# Output, real B(1:N+1), the values of the N+1 Bernstein polynomials at X.
#
import numpy as np
b = np.zeros ( n + 1 )
if ( n == 0 ):
b[0] = 1.0
elif ( 0 < n ):
b[0] = 1.0 - x
b[1] = x
for i in range ( 2, n + 1 ):
b[i] = x * b[i-1]
for j in range ( i - 1, 0, -1 ):
b[j] = x * b[j-1] + ( 1.0 - x ) * b[j]
b[0] = ( 1.0 - x ) * b[0]
return b
def bernstein_poly_01_test ( ):
#*****************************************************************************80
#
## BERNSTEIN_POLY_01_TEST tests BERNSTEIN_POLY_01.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 03 December 2015
#
# Author:
#
# John Burkardt
#
import platform
from bernstein_poly_01_values import bernstein_poly_01_values
print ( '' )
print ( 'BERNSTEIN_POLY_01_TEST:' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' BERNSTEIN_POLY_01 evaluates Bernstein polynomials.' )
print ( '' )
print ( ' N K X F F' )
print ( ' tabulated computed' )
print ( '' )
n_data = 0
while ( True ):
n_data, n, k, x, f1 = bernstein_poly_01_values ( n_data )
if ( n_data == 0 ):
break
f = bernstein_poly_01 ( n, x )
f2 = f[k]
print ( ' %6d %6d %12f %24.16g %24.16g' % ( n, k, x, f1, f2 ) )
#
# Terminate.
#
print ( '' )
print ( 'BERNSTEIN_POLY_01_TEST:' )
print ( ' Normal end of execution.' )
return
def bernstein_poly_01_test2 ( ):
#*****************************************************************************80
#
## BERNSTEIN_POLY_01_TEST2 tests the Partition-of-Unity property.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 03 December 2015
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
from r8_uniform_01 import r8_uniform_01
print ( '' )
print ( 'BERNSTEIN_POLY_01_TEST2:' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' BERNSTEIN_POLY_01 evaluates the Bernstein polynomials' )
print ( ' based on the interval [0,1].' )
print ( '' )
print ( ' Here we test the partition of unity property.' )
print ( '' )
print ( ' N X Sum ( 0 <= K <= N ) BP01(N,K)(X)' )
print ( '' )
seed = 123456789
for n in range ( 0, 11 ):
x, seed = r8_uniform_01 ( seed )
bvec = bernstein_poly_01 ( n, x )
print ( ' %4d %7.4f %14.6g' % ( n, x, np.sum ( bvec ) ) )
#
# Terminate.
#
print ( '' )
print ( 'BERNSTEIN_POLY_01_TEST2:' )
print ( ' Normal end of execution.' )
return
if ( __name__ == '__main__' ):
from timestamp import timestamp
timestamp ( )
bernstein_poly_01_test ( )
bernstein_poly_01_test2 ( )
timestamp ( )