Simple range and domain function on ti-nspire cx cas not working - ti-nspire

I am trying to code a simple function in the program editior of ti nspire cas cx.
Define LibPub transsolabcd()=
Prgm
:Request "Enter original expression: ",expr1,0
:Request "Enter mapped expression: ",expr2,0
:Request "Enter respect var: ",xvar,0
:Local a,b,c,d
:Define exprf1(xvar)=Func
: Return expr1
:EndFunc
:Define exprf2(xvar)=Func
: Return expr2
:EndFunc
:Disp (exprf1(2))
:EndPrgm
Disp (exprf1(2)) - This line here does not substitute for the value entered into the function, instead returns the entire function that was requested earlier. What I am doing wrong here?

Try this (assuming xvar is x):
Define LibPub transsolabcd()=
Prgm
:Request "Enter original expression: ",expr1,0
:Request "Enter mapped expression: ",expr2,0
:Request "Enter respect var: ",xvar,0
:Local a,b,c,d
:Define exprf1(xvar)=Func
: Return expr1|x=xvar
:EndFunc
:Define exprf2(xvar)=Func
: Return expr2|x=xvar
:EndFunc
:Disp (exprf1(2))
:EndPrgm
The part I added, |x=xvar, substitutes x with the value of xvar, which is 2 in your function call.

Related

Octave: how to answer an input() prompt with a variable from my workspace?

In the command window, I define a variable var = 10. I call My_Function, and it says "Input a value: ". I want to respond " var " and have it evaluate that as 10. Is there a way to pass in the variable through input() or any other similar command?
I know how to do My_Function(var) and pass it in that way, but is there a way to take in variables at a user input prompt during the function?
This is how the input function behaves by default, it runs the content user wrote as it is code. Make sure you are not using the 's' parameter that is commonly used to make octave interpret the input as text, instead of code.
>> var = 1;
>> x = input('Please enter the value: ')
Please enter the value: var
x = 1
Since it runs as a code, you can also do any operation here, including calling other functions.
>> x = input('Please enter the value: ')
Please enter the value: sind(30)
x = 0.5000
This works on the current workspace. If you want to reach the variables in another workspace, like the base one, you need to explicitly define that the input function should run on the base workspace.
function [] = My_Function ()
x = evalin("base","input('Please enter the value: ')");
disp(x)
endfunction
>> My_Function()
Please enter the value: var
1
In MATLAB you can make My_Function a nested function, so all the variables in the caller workspace are visible inside the nested function. E.g.,
function Top_Function
var = 10;
result = My_Function
function x = My_Function % This is nested inside Top_Function
x = input('Input a value: ');
end
end
My_Function( ) will see all the variables in Top_Function unless they are shadowed by My_Function variables. So you can use var in the input( ) statement and it will evaluate to 10 in the above example.

How to reduce the complexity and automate my approach for building a struct?

I have the following macro and it should allow me to build a struct with one or 2 arguments only!
macro baseStruct(name, arg)
if length(arg.args)==2 && isa(arg.args[1],Symbol) || length(arg.args)==1
aakws = Pair{Symbol,Any}[]
defaultValues=Array{Any}(nothing,1)
field_define(aakws,defaultValues,1,arg,first=true)
:(struct $name
$(arg.args[1])
function $name(;$(arg.args[1])=$(defaultValues[1]))
new(check($name,$(arg.args[1]);$aakws...))
end
end)
else length(arg.args)==2 && !isa(arg.args[1],Symbol)
aakws1 = Pair{Symbol,Any}[]
aakws2 = Pair{Symbol,Any}[]
defaultValues=Array{Any}(nothing,2)
field_define(aakws1,defaultValues,1,arg)
field_define(aakws2,defaultValues,2,arg)
:(struct $name
$(arg.args[1].args[1])
$(arg.args[2].args[1])
function $name(;$(arg.args[1].args[1])=$(defaultValues[1]),$(arg.args[2].args[1])=$(defaultValues[2]))
new(check($name,$(arg.args[1].args[1]);$aakws1...),check($name,$(arg.args[2].args[1]);$aakws2...))
end
end)
#baseStruct test(
(arg1,(max=100.0,description="this arg1",min=5.5)),
(arg2,(max=100,default=90))
)
The macro will expand to:
struct test
arg1
arg2
end
and the following instance:
test1=test(arg1=80.5)
should give:
test1(arg1=80.5,arg2=90)
#check_function returns the argument. It allows me to check the argument in a specific way.
check(str,arg;type=DataType,max=nothing,min=nothing,default=nothing,description="")
#do somthing
return arg
end
#field_define_function it takes the second parameter from the macro as Expr. and convert it to a array with Pair{Symbol, Any} and then assign it to aakws.
I can extend this Code for more arguments, but as you can see it will be so long and complex. Have anybody a tip or other approach to implement this code for more/unultimate arguments in a more efficient way?
I don't have the code for field_define or check, so I can't work your example directly. Instead I'll work with a simpler macro that demonstrates how you can splat-interpolate a sequence of Expr into a quoted Expr:
# series of Expr like :(a::Int=1)
macro kwstruct(name, arg_type_defaults...)
# :(a::Int)
arg_types = [x.args[1]
for x in arg_type_defaults]
# :a
args = [y.args[1]
for y in arg_types]
# build :kw Expr because isolated :(a=1) is parsed as assignment
arg_defaults = [Expr(:kw, x.args[1].args[1], x.args[2])
for x in arg_type_defaults]
# splatting interpolation into a quoted Expr
:(struct $name
$(arg_types...)
function $name(;$(arg_defaults...))
new($(args...))
end
end
)
end
This macro expands #kwstruct D a::Int=1 b::String="12" to:
struct D
a::Int
b::String
function D(; a = 1, b = "12")
new(a, b)
end
end
And you can put as many arguments as you want, like #kwstruct(F, a::Int=1, b::String="12", c::Float64=1.2).
P.S. Splatting interpolation $(iterable...) only works in quoted Expr, which look like :(___) or quote ___ end. If you're constructing with an Expr() call, just use splatting:
vars = (:b, :c)
exq = :( f(a, $(vars...), d) ) # :(f(a, b, c, d))
ex = Expr(:call, :f, :a, vars..., :d) # :(f(a, b, c, d))
exq == ex # true

Why is constness not respected inside these julia functions?

Prompted by Lyndon's question earlier today:
a.
julia> function f1(x::Float64)
const y = x;
y = "This should throw an error since y is of constant type";
return y;
end
f1 (generic function with 1 method)
julia> f1(1.0)
"This should throw an error since y is of constant type"
Why does the const keyword not work as expected here? (i.e., disallow assigning a string to y which has been declared as const).
b.
julia> function f2(x::Float64)
show(x);
const x = 1.;
end
f2 (generic function with 1 method)
julia> f2(1.0)
ERROR: UndefVarError: x not defined
Stacktrace:
[1] f2(::Float64) at ./REPL[1]:2
Why does defining x as const on line 3 affect the value of x on line 2?
c.
In particular, this prevents me from doing:
function f(x::Float64)
const x = x; # ensure x cannot change type, to simulate "strong typing"
x = "This should throw an error";
end
I was going to offer this as a way to simulate "strong typing", with regard to Lyndon's "counterexample" comment, but it backfired on me, since this function breaks at line 2, rather than line 3 as I expected it to.
What is causing this behaviour? Would this be considered a bug, or intentional behaviour?
Naming conventions aside, is there a more acceptable way to prevent an argument passed into a function from having its type altered?
(as in, is there a defined and appropriate way to do this: I'm not after workarounds, e.g. creating a wrapper that converts x to an immutable type etc)
EDIT:
So far, this is the only variant that allows me to enforce constness in a function, but it still requires the introduction of a new variable name:
julia> function f(x::Float64)
const x_const::Float64 = x;
x_const = "helle"; # this will break, as expected
end
but even then, the error just complains of an "invalid conversion from string to float" rather than an "invalid redefinition of a constant"
Because const in local scope is not yet implemented:
https://github.com/JuliaLang/julia/issues/5148

How to write symbols to file in scilab

I'm working on symbolic toolkit. Trying to solve some equations and that's a long string of symbols such as x= a1+a2^3+b0*b1... upto 80,000(80k) characters.
So I needed to put that in file.
mputstr() ans other wrting functions are not working since they are symbols.
Error is thrown as: not a string or specified format.
Does any method can help to bring down the variable to file.
code is :
Syms aa ab ac
z=ab^6*ac^6*ad^3*ba^3*bg^3*bh^3+3*aa^4*ab^6*ac^6*ad^4*ba^4*bg^2*bh^2+3*aa^5*ab^6*ac^6*ad^5*ba^5*bg*bh+aa^6*ab^6*ac^6*ad^6*ba^6
mputstr({char(z)},fd)
>>error 10000
>>char: Wrong type for input argument: Cell expected.
at line 95 of function char called by :
mputstr(z,fd)
>> !--error 999
>mputstr: Wrong type for input argument #1: A string expected.
p=string(z)
mputstr(p,fd)
>>!--error 999
>mputstr: Wrong type for input argument #1: A string expected.
mfprintf("%s",z)
>> !--error 246
>>Function not defined for given argument type(s),
check arguments or define function %c_mfprintf for overloading. ..
Let's say you have a symbolic equation x:
syms a b c
x = a + b * c
Here, x denotes a symbolic variable, so you cannot directly write it to a file. You need to convert it to a character array first. So you should be using something like
fd = mopen( this_file, "wt" );
mputstr( char(x), fd );
mclose( fd );
I think that #bremen_matt's answer is the good one, but with a modification.
If your "Syms" variables are something complex so char() and string() cannot be used, why you are not creating your own conversion function?
Please, see below my modification of #bremen_matt example:
syms a b c
x = a + b * c
fd = mopen( this_file, "wt" );
mputstr( syms_to_string(x), fd );
mclose( fd );
The syms_to_string() is returning a string of the information that you would like to print of the symbol x, and same function could be used to print other symbols (e.g. a). Of course, the syms_to_string() function could be better defined using overloading.

not enough argument, self-defined function in matlab

I am a newbie of matlab and am trying to define a pretty complex function to plot it. The content of file is following:
function [res] = distribution (input)
a = factorial(30)
b = factorial(input) * factorial(30-input)
c = power(0.05, input)
d = power(0.95, 30-input)
a/b*c*d
end
in the file named distribution with .m extension. But when I run it error returns: "Error using distribution (line 4). Not enough input arguments."
I read through the "Getting Started" and find no solution. Does anyone have suggestions on this?
The name of the single argument to your function distribution(..), namely argument input, conflicts with the existing native input command of Matlab,
input: Prompt for user input.
...
x = input(prompt)
Try choosing a different name of this argument (in example below: foo), and also remember to return your result by assigning it to the return variable res:
function res = distribution (foo)
a = factorial(30);
b = factorial(foo) * factorial(30-foo);
c = power(0.05, foo);
d = power(0.95, 30-foo);
res = a/b*c*d; % <--- note, return parameter assignment
end