/usr/share/tcltk/tcllib1.14/math/polynomials.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | # polynomials.tcl --
# Implement procedures to deal with polynomial functions
#
namespace eval ::math::polynomials {
variable count 0 ;# Count the number of specific commands
namespace eval v {}
namespace export polynomial polynCmd evalPolyn \
degreePolyn coeffPolyn allCoeffsPolyn \
derivPolyn primitivePolyn \
addPolyn subPolyn multPolyn \
divPolyn remainderPolyn
}
# polynomial --
# Return a polynomial definition
#
# Arguments:
# coeffs The coefficients of the polynomial
# Result:
# Polynomial definition
#
proc ::math::polynomials::polynomial {coeffs} {
set rev_coeffs {}
set degree -1
set index 0
foreach coeff $coeffs {
if { ! [string is double -strict $coeff] } {
return -code error "Coefficients must be real numbers"
}
set rev_coeffs [concat $coeff $rev_coeffs]
if { $coeff != 0.0 } {
set degree $index
}
incr index
}
#
# The leading coefficient must be non-zero
#
return [list POLYNOMIAL [lrange $rev_coeffs end-$degree end]]
}
# polynCmd --
# Return a procedure that implements a polynomial evaluation
#
# Arguments:
# coeffs The coefficients of the polynomial (or a definition)
# Result:
# New procedure
#
proc ::math::polynomials::polynCmd {coeffs} {
variable count
if { [lindex $coeffs 0] == "POLYNOMIAL" } {
set coeffs [allCoeffsPolyn $coeffs]
}
set degree [expr {[llength $coeffs]-1}]
set body "expr \{[join $coeffs +\$x*(][string repeat ) $degree]\}"
incr count
set name "::math::polynomials::v::POLYN$count"
proc $name {x} $body
return $name
}
# evalPolyn --
# Evaluate a polynomial at a given coordinate
#
# Arguments:
# polyn Polynomial definition
# x Coordinate
# Result:
# Value at x
#
proc ::math::polynomials::evalPolyn {polyn x} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
if { ! [string is double $x] } {
return -code error "Coordinate must be a real number"
}
set result 0.0
foreach c [lindex $polyn 1] {
set result [expr {$result*$x+$c}]
}
return $result
}
# degreePolyn --
# Return the degree of the polynomial
#
# Arguments:
# polyn Polynomial definition
# Result:
# The degree
#
proc ::math::polynomials::degreePolyn {polyn} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
return [expr {[llength [lindex $polyn 1]]-1}]
}
# coeffPolyn --
# Return the coefficient of the index'th degree of the polynomial
#
# Arguments:
# polyn Polynomial definition
# index Degree for which to return the coefficient
# Result:
# The coefficient of degree "index"
#
proc ::math::polynomials::coeffPolyn {polyn index} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
set coeffs [lindex $polyn 1]
if { $index < 0 || $index > [llength $coeffs] } {
return -code error "Index must be between 0 and [llength $coeffs]"
}
return [lindex $coeffs end-$index]
}
# allCoeffsPolyn --
# Return the coefficients of the polynomial
#
# Arguments:
# polyn Polynomial definition
# Result:
# The coefficients in ascending order
#
proc ::math::polynomials::allCoeffsPolyn {polyn} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
set rev_coeffs [lindex $polyn 1]
set coeffs {}
foreach c $rev_coeffs {
set coeffs [concat $c $coeffs]
}
return $coeffs
}
# derivPolyn --
# Return the derivative of the polynomial
#
# Arguments:
# polyn Polynomial definition
# Result:
# The new polynomial
#
proc ::math::polynomials::derivPolyn {polyn} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
set coeffs [lindex $polyn 1]
set new_coeffs {}
set idx [degreePolyn $polyn]
foreach c [lrange $coeffs 0 end-1] {
lappend new_coeffs [expr {$idx*$c}]
incr idx -1
}
return [list POLYNOMIAL $new_coeffs]
}
# primitivePolyn --
# Return the primitive of the polynomial
#
# Arguments:
# polyn Polynomial definition
# Result:
# The new polynomial
#
proc ::math::polynomials::primitivePolyn {polyn} {
if { [lindex $polyn 0] != "POLYNOMIAL" } {
return -code error "Not a polynomial"
}
set coeffs [lindex $polyn 1]
set new_coeffs {}
set idx [llength $coeffs]
foreach c [lrange $coeffs 0 end] {
lappend new_coeffs [expr {$c/double($idx)}]
incr idx -1
}
return [list POLYNOMIAL [concat $new_coeffs 0.0]]
}
# addPolyn --
# Add two polynomials and return the result
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The sum of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::addPolyn {polyn1 polyn2} {
if { [llength $polyn1] == 1 && [string is double -strict $polyn1] } {
set polyn1 [polynomial $polyn1]
}
if { [llength $polyn2] == 1 && [string is double -strict $polyn2] } {
set polyn2 [polynomial $polyn2]
}
if { [lindex $polyn1 0] != "POLYNOMIAL" ||
[lindex $polyn2 0] != "POLYNOMIAL" } {
return -code error "Both arguments must be polynomials or a real number"
}
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
set extra1 [expr {[llength $coeffs2]-[llength $coeffs1]}]
while { $extra1 > 0 } {
set coeffs1 [concat 0.0 $coeffs1]
incr extra1 -1
}
set extra2 [expr {[llength $coeffs1]-[llength $coeffs2]}]
while { $extra2 > 0 } {
set coeffs2 [concat 0.0 $coeffs2]
incr extra2 -1
}
set new_coeffs {}
foreach c1 $coeffs1 c2 $coeffs2 {
lappend new_coeffs [expr {$c1+$c2}]
}
while { [lindex $new_coeffs 0] == 0.0 } {
set new_coeffs [lrange $new_coeffs 1 end]
}
return [list POLYNOMIAL $new_coeffs]
}
# subPolyn --
# Subtract two polynomials and return the result
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The difference of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::subPolyn {polyn1 polyn2} {
if { [llength $polyn1] == 1 && [string is double -strict $polyn1] } {
set polyn1 [polynomial $polyn1]
}
if { [llength $polyn2] == 1 && [string is double -strict $polyn2] } {
set polyn2 [polynomial $polyn2]
}
if { [lindex $polyn1 0] != "POLYNOMIAL" ||
[lindex $polyn2 0] != "POLYNOMIAL" } {
return -code error "Both arguments must be polynomials or a real number"
}
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
set extra1 [expr {[llength $coeffs2]-[llength $coeffs1]}]
while { $extra1 > 0 } {
set coeffs1 [concat 0.0 $coeffs1]
incr extra1 -1
}
set extra2 [expr {[llength $coeffs1]-[llength $coeffs2]}]
while { $extra2 > 0 } {
set coeffs2 [concat 0.0 $coeffs2]
incr extra2 -1
}
set new_coeffs {}
foreach c1 $coeffs1 c2 $coeffs2 {
lappend new_coeffs [expr {$c1-$c2}]
}
while { [lindex $new_coeffs 0] == 0.0 } {
set new_coeffs [lrange $new_coeffs 1 end]
}
return [list POLYNOMIAL $new_coeffs]
}
# multPolyn --
# Multiply two polynomials and return the result
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The difference of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::multPolyn {polyn1 polyn2} {
if { [llength $polyn1] == 1 && [string is double -strict $polyn1] } {
set polyn1 [polynomial $polyn1]
}
if { [llength $polyn2] == 1 && [string is double -strict $polyn2] } {
set polyn2 [polynomial $polyn2]
}
if { [lindex $polyn1 0] != "POLYNOMIAL" ||
[lindex $polyn2 0] != "POLYNOMIAL" } {
return -code error "Both arguments must be polynomials or a real number"
}
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
#
# Take care of the null polynomial
#
if { $coeffs1 == {} || $coeffs2 == {} } {
return [polynomial {}]
}
set zeros {}
foreach c $coeffs1 {
lappend zeros 0.0
}
set new_coeffs [lrange $zeros 1 end]
foreach c $coeffs2 {
lappend new_coeffs 0.0
}
set idx 0
foreach c $coeffs1 {
set term_coeffs {}
foreach c2 $coeffs2 {
lappend term_coeffs [expr {$c*$c2}]
}
set term_coeffs [concat [lrange $zeros 0 [expr {$idx-1}]] \
$term_coeffs \
[lrange $zeros [expr {$idx+1}] end]]
set sum_coeffs {}
foreach t $term_coeffs n $new_coeffs {
lappend sum_coeffs [expr {$t+$n}]
}
set new_coeffs $sum_coeffs
incr idx
}
return [list POLYNOMIAL $new_coeffs]
}
# divPolyn --
# Divide two polynomials and return the quotient
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The difference of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::divPolyn {polyn1 polyn2} {
if { [llength $polyn1] == 1 && [string is double -strict $polyn1] } {
set polyn1 [polynomial $polyn1]
}
if { [llength $polyn2] == 1 && [string is double -strict $polyn2] } {
set polyn2 [polynomial $polyn2]
}
if { [lindex $polyn1 0] != "POLYNOMIAL" ||
[lindex $polyn2 0] != "POLYNOMIAL" } {
return -code error "Both arguments must be polynomials or a real number"
}
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
#
# Take care of the null polynomial
#
if { $coeffs1 == {} } {
return [polynomial {}]
}
if { $coeffs2 == {} } {
return -code error "Denominator can not be zero"
}
foreach {quotient remainder} [DivRemPolyn $polyn1 $polyn2] {break}
return $quotient
}
# remainderPolyn --
# Divide two polynomials and return the remainder
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The difference of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::remainderPolyn {polyn1 polyn2} {
if { [llength $polyn1] == 1 && [string is double -strict $polyn1] } {
set polyn1 [polynomial $polyn1]
}
if { [llength $polyn2] == 1 && [string is double -strict $polyn2] } {
set polyn2 [polynomial $polyn2]
}
if { [lindex $polyn1 0] != "POLYNOMIAL" ||
[lindex $polyn2 0] != "POLYNOMIAL" } {
return -code error "Both arguments must be polynomials or a real number"
}
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
#
# Take care of the null polynomial
#
if { $coeffs1 == {} } {
return [polynomial {}]
}
if { $coeffs2 == {} } {
return -code error "Denominator can not be zero"
}
foreach {quotient remainder} [DivRemPolyn $polyn1 $polyn2] {break}
return $remainder
}
# DivRemPolyn --
# Divide two polynomials and return the quotient and remainder
#
# Arguments:
# polyn1 First polynomial or a scalar
# polyn2 Second polynomial or a scalar
# Result:
# The difference of the two polynomials
# Note:
# Make sure that the first coefficient is not zero
#
proc ::math::polynomials::DivRemPolyn {polyn1 polyn2} {
set coeffs1 [lindex $polyn1 1]
set coeffs2 [lindex $polyn2 1]
set steps [expr { [degreePolyn $polyn1] - [degreePolyn $polyn2] + 1 }]
#
# Special case: polynomial 1 has lower degree than polynomial 2
#
if { $steps <= 0 } {
return [list [polynomial 0.0] $polyn1]
} else {
set extra_coeffs {}
for { set i 1 } { $i < $steps } { incr i } {
lappend extra_coeffs 0.0
}
lappend extra_coeffs 1.0
}
set c2 [lindex $coeffs2 0]
set quot_coeffs {}
for { set i 0 } { $i < $steps } { incr i } {
set c1 [lindex $coeffs1 0]
set factor [expr {$c1/$c2}]
set fpolyn [multPolyn $polyn2 \
[polynomial [lrange $extra_coeffs $i end]]]
set newpol [subPolyn $polyn1 [multPolyn $fpolyn $factor]]
#
# Due to rounding errors, a very small, parasitical
# term may still exist. Remove it
#
if { [degreePolyn $newpol] == [degreePolyn $polyn1] } {
set new_coeffs [lrange [allCoeffsPolyn $newpol] 0 end-1]
set newpol [polynomial $new_coeffs]
}
set polyn1 $newpol
set coeffs1 [lindex $polyn1 1]
set quot_coeffs [concat $factor $quot_coeffs]
}
set quotient [polynomial $quot_coeffs]
return [list $quotient $polyn1]
}
#
# Announce our presence
#
package provide math::polynomials 1.0.1
# some tests --
#
if { 0 } {
set prec $::tcl_precision
if {![package vsatisfies [package provide Tcl] 8.5]} {
set ::tcl_precision 17
} else {
set ::tcl_precision 0
}
set f1 [::math::polynomials::polynomial {1 2 3}]
set f2 [::math::polynomials::polynomial {1 2 3 0}]
set f3 [::math::polynomials::polynomial {0 0 0 0}]
set f4 [::math::polynomials::polynomial {5 7}]
set cmdf1 [::math::polynomials::polynCmd {1 2 3}]
foreach x {0 1 2 3 4 5} {
puts "[::math::polynomials::evalPolyn $f1 $x] -- \
[expr {1.0+2.0*$x+3.0*$x*$x}] -- \
[$cmdf1 $x] -- [::math::polynomials::evalPolyn $f3 $x]"
}
puts "Degree: [::math::polynomials::degreePolyn $f1] (expected: 2)"
puts "Degree: [::math::polynomials::degreePolyn $f2] (expected: 2)"
foreach d {0 1 2} {
puts "Coefficient $d = [::math::polynomials::coeffPolyn $f2 $d]"
}
puts "All coefficients = [::math::polynomials::allCoeffsPolyn $f2]"
puts "Derivative = [::math::polynomials::derivPolyn $f1]"
puts "Primitive = [::math::polynomials::primitivePolyn $f1]"
puts "Add: [::math::polynomials::addPolyn $f1 $f4]"
puts "Add: [::math::polynomials::addPolyn $f4 $f1]"
puts "Subtract: [::math::polynomials::subPolyn $f1 $f4]"
puts "Multiply: [::math::polynomials::multPolyn $f1 $f4]"
set f1 [::math::polynomials::polynomial {1 2 3}]
set f2 [::math::polynomials::polynomial {0 1}]
puts "Divide: [::math::polynomials::divPolyn $f1 $f2]"
puts "Remainder: [::math::polynomials::remainderPolyn $f1 $f2]"
set f1 [::math::polynomials::polynomial {1 2 3}]
set f2 [::math::polynomials::polynomial {1 1}]
puts "Divide: [::math::polynomials::divPolyn $f1 $f2]"
puts "Remainder: [::math::polynomials::remainderPolyn $f1 $f2]"
set f1 [::math::polynomials::polynomial {1 2 3}]
set f2 [::math::polynomials::polynomial {0 1}]
set f3 [::math::polynomials::divPolyn $f2 $f1]
set coeffs [::math::polynomials::allCoeffsPolyn $f3]
puts "Coefficients: $coeffs"
set f3 [::math::polynomials::divPolyn $f1 $f2]
set coeffs [::math::polynomials::allCoeffsPolyn $f3]
puts "Coefficients: $coeffs"
set f1 [::math::polynomials::polynomial {1 2 3}]
set f2 [::math::polynomials::polynomial {0}]
set f3 [::math::polynomials::divPolyn $f2 $f1]
set coeffs [::math::polynomials::allCoeffsPolyn $f3]
puts "Coefficients: $coeffs"
set ::tcl_precision $prec
}
|