Complex number, imaginary part usage question

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 3027 Views - Last Post: 03 June 2012 - 06:47 AM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Complex number, imaginary part usage question

Posted 12 March 2012 - 01:41 PM

I have no previous experience in using the complex.h library, and using the imaginary part. Therefore, I need some help with it. In the below code for some of the index elements I want to add the imaginary part "j". Can someone help me with it?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <complex.h>

#undef I
#define j _Imaginary_I

void rbits(short* buf, int nbits)
{
    int i;

    srand(time(NULL));

    for(i = 0; i < nbits; i++)
        *(buf + i) = (rand() % 2);
}

int main()
{
    int bits, i;

    short* input;

    input = malloc(sizeof(int) * bits);

    if(input == NULL)
    {
        fprintf(stderr, "Couldn't allocate that much memory!");
        return 1;
    }

    printf("Please enter the number of bits: ");
    scanf("%d", &bits);

    rbits(input, bits);

    for(i = 0; i < bits; i += 2)
    {
        if(input[i] == 0 && input[i+1] == 0)
        {
            input[i] = -1;
            input[i+1] = -j;
        }
        else if(input[i] == 0 && input[i+1] == 1)
        {
            input[i] = -1;
            input[i+1] = +j;
        }
        else if(input[i] == 1 && input[i+1] == 0)
        {
            input[i] = 1;
            input[i+1] = -j;
        }
        else
        {
            input[i] = 1;
            input[i+1] = +j;
        }
    }

    for(i = 0; i < bits; i++)
        printf("%d ", input[i]);
    printf("\n");

    return 0;
}



My method is not working, while when I run it I get the following errors:

C:\Users\Erkan\Desktop\asdsada.c||In function 'main':|
C:\Users\Erkan\Desktop\asdsada.c|44|error: '_Imaginary_I' undeclared (first use in this function)|
C:\Users\Erkan\Desktop\asdsada.c|44|note: each undeclared identifier is reported only once for each function it appears in|
||=== Build finished: 2 errors, 0 warnings ===|

If someone can offer me a better and working method for imaginary part, I would be glad.

This post has been edited by erkant: 12 March 2012 - 01:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Complex number, imaginary part usage question

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 02:30 PM

The first thing I noticed was you're using bits on line 26 before you assign it a value. The error appears to be you using j instead of I as an imaginary type. Also why does input have type short and not complex?

Heres an example of using complex numbers in C.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 02:35 PM

This was all covered in a different thread, the OP just didn't make the changes and reposted.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 03:09 PM

Sticking to the complex number aspect of the question, use of complex.h is explained pretty clearly here.

Don't undefine I unless you have a specific need to use the uppercase letter 'I' for some other purpose in your program. Otherwise, you want I because that's a quick way to get the constant value 0 + 1i.

Also, doing this
#define j _Imaginary_I

doesn't do anything but replace the macro _Imaginary_I (which is equivalent to the macro I) that's provided in complex.h with your own macro j. What good is that? You've just provided a non-standard name for the complex type. You still have to declare a variable in your program if you want to use it.

Here's a quick example of how to use the complex numbers (notice, no #define):

#include <stdio.h>
#include <complex.h>

int main() {
    complex double cplx = 0I;
    complex double cplx2 = -5I + 2.4;
    complex double cplx3 = 4.0 + 2.0i;
    printf( "%f %c %fi\n", creal(cplx),   cimag(cplx2) >= 0 ? '+' : '-', \
                cimag(cplx2) >= 0? cimag(cplx2) : -cimag(cplx2));
    printf( "%f %c %fi\n", creal(cplx2),  cimag(cplx2) >= 0 ? '+' : '-', \
                cimag(cplx2) >= 0? cimag(cplx2) : -cimag(cplx2));
    printf( "%f %c %fi\n", creal(cplx3),   cimag(cplx3) >= 0 ? '+' : '-', \
                cimag(cplx3) >= 0? cimag(cplx3) : -cimag(cplx3));
    return 0;
}




Notice the 5I. That's just a quick and easy way of getting the constant 0 + 5i.

And isn't that cute -- it even allows implied multiplication (5I).

(Don't worry too much about the ternary expressions in my printf. That's there just because I didn't want to print something as ugly as 2.4 + -5i)

This post has been edited by r.stiltskin: 12 March 2012 - 06:48 PM
Reason for edit:: See next post.

Was This Post Helpful? 1
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Complex number, imaginary part usage question

Posted 12 March 2012 - 05:37 PM

Cleaned up some sloppiness in my previous post. I is 0 + 1i not 0 + 0i

Also, there's nothing in the standard to say that an imaginary number is automatically initialized, so I corrected line 5 to initialize it to 0 + 0i. And I cleaned up the ternary expressions so 0i won't become -0i. And finally, added another way to initialize a complex variable on line 7.
Was This Post Helpful? 0
  • +
  • -

#6 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 09:03 AM

Thanks for everyone for useful information. You were very helpful.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 10:06 AM

Erkant, alot of what is wrong with your code is the same as whats wrong in the code you posted in another thread. There were a few posts in that thread that explained what was wrong. Before you make another thread, maybe check on threads you've already created.

Also, what wasn't helpful about my post above? I simply pointed out that if you checked your other thread, you would've seen some posts telling you what was wrong and how to fix it.
Was This Post Helpful? 0
  • +
  • -

#8 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 10:48 AM

I regularly check the threads that I open. I also feel sometimes like I'm spamming, but I'm highly into programming especially C language and I want to learn a lot of thing about it, and code as much as I can. Therefore, when I don't know something I try to look up it online, if I can't find it I post it here. I always try to post other questions. In my last thread I had problem with memory allocation, some pointer and arrays. But this one was different, maybe code was similar but my problem was completely different, I needed help about complex numbers in C. Thanks once more to everyone who helped me.
Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 11:06 AM

Yes but your code is still wrong. I told you how to fix it, but you didn't and then posted similar code (with the same things wrong with it) and ask a different question. If you want to learn C and are posting here for advice, then when people do make suggestions, you don't make the proper changes to your code. IMO, if you are serious about learning C, you would have made those changes and the code you posted above would've only had a problem with complex numbers.

Like I said in the other thread, your program might compile fine, but the input/output arrays are being allocated using an int variable that hasn't been initialized yet. You need to move the allocation part of your code to after you ask the user for the number of bits. Then use that number to allocate the input/output arrays.

I also mentioned something about the random number generator in the other thread. You seed it every time the rbits() function is called, this is wrong, you only want to seed it once in main(). The call to srand(time(NULL)); should me moved to the beginning of main(). Last thing, you should make sure you free the memory that you dynamically allocated using malloc because you can wind up with memory leaks. Again, this was all covered in the other thread you posted.

Here's a link to the thread from yesterday that I'm talking about: http://www.dreaminco...following-code/

This post has been edited by vividexstance: 13 March 2012 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#10 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:17 PM

In my original code, which I haven't posted here I have already asked the user to enter the number of bits, and I have scanned it, and used it for allocating memory for arrays (so don't worry about it). I always appreciate the answers and helps that you give, but I think that there is no need to reply to a topic about something other. The answer should be about what user asks. And in this thread I only asked about complex numbers and imaginary part, so I don't expect any answer about other parts of my code. I only expect reply about complex numbers in this topic, as r.stiltskin has answered. So thanks once more to the people who helped about complex numbers.

This post has been edited by erkant: 13 March 2012 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:24 PM

Quote

In my original code I have already asked the user to enter the number of bits, and I have scanned it, and used it for allocating the arrays.

You do not use that number scanned from the user to allocate your memory. My point was that you still have things wrong with your code and you're trying to add more to it. Why not first fix what's wrong with your code and then add things? Here is what I'm talking about where you allocate the memory with malloc() before you ask for the number of bits:
int main()
{
    int bits, i;

    short* input;

    input = malloc(sizeof(int) * bits);

    if(input == NULL)
    {
        fprintf(stderr, "Couldn't allocate that much memory!");
        return 1;
    }

    printf("Please enter the number of bits: ");
    scanf("%d", &bits);

    rbits(input, bits);

I bet your program runs fine, but that's because 'bits' most likely contains a very large garbage number. So you're declaring a huge array when you really want an array to hold a certain number of bits. Then you don't even free the memory for that huge array. To see what I'm talking about, print out bits in between where you declare it and where you use it in the call to malloc.
Was This Post Helpful? 0
  • +
  • -

#12 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:28 PM

View Posterkant, on 13 March 2012 - 12:17 PM, said:

In my original code, which I haven't posted here I have already asked the user to enter the number of bits, and I have scanned it, and used it for allocating memory for arrays (so don't worry about it).


If you read it correctly, I have written "which I haven't posted here". I just posted a short snapshot here, just regarding my question. The code and bits are all fine and working of my computer.

I already have this in my original code:

    printf("Please enter the number of bits: ");
    scanf("%d", &bits);


This post has been edited by erkant: 13 March 2012 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:33 PM

Here is a program to test what I was talking about:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	srand(time(NULL));		/* Seed the random number generator ONLY ONCE! */
	
    int bits;
    short *input, *output;
    
    printf("bits == %d\n", bits);
    printf("sizeof(short) * bits == %d\n", sizeof(short) * bits);

    input = malloc(sizeof(short) * bits);

    if(input == NULL)
    {
        fprintf(stderr, "Couldn't allocate that much memory!");
        return 1;
    }

    output = malloc(sizeof(short) * bits);

    if(output == NULL)
    {
        fprintf(stderr, "Couldn't allocate that much memory!");
        return 1;
    }

    printf("Please enter the number of bits: ");
    scanf("%d", &bits);

    return 0;
}


And the output of the printf calls is:

Quote

./erkant
bits == 5983317
sizeof(short) * bits == 11966634
Please enter the number of bits:

On the second run the output was different:

Quote

bits == 14883925
sizeof(short) * bits == 29767850
Please enter the number of bits:

And that's because you're still using bits in malloc() before it has been initialized. Move the part where you input bits to be above the malloc call.

This post has been edited by vividexstance: 13 March 2012 - 12:35 PM

Was This Post Helpful? 1
  • +
  • -

#14 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:39 PM

Dear vividexstance, thank you very much for all your information, I completely understand it, and I already knew it before. And thank you very much for all your efforts, but there is just a small part from my original code to see that I have gotten the value of bits before using it:

int main(int argc, char *argv[])
{
  short seed[8];
  int bits;
  char mode[5];


  printf("How many bits do you want to transmit?: ");
  scanf("%d", &bits);

  short* input;
  short* output;
  short* coder_output;

  input = malloc(sizeof(short) * bits);

  output = malloc(sizeof(short) * bits);

  coder_output = malloc(sizeof(short) * (bits * 2));

  if(coder_output == NULL)
  {
      fprintf(stderr, "Couldn't allocate that much memory!");
      return 1;
  }

  srand(time(NULL));
  rbits(seed, 8);
  rbits(input, bits);

  printf("Which modulation type to you want to use? (type BPSK or QPSK): ");
  scanf("%s", mode);

  // and it continues...


This post has been edited by erkant: 13 March 2012 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:44 PM

@erkant: It must be obvious by now that after someone has taken the trouble to examine your code and offer suggestions to correct it, they are likely to feel disrespected if you repost the same or similar code without using or acknowledging those suggestions, even if the reason for the re-posting is essentially unrelated to the original question.

@vividexstance: I understand how irritating it is to see your efforts seemingly ignored, but I'm sure no disrespect was intended and, to be fair, the question posed in this thread was clearly stated and really did not relate to the previous topic. I, for one, didn't even look at the rest of the code since the question was exclusively about complex numbers.

And, to both of you, this thread has wandered well off its original topic so maybe a new thread would be in order.

This post has been edited by r.stiltskin: 13 March 2012 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3