# input complex number to element of matrix

E

#### eric

Dear c++ experts(I am using g++4.6.1)
I have the following code segment
---------
matrix<std::complex<double> > m1(2, 1);
matrix<std::complex<double> > m2(1, 2);
kmatrix<std::complex<double> , 2, 2> m3;
m3 = 0;
m1 = (1.1, 1.1, 3.4); m1 = (2.2, 2.2);
m2 = (3.1, 3.3, 1.9); m2 = (4.4, 4.4);

cout << "m1 = " << m1 << endl;
cout << "m2 = " << m2 << endl;
----------------------------------------------------
that (1.1, 1.1, 3.4) is not valid complex number, but I did not get
any compile error, but it run (badly)
----------
m1 = (3.4,0)
m2 = (1.9,0)
m1 + m2 = (5.3,0)
--------------------------------------------
it look insert that last number in (1.1, 1.1, 3.4) into real part and
always put 0 to imaginary part of my matrix
element which I already claim/declare as std::complex<double>
I am not 100% sure this template type be correctly propagate to
matrix.hpp (and its element valarray)

actually, even I enter correct complex number format, like m1
=(1.1, 1.1), m2=(3.1, 3.3)
the output still is
m1=(1.1,0)
m2=(3.3,0)
just like previous example, put last number as real part and always
put 0 to imaginary part of my matrix element
plz help, and thanks a lot in advance, Eric
where go wrong such I can not get the same complex number I input into
my matrix element?

J

#### Juha Nieminen

eric said:
m1 = (1.1, 1.1, 3.4);

Where exactly have you seen this syntax being used, and what exactly
is it that you expect it to do? Because it's not doing what you mean.

What you want to do would probably look more like:

m1 = something(1.1, 1.1, 3.4);

or alternatively:

m1.something(1.1, 1.1, 3.4);

depending on what the type of m1 is.

R

#### Richard Damon

Dear c++ experts(I am using g++4.6.1)
I have the following code segment
---------
matrix<std::complex<double> > m1(2, 1);
matrix<std::complex<double> > m2(1, 2);
kmatrix<std::complex<double> , 2, 2> m3;
m3 = 0;
m1 = (1.1, 1.1, 3.4); m1 = (2.2, 2.2);
m2 = (3.1, 3.3, 1.9); m2 = (4.4, 4.4);

cout<< "m1 = "<< m1<< endl;
cout<< "m2 = "<< m2<< endl;
----------------------------------------------------
that (1.1, 1.1, 3.4) is not valid complex number, but I did not get
any compile error, but it run (badly)
----------
m1 = (3.4,0)
m2 = (1.9,0)
m1 + m2 = (5.3,0)
--------------------------------------------
it look insert that last number in (1.1, 1.1, 3.4) into real part and
always put 0 to imaginary part of my matrix
element which I already claim/declare as std::complex<double>
I am not 100% sure this template type be correctly propagate to
matrix.hpp (and its element valarray)

actually, even I enter correct complex number format, like m1
=(1.1, 1.1), m2=(3.1, 3.3)
the output still is
m1=(1.1,0)
m2=(3.3,0)
just like previous example, put last number as real part and always
put 0 to imaginary part of my matrix element
plz help, and thanks a lot in advance, Eric
where go wrong such I can not get the same complex number I input into
my matrix element?

In C++, the format of a complex number is: complex<type>(real, imag),
i.e. a call to the (template) constructor.

(number, number) is NOT a valid way to specify a complex number, but is
in fact a valid syntax that mean something else that goes all the way
back to K&R C in 1978.

If your compiler is also a C99 compiler, it may accept as an extension
the format used if C99 of 3.1+3.3i which actually ISN'T a complex
constant but a complex constant expression the result of adding the real
number 3.1 with the imaginary number 3.3i, the i suffix, being sort of
like f, l, or u suffixes, indicating the type of the number being expressed.