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

How to multiply 3 arrays?

I am trying to multiply three arrays together;
P_temp = A * A_t * P_k
Where the size or each array is [6][6].

Having difficulty in getting the correct answer. The method i am doing is;
A * A_t = a then using the result in a to use again in
P_temp = P_k * a

Any ideas how i could simplify this at all? Any help would be greatly appreciated!!

Expand|Select|Wrap|Line Numbers
  1. void prioriestimate (int a1[][3], int a2[][4], int a3[][4])
  2. {
  3. int T = 1;    
  4. double A[6][6] =    {{1, 0, 0, T, 0, 0},
  5.              {0, 1, 0, 0, T, 0},
  6.              {0, 0, 1, 0, 0, T},
  7.              {0, 0, 0, 1, 0, 0},
  8.              {0, 0, 0, 0, 1, 0},
  9.              {0, 0, 0, 0, 0, 1}};
  10.  
  11. double A_t[6][6] =  {{1, 0, 0, 0, 0, 0},
  12.              {0, 1, 0, 0, 0, 0},
  13.              {0, 0, 1, 0, 0, 0},
  14.              {T, 0, 0, 1, 0, 0},
  15.              {0, T, 0, 0, 1, 0},
  16.              {0, 0, T, 0, 1, 0}};
  17.  
  18. double P_k[6][6] =  {{1, 0, 0, 0, 0, 0},
  19.              {0, 1, 0, 0, 0, 0},
  20.              {0, 0, 1, 0, 0, 0},
  21.              {0, 0, 0, 1, 0, 0},
  22.              {0, 0, 0, 0, 1, 0},
  23.              {0, 0, 0, 0, 1, 0}}; 
  24.  
  25.  
  26. {
  27.  
  28.  int i = 0;
  29.  
  30.  int j = 0;
  31.  
  32.  int k = 0;
  33.  int a;
  34.         for(i = 0; i < 2; i++) 
  35.  
  36.            for( j = 0; j < 4; j++)
  37.  
  38.                for( k = 0; k < 3; k++)  
  39.  
  40.                    a[6][6] +=  A[6][6] * A_k[6][6];
  41.  
  42. }
  43. {
  44.  
  45.  int i = 0;
  46.  
  47.  int j = 0;
  48.  
  49.  int k = 0;
  50.      for(i = 0; i < 2; i++) 
  51.  
  52.            for( j = 0; j < 4; j++)
  53.  
  54.                for( k = 0; k < 3; k++)  
  55.  
  56.                    P_temp[6][6] +=  a[6][6] * P_t[6][6];
  57.  
  58. }
  59. }
Jan 14 '11 #1
7 2353
horace1
1,510 Expert 1GB
to multiply c=a*b c[i][j] is evaluated
Expand|Select|Wrap|Line Numbers
  1.  c[i][j] = c[i][j] + a[i][k] * b[k][j] 
Jan 14 '11 #2
So instead of the line a[6][6] += A[6][6] * A_k[6][6];

I can just put P_temp[i][j] = P_k[i][j] + A_t[i][k] * A[k][j]?
Jan 14 '11 #3
horace1
1,510 Expert 1GB
I think more likly
Expand|Select|Wrap|Line Numbers
  1. a[i][j]] += A[i][k] * A_k[k][j];
  2.  
make sure a[][] is zeroed first though
Jan 14 '11 #4
you can also try this:
1.define a function that multiply two matrices and return result matrices.
2. Then pass the matrices in the given order . pass the two and get result. Then pass the third and the result.
3. You will have to use pointers to do this.

I have code that can multiply two matrices of any order matrix ,order given by the user , and return the result matrix. If you want , ask me.
Jan 20 '11 #5
cheers vivek if you could send me the matrix code that would be great!!
Jan 25 '11 #6
Here below is a generalized matrix multiplication code. You can multiply any two matrices of any order.The code is simple and you can easily figure out how to multiply n number of matrices of any order. Dont forget to say thanks :)
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Generalised matrix multilication and addition program
  3.  
  4. /*Author:Vivek nandan*/
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <stdlib.h>
  9.  
  10. #define min(x1,x2) (x1>x2)?x2:x1
  11.  
  12. #define max(x1,x2) (x1>x2)?x1:x2
  13.  
  14. void add_mat(float*,float*,float*,int,int);
  15.  
  16. void mul_mat(float*,float*,float *,int,int,int,int);
  17.  
  18. void get_mat(float *,int,int);
  19.  
  20. int main(void)
  21.  
  22. {
  23.  
  24. static float *mat1,*mat2,*mat3;
  25.  
  26. static int row1,col1,row2,col2;int ch,k;
  27.  
  28. int i,j;
  29.  
  30. k=0;
  31.  
  32. printf("\n***********Matrix A*********************\n");
  33.  
  34. printf("\nEnter no of rows->");
  35.  
  36. scanf("%d",&row1);
  37.  
  38. printf("\nEnter no. of col->");
  39.  
  40. scanf("%d",&col1);
  41.  
  42. mat1=(float *)malloc(row1*col1*sizeof(float));
  43.  
  44. printf("\n************** Enter Matrx A data*******************\n");
  45.  
  46. get_mat(mat1,row1,col1);
  47.  
  48. printf("\n***********Matrix B*********************\n");
  49.  
  50. printf("\nEnter no of rows->");
  51.  
  52. scanf("%d",&row2);
  53.  
  54. printf("\nEnter no. of col->");
  55.  
  56. scanf("%d",&col2);
  57.  
  58. mat2=(float*)malloc(row2*col2*sizeof(float));
  59.  
  60. printf("\n************** Enter Matrx B data*******************\n");
  61.  
  62. get_mat(mat2,row2,col2);
  63.  
  64. printf("\nEnter Choice: \n");
  65.  
  66. printf("\n(1)Adding the matrices\n");
  67.  
  68. printf("\n(2)Multiplying matrices\n");
  69.  
  70. scanf("%d",&ch);
  71.  
  72. switch(ch)
  73.  
  74. {
  75.  
  76. case 1:
  77.  
  78. if((row1==row2)&&(col1==col2)){
  79.  
  80. mat3=(float*)malloc(row1*col1*sizeof(float));
  81.  
  82. add_mat(mat1,mat2,mat3,row1,col1);
  83.  
  84. printf("\n****Result of addition***********\n"); 
  85.  
  86. for(i=0;i<row1;i++)
  87.  
  88. {
  89.  
  90.   for(j=0;j<col1;j++)
  91.  
  92.         { printf("%f\t",*(mat3+k)); k++; }
  93.  
  94. printf("\n");
  95.  
  96. }
  97.  
  98. }
  99.  
  100. else{printf("\n<<<<<<<<Error!matrix cannot be added>>>>>>>>>>>>>>>\n");}
  101.  
  102. break;
  103.  
  104.  
  105.  
  106. case 2:
  107.  
  108. if(row2==col1){
  109.  
  110. mat3=(float*)malloc(row1*col2*sizeof(float));
  111.  
  112. mul_mat(mat1,mat2,mat3,row1,row2,col1,col2);
  113.  
  114. printf("\n<<<<<<<<<<<<Multiplication result>>>>>>>>>>>\n");
  115.  
  116. for(i=0;i<row1;i++)
  117.  
  118. {
  119.  
  120.   for(j=0;j<col2;j++)
  121.  
  122.         { printf("%f\t",*(mat3+k)); k++; }
  123.  
  124. printf("\n");
  125.  
  126. }
  127.  
  128. }
  129.  
  130. else{printf("\nERROR! Cannot be Multipied\n");}
  131.  
  132. break;
  133.  
  134.  
  135.  
  136. default:
  137.  
  138. printf("\n*******Nothing to be done!***********\n");
  139.  
  140. break;
  141.  
  142. };
  143.  
  144. system("PAUSE");
  145.  
  146. return 0;
  147.  
  148. }
  149.  
  150. void get_mat(float *x,int r,int c)
  151.  
  152. {
  153.  
  154. int i,j,k;
  155.  
  156. k=0;
  157.  
  158. for(i=0;i<r;i++)
  159.  
  160. {printf("\n........row=%d,col=%d.............\n",i,j);
  161.  
  162.  
  163.  
  164. for(j=0;j<c;j++)
  165.  
  166.   {scanf("%f",(x+k));
  167.  
  168. k++;
  169.  
  170.   }
  171.  
  172. }
  173.  
  174. printf("\n<<<<<Matrix entered>>>>>>>>>>>>\n");
  175.  
  176. k=0;
  177.  
  178. for(i=0;i<r;i++)
  179.  
  180. {//printf("\n........row=%d,col=%d.............\n",i,j);
  181.  
  182. for(j=0;j<c;j++)
  183.  
  184.   {printf("%f\t",*(x+k));
  185.  
  186. k++;
  187.  
  188.   }
  189.  
  190. printf("\n");
  191.  
  192. }
  193.  
  194.  
  195.  
  196. }
  197. /* Matrix Addition Function */
  198.  
  199. void add_mat(float *x,float *y,float *z,int r,int c)
  200.  
  201. {
  202.  
  203. int i,j,k;
  204.  
  205. k=0;
  206.  
  207. for(i=0;i<r;i++)
  208.  
  209. {
  210.  
  211.   for(j=0;j<c;j++)
  212.  
  213.         { *(z+k)=*(x+k)+*(y+k);
  214.  
  215.              k++; }
  216.  
  217. }
  218.  
  219.  
  220.  
  221. }
  222. /* Matrix Multiplication function */
  223.  
  224. void mul_mat(float *x,float *y,float * z,int r1,int c1,int r2,int c2) //mul_mat(*mat1,*mat2,*result,mat1_row,mat1_col,mat2_row,mat_col)
  225.  
  226. {
  227.  
  228. int i,j,k1,k2,k3;
  229.  
  230. float sum;
  231.  
  232. sum=0.00;
  233.  
  234. k1=k2=k3=0;
  235.  
  236. if(c1==r2)
  237.  
  238.  {
  239.  
  240. while(k3<r1*c2)
  241.  
  242. {
  243.  
  244. for(i=0;i<r1;i++){
  245.  
  246.                   for(j=0;j<r2;j++){
  247.  
  248. for(k1=0;k1<c2;k1++){
  249.  
  250. sum+=*(x+i*c1+k1)*(*(y+k1*c2+j)) ;
  251.  
  252.                      }
  253.  
  254. *(z+k3)=sum;
  255.  
  256. k3++;
  257.  
  258. sum=0;
  259.  
  260. }
  261.  
  262.  
  263.  
  264. }
  265.  
  266. }
  267.  
  268. }
  269.  
  270. else{printf("\nInside Error!\n");
  271.  
  272.     }
  273.  
  274.  
  275.  
Jan 27 '11 #7
Thank you very much!!!! helps alot
Feb 4 '11 #8

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

Similar topics

21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
5
by: chella mani | last post by:
hi all, I am developing a tool for scientific application which must be time and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good practice to use 3D arrays in applications or I...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
3
by: Kermit Rose | last post by:
From: Kermit Rose Date: 02/10/06 17:36:34 To: python-help@python.org Subject: Arrays Hello. I want to write a program in python using integer arrays.
8
by: l.j. | last post by:
Hi. I've a homework including calculations with arrays. I've stuck on multiply them. Can you help me?
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
1
by: conor.robinson | last post by:
Using large arrays of data I found it is MUCH faster to cast arrays to matricies and then multiply the two matricies togther (scipy.matrix(ARRAY1)*scipy.matrix(ARRAY2)) in order to do a matrix...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.