Skip to content

Instantly share code, notes, and snippets.

@Mark1626
Last active January 11, 2024 07:14
Show Gist options
  • Save Mark1626/a6eb382700c3f316409428afba57231a to your computer and use it in GitHub Desktop.
Save Mark1626/a6eb382700c3f316409428afba57231a to your computer and use it in GitHub Desktop.
Complex numbers for Dahlia
record complex {
real: float;
imag: float
}
def complex_mul(a: complex, b: complex): complex = {
let cr: float = (a.real * b.real) - (a.imag * b.imag);
let ci: float = (a.real * b.imag) + (a.imag * b.real);
let c: complex = { real=cr; imag=ci };
return c;
}
def complex_add(a: complex, b: complex): complex = {
let cr: float = a.real + b.real;
let ci: float = a.imag + b.imag;
let c: complex = { real=cr; imag=ci };
return c;
}
def complex_fma(a: complex, b: complex, c: complex): complex = {
let cr: float = c.real + (a.real * b.real) - (a.imag * b.imag);
let ci: float = c.imag + (a.real * b.imag) + (a.imag * b.real);
let rc: complex = { real=cr; imag=ci };
return rc;
}
def complex_scale(s: float, c: complex): complex = {
let sr: float = c.real * s;
let si: float = c.imag * s;
let sc: complex = { real=sr; imag=si };
return sc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment