class JAS::RingElem
Proxy for JAS ring elements.
Methods to be used as + - * ** / %.
Attributes
the Java element object
the Java factory object
Public Class Methods
Constructor for ring element.
# File examples/jas.rb, line 528 def initialize(elem) if elem.is_a? RingElem @elem = elem.elem; else @elem = elem; end begin @ring = @elem.factory(); rescue @ring = @elem; end @elem.freeze @ring.freeze self.freeze end
Public Instance Methods
Modular remainder of two ring elements.
# File examples/jas.rb, line 867 def %(other) s,o = coercePair(self,other); return RingElem.new( s.elem.remainder( o.elem ) ); end
Multiply two ring elements.
# File examples/jas.rb, line 828 def *(other) #puts "* self type(#{self.elem}) = #{self.elem.class}\n"; #puts "* other type(#{other.elem}) = #{other.elem.class}\n"; s,o = coercePair(self,other); #puts "* s = #{s}, o = #{o}, s*o = #{s.elem.multiply(o.elem)}\n"; return RingElem.new( s.elem.multiply( o.elem ) ); end
Power of this to other.
# File examples/jas.rb, line 882 def **(other) #puts "pow other type(#{other}) = #{other.class}"; if other.is_a? Integer n = other; else if other.is_a? RingElem n = other.elem; if n.is_a? BigRational #puts "#{n.numerator()} / #{n.denominator()}, other.elem = #{n}" #todo (x**n.n)/(x**(-n.d)) if n.numerator().is_a? BigInteger n = n.numerator().intValue() / n.denominator().intValue(); else n = n.numerator() / n.denominator(); end end if n.is_a? BigInteger n = n.intValue(); end end end if isFactory() p = Power.new(@elem).power( @elem, n ); else p = Power.new(@elem.factory()).power( @elem, n ); #puts "@elem**#{n} = #{p}, @elem = #{@elem}" #puts "@elem.ring = #{@elem.ring.toScript()}"; end return RingElem.new( p ); end
Add two ring elements.
# File examples/jas.rb, line 839 def +(other) #puts "+ self type(#{self}) = #{self.elem.class}\n"; #puts "+ other type(#{other}) = #{other.elem.class}\n"; s,o = coercePair(self,other); return RingElem.new( s.elem.sum( o.elem ) ); end
Positive value.
# File examples/jas.rb, line 639 def +@() return self; end
Subtract two ring elements.
# File examples/jas.rb, line 849 def -(other) #puts "+ self type(#{self}) = #{self.elem.class}\n"; #puts "+ other type(#{other}) = #{other.elem.class}\n"; s,o = coercePair(self,other); return RingElem.new( s.elem.subtract( o.elem ) ); end
Negative value.
# File examples/jas.rb, line 632 def -@() return RingElem.new( @elem.negate() ); end
Divide two ring elements.
# File examples/jas.rb, line 859 def /(other) s,o = coercePair(self,other); return RingElem.new( s.elem.divide( o.elem ) ); end
Compare two ring elements.
# File examples/jas.rb, line 795 def <=>(other) #s,o = coercePair(other); s,o = self, other return s.elem.compareTo( o.elem ); end
Test if two ring elements are equal.
# File examples/jas.rb, line 804 def ==(other) if not other.is_a? RingElem return false; end return (self <=> other) == 0; end
Can not be used as power.
# File examples/jas.rb, line 875 def ^(other) return nil; end
Absolute value.
# File examples/jas.rb, line 625 def abs() return RingElem.new( @elem.abs() ); end
Compute algebraic roots, i.e. real and complex algebraic roots of univariate polynomial.
# File examples/jas.rb, line 1232 def algebraicRoots(eps=nil) a = @elem; if eps.is_a? RingElem eps = eps.elem; end begin if eps == nil ar = RootFactory.algebraicRoots( a ); else ar = RootFactory.algebraicRoots( a, eps ); end #no: ar = ar.map{ |y| RingElem.new(y) }; return RingElem.new(ar); #?? rescue => e puts "error " + str(e) return nil end end
Coefficient ring of a polynomial.
# File examples/jas.rb, line 1414 def base_ring() begin ev = @elem.ring.coFac; rescue return nil; end return RingElem.new(ev); end
Apply this to num.
Call syntax is ringElem.(num). Only for interger num.
# File examples/jas.rb, line 1357 def call(num) if num == 0 return zero(); end if num == 1 return one(); end return RingElem.new( @ring.fromInteger(num) ); end
Get the coefficients of a polynomial.
# File examples/jas.rb, line 1330 def coefficients() a = @elem; #L = [ c.toScriptFactory() for c in a.coefficientIterator() ]; ll = a.coefficientIterator().map { |c| RingElem.new(c) }; return ll end
Coerce other to self
# File examples/jas.rb, line 646 def coerce(other) s,o = coercePair(self,other); return [o,s]; # keep order for non-commutative end
Coerce other to self
# File examples/jas.rb, line 679 def coerceElem(other) #puts "self type(#{self}) = #{self.class}\n"; #puts "other type(#{other}) = #{other.class}\n"; if @elem.is_a? GenVector if other.is_a? Array o = rbarray2arraylist(other,@elem.factory().coFac,rec=1); o = GenVector.new(@elem.factory(),o); return RingElem.new( o ); end end if @elem.is_a? GenMatrix if other.is_a? Array o = rbarray2arraylist(other,@elem.factory().coFac,rec=2); o = GenMatrix.new(@elem.factory(),o); return RingElem.new( o ); end end if other.is_a? RingElem if isPolynomial() and not other.isPolynomial() #puts "other parse(#{@ring})\n"; o = @ring.parse( other.elem.toString() ); # not toScript() return RingElem.new( o ); end #puts "other type(#{other}) = #{other.class}\n"; return other; end #puts "--1"; if other.is_a? Array # assume BigRational or BigComplex # assume self will be compatible with them. todo: check this puts "other type(#{other})_3 = #{other.class}\n"; o = makeJasArith(other); puts "other type(#{o})_4 = #{o.class}\n"; ##o = BigRational.new(other[0],other[1]); if isPolynomial() o = @ring.parse( o.toString() ); # not toScript(); #o = o.elem; elsif @elem.is_a? BigComplex o = CC( o ); o = o.elem; elsif @elem.is_a? BigQuaternion o = Quat( o ); o = o.elem; elsif @elem.is_a? BigOctonion o = Oct( Quat(o) ); o = o.elem; elsif @elem.is_a? Product o = RR(@ring, @elem.multiply(o) ); # valueOf #puts "o = #{o}"; o = o.elem; end puts "other type(#{o})_5 = #{o.class}\n"; return RingElem.new(o); end # test if @elem is a factory itself if isFactory() if other.is_a? Integer o = @elem.fromInteger(other); elsif other.is_a? Rational o = @elem.fromInteger( other.numerator ); o = o.divide( @elem.fromInteger( other.denominator ) ); elsif other.is_a? Float # ?? what to do ?? o = @elem.parse( other.to_s ); ##o = @elem.fromInteger( other.to_i ); else puts "unknown other type(#{other})_1 = #{other.class}\n"; o = @elem.parse( other.to_s ); end return RingElem.new(o); end # @elem has a ring factory if other.is_a? Integer o = @elem.factory().fromInteger(other); elsif other.is_a? Rational o = @elem.factory().fromInteger( other.numerator ); o = o.divide( @elem.factory().fromInteger( other.denominator ) ); elsif other.is_a? Float # ?? what to do ?? o = @elem.factory().parse( other.to_s ); if @elem.is_a? Product o = RR(@ring, @elem.idempotent().multiply(o) ); # valueOf o = o.elem; end else puts "unknown other type(#{other})_2 = #{other.class}\n"; o = @elem.factory().parse( other.to_s ); end return RingElem.new(o); end
Coerce type a to type b or type b to type a.
# File examples/jas.rb, line 654 def coercePair(a,b) #puts "a type(#{a}) = #{a.class}\n"; #puts "b type(#{b}) = #{b.class}\n"; begin if not a.isPolynomial() and b.isPolynomial() #puts "b = " + str(b.isPolynomial()); s = b.coerceElem(a); o = b; else s = a; o = a.coerceElem(b); end rescue => e #puts "e #{e.message}, a = #{a}"; s = a; o = a.coerceElem(b); end #puts "s type(#{s}) = #{s.class}\n"; #puts "o type(#{o}) = #{o.class}\n"; return [s,o]; end
Compute complex roots of univariate polynomial.
# File examples/jas.rb, line 1190 def complexRoots(eps=nil) a = @elem; if eps.is_a? RingElem eps = eps.elem; end cmplx = false; begin x = a.ring.coFac.getONE().getRe(); cmplx = true; rescue => e #pass; end begin if eps == nil #rr = ComplexRootsSturm.new(a.ring.coFac).complexRoots( a ); if cmplx rr = RootFactory.complexAlgebraicNumbersComplex( a ); else rr = RootFactory.complexAlgebraicNumbers( a ); end #R = [ r.centerApprox() for r in R ]; else ## rr = ComplexRootsSturm.new(a.ring.coFac).complexRoots( a, eps ); ## rr = [ r.centerApprox() for r in rr ]; ##rr = ComplexRootsSturm.new(a.ring.coFac).approximateRoots( a, eps ); if cmplx rr = RootFactory.complexAlgebraicNumbersComplex( a, eps ); else rr = RootFactory.complexAlgebraicNumbers( a, eps ); end end rr = rr.map{ |y| RingElem.new(y) }; return rr; rescue => e puts "error " + str(e) return nil end end
Compute decimal approximation of real and complex roots of univariate polynomial.
# File examples/jas.rb, line 1272 def decimalRoots(eps=nil) a = @elem; if eps.is_a? RingElem eps = eps.elem; end if a.is_a? Java::EduJasRoot::AlgebraicRoots a = a.p; end begin d = RootFactory.decimalRoots( a, eps ); return RingElem.new(d); # ?? rescue => e puts "error " + str(e) return nil end end
Degree of a polynomial.
# File examples/jas.rb, line 1402 def degree() begin ev = @elem.degree(); rescue return nil; end return ev; end
Differentiate a power series.
r is for partial differentiation in variable r.
# File examples/jas.rb, line 1036 def differentiate(r=nil) begin if r != nil e = @elem.differentiate(r); else e = @elem.differentiate(); end rescue e = @elem.factory().getZERO(); end return RingElem.new( e ); end
Test if self divides other.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1445 def divides(other) s,o = coercePair(self,other); return o.elem.remainder( s.elem ).isZERO(); end
Test if two ring elements are equal.
# File examples/jas.rb, line 916 def equal?(other) o = other; if other.is_a? RingElem o = other.elem; end return @elem.equals(o) end
Evaluate at a for power series or polynomial.
# File examples/jas.rb, line 958 def evaluate(a) #puts "self type(#{@elem}) = #{@elen.class}"; #puts "a type(#{a}) = #{a.class}"; x = nil; if a.is_a? RingElem x = a.elem; end if a.is_a? Array # assume BigRational or BigComplex # assume self will be compatible with them. todo: check this #x = makeJasArith(a); x = rbarray2arraylist(a); else x = rbarray2arraylist([a]); end begin if @elem.is_a? UnivPowerSeries e = @elem.evaluate(x[0]); else if @elem.is_a? MultiVarPowerSeries e = @elem.evaluate(x); else #puts "x = " + x[0].getClass().getSimpleName().to_s; x = x.map{ |p| p.leadingBaseCoefficient() }; e = PolyUtil.evaluateAll(@ring.coFac, @elem, x); end end rescue => e raise RuntimeError, e.to_s; e = 0; end return RingElem.new( e ); end
Compute irreducible factorization for modular, integer, rational number and algebriac number coefficients.
# File examples/jas.rb, line 1111 def factors() a = @elem; if isPolynomial() factor = Ring.getEngineFactor(@ring); if factor == nil raise NotImplementedError, "factors not implemented for " + @ring.to_s; end cf = @ring.coFac; if cf.is_a? GenPolynomialRing e = factor.recursiveFactors( a ); else e = factor.factors( a ); end ll = {}; for k in e.keySet() i = e.get(k); ll[ RingElem.new( k ) ] = i; end return ll; else raise NotImplementedError, "factors not implemented for " + a.to_s; end end
Compute absolute irreducible factorization for (modular,) rational number coefficients.
# File examples/jas.rb, line 1139 def factorsAbsolute() a = @elem; if isPolynomial() factor = Ring.getEngineFactor(@ring); if factor == nil raise NotImplementedError, "factors not implemented for " + @ring.to_s; end begin ll = factor.factorsAbsolute( a ); ## ll = {}; ## for a in e.keySet() ## i = e.get(a); ## ll[ RingElem.new( a ) ] = i; return ll; rescue => e raise RuntimeError, "error factorsAbsolute " + @ring.to_s; end else raise NotImplementedError, "factors not implemented for " + a.to_s; end end
Get the factory of this element.
# File examples/jas.rb, line 927 def factory() fac = @elem.factory(); begin nv = fac.nvar; rescue return RingElem.new(fac); end #return PolyRing(fac.coFac,fac.getVars(),fac.tord); return RingElem.new(fac); end
Compute the greatest common divisor of this/self and b.
# File examples/jas.rb, line 1064 def gcd(b) a = @elem; if b.is_a? RingElem b = b.elem; else b = element( b ); b = b.elem; end if isPolynomial() engine = Ring.getEngineGcd(@ring); return RingElem.new( engine.gcd(a,b) ); else return RingElem.new( a.gcd(b) ); end end
Get the generators for the factory of this element.
# File examples/jas.rb, line 941 def gens() ll = @elem.factory().generators(); #puts "L = #{ll}"; nn = ll.map {|e| RingElem.new(e) }; return nn; end
Create an ideal.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1455 def ideal(list) r = Ring.new("",ring=self.ring); #,fast=true return r.ideal("",list=list); end
Integrate a power series or rational function with constant a.
a is the integration constant, r is for partial integration in variable r.
# File examples/jas.rb, line 996 def integrate(a=0,r=nil) #puts "self type(#{@elem}) = #{@elem.class}"; #puts "a type(#{a}) = #{a.class}"; x = nil; if a.is_a? RingElem x = a.elem; end if a.is_a? Array # assume BigRational or BigComplex # assume self will be compatible with them. todo: check this x = makeJasArith(a); end # power series begin if r != nil e = @elem.integrate(x,r); else e = @elem.integrate(x); end return RingElem.new( e ); rescue #pass; end cf = @elem.ring; begin cf = cf.ring; rescue #pass; end # rational function integrator = ElementaryIntegration.new(cf.coFac); ei = integrator.integrate(@elem); return ei; end
Test if this is itself a ring factory.
# File examples/jas.rb, line 771 def isFactory() f = @elem.factory(); if @elem == f return true; else return false; end end
Test if this is the one element of the ring.
# File examples/jas.rb, line 604 def isONE() return @elem.isONE(); end
Test if this is a polynomial.
# File examples/jas.rb, line 783 def isPolynomial() begin nv = @elem.ring.coFac; #nvar; rescue return false; end return true; end
Test if this is the zero element of the ring.
# File examples/jas.rb, line 583 def isZERO() return @elem.isZERO(); end
Test if this RingElem is field.
# File examples/jas.rb, line 1426 def is_field() return @ring.isField(); end
Leading coefficient of a polynomial.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1383 def lc() c = @elem.leadingBaseCoefficient(); return RingElem.new(c); end
Length of an element.
# File examples/jas.rb, line 814 def len() return self.elem.length(); end
Leading monomial of a polynomial.
Compatibility method for Sage/Singular. Note: the meaning of lt and lm is swapped compared to JAS.
# File examples/jas.rb, line 1373 def lm() ev = @elem.leadingExpVector(); return ev; end
Leading term of a polynomial.
Compatibility method for Sage/Singular. Note: the meaning of lt and lm is swapped compared to JAS.
# File examples/jas.rb, line 1394 def lt() ev = @elem.leadingMonomial(); return Monomial.new(ev); end
Monic polynomial.
# File examples/jas.rb, line 951 def monic() return RingElem.new( @elem.monic() ); end
Test divide of ExpVectors.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1491 def monomial_divides(a,b) #print "JAS a = " + str(a) + ", b = " + str(b); if a.is_a? RingElem a = a.elem; end if a.is_a? GenPolynomial a = a.leadingExpVector(); end if not a.is_a? ExpVector raise ArgumentError, "No ExpVector given " + str(a) + ", " + str(b) end if b == nil return False; end if b.is_a? RingElem b = b.elem; end if b.is_a? GenPolynomial b = b.leadingExpVector(); end if not b.is_a? ExpVector raise ArgumentError, "No ExpVector given " + str(a) + ", " + str(b) end return a.divides(b); end
Lcm of ExpVectors.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1539 def monomial_lcm(e,f) if e.is_a? RingElem e = e.elem; end if f.is_a? RingElem f = f.elem; end # assume JAS ExpVector c = e.lcm(f); return c; end
Test if ExpVectors are pairwise prime.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1522 def monomial_pairwise_prime(e,f) if e.is_a? RingElem e = e.elem; end if f.is_a? RingElem f = f.elem; end # assume JAS ExpVector c = e.gcd(f); return c.isZERO(); end
Quotient of ExpVectors.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1465 def monomial_quotient(a,b,coeff=false) if a.is_a? RingElem a = a.elem; end if b.is_a? RingElem b = b.elem; end if coeff == false if a.is_a? GenPolynomial return RingElem.new( a.divide(b) ); else return RingElem.new( GenPolynomial.new(@ring, a.subtract(b)) ); end else # assume JAS Monomial c = a.coefficient().divide(b.coefficient()); e = a.exponent().subtract(b.exponent()) return RingElem.new( GenPolynomial.new(@ring, c, e) ); end end
All monomials of a polynomial.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1435 def monomials() ev = @elem.getMap().keySet(); return ev; end
Hash value.
# File examples/jas.rb, line 821 def object_id() return @elem.hashCode(); end
One element of this ring.
# File examples/jas.rb, line 597 def one() return RingElem.new( @elem.factory().getONE() ); end
Test if this is the one element of the ring.
# File examples/jas.rb, line 611 def one?() return @elem.isONE(); end
Parent in Sage is factory in JAS.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1348 def parent() return factory(); end
Random element.
n size for random element will be less than 2**n.
# File examples/jas.rb, line 1054 def random(n=3) if n.is_a? RingElem n = n.elem end return RingElem.new( @elem.factory().random(n) ); end
Compute real roots of univariate polynomial.
# File examples/jas.rb, line 1164 def realRoots(eps=nil) a = @elem; if eps.is_a? RingElem eps = eps.elem; end begin if eps == nil #rr = RealRootsSturm.new().realRoots( a ); rr = RootFactory.realAlgebraicNumbers( a ) else rr = RootFactory.realAlgebraicNumbers( a, eps ); ## rr = RealRootsSturm.new().realRoots( a, eps ); ## rr = [ r.toDecimal() for r in rr ]; #rr = RealRootsSturm.new().approximateRoots(a,eps); end rr = rr.map{ |e| RingElem.new(e) }; return rr; rescue => e puts "error " + str(e) return nil end end
Compute a normal form of self with respect to ff.
Compatibility method for Sage/Singular.
# File examples/jas.rb, line 1556 def reduce(ff) s = @elem; fe = ff.map {|e| e.elem }; n = ReductionSeq.new().normalform(fe,s); return RingElem.new(n); end
Root reduce of real and complex algebraic numbers. Compute an extension field with a primitive element.
# File examples/jas.rb, line 1311 def rootReduce(other) a = @elem; b = other; if b.is_a? RingElem b = b.elem; end begin #Java::EduJasApplication:: d = RootFactoryApp.rootReduce( a, b ); return RingElem.new(d); # ?? rescue => e puts "error " + str(e) return nil end end
Compute algebraic roots refinement.
# File examples/jas.rb, line 1254 def rootRefine(eps=nil) r = @elem; if eps.is_a? RingElem eps = eps.elem; end begin RootFactory.rootRefine( r, eps ); #no: ar = ar.map{ |y| RingElem.new(y) }; return RingElem.new(r); # ?? rescue => e puts "error " + str(e) return nil end end
Roots of unity of real and complex algebraic numbers.
# File examples/jas.rb, line 1292 def rootsOfUnity() a = @elem; begin #Java::EduJasApplication:: if a.is_a? AlgebraicRootsPrimElem d = RootFactoryApp.rootsOfUnity( a ); else d = RootFactory.rootsOfUnity( a ); end return RingElem.new(d); # ?? rescue => e puts "error " + str(e) return nil end end
Get the sign of this element.
# File examples/jas.rb, line 618 def signum() return @elem.signum(); end
Compute squarefree factors of polynomial.
# File examples/jas.rb, line 1083 def squarefreeFactors() a = @elem; if isPolynomial() sqf = Ring.getEngineSqf(@ring); if sqf == nil raise NotImplementedError, "squarefreeFactors not implemented for " + @ring.to_s; end cf = @ring.coFac; if cf.is_a? GenPolynomialRing e = sqf.recursiveSquarefreeFactors( a ); else e = sqf.squarefreeFactors( a ); end ll = {}; for k in e.keySet() i = e.get(k); ll[ RingElem.new( k ) ] = i; end return ll; else raise NotImplementedError, "squarefreeFactors not implemented for " + a.to_s; end end
Convert to float.
# File examples/jas.rb, line 558 def to_f() e = @elem; if e.is_a? BigInteger e = BigRational(e); end if e.is_a? BigRational e = BigDecimal(e); end if e.is_a? BigDecimal e = e.toString(); end e = e.to_f(); return e; end
Create a string representation.
# File examples/jas.rb, line 547 def to_s() begin return @elem.toScript(); rescue return @elem.to_s(); end end
Zero element of this ring.
# File examples/jas.rb, line 576 def zero() return RingElem.new( @elem.factory().getZERO() ); end
Test if this is the zero element of the ring.
# File examples/jas.rb, line 590 def zero?() return @elem.isZERO(); end