File: rational_funcs.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (367 lines) | stat: -rw-r--r-- 11,397 bytes parent folder | download | duplicates (2)
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
# rational_funcs.tcl --
#    Implement procedures to deal with rational functions
#

package require math::polynomials

namespace eval ::math::rationalfunctions {
    variable count 0  ;# Count the number of specific commands
    namespace eval v {}

    namespace export rationalFunction ratioCmd evalRatio \
                     coeffsNumerator coeffsDenominator \
                     derivRatio  \
                     addRatio    subRatio multRatio \
                     divRatio

    namespace import ::math::polynomials::*
}


# rationalFunction --
#    Return a rational function definition
#
# Arguments:
#    num          The coefficients of the numerator
#    den          The coefficients of the denominator
# Result:
#    Rational function definition
#
proc ::math::rationalfunctions::rationalFunction {num den} {

    foreach coeffs [list $num $den] {
        foreach coeff $coeffs {
            if { ! [string is double -strict $coeff] } {
                return -code error "Coefficients must be real numbers"
            }
        }
    }

    #
    # The leading coefficient must be non-zero
    #
    return [list RATIONAL_FUNCTION [polynomial $num] [polynomial $den]]
}

# ratioCmd --
#    Return a procedure that implements a rational function evaluation
#
# Arguments:
#    num          The coefficients of the numerator
#    den          The coefficients of the denominator
# Result:
#    New procedure
#
proc ::math::rationalfunctions::ratioCmd {num {den {}}} {
    variable count

    if { [llength $den] == 0 } {
        if { [lindex $num 0] == "RATIONAL_FUNCTION" } {
            set den [lindex $num 2]
            set num [lindex $num 1]
        }
    }

    set degree1 [expr {[llength $num]-1}]
    set degree2 [expr {[llength $num]-1}]
    set body "expr \{([join $num +\$x*(][string repeat ) $degree1])/\
(double([join $den +\$x*(][string repeat ) $degree2])\}"

    incr count
    set name "::math::rationalfunctions::v::RATIO$count"
    proc $name {x} $body
    return $name
}

# evalRatio --
#    Evaluate a rational function at a given coordinate
#
# Arguments:
#    ratio        Rational function definition
#    x            Coordinate
# Result:
#    Value at x
#
proc ::math::rationalfunctions::evalRatio {ratio x} {
    if { [lindex $ratio 0] != "RATIONAL_FUNCTION" } {
        return -code error "Not a rational function"
    }
    if { ! [string is double $x] } {
        return -code error "Coordinate must be a real number"
    }

    set num 0.0
    foreach c [lindex [lindex $ratio 1] 1] {
        set num [expr {$num*$x+$c}]
    }

    set den 0.0
    foreach c [lindex [lindex $ratio 2] 1] {
        set den [expr {$den*$x+$c}]
    }
    return [expr {$num/double($den)}]
}

# coeffsNumerator --
#    Return the coefficients of the numerator
#
# Arguments:
#    ratio        Rational function definition
# Result:
#    The coefficients in ascending order
#
proc ::math::rationalfunctions::coeffsNumerator {ratio} {
    if { [lindex $ratio 0] != "RATIONAL_FUNCTION" } {
        return -code error "Not a rational function"
    }
    set polyn [lindex $ratio 1]
    return [allCoeffsPolyn $polyn]
}

# coeffsDenominator --
#    Return the coefficients of the denominator
#
# Arguments:
#    ratio        Rational function definition
# Result:
#    The coefficients in ascending order
#
proc ::math::rationalfunctions::coeffsDenominator {ratio} {
    if { [lindex $ratio 0] != "RATIONAL_FUNCTION" } {
        return -code error "Not a rational function"
    }
    set polyn [lindex $ratio 2]
    return [allCoeffsPolyn $polyn]
}

# derivRatio --
#    Return the derivative of the rational function
#
# Arguments:
#    polyn        Polynomial definition
# Result:
#    The new polynomial
#
proc ::math::rationalfunctions::derivRatio {ratio} {
    if { [lindex $ratio 0] != "RATIONAL_FUNCTION" } {
        return -code error "Not a rational function"
    }
    set num_polyn [lindex $ratio 1]
    set den_polyn [lindex $ratio 2]
    set num_deriv [derivPolyn $num_polyn]
    set den_deriv [derivPolyn $den_polyn]
    set num       [subPolyn [multPolyn $num_deriv $den_polyn] \
                            [multPolyn $den_deriv $num_polyn] ]
    set den       [multPolyn $den_polyn $den_polyn]

    return [list RATIONAL_FUNCTION $num $den]
}

# addRatio --
#    Add two rational functions and return the result
#
# Arguments:
#    ratio1       First rational function or a scalar
#    ratio2       Second rational function or a scalar
# Result:
#    The sum of the two functions
# Note:
#    TODO: Check for the same denominator
#
proc ::math::rationalfunctions::addRatio {ratio1 ratio2} {
    if { [llength $ratio1] == 1 && [string is double -strict $ratio1] } {
        set polyn1 [rationalFunction $ratio1 1.0]
    }
    if { [llength $ratio2] == 1 && [string is double -strict $ratio2] } {
        set ratio2 [rationalFunction $ratio1 1.0]
    }
    if { [lindex $ratio1 0] != "RATIONAL_FUNCTION" ||
         [lindex $ratio2 0] != "RATIONAL_FUNCTION" } {
        return -code error "Both arguments must be rational functions or a real number"
    }

    set num1    [lindex $ratio1 1]
    set den1    [lindex $ratio1 2]
    set num2    [lindex $ratio2 1]
    set den2    [lindex $ratio2 2]

    set newnum  [addPolyn [multPolyn $num1 $den2] \
                          [multPolyn $num2 $den1] ]

    set newden  [multPolyn $den1 $den2]

    return [list RATIONAL_FUNCTION $newnum $newden]
}

# subRatio --
#    Subtract two rational functions and return the result
#
# Arguments:
#    ratio1       First rational function or a scalar
#    ratio2       Second rational function or a scalar
# Result:
#    The difference of the two functions
# Note:
#    TODO: Check for the same denominator
#
proc ::math::rationalfunctions::subRatio {ratio1 ratio2} {
    if { [llength $ratio1] == 1 && [string is double -strict $ratio1] } {
        set polyn1 [rationalFunction $ratio1 1.0]
    }
    if { [llength $ratio2] == 1 && [string is double -strict $ratio2] } {
        set ratio2 [rationalFunction $ratio1 1.0]
    }
    if { [lindex $ratio1 0] != "RATIONAL_FUNCTION" ||
         [lindex $ratio2 0] != "RATIONAL_FUNCTION" } {
        return -code error "Both arguments must be rational functions or a real number"
    }

    set num1    [lindex $ratio1 1]
    set den1    [lindex $ratio1 2]
    set num2    [lindex $ratio2 1]
    set den2    [lindex $ratio2 2]

    set newnum  [subPolyn [multPolyn $num1 $den2] \
                          [multPolyn $num2 $den1] ]

    set newden  [multPolyn $den1 $den2]

    return [list RATIONAL_FUNCTION $newnum $newden]
}

# multRatio --
#    Multiply two rational functions and return the result
#
# Arguments:
#    ratio1       First rational function or a scalar
#    ratio2       Second rational function or a scalar
# Result:
#    The product of the two functions
# Note:
#    TODO: Check for the same denominator
#
proc ::math::rationalfunctions::multRatio {ratio1 ratio2} {
    if { [llength $ratio1] == 1 && [string is double -strict $ratio1] } {
        set polyn1 [rationalFunction $ratio1 1.0]
    }
    if { [llength $ratio2] == 1 && [string is double -strict $ratio2] } {
        set ratio2 [rationalFunction $ratio1 1.0]
    }
    if { [lindex $ratio1 0] != "RATIONAL_FUNCTION" ||
         [lindex $ratio2 0] != "RATIONAL_FUNCTION" } {
        return -code error "Both arguments must be rational functions or a real number"
    }

    set num1    [lindex $ratio1 1]
    set den1    [lindex $ratio1 2]
    set num2    [lindex $ratio2 1]
    set den2    [lindex $ratio2 2]

    set newnum  [multPolyn $num1 $num2]
    set newden  [multPolyn $den1 $den2]

    return [list RATIONAL_FUNCTION $newnum $newden]
}

# divRatio --
#    Divide two rational functions and return the result
#
# Arguments:
#    ratio1       First rational function or a scalar
#    ratio2       Second rational function or a scalar
# Result:
#    The quotient of the two functions
# Note:
#    TODO: Check for the same denominator
#
proc ::math::rationalfunctions::divRatio {ratio1 ratio2} {
    if { [llength $ratio1] == 1 && [string is double -strict $ratio1] } {
        set polyn1 [rationalFunction $ratio1 1.0]
    }
    if { [llength $ratio2] == 1 && [string is double -strict $ratio2] } {
        set ratio2 [rationalFunction $ratio1 1.0]
    }
    if { [lindex $ratio1 0] != "RATIONAL_FUNCTION" ||
         [lindex $ratio2 0] != "RATIONAL_FUNCTION" } {
        return -code error "Both arguments must be rational functions or a real number"
    }

    set num1    [lindex $ratio1 1]
    set den1    [lindex $ratio1 2]
    set num2    [lindex $ratio2 1]
    set den2    [lindex $ratio2 2]

    set newnum  [multPolyn $num1 $den2]
    set newden  [multPolyn $num2 $den1]

    return [list RATIONAL_FUNCTION $newnum $newden]
}

#
# Announce our presence
#
package provide math::rationalfunctions 1.0.2

# some tests --
#
if { 0 } {
if {![package vsatisfies [package provide Tcl] 9]} {
    set prec $::tcl_precision
    if {![package vsatisfies [package provide Tcl] 8.5 9]} {
        set ::tcl_precision 17
    } else {
        set ::tcl_precision 0
    }
}

set f1    [::math::rationalfunctions::rationalFunction {1 2 3} {1 4}]
set f2    [::math::rationalfunctions::rationalFunction {1 2 3 0} {1 4}]
set f3    [::math::rationalfunctions::rationalFunction {0 0 0 0} {1}]
set f4    [::math::rationalfunctions::rationalFunction {5 7} {1}]
set cmdf1 [::math::rationalfunctions::ratioCmd {1 2 3} {1 4}]

foreach x {0 1 2 3 4 5} {
    puts "[::math::rationalfunctions::evalRatio $f1 $x] -- \
[expr {(1.0+2.0*$x+3.0*$x*$x)/double(1.0+4.0*$x)}] -- \
[$cmdf1 $x] -- [::math::rationalfunctions::evalRatio $f3 $x]"
}

puts "All coefficients = [::math::rationalfunctions::coeffsNumerator $f2]"
puts "                   [::math::rationalfunctions::coeffsDenominator $f2]"

puts "Derivative = [::math::rationalfunctions::derivRatio $f1]"

puts "Add:       [::math::rationalfunctions::addRatio $f1 $f4]"
puts "Add:       [::math::rationalfunctions::addRatio $f4 $f1]"
puts "Subtract:  [::math::rationalfunctions::subRatio $f1 $f4]"
puts "Multiply:  [::math::rationalfunctions::multRatio $f1 $f4]"

set f1    [::math::rationalfunctions::rationalFunction {1 2 3} 1]
set f2    [::math::rationalfunctions::rationalFunction {0 1} 1]

puts "Divide:    [::math::rationalfunctions::divRatio $f1 $f2]"

set f1    [::math::rationalfunctions::rationalFunction {1 2 3} 1]
set f2    [::math::rationalfunctions::rationalFunction {1 1} {1 2}]

puts "Divide:    [::math::rationalfunctions::divRatio $f1 $f2]"

set f1 [::math::rationalfunctions::rationalFunction {1 2 3} 1]
set f2 [::math::rationalfunctions::rationalFunction {0 1} {0 0 1}]
set f3 [::math::rationalfunctions::divRatio $f2 $f1]
set coeffs [::math::rationalfunctions::coeffsNumerator $f3]
puts "Coefficients: $coeffs"
set f3 [::math::rationalfunctions::divRatio $f1 $f2]
set coeffs [::math::rationalfunctions::coeffsNumerator $f3]
puts "Coefficients: $coeffs"
set f1 [::math::rationalfunctions::rationalFunction {1 2 3} {1 2}]
set f2 [::math::rationalfunctions::rationalFunction {0} {1}]
set f3 [::math::rationalfunctions::divRatio $f2 $f1]
set coeffs [::math::rationalfunctions::coeffsNumerator $f3]
puts "Coefficients: $coeffs"
puts "Eval null function: [::math::rationalfunctions::evalRatio $f2 1]"

if {![package vsatisfies [package provide Tcl] 9]} {
    set ::tcl_precision $prec
}
}