Using stdev command in Tcl math package - tcl

How do I use the stdev command in the ::math::statistics package? I have tried the following but none work
package require math
package require math::statistics
%eval ::math::statistics::stdev $my
invalid command name "::math::statistics::stdev"
% eval ::math::stdev $my
invalid command name "::math::stdev"
% eval ::statistics::stdev $my
invalid command name "::statistics::stdev"
Where $my is a list of numbers.

The correct way to use that code is:
package require math::statistics
set values {1 2 3 4 5}
set SD [::math::statistics::stdev $values]
I don't know why you were getting invalid command name "::math::statistics::stdev" as an error; I can only guess that you're getting a very old version of the package (it works with version 0.8.0).

What you are looking for is:
set standard_deviation [math::statistics::stdev $my]

Related

Why can't I access the correct value of pi in tcl?

Here is the code I have executed:
C:\intelFPGA\18.1\quartus\bin64>tclsh86
% puts $tcl_version
8.6
% package require math::constants
1.0.2
% puts [pi]
26576
% puts [PI]
invalid command name "PI"
% puts [math::constants::pi]
invalid command name "math::constants::pi"
% puts [math::constants::constants::pi]
invalid command name "math::constants::constants::pi"
Why do I get the wrong value of pi? Why can't I get access to the correct value even if I use the :: operator to get into the math package? I just installed the tcl libraries but I am still having trouble.
It's a variable, not a command:
% puts $::math::constants::pi
3.141592653589793
Or import it directly into your current namespace:
% ::math::constants::constants pi
% puts $pi
3.141592653589793

TCL issues with Octal numbers seen after porting from EDK 1.05 to EDK2

I have an EFI Shell tool which uses EDK 1.05 and TCL 8.3 sources. This tool accepts user commands to display PCI-E adapter information and to upgrade firmware on it. I recently ported it to UDK2017. I am using VS2012x86 toolchain to build the tool.
When I run the binary from EFI Shell, TCL reports errors such as these.
can't use invalid octal number as operand of "||"
syntax error in expression "(1<<0)"
syntax error in expression "(0x1<<0)"
I have read about TCL and Octal numbers
Since this issue is not being seen with EDK 1.05 code with the same TCL version, I am wondering if there is any flag I am missing out. I am hoping there is a simple solution to get past this error since there was no change in the TCL version.
Octal Issue
It's hard to be sure, but I suspect with the octal number issue you've got code that's parsing something like 080808 as a number, which is interpreted as octal because of the leading 0 (just like a constant in C or C++) and so can't contain an 8 (or 9). To parse a number definitely as decimal, the scan command is used:
set val 080808
scan $val "%d" parsedVal
# Properly, should check that [scan] has a result of 1, but I probably wouldn't bother
puts "$val -> $parsedVal"
Odd Expression Syntax Error
The other syntax error in expression "(1<<0)" errors are stranger, as those are definitely valid syntax. I've only got versions back to 8.4 on this machine, but…
$ tclsh8.4
% expr (1<<0)
1
The only ways that could be an invalid expression are if it is either in some custom expression language (which would be application-specific; you'll have to read the documentation to figure that out) or if you're using an expression string as a numeric value:
% set val (1<<0)
(1<<0)
% expr {$val + 1}
can't use non-numeric string as operand of "+"
but that wouldn't produce exactly the error you are seeing. Very puzzling indeed!
Use Stack Traces
There is something that might help you figure out what is going on. After an error, the global errorInfo variable has a stack trace generated. For example, after the above erroring expr it has this:
% puts $errorInfo
can't use non-numeric string as operand of "+"
while executing
"expr {$val + 1}"
The good thing is that this tells you exactly what command and where gave you the error; that can make a gigantic difference in your detective work to hunt down your problems.

ns: 217: invalid command name "217" while executing "217"

I am simulating Wireless Sensor Network using NS2.35 and I get an error saying
ns: 217: invalid command name "217"
while executing
"217"
I have no where used such command throughput my tcl file. Can any one help why I get this error?
You've probably used a variable containing a numeric value as a command name, perhaps by putting it at the start of a line or by placing [brackets] around it (because brackets do command substitution). The brackets can be even embedded in a string:
This example demonstrates what I mean:
set xyz 217
puts "This is [$xyz] in brackets"
If you want to print some literal brackets out around a variable, you have to add some backslashes:
set xyz 217
puts "This is \[$xyz\] in brackets"
The problem could also be if you've got a command that returns 217 and you've put brackets around it at the start of a line (or in other places where a command is expected):
proc xyz {} {
return 217
}
[xyz]
You've not shown us your code so which exact possibility it is… we can't tell. But I bet it'll be one of these problems. Tcl cares about its syntax characters, and is very exacting about making sure they do what they say they do.
invalid command name "217" :
"217" is an internal command in your 'ns' executable.
Please tell which changes you made to ns-2.35/, if any. (WSN ?)
And please upload your "wsn.tcl" file to e.g. 'Google Docs'.
ns2

Creating Function in Octave

I'm beginning with octave. I've created a file called squareThisNumber.m in My Documents with the following code:
function y = squareThisNumber(x)
y = x^2;
I set the directory to look at My Documents with
cd 'C:\Users\XXXX\My Documents'
I type "squareThisNumber(3)" into octave, and all I'm getting is "Error: 'squareThisNumber' undefined near line 3 column 1." What am I doing wrong?
EDIT:
When I type ls into octave, I get "error: ls: command exited abnormally with status 127". Did I not install Octave correctly?
This behavior sure does seem like there's a problem with octave's current working directory. Does the command dir or pwd also have the same problem?
But you might be able to ignore all of that by
addpath("C:\Users\XXXX\My Documents");
Did you place the end keyword at the end? Code example below works perfectly for me
https://saturnapi.com/fullstack/function-example
% Welcome to Saturn's MATLAB-Octave API.
% Delete the sample code below these comments and write your own!
function y = squareThisNumber(x)
y = x^2;
end
squareThisNumber(9)

How do I use the eval statement in tcl

So basically in my tcl script I generate a line of tcl code that I have to execute( ie I come up with this instruction during runtime). Once I have the tcl instruction say for example puts "hello world $test_var". How do I execute this using tcl?
Do I use the eval command or what?
The eval command is a reasonable way of doing this, but you might want to consider using catch instead, since that will trap any problems found during the evaluation of the generated code, for example like this:
# Generate the code somehow
set variable {puts "hello word $test_var"}
# Execute and trap problems
if {[catch $variable resultMsg]} {
# An error (or other exception) happened
puts stderr "BOOM, Error! $resultMsg"
}
Instead of using [eval] which works perfectly well, in newer versions of Tcl you can use the {*} syntax.
For example:
set cmd "puts"
lappend cmd "abcd ef"
{*}$cmd
Note that it's a good habit to use list commands to build up your command so you wont run into quoting issues.
I'm afraid you don't have another reasonable choice than to use the eval command, e.g.:
set variable {puts "hello world $test_var"}
eval $variable