Brian Bruggeman
Brian Bruggeman

Reputation: 5324

Matrix addition/multiplication in numpy

Forgive me, it's been nearly 20 years since I've pulled out my matrix math.

I have a point in space:

point1 = (x, y)

I have a scaler:

scaler = 0.5

I have a transformational matrix:

xform_matrix = [
    ( 1.0,  1.0),
    ( 1.0, -1.0),
    (-1.0, -1.0),
    (-1.0,  1.0)
]

I want the final matrix to be:

new_matrix = [
    (x + (1.0) * 0.5,  y + (1.0) * 0.5),
    (x + (1.0) * 0.5,  y - (1.0) * 0.5),
    (x - (1.0) * 0.5,  y - (1.0) * 0.5),
    (x - (1.0) * 0.5,  y + (1.0) * 0.5),
]

What are the numpy matrix operations to perform the translation above?

Thanks much in advance!

Upvotes: 0

Views: 680

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208475

I think something like the following is what you are looking for:

>>> point1 = (3, 5)
>>> scalar = 0.5
>>> xform_matrix = np.array([[1., 1.], [1., -1.], [-1., -1.], [-1., 1.]])
>>> (xform_matrix * scalar) + point1
array([[ 3.5,  5.5],
       [ 3.5,  4.5],
       [ 2.5,  4.5],
       [ 2.5,  5.5]])

If you actually meant to apply the scalar after the addition like (x + 1.0) * 0.5 then you can use the following:

>>> (xform_matrix + point1) * scalar
array([[ 2.,  3.],
       [ 2.,  2.],
       [ 1.,  2.],
       [ 1.,  3.]])

Upvotes: 4

Related Questions