complex.c 4.88 KB
Newer Older
1 2 3 4
/*
 * complex.c:  A quick library for complex math.
 *
 * Author:
Morten Welinder's avatar
Morten Welinder committed
5
 *  Morten Welinder <terra@gnome.org>
6 7 8
 *  Jukka-Pekka Iivonen <iivonen@iki.fi>
 */

9 10 11 12 13
#include <gnumeric-config.h>
#include "gnumeric.h"
#define GNUMERIC_COMPLEX_IMPLEMENTATION
#include "complex.h"

14
#include <stdlib.h>
15
#include <errno.h>
16 17


18 19
/* ------------------------------------------------------------------------- */

20 21 22 23 24 25
char *
complex_to_string (const complex_t *src, const char *reformat,
		   const char *imformat, char imunit)
{
	char *re_buffer = NULL;
	char *im_buffer = NULL;
26 27
	const char *sign = "";
	const char *suffix = "";
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	char *res;
	char suffix_buffer[2];

	if (src->re != 0 || src->im == 0) {
		/* We have a real part.  */
		re_buffer = g_strdup_printf (reformat, src->re);
	}

	if (src->im != 0) {
		/* We have an imaginary part.  */
		suffix = suffix_buffer;
		suffix_buffer[0] = imunit;
		suffix_buffer[1] = 0;
		if (src->im == 1) {
			if (re_buffer)
				sign = "+";
		} else if (src->im == -1) {
			sign = "-";
		} else {
Morten Welinder's avatar
Morten Welinder committed
47
			im_buffer = g_strdup_printf (imformat, src->im);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
			if (re_buffer && *im_buffer != '-' && *im_buffer != '+')
				sign = (src->im >= 0) ? "+" : "-";
		}
	}

	res = g_strconcat (re_buffer ? re_buffer : "",
			   sign,
			   im_buffer ? im_buffer : "",
			   suffix,
			   NULL);

	if (re_buffer) g_free (re_buffer);
	if (im_buffer) g_free (im_buffer);

	return res;
}

65 66
/* ------------------------------------------------------------------------- */

67
static int
68
is_unit_imaginary (const char *src, gnm_float *im, char *imunit)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	if (*src == '-') {
		*im = -1.0;
		src++;
	} else {
		*im = +1.0;
		if (*src == '+') src++;
	}

	if ((*src == 'i' || *src == 'j') && src[1] == 0) {
		*imunit = *src;
		return 1;
	} else
		return 0;
}

int
complex_from_string (complex_t *dst, const char *src, char *imunit)
{
88
	gnm_float x, y;
89 90 91 92 93 94 95 96
	char *end;

	/* Case: "i", "+i", "-i", ...  */
	if (is_unit_imaginary (src, &dst->im, imunit)) {
		dst->re = 0;
		return 0;
	}

97
	errno = 0;
98
	x = gnm_strto (src, &end);
99
	if (src == end || errno == ERANGE)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		return -1;
	src = end;

	/* Case: "42", "+42", "-42", ...  */
	if (*src == 0) {
		complex_real (dst, x);
		*imunit = 'i';
		return 0;
	}

	/* Case: "42i", "+42i", "-42i", ...  */
	if ((*src == 'i' || *src == 'j') && src[1] == 0) {
		complex_init (dst, 0, x);
		*imunit = *src;
		return 0;
	}

	/* Case: "42+i", "+42-i", "-42-i", ...  */
	if (is_unit_imaginary (src, &dst->im, imunit)) {
		dst->re = x;
		return 0;
	}

123
	y = gnm_strto (src, &end);
124
	if (src == end || errno == ERANGE)
125 126 127 128 129 130 131 132 133 134 135 136 137
		return -1;
	src = end;

	/* Case: "42+12i", "+42-12i", "-42-12i", ...  */
	if ((*src == 'i' || *src == 'j') && src[1] == 0) {
		complex_init (dst, x, y);
		*imunit = *src;
		return 0;
	}

	return -1;
}

138
/* ------------------------------------------------------------------------- */
Morten Welinder's avatar
Morten Welinder committed
139 140 141 142 143 144 145 146 147 148 149 150 151

void
complex_to_polar (gnm_float *mod, gnm_float *angle, const complex_t *src)
{
	*mod = complex_mod (src);
	*angle = complex_angle (src);
}

/* ------------------------------------------------------------------------- */

void
complex_from_polar (complex_t *dst, gnm_float mod, gnm_float angle)
{
152
	complex_init (dst, mod * gnm_cos (angle), mod * gnm_sin (angle));
Morten Welinder's avatar
Morten Welinder committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

/* ------------------------------------------------------------------------- */

void
complex_mul (complex_t *dst, const complex_t *a, const complex_t *b)
{
	complex_init (dst,
		      a->re * b->re - a->im * b->im,
		      a->re * b->im + a->im * b->re);
}

/* ------------------------------------------------------------------------- */

void
complex_div (complex_t *dst, const complex_t *a, const complex_t *b)
{
	gnm_float bmod = complex_mod (b);

	if (bmod >= GNM_const(1e10)) {
		/* Ok, it's big.  */
		gnm_float a_re = a->re / bmod;
		gnm_float a_im = a->im / bmod;
		gnm_float b_re = b->re / bmod;
		gnm_float b_im = b->im / bmod;
		complex_init (dst,
			      a_re * b_re + a_im * b_im,
			      a_im * b_re - a_re * b_im);
	} else {
		gnm_float bmodsqr = bmod * bmod;
		complex_init (dst,
			      (a->re * b->re + a->im * b->im) / bmodsqr,
			      (a->im * b->re - a->re * b->im) / bmodsqr);
	}
}

/* ------------------------------------------------------------------------- */

void
complex_sqrt (complex_t *dst, const complex_t *src)
{
	if (complex_real_p (src)) {
		if (src->re >= 0)
196
			complex_init (dst, gnm_sqrt (src->re), 0);
Morten Welinder's avatar
Morten Welinder committed
197
		else
198
			complex_init (dst, 0, gnm_sqrt (-src->re));
Morten Welinder's avatar
Morten Welinder committed
199 200
	} else
		complex_from_polar (dst,
201
				    gnm_sqrt (complex_mod (src)),
Morten Welinder's avatar
Morten Welinder committed
202 203 204 205 206 207 208 209 210 211 212 213
				    complex_angle (src) / 2);
}

/* ------------------------------------------------------------------------- */

void
complex_pow (complex_t *dst, const complex_t *a, const complex_t *b)
{
	complex_t lna, b_lna;

	/* ln is not defined for reals less than or equal to zero.  */
	if (complex_real_p (a) && complex_real_p (b))
214
		complex_init (dst, gnm_pow (a->re, b->re), 0);
Morten Welinder's avatar
Morten Welinder committed
215 216 217 218 219 220 221 222
	else {
		complex_ln (&lna, a);
		complex_mul (&b_lna, b, &lna);
		complex_exp (dst, &b_lna);
	}
}

/* ------------------------------------------------------------------------- */