/usr/include/singular/gfanlib/gfanlib_q.h is in libsingular4-dev-common 4.0.3+ds-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* lib_q.h
*
* Created on: Sep 29, 2010
* Author: anders
*/
#ifndef LIB_Q_H_
#define LIB_Q_H_
#include <string.h>
#include <ostream>
#include "gmp.h"
#include "gfanlib_z.h"
namespace gfan{
class Rational
{
mpq_t value;
public:
static bool isField()
{
return true;
}
Rational()
{
mpq_init(value);
}
Rational(signed long int value_)
{
mpq_init(value);
// mpz_init_set_si(mpq_numref(value), value_);
// mpz_init_set_ui(mpq_denref(value), 1);
mpz_set_si(mpq_numref(value), value_);
mpz_set_ui(mpq_denref(value), 1);
mpq_canonicalize(value);
}
Rational(Rational const & value_)
{
mpq_init(value);
mpq_set(value,value_.value);
}
Rational(mpq_t value_)
{
mpq_init(value);
mpq_set(value,value_);
}
explicit Rational(Integer const & value_)
{
mpq_init(value);
// mpz_init_set(mpq_numref(value), value_.value);
// mpz_init_set_ui(mpq_denref(value), 1);
mpz_set(mpq_numref(value), value_.value);
mpz_set_ui(mpq_denref(value), 1);
mpq_canonicalize(value);
}
~Rational()
{
mpq_clear(value);
}
Rational& operator=(const Rational& a)
{
const Rational *A=(const Rational*)&a;
if (this != A) {
mpq_clear(value);
mpq_init(value);
mpq_set(value,a.value);
}
return *this;
}
bool isZero()const{
return mpz_sgn(mpq_numref(value))==0;
}
friend std::ostream &operator<<(std::ostream &f, Rational const &a)
{
void (*freefunc)(void *, size_t);
mp_get_memory_functions(0,0,&freefunc);
char *str=mpq_get_str(0,10,a.value);
f<<str;
freefunc(str,strlen(str)+1);
return f;
}
Rational& operator+=(const Rational& a)
{
mpq_add(value,value,a.value);
return *this;
}
Rational& operator-=(const Rational& a)
{
mpq_sub(value,value,a.value);
return *this;
}
Rational& operator*=(const Rational& a)
{
mpq_mul(value,value,a.value);
return *this;
}
Rational& operator/=(const Rational& a)
{
assert(!a.isZero());
mpq_div(value,value,a.value);
return *this;
}
friend Rational operator-(const Rational &b)
{
Rational ret;
ret-=b;
return ret;
}
Rational operator+(const Rational &a)const
{
Rational ret(*this);
ret+=a;
return ret;
}
Rational operator-(const Rational &a)const
{
Rational ret(*this);
ret-=a;
return ret;
}
Rational operator*(const Rational &a)const
{
Rational ret(*this);
ret*=a;
return ret;
}
Rational operator/(const Rational &a)const
{
Rational ret(*this);
ret/=a;
return ret;
}
void madd(const Rational &a,const Rational &b)
{
mpq_t temp;
mpq_init(temp);
mpq_mul(temp,a.value,b.value);
mpq_add(value,value,temp);
mpq_clear(temp);
}
bool operator<(const Rational &a)const
{
return mpq_cmp(value,a.value)<0;
}
bool operator==(const Rational &a)const
{
return mpq_cmp(value,a.value)==0;
}
bool operator!=(const Rational &a)const
{
return mpq_cmp(value,a.value)!=0;
}
/**
* Returns +1,-1, or 0.
*/
int sign()const
{
return mpq_sgn(value);
}
static Rational gcd(Rational const &a, Rational const /*&b*/, Rational /*&s*/, Rational /*&t*/)
{
/* mpz_t r;
mpz_init(r);
mpz_gcdext(r,s.value,t.value,a.value,b.value);
Integer ret(r);
mpz_clear(r);*/
assert(0);
return a;
}
static Rational gcd(Rational const &a, Rational const /*&b*/)
{
/* mpz_t r;
mpz_init(r);
mpz_gcdext(r,s.value,t.value,a.value,b.value);
Integer ret(r);
mpz_clear(r);*/
assert(0);
return a;
}
/**
* Assigns the value to q. q must have been initialized as a gmp variable.
*/
void setGmp(mpq_t q)const
{
mpq_set(q,value);
}
};
}
#endif /* LIB_Q_H_ */
|