Reputation: 944
I have a few matrices defined as:
array <array <float, 3>, 23> mat1;
array <array <float, 23>, 3> mat2;
array <array <float, 2>, 23> mat3;
array <array <float, 23>, 2> mat4;
I have a matrix multiplication function defined as shown below:
void mat_mult(array <array <float, 3>, 23>& a, array <array <float, 23>, 3>& b, array <array <float, 3>, 3>& c);
The function call is such that matrix mat1
will be a
, mat2
will be b
and matrix c
will store the result of a * b
.
My understanding is that I will need to create a separate function to multiply mat3
and mat4
. Is there any way to avoid it and create a single function for all multiplication operations, with matrices made of std::array
object? Or in other words is it possible to pass 2d arrays without specifying any dimension and then pass dimensions separately?
Upvotes: 1
Views: 1198
Reputation: 4718
Looks like I'm a few minutes late. I wanted to make sure the answer I provide compiles (g++-5, with -std=c++0x) and I wanted to check its result against Octave on a few examples. So in essence, my suggestion is very similar to that of @tntxtnt: use templates.
#include <iostream>
#include <array>
typedef long unsigned int lui;
template <lui N,lui M,lui K>
std::array<std::array<float,N>,M> mult
(const std::array<std::array<float,N>,K> & A,
const std::array<std::array<float,K>,M> & B)
{
//of course you should use a more efficient algorithm, right now its complexity is cubic
std::array<std::array<float,N>,M> res;
for(int i=0;i<N;++i)
{
for(int j=0;j<M;++j)
{
res[i][j]=0;
for(int k=0;k<K;++k)
res[i][j]+=A[i][k]*B[k][j];
}
}
return res;
}
template<lui N,lui M>
void print_matrix(const std::array<std::array<float,N>,M> & A)
{
for(int i=0;i<N;++i)
{
for(int j=0;j<M;++j)
std::cout<<A[i][j]<<" ";
std::cout<<std::endl;
}
}
int main()
{
std::array<std::array<float,3>,4> A;
std::array<std::array<float,4>,2> B;
A[0][0]=14;A[0][1]=16;A[0][2]=2;A[0][3]=3;
A[1][0]=12;A[1][1]=1;A[1][2]=3;A[1][3]=14;
A[2][0]=5;A[2][1]=5;A[2][2]=31;A[2][3]=4;
B[0][0]=1;B[0][1]=3;
B[1][0]=2;B[1][1]=8;
B[2][0]=0;B[2][1]=3;
B[3][0]=-6;B[3][1]=3;
print_matrix<3,4>(A);
std::cout<<"*******************"<<std::endl;
print_matrix<4,2>(B);
std::cout<<"*******************"<<std::endl;
print_matrix<3,2>(mult<3,2,4>(A,B));
return 0;
}
Upvotes: 2
Reputation: 85
I really like tntxtnt's way, but there is an answer using the types more native to your question:
template <int widthFinal, int sharedSize, int heightFinal> void mat_mult(array<array<float, widthFinal>,sharedSize>& a, array <array <float, sharedSize>, heightFinal>& b, array <array <float, widthFinal>, heightFinal>& c);
so you can see it all at once:
template <int widthFinal, int sharedSize, int heightFinal>
void mat_mult (
array <array<float, widthFinal>,sharedSize>& a,
array <array <float, sharedSize>, heightFinal>& b,
array <array <float, widthFinal>, heightFinal>& c
);
It uses something called a template (those <> brackets). Usage:
array <array <float, 2>, 3> mat1;
array <array <float, 3>, 1> mat2;
array <array <float, 2>, 1> matResult;
mat_mult<2,3,1>(mat1, mat2, matResult);
Note: you could also replace those int
s with size_t
s, which would likely be better design.
Also Note: You only need those 3 (not 4) template parameters because you know that (by matrix properties) the width of matrix1 must eqaul the height of matrix2. I called this property the "SharedSize":
Upvotes: 1
Reputation: 1184
You can use template alias to reduce all the trouble array<array<...>>
, then define another template <M,N,P>
for matrix multiplication:
#include <iostream>
#include <array>
template <size_t M, size_t N>
using mat2f = std::array<std::array<float,N>, M>;
template <size_t M, size_t N, size_t P>
mat2f<M,P> operator*(const mat2f<M,N>& a, const mat2f<N,P>& b)
{
mat2f<M,P> c;
//...
return c;
}
int main()
{
mat2f<3,23> a;
mat2f<23,3> b;
auto c = a * b;
}
Upvotes: 3