-
Notifications
You must be signed in to change notification settings - Fork 1
/
maylib.texi
2127 lines (1747 loc) · 87.8 KB
/
maylib.texi
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\input texinfo @c -*-texinfo-*-
@setfilename may.info
@c Build with makeinfo --html --no-split --number-sections may.texi
@documentencoding ISO-8859-1
@set VERSION 0.7.5
@set UPDATED-MONTH October 2014
@set NAME MAYLIB
@set AUTHORS Patrick P@'elissier
@set MAIL Patrick.Pelissier@@removeme@@gmail.com
@set WEBPAGE https://github.com/P-p-H-d/maylib
@set COPYRIGHT 2007-2019 Patrick P@'elissier
@settitle @value{NAME} @value{VERSION}
@synindex tp fn
@iftex
@afourpaper
@end iftex
@copying
This manual documents how to install and use @value{NAME} Library,
version @value{VERSION}.
Copyright @value{COPYRIGHT}
@end copying
@c html <meta name=description content="...">
@documentdescription
How to install and use @value{NAME} Library, version @value{VERSION}.
@end documentdescription
@c smallbook
@finalout
@setchapternewpage on
@ifnottex
@node Top, Introduction, (dir), (dir)
@top @value{NAME}
@end ifnottex
@c If TEX (Dvi, pdf, ...)
@iftex
@titlepage
@title @value{NAME}
@subtitle A symbolic number evaluator library.
@subtitle Edition @value{VERSION}
@subtitle @value{UPDATED-MONTH}
@author @value{AUTHORS}
@email{@value{MAIL}}
@tex
\global\parindent=0pt
\global\parskip=8pt
\global\baselineskip=13pt
@end tex
@page
@vskip 0pt plus 1filll
@end titlepage
@headings double
@end iftex
@ifnottex
@sp 1
@insertcopying
@end ifnottex
@c Don't bother with contents for html, the menus seem adequate.
@ifnothtml
@contents
@end ifnothtml
@menu
* Introduction::
* Copying::
* Installing::
* Reporting Bugs::
* Basics::
* Example::
* Interface::
* Contributors::
* Concept Index::
* Function Index::
@end menu
@node Introduction, Copying, Top, Top
@comment node-name, next, previous, up
@unnumbered Introduction to the @value{NAME} Library
@unnumberedsec Description
@value{NAME} is a C library for symbolic mathematical calculations.
It is not a CAS (Computer Algebra System): it doesn't have any
internal programming language and it is very limited in
functionalities. It doesn't support global variables, neither global
functions: this is let to the user of the library.
@value{NAME} is a tool for programmer, which means that he must be able
to understand completely and fully what the tool can do and can not. This
implies that @value{NAME} specifications should be precise and exhaustive.
@value{NAME} is not designed to be efficient on pure numerical computations.
If you plan to do heavy computations involving integer polynomial or
float matrix (for example), you should use another tool or library,
and/or write the code yourself.
@value{NAME} should be portable to any systems that support GCC
(or a port of GCC) with the following assumptions :
@itemize
@item A @code{void (*)()} must be castable to a @code{void *} and vice-versa: we don't lose the value by applying the casting.
@item We can't have the address of a function which is inside an array of char.
@item @code{int *p; memset(&p, 0, sizeof p);} set p to NULL.
@end itemize
This is the case for the majority of the computers.
It should work fine on 16, 32 or 64 bits systems (Windows, Linux, *BSD).
Even if it only supports GCC, a port to a C99 compiler (not a C++ compiler but a real C99 compiler) should be quite easy: just replace GCC's extensions in the internal header file.
@unnumberedsec Features
@itemize @bullet
@item Arbitrary precision integer and rational through GMP.
@item Arbitrary precision float through MPFR.
@item Automatic and user controlled simplification.
@item Expansion and differentation of expressions.
@item Substitutions and pattern matching.
@item Calculations with symbolic and numeric matrices.
@item Default atomic types: integer, rational, float, complex, identifier and undefined data.
@item Extension through user defined types and user defined functions.
@end itemize
It doesn't do:
@itemize
@item Support of a tuning complete language.
@item Limits, series and integrations.
@item Factorization of polynomials.
@item Solving a set of polynomial equations and even solving.
@item Much more.
@end itemize
Computation on floating point numbers returns always the same output for a
given sequence of operation, regardless of the architecture or the system
(Thanks to MPFR).
@unnumberedsec How to use this Manual
Everyone should read @ref{Basics}. If you need to install the library
yourself, you need to read @ref{Installing}, too.
The rest of the manual can be used for later reference, although it is
probably a good idea to glance through it.
@node Copying, Installing, Introduction, Top
@comment node-name, next, previous, up
@unnumbered Copying Conditions
@cindex Copying conditions
@cindex Conditions for copying
@value{NAME} is copyrighted @value{COPYRIGHT}
This library is @dfn{free}; this means that everyone is free to use it and
free to redistribute it on a free basis. The library is not in the public
domain; it is copyrighted and there are restrictions on its distribution, but
these restrictions are designed to permit everything that a good cooperating
citizen would want to do. What is not allowed is to try to prevent others
from further sharing any version of this library that they might get from
you.@refill
Specifically, we want to make sure that you have the right to give away copies
of the library, that you receive source code or else can get it if you want
it, that you can change this library or use pieces of it in new free programs,
and that you know you can do these things.@refill
To make sure that everyone has such rights, we have to forbid you to deprive
anyone else of these rights. For example, if you distribute copies of the
@value{NAME} library, you must give the recipients all the rights that you have. You
must make sure that they, too, receive or can get the source code. And you
must tell them their rights.@refill
Also, for our own protection, we must make certain that everyone finds out
that there is no warranty for the @value{NAME} library. If it is modified by
someone else and passed on, we want their recipients to know that what they
have is not what we distributed, so that any problems introduced by others
will not reflect on our reputation.@refill
The precise conditions of the license for the @value{NAME} library are found in the
Lesser General Public License that accompanies the source code.
See the file COPYING.LESSER.txt.@refill
@node Installing, Reporting Bugs,Copying, Top
@comment node-name, next, previous, up
@chapter Installing the @value{NAME} Library
@cindex Installing
@section How to install
Here are the steps needed to install the library on Unix systems:
@enumerate
@item
To build @value{NAME}, you first have to install GNU MP
(version 4.2 or higher) on your computer and MPFR (version 2.1.0 or higher).
You need @samp{GCC} (or a compatible compiler) and a @samp{make} program.
You can get GMP at @url{http://www.gmplib.org/}, MPFR
at @url{http://www.mpfr.org/} and GCC at @url{http://gcc.gnu.org/}.
Please see their respective documentation to see how to install them.
@item
In the @value{NAME} source directory, type:
@samp{make}
Or if you have not installed GMP and/or MPFR in another directory, type:
@samp{make GMP=$GMP_DIR MPFR=$MPFR_DIR}
This will compile @value{NAME}, and create a library archive file @file{libmay.a}.
By default, @value{NAME} is built with the assertions turned on,
which slows down a lot the library (a factor of 3), but is a lot safer.
To have an optimized version, type:
@samp{make CFLAGS="-O2 -fomit-frame-pointer" GMP=$GMP_DIR MPFR=$MPFR_DIR}
or
@samp{make fast [GMP=...]}
@item
@samp{make check [GMP=...]}
This will make sure the built is correct. If you get error messages, please
report them (@xref{Reporting Bugs}.).
@item
@samp{make install}
This will copy the file @file{may.h} to the directory
@file{/usr/local/include}, the file @file{libmay.a} to the directory
@file{/usr/local/lib}, and the file @file{may.info} to the directory
@file{/usr/local/info}.
Note that it is not recommended to install them in /usr/local.
Type:
@samp{make install PREFIX=$PREFIX}
to install them in another directory.
@end enumerate
@section Known Build Problems
@value{NAME} suffers from all bugs from GMP and MPFR, plus many, many more.
Please report other problems. @xref{Reporting Bugs}.
@section Getting the Latest Version
The latest version of @value{NAME} is available from @url{@value{WEB-PAGE}}.
@section Known Problems
You may get a segmentation fault using @value{NAME}. This is mainly because
the stack size is too small. Uses:
@itemize
@item under BASH/ZSH: ulimit -s unlimited
@item under TCSH: unlimited
@end itemize
@node Reporting Bugs, Basics, Installing, Top
@comment node-name, next, previous, up
@chapter Reporting Bugs
@cindex Reporting bugs
If you think you have found a bug in the @value{NAME} Library,
first have a look on @url{@value{WEB-PAGE}}: perhaps this bug
is already known, in which case you may find there a workaround for it.
Otherwise, please investigate
and report it. We have made this library available to you, and it is not to ask
too much from you, to ask you to report the bugs that you find.
There are a few things you should think about when you put your bug report
together.
You have to send us a test case that makes it possible for us to reproduce the
bug. Include instructions on how to run the test case.
You also have to explain what is wrong; if you get a crash, or if the results
printed are incorrect and in that case, in what way.
Please include compiler version information in your bug report.
If your bug report is good, we will do our best to help you to get a corrected
version of the library; if the bug report is poor, we won't do anything about
it (aside of chiding you to send better bug reports).
Send your bug report to: @samp{@value{MAIL}}.
If you think something in this manual is unclear, or downright incorrect, or if
the language needs to be improved, please send a note to the same address.
@node Basics, Example, Reporting Bugs, Top
@comment node-name, next, previous, up
@chapter Basics
@cindex Basics
@cindex @file{may.h}
All declarations needed to use @value{NAME} are collected in the include file
@file{may.h}. It is designed to work with both C and C++ compilers.
You should include that file in any program using the library:
@verbatim
#include <may.h>
@end verbatim
It is recommended that you read the basic section of GMP and MPFR manuals
before continuing reading this one.
@section Nomenclature and Types
@subsection Symbolic number: may_t
@cindex Symbolic number
@tindex @code{may_t}
@noindent
A @dfn{symbolic number} is a symbolic expression (an integer, a rationnal,
a float, an identifier, a sum of such objets, a product, a power, ...).
The C data type for such objects is @code{may_t}.
It is a pointer to an hidden and undocumented C struct.
It can be seen as a tree with nodes (sum, product, functions)
and leafs (Integer, Rationnal, float, identifier, complex).
Don't mix the C variable names, and the identifiers: they are completly
independent.
@subsection Pair of symbolic number: may_pair_t
@tindex @code{may_pair_t}
@code{may_pair_t} is a pair of two @dfn{symbolic numbers}.
The first element is the field @code{first}.
The second element is the field @code{second}.
@subsection Domain of symbolic number: may_domain_e
@tindex @code{may_domain_e}
A domain is a part of the complex plane.
The C data type for such objects is @code{may_domain_e}.
The available parts are:
@itemize
@item MAY_COMPLEX_D: The complex plane.
@item MAY_NONZERO_D: The complex plane without zero.
@item MAY_NONREPOS_D: All complex such that @code{Re(x)<=0}
@item MAY_NONRENEG_D: All complex such that @code{Re(x)>=0}
@item MAY_NONIMPOS_D: All complex such that @code{Im(x)<=0}
@item MAY_NONIMNEG_D: All complex such that @code{Im(x)>=0}
@item MAY_CINTEGER_D: All complex integers
@item MAY_CRATIONAL_D: All complex rationals.
@item MAY_EVEN_D: All even complex integers.
@item MAY_ODD_D: All odd complex integers.
@item MAY_OUT_UNIT_CIRCLE_D: All complex such that @code{abs(x) >= 1}
@item MAY_IN_UNIT_CIRCLE_D: All complex such that @code{abs(x) <= 1}
@end itemize
You can create new parts by intersecting two parts (or more) of this list:
for example, the reals are the part defined by @code{MAY_NONIMPOS_D|MAY_NONIMNEG_D}
- you have to use the symbol @code{|} for the intersection.
Other parts of the complex plane are already defined:
@itemize
@item MAY_REAL_D: The reals
@item MAY_REAL_NONPOS_D: The reals <= 0
@item MAY_REAL_NONNEG_D: The reals >= 0
@item MAY_REAL_POS_D: The reals > 0
@item MAY_REAL_NEG_D: The reals < 0
@item MAY_INTEGER_D: The integers
@item MAY_INT_NONPOS_D: The integers <= 0
@item MAY_INT_NONNEG_D: The integers >= 0
@item MAY_INT_POS_D: The integers > 0
@item MAY_INT_NEG_D: The integers < 0
@item MAY_INT_EVEN_D: The even integers.
@item MAY_INT_ODD_D: The odd integers.
@item MAY_RATIONAL_D: The rationals.
@end itemize
@section The memory model
@cindex Memory model
Contrary to the classical memory model (the @code{malloc}
and @code{free} functions and friends) where you explicitely
say to the memory handler which areas you don't want to use anymore,
you have to say to @value{NAME} which @dfn{symbolic number} you want to
keep after a calculus.
It is harder than using a true garbage collector, but a lot
simpler than using the classical memory model. It seems to be
also very efficient.
When you start a new calculus, you put a mark (using
@code{may_mark}). Then, you create a new symbolic expression.
You evaluate it, and keep the evaluated result (using
@code{may_keep}): all the intermediary calculus created after
putting the mark are deleted except the kept expression.
The main philosophy of @value{NAME} is copy-on-write: a
@dfn{symbolic number} may be referenced by many other
@dfn{symbolic numbers}. There is no way to known how many
references there are (there is no reference counter or other
tricks like this). So when you want to modify it, you have to
create a new expression. This philosophy is usually quite good.
But in some cases, it may be a nightmare: for example, to create
accumulators or arithmetics on polynomial. So inside @value{NAME} itself,
there may be some exceptions but they are locals, and not visible
from the outside.
@section The Stack
@cindex Stack
Like many other programs which work with mathematical expressions,
@value{NAME} also works internally with a stack. But this stack is
completly hidden. Putting a mark means saving the stack pointer
value. Allocating memory is only updating the stack pointer.
Keeping a @dfn{symbolic number} means garbaging the stack and
moving it at the saved position of the stack.
The GMP Custom Allocation functions are set to use the stack instead of the
standard @code{malloc} functions. This may cause problems with other
libraries which also redirect the GMP Custom Allocation for their own
usage. MPFR caches are freed when you
garbesh the stack.
@section Evaluated form
@cindex Constructor functions
@cindex Evaluated form
@cindex Unevaluated form
Usually, you construct an expression by using @dfn{constructor functions}:
@code{may_add_c}, @code{may_sub_c}, etc. They return the expression in
an @dfn{unevaluated form}: they are not simplified, neither in their internal
canonic form. The function which transforms them in @dfn{evaluated form}
is @code{may_eval}.
During the conversion, various simplifications are performed as well.
This function, contrary to all other, might not copy the expression while
changing its form (for efficiency reasons).
All functions which endend with @code{_c} are @dfn{constructor functions}:
they accept and return @dfn{unevaluated form}. All other functions, and
except if documented otherwise, accept only and return only
@dfn{evaluated form}.
@dfn{Constructor functions} may or may not corrupt the input
@dfn{symbolic number} to construct the output : this liberty is let
to the function for efficiency reasons since copying an image
of the input may be much more expensive than performing the construction
itself. Nevertheless, the default behavior is not to corrupt the input.
If the function constructs the output by updating the input,
it must explicitely say it. An evaluated symbolic number is never corrupted,
except on garbaging.
@section Boolean
@value{NAME} doesn't support boolean arithmetic natively (ie. without
an extension). In this documentation, TRUE is assumed to be a non zero
integer (of type int), and FALSE the integer zero.
@node Example, Interface, Basics, Top
@comment node-name, next, previous, up
@chapter An example of use of the @value{NAME} Library
@cindex Example
Before using @value{NAME}, we need to initialize the library by calling
@code{may_kernel_start}. Before quitting the program, it may be a good
idea to call @code{may_kernel_end} to free the used memory.
This example initializes the library, parses an expression, expands it,
and finally displays the results.
@example
@{
may_t x, y;
may_kernel_start (3000000, 0);
x = may_parse_str ("(x+y)*(x-y)");
y = may_expand (x);
printf ("x=%s y=%s\n", may_get_string (NULL, 0, x),
may_get_string (NULL, 0, y));
may_kernel_end ();
@}
@end example
The first line creates two uninitialized C variables @code{x} and @code{y}.
The second line initializes the @value{NAME} library.
The third line parses the string @code{"(x+y)*(x-y)"}, returns
the evaluated @dfn{symbolic number} and saves it in @code{x}.
Then it expands @code{x} and stores the expanded result in @code{y}
(in its evaluated form).
Then it displays the value of @code{x} and @code{y} by converting them
to strings before printing them.
Finally, it quits the library.
@node Interface, Contributors, Example, Top
@comment node-name, next, previous, up
@chapter Interface
@cindex The Interface of the @value{NAME} Library
@section Kernel functions
@deftypefun void may_kernel_start (size_t @var{stackSize}, int allow)
Initialize the @value{NAME} library by allocating a stack of initial
size at least @var{stackSize} bytes. The stack is expanded automatically if
needed (On supported systems) if @var{allow} is not zero.
It sets the GMP memory functions to use this stack after saving them.
You must call this function before any-other call to @value{NAME} functions,
otherwise you WILL crash your program.
You can only start the @value{NAME} library once, which means that you can't
call @code{may_kernel_start} more than once: if you call this function
twice without calling @code{may_kernel_end} between, the behavior
is undefined.
@end deftypefun
@deftypefun void may_kernel_end (void)
End the @value{NAME} library by freeing the allocated stuff.
Calling any other @value{NAME} functions except @code{may_kernel_start}
after this function is undefined behaviour.
@end deftypefun
@deftypefun void may_kernel_stop (void)
Stop the @value{NAME} Kernel.
Calling any other @value{NAME} functions except @code{may_kernel_restart}
after this function is undefined behaviour.
It restores the original GMP memory functions.
It is designed so that @value{NAME} can be used with other libraries which
also change the GMP memory functions: it stops @value{NAME} so that you can
use them.
It doesn't destroy any @value{NAME} globals or state but simply pause @value{NAME}.
@end deftypefun
@deftypefun void may_kernel_restart (void)
Restart the @value{NAME} library by saving the current GMP memory functions
and restoring the @value{NAME} ones. You can use once again all the @value{NAME}
functions.
@end deftypefun
@deftypefun mp_rnd_t may_kernel_rnd (mp_rnd_t @var{rnd})
Set the current rounding mode used by the MPFR functions.
Return the previous used rounding mode.
The default is @code{GMP_RNDN}. It doesn't interfere with
@code{mpfr_set_default_rnd_mode}.
@end deftypefun
@c @deftypefun int may_kernel_base (int @var{base})
@c Set the current base used for input/ouput.
@c Return the previous used base. The default is 10.
@c NOTE: Not really supported actually.
@c @end deftypefun
@deftypefun mp_prec_t may_kernel_prec (mp_prec_t @var{prec})
Set the current precision used for new MPFR floats.
Return the previous used precision. The default is 113 (It may changed).
Previous computed floats aren't expanded to this new precision.
@end deftypefun
@deftypefun may_domain_e may_kernel_domain (may_domain_e @var{domain})
Set the current domain used for new anonymous identifier.
Return the previous used domain. The default is @code{@value{NAME}_COMPLEX_D}
. Previous anonymous identifiers aren't set to this new domain.
@end deftypefun
@deftypefun may_t may_kernel_intmod (may_t @var{n})
Set the used integer for all modulo operations, ie it defines the integer
arithmetics on Z (if @var{n}=NULL or @var{n}=0) or Z/@var{n}Z (otherwise).
If @var{n} disapears after a heap compact, this integer is reseted
to NULL. However, you should not depend on this behavior.
It assumes n is an integer or NULL. It returns the previous used integer.
@end deftypefun
@deftypefun {unsigned long} may_kernel_intmaxsize (unsigned long @var{n})
Set the maximum size in bit for the size of the results of operation
involving integers (typically power and factorial). If the result is estimated
to be bigger than the given @var{n} number of bits, it won't compute it.
It returns the previous used number of bits.
@end deftypefun
@c int (*may_kernel_sort_cb (int (*n)(may_t, may_t)))(may_t, may_t);
@c int (*may_kernel_zero_cb (int (*n)(may_t)))(may_t);
@deftypefun int may_kernel_num_presimplify (int @var{flag})
When you parse strings, the float numbers may be evaluated to
the current precision if @var{flag} is set, or they may be stored as
float-string, without evaluating them with the current precision: we
delay the evaluation to a later step where we know the needed precision.
Set the current value of this flag.
Return the previous used flag. The default is @code{1}, e.g. we
evaluate the float at parsing time.
@end deftypefun
@deftypefun void may_kernel_info (FILE *@var{stream}, const char *@var{str})
Display various kernel information inside the stream @var{stream} using
the string @var{str}.
@end deftypefun
Any change in the global settings by using the may_kernel_XXXX functions has as
consequence that you have to reevaluate all the previously computed variables
(using @code{may_reeval}) so that theses variables are computed using this
new set of global settings.
@section Memory functions
@deftypefun {void *} may_alloc (size_t @var{size})
Allocate @var{size} bytes and return a pointer to the allocated
memory. The memory is not cleared.
The value returned is a pointer to the allocated memory,
which is suitably aligned for a @code{long}, or
@code{NULL} if the request fails.
@end deftypefun
@deftypefun {void *} may_realloc (void *@var{ptr}, size_t @var{oldsize}, size_t @var{newsize})
Change the size of the memory block pointed to by @var{ptr}
whose size is @var{oldsize} bytes to @var{newsize} bytes.
The contents will be unchanged to the minimum of the old
and new sizes; newly allocated memory will be uninitialized.
@var{ptr} can not be @code{NULL} and @var{newsize} can not be @code{0},
otherwise the behavior is undefined.
@var{ptr} must have been returned by an earlier call to
@code{may_alloc} or @code{may_realloc}.
The value returned is a pointer to the allocated memory,
which is suitably aligned for a @code{long}, or
@code{NULL} if the request fails.
@end deftypefun
@deftypefun void may_free (void *@var{ptr}, size_t @var{oldsize})
Tries to free the memory space pointed to by @var{ptr}, which must have been
returned by a previous call to @code{may_alloc()} or @code{may_realloc()}.
@var{oldsize} must be the size in bytes of the allocated memory. It is needed
since it is not stored internally.
Otherwise, or if @code{may_free(ptr)} has already been called before,
or if @var{oldsize} is not equal to the size passed to @code{may_alloc}
or @code{may_realloc},
undefined behavior occurs. If @var{ptr} is NULL, no operation is performed.
Note 1: This function tries to free the memory, but it may fail. The only way
to be sure the memory is cleaned is to call @code{may_compact} or friends.
Note 2: To help this function to succeed, you have to call @code{may_free}
in the reverse order of the call to @code{may_alloc}, just like some push/pop
functions.
@end deftypefun
@deftypefun void may_mark ([may_mark_t @var{mark}])
Push a mark and save it in @var{mark}: all the current state of the memory is saved.
On C99 or GNU C compilers, the parameter @var{mark} is optionnal and may be ommited:
a dummy mark will be created at the level of the function call.
@end deftypefun
@deftypefun may_t may_compact ([may_mark_t @var{mark},]may_t @var{x})
Compact the @dfn{symbolic number} @var{x} from the
mark: all the created variables between
the referenced mark @var{mark} and the call to this function are lost, except @var{x}
which is garbaged. @var{x} may be NULL, in which case it clears
all the @dfn{symbolic numbers} created during the referenced mark and
this function call.
Return the new valid pointer to the compacted @dfn{symbolic number}.
@var{x} is not a valid reference to a @dfn{symbolic number} anymore.
You usually use it like this:
@example
may_mark_t mark;
may_mark(mark);
// Perform some computation
// Keep only x
x = may_compact (mark, x);
@end example
On C99 or GNU C compilers, the parameter @var{mark} is optionnal and may be ommited:
the dummy mark created using omitting the parameter in @code{may_mark} is used:
@example
may_mark();
// Perform some computation
// Keep only x
x = may_compact (x);
@end example
@end deftypefun
@deftypefun {may_t *} may_compact_v ([may_mark_t @var{mark},] size_t @var{size}, may_t *@var{tab})
Compact all the @dfn{symbolic number} stored in the array
with @var{size} elements pointed by @var{tab}.
If @var{tab} was allocated using @code{may_alloc}, it returns the updated @var{tab} if @var{tab} has
to be compacted too. It returns @var{tab} otherwise.
On C99 or GNU C compilers, the parameter @var{mark} is optionnal and may be ommited:
the dummy mark created using omitting the parameter in @code{may_mark} is used:
@end deftypefun
@deftypefun void may_compact_va (may_mark_t @var{mark}, may_t *@var{x}, ...)
Compact all the @dfn{symbolic numbers} passed as arguments to the function (as a variable argument list).
The variable argument list is assumed to be composed of the address to the symbolic numbers to compact (@code{may_t*})
It begins from @var{x} and ends when it encounters a null pointer.
A maximum of 20 parameters can be compacted using this function.
The parameter @var{mark} is not optionnal and can not be ommited.
@example
may_t x, y, z;
may_mark_t mark;
may_mark(mark);
// Perform some computation
// Keep only x, y and z
may_compact_va (mark, &x, &y, &z, NULL);
@end example
@end deftypefun
@deftypefun may_t may_keep ([may_mark_t @var{mark},]may_t @var{x})
Perform a similar operation than @code{may_compact} except that it
assumes that the parameter @var{mark} won't be used anymore.
On C99 or GNU C compilers, the parameter @var{mark} is optionnal and may be ommited:
the dummy mark created using omitting the parameter in @code{may_mark} is used.
@example
may_mark();
// Perform some computation
// Keep and return the expanded result
return may_keep (may_expand (x));
@end example
@end deftypefun
@section Setting functions
@deftypefun may_t may_set_si (long @var{x})
@deftypefunx may_t may_set_ui (unsigned long @var{x})
@deftypefunx may_t may_set_z (mpz_t @var{x})
@deftypefunx may_t may_set_q (mpq_t @var{x})
@deftypefunx may_t may_set_d (double @var{x})
@deftypefunx may_t may_set_ld (long double @var{x})
@deftypefunx may_t may_set_fr (mpfr_t @var{x})
Return a newly created @dfn{symbolic number}, setting it to
@var{x}. The internal type is set to INTEGER (@code{may_set_si},
@code{may_set_ui} and @code{may_set_z}), RATIONAL (@code{may_set_q}) and
FLOAT (@code{may_set_d}, @code{may_set_ld} and @code{may_set_fr}).
@end deftypefun
@deftypefun may_t may_set_zz (mpz_t @var{x})
Return a newly created @dfn{symbolic number}, setting it to
@var{x} regardless of the current INTEGER modulo (See @code{may_kernel_intmod}).
The internal type is set to INTEGER.
@end deftypefun
@deftypefun may_t may_set_str (const char *@var{x})
@deftypefunx may_t may_set_str_domain (const char *@var{x}, may_domain_e @var{domain})
Return a new @dfn{symbolic number} of type IDENTIFIER (or STRING or SYMBOL,
it is the same thing),
setting it to @var{x} inside domain @var{domain}.
@code{may_set_str} uses the global domain as defined by
@code{may_kernel_domain}, whereas @code{may_set_str_domain} uses the
given parameter @var{domain}.
If @var{x} is @code{NAN}, @code{INFINITY}, @code{PI} or @code{I},
it doesn't return an IDENTIFIER, but the @dfn{symbolic number}
@code{NAN}, @code{INFINITY}, @code{PI} or @code{I} without taking care
about @var{domain}.
The domain @var{domain} of @var{x} must be consistent for all the creations of
symbols involving the string @var{x},
until a call to @code{may_keep}, otherwise the behaviour is undefined.
@end deftypefun
@deftypefun may_t may_set_si_ui (long @var{num}, unsigned long @var{denom})
Return a newly created @dfn{symbolic number}, setting it to the rational
@math{ @var{num} / @var{denom} }.
@end deftypefun
@deftypefun may_t may_set_cx (may_t @var{re}, may_t @var{im})
Return a newly created @dfn{symbolic number}, setting it to the complex
number @math{@var{re} + @var{im} \times I}.
@end deftypefun
@deftypefun may_t may_parse_str (const char *@var{str})
Return a newly created @dfn{symbolic number}, setting it to the value
of the parsed string @var{str}. It is different from @code{may_set_str}
since this function parsed the string, whereas @code{may_set_str} just
set an identifier.
TODO: Explain valid input, grammar, etc.
@example
x = may_parse_str ("x+y+z(x*y)");
x = may_parse_str ("x^2+y/z(x^y)");
x = may_parse_str ("(x+cos(PI))/f(x*y+3!)");
@end example
@end deftypefun
@section Getting functions
@deftypefun int may_get_ui (unsigned long *@var{val}, may_t @var{x})
@deftypefunx int may_get_si (long *@var{val}, may_t @var{x})
@deftypefunx int may_get_str (const char **@var{val}, may_t @var{x})
@deftypefunx int may_get_d (double *@var{val}, may_t @var{x})
@deftypefunx int may_get_ld (long double *@var{val}, may_t @var{x})
@deftypefunx int may_get_z (mpz_t @var{val}, may_t @var{x})
@deftypefunx int may_get_q (mpq_t @var{val}, may_t @var{x})
@deftypefunx int may_get_fr (mpfr_t @var{val}, may_t @var{x})
Set @code{*@var{val}} to the @dfn{symbolic number} @var{x}.
It may perform some convertions to fit the type needs.
Return 0 if the setting was ok, and there was no convertion.
Return 1 if the setting was ok, but a convertion was done.
Return -1 if the setting was impossible: the internal types mismatch.
Return -2 if the setting was impossible because the C type is too
small to fit the requested value.
@end deftypefun
@deftypefun int may_get_cx (may_t *@var{re}, may_t *@var{im}, may_t @var{x});
Set @code{*@var{re}} to the real part of the @dfn{symbolic number} @var{x}.
Set @code{*@var{im}} to the imaginary part of the @dfn{symbolic number}
@var{x}. @var{x} must be a pure numerical type (INTEGER, RATIONAL, FLOAT or
COMPLEX).
Return 0 if the setting was ok, and there was no convertion.
Return 1 if the setting was ok, but a convertion was done.
Return -1 if the setting was impossible: the internal types mismatch.
@end deftypefun
@deftypefun {char *} may_get_string (char *@var{out}, size_t @var{length}, may_t @var{x})
Transform a @dfn{symbolic number} to a string, parsable by @code{may_parse_str}. See @code{may_parse_str} for the format of the string. The string is:
@itemize
@item either stored at the buffer of size @var{length} bytes, pointed by @var{out}. If the string needs more space than what is available, the string is simply cut to fit inside the buffer.
@item or allocated on the internal stack is if @var{out} is @code{NULL}. In which case, the string is valid until a later call to @code{may_keep} or friends.
@end itemize
In both cases, it returns a pointer to the created string, or @code{NULL} in
case of an error.
@end deftypefun
@section Constructors functions
@deftypefun may_t may_add_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_sub_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_mul_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_div_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_mod_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_pow_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_pow_si_c (may_t @var{x}, long @var{y})
@deftypefunx may_t may_gcd_c (may_t @var{x}, may_t @var{y})
Construct a new @dfn{symbolic number} defined as the sum, difference,
product, quotient, remainder, power or gcd of @var{x} and @var{y}.
@end deftypefun
@deftypefun may_t may_addinc_c (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_mulinc_c (may_t @var{x}, may_t @var{y})
Construct a new @dfn{symbolic number} defined as the sum or product
of @var{x} and @var{y}.
The memory used by @var{x} may be reused to construct the result if @var{x}
is not evaluated, making @var{x} an invalid variable which can't be used anymore.
They are MUCH more efficient than @code{may_add_c} or @code{may_mul_c} if you use
@var{x} as an accumulator like in this example:
@example
for (i = 0; i < 100; i++) @{
may_t tmpresult = my_function (i);
acc = may_addinc_c (acc, tmpresult);
@}
acc = may_eval (acc);
@end example
@end deftypefun
@deftypefun may_t may_neg_c (may_t @var{x})
@deftypefunx may_t may_sqr_c (may_t @var{x})
@deftypefunx may_t may_sqrt_c (may_t @var{x})
@deftypefunx may_t may_exp_c (may_t @var{x})
@deftypefunx may_t may_log_c (may_t @var{x})
@deftypefunx may_t may_abs_c (may_t @var{x})
@deftypefunx may_t may_sin_c (may_t @var{x})
@deftypefunx may_t may_cos_c (may_t @var{x})
@deftypefunx may_t may_tan_c (may_t @var{x})
@deftypefunx may_t may_asin_c (may_t @var{x})
@deftypefunx may_t may_acos_c (may_t @var{x})
@deftypefunx may_t may_atan_c (may_t @var{x})
@deftypefunx may_t may_sinh_c (may_t @var{x})
@deftypefunx may_t may_cosh_c (may_t @var{x})
@deftypefunx may_t may_tanh_c (may_t @var{x})
@deftypefunx may_t may_asinh_c (may_t @var{x})
@deftypefunx may_t may_acosh_c (may_t @var{x})
@deftypefunx may_t may_atanh_c (may_t @var{x})
@deftypefunx may_t may_conj_c (may_t @var{x})
@deftypefunx may_t may_real_c (may_t @var{x})
@deftypefunx may_t may_imag_c (may_t @var{x})
@deftypefunx may_t may_argument_c (may_t @var{x})
@deftypefunx may_t may_sign_c (may_t @var{x})
@deftypefunx may_t may_floor_c (may_t @var{x})
@deftypefunx may_t may_ceil_c (may_t @var{x})
@deftypefunx may_t may_fact_c (may_t @var{x})
@deftypefunx may_t may_gamma_c (may_t @var{x})
Construct a new @dfn{symbolic number} defined as the image of the function
neg, sqr, sqrt, exp, log, abs, sin, cos, tan, asin, acos, atan, sinh,
cosh, tanh, asinh, acosh, atanh, conj, real, imag, argument, sign,
floor, ceil, factorial or gamma at @var{x}. Theses functions are handled
internally by @value{NAME}, except ceil and factorial which are just wrappers.
@end deftypefun
@deftypefun may_t may_func_c (const char *@var{name}, may_t @var{x})
@deftypefunx may_t may_func_domain_c (const char *@var{name}, may_t @var{x}, may_domain_e @var{domain})
Construct a new @dfn{symbolic number} defined as the image of the function
whose name is @var{name} at @var{x}.
The function is assumed to have its image inside either the global domain
as defined by @code{may_kernel_domain} for @code{may_func_c} or @var{domain}
for @code{may_func_domain_c}.
@var{name} may be one of the internal functions of @value{NAME},
in which case the appropriate constructor is called, and @var{domain} is
ignored.
@end deftypefun
@deftypefun may_t may_range_c (may_t @var{l}, may_t @var{r})
Construct a new @dfn{symbolic number} defined as the Real Interval from
@var{l} to @var{r}. @var{l} and @var{r} must be numerical reals, otherwise
the behavior is undefined.
@end deftypefun
@deftypefun may_t may_list_vc (size_t @var{size}, const may_t *@var{tab})
@deftypefunx may_t may_add_vc (size_t @var{size}, const may_t *@var{tab})
@deftypefunx may_t may_mul_vc (size_t @var{size}, const may_t *@var{tab})
Construct a new @dfn{symbolic number} defined as the list, sum or product
of the elements of the array of size @var{size} elements, and pointed by
@var{tab}.
@end deftypefun
@deftypefun may_t may_list_vac (may_t @var{x}, ...)
@deftypefunx may_t may_add_vac (may_t @var{x}, ...)
@deftypefunx may_t may_mul_vac (may_t @var{x}, ...)
Construct a new @dfn{symbolic number} defined as the list, sum or product
of the elements of the given @code{va_list}.
The @code{va_list} is assumed to be composed only of type @code{may_t}.
It begins from @var{x}. It ends when it encounters a null pointer (@code{(may_t) 0}).
@end deftypefun
@deftypefun may_t may_diff_c (may_t @var{f}, may_t @var{vx}, may_t @var{order}, may_t @var{at})
Construct a new @dfn{symbolic number} defined as the differentiate of order @var{order}
of the expression @var{f} by the variable @var{vx}, evaluated at point @var{at}.
@var{vx} must be an identifier, otherwise the behavior is undefined. @var{order} may not
be an integer, but the meaning of the constructed expression is undefined.
@end deftypefun
@section Mathematical functions
@deftypefun may_t may_add (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_sub (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_mul (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_div (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_mod (may_t @var{x}, may_t @var{y})
@deftypefunx may_t may_pow (may_t @var{x}, may_t @var{y})
Return a new @dfn{symbolic number} defined as the evaluated sum, difference,
product, quotient, remainder or power of @var{x} and @var{y}.
@end deftypefun
@deftypefun may_t may_neg (may_t @var{x})
@deftypefunx may_t may_sqr (may_t @var{x})
@deftypefunx may_t may_sqrt (may_t @var{x})
@deftypefunx may_t may_exp (may_t @var{x})
@deftypefunx may_t may_log (may_t @var{x})
@deftypefunx may_t may_abs (may_t @var{x})
@deftypefunx may_t may_sin (may_t @var{x})
@deftypefunx may_t may_cos (may_t @var{x})
@deftypefunx may_t may_tan (may_t @var{x})
@deftypefunx may_t may_asin (may_t @var{x})
@deftypefunx may_t may_acos (may_t @var{x})
@deftypefunx may_t may_atan (may_t @var{x})
@deftypefunx may_t may_sinh (may_t @var{x})
@deftypefunx may_t may_cosh (may_t @var{x})
@deftypefunx may_t may_tanh (may_t @var{x})
@deftypefunx may_t may_asinh (may_t @var{x})
@deftypefunx may_t may_acosh (may_t @var{x})
@deftypefunx may_t may_atanh (may_t @var{x})
@deftypefunx may_t may_conj (may_t @var{x})
@deftypefunx may_t may_real (may_t @var{x})
@deftypefunx may_t may_imag (may_t @var{x})
@deftypefunx may_t may_argument (may_t @var{x})
@deftypefunx may_t may_sign (may_t @var{x})
@deftypefunx may_t may_floor (may_t @var{x})
@deftypefunx may_t may_ceil (may_t @var{x})
@deftypefunx may_t may_fact (may_t @var{x})
@deftypefunx may_t may_gamma (may_t @var{x})
Return a new @dfn{symbolic number} defined as the evaluated image of
the function
neg, sqr, sqrt, exp, log, abs, sin, cos, tan, asin, acos, atan, sinh,
cosh, tanh, asinh, acosh, atanh, conj, real, imag, argument, sign,
floor, ceil, factorial or gamma
at @var{x}.
@end deftypefun
@section Replace functions
@deftypefun may_t may_replace (may_t @var{x}, may_t @var{o}, may_t @var{n})
Traverse the @dfn{symbolic number} @var{x} and replace each references
to @var{o} by @var{n}. @var{o} may be a symbolic number of any type.
It returns the newly created @dfn{symbolic number}.
For nodes with variable number of arguments (sum, product), it won't look
if a sub-expression matches @var{o}, e.g. @math{2+y} won't be replaced
by @math{z} in @math{2+x+y}. Use @code{may_rewrite} instead.