Ask Your Question
0

displaying coefficients of two variable polynomials on a plane

asked 2021-11-01 11:19:43 +0200

kinchocobo gravatar image

updated 2021-11-01 11:46:47 +0200

tmonteil gravatar image

Is there a way to arrange the coefficients of a multivariable polynomial in a specific way? For example, given

a+bx+cx^2+dy+exy in ZZ[x,y],

I want to see something similar to

d e
a b c.
edit retag flag offensive close merge delete

Comments

To show verbatim code (e.g. sage code), you should indent the corresponding block with 4 space. You can to this by clicking on th 101010 button in the editor.

tmonteil gravatar imagetmonteil ( 2021-11-01 11:46:37 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2021-11-01 11:45:38 +0200

tmonteil gravatar image

updated 2021-11-01 12:18:24 +0200

First, you should define the polynomial ring so that you can define the polynomial:

sage: R.<x,y> = ZZ[]
sage: R
Multivariate Polynomial Ring in x, y over Integer Ring

Then, you can define your polynomial:

sage: P = 3+4*x+8*x^2+7*y+2*x*y
sage: P
8*x^2 + 2*x*y + 4*x + 7*y + 3

A handy way to get the coefficients and exponents of the polynomial is by using the dict method:

sage: d = P.dict()
sage: d
{(2, 0): 8, (1, 1): 2, (1, 0): 4, (0, 1): 7, (0, 0): 3}
sage: list(d.items())
[((2, 0), 8), ((1, 1), 2), ((1, 0), 4), ((0, 1), 7), ((0, 0), 3)]

Now, you can start from an empty graphics and add the text elements one by one:

sage: G = Graphics()
sage: for (x,y),t in d.items():
....:     G += text(t, (x, y))

Then, you can see your plot:

sage: G

You can also tune your plot, e.g. by removing the axes:

sage: G.show(axes=False)
Launched png viewer for Graphics object consisting of 5 graphics primitives
edit flag offensive delete link more
1

answered 2021-11-01 11:59:02 +0200

FrédéricC gravatar image

Here is a function

def matrice_2d(self, variables=None):
    """
    EXAMPLES::

        sage: x, y = PolynomialRing(QQ,['x', 'y']).gens()
        sage: matrice_2d(x**2+x*y+y**3)
        [1 0 0]
        [0 0 0]
        [0 1 0]
        [0 0 1]

        sage: x, y, z = PolynomialRing(QQ,['x','y','z']).gens()
        sage: matrice_2d(x**2+z*x*y+z*y**3+z*x,[y,z])
        [  x   x   0   1]
        [x^2   0   0   0]
        sage: matrice_2d(x**2+z*x*y+z*y**3+z*x,[x,z])
        [  y^3 y + 1     0]
        [    0     0     1]
    """
    if variables is None:
        ring = self.parent().base_ring()
        x, y = self.parent().gens()
        ix = 0
        iy = 1
    else:
        x, y = variables
        ring = self.parent()
        toutes_vars = x.parent().gens()
        ix = toutes_vars.index(x)
        iy = toutes_vars.index(y)
    support = self.exponents()
    minx = min(u[ix] for u in support)
    maxx = max(u[ix] for u in support)
    miny = min(u[iy] for u in support)
    maxy = max(u[iy] for u in support)
    mat = matrix(ring, maxy - miny + 1, maxx - minx + 1)
    for u in support:
        ex = u[ix]
        ey = u[iy]
        i, j = (maxy - ey, ex - minx)
        mat[i, j] = self.coefficient({x: ex, y: ey})
    return mat
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

1 follower

Stats

Asked: 2021-11-01 11:19:43 +0200

Seen: 276 times

Last updated: Nov 01 '21