how to programmatically define parameters and assign values

how to programmatically define parameters and assign values - Messages

#1 Posted: 11/25/2021 10:19:27 AM
Stefano

Stefano

0 likes in 22 posts.

Group: User

I am little bit at loss.

I would like to build a function that will extract a row of data from a matrix and assign each columns to a named parameter. Basically I want a vlookup function + paramters assignment.

I got the vlookup part working quite well (for my needs), but I'm unable to assign programmatically the extracted value to the proper variable. Here an example:

2021-11-25 13_59_30-SMath Solver - [example.sm].png
example.sm (14 KiB) downloaded 31 time(s).

As you can see the vlookup function (1) works (I know I'm cheating a little bit because it works based on row index and not on a condition being checked, but it is fine for now), and also the manual assignment (2) works as intended, but I would like to automate that so that I just need to invoke the function to update the parameters.

I've tried to put the assignment ( "a:=..." ) inside the function, but does not work (I guess something to do with local and global variables?).

My final goal is to build a library of equations to be included() as needed, and then invoke functions to set up the parameters to be actually used in the worksheet, without the need to rewriting or copy and pasting the same code for each worksheet.

BONUS: if it would be possible to read the parameter name from the header and then assign it the proper value, it would be sweet: let's say the header is '"var.1"', then the function will create 'var.1:=...' without the double quotation mark ( " ).

Am I out of my mind?
#2 Posted: 11/25/2021 12:02:47 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi. As far as I know, there are two methods to modify a variable from a SMath function. One is modifying in the body of the function one of the parameters that has been passed, which must be done explicitly. That is, f (a, x) can only modify variables x or a, but not others. The second way is to create a string that assigns any variable and executing that string in the scope that you need that assignment, that is, global from the worksheet or local if you do it within a function.

img0.png

Hope that helps.
Best regards.
Alvaro.
#3 Posted: 11/25/2021 1:12:52 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

My final goal is to build a library of equations to be included() as needed, and then invoke functions to set up the parameters to be actually used in the worksheet, without the need to rewriting or copy and pasting the same code for each worksheet.


Include works charming well if done right.
Download both in same Smath session ... explore two different fits.

For the remaining of your visit ... very many questions.
Please, don't hesitate for more ... Jean

Genfit Minimize Include.sm (31 KiB) downloaded 37 time(s).
example.sm (35 KiB) downloaded 34 time(s).
Genfit Minimize Include Test.sm (58 KiB) downloaded 33 time(s).

#4 Posted: 11/26/2021 5:33:20 AM
Stefano

Stefano

0 likes in 22 posts.

Group: User

Wrote

Hi. As far as I know, there are two methods to modify a variable from a SMath function. One is modifying in the body of the function one of the parameters that has been passed, which must be done explicitly. That is, f (a, x) can only modify variables x or a, but not others. The second way is to create a string that assigns any variable and executing that string in the scope that you need that assignment, that is, global from the worksheet or local if you do it within a function.

img0.png

Hope that helps.
Best regards.
Alvaro.



Ah I didn't expect the num2str()/str2num() to work that way!

Following your example I written a function that will take a key, value pair and assign the value to the proper key, but the line() thing is throwing me off a little bit: if I use it, then declared variables would be local, else if I don't use it the function will throw an error and will not work.

Best I can do is a 2 functions approach:

  1. the first will extract a vector of [key, value] pairs
  2. the second will declare the variables using the vectorize function



Ideally I would like this two functions to be wrapped in only one, but I am not able to.

example.sm (28 KiB) downloaded 30 time(s).
#5 Posted: 11/26/2021 6:01:06 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Hi. Your worksheet have only a couple of non related issues.

For the vectorized case you need to ensure that what goes to be vectorized is evaluated, if not vectorize function try to do its task to the argument of declare_var too. One workaround is store the argument of declare_var in one temp variable

img1.png

Your for loop version is correct to, but you need to let the loop "free" at the worksheet or, if you want to assign it a variable like f, as you do, you need to end the loop with some exit value

img2.png

img3.png

Best regards.
Alvaro.
#6 Posted: 11/26/2021 8:10:08 AM
Stefano

Stefano

0 likes in 22 posts.

Group: User

Wrote

Hi. Your worksheet have only a couple of non related issues.

For the vectorized case you need to ensure that what goes to be vectorized is evaluated, if not vectorize function try to do its task to the argument of declare_var too. One workaround is store the argument of declare_var in one temp variable


img1.png



I did this in the first page of the worksheet, and then I tried to combine in one go without success.

2021-11-26 11_47_17-SMath Solver - [example (2).sm_].png

Quote



Your for loop version is correct to, but you need to let the loop "free" at the worksheet or, if you want to assign it a variable like f, as you do, you need to end the loop with some exit value

img2.png

img3.png

Best regards.
Alvaro.



even if I let the for-loop "free" at the worksheet, it will work, but I still get an error ( "there is no result" ):

2021-11-26 11_57_24-SMath Solver - [example (2).sm_].png

the "=" in this case is needed to trigger the evaluation of the for-loop. Without it, it wouldn't do anything. In my previous version I tried instead the "f:=for(...)" because then the for-loop would get evaluated, but if I put the for-loop in the line() as in your example, I "entrap" the "declare_vars()" in the local scope and would not define the variables in the global scope.

As of now, I see no other solution than to use the two functions approach with the temporary variable to pass to declare_var().




#7 Posted: 11/26/2021 12:59:43 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

Wrote

... I did this in the first page of the worksheet, and then I tried to combine in one go without success. ...



You're right, I don't see that. BTW I think that from the view point of SMath, this is a only one instruction, because line:

img1.png


Wrote



even if I let the for-loop "free" at the worksheet, it will work, but I still get an error ( "there is no result" ):

...

the "=" in this case is needed to trigger the evaluation of the for-loop. Without it, it wouldn't do anything. In my previous version I tried instead the "f:=for(...)" because then the for-loop would get evaluated, but if I put the for-loop in the line() as in your example, I "entrap" the "declare_vars()" in the local scope and would not define the variables in the global scope.



You can 'force' or 'trigger' or or whatever it takes for the loops to realize that it exists and matters assigning a dummy variable in the loop body

img2.png

Wrote



As of now, I see no other solution than to use the two functions approach with the temporary variable to pass to declare_var().



I guess that this is another approach wich can work and it is which I use

img3.png

example.sm (34 KiB) downloaded 51 time(s).

Best regards.
Alvaro.
  • New Posts New Posts
  • No New Posts No New Posts