classic_polyns.tcl

Go to the documentation of this file.
00001 /*  classic_polyns.tcl --*/
00002 /*     Implement procedures for the classic orthogonal polynomials*/
00003 /* */
00004 package require math::polynomials
00005 
00006 namespace ::math::special {
00007     if {[info commands addPolyn] == {} } {
00008         namespace import ::math::polynomials::*
00009     }
00010 }
00011 
00012 
00013 /*  legendre --*/
00014 /*     Return the nth degree Legendre polynomial*/
00015 /* */
00016 /*  Arguments:*/
00017 /*     n            The degree of the polynomial*/
00018 /*  Result:*/
00019 /*     Polynomial definition*/
00020 /* */
00021 ret  ::math::special::legendre (type n) {
00022     if { ! [string is integer -strict $n] || $n < 0 } {
00023         return -code error "Degree must be a non-negative integer"
00024     }
00025 
00026     set pnm1   [polynomial 1.0]
00027     set pn     [polynomial {0.0 1.0}]
00028 
00029     if { $n == 0 } {return $pnm1}
00030     if { $n == 1 } {return $pn}
00031 
00032     set degree 1
00033     while { $degree < $n } {
00034         set an       [expr {(2.0*$degree+1.0)/($degree+1.0)}]
00035         set bn       0.0
00036         set cn       [expr {$degree/($degree+1.0)}]
00037         set factor_n [polynomial [list $bn $an]]
00038         set term_nm1 [multPolyn $pnm1 [expr {-1.0*$cn}]]
00039         set term_n   [multPolyn $factor_n $pn]
00040         set pnp1     [addPolyn $term_n $term_nm1]
00041 
00042         set pnm1     $pn
00043         set pn       $pnp1
00044         incr degree
00045     }
00046 
00047     return $pnp1
00048 }
00049 
00050 /*  chebyshev --*/
00051 /*     Return the nth degree Chebeyshev polynomial of the first kind*/
00052 /* */
00053 /*  Arguments:*/
00054 /*     n            The degree of the polynomial*/
00055 /*  Result:*/
00056 /*     Polynomial definition*/
00057 /* */
00058 ret  ::math::special::chebyshev (type n) {
00059     if { ! [string is integer -strict $n] || $n < 0 } {
00060         return -code error "Degree must be a non-negative integer"
00061     }
00062 
00063     set pnm1   [polynomial 1.0]
00064     set pn     [polynomial {0.0 1.0}]
00065 
00066     if { $n == 0 } {return $pnm1}
00067     if { $n == 1 } {return $pn}
00068 
00069     set degree 1
00070     while { $degree < $n } {
00071         set an       2.0
00072         set bn       0.0
00073         set cn       1.0
00074         set factor_n [polynomial [list $bn $an]]
00075         set term_nm1 [multPolyn $pnm1 [expr {-1.0*$cn}]]
00076         set term_n   [multPolyn $factor_n $pn]
00077         set pnp1     [addPolyn $term_n $term_nm1]
00078 
00079         set pnm1     $pn
00080         set pn       $pnp1
00081         incr degree
00082     }
00083 
00084     return $pnp1
00085 }
00086 
00087 /*  laguerre --*/
00088 /*     Return the nth degree Laguerre polynomial with parameter alpha*/
00089 /* */
00090 /*  Arguments:*/
00091 /*     alpha        The parameter for the polynomial*/
00092 /*     n            The degree of the polynomial*/
00093 /*  Result:*/
00094 /*     Polynomial definition*/
00095 /* */
00096 ret  ::math::special::laguerre (type alpha , type n) {
00097     if { ! [string is double -strict $alpha] } {
00098         return -code error "Parameter must be a double"
00099     }
00100     if { ! [string is integer -strict $n] || $n < 0 } {
00101         return -code error "Degree must be a non-negative integer"
00102     }
00103 
00104     set pnm1   [polynomial 1.0]
00105     set pn     [polynomial [list [expr {1.0-$alpha}] -1.0]]
00106 
00107     if { $n == 0 } {return $pnm1}
00108     if { $n == 1 } {return $pn}
00109 
00110     set degree 1
00111     while { $degree < $n } {
00112         set an       [expr {-1.0/($degree+1.0)}]
00113         set bn       [expr {(2.0*$degree+$alpha+1)/($degree+1.0)}]
00114         set cn       [expr {($degree+$alpha)/($degree+1.0)}]
00115         set factor_n [polynomial [list $bn $an]]
00116         set term_nm1 [multPolyn $pnm1 [expr {-1.0*$cn}]]
00117         set term_n   [multPolyn $factor_n $pn]
00118         set pnp1     [addPolyn $term_n $term_nm1]
00119 
00120         set pnm1     $pn
00121         set pn       $pnp1
00122         incr degree
00123     }
00124 
00125     return $pnp1
00126 }
00127 
00128 /*  hermite --*/
00129 /*     Return the nth degree Hermite polynomial*/
00130 /* */
00131 /*  Arguments:*/
00132 /*     n            The degree of the polynomial*/
00133 /*  Result:*/
00134 /*     Polynomial definition*/
00135 /* */
00136 ret  ::math::special::hermite (type n) {
00137     if { ! [string is integer -strict $n] || $n < 0 } {
00138         return -code error "Degree must be a non-negative integer"
00139     }
00140 
00141     set pnm1   [polynomial 1.0]
00142     set pn     [polynomial {0.0 2.0}]
00143 
00144     if { $n == 0 } {return $pnm1}
00145     if { $n == 1 } {return $pn}
00146 
00147     set degree 1
00148     while { $degree < $n } {
00149         set an       2.0
00150         set bn       0.0
00151         set cn       [expr {2.0*$degree}]
00152         set factor_n [polynomial [list $bn $an]]
00153         set term_n   [multPolyn $factor_n $pn]
00154         set term_nm1 [multPolyn $pnm1 [expr {-1.0*$cn}]]
00155         set pnp1     [addPolyn $term_n $term_nm1]
00156 
00157         set pnm1     $pn
00158         set pn       $pnp1
00159         incr degree
00160     }
00161 
00162     return $pnp1
00163 }
00164 
00165 /*  some tests --*/
00166 /* */
00167 if { 0 } {
00168  tcl = _precision 17
00169 
00170 puts "Legendre:"
00171 foreach n {0 1 2 3 4} {
00172     puts [::math::special::legendre $n]
00173 }
00174 
00175 puts "Chebyshev:"
00176 foreach n {0 1 2 3 4} {
00177     puts [::math::special::chebyshev $n]
00178 }
00179 
00180 puts "Laguerre (alpha=0):"
00181 foreach n {0 1 2 3 4} {
00182     puts [::math::special::laguerre 0.0 $n]
00183 }
00184 puts "Laguerre (alpha=1):"
00185 foreach n {0 1 2 3 4} {
00186     puts [::math::special::laguerre 1.0 $n]
00187 }
00188 
00189 puts "Hermite:"
00190 foreach n {0 1 2 3 4} {
00191     puts [::math::special::hermite $n]
00192 }
00193 }
00194 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1