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

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2018-06-30 13:18:38 +0100 **

Seen: **467 times**

Last updated: **Jun 30 '18**

Rounding entries of a random vector

Gram-Schmidt with arbitrary inner product?

Eigenvalues of Hecke operators

How to solve this system of equation in sage?

Elements of finitely presented module

Unimodular matrices with additional restrictions

Linear Independence in Spaces of Matrices (or even tensors)

Matrix-scalar and vector-scalar operations

Equation for hyperplane of a reflection - Try to do TODO on reflection group sage page

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.