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

我正在使用以下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


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

10-08 16:28