Home > Software design >  ValueError: shapes and not aligned: (dim 2) != 4 (dim 0)
ValueError: shapes and not aligned: (dim 2) != 4 (dim 0)

Time:05-07

I am currently working on a script that does some array manipulating and calculations for modeling.

I am running into an error and unsure how to solve it.

from calendar import c
from math import pi
import numpy as np
import pandas as pd

df = pd.read_csv(....file and variables)

omega = 31
MLS = df['gpsAltitude']
rho = (1.225*(np.exp(-0.0296*MLS)/304.8))
R = 6.7
T = df['grossWeight']
Nb = 4
c = 0.39
Arotor = (pi * R**2)
Vinf = df['indicatedAirspeed']

Vtip=omega*R 
MRct=T/(rho*Arotor*(Vtip)**2) 
Ablade=c*R*Nb  
sigma=Ablade/Arotor  
beta= df['driftAngle'] 
MRCTSIGMA=MRct/sigma
MU=Vinf/Vtip

T1 = np.concatenate((np.array([[beta]]),np.array([[MRCTSIGMA]]),np.array([[MU]]),np.array([[np.ones(82832)]])), 1)

T2 = np.concatenate((np.tanh(np.dot(np.array([[0.5]]), np.dot(T_1, np.array([[ 
-0.00896416974712778], [21.8178612469974], 
[-46.4671026803775], [-1.42758902156871]]))))))

Which produces an error following T2: ValueError: shapes (1,4,82832) and (4,1) not aligned: 82832 (dim 2) != 4 (dim 0)

The 2nd cross product does not like that T1 which has 82832 (thats the file size of my array that I assigned based on the file this is reading from) is a 3X1 wheras the np array with floats is a 2x1.

How/where can I assign my 2x1 array so that it reads as a 3x1, e.g (82832, 4, 1) which is what I believe I need.

Thank you.

CodePudding user response:

I admit that what you're trying to do is not absolutely clear to me, in particular your reference to 2x1 and 3x1, so I'll decompose the reasoning to make sure that I understood your point and that what I'm suggesting does what you want.

So, T1 is a matrix of shape (1, 4, 82832). The first dimension, 1, is not really meaningful. The rest can be viewed as an equivalent of a table of 4 columns holding values for "beta", "MRCTSIGMA", "MU" and a 1 constant for 82832 rows. You first want, for every row, to apply a dot product to the 4 columns with the constants -0.00896416974712778, 21.8178612469974, -46.4671026803775, -1.42758902156871. It seems to be your main issue, specifically the mismatch of dimension. It can be sorted out by switching the two terms. Doing so, we can even use a simple list of floats instead of a 2-dimension ndarray, numpy will take care of the rest. This should result in 82832 single values in a (1, 82832) array.

constants = [-0.00896416974712778, 21.8178612469974, -46.4671026803775, -1.42758902156871]

val_by_constants = np.dot(constants, T1)  # shape: (1, 82832)

Then you want to apply a ratio of 0.5 to this result. You don't really need a dot product for that, numpy can find its way with a simple multiplication:

halved = val_by_constants * .5

Finally, you want to apply a tanh function to the result. There is also this np.concatenate() call, I will guess that you use it to reduce the foreseen output shape of (1, 82832) to a simple (82832, ). If so, there are several ways to achieve that, concatenate is one but I'll suggest to use flatten instead in this case, for the sake of code clarity.

T2 = np.tanh(halved).flatten()  # shape: (82832, )

That should do it.

If you prefer a one-liner, to compare with your version:

T2 = np.tanh(.5 * np.dot([-0.00896416974712778, 
                          21.8178612469974, 
                          -46.4671026803775, 
                          -1.42758902156871], T1)).flatten()
  • Related