Welcome, Guest. Please Login
YaBB - Yet another Bulletin Board
 
  HomeHelpSearchLogin  
 
Return Numeric Value of Variable (Read 2742 times)
Austin Meyer
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 8
Return Numeric Value of Variable
Apr 1st, 2012 at 11:29am
 
Hello,

I am using a codon substitution model that is being imported with variables in it that I set in the hyphy batch script. 

If I set the variable as global and constrain certain values to numerics then everything works fine.  For example, if I do:

global fixed_w := 2;

Then, during optimization the value of fixed_w is properly constrained to the numerical value 2.

On the other hand, if I import a matrix of numbers that look like so:

ws = {{1,0.25} {2,0.5} {3, 0.75} {4, 1}};

If we pretend each of the values in that matrix is associated with a site in a protein.  I want to evaluate each one individually, setting fixed_w to the second value of each one.  It looks like so:

for loop:
global fixed_w := ws[iterator][1];

It appears the value of fixed_w is not set numeric, but rather symbolic causing it to be optimized rather than constrained.  It particular it seems to get set to the name of the iterator variable... which seems very strange to me.

I should add.  Unless I'm totally misguided, I think I'm just looking for a function analogous to the N[] function in Mathematica that forces a numeric value.

Alternatively I could just set a variable constant rather than constrain it, but I'm not sure how to do that either.

Need help!!

Thanks,
Austin
Back to top
« Last Edit: Apr 1st, 2012 at 12:38pm by Austin Meyer »  
 
IP Logged
 
Austin Meyer
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 8
Re: Return Numeric Value of Variable
Reply #1 - Apr 1st, 2012 at 1:11pm
 
So I think I figured it out.  Until now I never understood why there was a way to construct a string and execute that string.  It allows you to execute a line without dealing with the baggage of hidden variables.

Just one more question.  When I construct my line and execute it I often get some weird symbols at the end of the line.  Here is my line construction:

Code:
  gdDefString = "";
  gdDefString*("\n\nglobal fixed_k := " + CalcParam(new_rsa_list[siteCount], ka, kb) + ";");
  fprintf(stdout, gdDefString);
  ExecuteCommands(gdDefString); 



Here is what it outputs:

Code:
global fixed_k := 23.6018;o
global fixed_t := 0.263447;p
 



The characters at the end aren't always the same.  Sometimes they aren't there at all.

Is that a problem?

Austin
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: Return Numeric Value of Variable
Reply #2 - Apr 1st, 2012 at 2:29pm
 
Hi Austin,

Try
Code:
global fixed_w := ws[iterator__][1];
 



Putting the double underscore following a variable name will tell HyPhy to insert the value of the variable in the expression.

You can also go the ExecuteCommands route, but when you build the string buffer (using string_id * something command), follow these three steps:

Code:
some_string = ""; /* this will tell HyPhy that some_string is a string */
some_string * 128; /* this will convert some_string into a buffer with initial size of 128 characters */
some_string * (string expression 1);
...
some_string * (string expression N); /* append strings to the buffer */
some_string * 0; /* close the buffer -- if you don't do this step, there may be garbage at the end because of unused allocated space */
 



You can also use
Code:
string1 = string2+string3+...+stringN;
 


to avoid messing around with string buffers. The '+' operation behaves as expected (concatenation).

Cheers,
Sergei
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
Austin Meyer
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 8
Re: Return Numeric Value of Variable
Reply #3 - Apr 1st, 2012 at 3:06pm
 
Yes thanks for the quick response.  All ways work.  The return numeric value is a useful thing to know.

Thanks,

Austin
Back to top
 
 
IP Logged