# SPVM Document

## Complex Tutorial

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

### Complex Type

Complex Type is SPVM::Complex_2f for complex float and SPVM::Complex_2d for complex double.

SPVM::Complex_2f and SPVM::Complex_2d is Multi Numeric Type. This is allocated on Subroutine Call Stack.

This is not Object Type which is allocated on Heap Memory.

See SPVM Complex Examples at first.

#### SPVM::Complex_2f

```# Complex float type
use SPVM::Complex_2f;

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

#### SPVM::Complex_2d

```# Complex double type
use SPVM::Complex_2d;

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

### New Complex Functions

New Complex functions

```use SPVM::Math (complexf, complex);

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

# SPVM::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 SPVM::Math(caddf, csubf, cmulf, cscamulf, cdivf);

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

# Addition
my \$z_add = caddf(\$z1, \$z2);

# Subtract
my \$z_sub = 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 SPVM::Math(cadd, csub, cmul, cscamul, cdiv);

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

# Addition
my \$z_add = cadd(\$z1, \$z2);

# Subtract
my \$z_sub = 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 SPVM::Math(csinf, ccosf, ctanf);

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

# Addition
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 SPVM::Math(csin, ccos, ctan);

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

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

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

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

See SPVM::Math for more complex functions

### Complex Array

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

#### SPVM::Complex_2f[]

```# Complex float type
use SPVM::Complex_2f;

my \$zs = new SPVM::Complex_2f;

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

#### SPVM::Complex_2d

```# Complex double type
use SPVM::Complex_2d;

my \$zs = new SPVM::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";
```
Documents