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

◆ power() [1/2]

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

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

Definition at line 166 of file ring.cpp.

167{
168 // TODO: reorganize to match the above routine (but using an int).
169 ring_elem ff = gg;
170 if (n == 0) return one();
171 if (n < 0)
172 {
173 n = -n;
174 ff = invert(ff);
175 if (is_zero(ff))
176 {
177 ERROR("negative power of noninvertible element requested");
178 return ff;
179 }
180 }
181
182 // The exponent 'n' should be > 0 here.
183 ring_elem prod = from_long(1);
184 ring_elem base = copy(ff);
185 ring_elem tmp;
186
187 for (;;)
188 {
189 if ((n % 2) != 0)
190 {
191 tmp = mult(prod, base);
192 prod = tmp;
193 }
194 n >>= 1;
195 if (n == 0) { return prod; }
196 else
197 {
198 tmp = mult(base, base);
199 base = tmp;
200 }
201 }
202}
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 CanonicalForm base
Definition factory.cpp:289
const int ERROR
Definition m2-mem.cpp:55

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