Complex Number

This is a discussion on Complex Number within the C Programming forums, part of the General Programming Boards category; Please, I am beginner and I need to work with complex matrices in C, but do not know how to ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10

    Complex Number

    Please, I am beginner and I need to work with complex matrices in C, but do not know how to work with that type. I have an idea of how working with complex numbers, it would just declare a variable and complex and the real parts and print them:
    Code:
    #include <stdio.h>  
    #include <complex.h>    
    
    int main (){    
      complex double z = 99.0 + 12.0*I; 
     	   
      printf("z = %f + %fI\n", creal(z), cimag(z));        
    
      return 0;  
    }
    My doubt is how to dynamically allocate a complex array , fill it out and access the elements? I searched the internet and only found a few things in C + +. Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Same as you would any other dynamically allocated array. Something like:
    Code:
    complex double *complex_array = malloc(42 * sizeof(*complex_array));  // allocate 42 elements
    complex_array[17] = 99.0 + 12.0*I;  // set 18th element to 99+12i
    EDIT: I think this is less an issue of complex numbers and more an issue of arrays and dynamic memory. Try some tutorials on those (without adding the confusion that comes with complex numbers). We have some here on this site, and you can Google for several more. Check your text books too.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    anduril462, your guess was wrong, my problens are with complex type.
    Please, I would like to know how fill a complex arrary when the elements are in ohter array.

    thanks

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    anduril462, your guess was wrong, my problens are with complex type.
    I'm sure anduril462 realizes you are using complex numbers, but tried to indicate that your problem is really in your not understanding some basic issues with arrays - issues that transcend the type.

    Do you know how to declare an array of type complex (dynamically or not)? Or of any type?
    Once declared, do you know how to access its elements?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Maybe you want each element to have real and imaginary parts.
    Code:
    typedef struct {
        double real;
        double imag;
        } complex_def;
    
    complex_def *array;
    
    array = malloc(1000 * sizeof(*array)); /* allow 1000 elements */
    
    array[0].real = 99.0;
    array[0].imag = 12.0;

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I think OP is using C99 complex.
    In that case
    Code:
    complex double *cv = calloc( 10, sizeof(*cv) );
    
    for(i = 0; i < 10;i++) {
      cv[i] = x + y * I;
    }
    Edit: complex is built in data type in C99. If you want to change real/imag part of complex number, you've to
    Code:
     c = x + cimag(c);  // change c real part to x
    Last edited by Bayint Naung; 04-13-2011 at 10:52 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by Bayint Naung View Post
    I think OP is using C99 complex.
    Sure hope so...it's not in my K&R!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Completey breaking syntax rules. But hey.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by figura View Post
    anduril462, your guess was wrong, my problens are with complex type.
    Please, I would like to know how fill a complex arrary when the elements are in ohter array.

    thanks
    Can you do this with two arrays of integers? If so, then you can do it with complex types. You can always assign one variable of a specific type to another variable of that same type in C, whether complex, structs, unions, floats or ints. As for arrays, you just have to do this with each element individually. array1[i] = array2[i].

    Don't worry about complex types yet. Write a small program that assigns elements of one array to elements of another the way you want using plain integers. Once you figure that out, you can change the type of the array from int to complex.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    Quote Originally Posted by anduril462 View Post
    Can you do this with two arrays of integers? If so, then you can do it with complex types. You can always assign one variable of a specific type to another variable of that same type in C, whether complex, structs, unions, floats or ints. As for arrays, you just have to do this with each element individually. array1[i] = array2[i].

    Don't worry about complex types yet. Write a small program that assigns elements of one array to elements of another the way you want using plain integers. Once you figure that out, you can change the type of the array from int to complex.

    anduril462, sorry I think I did not expressed myself in the right way. I can do these operations with other number types, and with things I read, now I saw that the allocation is the same.My question is: I have 3 arrays A B and C.

    Array A have real elements
    Array B have imaginary elements
    Array C is a complex array wich I want to fill merging A and B, like C = A (wich is real) + B (imaginary)

    How can I do that?

    Code:
    C[i].real = A[i];
    C[i].imag = B[i];
    Like this? As nonoob did?

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    I don't get your question... did you not try it?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Umm...how about:
    Code:
    C[i] = A[i] + B[i]*I
    You did exactly this in your first example, except you used literal values instead of scalar variables. All you needed to do was replace your 99.0 with the real component in A[i] and your 12.0 with the imaginary component in B[i].

  13. #13
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    Suppose I did not know their size, which is not the case in this example, so I've allocated arrays dynamically, and this is my example:

    Code:
    #include <stdio.h>  
    #include <complex.h>    
    #include <stdlib.h>
    int main (){    
    
    	double *B,*A;
    	complex double *z;
    	int  i,size=5;
    	
    	A=malloc(size*sizeof(double));
    	B=malloc(size*sizeof(double));
    	z=malloc(size*sizeof(complex double));
    	
    	for(i=0;i<size;i++){	
    		A[i]=i*1.;
    		B[i]=i*2.;
    		z[i]=A[i]+B[i]*I;
    	}
    	for(i=0;i<size;i++)	
    		printf("A= %f  B=%fI    Z=%f + %fI\n", A[i],B[i],creal(z[i]),cimag(z[i]));
    	free(z);
    	free(A);
    	free(B);
    
      return 0;  
    }
    and when I compilled:

    Code:
    [figura@localhost testes]$ gcc -c mat_complex.c
    mat_complex.c:27:2: warning: no newline at end of file
    [figura@localhost testes]$ gcc mat_complex.o
    [figura@localhost testes]$ ./a.out
    A= 0.000000  B=0.000000I    Z=0.000000 + 0.000000I
    A= 1.000000  B=2.000000I    Z=1.000000 + 2.000000I
    A= 2.000000  B=4.000000I    Z=2.000000 + 4.000000I
    A= 3.000000  B=6.000000I    Z=3.000000 + 6.000000I
    A= 4.000000  B=8.000000I    Z=4.000000 + 8.000000I
    [figura@localhost testes]$
    Is this the right way to print?? If so, I think that now everything's right!! Now I can back to my big code. Thanks anduril462 and everybody!!
    Last edited by figura; 04-14-2011 at 07:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complex Number Class
    By Sephiroth1109 in forum C++ Programming
    Replies: 15
    Last Post: 12-12-2007, 04:46 PM
  2. Storing components of a complex number
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-19-2007, 01:37 PM
  3. Complex Number Implementation
    By CaptainMorgan in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 09:47 PM
  4. Complex number in qudratic equation C++
    By ee04jjn in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 02:49 PM
  5. complex number arithmetic
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2003, 09:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21