في التحليل العددي ، خوارزمية دوكاستلجو (نسبة إلى مبتكره بول دوكاستلجو )، هو طريقة تكرارية لتقييم كثيرات حدود في صورة بيرنستين أو منحنيات بيزير . يمكن استعمالها أيضاً في فصل منحنى بيزير مفرد إلى منحنيات بيزيرية عند قيمة وسيطة اختيارية.
بالرغم من بطء هذا الخوارزم مقارنة بالطريقة المباشرة إلّا أنه أكثر استقراراً عددياً .
لتكن كثيرة الحدود B في صورة بيرنستين من الدرجة n
B
(
t
)
=
∑
i
=
0
n
β
i
b
i
,
n
(
t
)
,
{\displaystyle B(t)=\sum _{i=0}^{n}\beta _{i}b_{i,n}(t),}
حيث b هي متعددة الحدود لبيرنشتاين أساسية، تكون كثيرة الحدود عن نقطة t 0 قابلة للتقييم بفضل علاقة التكرار.
β
i
(
0
)
:=
β
i
,
i
=
0
,
…
,
n
{\displaystyle \beta _{i}^{(0)}:=\beta _{i}{\mbox{ , ))i=0,\ldots ,n}
β
i
(
j
)
:=
β
i
(
j
−
1
)
(
1
−
t
0
)
+
β
i
+
1
(
j
−
1
)
t
0
,
i
=
0
,
…
,
n
−
j
,
j
=
1
,
…
,
n
{\displaystyle \beta _{i}^{(j)}:=\beta _{i}^{(j-1)}(1-t_{0})+\beta _{i+1}^{(j-1)}t_{0}{\mbox{ , ))i=0,\ldots ,n-j{\mbox{ , ))j=1,\ldots ,n}
حينئذ، يكون تقييم
B
{\displaystyle B}
عند نقطة
t
0
{\displaystyle t_{0))
ممكناً في
n
{\displaystyle n}
خطوة من الخوارزم. وتعطى النتيجة
B
(
t
0
)
{\displaystyle B(t_{0})}
بالعلاقة:
B
(
t
0
)
=
β
0
(
n
)
.
{\displaystyle B(t_{0})=\beta _{0}^{(n)}.}
بالإضافة لذلك، يمكن فصل منحنى بيزيه
B
{\displaystyle B}
عن نقطة
t
0
{\displaystyle t_{0))
إلى منحنيين بنقطتي تحكم متتاليتين:
β
0
(
0
)
,
β
0
(
1
)
,
…
,
β
0
(
n
)
{\displaystyle \beta _{0}^{(0)},\beta _{0}^{(1)},\ldots ,\beta _{0}^{(n)))
β
0
(
n
)
,
β
1
(
n
−
1
)
,
…
,
β
n
(
0
)
{\displaystyle \beta _{0}^{(n)},\beta _{1}^{(n-1)},\ldots ,\beta _{n}^{(0)))
المثال التالي يتضمن خوارزم كاستلجو بلغة هاسكل:
import Data.Array
deCasteljau :: Array Int ( Double , Double ) -> Double -> ( Double , Double )
deCasteljau controls t0 =
coefs ! ( 0 , n )
where
( c0 , n ) = bounds controls
coefs = listArray (( 0 , 0 ),( n , n )) $ map deCasteljau' [( i , j ) | i <- [ 0 .. n ], j <- [ 0 .. n ]]
deCasteljau' ( i , 0 )
| i >= c0 = controls ! i
| otherwise = 0
deCasteljau' ( i , j ) =
let ( x0 , y0 ) = coefs ! ( i , j - 1 )
( x1 , y1 ) = coefs ! ( i + 1 , j - 1 )
in (( 1 - t0 ) * x0 + t0 * x1 , ( 1 - t0 ) * y0 + t0 * y1 )
عند تنفيذ الحسابات يدوياً سيكون من الأجدر تدوين المعاملات في مخطط مثلثي كما يلي
β
0
=
β
0
(
0
)
β
0
(
1
)
β
1
=
β
1
(
0
)
⋱
⋮
⋮
β
0
(
n
)
β
n
−
1
=
β
n
−
1
(
0
)
β
n
−
1
(
1
)
β
n
=
β
n
(
0
)
{\displaystyle {\begin{matrix}\beta _{0}&=\beta _{0}^{(0)}&&&\\&&\beta _{0}^{(1)}&&\\\beta _{1}&=\beta _{1}^{(0)}&&&\\&&&\ddots &\\\vdots &&\vdots &&\beta _{0}^{(n)}\\&&&&\\\beta _{n-1}&=\beta _{n-1}^{(0)}&&&\\&&\beta _{n-1}^{(1)}&&\\\beta _{n}&=\beta _{n}^{(0)}&&&\\\end{matrix))}
عن وقع الاختيار عند نقطة t 0 لتقييم كثيرة حدود بيرنستين، بإمكاننا استعمال قطري المخطط المثلثي لإنشاء قسمة كثيرة الحدود.
B
(
t
)
=
∑
i
=
0
n
β
i
(
0
)
b
i
,
n
(
t
)
,
t
∈
[
0
,
1
]
{\displaystyle B(t)=\sum _{i=0}^{n}\beta _{i}^{(0)}b_{i,n}(t){\mbox{ , ))\qquad t\in [0,1]}
إلى
B
1
(
t
)
=
∑
i
=
0
n
β
0
(
i
)
b
i
,
n
(
t
t
0
)
,
t
∈
[
0
,
t
0
]
{\displaystyle B_{1}(t)=\sum _{i=0}^{n}\beta _{0}^{(i)}b_{i,n}\left({\frac {t}{t_{0))}\right){\mbox{ , ))\qquad t\in [0,t_{0}]}
و
B
2
(
t
)
=
∑
i
=
0
n
β
n
−
i
(
i
)
b
i
,
n
(
t
−
t
0
1
−
t
0
)
,
t
∈
[
t
0
,
1
]
{\displaystyle B_{2}(t)=\sum _{i=0}^{n}\beta _{n-i}^{(i)}b_{i,n}\left({\frac {t-t_{0)){1-t_{0))}\right){\mbox{ , ))\qquad t\in [t_{0},1]}
نرغب بتقييم كثيرة حدود بيرنستين من الدرجة 2 بمعاملات بيرنستين
β
0
(
0
)
=
β
0
{\displaystyle \beta _{0}^{(0)}=\beta _{0))
β
1
(
0
)
=
β
1
{\displaystyle \beta _{1}^{(0)}=\beta _{1))
β
2
(
0
)
=
β
2
{\displaystyle \beta _{2}^{(0)}=\beta _{2))
عند النقطة t 0 .
نستهل المعاودة بـ
β
0
(
1
)
=
β
0
(
0
)
(
1
−
t
0
)
+
β
1
(
0
)
t
0
=
β
0
(
1
−
t
0
)
+
β
1
t
0
{\displaystyle \beta _{0}^{(1)}=\beta _{0}^{(0)}(1-t_{0})+\beta _{1}^{(0)}t_{0}=\beta _{0}(1-t_{0})+\beta _{1}t_{0))
β
1
(
1
)
=
β
1
(
0
)
(
1
−
t
0
)
+
β
2
(
0
)
t
0
=
β
1
(
1
−
t
0
)
+
β
2
t
0
{\displaystyle \beta _{1}^{(1)}=\beta _{1}^{(0)}(1-t_{0})+\beta _{2}^{(0)}t_{0}=\beta _{1}(1-t_{0})+\beta _{2}t_{0))
وبالتكرار الثاني يتوقف الاستدعاء الذاتي مع
β
0
(
2
)
=
β
0
(
1
)
(
1
−
t
0
)
+
β
1
(
1
)
t
0
=
β
0
(
1
−
t
0
)
(
1
−
t
0
)
+
β
1
t
0
(
1
−
t
0
)
+
β
1
(
1
−
t
0
)
t
0
+
β
2
t
0
t
0
=
β
0
(
1
−
t
0
)
2
+
β
1
2
t
0
(
1
−
t
0
)
+
β
2
t
0
2
{\displaystyle {\begin{aligned}\beta _{0}^{(2)}&=\beta _{0}^{(1)}(1-t_{0})+\beta _{1}^{(1)}t_{0}\\\ &=\beta _{0}(1-t_{0})(1-t_{0})+\beta _{1}t_{0}(1-t_{0})+\beta _{1}(1-t_{0})t_{0}+\beta _{2}t_{0}t_{0}\\\ &=\beta _{0}(1-t_{0})^{2}+\beta _{1}2t_{0}(1-t_{0})+\beta _{2}t_{0}^{2}\end{aligned))}
وهي كثيرة الحدود المتوقعة من الدرجة n .