diff --git a/qpms/polynomials.c b/qpms/polynomials.c index e8f16ec..37c8a21 100644 --- a/qpms/polynomials.c +++ b/qpms/polynomials.c @@ -7,6 +7,57 @@ #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) <= (y)) ? (x) : (y)) +void mpzs_init(mpzs_t x) { + mpz_init(x->_1); + mpz_init(x->_2); +} + +void mpzs_clear(mpzs_t x) { + mpz_clear(x->_1); + mpz_clear(x->_2); +} + +void mpzs_set(mpzs_t x, const mpzs_t y) { + mpz_set(x->_1, y->_1); + mpz_set(x->_2, y->_2); +} + +// Compares the square-rooted part of mpzs_t, so we can use it as a key in a search tree. +static int mpzs_cmp2(void *op1, void *op2) { + mpz_cmpabs(((mpzs_t) op1)->_2, ((mpzs_t) op2)->_2); +} + +void mpqs_clear_num(mpqs_t x) { + TODO; +} + +void mpqs_init(mpqs_t x) { + x->sz = 0; + x->nt = 0; + mpz_init_set_ui(x->den, 1); +} + +void mpqs_clear(mpqs_t x) { + mpqs_clear_num(mpqs_t x); + mpz_clear(x->den); + x->sz = -1 + x->nt = 0; +} + +static void mpqs_num_add_elem(mpqs_t q, mpzs_t n) { + mpzs_t *n_owned; + QPMS_CRASHING_MALLOC(n_owned, sizeof(mpzs_t)); + mpzs_init(*n_owned); + mpzs_set(*n_owned, n); + void *added = + tsearch(n_owned, &(t->nt), mpzs_cmp2); + QPMS_ENSURE(added, "Failed to add numerator element. Memory error?"); + QPMS_ENSURE(added != n_owned, "FIXME another numerator elements with the same square root found."); // TODO how to handle this? +} + + + + // Auxillary function to set a mpq_t to 0/1 static inline void mpq_zero(mpq_t q) { diff --git a/qpms/polynomials.h b/qpms/polynomials.h index 2f7adff..f722c6d 100644 --- a/qpms/polynomials.h +++ b/qpms/polynomials.h @@ -136,6 +136,32 @@ int qpqs_get_elem_si(long *numerator, unsigned long *denominator, const qpqs_t * void qpqs_clear(qpqs_t *p); +/// Type representing a number of form \f$ a \sqrt{b}; a \in \ints, b \in \nats \f$. +typedef struct _qp_mpzs { + mpz_t _1; ///< The integer factor \f$ a \f$. + mpz_t _2; ///< The square-rooted factor \f$ b \f$. Always positive. +} mpzs_t[1]; + +void mpzs_init(mpzs_t x); +void mpzs_clear(mpzs_t x); +void mpzs_set(mpzs_t x, const mpzs_t y); + +/// Sum of square roots of rational numbers. +/// Represented as \f$ \sum_s a_i \sqrt{b_i} / d \f$. +typedef struct _qp_mpqs { + int sz; ///< Used size of the numtree. + void *nt; ///< Numerator tree. + mpz_t den; ///< Denominator. Always positive. +} mpqs_t[1]; + +void mpqs_init(mpqs_t x); +void mpqs_clear(mpqs_t x); +void mpqs_add(mpqs_t sum, const mpqs_t addend1, const mpqs_t addend2); +void mpqs_sub(mpqs_t difference, const mpqs_t minuend, const mpqs_t substrahend); +void mpqs_mul(mpqs_t product, const mpqs_t multiplier, const mpqs_t multiplicand); +void mpqs_div(mpqs_t quotient, const mpqs_t dividend, const mpqs_t divisor); +void mpqs_clear_num(mpqs_t x); ///< Sets the numerator to zero, clearing the numerator tree. + /// A type representing a polynomial with rational coefficients times an optional factor \f$ \sqrt{1-x^2} \f$. typedef struct qpq_legendroid_t {