Version 30 of math

Updated 2007-06-11 15:35:13 by FF

Documentation can be found at http://tcllib.sourceforge.net/doc/math.html

This package is a part of the Tcllib distribution.

It contains:

  • ::math::cov -- coefficient of variation of 3 or more arguments
  • ::math::fibonacci -- Return the nth Fibonacci number
  • ::math::integrate -- calculate the area under a curve defined by an argument of a list of 5 or more x,y data pairs
  • ::math::max -- Return the maximum of two or more arguments
  • ::math::mean -- Return the arithmetic mean of two or more arguments
  • ::math::min -- Return the minimum of two or more arguments
  • ::math::product -- Return the product of two or more arguments
  • ::math::random -- Return a random number, value chosen based on an argument selecting one of these ranges:
                (null)          choose a number between 0 and 1
                val             choose a number between 0 and val
                val1 val2       choose a number between val1 and val2
  • ::math::roman -- Handling of Roman Numerals.
  • ::math::sigma -- population standard deviation value (from 3 or more arguments)
  • ::math::stats -- calculate mean, standard deviation, and coefficient of variation from 3 or more arguments.
  • ::math::sum -- calculate arithmetic sum of two or more arguments

Things to keep in mind:

If you pick tcllib up from its CVS repository, you will be able to get access to even more functionality:

  • ::math::bigfloat::abs, acos, add, addInt2Float, asin, atan, ceil, ...
  • ::math::bignum::cmp, tostr, add, div, fromstr, max, min, mul, abs, iszero, setsign, ...
  • ::math::calculus::eulerStep, heunStep, rungeKuttaStep, ...
  • ::math::combinatorics Beta, factorialList, pascal, InitializeFactorial, InitializePascal, choose, ln_Gamma, factorial
  • ::math::complexnumbers::complex, +, -, *, / mod, pow, real, sin, sqrt, ...
  • ::math::constants::constants::pi e, ln10 onethird eps print-constants, find_huge, find_tiny
  • ::math::fourier::dft, ...
  • ::math::geometry::calculateDistanceToLine, findClosestPointOnLine, angle, areaPolygon, bbox, calculateDistanceToLine, ...
  • ::math::interpolate::interp-linear, neville, ...
  • ::math::linearalgebra::angle, ...
  • ::math::cov, expectDouble, expectInteger, fibonacci, integrate, max, mean, min, product, random, sigma, stats, sum, ...
  • ::math::optimize::minimum, maximum, min_bound_1d, ...
  • ::math::polynomials::polynomial, addPolyn, subPolyn, ...
  • ::math::rationalnumbers::rationalFunction, ratioCmd, evalRatio, ...
  • ::math::special::elliptic_K, ...
  • ::math::special::exponential_Ei, ...
  • ::math::special::Gamma, ...
  • ::math::special::I_n, J-1/2, J0, J1, J1/2, Jn ...
  • ::math::special::legendre, ...
  • ::math::statistics::BasicStats, histogram, corr, ...
  • ::math::statistics::filter, map, samplescount, ...
  • ::math::statistics::Inverse-cdf-exponential, cdf-normal, ...
  • ::math::statistics::pdf-normal, pdf-uniform, ...
  • ::math::statistics::plot-scale, plot-xydata, ...
  • ::math::statistics::random-exponential, random-normal, ...

FF - 2007-06-11 - I implemented some missing features of math::linearalgebra. (If they are going to be included in tcllib -I hope for that- you can delete this comment.)

 package require math::linearalgebra

 namespace eval ::math::linearalgebra {
     namespace export joinMatrix cancelrow cancelcol
     namespace export cofactor cofactorMatrix
     namespace export adjointMatrix
     namespace export determinant invert
 }

 # joinMatrix --
 #     Join two matrices along the specified dimension
 # Arguments:
 #     dim        Dimension to join [1,2]
 #     mv1,mv2    Matrices to be treated
 #
 # Result:
 #     Matrix result of the join
 #
 proc ::math::linearalgebra::joinMatrix { dim mv1 mv2 } {
     set result $mv1
     switch -exact -- $dim {
         1 {
             foreach r $mv2 {lappend $result $r}
         }
         2 {
             set l [llength $mv1]
             for {set i 0} {$i < $l} {incr i} {
                 lset result $i [concat [lindex $mv1 $i] [lindex $mv2 $i]]
             }
         }
         default {
             return -code error "do we work on ${dim}D matrices?"
         }
     }
     return $result
 }

 # cancelrow --
 #     Return a matrix minus the specified row
 # Arguments:
 #     matrix     Matrix to work on
 #     row        to delete
 #
 # Result:
 #     Matrix result of the operation
 #
 proc ::math::linearalgebra::cancelrow { matrix row } {
     return [concat [lrange $matrix 0 $row-1] [lrange $matrix $row+1 end]]
 }

 # cancelrow --
 #     Return a matrix minus the specified column
 # Arguments:
 #     matrix     Matrix to work on
 #     col        to delete
 #
 # Result:
 #     Matrix result of the operation
 #
 proc ::math::linearalgebra::cancelcol { matrix col } {
     set result {}
     foreach r $matrix {
         lappend result [concat [lrange $r 0 $col-1] [lrange $r $col+1 end]]
     }
     return $result
 }

 # cofactor --
 #     Compute the cofactor of the specified element
 # Arguments:
 #     matrix     Matrix to work on
 #     row        
 #     col        
 #
 # Result:
 #     The cofactor of element at a{row,col}
 #
 proc ::math::linearalgebra::cofactor { matrix row col } {
     set submatrix [cancelcol [cancelrow $matrix $row] $col]
     return [determinant $submatrix]
 }

 # cofactorMatrix --
 #     Compute the cofactor of the specified matrix
 # Arguments:
 #     matrix     Matrix to work on
 #
 # Result:
 #     The cofactor of specified matrix
 #
 proc ::math::linearalgebra::cofactorMatrix { matrix } {
     set result {}
     lassign [shape $matrix] rows cols
     for {set i 0} {$i < $rows} {incr i} {
         set newrow {}
         for {set j 0} {$j < $cols} {incr j} {
             lappend newrow [cofactor $matrix $i $j]
         }
         lappend result $newrow
     }
     return $result
 }

 # adjointMatrix --
 #     The adjoint matrix is the transpose of the cofactor matrix
 # Arguments:
 #     matrix
 #
 # Result:
 #     The adjoint matrix
 #
 proc ::math::linearalgebra::adjointMatrix {matrix} {
     return [transpose [cofactorMatrix $matrix]]
 }

 # determinant --
 #     Compute the cofactor of the specified element
 # Arguments:
 #     matrix     Matrix to work on
 #     row        
 #     col        
 #
 # Result:
 #     The cofactor of element at a{row,col}
 #
 proc ::math::linearalgebra::determinant { matrix } {
     set shape [shape $matrix]
     if { [lindex $shape 0] != [lindex $shape 1] } {
         return -code error "determinant only defined for a square matrix"
     }

     switch -exact -- [join $shape x] {
         1x1 {
             return [lindex [lindex $matrix 0] 0]
         }
         2x2 {
             lassign [getrow $matrix 0] a b
             lassign [getrow $matrix 1] c d
             return [expr {($a*$d)-($c*$b)}]
         }
         default {
             set det 0
             set sign 0
             set row_no 0
             foreach row $matrix {
                 set sign [expr {($sign==1)?(-1):(1)}]
                 set det [expr {
                     $det+$sign*[lindex $row 0]*[cofactor $matrix $row_no 0]
                 }]
                 incr row_no
             }
             return $det
         }
     }
 }

 # invert --
 #     Perform the matrix inversion using the adjoint method
 #     Note: probably the Gauss-Jordan elimination over an
 #           augmented matrix is *much* faster.
 # Arguments:
 #     matrix     Matrix to work on
 #
 # Result:
 #     The inverted matrix
 #
 proc ::math::linearalgebra::invert { matrix } {
     set det [determinant $matrix]
     if { $det == 0 } {
         return -code error "cannot invert a singular matrix"
     }
     return [expr pow($det,-1)*[adjointMatrix $matrix]]
 }

Category Package, subset Tcllib, Category Mathematics