# Name

SPVM::Document::Tutorial::Complex - complex tutorial.

# Description

This document is complex tutorial.

# Exampels

## Complex Tutorial

SPVM provides common arithmetic for complex numbers at the same level as C99.

### Complex Type

Complex Type is Complex_2f for complex float and Complex_2d for complex double. Complex_2f and Complex_2d is Multi Numeric Type. This is allocated on Method Call Stack. This is not Object Type which is allocated on Heap Memory. See SPVM Complex Examples at first.

#### Complex_2f

```# Complex float type
use Complex_2f;

my \$z : Complex_2f;
\$z->{re} = 1.5f;
\$z->{im} = 1.7f;
```

#### Complex_2d

```# Complex double type
use Complex_2d;

my \$z : Complex_2d;
\$z->{re} = 1.5;
\$z->{im} = 1.7;
```

### New Complex Functions

New Complex functions
```use Math (complexf, complex);

# Complex_2f
my \$z = complexf(1.5f, 1.7f);

# Complex_2d
my \$z = complex(1.5, 1.7);
```

### Complex Operation

#### float Complex Addition, Subtract, Multiply, Scalar Multiply, Division

float Complex Addition, Subtract, Multiply, Scalar Multiply, Division.
```# float Addition, Subtract, Multiply, Scalar Multiply, Division functions
use Math(caddf, csubf, cmulf, cscamulf, cdivf);

my \$z1 = complexf(1.5f, 1.7f);
my \$z2 = complexf(2.5f, 2.7f);

# Subtract
my \$z_method = csubf(\$z1, \$z2);

# Multiply
my \$z_mul = cmulf(\$z1, \$z2);

# Scalar Multiply
my \$z_scamul = cscamulf(3, \$z2);

# Division
my \$z_div = cdivf(\$z1, \$z2);
```

#### double Complex Addition, Subtract, Multiply, Scalar Multiply, Division

```# double Addition, Subtract, Multiply, Scalar Multiply, Division functions
use Math(cadd, csub, cmul, cscamul, cdiv);

my \$z1 = complex(1.5, 1.7);
my \$z2 = complex(2.5, 2.7);

# Subtract
my \$z_method = csub(\$z1, \$z2);

# Multiply
my \$z_mul = cmul(\$z1, \$z2);

# Scalar Multiply
my \$z_scamul = cscamul(3, \$z2);

# Division
my \$z_div = cdiv(\$z1, \$z2);
```

### Trigonometric functions

Trigonometric functions.

#### float Trigonometric functions

float Trigonometric functions.
```# float Trigonometric functions
use Math(csinf, ccosf, ctanf);

my \$z = complexf(1.5f, 1.7f);

my \$z_sin = csinf(\$z);

# Subtract
my \$z_cos = ccosf(\$z);

# Multiply
my \$z_tan = ctanf(\$z);
```

#### double Trigonometric functions

double Trigonometric functions.
```# double Trigonometric functions
use Math(csin, ccos, ctan);

my \$z = complex(1.5, 1.7);

my \$z_sin = csin(\$z);

# Subtract
my \$z_cos = ccos(\$z);

# Multiply
my \$z_tan = ctan(\$z);
```
See Math for more complex functions

### Complex Array

SPVM Array of Complex has values arranged in contiguous memory areas.

#### Complex_2f[]

```# Complex float type
use Complex_2f;

my \$zs = new Complex_2f;

for (my \$i = 0; \$i < @\$zs; \$i++) {
my \$z = \$zs->[\$i];
\$z->{re} = 1.5f;
\$z->{im} = 1.7f;
}
```

#### Complex_2d

```# Complex double type
use Complex_2d;

my \$zs = new Complex_2d;

for (my \$i = 0; \$i < @\$zs; \$i++) {
my \$z = \$zs->[\$i];
\$z->{re} = 1.5;
\$z->{im} = 1.7;
}
```

### Call complex function from Perl

Call complex function from Perl. Argument is passed and return value is return.
```use SPVM 'MyComplex';

my \$z1 = {re => 1.7, im => 2.7};
my \$z2 = {re => 7.5, im => 2.5};

my \$z_ret = MyComplex->complex_call_from_perl(\$z1, \$z2);

print "(\$z_ret->{re}, \$z_ret->{im})\n";
```