Skip to content

Commit

Permalink
complex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNaeAbC committed Jan 24, 2021
1 parent 5aad377 commit 8b9d44e
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 4 deletions.
157 changes: 157 additions & 0 deletions amathlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#endif

#include <cmath>
#include <iostream>


union doublevec4 {
Expand Down Expand Up @@ -266,6 +267,40 @@ union u16vec64 {
uint16_t c[64];
};

class VectorU16_2D {
public:
u16vec2 v;

VectorU16_2D() {
v.c[0] = 0;
v.c[1] = 0;
}


VectorU16_2D(uint16_t a, uint16_t b) {
v.c[0] = a;
v.c[1] = b;
}
};

class VectorU8_4D {
public:
u8vec4 v;

VectorU8_4D() {
v.c[0] = 0;
v.c[1] = 0;
}


VectorU8_4D(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
v.c[0] = a;
v.c[1] = b;
v.c[3] = c;
v.c[4] = d;
}
};


class VectorDouble4D {
private:
Expand Down Expand Up @@ -695,4 +730,126 @@ class VectorDouble8D {
}
};


class Complex64 {
public:
doublevec2 c;


inline Complex64(double real, double img) {
c.c[0] = real;
c.c[1] = img;
}

inline Complex64() {
c.c[0] = 0;
c.c[1] = 0;
}

inline Complex64 *add(Complex64 a) {
c.c[0] += a.c.c[0];
c.c[1] += a.c.c[1];
return this;
}

inline Complex64 *subtract(Complex64 a) {
c.c[0] -= a.c.c[0];
c.c[1] -= a.c.c[1];
return this;
}

inline Complex64 *conjugate() {
c.c[1] = -c.c[1];
return this;
}


inline Complex64 *multiply(Complex64 a) {
double d1 = c.c[0] * a.c.c[0] - c.c[1] * a.c.c[1];
double d2 = c.c[0] * a.c.c[1] + c.c[1] * a.c.c[0];
c.c[0] = d1;
c.c[1] = d2;
return this;
}

inline Complex64 *divide(Complex64 a) {
double d1 = (c.c[0] * a.c.c[0] + c.c[1] * a.c.c[1]) / (a.c.c[0] * a.c.c[0] + a.c.c[1] * a.c.c[1]);
double d2 = (c.c[1] * a.c.c[0] - c.c[0] * a.c.c[1]) / (a.c.c[0] * a.c.c[0] + a.c.c[1] * a.c.c[1]);

c.c[0] = d1;
c.c[1] = d2;

return this;
}

inline Complex64 *sqrt() {
double d2 = ::sqrt((-c.c[0] + ::sqrt(c.c[0] * c.c[0] + c.c[1] * c.c[1])) / (2));
double d1;
if (d2 == 0) {
d1 = ::sqrt(c.c[0]);
} else {
d1 = c.c[1] / (2 * d2);
}
c.c[0] = d1;
c.c[1] = d2;
return this;
}

inline Complex64 *sin() {
double d1 = ::sin(c.c[0]) * ::cosh(c.c[1]);
double d2 = ::cos(c.c[1]) * ::sinh(c.c[0]);

c.c[0] = d1;
c.c[1] = d2;
return this;
}


inline Complex64 *exp() {
double d1 = ::exp(c.c[0]) * ::cos(c.c[1]);
double d2 = ::exp(c.c[0]) * ::sin(c.c[1]);


c.c[0] = d1;
c.c[1] = d2;
return this;
}

inline Complex64 *exp(double n) {
double d1 = ::pow(n, c.c[0]) * ::cos(c.c[1] * ::log(n));
double d2 = ::pow(n, c.c[0]) * ::sin(c.c[1] * ::log(n));


c.c[0] = d1;
c.c[1] = d2;
return this;
}

inline double abs() {
return ::sqrt(c.c[0] * c.c[0] + c.c[1] * c.c[1]);
}

inline bool abs_gt(double a) {
return a * a < c.c[0] * c.c[0] + c.c[1] * c.c[1];
}

inline bool abs_lt(double a) {
return a * a > c.c[0] * c.c[0] + c.c[1] * c.c[1];
}

inline bool abs_eq(double a) {
return a * a == c.c[0] * c.c[0] + c.c[1] * c.c[1];
}

inline double imaginary() {
return c.c[1];
}

inline double real() {
return c.c[0];
}


};

#endif //MATH_LIB_A_MATH_LIB_H
2 changes: 1 addition & 1 deletion aml_lua_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "aml_lua_binding.h"
#include <iostream>

int amlAdd4xN(lua_State *L) {
int amlAdd4xN(lua_State *L) { // TODO detect the length of the table
if (lua_istable(L, 1) && lua_istable(L, 2)) {

double a0 = 0;
Expand Down
21 changes: 20 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ int main() {
std::cout << vector8_1[0] << " " << vector8_1[1] << " " << vector8_1[2] << " " << vector8_1[3] << " "
<< vector8_1[4] << " " << vector8_1[5] << " " << vector8_1[6] << " " << vector8_1[7] << std::endl;

Complex64 cmp1(2, 4);
Complex64 cmp2(4, 1);

cmp2.divide(cmp1);

std::cout << cmp2.real() << " + " << cmp2.imaginary() << "i" << std::endl;


cmp2.multiply(cmp1);

std::cout << cmp2.real() << " + " << cmp2.imaginary() << "i" << std::endl;

cmp2.sqrt();
std::cout << cmp2.real() << " + " << cmp2.imaginary() << "i" << std::endl;

cmp2.multiply(cmp2);

std::cout << cmp2.real() << " + " << cmp2.imaginary() << "i" << std::endl;

lua_State *L = luaL_newstate();
luaL_openlibs(L);

Expand All @@ -105,7 +124,7 @@ int main() {

uint64_t beginTimer = std::chrono::steady_clock::now().time_since_epoch().count();

for (int i = 0; i < 10000000; i++) {
for (int i = 0; i < 10; i++) {

VectorDouble4D vecABench(1.0, 2.0, 3.0, 4.0);
VectorDouble4D vecBBench(-20.0, 100.0, 80.0, 200.0);
Expand Down
4 changes: 2 additions & 2 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end

first_time = os.clock()

for i = 1, 10000000 do
for i = 1, 10 do

local a = { 1, 2, 3, 4 }
local b = { -20.0, 100.0, 80.0, 200.0 }
Expand All @@ -34,7 +34,7 @@ print(last_time - first_time)

first_time = os.clock()

for i = 1, 10000000 do
for i = 1, 10 do

local a = { 1, 2, 3, 4 }
local b = { -20.0, 100.0, 80.0, 200.0 }
Expand Down

0 comments on commit 8b9d44e

Please sign in to comment.