Skip to content
Paco Zamora Martinez edited this page Jan 2, 2014 · 3 revisions

Introduction

Package complex could be loaded via the standalone binary, or in Lua with require("aprilann.complex).

The complex is a new kind of data added binded from C++, which could be used with matrixComplex and has available math operations in Lua and using CBLAS interface.

IMPORTANT as the complex data-type is a C++ object, it is available via a reference pointer, be careful because the assignation is done by reference, not by content.

Construction

Exists two possible constructors:

> c = complex(2,-1) -- 2 is real part, -1 is imaginary part
> print(c)
 2-1i
>
> -- the string is parsed in C++, worst performance than previous constructor
> c = complex("2+4i")
> print(c)
 2+4i

Math operations

The opreators '==', '*', '/', '+', '-' are defined to work with complex objects. If the other operand is a number, it is converted to a complex with only real part. If the other operand is a string, it will be converted to a complex number using the constructor from string.

Besides previous operations, the following math methods are available:

  • self = c:conj() conjugates the given object. It is done in-place, so the object will be modified. Returns the caller object (self).

  • real,imaginary = c:plane() returns the real and imaginary part.

  • number = c:real() returns the real part of the number.

  • number = c:img() returns the real part of the number.

  • abs,angle = c:polar() returns the abs and angle of its polar form.

  • number = c:abs() returns the 2-norm of the caller complex number.

  • number = c:angle() returns the angle of its polar form.

  • other_complex = c:exp() returns the exponential (e^z) of the caller complex number.

  • number = c:sqrt() returns the square-root of the caller complex number.

> c1 = complex(1,2)
> c2 = complex(-4,-5)
> print(c1+c2)
 -3-3i
> print(c1*c2)
 6-13i
> print(c1-c2)
 5+7i
> print(c1:exp(), c2:exp())
 1.46869+2.47173i	-0.0119719+0.0175633i
> print(c1:abs(), c2:abs())
2.2360680103302	6.403124332428

Other methods

  • other = c:clone() produces a new complex instance which has the same content as the caller.
Clone this wiki locally