Skip to content

Instantly share code, notes, and snippets.

@ikr7 ikr7/complex.glsl
Last active Jan 6, 2018

Embed
What would you like to do?
Complex functions for GLSL (Note: not very optimized)
// absolute value of z (equivalent of built-in function `length`)
float cabs (vec2 z) {
return length(z);
}
// complex conjugate of z
vec2 cconj (vec2 z) {
return vec2(z.x, -z.y);
}
// a times b
vec2 cmul (vec2 a, vec2 b) {
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + b.x * a.y);
}
// a divided by b
vec2 cdiv (vec2 a, vec2 b) {
float den = dot(b, b);
return vec2(
(a.x * b.x + a.y * b.y) / den,
(a.x * b.x - a.y * b.y) / den
);
}
// exponential of z
vec2 cexp (vec2 z) {
return exp(z.x) * vec2(cos(z.y), sin(z.y));
}
// principal value of logarithm of z
vec2 clog (vec2 z) {
return vec2(log(cabs(z)), atan(z.y, z.x));
}
// principal value of z to the a power
vec2 cpow (vec2 z, vec2 a) {
return cexp(cmul(a, clog(z)));
}
// cosine of z
vec2 ccos (vec2 z) {
return (cexp(vec2(-z.y, z.x)) + cexp(vec2(z.y, -z.x))) / 2.0;
}
// sine of z
vec2 csin (vec2 z) {
vec2 t = (cexp(vec2(-z.y, z.x)) - cexp(vec2(z.y, -z.x))) / 2.0;
return vec2(-t.y, -t.x);
}
// tangent of z
vec2 ctan (vec2 z) {
return cdiv(csin(z), ccos(z));
}
// hyperbolic cosine of z
vec2 ccosh (vec2 z) {
return (cexp(z) + cexp(-z)) / 2.0;
}
// hyperbolic sine of z
vec2 csinh (vec2 z) {
return (cexp(z) - cexp(-z)) / 2.0;
}
// hyperbolic tangent of z
vec2 ctanh (vec2 z) {
return cdiv(csinh(z), ccosh(z));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.