Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix by vector multiplication issues #10

Open
jussihi opened this issue Oct 17, 2022 · 6 comments
Open

Matrix by vector multiplication issues #10

jussihi opened this issue Oct 17, 2022 · 6 comments

Comments

@jussihi
Copy link

jussihi commented Oct 17, 2022

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
Copy link
Author

jussihi commented Oct 17, 2022

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
Copy link
Author

jussihi commented Oct 17, 2022

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?

@zmij
Copy link
Owner

zmij commented Oct 17, 2022

@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

@jussihi
Copy link
Author

jussihi commented Oct 17, 2022

@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.

@zmij
Copy link
Owner

zmij commented Oct 17, 2022

@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 😕

@jussihi
Copy link
Author

jussihi commented Oct 17, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants