2

I m looking at ILNumerics to translate some matlab code into c#.

How would I multiply a complex and a double?

Simplified description:

In Matlab:

A=[1 2 3] 
i*A*A'

Returns a complex number.

How would I do the same in ILNumerics:

        ILArray<double> A = ILMath.array(1.0, 2.0, 3.0);
        complex B = complex.i * ILMath.multiply(A,A.T);

Throws the error:

Operator '*' cannot be applied to operands of type 'ILNumerics.complex' and 'ILNumerics.ILRetArray<double>' 

Update

This works:

        double C = 14.0;
        complex D = complex.i * C;

But shouldnt: ILMath.multiply(A,A.T)

also return 14.0?

nik
  • 1,672
  • 2
  • 17
  • 36
  • What complex number did Matlab return? – ClickRick May 31 '14 at 14:42
  • Matlab returned 0 +14.0000i – nik May 31 '14 at 14:56
  • Matlab is outside of my area of expertise, and so don't know what operator * would do in that context, but did you perhaps mean to get the dot product of A and A' and multiply those? The dot product of A is 6.0, so that would yield 0+36i, which still isn't correct. – ClickRick May 31 '14 at 15:10
  • thank you ClickRick but I think they mean the same, * in matlab is teh matrix multiplication whereas .* is dotproduct. A = row, A.T=vector, so 1*1+2*2+3*3=14. No? – nik May 31 '14 at 15:13

2 Answers2

3

The first step is to make your array be one of complex values:

ILArray<complex> A = ILMath.array((complex)1.0, 2.0, 3.0);

The remaining question - that of multiplying a scalar by an array - boils down to what it means. The answer is that it is an array where each element of the original is multiplied by the scalar.

ILArray<complex> B = complex.i * ILMath.multiply(A, A.T);

B.ToString() is this:

0.00000+1.00000i 0.00000+2.00000i 0.00000+3.00000i
0.00000+2.00000i 0.00000+4.00000i 0.00000+6.00000i
0.00000+3.00000i 0.00000+6.00000i 0.00000+9.00000i

However, transpose the parameters to multiply like this:

complex B = complex.i * (complex)ILMath.multiply(A.T, A);

and the result is 0+14i, the same as from Matlab.

ClickRick
  • 1,553
  • 2
  • 17
  • 37
  • Thanks. The first part I get, I must use a complex array with only real parts (because my array is really a double). The second part I dont get at all. In my example A*A.T should return 14, how would I get a complex array back? This should only return one complex number. No? – nik May 31 '14 at 14:48
  • 2
    ClickRick is right. `array(1.0,2.0,3.0)` creates a column vector. `multiply(A, A.T)` returns a matrix. If you transpose `A.T` you get the result you expect: `0+14i` – Haymo Kutschbach May 31 '14 at 15:17
  • @HaymoKutschbach: But I do need to cast to double even if ILNumerics.ILRetArray only contains 1 double, correct? – nik May 31 '14 at 15:32
  • If you need a System.Double from a scalar ILArray, yes: you need to cast (double) or use `A.GetValue(0)`. It is not done implicitely since both variants can fail at runtime. (The other way around *is* implicitly converted: System.Double -> ILArray however.) – Haymo Kutschbach May 31 '14 at 21:42
1

I guess this works:

 ILArray<double> A = ILMath.array(1.0, 2.0, 3.0);
 complex B = complex.i * (double)ILMath.multiply(A.T,A);

and thus returns the same as Matlab

nik
  • 1,672
  • 2
  • 17
  • 36
  • I get an ILCastException thrown with the details "Additional information: unable to convert array to scalar". – ClickRick May 31 '14 at 15:14