-
Notifications
You must be signed in to change notification settings - Fork 14
Complex
Complex numbers in SPVM are described.
Complex types are Complex_2f and Complex_2d.
The Complex_2f type is a multi-numeric type for a float complex number.
use Complex_2f;
my $z : Complex_2f;
$z->{re} = 1.5f;
$z->{im} = 1.7f;
The Complex_2d type is a multi-numeric type for a double complex number.
use Complex_2d;
my $z : Complex_2d;
$z->{re} = 1.5;
$z->{im} = 1.7;
The Math module has methods to manipulate complex numbers.
cpanm SPVM::Math
Complex numbers can be created by the complexf
method and the complex
method.
use Math;
# Complex_2f
my $z = Math->complexf(1.5f, 1.7f);
# Complex_2d
my $z = Math->complex(1.5, 1.7);
These are addition, subtraction, multiplication, scalar product, and division of complex numbers.
float Complex numbers:
use Math;
my $z1 = Math->complexf(1.5f, 1.7f);
my $z2 = Math->complexf(2.5f, 2.7f);
# Addition
my $z_add = Math->caddf($z1, $z2);
# Subtraction
my $z_method = Math->csubf($z1, $z2);
# Multiplication
my $z_mul = Math->cmulf($z1, $z2);
# Scalar Product
my $z_scamul = Math->cscamulf(3, $z2);
# Division
my $z_div = Math->cdivf($z1, $z2);
double Complex numbers:
use Math;
my $z1 = Math->complex(1.5, 1.7);
my $z2 = Math->complex(2.5, 2.7);
# Addition
my $z_add = Math->cadd($z1, $z2);
# Subtraction
my $z_method = Math->csub($z1, $z2);
# Multiplication
my $z_mul = Math->cmul($z1, $z2);
# Scalar Product
my $z_scamul = Math->cscamul(3, $z2);
# Division
my $z_div = Math->cdiv($z1, $z2);
These are trigonometric functions.
float complex numbers:
use Math;
my $z = Math->complexf(1.5f, 1.7f);
# Addition
my $z_sin = Math->csinf($z);
# Subtract
my $z_cos = Math->ccosf($z);
# Multiply
my $z_tan = Math->ctanf($z);
double complex numers:
use Math;
my $z = Math->complex(1.5, 1.7);
# Addition
my $z_sin = Math->csin($z);
# Subtract
my $z_cos = Math->ccos($z);
# Multiply
my $z_tan = Math->ctan($z);
An array of complex numbers with contiguous memory areas can be created.
float complex array:
use Complex_2f;
my $zs = new Complex_2f[100];
for (my $i = 0; $i < @$zs; $i++) {
my $z = $zs->[$i];
$z->{re} = 1.5f;
$z->{im} = 1.7f;
}
double complex array:
use Complex_2d;
my $zs = new Complex_2d[100];
for (my $i = 0; $i < @$zs; $i++) {
my $z = $zs->[$i];
$z->{re} = 1.5;
$z->{im} = 1.7;
}
Hash references are used to pass complex numbers to SPVM from Perl,
use SPVM 'MyComplex';
my $z1 = {re => 1.7, im => 2.7};
my $z2 = {re => 7.5, im => 2.5};
# method calc_complex : Complex_2d ($z1 : Complex_2d, $z2 : Complex_2d);
my $z_ret = SPVM::MyComplex->calc_complex($z1, $z2);