Ask Your Question

Revision history [back]

It's not clear what type of object you want as a result. You could store an 'unevaluated' expression such as a scalar times a vector as a formal sum (FormalSum) with coefficients in the base ring of the vector:

def extract_gcd(vector):
    g = gcd(vector)
    return FormalSum([(g, vector/g)], FormalSums(vector.base_ring()))

Now

var('a,x,y')
extract_gcd(vector([a*x,a*y]))

yields

a*(x, y)

As an alternative, you might just want to work with pairs:

def extract_gcd(vector):
    g = gcd(vector)
    return (g, vector/g)

It's not clear what type of object you want as a result. You could store an 'unevaluated' expression such as a scalar times a vector as a formal sum (FormalSum) with coefficients in the base ring of the vector:

def extract_gcd(vector):
extract_gcd(vec):
    g = gcd(vector)
gcd(vec)
    return FormalSum([(g, vector/g)], FormalSums(vector.base_ring()))
vec/g)], FormalSums(vec.base_ring()))

Now

var('a,x,y')
extract_gcd(vector([a*x,a*y]))

yields

a*(x, y)

As an alternative, you might just want to work with pairs:

def extract_gcd(vector):
extract_gcd(vec):
    g = gcd(vector)
gcd(vec)
    return (g, vector/g)
vec/g)

It's not clear what type of object you want as a result. You could store an 'unevaluated' expression such as a scalar times a vector as a formal sum (FormalSum) with coefficients in the base ring of the vector:

def extract_gcd(vec):
    g = gcd(vec)
    return FormalSum([(g, vec/g)], FormalSums(vec.base_ring()))
parent=FormalSums(vec.base_ring()))

Now

var('a,x,y')
extract_gcd(vector([a*x,a*y]))

yields

a*(x, y)

As an alternative, you might just want to work with pairs:

def extract_gcd(vec):
    g = gcd(vec)
    return (g, vec/g)