Ask Your Question
1

Keeping zeros after matrix multiplication

asked 2019-12-01 10:46:45 -0600

dsejas gravatar image

Hello, Sage Community!

Suppose I do the following:

var('x y z')
u = vector([0, 1, 1])
v = vector([x, y, z])
u * v

The result is obviously y+z. I would like to keep the zeros after the multiplication, in order to have 0x+y+z as my result. Is it possible?

Thanks in advance for your answers!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2019-12-03 23:59:20 -0600

slelievre gravatar image

updated 2019-12-23 22:27:12 -0600

(Edited to address the follow-up question posted as a comment to an earlier version of this answer).

Obtain a complete expression using string formatting:

sage: x, y, z = SR.var('x y z')
sage: u = vector([0, 1, 1])
sage: uu = vector([1, 2, -1])
sage: v = vector([x, y, z])
sage: print(' + '.join('({})*({})'.format(a, b) for a, b in zip(u, v)))
(0)*(x) + (1)*(y) + (1)*(z)
sage: print(' + '.join('({})*({})'.format(a, b) for a, b in zip(uu, v)))
(1)*(x) + (2)*(y) + (-1)*(z)

The parentheses are maybe not so nice but they take care of keeping things clean whether coefficients are positive, negative or zero.

If one wants to push the "pretty-printing" effort a little, one should probably write a function. For example:

def axbycz(u):
    r"""
    Return a custom string for the scalar product of
    this vector with the vector (x, y, z), keeping zeros.

    EXAMPLES::

        sage: axbycz(vector([0, 1, 1]))
        '0x + y + z'
        sage: axbycz(vector([1, 2, -1]))
        'x + 2y - z'
        sage: print(axbycz(vector([0, 1, 1])))
        0x + y + z
        sage: print(axbycz(vector([1, 2, -1])))
        x + 2y - z
    """
    aa = lambda c: '' if c == 1 else '-' if c == -1 else c
    bc = lambda c: '+ {}'.format(aa(c)) if c >=0 else '- {}'.format(aa(-c))
    a, b, c = u
    return '{}x {}y {}z'.format(aa(a), bc(b), bc(c))

where the output will be as in the examples included in the function's documentation.

One could instead decide to have the function print a result rather than return a string.

def print_axbycz(u):
    r"""
    Print a custom string for the scalar product of
    this vector with the vector (x, y, z), keeping zeros.

    EXAMPLES::

        sage: print_axbycz(vector([0, 1, 1]))
        0x + y + z
        sage: print_axbycz(vector([1, 2, -1]))
        x + 2y - z
    """
    aa = lambda c: '' if c == 1 else '-' if c == -1 else c
    bc = lambda c: '+ {}'.format(aa(c)) if c >=0 else '- {}'.format(aa(-c))
    a, b, c = u
    print('{}x {}y {}z'.format(aa(a), bc(b), bc(c)))
edit flag offensive delete link more

Comments

Hello, @slelievre. Sorry for not answering before; I was really busy. Yes, that solves the particular example that I posed. However, it cannot handle negative coefficients. For example, by changing to u = vector([1, 2, -1]), and then doing u * v, I was hoping to get x+0y-z

dsejas gravatar imagedsejas ( 2019-12-23 11:22:50 -0600 )edit

I edited my answer to address your follow-up question.

slelievre gravatar imageslelievre ( 2019-12-23 22:27:25 -0600 )edit

Awesome! Thank you very much!

dsejas gravatar imagedsejas ( 2019-12-24 08:53:58 -0600 )edit

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: 2019-12-01 10:46:45 -0600

Seen: 81 times

Last updated: Dec 23 '19