source: git/gfanlib/gfanlib_q.h

spielwiese
Last change on this file was f82665, checked in by Hans Schoenemann <hannes@…>, 5 years ago
update gfanlib to 0.6.2
  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * lib_q.h
3 *
4 *  Created on: Sep 29, 2010
5 *      Author: anders
6 */
7
8#ifndef LIB_Q_H_
9#define LIB_Q_H_
10
11#include <string.h>
12#include <ostream>
13#include <assert.h>
14#include "gmp.h"
15
16#include "gfanlib_z.h"
17
18namespace gfan{
19class Rational
20{
21  mpq_t value;
22public:
23  static bool isField()
24  {
25    return true;
26  }
27  Rational()
28  {
29    mpq_init(value);
30  }
31  Rational(signed long int value_)
32  {
33    mpq_init(value);
34//    mpz_init_set_si(mpq_numref(value), value_);
35//    mpz_init_set_ui(mpq_denref(value), 1);
36    mpz_set_si(mpq_numref(value), value_);
37    mpz_set_ui(mpq_denref(value), 1);
38    mpq_canonicalize(value);
39  }
40  Rational(Rational const & value_)
41  {
42    mpq_init(value);
43    mpq_set(value,value_.value);
44  }
45  Rational(mpq_t value_)
46  {
47    mpq_init(value);
48    mpq_set(value,value_);
49  }
50  explicit Rational(Integer const & value_)
51  {
52    mpq_init(value);
53//    mpz_init_set(mpq_numref(value), value_.value);
54//    mpz_init_set_ui(mpq_denref(value), 1);
55    mpz_set(mpq_numref(value), value_.value);
56    mpz_set_ui(mpq_denref(value), 1);
57    mpq_canonicalize(value);
58  }
59  ~Rational()
60  {
61    mpq_clear(value);
62  }
63  Rational& operator=(const Rational& a)
64    {
65      const Rational *A=(const Rational*)&a;
66      if (this != A) {
67        mpq_clear(value);
68        mpq_init(value);
69        mpq_set(value,a.value);
70      }
71      return *this;
72    }
73  bool isZero()const{
74    return mpz_sgn(mpq_numref(value))==0;
75  }
76  friend std::ostream &operator<<(std::ostream &f, Rational const &a)
77  {
78    void (*freefunc)(void *, size_t);
79    mp_get_memory_functions(0,0,&freefunc);
80    char *str=mpq_get_str(0,10,a.value);
81    f<<str;
82    freefunc(str,strlen(str)+1);
83    return f;
84  }
85  Rational& operator+=(const Rational& a)
86    {
87      mpq_add(value,value,a.value);
88      return *this;
89    }
90  Rational& operator-=(const Rational& a)
91    {
92      mpq_sub(value,value,a.value);
93      return *this;
94    }
95  Rational& operator*=(const Rational& a)
96    {
97      mpq_mul(value,value,a.value);
98      return *this;
99    }
100  Rational& operator/=(const Rational& a)
101    {
102      assert(!a.isZero());
103      mpq_div(value,value,a.value);
104      return *this;
105    }
106  friend Rational operator-(const Rational &b)
107  {
108    Rational ret;
109    ret-=b;
110    return ret;
111  }
112  Rational operator+(const Rational &a)const
113  {
114    Rational ret(*this);
115    ret+=a;
116    return ret;
117  }
118  Rational operator-(const Rational &a)const
119  {
120    Rational ret(*this);
121    ret-=a;
122    return ret;
123  }
124  Rational operator*(const Rational &a)const
125  {
126    Rational ret(*this);
127    ret*=a;
128    return ret;
129  }
130  Rational operator/(const Rational &a)const
131  {
132    Rational ret(*this);
133    ret/=a;
134    return ret;
135  }
136  void madd(const Rational &a,const Rational &b)
137    {
138      mpq_t temp;
139      mpq_init(temp);
140      mpq_mul(temp,a.value,b.value);
141      mpq_add(value,value,temp);
142      mpq_clear(temp);
143    }
144  bool operator<(const Rational &a)const
145  {
146    return mpq_cmp(value,a.value)<0;
147  }
148  bool operator==(const Rational &a)const
149  {
150    return mpq_cmp(value,a.value)==0;
151  }
152  bool operator!=(const Rational &a)const
153  {
154    return mpq_cmp(value,a.value)!=0;
155  }
156  /**
157   * Returns +1,-1, or 0.
158   */
159  int sign()const
160  {
161    return mpq_sgn(value);
162  }
163  static Rational gcd(Rational const &a, Rational const &b, Rational &s, Rational &t)
164  {
165/*    mpz_t r;
166    mpz_init(r);
167    mpz_gcdext(r,s.value,t.value,a.value,b.value);
168    Integer ret(r);
169    mpz_clear(r);*/
170    assert(0 && "gcd for Rational not defined");
171    return a;
172  }
173  /**
174   * Assigns the value to q. q must have been initialized as a gmp variable.
175   */
176  void setGmp(mpq_t q)const
177  {
178    mpq_set(q,value);
179  }
180};
181
182
183}
184
185#endif /* LIB_Q_H_ */
Note: See TracBrowser for help on using the repository browser.