# how to extract a common factor of vector components

v=vec([a*x,a*y])

# should give

a*vec([x,y])

how to extract a common factor of vector components

v=vec([a*x,a*y])

a*vec([x,y])

1

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)
```

Asked: **
2018-06-30 06:18:38 -0500
**

Seen: **86 times**

Last updated: **Jun 30 '18**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.

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):

For instance, typing

produces:

Please edit your question to do that.