summaryrefslogtreecommitdiff
path: root/source/luametatex/source/luarest/lmtxcomplexlib.c
blob: b97a5ca2310be465b7a154c976425370a478dd60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*

    See license.txt in the root of this project.

    This is a reformatted and slightly adapted version of lcomplex.c:

    title  : C99 complex numbers for Lua 5.3+
    author : Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
    date   : 26 Jul 2018 17:57:06
    licence: This code is hereby placed in the public domain and also under the MIT license

    That implementation doesn't work for MSVC so I rewrote the code to support the microsoft
    compiler. I no longer use the macro approach to save bytes because with expanded code it is
    easier to get rid of some compiler warnings (if possible at all).

    In an optional module we hook the error functions into the complex library.

    Note: Alan has to test if all works okay.

*/

# include "luametatex.h"

# include <complex.h>

# define COMPLEX_METATABLE "complex number"

# if (_MSC_VER)

    /*tex
        Instead of the somewhat strange two-doubles-in-a-row hack in C the microsoft vatiant
        uses structs. Here we use the double variant.
    */

    # define Complex _Dcomplex

    inline static Complex xcomplexlib_get(lua_State *L, int i)
    {
        switch (lua_type(L, i)) {
            case LUA_TUSERDATA:
                return *((Complex*) luaL_checkudata(L, i, COMPLEX_METATABLE));
            case LUA_TNUMBER:
            case LUA_TSTRING:
                return _Cbuild(luaL_checknumber(L, i), 0);
            default:
                return _Cbuild(0, 0);
        }
    }

# else

    /*tex
        Here we use the two-doubles-in-a-row variant.
    */

    # define Complex double complex

    inline static Complex xcomplexlib_get(lua_State *L, int i)
    {
        switch (lua_type(L, i)) {
            case LUA_TUSERDATA:
                return *((Complex*)luaL_checkudata(L, i, COMPLEX_METATABLE));
            case LUA_TNUMBER:
            case LUA_TSTRING:
                return luaL_checknumber(L, i);
            default:
                return 0;
        }
    }

# endif

inline static int xcomplexlib_push(lua_State *L, Complex z)
{
    Complex *p = lua_newuserdatauv(L, sizeof(Complex), 0);
    luaL_setmetatable(L, COMPLEX_METATABLE);
    *p = z;
    return 1;
}

# if (_MSC_VER)

    static int xcomplexlib_new(lua_State *L)
    {
        xcomplexlib_push(L, _Cbuild(0, 0));
        return 1;
    }

    static int xcomplexlib_inew(lua_State *L)
    {
        xcomplexlib_push(L, _Cbuild(0, 1));
        return 1;
    }

    static int xcomplexlib_eq(lua_State *L)
    {
        Complex a = xcomplexlib_get(L, 1);
        Complex b = xcomplexlib_get(L, 2);
        lua_pushboolean(L, creal(a) == creal(b) && cimag(a) == cimag(b));
        return 1;
    }

    static int xcomplexlib_add(lua_State *L) {
        Complex a = xcomplexlib_get(L, 1);
        Complex b = xcomplexlib_get(L, 2);
        return xcomplexlib_push(L, _Cbuild(creal(a) + creal(b), cimag(a) + cimag(b)));
    }

    static int xcomplexlib_sub(lua_State *L) {
        Complex a = xcomplexlib_get(L, 1);
        Complex b = xcomplexlib_get(L, 2);
        return xcomplexlib_push(L, _Cbuild(creal(a) - creal(b), cimag(a) - cimag(b)));
    }

    static int xcomplexlib_neg(lua_State *L) {
        Complex a = xcomplexlib_get(L, 1);
        return xcomplexlib_push(L, _Cbuild(-creal(a), -cimag(a)));
    }

    static int xcomplexlib_div(lua_State *L) {
        Complex b = xcomplexlib_get(L, 2);
        if (creal(b) == 0.0 || cimag(b) == 0.0) {
            return 0;
        } else {
            Complex a = xcomplexlib_get(L, 1);
            Complex t = { 1 / creal(b), 1 / cimag(b) };
            return xcomplexlib_push(L, _Cmulcc(a, t));
        }
    }

    static int xcomplexlib_mul(lua_State *L) {
        Complex a = xcomplexlib_get(L, 1);
        Complex b = xcomplexlib_get(L, 2);
        return xcomplexlib_push(L, _Cmulcc(a, b));
    }

# else

    static int xcomplexlib_new(lua_State *L)
    {
        return xcomplexlib_push(L, luaL_optnumber(L, 1, 0) + luaL_optnumber(L, 2, 0) * I);
    }

    static int xcomplexlib_inew(lua_State *L)
    {
        return xcomplexlib_push(L, I);
    }

    static int xcomplexlib_eq(lua_State *L)
    {
        lua_pushboolean(L, xcomplexlib_get(L, 1) == xcomplexlib_get(L, 2));
        return 1;
    }

    static int xcomplexlib_add(lua_State *L)
    {
        return xcomplexlib_push(L, xcomplexlib_get(L, 1) + xcomplexlib_get(L, 2));
    }

    static int xcomplexlib_sub(lua_State *L)
    {
        return xcomplexlib_push(L, xcomplexlib_get(L, 1) - xcomplexlib_get(L, 2));
    }

    static int xcomplexlib_neg(lua_State *L)
    {
        return xcomplexlib_push(L, - xcomplexlib_get(L, 1));
    }

    static int xcomplexlib_div(lua_State *L)
    {
        return xcomplexlib_push(L, xcomplexlib_get(L, 1) / xcomplexlib_get(L, 2));
    }

    static int xcomplexlib_mul(lua_State *L)
    {
        return xcomplexlib_push(L, xcomplexlib_get(L, 1) * xcomplexlib_get(L, 2));
    }

# endif

static int xcomplexlib_abs(lua_State *L)
{
    lua_pushnumber(L, (lua_Number) cabs(xcomplexlib_get(L, 1)));
    return 1;
}

static int xcomplexlib_acos(lua_State *L)
{
    return xcomplexlib_push(L, cacos(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_acosh(lua_State *L)
{
    return xcomplexlib_push(L, cacosh(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_arg(lua_State *L)
{
    lua_pushnumber(L, (lua_Number) carg(xcomplexlib_get(L, 1)));
    return 1;
}

static int xcomplexlib_asin(lua_State *L)
{
    return xcomplexlib_push(L, casin(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_asinh(lua_State *L)
{
    return xcomplexlib_push(L, casinh(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_atan(lua_State *L)
{
    return xcomplexlib_push(L, catan(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_atanh(lua_State *L)
{
    return xcomplexlib_push(L, catanh(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_cos(lua_State *L)
{
    return xcomplexlib_push(L, ccos(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_cosh(lua_State *L)
{
    return xcomplexlib_push(L, ccosh(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_exp(lua_State *L)
{
    xcomplexlib_push(L, cexp(xcomplexlib_get(L, 1)));
    return 1;
}

static int xcomplexlib_imag(lua_State *L) {
    lua_pushnumber(L, (lua_Number) (cimag)(xcomplexlib_get(L, 1)));
    return 1;
}

static int xcomplexlib_log(lua_State *L)
{
    return xcomplexlib_push(L, clog(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_pow(lua_State *L)
{
    return xcomplexlib_push(L, cpow(xcomplexlib_get(L, 1), xcomplexlib_get(L, 2)));
}

static int xcomplexlib_proj(lua_State *L)
{
    return xcomplexlib_push(L, cproj(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_real(lua_State *L)
{
    lua_pushnumber(L, (lua_Number) creal(xcomplexlib_get(L, 1)));
    return 1;
}

static int xcomplexlib_sin(lua_State *L)
{
    return xcomplexlib_push(L, csin(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_sinh(lua_State *L)
{
    return xcomplexlib_push(L, csinh(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_sqrt(lua_State *L)
{
    return xcomplexlib_push(L, csqrt(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_tan(lua_State *L)
{
    return xcomplexlib_push(L, ctan(xcomplexlib_get(L, 1)));
}

static int xcomplexlib_tanh(lua_State *L)
{
    return xcomplexlib_push(L, ctanh(xcomplexlib_get(L, 1)));
}

/*tex A few convenience functions: */

static int xcomplexlib_tostring(lua_State *L)
{
    Complex z = xcomplexlib_get(L, 1);
    lua_Number x = creal(z);
    lua_Number y = cimag(z);
    lua_settop(L, 0);
    if (x != 0.0 || y == 0.0) {
        lua_pushnumber(L, x);
    }
    if (y != 0.0) {
        if (y == 1.0) {
            if (x != 0.0) {
                lua_pushliteral(L, "+");
            }
        } else if (y == -1.0) {
            lua_pushliteral(L, "-");
        } else {
            if (y > 0.0 && x != 0.0) {
                lua_pushliteral(L, "+");
            }
            lua_pushnumber(L, y);
        }
        lua_pushliteral(L, "i");
    }
    lua_concat(L, lua_gettop(L));
    return 1;
}

static int xcomplexlib_topair(lua_State *L)
{
    Complex z = xcomplexlib_get(L, 1);
    lua_pushnumber(L, (lua_Number) creal(z));
    lua_pushnumber(L, (lua_Number) cimag(z));
    return 2;
}

static int xcomplexlib_totable(lua_State *L)
{
    Complex z = xcomplexlib_get(L, 1);
    lua_createtable(L, 2, 0);
    lua_pushnumber(L, (lua_Number) creal(z));
    lua_pushnumber(L, (lua_Number) cimag(z));
    lua_rawseti(L, -3, 1);
    lua_rawseti(L, -3, 2);
    return 1;
}

/*tex Now we assemble the library: */

static const struct luaL_Reg xcomplexlib_function_list[] = {
    /* management */
    { "new",        xcomplexlib_new        },
    { "tostring",   xcomplexlib_tostring   },
    { "topair",     xcomplexlib_topair     },
    { "totable",    xcomplexlib_totable    },
    { "i",          xcomplexlib_inew       },
    /* operators */
    { "__add",      xcomplexlib_add        },
    { "__div",      xcomplexlib_div        },
    { "__eq",       xcomplexlib_eq         },
    { "__mul",      xcomplexlib_mul        },
    { "__sub",      xcomplexlib_sub        },
    { "__unm",      xcomplexlib_neg        },
    { "__pow",      xcomplexlib_pow        },
    /* functions */
    { "abs",        xcomplexlib_abs        },
    { "acos",       xcomplexlib_acos       },
    { "acosh",      xcomplexlib_acosh      },
    { "arg",        xcomplexlib_arg        },
    { "asin",       xcomplexlib_asin       },
    { "asinh",      xcomplexlib_asinh      },
    { "atan",       xcomplexlib_atan       },
    { "atanh",      xcomplexlib_atanh      },
    { "conj",       xcomplexlib_neg        },
    { "cos",        xcomplexlib_cos        },
    { "cosh",       xcomplexlib_cosh       },
    { "exp",        xcomplexlib_exp        },
    { "imag",       xcomplexlib_imag       },
    { "log",        xcomplexlib_log        },
    { "pow",        xcomplexlib_pow        },
    { "proj",       xcomplexlib_proj       },
    { "real",       xcomplexlib_real       },
    { "sin",        xcomplexlib_sin        },
    { "sinh",       xcomplexlib_sinh       },
    { "sqrt",       xcomplexlib_sqrt       },
    { "tan",        xcomplexlib_tan        },
    { "tanh",       xcomplexlib_tanh       },
    /* */
    { NULL,         NULL                   },
};

int luaopen_xcomplex(lua_State *L)
{
    luaL_newmetatable(L, COMPLEX_METATABLE);
    luaL_setfuncs(L, xcomplexlib_function_list, 0);
    lua_pushliteral(L, "__index");
    lua_pushvalue(L, -2);
    lua_settable(L, -3);
    lua_pushliteral(L, "__tostring");
    lua_pushliteral(L, "tostring");
    lua_gettable(L, -3);
    lua_settable(L, -3);
    lua_pushliteral(L, "I");
    lua_pushliteral(L, "i");
    lua_gettable(L, -3);
    lua_settable(L, -3);
    lua_pushliteral(L, "__name"); /* kind of redundant */
    lua_pushliteral(L, "complex");
    lua_settable(L, -3);
    return 1;
}