math icon indicating copy to clipboard operation
math copied to clipboard

Matrix by vector multiplication issues

Open jussihi opened this issue 3 years ago • 6 comments

Hello, when experimenting further with the library, I found that the following snippet:

#include <iostream>
#include <cassert>

#include <psst/math/vector.hpp>
#include <psst/math/matrix.hpp>
#include <psst/math/coordinate_conversion.hpp>
#include <psst/math/spherical_coord.hpp>


void test_psst_math()
{
  using vector3d = psst::math::vector<double, 3>;
  using matrix3x3 = psst::math::matrix<double, 3, 3>;

  vector3d v1 = {1, 2, 3};

  matrix3x3 m1 { { 1,  2,  3 },
                 { 4,  5,  6 },
                 { 7,  8,  9 } };

  vector3d v2 = m1 * psst::math::expr::as_col_matrix(v1);
  matrix3x3 m2 = psst::math::expr::transpose(m1);

}


int main(int argc, char *argv[])
{
  test_psst_math();
}

results in the following compiler error:

test.cpp: In function 'void test_psst_math()':
test.cpp:21:20: error: conversion from 'psst::math::expr::m::matrix_matrix_multiply<psst::math::matrix<double, 3, 3>, psst::math::expr::m::vector_as_col_matrix<psst::math::vector<double, 3> >&&>' to non-scalar type 'vector3d' {aka 'psst::math::vector<double, 3>'} requested
   21 |   vector3d v2 = m1 * psst::math::expr::as_col_matrix(v1);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

However, this is almost completely copy&pasted from the git readme example, so one would assume that it should work.

jussihi avatar Oct 17 '22 12:10 jussihi

If I instead compile this with

auto v2 = m1 * psst::math::expr::as_col_matrix(v1);

(auto was explicitly declared vector3d before)

It works! However, the resulting v2 is not a 1d-vector {something, something, something} as someone would expect, but a matrix instead {{something}, {something}, {something}}. How could I force the result to be a 1d-vector?

jussihi avatar Oct 17 '22 13:10 jussihi

To get a 1-d vector (v2), I now need to do the following:

#include <iostream>
#include <cassert>

#include <psst/math/vector.hpp>
#include <psst/math/matrix.hpp>
#include <psst/math/coordinate_conversion.hpp>
#include <psst/math/spherical_coord.hpp>


void test_psst_math()
{
  using vector3d = psst::math::vector<double, 3>;
  using matrix3x3 = psst::math::matrix<double, 3, 3>;

  vector3d v1 = {1, 2, 3};

  matrix3x3 m1 { { 1,  2,  3 },
                 { 4,  5,  6 },
                 { 7,  8,  9 } };

  psst::math::matrix<double, 3, 1> v2_matrix = m1 * psst::math::expr::as_col_matrix(v1);
  psst::math::matrix<double, 1, 3> v2_transp = psst::math::expr::transpose(v2_matrix);
  vector3d v2 = v2_transp[0];
}


int main(int argc, char *argv[])
{
  test_psst_math();
}

however, this is really cumbersome. Is this really the only way to get it?

jussihi avatar Oct 17 '22 14:10 jussihi

@jussihi The result of multiplying a matrix by a single-columm matrix is a single-row matrix.

There is a function as_vector for matrices with a single row/column

zmij avatar Oct 17 '22 14:10 zmij

@jussihi The result of multiplying a matrix by a single-columm matrix is a single-row matrix.

There is a function as_vector for matrices with a single row/column

Thank you. psst::math::expr::as_vector seems to work indeed. However, this was not described in the examples of readme. Instead, vector3d v2 = m1 * psst::math::expr::as_col_matrix(v1); was suggested, which doesn't seem to compile - at least for me.

jussihi avatar Oct 17 '22 14:10 jussihi

@jussihi Well, obviously there is a couple of issues with README, sorry. I think the issues were introduced when I implemented lazy evalueation. I’ll fix it when I find some free time 😕

zmij avatar Oct 17 '22 14:10 zmij

@zmij don't worry! Thanks for the comprehensive support. It is much appreciated!

jussihi avatar Oct 17 '22 14:10 jussihi