Matrix math with complex numbers

Hello

I'm working on a project to make a one-port Vector Network analyzer. This device needs to be calibrated by Arduino, and for this I need to do a matrix multiplication and inversion with complex numbers in it. I'm using the Arduino Nano board but I'm planning on switching to the Arduino Nano 33 BLE Sense due to memory issues.

Here is the relevant part of the code:

#include <BasicLinearAlgebra.h>
using namespace BLA;
#include <Complex.h>

void Calibration() {
  Complex gamma_a_1[n];
  Complex gamma_a_2[n];
  int gamma_a_3 = 0;


  float gamma_m_1[n];
  float gamma_m_2[n];
  float gamma_m_3[n];

  BLA::Matrix<3,3> C;
  BLA::Matrix<3> V;
  BLA::Matrix<3> E;
   
  for (i=0; i<n; i++){
  V(0) = gamma_m_1[i];
  V(1) = gamma_m_2[i];
  V(2) = gamma_m_3[i];


  C = {gamma_a_1[i], 1 , gamma_a_1[i]*gamma_m_1[i],gamma_a_2[i], 1 , gamma_a_2[i]*gamma_m_2[i],gamma_a_3, 1 , gamma_a_3*gamma_m_3[i]};
//  Serial << "C: " << C << '\n';
  Invert(C);
  E = C*V;
  
  e_00[i] = E(1);
  e_11[i] = E(2);
  delta_e[i] = -E(0);  
  }

The gamma_m and gamma_a arrays are filled with values by for-loops that I've taken out to limit the code here.
The goal of this part of the code is to get the error coefficients e_00, e_11, and delta_e, they are stored in global arrays.

The error that I get is the following (on the line of C = {...}):

//no match for 'operator=' (operand types are 'BLA::Matrix<3, 3>' and '<brace-enclosed initializer list>')

It seems like the Basiclinearalgebra library doesn't cover the values of the matrices to be complex numbers. This project is my first experience with Arduino so I'm rather new to it.
Does anyone have a solution for my problem?

Thank you in advance

have a look at BasicLinearAlgebra section on Changing the Element Type

Is there a data type covering complex numbers? 'Changing the Element Type' is used to change the data type to e.g. int or double. As Complex isn't a datatype, it doesn't work and I don't know how I could go around it.

The "{" does not seem to make sense. Are you trying to call a function or...?

From https://github.com/tomstewart89/BasicLinearAlgebra/blob/master/examples/SolveLinearEquations/SolveLinearEquations.ino

Creating a matrix is done a bit differently than your code shows.

Matrix<6, 6> A = {16, 78, 50, 84, 70, 63, 2,  32, 33, 61, 40, 17, 96, 98, 50, 80, 78, 27,
                      86, 49, 57, 10, 42, 96, 44, 87, 60, 67, 16, 59, 53, 8,  64, 97, 41, 90};

I'm filling the matrix C here with the values specified between the {} brackets. I based it on the example of matrix A in the HowToUse section.

You'd do it by defining your own class that implements complex numbers and operators on them:

i suggest you develop your code with simulated input on your laptop. it will save hours of time

I fixed that for you. :wink:

It will also let you age less and retain more hair.

a7

In C++, complex numbers are a class. You will have to check whether Arduino IDE supports them, but in any case, the AVR (Arduino Uno, Mega, etc.) libraries do not support "double" data type. It is treated as single precision floats, which is not good for some operations.

https://www.cplusplus.com/reference/complex/complex/

That's worth looking in to. I don't have the BasicLinearAlgebra library installed, but std::complex works on a Teensy 3.2:

#include <complex>

using ComplexDouble = std::complex<double>;

void printComplex(const ComplexDouble &z);

void setup() {
  Serial.begin(115200);
  delay(1000);
  ComplexDouble z1(0, 1);
  ComplexDouble z2;

  Serial.print("z1: ");
  printComplex(z1);

  z2 = z1 * z1;
  Serial.print("z2: ");
  printComplex(z2);

}

void printComplex(const ComplexDouble &z) {
  Serial.printf("Real = %.8f, Imag = %.8f\n", z.real(), z.imag());
}

void loop() {
}

Output:

z1: Real = 0.00000000, Imag = 1.00000000
z2: Real = -1.00000000, Imag = 0.00000000

Of course, the AVR-based Arduinos don't support the STL. But, if I needed to do matrix math with complex numbers, an 8-bit AVR would not be my first choice anyway :laughing:

You can do tons of linear algebra stuff with the Arduino port of C++'s Eigen library: https://github.com/bolderflight/Eigen

I believe it supports complex matrices: https://stackoverflow.com/a/34707638/9860973

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.