3

Is there a python (numpy) functionality that would accomplish the 3rd "equation"?

using it as a returned lambda function

1. Vector * Scalar

vector*scalar

import numpy as np
a = np.array([3,4])
b = 2
print(a*b)
>>[6,8]

or as lambda function:

import numpy as np

def multiply():
  return lambda a,b: a*b

a = np.array([3,4])
b = 2
j = multiply()
print(j(a,b))
>>[6,8]

2. Matrix * Vector

matrix*vector

import numpy as np
a = np.array([[3,4],[2,5]])
b = np.array([2,4])
print(a*b)
print()
print(np.multiply(a,b))
print()
print(a.dot(b))
print()
print(b.dot(a))
>>[[ 6 16]
>>[ 4 20]]
>>
>>[[ 6 16]
>>[ 4 20]]
>>
>>[22 24]
>>
>>[14 28]

or as lambda function:

import numpy as np

def multiply():
  return lambda a,b: a.dot(b)

a = np.array([[3,4],[2,5]])
b = np.array([2,4])
j = multiply()
print(j(a,b))
>>[22 24]

3. Matrix (interpreted as many (2,1)-Vectors) * Vector (interpreted as many Scalars) or: Vector*Scalar for each row

matrix(many vectors)*vector(many scalars)

import numpy as np
a = np.array([[3,4],[2,5]])
b = np.array([2,4])

see answer by ALI

or as lambda function:

import numpy as np

def multiply():
  return lambda a,b: ???

a = np.array([[3,4],[2,5]])
b = np.array([2,4])
j = multiply()
print(j(a,b))
>>[[6,8],
>>[8,20]]
Chris F
  • 85
  • 6
  • What are the lambda functions for? They're entirely unrelated to the problem... – Nils Werner Nov 20 '19 at 14:15
  • Seemed unnecessary to divide the question in two seperate Stackoverflow Posts (1st how to do it and 2nd how to do it as lambda function). I have a more complex lambda function, and this was the part I struggled with, I left out the other stuff because they are not important for this part and would complicate this question. – Chris F Nov 20 '19 at 16:33
  • Quite the contrary: posting [MCVE](https://stackoverflow.com/help/minimal-reproducible-example)s ist encouraged. Splitting these questions makes it easier to understand and answer them individually. – Nils Werner Nov 20 '19 at 17:11

2 Answers2

1
import numpy as np
a = np.array([[3,2], [4, 5]])
b = np.array([2, 4])
c = np.vstack((b, b)).T
d = np.multiply(a,c)
print(d)


array([[ 6,  8],
       [8, 20]])

If you need a function

def elementwisemult(a, b):
    b = np.vstack((b, b)).T
    d = np.multiply(a,b)
    return d

If you want to use lambda function:

import numpy as np
a = np.array([[3,4],[2,5]])
b = np.array([2,4])
def elementwisemult():
    return lambda a, b: np.multiply(a, (np.vstack((b, b)).T))
j = elementwisemult()
j(a,b)
Danish
  • 2,719
  • 17
  • 32
  • How could one rewrite this as a lambda function? (see my edited post for 1. and 2.) – Chris F Nov 20 '19 at 14:01
  • Stacking data prior to multiplication is an unnecessary complication and waste of memory. You should use broadcasting instead. – Nils Werner Nov 20 '19 at 17:09
0

With

import numpy as np

a = np.array([
    [3, 4],
    [2, 5],
])
b = np.array([2, 4])

Equation 1 using scalar product

b * 3
# array([ 6, 12])

Equation 2 using dot product

np.dot(a, b)
# array([22, 24])

Equation 3 using broadcasted elementwise product

a * b[:, None]
# array([[ 6,  8],
#        [ 8, 20]])
Nils Werner
  • 34,832
  • 7
  • 76
  • 98