I have a simple problem. The numerous tutorials I've looked at show everything but this common situation. I want to do an element by element vector-vector multiply, where one of the vectors is a slice of a matrix.
I have not been able to duplicate this problem interactively with the shell, so I don't understand it. The following is just the portion of code which illustrates the problem.
Ordr = 2 # these values set higher up in the code
nx = 5
dx = np.empty((Ordr-1,nx),dtype=float)
# vector dpx and dx calculated in other code here
print('shapes dpx,dx',dpx.shape,dx.shape)
ap.arrayprint('root calc:',dpx,np.transpose(dx)) # not shown originally
dtmp = dpx*dx[0,:]
print('shape dtmp',dtmp.shape)
results are:
shapes dpx,dx (5,) (1, 5)
shape dtmp (5,5)
I expected dtmp to be shape (5,) It seems an outer product is being calculated, although I have not checked the actual numbers. It doesn't make any difference if I reverse the order, i.e. dpx[0,:]*dx As I say, I haven't been able to replicate the problem in the shell, so I'm wondering what gives. I am running version 3.6.8 on Cygwin. The same result occurs under Windows version 3.7.3rc1.
New addition to post on July 18,2019:
Further checks revealed that a call to a function to do tabular printing was causing the problem. The function prints vectors as columns together with two dimensional arrays. This is accomplished by reshaping the vectors by something like:
a.shape = (n,1)
After the function call, this reshape is passed back to the calling routine. Since dp is then (5,1) the multiply is a (5,1) matrix by a (5,) vector. Broadcasting is invoked resulting in a (5,5) result.
I am a new python programmer and I thought (erroneously) that this modification to the inputs would not pass back to the caller. Is it an error to modify the function inputs in this way? Should this have been flagged as an error in the function? I understand C pass by value and Fortran pass by reference, but do not understand how Python passes parameters to functions. Can someone point me to an explanation of calling conventions in Python?
Based on this discovery, the title of the post is completely wrong. My question now is - Why does Python allow me to change the inputs to the function? Would it be worth posting this under a different title?