473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to add+multiply matrices

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 have only been able get the vector 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!


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. void unit_norm(double *vec, int cols);
  4. void matrix_mult(double a[3][3], double *b, double *c, int rows);
  5. main()
  6. {
  7.  int row, i, j;
  8.  double k[3][3] = {{1.2, -3.0,  1.1},
  9.                   {-4.1, 6.2, -2.2},
  10.                   {3.4, -2.5, -3.3}};
  11.  double y[3], x[3] = { 1./sqrt(2.), 0., -1./sqrt(2.) };
  12.  
  13.  double w[3], z[3] = { 1./sqrt(3.), -1/sqrt(3.), 1./sqrt(3.) };
  14.  
  15.  printf("\nInitial vector:\n\n");
  16.  printf("x[0] = [");
  17.  for(row=0; row<3; row++)
  18.         printf(" %.6f ", x[row]);
  19.  printf("]\n\n");
  20.  
  21.   for(i=0; i<=4; i++)
  22.   {
  23.    matrix_mult(k,x,y,3);
  24.    printf("New Vector:\ny[%d] = [", i+1);
  25.    for(j=0; j<=2; j++)
  26.         printf(" %.6f ", y[j]);
  27.    printf("]\n");
  28.  
  29.    unit_norm(y,3);
  30.    printf("Normalized new vector:\nx[%d] = [", i+1);
  31.    for(j=0; j<=2; j++)
  32.         printf(" %.6f ", y[j]);
  33.    printf("]\n");
  34.    for(j=0; j<=2; j++)
  35.         x[j] = y[j];
  36.    printf("\n");
  37.   }
  38.   return(0);
  39. }
  40.  
  41. void matrix_mult(double a[3][3], double *b, double *c, int rows)
  42. {
  43.         int k, s;
  44.         double dot_p;
  45.         for(s=0; s<rows; s++)
  46.         {
  47.          for(k=0, dot_p=0.; k<3; k++)
  48.                 dot_p += a[s][k]*b[k];
  49.         c[s] = dot_p;
  50.         }
  51.         return;
  52. }
  53.  
  54. void unit_norm(double *vec, int cols)
  55. {
  56.         double norm;
  57.         double sum=0.;
  58.         int n;
  59.         for(n=0; n<cols; n++)
  60.          sum += vec[n] * vec[n];
  61.         sum = sqrt(sum);
  62.         for(n=0; n<cols; n++)
  63.             vec[n] = vec[n]/sum;
  64. return;
  65. }
  66.  
Feb 28 '10 #1
0 2374

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: tommy | last post by:
Hello everyone, I'm trying to write a program which will be adding and subtracting two matrices. I've already written some code but there are a few problems. 1) During compilation I get this...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
5
by: per.nordlow | last post by:
Hey there! I want to multiply/divide (shift) a float with variable containing a power-of-two value: 2, 4, 8, 16, .... Is there such a function in the C standard library or the glibc library...
11
by: vips | last post by:
I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ? is there any function in vb.net to do that ?? if i put it as query to...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
8
by: gavinstone007 | last post by:
Who can solve this? Wrte a class number which represents all numbers. Implement member functions to carry out the following arithmetic operations on class instances, add- which returns an object...
12
by: Eric Renken | last post by:
I have an application that I am adding support for plug-ins and I am looking for some help on the best way to handle this. My question has to do with AppDomains and the best way to use them. I...
5
by: td0g03 | last post by:
This program adds two square matrices - Algorithm 1-3, Page 35. Change it to implement the generalized algorithm to add two m x n matrices. Matrices, in general, are not square or n x n...
5
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop...
0
by: adinda | last post by:
Well actually this is a smaller example than my main text to be honest... What i need to find is a way to save the resulting matrix Vnul, pointed to by Vnulpoint in an array of sorts. But i need...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.