Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563878 views
1
/* mpq expression evaluation
2
3
Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
11
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
16
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
21
22
#include <stdio.h>
23
#include <string.h>
24
#include "gmp.h"
25
#include "expr-impl.h"
26
27
28
/* Change this to "#define TRACE(x) x" to get some traces. */
29
#define TRACE(x)
30
31
32
static void
33
e_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e)
34
{
35
mpz_pow_ui (mpq_numref(r), mpq_numref(b), e);
36
mpz_pow_ui (mpq_denref(r), mpq_denref(b), e);
37
}
38
39
/* Wrapped because mpq_sgn is a macro. */
40
static int
41
e_mpq_sgn (mpq_srcptr x)
42
{
43
return mpq_sgn (x);
44
}
45
46
/* Wrapped because mpq_equal only guarantees a non-zero return, whereas we
47
want 1 or 0 for == and !=. */
48
static int
49
e_mpq_equal (mpq_srcptr x, mpq_srcptr y)
50
{
51
return mpq_equal (x, y) != 0;
52
}
53
static int
54
e_mpq_notequal (mpq_srcptr x, mpq_srcptr y)
55
{
56
return ! mpq_equal (x, y);
57
}
58
59
static void
60
e_mpq_num (mpq_ptr w, mpq_srcptr x)
61
{
62
if (w != x)
63
mpz_set (mpq_numref(w), mpq_numref(x));
64
mpz_set_ui (mpq_denref(w), 1L);
65
}
66
static void
67
e_mpq_den (mpq_ptr w, mpq_srcptr x)
68
{
69
if (w == x)
70
mpz_swap (mpq_numref(w), mpq_denref(w));
71
else
72
mpz_set (mpq_numref(w), mpq_denref(x));
73
mpz_set_ui (mpq_denref(w), 1L);
74
}
75
76
77
static __gmp_const struct mpexpr_operator_t _mpq_expr_standard_table[] = {
78
79
{ "**", (mpexpr_fun_t) e_mpq_pow_ui,
80
MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },
81
82
{ "!", (mpexpr_fun_t) e_mpq_sgn,
83
MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },
84
{ "-", (mpexpr_fun_t) mpq_neg,
85
MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },
86
87
{ "*", (mpexpr_fun_t) mpq_mul, MPEXPR_TYPE_BINARY, 200 },
88
{ "/", (mpexpr_fun_t) mpq_div, MPEXPR_TYPE_BINARY, 200 },
89
90
{ "+", (mpexpr_fun_t) mpq_add, MPEXPR_TYPE_BINARY, 190 },
91
{ "-", (mpexpr_fun_t) mpq_sub, MPEXPR_TYPE_BINARY, 190 },
92
93
{ "<<", (mpexpr_fun_t) mpq_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
94
{ ">>", (mpexpr_fun_t) mpq_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
95
96
{ "<=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LE, 170 },
97
{ "<", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LT, 170 },
98
{ ">=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GE, 170 },
99
{ ">", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GT, 170 },
100
101
{ "==", (mpexpr_fun_t) e_mpq_equal, MPEXPR_TYPE_I_BINARY, 160 },
102
{ "!=", (mpexpr_fun_t) e_mpq_notequal, MPEXPR_TYPE_I_BINARY, 160 },
103
104
{ "&&", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },
105
{ "||", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },
106
107
{ ":", NULL, MPEXPR_TYPE_COLON, 101 },
108
{ "?", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_QUESTION, 100 },
109
110
{ ")", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_CLOSEPAREN, 4 },
111
{ "(", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_OPENPAREN, 3 },
112
{ ",", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_ARGSEP, 2 },
113
{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },
114
115
{ "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY },
116
{ "cmp", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_I_BINARY },
117
{ "den", (mpexpr_fun_t) e_mpq_den, MPEXPR_TYPE_UNARY },
118
{ "max", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
119
{ "min", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },
120
{ "num", (mpexpr_fun_t) e_mpq_num, MPEXPR_TYPE_UNARY },
121
{ "sgn", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_I_UNARY },
122
123
{ NULL }
124
};
125
126
__gmp_const struct mpexpr_operator_t * __gmp_const mpq_expr_standard_table
127
= _mpq_expr_standard_table;
128
129
130
int
131
#if HAVE_STDARG
132
mpq_expr (mpq_ptr res, int base, __gmp_const char *e, ...)
133
#else
134
mpq_expr (va_alist)
135
va_dcl
136
#endif
137
{
138
mpq_srcptr var[MPEXPR_VARIABLES];
139
va_list ap;
140
int ret;
141
#if HAVE_STDARG
142
va_start (ap, e);
143
#else
144
mpq_ptr res;
145
int base;
146
__gmp_const char *e;
147
va_start (ap);
148
res = va_arg (ap, mpq_ptr);
149
base = va_arg (ap, int);
150
e = va_arg (ap, __gmp_const char *);
151
#endif
152
153
TRACE (printf ("mpq_expr(): base %d, %s\n", base, e));
154
ret = mpexpr_va_to_var ((void **) var, ap);
155
va_end (ap);
156
157
if (ret != MPEXPR_RESULT_OK)
158
return ret;
159
160
return mpq_expr_a (mpq_expr_standard_table, res, base, e, strlen(e), var);
161
}
162
163