SciSharp/Numpy.NET

Problem with passing imaginery numbers

dariusz-wozniak opened this issue · 3 comments

I'm rewriting part of the Signal.freqz (ln. 478-483) function form Python and got stuck with the following Python part:

freqz function part:

zm1 = exp(-1j * w)
h = (npp_polyval(zm1, b, tensor=False) / npp_polyval(zm1, a, tensor=False))

npp_polyval function:

c = np.array(c, ndmin=1, copy=False)

#...

c0 = c[-1] + x*0
for i in range(2, len(c) + 1):
    c0 = c[-i] + c0*x
return c0

I translated the above to the following C# code—

freqz function part:

using var i = np.array(new Complex[] { new(real: 0, imaginary: -1) });
var zm1 = (i.imag * w).exp();
NDarray h = npp_polyval(zm1, b, tensor: false) / npp_polyval(zm1, a, tensor: false);

and npp_polyval:

method definition: NDarray npp_polyval(NDarray x, NDarray c, bool tensor = false)

c = np.array(c, ndmin: 1, copy: false);
var c0 = c[":, -1"] + x * 0;

for (int i = 2; i <= c.len + 1; i++)
{
    c0 = c[$":, -{i}"] + c0 * x;
}
return c0;

Now, the results are different and question - why.

One note on the x.dtype: I've found the x in Python has dtype = complex128 while in C# it is float64. Should I convert that array to complex128? If so, how can I do that?

henon commented

Hi this issue is still too complex for my little time at the moment. Can you try to find out exactly where the values or the array type diverges and reduce the problem to its core? Then I'll see what I can do to help

henon commented

Hi this issue is still too complex ...

Btw, no pun intended ;)

This has been resolved; there are a few problems with the aforementioned code:

  • i.imagi
  • c.lenc.size

Now, the C# code returns the same results as Python one ;)