gpt4 book ai didi

python - 如何在 python 中规范化 vector ?

转载 作者:行者123 更新时间:2023-11-28 06:51:43 24 4
gpt4 key购买 nike

我正在尝试在 python 中计算 temp_a 和 E 的标量积和归一化 vector 。

我正在使用这个 C++ 示例:

vector temp_a = a;
vector_normalize(&temp_a);
vector E;
vector_normalize(&E);

float LSM303DLH::vector_dot(const vector *a,const vector *b)
{
return (a->x * b->x) + (a->y * b->y) + (a->z * b->z);
}

void LSM303DLH::vector_normalize(vector *a)
{
float mag = sqrt(vector_dot(a,a));
a->x /= mag;
a->y /= mag;
a->z /= mag;
}

这是我目前为止在 python 中得到的:

vector_a = [321,321,321]
vector_e = [123,123,123]

#calculating the scalar product between two vectors
vector_dot_a = numpy.dot(vector_a, vector_a)
vector_dot_e = numpy.dot(vector_e, vector_e)

#normalizing the vectors
scalar_a = math.sqrt(vector_dot_a)
vector_a[0] /= scalar_a
vector_a[1] /= scalar_a
vector_a[2] /= scalar_a

scalar_e = math.sqrt(vector_dot_e)
vector_e[0] /= scalar_e
vector_e[1] /= scalar_e
vector_e[2] /= scalar_e

对于规范化,我可以只使用它吗?

numpy.linalg.norm(vector_a)
numpy.linalg.norm(vector_e)

-+-+-+-+编辑 1-+-+-+-+

这是我的结果:

def readMagneticHeading(x_offset_min_m, y_offset_min_m, z_offset_min_m, x_offset_max_m, y_offset_max_m, z_offset_max_m):

#shift and scale the calibrated min/max magnetic data
x_heading_m = (x_data_m - x_offset_min_m) / (x_offset_max_m - x_offset_min_m) * 2 - 1.0;
y_heading_m = (y_data_m - y_offset_min_m) / (y_offset_max_m - y_offset_min_m) * 2 - 1.0;
z_heading_m = (z_data_m - z_offset_min_m) / (z_offset_max_m - z_offset_min_m) * 2 - 1.0;

vector_from = [0,-1,0] #from vector
vector_a = readAccelerations() #accelerations vector
vector_e = [0,0,0] #east vector
vector_n = [0,0,0] #north vector
vector_m = readMagnetics() #magnetics vector

#vector_a dot vector_a, the scalar dot product between two vectors
scalar_dot_a = numpy.dot(vector_a, vector_a)
#get the vector norm
vector_norm__a = numpy.linalg.norm(scalar_dot_a)
vector_a /= vector_norm__a

#create the cross product of the east vector
vector_e = numpy.cross(vector_m, vector_a)

#vector_e dot vector_e, the scalar dot product between two vectors
scalar_dot_e = numpy.dot(vector_e, vector_e)
#get the vector norm
vector_norm__e = numpy.linalg.norm(scalar_dot_e)
vector_e /= vector_norm__e

#create the cross product of the north vector
vector_n = numpy.cross(vector_a, vector_e)

vector_dot_e_from = numpy.dot(vector_e,vector_from)
vector_dot_n_from = numpy.dot(vector_n,vector_from)

#calculate the heading
heading_m = round(math.atan2(vector_dot_e_from, vector_dot_n_from) * 180 / math.pi)

if (heading_m < 0):
heading_m += 360

return heading_m

与c++版本相比:

// Returns the number of degrees from the -Y axis that it
// is pointing.
int LSM303DLH::heading(void)
{
return heading((vector){0,-1,0});
}

// Returns the number of degrees from the From vector projected into
// the horizontal plane is away from north.
//
// Description of heading algorithm:
// Shift and scale the magnetic reading based on calibration data to
// to find the North vector. Use the acceleration readings to
// determine the Down vector. The cross product of North and Down
// vectors is East. The vectors East and North form a basis for the
// horizontal plane. The From vector is projected into the horizontal
// plane and the angle between the projected vector and north is
// returned.
int LSM303DLH::heading(vector from)
{
// shift and scale
m.x = (m.x - m_min.x) / (m_max.x - m_min.x) * 2 - 1.0;
m.y = (m.y - m_min.y) / (m_max.y - m_min.y) * 2 - 1.0;
m.z = (m.z - m_min.z) / (m_max.z - m_min.z) * 2 - 1.0;

vector temp_a = a;
// normalize
vector_normalize(&temp_a);
//vector_normalize(&m);

// compute E and N
vector E;
vector N;
vector_cross(&m, &temp_a, &E);
vector_normalize(&E);
vector_cross(&temp_a, &E, &N);

// compute heading
int heading = round(atan2(vector_dot(&E, &from), vector_dot(&N, &from)) * 180 / M_PI);
if (heading < 0) heading += 360;
return heading;
}

void LSM303DLH::vector_cross(const vector *a,const vector *b, vector *out)
{
out->x = a->y*b->z - a->z*b->y;
out->y = a->z*b->x - a->x*b->z;
out->z = a->x*b->y - a->y*b->x;
}

float LSM303DLH::vector_dot(const vector *a,const vector *b)
{
return a->x*b->x+a->y*b->y+a->z*b->z;
}

void LSM303DLH::vector_normalize(vector *a)
{
float mag = sqrt(vector_dot(a,a));
a->x /= mag;
a->y /= mag;
a->z /= mag;
}

逻辑上还有什么不同吗?

最佳答案

是的,您可以使用 numpy.linalg.norm。它给出与您的代码相同的结果。

另请注意,您可以像这样以矢量化形式进行除法:

vector_a /= scalar_a

这比您拥有的三个独立的要快得多,而且可以说也更清晰。

关于python - 如何在 python 中规范化 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com