Complex number, imaginary part usage question

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

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

#16 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:46 PM

That's not the code you posted above. All the code you posted today and yesterday had the input of 'bits' part after you had used it for allocation.

This goes to show you though that if you post a thread on here for help, after you make changes and still need more help, you should post your updated code so we can see what's going on.
Was This Post Helpful? 0
  • +
  • -

#17 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:49 PM

I also wrote in my earlier posts, that I posted here just a small snapshot from my code, I didn't put the whole source code, while it is not convenient to put the whole code (well maybe in my case it is because it is around 400 lines, including the functions). But what if someone is working on a large project and has a question, should he post all the lines of his/her code? That's why I also posted the part that I had problem with, and as I said once before, I only expect answers regarding my problem and question, not about other parts of the code.
Was This Post Helpful? 0
  • +
  • -

#18 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Complex number, imaginary part usage question

Posted 13 March 2012 - 12:51 PM

The cool thing about the code tags is they have scrollbars, and you could also put the code inside a spoiler tag so that people would have to click something just to see the code. Take a look at all the features you have available when writing your posts.
Was This Post Helpful? 0
  • +
  • -

#19 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:55 PM

For what it's worth, often the best solution to the "large program problem" is to post only code that's relevant to the question. Rather than posting a huge project or an uncompilable, unrunnable snippet from a large program, it might be better to make up a short simple, even trivial, program specifically to illustrate a problem or question.
Was This Post Helpful? 1
  • +
  • -

#20 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:57 PM

Having scrollbar doesn't make it convenient to post 10000 lines of code here, for example. If I want to help someone, I want to see where he is having problem and try to help him/her, I don't want to go through a huge bunch of code and try to guess where the problem might be. This is both mind-blowing and people don't have time for it (at least most of them).

I totally agree with r.stiltskin

This post has been edited by erkant: 13 March 2012 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#21 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 17 March 2012 - 10:43 AM

I forgot to ask whether you can cast an array to a complex. Let's say that I have an array of short, and I want it to hold some imaginary numbers, can I cast that array to complex and add to it some imaginary numbers? If yes, how can I achieve it?


short array[10];
int i;

for(i = 0; i < 10; i++)
{
   if(i%2 == 0)
         (complex)array[i] = +I;
   else (complex)array[i] = -I;
}



Will the above code work? And can I also cast it at the function call, like:

short array[10];

function((complex)array);



Thank in advance.

This post has been edited by erkant: 17 March 2012 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#22 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 17 March 2012 - 10:53 AM

I don't think there's any practical way to do that. An array can only contain a single datatype. It can of course be a complex datatype like a structure or a union, but whatever it is, it must be consistent throughout.

If you cast the pointer to some other type it would be possible to overwrite the array with a different type of data, but you would essentially be trashing that memory -- afterwards there would be no way of knowing what's stored there.

A better approach would be to create an array of complex, since a complex number can have a 0 imaginary part. For the "shorts", just declare them as complex numbers with 0I.

Alternatively, you could create a union of a short with a complex, and declare an array of those unions, but I think that would be harder to deal with later.
Was This Post Helpful? 0
  • +
  • -

#23 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 17 March 2012 - 11:05 AM

Actually my problem is this. I have an input and output arrays, and the user enters the size of the input array, and also output array gets the same size. At the beginning I give random values to the each element of the array, just 0s and 1s. Then I need to call this QPSKmapping function, which checks the first and second, third and fourth elements and so on, and if the bits are 00, they need to be converted to -1-I, if it is 01, they will be -1+I, and so on, the second bit is always imaginary number. I could easily create a complex array, but the problem is that after this function, some other functions are called and at the end the array will be reversed and will again contain only 0s and 1s. So I need imaginary numbers only for this function and some other functions, not for every function, so the type of the array needs to be changed back and forth. Therefore, I was asking whether there is a possibility for a cast.

void QPSKmapping(short* input, short* output, int nbits)
{
    int i;

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


This post has been edited by erkant: 17 March 2012 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

#24 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 17 March 2012 - 11:23 AM

But a complex number requires more memory space than a short. Let's say for argument's sake that a short is 2 bytes and a complex is 8. If you write a complex in the array as "element #0" it occupies bytes 0 through 7. If you then try to write a short as "element #1", it occupies bytes 2 and 3, so it overwrites part of the complex.

Furthermore, even if you could do this, how would the other functions that try to use the array know which bytes are part of a short and which bytes are part of a complex?

I think the easiest way is to convert everything into complex, work with the data in that form, and then convert back to short at the end.
Was This Post Helpful? 1
  • +
  • -

#25 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 18 March 2012 - 01:50 PM

Thanks for the information. I have also another question. I have seen that you have written complex type with double. Like this:

complex double test;



I want to ask whether a complex type can be used with another type apart from double, like complex int, or can a complex type be created alone, just by writing:

complex variable;



And one more question, is the array declaration with complex type the same as with the other types?

This post has been edited by erkant: 18 March 2012 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#26 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 18 March 2012 - 02:15 PM

As far as I know, the choices are float, double and long double, and I believe the default is double unless otherwise specified. An array would be declared as with any other type:
complex double c_arr[SIZE];

Was This Post Helpful? 1
  • +
  • -

#27 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 26 March 2012 - 01:42 PM

Thanks. And is the imaginary part of the complex numbers interchangeable? What I mean with that is whether it is the same 3.0I and I3.0?
Was This Post Helpful? 0
  • +
  • -

#28 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 26 March 2012 - 03:27 PM

Yes it matters.

3.0I is 3.0 times I. I3.0 will be parsed as a variable name, I3, followed by a numerical constant .0, which will produce a compiler error assuming that no variable named "I3" has been declared, and another compiler error because the constant .0 is just "floating" there and the compiler doesn't know what to do with it.

And PS, aren't we getting a little lazy here? How hard would it be for you to figure this out, or test it, yourself?

This post has been edited by r.stiltskin: 26 March 2012 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#29 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 27 March 2012 - 02:27 PM

Thanks for the information. I also try to change the sign of the imaginary part of the complex number, simply if the imaginary part is plus I want it as minus. I try the code below but I get the following error:

error: lvalue required as left operand of assignment

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

int main()
{
    complex double cplx3 = 4.0 + 2.0j;
    printf("%.0f %c %.0fj\n", creal(cplx3), cimag(cplx3) >= 0 ? '+' : '-', cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3));

    if(cimag(cplx3) >= 0)
    {
        cimag(cplx3) = -cimag(cplx3);
    }

    printf("%.0f %c %.0fj\n", creal(cplx3), cimag(cplx3) >= 0 ? '+' : '-', cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3));
    
    return 0;
}



I can change this one at the printing and make it change the sign:
cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3)


But I need some method to change all the imaginary part signs of the numbers in the array.

This post has been edited by erkant: 27 March 2012 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#30 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 27 March 2012 - 03:13 PM

You need to learn (and I say this with the best of intentions, not to be mean or sarcastic at all) how to read documentation. You will not get far (or remain employed for very long) if you have to ask your boss how to do every little thing.

I gave you this link before: everything you need to work with the complex type is described on this page and the "next" page.

It tells you that the functions creal(), cimag(), and so on, return certain values associated with the complex number passed as an argument. They don't allow you to modify the argument. In colloquial terms, they are "getters", not "setters".

For assignment, you have the usual operators =, +=, -=, and so on.

Now, suppose you have an ordinary int n and n has any arbitrary positive value. What amount can you add to (or subtract from) n that will change n to -n?

And finally, remember that there is a function cimag() that lets you "get" the imaginary part of a complex number.
Was This Post Helpful? 0
  • +
  • -

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