Multiply a matrix by a complex

Hey,

I'm new with c++. I'm trying to do something like:
Z=X+Y*i
where I already have the matrices X and Y. For instance,
X = [ -1 -1 -1; 0 0 0; 1 1 1] and Y = [-1 0 1; -1 0 1; -1 0 1].

My problem is how to multiply the matrix Y by a complex number in order to get:
Y*i = [-i 0 i; -i 0 i; -i 0 i];

Thanks for your help.
1
2
3
for (int a = 0; a < 3; a ++)
   for (int b = 0; b < 2; b ++)
      Y[a][b] *= i;
C++ supports complex numbers: #include <complex> and use the type std::complex<double> for your matrix elements.

Unlike C, C++ (until C++14) has no simple way to write i, but you can declare your own constant, const std::complex<double> i(0, 1);

(not writing code because it depends on what matrix library/class/etc you're using)
Thank you for your answers. They were very helpful.
Sorry guys, but I'm facing other problem which I can't solve. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void prepareKernel(int binSize, int order) {
	// Triangular kernel
	cv::Mat1i X, Y;
	meshgridTest(cv::Range(-binSize+1,binSize-1), cv::Range(-binSize+1,binSize-1), X, Y);

	complex<int> i(0,1);
	for(int j=0; j<2*binSize-2; j++) {
	   for(int k=0; k<2*binSize-2; k++) {
	      Y[j][k] *= i;
	   }
	}
	cv::Mat1i Z = X + Y;
	std::cerr << X << std::endl;
	std::cerr << Y << std::endl;
	std::cerr << Z << std::endl;
}


When I run my code, I'm getting the following error regarding the bold line:
no match for ‘operator*=’ in ‘*(Y.cv::Mat_<_Tp>::operator[] [with _Tp = int](j) + ((long unsigned int)(((long unsigned int)k) * 4ul))) *= i’ main.cpp /rihog/src line 130 C/C++ Problem

Can anyone help me?

Thank you in advance.
Last edited on
> cv::Mat1i
iirc that would store integers, no complex numbers.
Also, some operations, like matrix multiplication, don't work on integer matrices.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char **argv){
	std::complex<double> 
		x[] = {
			-1, -1, -1,
			0, 0, 0,
			1, 1, 1
		},
		y[] = {
			-1, 0, 1,
			-1, 0, 1,
			-1, 0, 1
		};

	cv::Mat X(3, 3, CV_64FC2, x); //complex double matrix
	cv::Mat Y(3, 3, CV_64FC2, y);

	//Y *= std::complex<double>(0,1); //no match for operator...
	//it seems that you'll need to create the loop
	for(int K=0; K<Y.rows; ++K)
		for(int L=0; L<Y.cols; ++L)
			Y.at<std::complex<double> >(K,L) *= std::complex<double>(0,1);

	cv::Mat Z = X*Y;


	for(int K=0; K<Z.rows; ++K){
		for(int L=0; L<Z.cols; ++L)
			std::cout << Z.at<std::complex<double> >(K,L) << ' ';
		std::cout << '\n';
	}
	return 0;
}
(0,3) (0,0) (0,-3) 
(0,0) (0,0) (0,0) 
(0,-3) (0,0) (0,3)
ne555, thank you for your answer.

If I run the code below,
1
2
3
4
5
std::complex<double> y[] = {-5, 0, 5,
								-5, 0, 5,
								-5, 0, 5};
	Mat Y(3, 3, CV_64F, y);
	cout << Y << endl;

I'll get Y =
[-5, 0, 0;
0, 5, 0;
-5, 0, 0]

Could you explain me what is happening? I would like to have matrix Y equal to y. How can I do it?

Thanks
Last edited on
`y' stores complex doubles
`Y' stores doubles.

Mat Y(3, 3, CV_64FC2, y);
In 'y', what is the real and imaginary part?
When you create 'Y', why the result is not the same as 'y'? I mean, I don't understand how the values are assigned (for instance in my case, where I just use 1 channel instead of 2 as you).
Try to print it.
there you are assigning only the real part, so the imaginary is 0.

What it is in memory is something like -5 0 0 0 5 0 -5 0... (pairs real-imaginary)
When you construct the cv::Mat you say `I'm going to pass you an array, and threat it as it is storing doubles', which is a lie.

So now 5 0 0 0 does not appear as two complex number in the form real-imaginary, but as four real numbers.
Topic archived. No new replies allowed.