Problem using function in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem using function in awk
# 1  
Old 09-05-2010
Problem using function in awk

I created two functions that output two random variables. I want to output them in the output file. But it does not seem to work.

Code:
# Function rgaussian1(r1, r2)
# Gaussian random number generator
  function rgaussian1(r1, r2) {
      pi = 3.142
      v1 = sqrt( -2 * log(rand()) )
      v2 = 2 * pi * rand()
      r1 = a * sin(b)
      r2 = a * cos(b)
  }

# Function rgaussian2(r1, r2)
# Gaussian random number generator
  function rgaussian2(r1, r2) {

      do {
          v1 = 2 * rand() - 1
          v2 = 2 * rand() - 1
          rsq = v1 * v1 + v2 * v2
      } while (rsq > 1)

      fac = sqrt(-2 * log(rsq) / rsq)
      r1 = v2 * fac
      r2 = v1 * fac

  }

# Include gaussian distributed random numbers
  NF == 2 {
      rgaussian1(r1, r2)
      rgaussian2(r3, r4)
      print $0,$2,r1,r2,r3,r4
  }

# 2  
Old 09-05-2010
Code:
$ 
$ awk '
function rgaussian1(r1, r2)
{      pi = 3.142
      v1 = sqrt( -2 * log(rand()) )
      v2 = 2 * pi * rand()
      r1 = a * sin(b)
      r2 = a * cos(b)
      print r1, r2
}
function rgaussian2(r1, r2)
{
      do {
          v1 = 2 * rand() - 1
          v2 = 2 * rand() - 1
          rsq = v1 * v1 + v2 * v2
      } while (rsq > 1)
      fac = sqrt(-2 * log(rsq) / rsq)
      r1 = v2 * fac
      r2 = v1 * fac
      print r1, r2
}
BEGIN {
  rgaussian1(0.556, 0.677);
  rgaussian2(0.799,0.808)
}'
0 0
-0.196896 0.195777
$ 
$

tyler_durden
# 3  
Old 09-05-2010
A couple of things that might be causing you problems:

1) I assume that you want to invoke your functions with the values from the input line ($1 and $2) and not r1/r2 which are not defeined.

2) If the result of your computation is 0, print will assume the empty string and not 0 and print a blank. print r1+0 will force it to print a number.
# 4  
Old 09-05-2010
Another thing to note is that the logic of these functions is such that they return the same values for different arguments.

Code:
$ 
$ 
$ awk '
function rgaussian1(r1, r2)
{      pi = 3.142
      v1 = sqrt( -2 * log(rand()) )
      v2 = 2 * pi * rand()
      r1 = a * sin(b)
      r2 = a * cos(b)
      print r1, r2
}
function rgaussian2(r1, r2)
{
      do {
          v1 = 2 * rand() - 1
          v2 = 2 * rand() - 1
          rsq = v1 * v1 + v2 * v2
      } while (rsq > 1)
      fac = sqrt(-2 * log(rsq) / rsq)
      r1 = v2 * fac
      r2 = v1 * fac
      print r1, r2
}
BEGIN {
  rgaussian1(200, 200);
  rgaussian2(0.1254544647, 0.57385902382745)
}'
0 0
-0.196896 0.195777
$ 
$ 

With some diagnostics added:

Code:
$ 
$ awk '
function rgaussian1(r1, r2)
{
  print "BEGIN rgaussian1 =>",r1, r2, r1+r2, r1*r2;
  pi = 3.142
  v1 = sqrt( -2 * log(rand()) )
  v2 = 2 * pi * rand()
  r1 = a * sin(b)
  r2 = a * cos(b)
  #print r1, r2
  print "END rgaussian1 =>",r1, r2, r1+r2, r1*r2;
}
function rgaussian2(r1, r2)
{
  print "BEGIN rgaussian2 =>",r1, r2, r1+r2, r1*r2;
  do {
    v1 = 2 * rand() - 1
    v2 = 2 * rand() - 1
    rsq = v1 * v1 + v2 * v2
  } while (rsq > 1)
  fac = sqrt(-2 * log(rsq) / rsq)
  r1 = v2 * fac
  r2 = v1 * fac
  #print r1, r2;
  print "END rgaussian2 =>",r1, r2, r1+r2, r1*r2;
}
BEGIN {
  rgaussian1(100, 100);
  rgaussian2(0.1254544647, 0.57385902382745)
}'
BEGIN rgaussian1 => 100 100 200 10000
END rgaussian1 => 0 0 0 0
BEGIN rgaussian2 => 0.125454 0.573859 0.699313 0.0719932
END rgaussian2 => -0.196896 0.195777 -0.00111973 -0.0385477
$ 
$ 

tyler_durden
# 5  
Old 09-05-2010
Input arguments don't matter. They only return values from the function. I thought I would need to pass them through in order to return the values out from the function. I thought of using "return r1" for example. Calling srand() within the BEGIN statement should solve the problem of always getting the same values.
# 6  
Old 09-05-2010
I believe you could use the return statement, but it can return only one value.

Code:
$ 
$ awk '
function rgaussian2(r1, r2)
{
  do {
    v1 = 2 * rand() - 1
    v2 = 2 * rand() - 1
    rsq = v1 * v1 + v2 * v2
  } while (rsq > 1)
  fac = sqrt(-2 * log(rsq) / rsq)
  r1 = v2 * fac
  r2 = v1 * fac
  return r1
}
BEGIN {
  print rgaussian2(1.234586, 8.576848)
}'
-0.787923
$ 
$ 

Alternatively, you could return a formatted string with values of r1 and r2, and print them in your function call.

Code:
$ 
$ 
$ awk '
function rgaussian2(r1, r2)
{
  do {
    v1 = 2 * rand() - 1
    v2 = 2 * rand() - 1
    rsq = v1 * v1 + v2 * v2
  } while (rsq > 1)
  fac = sqrt(-2 * log(rsq) / rsq)
  r1 = v2 * fac
  r2 = v1 * fac
  return sprintf("%.20f %.20f",r1,r2)
}
BEGIN {
  x = "Play it again, Sam"
  y = 999
  printf("%s %d %s\n", x, y, rgaussian2(1.234586, 8.576848))
}'
Play it again, Sam 999 -0.78792338859613808566 -0.98884380096718782482
$ 
$ 

tyler_durden
# 7  
Old 09-05-2010
Yes, I think you are right. Can only return one value. But it does not matter in my case. Since they are both random numbers I can just use one of them.

---------- Post updated at 03:33 PM ---------- Previous update was at 02:47 PM ----------

I have this awk script, and I output mean and sigma, but the result is not what I assigned to mean and sigma.

Code:
 
 function rgaussian1() {
      a = rand()
      b = rand()
      c = rand()
      r1 = a + b + c
      return r1
  }

  function rgaussian11(mean, sigma) {
      a = rgaussian1()
      b = mean + (a * sigma)
      print "VALUES: ",mean,sigma
      return b
  }

  NF == 2 {
      a = 5
      b = 0.5
      r1 = rgaussian1()
      r2 = rgaussian11(a,b)
      print $0,$2,r1,r2
  }

As you can see below I am not getting the values of mean and sigma that I have passed to the function.



Code:
VALUES:  0.319071 0.692849
10 0  0 1.50904 -0.654771 1.0627 0.833281
VALUES:  0.244565 0.468954
13 5.74484  5.74484 2.47868 1.11852 0.990984 -0.889183
VALUES:  0.787803 0.202516
16 10.1769  10.1769 0.570419 -0.736454 1.48426 1.09622
VALUES:  0.187611 0.611989
19 13.8527  13.8527 1.40469 -0.309851 0.53924 -1.40168
VALUES:  0.669174 0.450629
22 16.8957  16.8957 1.38384 0.0894231 -0.804437 -0.168806
VALUES:  0.523561 0.360272
25 19.3552  19.3552 1.05967 1.17779 2.0299 -0.459928
VALUES:  0.749601 0.872226
28 21.3932  21.3932 1.02497 -0.17011 -1.12271 0.916729
VALUES:  0.39957 0.0115686
31 23.0869  23.0869 0.713036 -0.15845 -0.852606 -0.867672
VALUES:  0.488232 0.786265
34 24.5867  24.5867 1.38646 0.907416 0.215826 -0.207289
VALUES:  0.721516 0.283772
37 25.9775  25.9775 1.2652 0.18478 0.643813 1.46849
VALUES:  0.796942 0.08114
40 27.2779  27.2779 1.07348 2.44546 -0.369929 0.784368
VALUES:  0.32347 0.847031
43 28.5486  28.5486 1.86091 -0.337079 0.683475 0.124624
VALUES:  0.858226 0.607827
46 29.754  29.754 1.81017 0.697858 -0.547066 1.12782
VALUES:  0.096079 0.495649
49 30.9075  30.9075 1.82609 1.88842 -0.843563 0.535018
VALUES:  0.601441 0.696511
52 32.0179  32.0179 1.4897 0.233247 -0.0728211 -1.17591
VALUES:  0.460437 0.181952
55 33.0682  33.0682 0.657609 -0.594157 -0.79817 -0.281863
VALUES:  0.630648 0.42759
58 34.082  34.082 1.48051 0.869132 0.319669 -0.426564
VALUES:  0.446626 0.770096
61 35.0402  35.0402 1.70818 -0.775314 -0.0754717 -0.160226
VALUES:  0.127291 0.985517
64 35.9725  35.9725 1.59898 -1.89246 -0.441951 0.257042
VALUES:  0.970425 0.0665038

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

3. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

4. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

5. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

6. Shell Programming and Scripting

splice function problem

Hi All, I am using splice function in for loop to delete particular element from array with one condition. my $cnt=0; foreach my $elem (@result) { if (condition){ splice(@result, $cnt, 1);} else{ $cnt++;} } Now when in array, two elements comes sequentially with the... (3 Replies)
Discussion started by: gentleDean
3 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

9. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

10. Programming

Problem with aio_write() function

Hello, How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7? I have filled the control block struct aiocb as follows: aio_sigevent.sigev_signo = SIGEV aio_sigevent.sigev_notify = SIGEV_THREAD Then I have filled the call back function in ... (0 Replies)
Discussion started by: hmurali
0 Replies
Login or Register to Ask a Question