# Thread: Complex Number

1. ## 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. 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. 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. 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. 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. 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`

7. Originally Posted by Bayint Naung
I think OP is using C99 complex.
Sure hope so...it's not in my K&R!

8. Completey breaking syntax rules. But hey.

9. Originally Posted by figura
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. Originally Posted by anduril462
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. I don't get your question... did you not try it?

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. 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!!

Popular pages Recent additions