Home > Software engineering >  Python matrices calculations - dot product
Python matrices calculations - dot product

Time:12-09

I have array vector where I am trying to calculate the dot product between the two, however I am getting the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-d458314e2348> in <module>
     13 # wavelength
     14 lam = frequency/c
---> 15 baseline_length = np.array([np.sin(HA), np.cos(HA), 0],
     16                                [-np.sin(dec_ang)*np.cos(HA), np.sin(dec_ang)*np.sin(HA), np.cos(dec_ang)],
     17                                [np.cos(dec_ang)*np.cos(HA), -np.cos(dec_ang)*np.sin(HA), np.sin(dec_ang)])

TypeError: array() takes from 1 to 2 positional arguments but 3 were given

Here is my code;

#positions of the first and second antenna of shape (n,3)
ant1, ant2  =  np.empty((0,3)), np.empty((0,3))
    
#convert the hour angle to radians
HA = 1.5*2*math.pi/24
#convert the declination angle to radians
dec_ang = 45*math.pi/180
#convert the frequency to hertz
frequency = 2*10**9
#calculate the baseline
baseline = ant2 - ant1
#speed of light in m/s
c = 3*math.pow(10, 8)
# wavelength
lam = frequency/c
baseline_length = np.array([np.sin(HA), np.cos(HA), 0],
                           [-np.sin(dec_ang)*np.cos(HA), np.sin(dec_ang)*np.sin(HA), np.cos(dec_ang)],
                           [np.cos(dec_ang)*np.cos(HA), -np.cos(dec_ang)*np.sin(HA), np.sin(dec_ang)])
        
u = np.sum(baseline_length[0]*baseline[0], axis=1) * lam
v = np.sum(baseline_length[1]*baseline[1], axis=1) * lam

in fact I could do it like with code below, but that will require me to write that down every time should I change my antenna dimensions

u = (np.sin(HA)*baseline[0]   np.cos(HA)*baseline[1]) * lam
v = (-np.sin(dec_ang)*np.cos(HA)*baseline[0]   np.sin(dec_ang)*np.sin(HA)*baseline[1]   np.cos(dec_ang)*baseline[2]) * lam 

CodePudding user response:

In the definition of baseline_length start with two square brackets: np.array([[np.sin(HA), ...], [...], [...]]). Otherwise you're not creating a 2D array.

CodePudding user response:

This feels like a question about writing code to do rotations with matrices. I don't really understand what your angles mean, but here is an attempt at rotating a vector using 2 angles:

import math
import numpy
def GenerateEulerRotationMatrix(
    Angle= None,
    AxesPair= None,
    DimensionCount = None,
    ):

    Result = None
    BaseRotation = numpy.identity( DimensionCount )
    BaseRotation[AxesPair[0], AxesPair[0]] = numpy.cos( Angle )
    BaseRotation[AxesPair[1], AxesPair[1]] = numpy.cos( Angle )
    BaseRotation[AxesPair[0], AxesPair[1]] = -numpy.sin( Angle )
    BaseRotation[AxesPair[1], AxesPair[0]] =  numpy.sin( Angle )
    Result  = BaseRotation

    return Result 


Angle1 = numpy.pi/4
Angle2 = numpy.pi/4

RotationMatrix1 = GenerateEulerRotationMatrix(
    Angle= Angle1,
    AxesPair= [0,1],
    DimensionCount=  3,
    )
print('RotationMatrix1', RotationMatrix1)


RotationMatrix2 = GenerateEulerRotationMatrix(
    Angle= Angle2,
    AxesPair= [1,2],
    DimensionCount=  3,
    )
print('RotationMatrix2', RotationMatrix2)


SomeVector = numpy.atleast_2d( numpy.array( [1,2,3] ) ).T
print ('SomeVector', SomeVector)


RotatedVector = numpy.dot( RotationMatrix1 , numpy.dot(RotationMatrix2, SomeVector) )
print('RotatedVector', RotatedVector)

Produces output:

RotationMatrix1 [[ 0.70710678 -0.70710678  0.        ]
 [ 0.70710678  0.70710678  0.        ]
 [ 0.          0.          1.        ]]
RotationMatrix2 [[ 1.          0.          0.        ]
 [ 0.          0.70710678 -0.70710678]
 [ 0.          0.70710678  0.70710678]]
SomeVector [[1]
 [2]
 [3]]
RotatedVector [[1.20710678]
 [0.20710678]
 [3.53553391]]

This will extend to higher dimensions as well (N-D), as the rotation matrix is well defined for arbitrary number of dimensions. Which you seem to indicate in the question as having importance. (Although in physics using antae you dont really need more than 4 even with relativistic calculations...)

  • Related