Macaulay2 Engine
Loading...
Searching...
No Matches

◆ power() [2/2]

ring_elem Ring::power ( const ring_elem f,
mpz_srcptr n ) const
virtual

Exponentiation. This is the default function, if a class doesn't define this.

Reimplemented in FractionField, GF, LocalRing, M2::ConcreteRing< RingType >, M2::ConcreteRing< ARingQQ >, M2::ConcreteRing< M2::ARingCCC >, M2::ConcreteRing< M2::ARingRRR >, M2FreeAlgebra, M2FreeAlgebraQuotient, PolyRing, PolyRingQuotient, RingZZ, SchurRing, SkewPolynomialRing, SolvableAlgebra, WeylAlgebra, and Z_mod.

Definition at line 109 of file ring.cpp.

110{
111 ring_elem ff = gg;
112 int cmp = mpz_sgn(m); // the sign of m, <0, ==0, >0
113 if (cmp == 0) return one();
114 mpz_t n;
115 mpz_init_set(n, m);
116 // @TODO MES: rewrite this so it inverts before creating n.
117 // That way we don't have to catch any exceptions here.
118 // e.g. as
119#if 0
120 if (cmp < 0)
121 ff = invert(ff);
122 mpz_init_set(n, m);
123 mpz_t n;
124 if (cmp < 0)
125 mpz_neg(n, n);
126#endif
127 if (cmp < 0)
128 {
129 mpz_neg(n, n);
130 ff = invert(
131 ff); // this can raise an exception, in which case we need to free n.
132 if (is_zero(ff))
133 {
134 ERROR(
135 "either element not invertible, or no method available to "
136 "compute its inverse");
137 mpz_clear(n);
138 return ff;
139 }
140 }
141 ring_elem prod = from_long(1);
142 ring_elem base = copy(ff);
143 ring_elem tmp;
144
145 for (;;)
146 {
147 if (RingZZ::mod_ui(n, 2) == 1)
148 {
149 tmp = mult(prod, base);
150 prod = tmp;
151 }
152 mpz_tdiv_q_2exp(n, n, 1);
153 if (mpz_sgn(n) == 0)
154 {
155 mpz_clear(n);
156 return prod;
157 }
158 else
159 {
160 tmp = mult(base, base);
161 base = tmp;
162 }
163 }
164}
ring_elem one() const
Definition ring.hpp:357
virtual ring_elem invert(const ring_elem f) const =0
virtual ring_elem from_long(long n) const =0
virtual ring_elem copy(const ring_elem f) const =0
virtual bool is_zero(const ring_elem f) const =0
virtual ring_elem mult(const ring_elem f, const ring_elem g) const =0
static unsigned int mod_ui(mpz_srcptr n, unsigned int p)
Definition ZZ.cpp:55
static CanonicalForm base
Definition factory.cpp:289
const int ERROR
Definition m2-mem.cpp:55

References base, copy(), ERROR, from_long(), invert(), is_zero(), RingZZ::mod_ui(), mult(), and one().

Referenced by convertToM2(), GF::eval(), M2::ARingGFFlint::eval(), M2::ARingGFM2::eval(), FreeAlgebra::power(), PolyRingQuotient::power(), PolyRingQuotient::power(), SkewPolynomialRing::power(), SolvableAlgebra::power(), WeylAlgebra::power(), TEST(), and Matrix::top_coefficients().