## Experiment with numerical methods and XOTcl

Arjen Markus (24 october 2008) As follow-up to my experiments with Snit, here is one with XOTcl. See the comp.lang.tcl thread referred to in the code for some background information.

```# wrap_integral.tcl --
#     Small experiment with XOTcl: wrapping some of the
#     numerical functions in Tcllib, to get a more object-oriented
#     approach.
#
#

package require XOTcl
package require math::calculus

# integrable --
#     Define a simple class that allows you integrate a function (f)
#     For individual objects, the function f must be defined separately
#
::xotcl::Class integrable
integrable method integral {a b} {
#
# Romberg returns the value and an error estimate. Just return the
# value of the integral.
#
lindex [::math::calculus::romberg [list [::xotcl::self] f] \$a \$b] 0
}

# main --
#     Test this very basic type:
#     - Create a few objects each with its own function f
#
integrable obj1
integrable obj2
integrable obj3 ;# No function f for this one!

obj1 proc f {x} {expr {\$x}}
obj2 proc f {x} {expr {\$x*exp(-\$x)}}

foreach {a b} {1 2 3 4 5 6} {
puts "\$a -- \$b: [obj1 integral \$a \$b] [obj2 integral \$a \$b]"
}

#
# Override the existing function ...
#
puts "Defining a new function for the object:"
obj1 proc f {x} {expr {\$x*cos(\$x)}}
puts "0 -- 2: [obj1 integral 0 2]"

#
# Express error
#
puts "This ought to cause a run-time error: \[obj3 integral 1 2\]:"
puts "1 -- 2 (obj3): [obj3 integral 1 2]"
```

 Category Numerical Analysis Category Object Orientation