Problem with complex array input arguments to Octave C++ Dynamically Linked Functions

Viewed 29

Consider the following simplified example:

File 1 (The Octave .m file)

% test_multiplyCxArrays.m

clc; clear all; close all;
pkg load signal
mkoctfile multiplyCxArrays.cpp
disp('Compiled Octave-C++ function multiplyCxArrays.cpp');

cx_one = 1+1i;
a = [cx_one; cx_one*2; cx_one*3; cx_one*4; cx_one*5];
b = [cx_one; cx_one/2; cx_one/3; cx_one/4; cx_one/5];

disp('First input vector: ');
disp(a);

disp('First input vector inside DLD: ');
cx_out = multiplyCxArrays(a,b);

disp('Output vector returned DLD: ');
disp(cx_out);

File 2 (The C++ DLD file)

// multiplyCxArrays.cpp

#include <iostream>
#include <octave/oct.h>
#include <iostream>
#include <complex>
#include <vector>
#include <fstream>
#include <cmath>
#include <math.h>
#include "CNDArray.h"

using namespace std;

DEFUN_DLD (multiplyCxArrays, args, nargout,
           "multiplyCxArrays is a c++ compiled example to illustrate dynamically loaded functions in octave ")
{
  ComplexNDArray cxIn1 = args(0).array_value();
  ComplexNDArray cxIn2 = args(1).array_value();
  const int cxIn1_length = cxIn1.rows();
  const int cxIn2_length = cxIn2.rows();

  std::vector<std::complex<double>> cxVec;

  std::complex<double> cx_elem1, cx_elem2;
  for(octave_idx_type i = 0; i < cxIn1_length; ++i)
  {
    cout << "i = " << i << ", cxIn1(i) = " << cxIn1(i) << endl;
    cx_elem1 = cxIn1(i);
    cx_elem2 = cxIn2(i);
    cxVec.push_back(cx_elem1*cx_elem2);
  }

  const int cxOut_length = (int)cxVec.size();

  dim_vector dv (cxOut_length, 1);
  ComplexNDArray cxOut(dv);
  dv = cxOut.dims();
  for (octave_idx_type i = 0; i < cxOut_length; ++i)
  {
    cxOut(i) = cxVec[(int)i];
  }

  octave_value_list retval(nargout);
  retval(0) = octave_value(cxOut);
  return retval;
}

OUTPUT printout in Octave command window:

First input vector:
   1 + 1i
   2 + 2i
   3 + 3i
   4 + 4i
   5 + 5i
First input vector inside DLD:
i = 0, cxIn1(i) = (1,0)
i = 1, cxIn1(i) = (2,0)
i = 2, cxIn1(i) = (3,0)
i = 3, cxIn1(i) = (4,0)
i = 4, cxIn1(i) = (5,0)
Output vector returned DLD:
   1
   1
   1
   1
   1

For some reason the first argument of the DLD args(0).array_value() is only returning the real part of the input complex array and zeroing out the imaginary part as you can see from the output printout for cxIn1(i). Is there any way to retrieve the full complex array from the input argument list?

0 Answers
Related