Counterglow Forums  

Go Back   Counterglow Forums > General Discussions > Technoglow

Reply
 
Thread Tools Display Modes
Old 17-02-2004, 01:36:50   #1
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Sorry to bug you guys again, but more c problems

Code:
#include <stdio.h>
#include <math.h> 
#include "nr.h"
#include "nrutil.h"

main()

{

  float *x, *y, *X, *Y;
  float a, s0, M, g, w0;
  float **s, **w;
  float *Dx, *Dy;
  float **f, **F;
  float *I;
  int i;
  int j;
  int N = 11;

  x = vector (0, N-1);
  y = vector (0, N-1);
  X = vector (0, N-1);
  Y = vector (0, N-1);
  w = matrix (0, N-1, 0, N-1);
  s = matrix (0, N-1, 0, N-1);
  Dx = vector (0, N-1);
  Dy = vector (0, N-1);
  f = matrix (0, N-1, 0, N-1);
  F = matrix (0, N-1, 0, N-1);
  I = vector (0, N-1);

  a = 0.054;
  s0 = 8328.4;
  M = 91.187;
  g = 2.494;
  w0 = 41.54;


  for(i = 0; i < N; i++)
    x[i] = i / 2; 

  for (j = 0; j < N; j++)
    y[j] = j;
  
  for (i = 0; i < N; i++)
    X[i] = a * pow((1-x[i]), (a-1));  
  
  for (j = 0; j < N; j++)
    Y[j] = a * pow((1-y[j]), (a-1));
  
  for(i = 0; i < N; i++)
    {for(j = 0; j < N; j++)
      s[i][j] = x[i] * y[j] * s0;
    }

  for(i = 0; i < N; i++)
    Dx[i] = a * pow((1 - x[i]), (a-1));

  for(j = 0; j < N; j++)
    Dy[j] = a * pow((1 - y[j]), (a-1));

  for(i = 0; i < N; i++)
    {for(j = 0; j < N; j++)  
      w[i][j] = w0 * s[i][j] * g * g / ( ( s[i][j] - M * M ) * ( s[i][j] - M * M ) + s[i][j] * s[i][j] * g * g / ( M * M));
    }

  for(i = 0; i < N; i++)
    {for(j = 0; j < N; j++)  
      f[i][j] = Dx[i] * Dy[j] * w[i][j];
    }

/*   for(i = 0; i < N; i++) */
/*     {for(j = 0; j < N; j++) */
/*       f[i][j] = Dx[i] * Dy[j]; */
/*     } */

  F[0][j] = 0.0;

  for(i = 2; i < N; i = i + 2)
    {for(j = 0; j < N; j++)
      F[i][j] = F[i-2][j] + (f[i-2][j] / 3 + 4 * f[i-1][j] / 3 + f[i][j] / 3) / (N-1);
    }

  I[0] = 0.0;

  for(j = 2; j < N; j = j + 2)
    I[j] = I[j-2] + (F[N-1][j-2] / 3 + 4 * F[N-1][j-1] / 3 + F[N-1][j] / 3) / (N-1);


/*   for(i = 0; i < N; i= i + 2) */
    printf("%18.15f\n", x[5]);

}
Now, most of this is irrelevant right now (it's supposed to be 2-d integration of some shit function via simpson's rule, but no matter)

I was getting a 0 for my integration, so I decided to do some checking.

The relevant part is

float *x, *y, *X, *Y;

int i;
int j;
int N = 11;

x = vector (0, N-1);
y = vector (0, N-1);

for(i = 0; i < N; i++)
x[i] = i / 2;

printf("%18.15f\n", x[5]);

I tell it to print 5/2 and it gives me 2.0000000

I defined x[i] as float; what's the problem?

Last edited by KrazyHorse; 17-02-2004 at 02:13:45.
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:41:55   #2
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
You defined *x as a float. you need to define x as an array with a specific number of variables.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 01:42:39   #3
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Don't understand. Please explain.
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:43:33   #4
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
This same procedure worked last time for different problem (writing float *b and then writing b[i] = some shit)
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:46:18   #5
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Just wrote this :

float m;

m = 1/15;

printf("%18.15f\n", m);

got back 0.000000000000000
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:49:25   #6
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
If I write 1.000000000/15.000000 though I get the right answer.

Is it possible this is my problem?
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:50:34   #7
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
It might work if *b is the last variable you define, only because there'll be nothing after it in memory. The proper way do declare an array though, is to declare it as
Code:
float x[11];
or however many elements you want to have. It would be better for your program to include the statement
Code:
#define N 11
after your includes and then define all your arrays with
Code:
float x[N], y[N], X[N], Y[N];
as this will create set arrays going from 0 to N-1.

Last edited by TV4Fun; 17-02-2004 at 04:22:30.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 01:52:16   #8
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Also in order for your division of constants to work you need to specify them as floats in the program. The proper way to do this would be to write them as 1.0f/15.0f as you don't need more than one 0 and omitting the f will make the compiler think that they are doubles.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 01:53:12   #9
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Ah.

But I was told that if I defined x = vector(0, N-1) after writing float *x then this would work (it's possible that the vector notation is a special thing in my numerical recipes library)
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 01:56:54   #10
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
I have not seen the vector function, I could not tell you how it works, but from what you're doing it looks like you should just define your variables as arrays and leave out the vectors.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:00:26   #11
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Quote:
Originally posted by TV4Fun
Also in order for your division of constants to work you need to specify them as floats in the program. The proper way to do this would be to write them as 1.0f/15.0f as you don't need more than one 0 and omitting the f will make the compiler think that they are doubles.
Just figured something out; to divide my indices I need to create new floats then define these floats to be equal to my indices

i.e.

int i, j;
int N = 11;

float r, s, t;

r= i
s= j
t = N

This seems to work
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:05:21   #12
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
You could just cast your indices as floats. You would do that by writing static_cast<float>(i) in place of i in youe calculations.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:06:01   #13
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Okay. Sounds good, because my workaround isn't working.
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:06:58   #14
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
So the proper way to write that would be
Code:
x[i] = static_cast<float>(i) / 2.0f;
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:08:03   #15
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
And rememver to use the proper indentation. It makes your code much more readable.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:08:19   #16
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
What library do I need to include? static_cast gives me an undeclared error
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:08:53   #17
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Quote:
Originally posted by TV4Fun
And rememver to use the proper indentation. It makes your code much more readable.
I am here, but the code doesn't post properly on CG
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:09:35   #18
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Sorry, static_cast is from c++, I believe for c it is float(i).
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:10:13   #19
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Quote:
Originally posted by KrazyHorse
I am here, but the code doesn't post properly on CG
Put it inside [code] tags.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:12:49   #20
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
parse error before float
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:14:21   #21
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Quote:
Originally posted by TV4Fun
Put it inside [code] tags.
Corrected
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:14:25   #22
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
It might be (float) i
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:15:34   #23
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Brilliant.

Thanks TV
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:16:17   #24
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
No problem. I love this kind of stuff.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 02:18:36   #25
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
It's outputting a number. I have no idea if it's the right number or not, but it's better than 0.
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:22:52   #26
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
giving me 1.5 * 10 ^ (-3)
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:26:26   #27
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Wow. When I increase step number to N = 400 that answer jumps to 0.400

It is a very steeply varying function of x (there is an integrable singularity at x = 1 and y = 1)
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:30:16   #28
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Hah. When I increased step number to 4001 (increasing total evaluated points steps to somewhere around 16 million) the value goes to 1.416

And the computer takes almost a full minute to run the program.
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 02:43:50   #29
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
It would help if I knew what your program was supposed to do.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 03:01:03   #30
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
It integrates the function D(x1)D(x2)w(s) from x1 = 0 to 1 and x2 = 0 to 1

s = x1 * x2 * s0
D(x) = a / (1-x)^(1-a)
w(s) = w0 * g ^2 / ((s^2 - M^2)^2 + s^2 * g^2 / M^2)

s0, w0, a, g and M are constants

This is a cross-section calculation for the e+e- -> Z process
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 03:01:56   #31
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
Next I get to do it via Monte Carlo integration.


Wheeeeee
KrazyHorse is offline   Reply With Quote
Old 17-02-2004, 03:18:23   #32
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
One big problem is your variables need more descriptive names.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 17-02-2004, 03:23:01   #33
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
That would help.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 04:02:15   #34
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
my variables need to be as quick to type as possible
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-02-2004, 04:04:11   #35
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Try find and replace.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 04:06:36   #36
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
find and replace doesn't work on xemacs

__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-02-2004, 04:07:35   #37
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Try an IDE.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 04:08:00   #38
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Try sed.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 04:11:15   #39
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
I'll keep using what I "know" how to use.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-02-2004, 04:21:52   #40
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Then don't expect people who don't know what your variables mean to be able to help you.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 17-02-2004, 04:23:21   #41
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Quote:
Originally posted by KrazyHorse
It integrates the function D(x1)D(x2)w(s) from x1 = 0 to 1 and x2 = 0 to 1

s = x1 * x2 * s0
D(x) = a / (1-x)^(1-a)
w(s) = w0 * g ^2 / ((s^2 - M^2)^2 + s^2 * g^2 / M^2)

s0, w0, a, g and M are constants

This is a cross-section calculation for the e+e- -> Z process
I must get around to taking calculus so I know what the hell you're talking about.
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 04:32:25   #42
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Can anyone tell me how to get a plain C project with Visual C++ in Visual Studio .NET 2k3 Pro?

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 17-02-2004, 04:52:43   #43
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
Quote:
Originally posted by Sir Penguin
Then don't expect people who don't know what your variables mean to be able to help you.

SP
I can generally narrow down the problem to something I can put into cogent question form.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-02-2004, 04:54:33   #44
JM^3
colossal bender
 
JM^3's Avatar
 
Join Date: Jul 2003
Location: In front of my computer
use fortran, is a lot easier for what you are doing

Jon Miller
__________________
The G in Greg stands for Gaylord.
JM^3 is offline   Reply With Quote
Old 17-02-2004, 04:59:09   #45
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
The other students are using C.

That means I use C too.

Plus I've now got this mass of computer knowledge *snigger* that I don't want to go to waste.

2 assignments down, 10 to go.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-02-2004, 05:01:57   #46
JM^3
colossal bender
 
JM^3's Avatar
 
Join Date: Jul 2003
Location: In front of my computer
well, you are less likely to screw up in fortran

all I am saying

it is much more intuitive

Jon Miller
__________________
The G in Greg stands for Gaylord.
JM^3 is offline   Reply With Quote
Old 17-02-2004, 18:08:25   #47
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Quote:
Originally posted by Sir Penguin
Can anyone tell me how to get a plain C project with Visual C++ in Visual Studio .NET 2k3 Pro?

SP
New Project|VC++ project|Makefile project
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 21:11:20   #48
QtFLW@Work
But you can just call me QtFLW
 
QtFLW@Work's Avatar
 
Join Date: Jan 2004
Location: In the Government Salt Mines
Code:
10 PRINT "HELLO WORLD"
20 GOTO 10
__________________
Counterglow Pity Poster
QtFLW@Work is offline   Reply With Quote
Old 17-02-2004, 21:16:58   #49
TV4Fun
mostly harmless... mostly
 
TV4Fun's Avatar
 
Join Date: Jul 2002
Location: Coventry
Code:
int main()
{
     while (true)
          printf("Hello World!");

     return 0;
}
TV4Fun is offline   Reply With Quote
Old 17-02-2004, 21:34:02   #50
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Quote:
Originally posted by TV4Fun
New Project|VC++ project|Makefile project
Well, it gave me a different error ("Error spawning ''."). I wonder if it has to do with the fact that I don't have a makefile.

This is stupid. Maybe I'll just learn C# instead.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Reply
Forum Jump

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 04:05:24.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© Counterglow 2001-2012. All rights reserved.