473,385 Members | 1,753 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,385 software developers and data experts.

help with 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), n = 0,1,2, ... ,8,9.

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

Perform the matrix multiplication by using the function:

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

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

[K] is a constant 4x4 matrix defined by:

double K[4][4] = { { 2., -4., 1., 0.},
{-4., 5., -2., 1.},
{ 1., -2., 6., -3.},
{ 0., 1., -3., 7.}};

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

double x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};

First, print the value of the initial vector {x}(0) on the screen.
Then, perform forward iteration 10 times (for n=0 to 9). Each time
after new vector is computed from [K]{x}, normalize that vector by
using the function

double unit_vec(double vec[4], int cols)

which was discusseed in class (see Program w8-5.c; this function
transforms a vector to a unit vector - of magnitude 1). For the
matrix multiplication with the vector [K]{x}, see Program w8-3.c .

Always use the normalized vector as the vector {x}(n) for the next
iteration.

For each iteration, print the new vector, its length, and the normalized
new vector in main().


That is the assignment. I don't even know where to start. I've constructed an outline but I don't know what to put in for the C statements.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double unit_norm(double vec[4], int cols);
  4. void matrix_mult(double a[4][4], double b[4], double c[4], int rows);
  5. main()
  6. {
  7.  double K[4][4] = { { 2., -4.,  1.,  0.},
  8.                     {-4.,  5., -2.,  1.},
  9.                     { 1., -2.,  6., -3.},
  10.                     { 0.,  1., -3.,  7.}};
  11.  double y[4], x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};
  12.  
  13. // Enter your C statements
  14. .........................
  15. .........................
  16. }
  17.  
  18. void matrix_mult(double a[4][4], double b[4], double c[4], int rows)
  19. {
  20. // compute c[]=a[][]*b[]
  21. ..........................
  22. ..........................
  23. return;
  24. }
  25.  
  26. double unit_norm(double vec[4], int cols)
  27. {
  28. double sum;
  29. // normalize a vector
  30. ...........................
  31. ...........................
  32. return sum;
  33. }
  34.  
  35.  
My teacher says the output should look like:

Initial vector:
x[0] = [ 0.333333 0.666667 0.333333 0.577350]

New vector:
y[1] = [-1.666667 1.910684 -0.732051 3.708119]
The length of this vector is: 4.551322
Normalized new vector:
x[1] = [-0.366194 0.419808 -0.160844 0.814734]

New vector:
y[10] = [-3.096892 5.315900 -6.556405 6.293838]
The length of this vector is: 10.974897
Normalized new vector:
x[10] = [-0.282180 0.484369 -0.597400 0.573476]
Nov 17 '06 #1
2 2098
Okay, here is my code now

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. double unit_norm(double vec[4], int cols);
  5. void matrix_mult(double a[4][4], double b[4], double c[4], int rows);
  6. main()
  7. {
  8.  double K[4][4] = { { 2., -4.,  1.,  0.},
  9.                     {-4.,  5., -2.,  1.},
  10.                     { 1., -2.,  6., -3.},
  11.                     { 0.,  1., -3.,  7.}};
  12.  double y[4], x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};
  13.  int k, rows, n;
  14.  double dot_p;
  15. printf("\nInitial vector:\n");
  16. printf("x[0] = [" );
  17. for(rows=0; rows<4; rows++){
  18.         printf(" %f", x[rows]);
  19.         }
  20. printf("]\n");
  21. printf("\nNew Vector:\n");
  22. for(n=0; n<11; n++){
  23.         printf("y[%d] = [", n);
  24.         for(rows = 0; rows<4; rows++){
  25.                 printf(" %f", matrix_mult(double a[4][4], double b[4], double c[4], int row));
  26.         }
  27.         printf("]\n");
  28. }
  29. }
  30.  
  31. void matrix_mult(double a[4][4], double b[4], double c[4], int row)
  32. {
  33. /* compute c[]=a[][]*b[]*/
  34. int col;
  35. double sum;
  36. for(row=0;row<4; row++){
  37.         for(col=0, sum=0.0;col<4; col++){
  38.                 sum +=a[row][col]*b[col];
  39.                 }
  40.         c[row]=sum;
  41.         }
  42. return;
  43. }
  44.  
  45. double unit_norm(double vec[4], int cols)
  46. {
  47. double sum=0;
  48. /* normalize a vector */
  49. int i;
  50. for(i=0; i<cols; i++)
  51.         sum += vec[i] * vec[i];
  52. sum = sqrt(sum);
  53. for(i=0; i<cols; i++)
  54. vec[i]=vec[i]/sum;
  55. return sum;
  56. }
  57.  
i can't get the matrix_mult function to show up when i'm trying to do the printf. anyone know how to fix this?
Nov 19 '06 #2
REICC
3
for lines 30, why are you using a,b,c? You have not declared them yet.
Dec 4 '07 #3

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

Similar topics

1
by: Nils Wagner | last post by:
Hi all, Has someone written a C binding to transfer matrices from C to Python and vice versa ? Any pointer would be appreciated. Nils
4
by: Vinc_get31 | last post by:
Hi everyone! I have 2 matrices : The 1 is smaller than the 2. I would like to select in the 2nd only lines which correspond to couples of 1st... Is that possible? And how? However, I need a...
3
by: Prototipo | last post by:
Hi! I need to dynamically create X vectors of matrices (with a known size of 5x5). It's like a three-dimensional matrix with 2 known dimensions and the third unknown. I have something like: ...
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
1
emaghero
by: emaghero | last post by:
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices? I have an algorithm, which I'm not too pleased with, that does the job. Does anybody know a faster...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
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...
3
by: nadzna | last post by:
i'm new in C++ world..i have a trouble in to solve the problem in array chapter... can someone help me how to solve this??? the QUESTION: Write a Program that calculate the 3 sum of matrices (4...
1
by: dcatunlucky | last post by:
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.