Tcl wrapper for C interpolation procedures (v0.14)

::tclinterp::interpolationTop, Main, Index

CommandsTop, Main, Index

cubicSpline1d [::tclinterp::interpolation]Top, Main, Index

Does piecewise cubic spline interpolation.

cubicSpline1d -t|x list -y list -ti|xi list ?-ibcbeg|begflag value -ybcbeg value? ?-ibcend|endflag value -ybcend value? ?-deriv?
Parameters
-derivSelect the alternative output option.
-ibcbegLeft boundary condition flag, -begflag is an alias. Possible values: quad, the cubic spline should be a quadratic over the first interval; der1, the first derivative at the left endpoint should be YBCBEG; der2, the second derivative at the left endpoint should be YBCBEG; notaknot, not-a-knot, the third derivative is continuous at T(2).
-ibcendRight boundary condition flag, -endflag is an alias. Possible values: quad, the cubic spline should be a quadratic over the last interval; der1, the first derivative at the right endpoint should be YBCBEG; der2, the second derivative at the right endpoint should be YBCBEG; notaknot, not-a-knot, the third derivative is continuous at T(2).
-tList of independent variable (t) values, -x is an alias.
-tiList of independent variable interpolation (ti) values, -xi is an alias.
-yList of dependent variable (y) values.
-ybcbegThe values to be used in the boundary conditions if ibcbeg is equal to der1 or der2, default is 0.0.
-ybcendThe values to be used in the boundary conditions if ibcend is equal to der1 or der2, default is 0.0.
Return value

list of interpolated dependent variable values under. If -deriv switch is in args, the output is dictionary that contains yi values under yi key, yi derivative under yder1 key, and yi second derivative under yder2 key.

proc ::tclinterp::interpolation::cubicSpline1d {args} {

    # Does piecewise cubic spline interpolation.
    #  -ibcbeg - left boundary condition flag, -begflag is an alias. Possible values:
    #   **quad**, the cubic spline should be a quadratic over the first interval;
    #   **der1**, the first derivative at the left endpoint should be YBCBEG;
    #   **der2**, the second derivative at the left endpoint should be YBCBEG;
    #   **notaknot**, not-a-knot, the third derivative is continuous at T(2).
    #  -ibcend - right boundary condition flag, -endflag is an alias. Possible values:
    #   **quad**, the cubic spline should be a quadratic over the last interval;
    #   **der1**, the first derivative at the right endpoint should be YBCBEG;
    #   **der2**, the second derivative at the right endpoint should be YBCBEG;
    #   **notaknot**, not-a-knot, the third derivative is continuous at T(2).
    #  -ybcbeg - the values to be used in the boundary conditions if ibcbeg is equal to der1 or der2, default is 0.0
    #  -ybcend - the values to be used in the boundary conditions if ibcend is equal to der1 or der2, default is 0.0
    #  -t - list of independent variable (t) values, -x is an alias
    #  -y - list of dependent variable (y) values
    #  -ti - list of independent variable interpolation (ti) values, -xi is an alias
    #  -deriv - select the alternative output option
    # Returns: list of interpolated dependent variable values under. If `-deriv` switch is in args, the output is
    # dictionary that contains `yi` values under `yi` key, `yi` derivative under `yder1` key, and `yi` second derivative
    # under `yder2` key.
    # Synopsis: -t|x list -y list -ti|xi list ?-ibcbeg|begflag value -ybcbeg value? ?-ibcend|endflag value -ybcend value?
    #   ?-deriv?
    argparse {
        {-ibcbeg= -default quad -enum {quad der1 der2 notaknot} -alias begflag}
        {-ibcend= -default quad -enum {quad der1 der2 notaknot} -alias endflag}
        {-ybcbeg= -default 0.0}
        {-ybcend= -default 0.0}
        {-t= -required -alias x}
        {-y= -required}
        {-ti= -required -alias xi}
        -deriv
    }
    set keyMap [dcreate quad 0 der1 1 der2 2 notaknot 3]
    set tLen [llength $t]
    set yLen [llength $y]
    set tiLen [llength $ti]
    if {$tLen!=$yLen} {
        return -code error "Length of -y '$yLen' must be equal to length of -t '$tLen'"
    } elseif {$tiLen==0} {
        return -code error "Length of interpolation points list -ti must be more than zero"
    }
    ::tclinterp::lists2arrays [list tArray yArray] [list $t $y]
    ::tclinterp::newArrays [list yppArray] [list $tLen]
    ::tclinterp::newDoubleps [list ypPnt yppPnt]
    set yppArray [::tclinterp::spline_cubic_set $tLen $tArray $yArray [dget $keyMap $ibcbeg] $ybcbeg [dget $keyMap $ibcend] $ybcend]
    for {set i 0} {$i<$tiLen} {incr i} {
        set iElem [::tclinterp::spline_cubic_val $tLen $tArray $yArray $yppArray [@ $ti $i] $ypPnt $yppPnt]
        lappend yiList $iElem
        lappend ypList [::tclinterp::doublep_value $ypPnt]
        lappend yppList [::tclinterp::doublep_value $yppPnt]
    }
    ::tclinterp::deleteArrays $tArray $yArray $yppArray
    ::tclinterp::deleteDoubleps $ypPnt $yppPnt
    if {[info exists deriv]} {
        return [dict create yi $yiList yder1 $ypList yder2 $yppList]
    } else {
        return $yiList
    }
}

divDif1d [::tclinterp::interpolation]Top, Main, Index

Does divided difference one-dimensional interpolation.

divDif1d -x list -y list -xi list ?-coeffs?
Parameters
-coeffsSelect the alternative output option.
-xList of independent variable (x) values.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

list of interpolated dependent variable values, yi, at xi. If -coeffs switch is in args, the output is dictionary that contains yi values under yi key, and the values of difference table under the key coeffs.

proc ::tclinterp::interpolation::divDif1d {args} {

    # Does divided difference one-dimensional interpolation.
    #  -x - list of independent variable (x) values
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    #  -coeffs - select the alternative output option
    # Returns: list of interpolated dependent variable values, `yi`, at `xi`. If `-coeffs` switch is in args, the output
    # is dictionary that contains `yi` values under `yi` key, and the values of difference table under the key `coeffs`.
    # Synopsis: -x list -y list -xi list ?-coeffs?
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
        -coeffs
    }
    set xLen [llength $x]
    set yLen [llength $y]
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        return -code error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        return -code error "Length of interpolation points list -xi must be more than zero"
    }
    if {[::tclinterp::duplListCheck $x]=="true"} {
        return -code error "List of -x values must not contain duplicated elements"
    }
    ::tclinterp::lists2arrays [list xArray yArray] [list $x $y]
    ::tclinterp::newArrays [list difTab] [list $xLen]
    # create difference table for given data
    ::tclinterp::data_to_dif $xLen $xArray $yArray $difTab
    # calculate polynomial value for each xi value
    for {set i 0} {$i<$xiLen} {incr i} {
        set iElem [::tclinterp::dif_val $xLen $xArray $difTab [@ $xi $i]]
        lappend yiList $iElem
    }
    if {[info exists coeffs]} {
        ::tclinterp::arrays2lists [list difTabList] [list $difTab] [list $xLen]
        ::tclinterp::deleteArrays $difTab $xArray $yArray
        return [dcreate yi $yiList coeffs $difTabList]
    } else {
        ::tclinterp::deleteArrays $difTab $xArray $yArray
        return $yiList
    }
    return
}

hermiteSpline1d [::tclinterp::interpolation]Top, Main, Index

Does Hermite polynomial spline interpolation.

hermiteSpline1d -t|x list -y list -yp list -ti|xi list ?-deriv?
Parameters
-derivSelect the alternative output option.
-tList of independent variable (t) values, must be strictly increasing, -x is an alias.
-tiList of independent variable interpolation (ti) values, -xi is an alias.
-yList of dependent variable (y) values.
-ypList of dependent variable (y) derivative values.
Return value

list of interpolated dependent variable values. If -deriv switch is in args, the output is dictionary that contains yi values under yi key, yi derivative under yder1 key.

proc ::tclinterp::interpolation::hermiteSpline1d {args} {

    # Does Hermite polynomial spline interpolation.
    #  -t - list of independent variable (t) values, must be strictly increasing, -x is an alias
    #  -y - list of dependent variable (y) values
    #  -yp - list of dependent variable (y) derivative values
    #  -ti - list of independent variable interpolation (ti) values, -xi is an alias
    #  -deriv - select the alternative output option
    # Returns: list of interpolated dependent variable values. If `-deriv` switch is in args, the output is
    # dictionary that contains `yi` values under `yi` key, `yi` derivative under `yder1` key.
    # Synopsis: -t|x list -y list -yp list -ti|xi list ?-deriv?
    argparse {
        {-t= -required -alias x}
        {-y= -required}
        {-yp= -required}
        {-ti= -required -alias xi}
        -deriv
    }
    set tLen [llength $t]
    set yLen [llength $y]
    set ypLen [llength $yp]
    set tiLen [llength $ti]
    if {$tLen!=$yLen} {
        return -code error "Length of -y '$yLen' must be equal to length of -t '$tLen'"
    } elseif {$tLen!=$ypLen} {
        return -code error "Length of -yp '$ypLen' must be equal to length of -t '$tLen'"
    } elseif {$tiLen==0} {
        return -code error "Length of interpolation points list -ti must be more than zero"
    }
    ::tclinterp::lists2arrays [list tArray yArray ypArray] [list $t $y $yp]
    if {[::tclinterp::r8vec_ascends_strictly $tLen $tArray]==0} {
        error "Independent variable array -t is not strictly increasing"
    }
    ::tclinterp::newArrays [list cArray] [list [= {$tLen*4}]]
    ::tclinterp::newDoubleps [list yiPnt yipPnt]
    set cArray [::tclinterp::spline_hermite_set $tLen $tArray $yArray $ypArray]
    for {set i 0} {$i<$tiLen} {incr i} {
        ::tclinterp::spline_hermite_val $tLen $tArray $cArray [@ $ti $i] $yiPnt $yipPnt
        lappend yiList [::tclinterp::doublep_value $yiPnt]
        lappend yipList [::tclinterp::doublep_value $yipPnt]
    }
    ::tclinterp::deleteArrays $tArray $yArray $cArray $ypArray
    ::tclinterp::deleteDoubleps $yiPnt $yipPnt
    if {[info exists deriv]} {
        return [dict create yi $yiList yder1 $yipList]
    } else {
        return $yiList
    }
}

lagr1d [::tclinterp::interpolation]Top, Main, Index

Does Lagrange polynomial one-dimensional interpolation.

lagr1d -x list -y list -xi list
Parameters
-xList of independent variable (x) values.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

list of interpolated dependent variable values, yi, at xi

proc ::tclinterp::interpolation::lagr1d {args} {

    # Does Lagrange polynomial one-dimensional interpolation.
    #  -x - list of independent variable (x) values
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    # Returns: list of interpolated dependent variable values, `yi`, at `xi`
    # Synopsis: -x list -y list -xi list
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
    }
    set xLen [llength $x]
    set yLen [llength $y]
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        error "Length of interpolation points list -xi must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray yArray xiArray] [list $x $y $xi]
    set yiArray [::tclinterp::interp_lagrange 1 $xLen $xArray $yArray $xiLen $xiArray]
    set yiList [::tclinterp::array2list $yiArray $xiLen]
    ::tclinterp::deleteArrays $xArray $yArray $xiArray $yiArray
    return $yiList
}

least1d [::tclinterp::interpolation]Top, Main, Index

Does least squares polynomial one-dimensional interpolation.

least1d -x list -y list -xi list ?-w list? ?-nterms value? ?-coeffs?
Parameters
-coeffsSelect the alternative output option.
-ntermsNumber of terms of interpolation polynom, default is 3.
-wList of weights, optional.
-xList of independent variable (x) values.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

list of interpolated dependent variable values, yi, at xi. If -coeffs switch is in args, the output is dictionary that contains yi values under yi key, and the values of interpolation polynom coefficients under the keys b, c and d.

proc ::tclinterp::interpolation::least1d {args} {

    # Does least squares polynomial one-dimensional interpolation.
    #  -x - list of independent variable (x) values
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    #  -w - list of weights, optional
    #  -nterms - number of terms of interpolation polynom, default is 3
    #  -coeffs - select the alternative output option
    # Returns: list of interpolated dependent variable values, `yi`, at `xi`. If `-coeffs` switch is in args, the output
    # is dictionary that contains `yi` values under `yi` key, and the values of interpolation polynom coefficients under
    # the keys `b`, `c` and `d`.
    # Synopsis: -x list -y list -xi list ?-w list? ?-nterms value? ?-coeffs?
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
        -w=
        {-nterms= -default 3}
        -coeffs
    }
    set xLen [llength $x]
    set yLen [llength $y]
    if {[info exists w]} {
        set wLen [llength $w]
    } else {
        set wLen [llength $x]
        set w [lrepeat $wLen 1]
    }
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        return -code error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xLen!=$wLen} {
        return -code error "Length of -w '$wLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        return -code error "Length of interpolation points list -xi must be more than zero"
    } elseif {[string is integer -strict $nterms]==0} {
        return -code error "Number of terms -nterms '$nterms' must be of integer type"
    } elseif {$nterms<=0} {
        return -code error "Number of terms -nterms must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray yArray wArray xiArray] [list $x $y $w $xi]
    ::tclinterp::newArrays [list b c d] [list $nterms $nterms $nterms]
    # create polynomial coefficients for given data
    ::tclinterp::least_set $xLen $xArray $yArray $wArray $nterms $b $c $d
    # calculate polynomial value for each xi value
    for {set i 0} {$i<$xiLen} {incr i} {
        set iElem [::tclinterp::least_val $nterms $b $c $d [@ $xi $i]]
        lappend yiList $iElem
    }
    if {[info exists coeffs]} {
        ::tclinterp::arrays2lists [list bList cList dList] [list $b $c $d] [list $nterms $nterms $nterms]
        ::tclinterp::deleteArrays $b $c $d $xArray $yArray $xiArray
        return [dcreate yi $yiList coeffs [dcreate b $bList c $cList d $dList]]
    } else {
        ::tclinterp::deleteArrays $b $c $d $xArray $yArray $xiArray
        return $yiList
    }
    return
}

least1dDer [::tclinterp::interpolation]Top, Main, Index

Does least squares polynomial one-dimensional interpolation with calculation of its derivative.

least1dDer -x list -y list -xi list ?-w list? ?-nterms value? ?-coeffs?
Parameters
-coeffsSelect the alternative output option.
-ntermsNumber of terms of interpolation polynom, default is 3.
-wList of weights, optional.
-xList of independent variable (x) values.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

dict of interpolated dependent variable values and its derivatives under yi and yiDer keys. If -coeffs switch is in args, the output is dictionary that contains yi values under yi key, yi derivatives under yiDer key, and the values of interpolation polynom coefficients under the keys b, c and d.

proc ::tclinterp::interpolation::least1dDer {args} {

    # Does least squares polynomial one-dimensional interpolation with calculation of its derivative.
    #  -x - list of independent variable (x) values
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    #  -w - list of weights, optional
    #  -nterms - number of terms of interpolation polynom, default is 3
    #  -coeffs - select the alternative output option
    # Returns: dict of interpolated dependent variable values and its derivatives under `yi` and `yiDer` keys. If
    # `-coeffs` switch is in args, the output is dictionary that contains `yi` values under `yi` key, `yi` derivatives
    # under `yiDer` key, and the values of interpolation polynom coefficients under the keys `b`, `c` and `d`.
    # Synopsis: -x list -y list -xi list ?-w list? ?-nterms value? ?-coeffs?
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
        -w=
        {-nterms= -default 3}
        -coeffs
    }
    set xLen [llength $x]
    set yLen [llength $y]
    if {[info exists w]} {
        set wLen [llength $w]
    } else {
        set wLen [llength $x]
        set w [lrepeat $wLen 1]
    }
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        return -code error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xLen!=$wLen} {
        return -code error "Length of -w '$wLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        return -code error "Length of interpolation points list -xi must be more than zero"
    } elseif {[string is integer -strict $nterms]==0} {
        return -code error "Number of terms -nterms '$nterms' must be of integer type"
    } elseif {$nterms<=0} {
        return -code error "Number of terms -nterms must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray yArray wArray xiArray] [list $x $y $w $xi]
    ::tclinterp::newArrays [list b c d] [list $nterms $nterms $nterms]
    ::tclinterp::newDoubleps [list yiPnt yiDerPnt]
    # create polynomial coefficients for given data
    ::tclinterp::least_set $xLen $xArray $yArray $wArray $nterms $b $c $d
    # calculate polynomial value and derivative for each xi value
    for {set i 0} {$i<$xiLen} {incr i} {
        ::tclinterp::least_val2 $nterms $b $c $d [@ $xi $i] $yiPnt $yiDerPnt
        lappend yiList [::tclinterp::doublep_value $yiPnt]
        lappend yiDerList [::tclinterp::doublep_value $yiDerPnt]
    }
    if {[info exists coeffs]} {
        ::tclinterp::arrays2lists [list bList cList dList] [list $b $c $d] [list $nterms $nterms $nterms]
        ::tclinterp::deleteArrays $b $c $d $xArray $yArray $xiArray
        ::tclinterp::deleteDoubleps $yiPnt $yiDerPnt
        return [dcreate yi $yiList yiDer $yiDerList coeffs [dcreate b $bList c $cList d $dList]]
    } else {
        ::tclinterp::deleteArrays $b $c $d $xArray $yArray $xiArray
        ::tclinterp::deleteDoubleps $yiPnt $yiDerPnt
        return [dcreate yi $yiList yiDer $yiDerList]
    }
    return
}

lin1d [::tclinterp::interpolation]Top, Main, Index

Does linear one-dimensional interpolation.

lin1d -x list -y list -xi list
Parameters
-xList of independent variable (x) values, must be strictly increasing.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

list of interpolated dependent variable values, yi, at xi

proc ::tclinterp::interpolation::lin1d {args} {

    # Does linear one-dimensional interpolation.
    #  -x - list of independent variable (x) values, must be strictly increasing
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    # Returns: list of interpolated dependent variable values, `yi`, at `xi`
    # Synopsis: -x list -y list -xi list
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
    }
    set xLen [llength $x]
    set yLen [llength $y]
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        error "Length of interpolation points list -xi must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray yArray xiArray] [list $x $y $xi]
    if {[::tclinterp::r8vec_ascends_strictly $xLen $xArray]==0} {
        error "Independent variable array -x is not strictly increasing"
    }
    set yiArray [::tclinterp::interp_linear 1 $xLen $xArray $yArray $xiLen $xiArray]
    set yiList [::tclinterp::array2list $yiArray $xiLen]
    ::tclinterp::deleteArrays $xArray $yArray $xiArray $yiArray
    return $yiList
}

near1d [::tclinterp::interpolation]Top, Main, Index

Does nearest one-dimensional interpolation.

near1d -x list -y list -xi list
Parameters
-xList of independent variable (x) values.
-xiList of independent variable interpolation (xi) values.
-yList of dependent variable (y) values.
Return value

list of interpolated dependent variable values, yi, at xi

proc ::tclinterp::interpolation::near1d {args} {

    # Does nearest one-dimensional interpolation.
    #  -x - list of independent variable (x) values
    #  -y - list of dependent variable (y) values
    #  -xi - list of independent variable interpolation (xi) values
    # Returns: list of interpolated dependent variable values, `yi`, at `xi`
    # Synopsis: -x list -y list -xi list
    argparse {
        {-x= -required}
        {-y= -required}
        {-xi= -required}
    }
    set xLen [llength $x]
    set yLen [llength $y]
    set xiLen [llength $xi]
    if {$xLen!=$yLen} {
        error "Length of -y '$yLen' must be equal to length of -x '$xLen'"
    } elseif {$xiLen==0} {
        error "Length of interpolation points list -xi must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray yArray xiArray] [list $x $y $xi]
    set yiArray [::tclinterp::interp_nearest 1 $xLen $xArray $yArray $xiLen $xiArray]
    set yiList [::tclinterp::array2list $yiArray $xiLen]
    ::tclinterp::deleteArrays $xArray $yArray $xiArray $yiArray
    return $yiList
}

pchip1d [::tclinterp::interpolation]Top, Main, Index

Does piecewise cubic Hermite interpolation (PCHIP).

pchip1d -x list -f|y list -xe|xi list
Parameters
-fList of dependent variable (f) values, -y is an alias.
-xList of independent variable (x) values, must be strictly increasing.
-xeList of independent variable interpolation (xe) values, -xi is an alias.
Return value

list of interpolated dependent variable values.

proc ::tclinterp::interpolation::pchip1d {args} {

    # Does piecewise cubic Hermite interpolation (PCHIP).
    #  -x - list of independent variable (x) values, must be strictly increasing
    #  -f - list of dependent variable (f) values, -y is an alias
    #  -xe - list of independent variable interpolation (xe) values, -xi is an alias
    # Returns: list of interpolated dependent variable values.
    # Synopsis: -x list -f|y list -xe|xi list
    argparse {
        {-x= -required}
        {-f= -required -alias y}
        {-xe= -required -alias xi}
    }
    set xLen [llength $x]
    set fLen [llength $f]
    set xeLen [llength $xe]
    if {$xLen!=$fLen} {
        return -code error "Length of -f '$fLen' must be equal to length of -x '$xLen'"
    } elseif {$xeLen==0} {
        return -code error "Length of interpolation points list -xe must be more than zero"
    }
    ::tclinterp::lists2arrays [list xArray fArray xeArray] [list $x $f $xe]
    if {[::tclinterp::r8vec_ascends_strictly $xLen $xArray]==0} {
        error "Independent variable array -x is not strictly increasing"
    }
    ::tclinterp::newArrays [list dArray feArray] [list $xLen $xeLen]
    ::tclinterp::spline_pchip_set $xLen $xArray $fArray $dArray
    ::tclinterp::spline_pchip_val $xLen $xArray $fArray $dArray $xeLen $xeArray $feArray
    set feList [::tclinterp::array2list $feArray $xeLen]
    ::tclinterp::deleteArrays $xArray $fArray $dArray $xeArray $feArray
    return $feList
}