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[0][0] = (1.1, 1.1, 3.4); m1[1][0] = (2.2, 2.2);
m2[0][0] = (3.1, 3.3, 1.9); m2[0][1] = (4.4, 4.4);

cout << "m1[00] = " << m1[0][0] << endl;
cout << "m2[00] = " << m2[0][0] << 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[00] = (3.4,0)
m2[00] = (1.9,0)
m1[00] + m2[00] = (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[0]
[0]=(1.1, 1.1), m2[0][0]=(3.1, 3.3)
the output still is
m1[00]=(1.1,0)
m2[00]=(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[0][0] = (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[0][0] = something(1.1, 1.1, 3.4);

or alternatively:

m1[0][0].something(1.1, 1.1, 3.4);

depending on what the type of m1[0][0] 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[0][0] = (1.1, 1.1, 3.4); m1[1][0] = (2.2, 2.2);
m2[0][0] = (3.1, 3.3, 1.9); m2[0][1] = (4.4, 4.4);

cout<< "m1[00] = "<< m1[0][0]<< endl;
cout<< "m2[00] = "<< m2[0][0]<< 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[00] = (3.4,0)
m2[00] = (1.9,0)
m1[00] + m2[00] = (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[0]
[0]=(1.1, 1.1), m2[0][0]=(3.1, 3.3)
the output still is
m1[00]=(1.1,0)
m2[00]=(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top