Ask Your Question
0

how to extract a common factor of vector components

asked 2018-06-30 13:18:38 +0200

rewolf gravatar image

v=vec([ax,ay])

should give

a*vec([x,y])

by a command. Which command?

edit retag flag offensive close merge delete

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 ( 2018-06-30 19:25:54 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-06-30 22:04:38 +0200

rburing gravatar image

updated 2018-06-30 22:09:31 +0200

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)
edit flag offensive delete link more

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: 2018-06-30 13:18:38 +0200

Seen: 502 times

Last updated: Jun 30 '18