Last active
September 11, 2021 10:49
-
-
Save YujiSODE/e9f1dbee633295fbd5f36c71f3468076 to your computer and use it in GitHub Desktop.
Tool to count frequencies of logarithm to a base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#logCount | |
#logCount.tcl | |
##=================================================================== | |
# Copyright (c) 2021 Yuji SODE <yuji.sode@gmail.com> | |
# | |
# This software is released under the MIT License. | |
# See LICENSE or http://opensource.org/licenses/mit-license.php | |
##=================================================================== | |
#Tool to count frequencies of logarithm to a base | |
#=== Synopsis === | |
#tclsh logCount.tcl 1 2 2 8 8 8 1024 2024 | |
#** Shell ** | |
# | |
#`tclsh logCount.tcl base precision value1 ?value2 ?... valueN??;` | |
# | |
#- `$base`: a positive value that is more than 1.0, or keywords | |
#- `$precision`: precision of converted value in integer | |
#- `$value1`: a positive value | |
#- `$value2` (, ..., `$valueN`): optional positive values | |
#-------------------------------------------------------------------- | |
# | |
#** Tcl ** | |
# | |
#- `::logCount::count value ?base ?precision??;` | |
# it counts a frequency of logarithm to a base | |
# returned list is `{x1 frequency1}` | |
# | |
#- `::logCount::listCount list ?base ?precision??;` | |
# it counts frequencies of logarithm to a base | |
# returned list is {x1 frequency1 ... xN frequencyN} | |
# | |
#- `$value`: a positive value | |
#- `$list`: a list of positive values | |
#- `$base`: a positive value that is more than 1.0, or keywords | |
#- `$precision`: precision of converted value in integer | |
# | |
#- `::logCount::reset;` | |
# it resets count result | |
#-------------------------------------------------------------------- | |
# | |
#**keywords for `$base`** | |
#- none or NONE: original value (without base) | |
#- e or E: value is converted into the natural logarithmc value | |
##=================================================================== | |
# | |
#=== <namespace: logCount> === | |
namespace eval ::logCount { | |
# | |
#=== variables === | |
# | |
#an array of frequencies | |
variable FREQS;array set FREQS {}; | |
# | |
#$base = none or NONE -> original value | |
variable BASE_NONE {none NONE}; | |
# | |
#$base = e or E -> natural logarithm | |
variable BASE_E {e E}; | |
}; | |
# | |
#it counts a frequency of logarithm to a base | |
#returned list is {x1 frequency1} | |
proc ::logCount::count {value {base 10.0} {precision 2}} { | |
#- $value: a positive value | |
#- $base: a positive value that is more than 1.0, or keywords | |
#- $precision: precision of converted value in integer | |
# | |
# **keywords for $base** | |
# - none or NONE: original value (without base) | |
# - e or E: value is converted into the natural logarithmc value | |
# | |
variable ::logCount::FREQS;variable ::logCount::BASE_NONE;variable ::logCount::BASE_E; | |
### | |
# | |
set precision "%.[expr {int($precision)}]f"; | |
# | |
set freqNames [array names ::logCount::FREQS]; | |
set v {}; | |
# | |
# | |
set v [format $precision [expr {${base}in${::logCount::BASE_NONE}?double($value):(${base}in${::logCount::BASE_E}?log($value):log10($value)/log10(double($base)))}]]; | |
# | |
#when $v is a new value | |
if {${v}ni${freqNames}} { | |
set ::logCount::FREQS($v) 0; | |
}; | |
# | |
incr ::logCount::FREQS($v) 1; | |
# | |
#returned list is {x1 frequency1} | |
return [array get ::logCount::FREQS $v]; | |
}; | |
# | |
#it counts frequencies of logarithm to a base | |
#returned list is {x1 frequency1 ... xN frequencyN} | |
proc ::logCount::listCount {list {base 10.0} {precision 2}} { | |
#- $list: a list of positive values | |
#- $base: a positive value that is more than 1.0, or keywords | |
#- $precision: precision of converted value in integer | |
# | |
# **keywords for $base** | |
# - none or NONE: original value (without base) | |
# - e or E: value is converted into the natural logarithmc value | |
# | |
variable ::logCount::FREQS; | |
### | |
# | |
set output {}; | |
# | |
foreach v $list { | |
::logCount::count $v $base $precision; | |
}; | |
# | |
foreach e [lsort -real -increasing [array names ::logCount::FREQS]] { | |
lappend output $e $::logCount::FREQS($e); | |
}; | |
# | |
#returned list is {x1 frequency1 ... xN frequencyN} | |
return $output; | |
}; | |
# | |
#it resets count result | |
proc ::logCount::reset {} { | |
variable ::logCount::FREQS; | |
### | |
array unset ::logCount::FREQS; | |
array set ::logCount::FREQS {}; | |
}; | |
##=================================================================== | |
# | |
#=== shell === | |
#`tclsh logCount.tcl base precision value1 ?value2 ?... valueN??;` | |
#- `$base`: a positive value that is more than 1.0, or keywords | |
#- `$precision`: precision of converted value in integer | |
#- `$value1`: a positive value | |
#- `$value2` (, ..., `$valueN`): optional positive values | |
# | |
#**keywords for $base** | |
#- none or NONE: original value (without base) | |
#- e or E: value is converted into the natural logarithmc value | |
### | |
# | |
#--- arguments --- | |
set base_shell [lindex $argv 0]; | |
set precision_shell [lindex $argv 1]; | |
set values_shell [lrange $argv 2 end]; | |
# | |
if {$argc&&[llength $values_shell]>0} { | |
foreach {e1 e2} [::logCount::listCount $values_shell $base_shell $precision_shell] { | |
puts stdout "$e1: $e2"; | |
}; | |
# | |
unset base_shell precision_shell values_shell; | |
}; | |
##=================================================================== | |
#*** License *** | |
#MIT License | |
# | |
#Copyright (c) 2021 Yuji Sode | |
# | |
#Permission is hereby granted, free of charge, to any person obtaining a copy | |
#of this software and associated documentation files (the "Software"), to deal | |
#in the Software without restriction, including without limitation the rights | |
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
#copies of the Software, and to permit persons to whom the Software is | |
#furnished to do so, subject to the following conditions: | |
# | |
#The above copyright notice and this permission notice shall be included in all | |
#copies or substantial portions of the Software. | |
# | |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
#SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment