math
math copied to clipboard
Matrix by vector multiplication issues
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.
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?
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 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 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 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 don't worry! Thanks for the comprehensive support. It is much appreciated!