Matrix calculus in C++ - product of a matrix by a vector
using namespace std;
#include< stdio.h>
#include< stdlib.h>
#include < iostream>
int * product (int **M, int *V, int N)
{
int *Vp = new int[N];
for(int i = 0; i < N; i++)
Vp[i]=0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++)
Vp[i] += M[i][j] * V[j];
}
return Vp;
}
void display(int **Mat, int N, int M){
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
Cost < < Mat[i][j];
Cost < < "\t";
}
Cost < < "\n";
}
Cost < < "\n";
}
int main(int argc, char *argv[])
{
int n=3;
int** M = new int* [ n ];
for (int i=0; i < n; i++)
M[i] = new int[ n ];
srand(time(NULL));
for(int i=0; i< n; i++)
for(int j=0; j< n; j++)
M[i][j] =rand()%10;
display(M,n,n);
int V[3] = {2,6,3};
Cost < < "\n V = [ ";
for(int i = 0; i < n; i++)
Cost < < " "< < V[i]< < " ";
Cost < < " ] ";
int *Vp = product(M,V,n);
Cost < < "\n Vp = [ ";
for(int i = 0; i < n; i++)
Cost < < " "< < Vp[i]< < " ";
Cost < < " ] ";
system("pause");
}
#include< stdio.h>
#include< stdlib.h>
#include < iostream>
int * product (int **M, int *V, int N)
{
int *Vp = new int[N];
for(int i = 0; i < N; i++)
Vp[i]=0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++)
Vp[i] += M[i][j] * V[j];
}
return Vp;
}
void display(int **Mat, int N, int M){
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
Cost < < Mat[i][j];
Cost < < "\t";
}
Cost < < "\n";
}
Cost < < "\n";
}
int main(int argc, char *argv[])
{
int n=3;
int** M = new int* [ n ];
for (int i=0; i < n; i++)
M[i] = new int[ n ];
srand(time(NULL));
for(int i=0; i< n; i++)
for(int j=0; j< n; j++)
M[i][j] =rand()%10;
display(M,n,n);
int V[3] = {2,6,3};
Cost < < "\n V = [ ";
for(int i = 0; i < n; i++)
Cost < < " "< < V[i]< < " ";
Cost < < " ] ";
int *Vp = product(M,V,n);
Cost < < "\n Vp = [ ";
for(int i = 0; i < n; i++)
Cost < < " "< < Vp[i]< < " ";
Cost < < " ] ";
system("pause");
}