Thread: Gnu Scientific Library functions

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    Gnu Scientific Library functions

    I'm trying to figure out gsl - I've just downloaded it.
    I'm really confused about how to actually use the functions. I've definitely got everything worked out so I can pull them into my program, but getting the result is more complicated than I thought it would be. Or maybe I'm just being stupid about something. :P
    But for example, if I just want to print out a random number, can someone explain how that works (just in general, I might be confused about what the library is itself lol)?
    I found the function that returns a random number, and have the appropriate header.
    But what do I do with it?
    I can't just say printf("%e", -function-)...

    All I want to do right now is print out the random number.

    Thanks.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I can't just say printf("%e", -function-)...

    I'm not really clear what problem you're having. are you saying you don't know how to call a function? could you post a more complete code snippet of what you've trying to do?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zdream8 View Post
    I can't just say printf("%e", -function-)...
    Why not? What kind of number is returned (%e prints a floating point number)?

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Can you post the function definition?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    30
    All right, I don't really know what I'm doing, but this is what I have.
    Code:
    #include <stdio.h>
    #include <gsl\gsl_rng.h>
    
    main ()
    {
         double gsl_rng_uniform (const gsl_rng * r);
         printf("%e", gsl_rng_uniform (const gsl_rng * r));
         getchar();
    }
    It says there's a syntax error before "const" in the printf line.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    what you need is a good C programming reference book. period.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    30
    Well I feel like this is a pretty simple problem someone should be able to tell me pretty quickly. I think I'm just bad at phrasing the question, since I'm confused. Same with looking things up - if I knew what to do I would know what to look up. :S

    Oh, and sand_man the definition, like in the above code, is double gsl_rng_uniform (const gsl_rng * r).

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if the prototype is double gsl_rng_uniform(const gsl_rng *r), you need to have a gsl_rng pointer around (whatever that may happen to be) to pass to the function.

    I mean, the prototype for the square root function is double sqrt(double x), but you don't actually call it with "sqrt(double x)", you call it with "sqrt(25.5)", or "sqrt(y)" if y is a double variable.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    30
    That's definitely what's confusing me.

    This is the sample program they give
    Code:
    #include <stdio.h>
    #include <gsl/gsl_sf_bessel.h>
    int
    main (void)
    {
    double x = 5.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(&#37;g) = %.18e\n", x, y);
    return 0;
    }
    With the bessel function defined as: double gsl_sf_bessel_J0 (double x)

    This makes sense to me because it's a function that depends on x, so you give it an x and it runs.

    But I haven't been able to get the rng function to do anything. If I give it something else, it says there's an undefined reference to gsl_rng_uniform (even though it's declared and everything). Or an incompatible type argument.
    I've written functions myself and used them and it's been fine...
    Maybe I'm doing something wrong with the pointer? But I've used pointers before...

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So have you looked here? It looks like you need to do some initialization to get a valid gsl_rng pointer thing.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Have you tried:
    Code:
         #include <stdio.h>
         #include <gsl/gsl_rng.h>
         
         int
         main (void)
         {
           const gsl_rng_type * T;
           gsl_rng * r;
         
           int i, n = 10;
         
           gsl_rng_env_setup();
         
           T = gsl_rng_default;
           r = gsl_rng_alloc (T);
         
           for (i = 0; i < n; i++) 
             {
               double u = gsl_rng_uniform (r);
               printf ("%.5f\n", u);
             }
         
           gsl_rng_free (r);
         
           return 0;
         }

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    30
    Thanks for your help, everyone, I finally got my whole program to work.
    Sorry that was so vague, but I was really confused and there were a few problems. :S But anyway, thanks again. I'll probably be back, but I'm okay for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Array
    By dldsob in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2009, 04:32 PM
  2. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  3. Library Functions
    By gr8npwrfl in forum C++ Programming
    Replies: 0
    Last Post: 07-28-2008, 11:16 AM
  4. Library functions eg. String manipulation function
    By bhupesh.kec in forum C Programming
    Replies: 6
    Last Post: 07-08-2008, 07:32 AM
  5. Gnu C++ Library Header file question?
    By xddxogm3 in forum C++ Programming
    Replies: 13
    Last Post: 10-01-2003, 03:48 PM