elementwise-operations

per-element vector multiplication with gurobipy

per-element vector multiplication with gurobipy Question: The last line of this code fails: A = np.random.rand(d, d)*5.0 b = np.random.rand(d)*10.0 m = gp.Model() x = m.addMVar(d, name=’x’) y = m.addMVar(d, name=’y’, vtype=gp.GRB.BINARY) m.addConstr(A@x <= b) m.addConstr(A@x >= y*b) // error occurs here on y*b It gives this error for gurobipy version 9.5.1: File "src/gurobipy/mvar.pxi", line …

Total answers: 1

How to get element-wise matrix multiplication (Hadamard product) in numpy?

How to get element-wise matrix multiplication (Hadamard product) in numpy? Question: I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the result [[19 22], [43 50]] which is the matrix …

Total answers: 5

How to multiply all integers inside list

How to multiply all integers inside list Question: Hello so I want to multiply the integers inside a list. For example; l = [1, 2, 3] l = [1*2, 2*2, 3*2] output: l = [2, 4, 6] So I was searching online and most of the answers were regarding multiply all the integers with each …

Total answers: 6

Element-wise addition of 2 lists?

Element-wise addition of 2 lists? Question: I have now: list1 = [1, 2, 3] list2 = [4, 5, 6] I wish to have: [1, 2, 3] + + + [4, 5, 6] || || || [5, 7, 9] Simply an element-wise addition of two lists. I can surely iterate the two lists, but I don’t …

Total answers: 17

Comparing two NumPy arrays for equality, element-wise

Comparing two NumPy arrays for equality, element-wise Question: What is the simplest way to compare two NumPy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i])? Simply using == gives me a boolean array: >>> numpy.array([1,1,1]) == numpy.array([1,1,1]) array([ True, True, True], dtype=bool) Do I have …

Total answers: 8

How to perform element-wise multiplication of two lists?

How to perform element-wise multiplication of two lists? Question: I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab. This is how I would do it in Matlab. a = [1,2,3,4] b = [2,3,4,5] a .* b = [2, 6, 12, …

Total answers: 15

Element-wise string concatenation in numpy

Element-wise string concatenation in numpy Question: Is this a bug? import numpy as np a1=np.array([‘a’,’b’]) a2=np.array([‘E’,’F’]) In [20]: add(a1,a2) Out[20]: NotImplemented I am trying to do element-wise string concatenation. I thought Add() was the way to do it in numpy but obviously it is not working as expected. Asked By: Dave31415 || Source Answers: This …

Total answers: 6

Are there builtin functions for elementwise boolean operators over boolean lists?

Are there builtin functions for elementwise boolean operators over boolean lists? Question: For example, if you have n lists of bools of the same length, then elementwise boolean AND should return another list of that length that has True in those positions where all the input lists have True, and False everywhere else. It’s pretty …

Total answers: 5