v=vec([ax,ay])

a*vec([x,y])

# by a command. Which command?

edit retag close merge delete

To display inline code, like z = x*y, use backticks.

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

• indent all code lines with 4 spaces
• select all code lines and click the "code" button (the icon with '101 010')
• select all code lines and hit ctrl-K

For instance, typing

If we define f by

def f(x, y):
return (x, y)

then f(2, 3) returns (2, 3) but f(2) gives:

TypeError: f() takes exactly 2 arguments (1 given)


produces:

If we define f by

def f(x, y):
return (x, y)


then f(2, 3) returns (2, 3) but f(2) gives:

TypeError: f() takes exactly 2 arguments (1 given)


Sort by » oldest newest most voted 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)], 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)

more