liba 0.1.15

An algorithm library based on C/C++
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/***
 complex number
 @module liba.complex
*/

#include "complex.h"
#include "a/complex.h"

static int liba_complex_isok(lua_State *L, int idx)
{
    int ok = 0;
    if (lua_getmetatable(L, idx))
    {
        lua_registry_get(L, liba_complex_new);
        ok = lua_rawequal(L, -1, -2);
        lua_pop(L, 2);
    }
    return ok;
}

static int liba_complex_from(lua_State *L, a_complex *z, int idx)
{
    int type = lua_type(L, idx);
    switch (type)
    {
    case LUA_TNUMBER:
        z->real = (a_float)lua_tonumber(L, idx);
        z->imag = 0;
        break;
    case LUA_TSTRING:
        a_complex_parse(z, lua_tostring(L, idx));
        break;
    case LUA_TUSERDATA:
        if (liba_complex_isok(L, idx))
        {
            *z = *(a_complex *)lua_touserdata(L, idx);
            break;
        }
        A_FALLTHROUGH;
    default:
        z->real = 0;
        z->imag = 0;
    }
    return type;
}

static a_complex *liba_complex_new_(lua_State *L)
{
    a_complex *const ctx = lua_newclass(L, a_complex);
    lua_registry_get(L, liba_complex_new);
    lua_setmetatable(L, -2);
    return ctx;
}

static int liba_complex_tostring(lua_State *L)
{
    a_complex const *const ctx = (a_complex const *)lua_touserdata(L, 1);
    if (ctx)
    {
        lua_pushfstring(L, "(%f,%f)", (double)ctx->real, (double)ctx->imag);
        return 1;
    }
    return 0;
}

/***
 constructor for complex number from real and imaginary parts
 @tparam[opt] number|string|a.complex real real part of complex number
 @tparam[opt] number imag imaginary part of complex number
 @treturn a.complex complex number userdata
 @function new
*/
int liba_complex_new(lua_State *L)
{
    a_complex z = A_COMPLEX_C(0.0, 0.0);
    int const top = lua_gettop(L);
    if (top >= 1)
    {
        int const type = liba_complex_from(L, &z, 1);
        if (type == LUA_TNUMBER && top >= 2)
        {
            z.imag = (a_float)lua_tonumber(L, 2);
        }
    }
    *liba_complex_new_(L) = z;
    return 1;
}

/***
 constructor for complex number from rectangular Cartesian components
 @tparam[opt] number real real part of complex number
 @tparam[opt] number imag imaginary part of complex number
 @treturn a.complex complex number userdata
 @function rect
*/
int liba_complex_rect(lua_State *L)
{
    a_float real = 0, imag = 0;
    int const top = lua_gettop(L);
    if (top >= 1) { real = (a_float)lua_tonumber(L, 1); }
    if (top >= 2) { imag = (a_float)lua_tonumber(L, 2); }
    a_complex_rect(liba_complex_new_(L), real, imag);
    return 1;
}

/***
 constructor for complex number from polar form
 @tparam[opt] number rho a distance from a reference point
 @tparam[opt] number theta an angle from a reference direction
 @treturn a.complex complex number userdata
 @function polar
*/
int liba_complex_polar(lua_State *L)
{
    a_float rho = 0, theta = 0;
    int const top = lua_gettop(L);
    if (top >= 1) { rho = (a_float)lua_tonumber(L, 1); }
    if (top >= 2) { theta = (a_float)lua_tonumber(L, 2); }
    a_complex_polar(liba_complex_new_(L), rho, theta);
    return 1;
}

/***
 complex number x is equal to complex number y
 @tparam a.complex x complex number on the left
 @tparam a.complex y complex number on the right
 @treturn bool result of comparison
 @function eq
*/
int liba_complex_eq(lua_State *L)
{
    if (lua_gettop(L) >= 2)
    {
        a_complex x, y;
        liba_complex_from(L, &x, 1);
        liba_complex_from(L, &y, 2);
        lua_pushboolean(L, a_complex_eq(x, y));
        return 1;
    }
    return 0;
}

/***
 complex number x is not equal to complex number y
 @tparam a.complex x complex number on the left
 @tparam a.complex y complex number on the right
 @treturn bool result of comparison
 @function ne
*/
int liba_complex_ne(lua_State *L)
{
    if (lua_gettop(L) >= 2)
    {
        a_complex x, y;
        liba_complex_from(L, &x, 1);
        liba_complex_from(L, &y, 2);
        lua_pushboolean(L, a_complex_ne(x, y));
        return 1;
    }
    return 0;
}

#undef F1
#define F1(func)                                   \
    int liba_complex_##func(lua_State *L)          \
    {                                              \
        if (lua_gettop(L) >= 1)                    \
        {                                          \
            a_complex z;                           \
            liba_complex_from(L, &z, 1);           \
            z.real = a_complex_##func(z);          \
            lua_pushnumber(L, (lua_Number)z.real); \
            return 1;                              \
        }                                          \
        return 0;                                  \
    }
/***
 computes the natural logarithm of magnitude of a complex number
 @tparam a.complex z complex number userdata
 @treturn number log|z|
 @function logabs
*/
F1(logabs)
/***
 computes the squared magnitude of a complex number
 @tparam a.complex z complex number userdata
 @treturn number a^2+b^2
 @function abs2
*/
F1(abs2)
/***
 computes the magnitude of a complex number
 @tparam a.complex z complex number userdata
 @treturn number sqrt{a^2+b^2}
 @function abs
*/
F1(abs)
/***
 computes the phase angle of a complex number
 @tparam a.complex z complex number userdata
 @treturn number arctan(b/a)
 @function arg
*/
F1(arg)
#undef F1
#define F1(func)                          \
    int liba_complex_##func(lua_State *L) \
    {                                     \
        if (lua_gettop(L) >= 1)           \
        {                                 \
            a_complex z, *ctx;            \
            ctx = liba_complex_new_(L);   \
            liba_complex_from(L, &z, 1);  \
            a_complex_##func(ctx, z);     \
            return 1;                     \
        }                                 \
        return 0;                         \
    }
/***
 computes the projection on Riemann sphere
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function proj
*/
F1(proj)
/***
 computes the complex conjugate
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function conj
*/
F1(conj)
/***
 computes the complex negative
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function unm
*/
F1(neg)
/***
 inverse of a complex number
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function inv
*/
F1(inv)
#undef F1
#define F1(func)                          \
    int liba_complex_##func(lua_State *L) \
    {                                     \
        if (lua_gettop(L) >= 2)           \
        {                                 \
            a_complex x, y, *ctx;         \
            ctx = liba_complex_new_(L);   \
            liba_complex_from(L, &x, 1);  \
            liba_complex_from(L, &y, 2);  \
            a_complex_##func(ctx, x, y);  \
            return 1;                     \
        }                                 \
        return 0;                         \
    }
#undef F2
#define F2(func)                                            \
    int liba_complex_##func(lua_State *L)                   \
    {                                                       \
        if (lua_gettop(L) >= 2)                             \
        {                                                   \
            a_complex x, y, *ctx = liba_complex_new_(L);    \
            liba_complex_from(L, &x, 1);                    \
            if (liba_complex_from(L, &y, 2) == LUA_TNUMBER) \
            {                                               \
                a_complex_##func##_real(ctx, x, y.real);    \
            }                                               \
            else { a_complex_##func(ctx, x, y); }           \
            return 1;                                       \
        }                                                   \
        return 0;                                           \
    }
/***
 addition of complex numbers
 @tparam a.complex x complex number userdata
 @tparam a.complex y complex number userdata
 @treturn a.complex complex number userdata
 @function add
*/
F2(add)
/***
 subtraction of complex numbers
 @tparam a.complex x complex number userdata
 @tparam a.complex y complex number userdata
 @treturn a.complex complex number userdata
 @function sub
*/
F2(sub)
/***
 multiplication of complex numbers
 @tparam a.complex x complex number userdata
 @tparam a.complex y complex number userdata
 @treturn a.complex complex number userdata
 @function mul
*/
F2(mul)
/***
 division of complex numbers
 @tparam a.complex x complex number userdata
 @tparam a.complex y complex number userdata
 @treturn a.complex complex number userdata
 @function div
*/
F2(div)
/***
 complex number z raised to complex power a
 @tparam a.complex z complex number userdata
 @tparam a.complex a complex number userdata
 @treturn a.complex complex number userdata
 @function pow
*/
F1(pow)
/***
 computes the complex base-b logarithm
 @tparam a.complex z complex number userdata
 @tparam a.complex b complex number userdata
 @treturn a.complex complex number userdata
 @function logb
*/
F1(logb)
#undef F1
#define F1(func)                          \
    int liba_complex_##func(lua_State *L) \
    {                                     \
        if (lua_gettop(L) >= 1)           \
        {                                 \
            a_complex z, *ctx;            \
            ctx = liba_complex_new_(L);   \
            liba_complex_from(L, &z, 1);  \
            a_complex_##func(ctx, z);     \
            return 1;                     \
        }                                 \
        return 0;                         \
    }
#undef F2
#define F2(func)                                      \
    int liba_complex_##func(lua_State *L)             \
    {                                                 \
        if (lua_gettop(L) >= 1)                       \
        {                                             \
            a_complex z, *ctx = liba_complex_new_(L); \
            int type = liba_complex_from(L, &z, 1);   \
            if (type == LUA_TNUMBER)                  \
            {                                         \
                a_complex_##func##_real(ctx, z.real); \
            }                                         \
            else { a_complex_##func(ctx, z); }        \
            return 1;                                 \
        }                                             \
        return 0;                                     \
    }
/***
 computes the complex base-e exponential
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function exp
*/
F1(exp)
/***
 computes the complex natural logarithm
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function log
*/
F1(log)
/***
 computes the complex square root
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function sqrt
*/
F2(sqrt)
/***
 computes the complex base-2 logarithm
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function log2
*/
F1(log2)
/***
 computes the complex base-10 logarithm
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function log10
*/
F1(log10)
/***
 computes the complex sine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function sin
*/
F1(sin)
/***
 computes the complex cosine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function cos
*/
F1(cos)
/***
 computes the complex tangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function tan
*/
F1(tan)
/***
 computes the complex secant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function sec
*/
F1(sec)
/***
 computes the complex cosecant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function csc
*/
F1(csc)
/***
 computes the complex cotangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function cot
*/
F1(cot)
/***
 computes the complex arc sine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function asin
*/
F2(asin)
/***
 computes the complex arc cosine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acos
*/
F2(acos)
/***
 computes the complex arc tangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function atan
*/
F1(atan)
/***
 computes the complex arc secant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function asec
*/
F2(asec)
/***
 computes the complex arc cosecant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acsc
*/
F2(acsc)
/***
 computes the complex arc cotangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acot
*/
F1(acot)
/***
 computes the complex hyperbolic sine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function sinh
*/
F1(sinh)
/***
 computes the complex hyperbolic cosine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function cosh
*/
F1(cosh)
/***
 computes the complex hyperbolic tangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function tanh
*/
F1(tanh)
/***
 computes the complex hyperbolic secant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function sech
*/
F1(sech)
/***
 computes the complex hyperbolic cosecant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function csch
*/
F1(csch)
/***
 computes the complex hyperbolic cotangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function coth
*/
F1(coth)
/***
 computes the complex arc hyperbolic sine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function asinh
*/
F1(asinh)
/***
 computes the complex arc hyperbolic cosine
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acosh
*/
F2(acosh)
/***
 computes the complex arc hyperbolic tangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function atanh
*/
F2(atanh)
/***
 computes the complex arc hyperbolic secant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function asech
*/
F1(asech)
/***
 computes the complex arc hyperbolic cosecant
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acsch
*/
F1(acsch)
/***
 computes the complex arc hyperbolic cotangent
 @tparam a.complex z complex number userdata
 @treturn a.complex complex number userdata
 @function acoth
*/
F1(acoth)

static int liba_complex_set(lua_State *L)
{
    a_complex *const ctx = (a_complex *)lua_touserdata(L, 1);
    switch (a_hash_bkdr(lua_tostring(L, 2), 0))
    {
    case 0x0F6133A2: // real
        ctx->real = (a_float)luaL_checknumber(L, 3);
        break;
    case 0x0E2E9172: // imag
        ctx->imag = (a_float)luaL_checknumber(L, 3);
        break;
    case 0x001E0FA9: // rho
    {
        a_float const rho = (a_float)luaL_checknumber(L, 3);
        a_float const theta = a_complex_arg(*ctx);
        a_complex_polar(ctx, rho, theta);
        break;
    }
    case 0x0240D1F6: // theta
    {
        a_float const theta = (a_float)luaL_checknumber(L, 3);
        a_float const rho = a_complex_abs(*ctx);
        a_complex_polar(ctx, rho, theta);
        break;
    }
    case 0x0CD3E0FC: // __eq
    case 0x906DF07D: // __len
    case 0x90705068: // __unm
    case 0x906B0E8D: // __add
    case 0x906FCDE0: // __sub
    case 0x906E3BB4: // __mul
    case 0x906BDA49: // __div
    case 0x906F01C8: // __pow
    case 0xE8859EEB: // __name
    case 0xA65758B2: // __index
    case 0xAEB551C6: // __newindex
    case 0x5DA21A54: // __tostring
        break;
    default:
        lua_getmetatable(L, 1);
        lua_replace(L, 1);
        lua_rawset(L, 1);
    }
    return 0;
}

static int liba_complex_get(lua_State *L)
{
    a_complex const *const ctx = (a_complex const *)lua_touserdata(L, 1);
    switch (a_hash_bkdr(lua_tostring(L, 2), 0))
    {
    case 0x0F6133A2: // real
        lua_pushnumber(L, (lua_Number)ctx->real);
        break;
    case 0x0E2E9172: // imag
        lua_pushnumber(L, (lua_Number)ctx->imag);
        break;
    case 0x001E0FA9: // rho
        lua_pushnumber(L, (lua_Number)a_complex_abs(*ctx));
        break;
    case 0x0240D1F6: // theta
        lua_pushnumber(L, (lua_Number)a_complex_arg(*ctx));
        break;
    case 0xA65758B2: // __index
        lua_registry_get(L, liba_complex_new);
        lua_num_set(L, -1, "real", ctx->real);
        lua_num_set(L, -1, "imag", ctx->imag);
        lua_num_set(L, -1, "rho", a_complex_abs(*ctx));
        lua_num_set(L, -1, "theta", a_complex_arg(*ctx));
        break;
    default:
        lua_getmetatable(L, 1);
        lua_replace(L, 1);
        lua_rawget(L, 1);
    }
    return 1;
}

static int liba_complex_(lua_State *L)
{
    lua_pushcfunction(L, liba_complex_new);
    lua_replace(L, 1);
    lua_call(L, lua_gettop(L) - 1, 1);
    return 1;
}

int luaopen_liba_complex(lua_State *L)
{
    static lua_fun const funcs[] = {
        {"new", liba_complex_new},
        {"rect", liba_complex_rect},
        {"polar", liba_complex_polar},
        {"eq", liba_complex_eq},
        {"ne", liba_complex_ne},
        {"logabs", liba_complex_logabs},
        {"proj", liba_complex_proj},
        {"conj", liba_complex_conj},
        {"abs2", liba_complex_abs2},
        {"abs", liba_complex_abs},
        {"arg", liba_complex_arg},
        {"unm", liba_complex_neg},
        {"add", liba_complex_add},
        {"sub", liba_complex_sub},
        {"mul", liba_complex_mul},
        {"div", liba_complex_div},
        {"inv", liba_complex_inv},
        {"pow", liba_complex_pow},
        {"exp", liba_complex_exp},
        {"log", liba_complex_log},
        {"log2", liba_complex_log2},
        {"log10", liba_complex_log10},
        {"logb", liba_complex_logb},
        {"sqrt", liba_complex_sqrt},
        {"sin", liba_complex_sin},
        {"cos", liba_complex_cos},
        {"tan", liba_complex_tan},
        {"sec", liba_complex_sec},
        {"csc", liba_complex_csc},
        {"cot", liba_complex_cot},
        {"asin", liba_complex_asin},
        {"acos", liba_complex_acos},
        {"atan", liba_complex_atan},
        {"asec", liba_complex_asec},
        {"acsc", liba_complex_acsc},
        {"acot", liba_complex_acot},
        {"sinh", liba_complex_sinh},
        {"cosh", liba_complex_cosh},
        {"tanh", liba_complex_tanh},
        {"sech", liba_complex_sech},
        {"csch", liba_complex_csch},
        {"coth", liba_complex_coth},
        {"asinh", liba_complex_asinh},
        {"acosh", liba_complex_acosh},
        {"atanh", liba_complex_atanh},
        {"asech", liba_complex_asech},
        {"acsch", liba_complex_acsch},
        {"acoth", liba_complex_acoth},
    };
    lua_createtable(L, 0, A_LEN(funcs));
    lua_fun_reg(L, -1, funcs, A_LEN(funcs));
    lua_createtable(L, 0, 1);
    lua_fun_set(L, -1, "__call", liba_complex_);
    lua_setmetatable(L, -2);

    static lua_fun const metas[] = {
        {"__tostring", liba_complex_tostring},
        {"__newindex", liba_complex_set},
        {"__index", liba_complex_get},
        {"__len", liba_complex_abs},
        {"__unm", liba_complex_neg},
        {"__add", liba_complex_add},
        {"__sub", liba_complex_sub},
        {"__mul", liba_complex_mul},
        {"__div", liba_complex_div},
        {"__pow", liba_complex_pow},
        {"__eq", liba_complex_eq},
    };
    lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
    lua_fun_reg(L, -1, metas, A_LEN(metas));
    lua_fun_reg(L, -1, funcs, A_LEN(funcs));
    lua_str_set(L, -1, "__name", "a.complex");
    lua_registry_set(L, liba_complex_new);

    return 1;
}