First time here? Check out the FAQ!

Ask Your Question
0

how to extract a common factor of vector components

asked 6 years ago

rewolf gravatar image

v=vec([ax,ay])

should give

a*vec([x,y])

by a command. Which command?

Preview: (hide)

Comments

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)

Please edit your question to do that.

slelievre gravatar imageslelievre ( 6 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 6 years ago

rburing gravatar image

updated 6 years ago

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)
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 6 years ago

Seen: 622 times

Last updated: Jun 30 '18