Division into Parts by Multiple Ratios and eTCL demo example calculator, numerical analysis edit
This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER in your comment with the same courtesy that I will give you. Its very hard to reply intelligibly without some background of the correspondent. Thanks,gold
gold Here is some eTCL starter code for Division into Parts by Multiple Ratios in calculator shell. Additional console programs below are used to check or improve subroutines in calculator shell.
# using pseudocode
proc test1 {
# possible problem instances,
low vs high
middle vs low
high vs middle}
proc test2 {
initialize algorithm_result = 1.
initialize terms
proc test6 {
check algorithm
f(solution) =? initial product
check_sum = a+b+c+d+e
set answers and printout with resulting values
pseudocode: need test cases > small,medium, giant
pseudocode: need testcases within range of expected operation.
pseudocode: are there any cases too small or large to be solved?
pseudocode: limit to size of eTCL list, ref versions. }
#end of file
Testcases Section
In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the capacity functions (subroutines).
# pretty print from autoindent and ased editor
# Division into parts by ratios calculator
# written on Windows XP on eTCL
# working under TCL version 8.5.6 and eTCL 1.0.1
# gold on TCL WIKI, 2mar2017
package require Tk
package require math::numtheory
namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
set tcl_precision 17
frame .frame -relief flat -bg aquamarine4
pack .frame -side top -fill y -anchor center
set names {{} {quantity :} }
lappend names {ratio 1:}
lappend names {ratio 2: }
lappend names {ratio 3: }
lappend names {optional:}
lappend names {answers: quantity : }
lappend names {sum of ratios : }
lappend names {list :}
foreach i {1 2 3 4 5 6 7 8} {
label .frame.label$i -text [lindex $names $i] -anchor e
entry .frame.entry$i -width 35 -textvariable side$i
grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1 }
proc about {} {
set msg "Calculator for Division into Parts by Ratios
from TCL WIKI,
written on eTCL "
tk_messageBox -title "About" -message $msg }
# adapted from tcl-wiki Stats 2011-05-22, arithmetic mean [RLE]
#
# ::math::quantity_into_ratios --
#
# Return the division of quantity by two or more given ratios
#
# Arguments:
# val first value is quantity
# args other values are two or more given ratios
#
# Results: parts of quantity divided by ratios
#
proc ::math::quantity_into_ratios {quantity args} {
set sum 0.
set N [ expr { [ llength $args ] + 1 } ]
if { $N == 1 } { return 0 }
foreach val $args {
set sum [ expr { $sum + $val } ]
}
foreach val $args {
lappend answer [ expr { $quantity * ($val/$sum) } ]
}
set answer
}
#puts " ::math::quantity_into_ratios ( 84 2 2 2 ) answer 12.0 24.0 48.0 "
#puts " [ ::math::quantity_into_ratios 84 1 2 4 ] "
#puts " [ ::math::quantity_into_ratios 84 ] for (::math::quantity_into_ratios 84) returns zero "
proc calculate { } {
global answer2
global side1 side2 side3 side4 side5
global side6 side7 side8
global testcase_number
incr testcase_number
set side1 [* $side1 1. ]
set side2 [* $side2 1. ]
set side3 [* $side3 1. ]
set side4 [* $side4 1. ]
set side5 [* $side5 1. ]
set side6 [* $side6 1. ]
set side7 [* $side7 1. ]
set side8 [* $side8 1. ]
set answer2 [ ::math::quantity_into_ratios $side1 $side2 $side3 $side4 ]
set sum_of_ratios [+ $side2 $side3 $side4 ]
set side6 $side1
set side7 $sum_of_ratios
set side8 $answer2
}
proc fillup {aa bb cc dd ee ff gg hh} {
.frame.entry1 insert 0 "$aa"
.frame.entry2 insert 0 "$bb"
.frame.entry3 insert 0 "$cc"
.frame.entry4 insert 0 "$dd"
.frame.entry5 insert 0 "$ee"
.frame.entry6 insert 0 "$ff"
.frame.entry7 insert 0 "$gg"
.frame.entry8 insert 0 "$hh"
}
proc clearx {} {
foreach i {1 2 3 4 5 6 7 8 } {
.frame.entry$i delete 0 end } }
proc reportx {} {
global side1 side2 side3 side4 side5
global side6 side7 side8
global testcase_number
console show;
puts "%|table $testcase_number|printed in| tcl wiki format|% "
puts "&| quantity| value| comment, if any|& "
puts "&| $testcase_number:|testcase_number | |&"
puts "&| $side1 :|quantity | |&"
puts "&| $side2 :|ratio 1 | |& "
puts "&| $side3 :|ratio 2 | |& "
puts "&| $side4 :|ratio 3 | |&"
puts "&| $side5 :|optional: | |&"
puts "&| $side6 :|answers: quantity | |&"
puts "&| $side7 :|sum of ratios | |&"
puts "&| $side8 :|list| |&"
}
frame .buttons -bg aquamarine4
::ttk::button .calculator -text "Solve" -command { set side8 0 ; calculate }
::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 84.0 1. 2.0 4.0 0.0 84. 7.0 12.0}
::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 120.0 1.0 2.0 3.0 0.0 120.0 6.0 20.0}
::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 360.0 1.0 2.0 3.0 0.0 360.0 12.0 30.0}
::ttk::button .clearallx -text clear -command {clearx }
::ttk::button .about -text about -command {about}
::ttk::button .cons -text report -command { reportx }
::ttk::button .exit -text exit -command {exit}
pack .calculator -in .buttons -side top -padx 10 -pady 5
pack .clearallx .cons .about .exit .test4 .test3 .test2 -side bottom -in .buttons
grid .frame .buttons -sticky ns -pady {0 10}
. configure -background aquamarine4 -highlightcolor brown -relief raised -border 30
wm title . "Division into Parts by Ratios Calculator"
# end of deck
Pushbutton Operation
For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console.For testcases in a computer session, the eTCL calculator increments a new testcase number internally, eg. TC(1), TC(2) , TC(3) , TC(N). The testcase number is internal to the calculator and will not be printed until the report button is pushed for the current result numbers. The current result numbers will be cleared on the next solve button. The command { calculate; reportx } or { calculate ; reportx; clearx } can be added or changed to report automatically. Another wrinkle would be to print out the current text, delimiters, and numbers in a TCL wiki style table as
puts " %| testcase $testcase_number | value| units |comment |%"
puts " &| volume| $volume| cubic meters |based on length $side1 and width $side2 |&"
# pretty print from autoindent and ased editor
# quantity_into_ratios for math::stats? package
# working under TCL version 8.5.6 and eTCL 1.0.1
# console program written on Windows XP on eTCL
# gold on TCL WIKI, 2Mar2017
package require Tk
console show
package require math::numtheory
namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
set tcl_precision 17
# adapted from tcl-wiki Stats 2011-05-22, arithmetic mean [RLE]
#
# ::math::quantity_into_ratios --
#
# Return the division of quantity by two or more given ratios
#
# Arguments:
# val first value is quantity
# args other values are two or more given ratios
#
# Results: parts of quantity divided by ratios
#
proc ::math::quantity_into_ratios {quantity args} {
set sum 0.
set N [ expr { [ llength $args ] + 1 } ]
if { $N == 1 } { return 0 }
foreach val $args {
set sum [ expr { $sum + $val } ]
}
foreach val $args {
lappend answer [ expr { $quantity * ($val/$sum) } ]
}
set answer
}
puts " ::math::quantity_into_ratios ( 84 2 2 2 ) answer 12.0 24.0 48.0 "
puts " [ ::math::quantity_into_ratios 84 1 2 4 ] "
puts " [ ::math::quantity_into_ratios 84 ] for (::math::quantity_into_ratios 84) returns zero "
#end of deck
# pretty print from autoindent and ased editor
# geometric_mean for math::stats? package
# working under TCL version 8.5.6 and eTCL 1.0.1
# console program written on Windows XP on eTCL
# gold on TCL WIKI, 2Mar2017
package require Tk
console show
package require math::numtheory
namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
set tcl_precision 17
# adapted from tcl-wiki Stats 2011-05-22, arithmetic mean [RLE]
console show
# ::math::geometric_mean --
#
# Return the geometric_mean of two or more values
#
# Arguments:
# val first value
# args other values
#
# Results:
# geometric_mean arithmetic geometric_mean value
proc ::math::geometric_mean { args } {
set product 1.
set N [ expr { [ llength $args ] } ]
foreach val $args {
set product [ expr { $product*$val } ]
}
set exponent [ expr { 1./$N } ]
set geometric_mean [ expr { $product**$exponent } ]
#puts " $N $exponent $product $args "
set geometric_mean
}
puts " ::math::geometric_mean ( 1 2 3 4 ) answer 2.2133638 "
puts " [ ::math::geometric_mean 1 2 3 4 ] answer 2.2133638 "
#end of deck
gold This page is copyrighted under the TCL/TK license terms, this license.