WikiExpressions.expr

From Odwiki

Jump to: navigation, search
# wikiExpressions.expr 
#    - A library of expressions which can be read in using the hscript command "exread" - insert into your 456.cmd file.
#      If you have useful expressions to contribute, please add them here.

# compute gamma
#
float 
gamma( x, g )
{
if( g == 0 )
  return 0; 
return pow(x,1/g);
}

# compute bias 
#
float
bias( x, g )
{
return pow(x,log(g)/log(0.5));
} 

# compute gain 
#
float
gain( x, g )
{
if( x < 0.5 )
  return bias( 2*x, 1-g )/2;
return 1 - (bias(2-2*x,1-g)/2);
}
 
# return a number in Gaussian distribution
#
float ExpGaussRand( idum )
{
        idum = idum * -1;
        v1 = 2.0 * rand(idum) - 1.0;
        idum = idum + .01;
        v2 = 2.0 * rand(idum) - 1.0;
        idum = idum + .01;
        rsq = v1 * v1 + v2 * v2;
        while (rsq >= 1.0 || rsq == 0.0) {
                v1 = 2.0 * rand(idum) - 1.0;
                idum = idum + .01;
                v2 = 2.0 * rand(idum) - 1.0;
                idum = idum + .01;
                rsq = v1 * v1 + v2 * v2;
        }
        fac = sqrt(-2.0 * log(rsq) / rsq);
        return v2 * fac;
}

# This one of the odWiki CustomExpressions which uses PERL to return a substitution of a RegularExpression
# e.g  hscript> echo `~ExpRegSub("hello","ll","foobar")`
#      result: hefoobaro
#                               credit: AntioneDurr
#
string ExpRegSub(string source; string from; string to;)
{
  string perlstr = "";
  perlstr = "echo '" + source + "' | perl -pe 's/" + from + "/" + to + "/g'";
  return system(perlstr);
}

# This one of the odWiki CustomExpressions which uses PERL to derive a legal node name from an English description.
# e.g hscript> echo `ExpLegalizeName("Big Dog_Model (Proxy) 3")`
#     BigDog_ModelProxy3
#                               credit: AntioneDurr
#
string ExpLegalizeName(string source)
{
  string perlstr = "";
  perlstr = "echo '" + source + "' | perl -pe 's/^[[:digit:]]*|[^[:alnum:]^_]//g'";
  return system(perlstr);
}
© 2009 od[force].net | advertise