matrix multiplication and addition

Feb 28, 2010 at 8:09pm
Develop a program which computes the current value of the vector {x}
based on the following forward iteration:

{x}(n+1) = [K] {x}(n)+ {z}, n = 0,1,2,3,4.

In other words, the next vector {x} is equal to the product of [K] and
the current vector {x} + constant vector {z}.

Perform the matrix multiplication by using the function:

void matrix_mult(double a[3][3], double *b, double *c, int rows);

You should pass the matrix [K], and a pointer array {x}
to matrix mult(), which will pass back a vector {y}=[K]{x}.

[K] is a constant 3x3 matrix defined by:

double k[3][3] = { { 1.2, -3.0, 1.1},
{-4.1, 6.2, -2.2},
{ 3.4, -2.5, -3.3} };

The initial components of the vector {x}(0) are specified by:

double x[3] = { 1./sqrt(2.), 0., -1./sqrt(2.) },

while the constant vector {z} is:

double z[3] = { 1./sqrt(3.), -1./sqrt(3.), 1./sqrt(3.) }.

>>I've been trying for a week to finish this but i cant get the matrix addition part done in [K] {x}(n)+ {z} so now it only computes [K] {x}(n). Can someone finish this off for me or help me in newbie language because it is due soon and my head hurts from trying. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <math.h>
void unit_norm(double *vec, int cols);
void matrix_mult(double a[3][3], double *b, double *c, int rows);
main()
{
 int row, i, j;
 double k[3][3] = {{1.2, -3.0,  1.1},
                  {-4.1, 6.2, -2.2},
                  {3.4, -2.5, -3.3}};
 double y[3], x[3] = { 1./sqrt(2.), 0., -1./sqrt(2.) };

 double w[3], z[3] = { 1./sqrt(3.), -1/sqrt(3.), 1./sqrt(3.) };

 printf("\nInitial vector:\n\n");
 printf("x[0] = [");
 for(row=0; row<3; row++)
        printf(" %.6f ", x[row]);
 printf("]\n\n");

  for(i=0; i<=4; i++)
  {
   matrix_mult(k,x,y,3);
   printf("New Vector:\ny[%d] = [", i+1);
   for(j=0; j<=2; j++)
        printf(" %.6f ", y[j]);
   printf("]\n");

   unit_norm(y,3);
   printf("Normalized new vector:\nx[%d] = [", i+1);
   for(j=0; j<=2; j++)
        printf(" %.6f ", y[j]);
   printf("]\n");
   for(j=0; j<=2; j++)
        x[j] = y[j];
   printf("\n");
  }
  return(0);
}

void matrix_mult(double a[3][3], double *b, double *c, int rows)
{
        int k, s;
        double dot_p;
        for(s=0; s<rows; s++)
        {
         for(k=0, dot_p=0.; k<3; k++)
                dot_p += a[s][k]*b[k];
        c[s] = dot_p;
        }
        return;
}

void unit_norm(double *vec, int cols)
{
        double norm;
        double sum=0.;
        int n;
        for(n=0; n<cols; n++)
         sum += vec[n] * vec[n];
        sum = sqrt(sum);
        for(n=0; n<cols; n++)
            vec[n] = vec[n]/sum;
return;
}

Last edited on Feb 28, 2010 at 11:52pm
Feb 28, 2010 at 8:40pm
What matrix addition? I can only see vector addition here.
I don't understand how can you write vector matrix multiplication, but fail to write vector addition...
1
2
3
for(int r = 0; r < number_of_rows; r++){
    result_vector[r] = first_vector[r] + second_vector[r];
}

here first_vector is what you got from [K]{x}(n) and second_vector is {z}
Feb 28, 2010 at 11:11pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void matrix_mult(double a[3][3], double *b, double *c, int rows)
{
        int k, s, r;
        double dot_p, res_vec;
        double w[3], z[3] = { 1./sqrt(3.), -1/sqrt(3.), 1./sqrt(3.) };
        for(r=0; r<rows; r++)
        {
                for(s=0; s<3; s++)
                {
                        for(k=0, dot_p=0; k<3; k++)
                        { dot_p += a[s][k]*b[k]; }
                }
                { c[s] = dot_p; }
        { res_vec[r] = c[r] + z[r]; }      
        }
        return;
}


now it says " error: subscripted value is neither array nor pointer" >>referring to line 14
how can i fix this?
thanks
Mar 1, 2010 at 12:45pm
res_vector is a double. It should be an array.
also, note that you never return res_vector, so it's kind of useless.
I guess that what you need is c[r] += z[r];
Last edited on Mar 1, 2010 at 12:46pm
Mar 2, 2010 at 6:22am
thanks alot!!!! that along with rearranging my loop produced the desired output. this is what the final matric_mult looked like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void matrix_mult(double a[3][3], double *b, double *c, int rows)
{
int k, s, r;
double dot_p, res_vec[5];
double w[3], z[3] = { 1./sqrt(3.), -1/sqrt(3.), 1./sqrt(3.) };
        for(s=0; s<3; s++)
        {
                for(k=0, dot_p=0; k<3; k++) 
                {
                dot_p += a[s][k]*b[k];
                }
        c[s] = dot_p;
        }
        for(r=0; r<rows; r++)
        {
                c[r] += z[r];
        }
return;
}

Last edited on Mar 2, 2010 at 6:23am
Topic archived. No new replies allowed.