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!! - void prioriestimate (int a1[][3], int a2[][4], int a3[][4])
-
{
-
int T = 1;
-
double A[6][6] = {{1, 0, 0, T, 0, 0},
-
{0, 1, 0, 0, T, 0},
-
{0, 0, 1, 0, 0, T},
-
{0, 0, 0, 1, 0, 0},
-
{0, 0, 0, 0, 1, 0},
-
{0, 0, 0, 0, 0, 1}};
-
-
double A_t[6][6] = {{1, 0, 0, 0, 0, 0},
-
{0, 1, 0, 0, 0, 0},
-
{0, 0, 1, 0, 0, 0},
-
{T, 0, 0, 1, 0, 0},
-
{0, T, 0, 0, 1, 0},
-
{0, 0, T, 0, 1, 0}};
-
-
double P_k[6][6] = {{1, 0, 0, 0, 0, 0},
-
{0, 1, 0, 0, 0, 0},
-
{0, 0, 1, 0, 0, 0},
-
{0, 0, 0, 1, 0, 0},
-
{0, 0, 0, 0, 1, 0},
-
{0, 0, 0, 0, 1, 0}};
-
-
-
{
-
-
int i = 0;
-
-
int j = 0;
-
-
int k = 0;
-
int a;
-
for(i = 0; i < 2; i++)
-
-
for( j = 0; j < 4; j++)
-
-
for( k = 0; k < 3; k++)
-
-
a[6][6] += A[6][6] * A_k[6][6];
-
-
}
-
{
-
-
int i = 0;
-
-
int j = 0;
-
-
int k = 0;
-
for(i = 0; i < 2; i++)
-
-
for( j = 0; j < 4; j++)
-
-
for( k = 0; k < 3; k++)
-
-
P_temp[6][6] += a[6][6] * P_t[6][6];
-
-
}
-
}
7 2353
to multiply c=a*b c[i][j] is evaluated - c[i][j] = c[i][j] + a[i][k] * b[k][j]
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]?
I think more likly -
a[i][j]] += A[i][k] * A_k[k][j];
-
make sure a[][] is zeroed first though
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.
cheers vivek if you could send me the matrix code that would be great!!
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 :) -
-
//Generalised matrix multilication and addition program
-
-
/*Author:Vivek nandan*/
-
-
#include <stdio.h>
-
-
#include <stdlib.h>
-
-
#define min(x1,x2) (x1>x2)?x2:x1
-
-
#define max(x1,x2) (x1>x2)?x1:x2
-
-
void add_mat(float*,float*,float*,int,int);
-
-
void mul_mat(float*,float*,float *,int,int,int,int);
-
-
void get_mat(float *,int,int);
-
-
int main(void)
-
-
{
-
-
static float *mat1,*mat2,*mat3;
-
-
static int row1,col1,row2,col2;int ch,k;
-
-
int i,j;
-
-
k=0;
-
-
printf("\n***********Matrix A*********************\n");
-
-
printf("\nEnter no of rows->");
-
-
scanf("%d",&row1);
-
-
printf("\nEnter no. of col->");
-
-
scanf("%d",&col1);
-
-
mat1=(float *)malloc(row1*col1*sizeof(float));
-
-
printf("\n************** Enter Matrx A data*******************\n");
-
-
get_mat(mat1,row1,col1);
-
-
printf("\n***********Matrix B*********************\n");
-
-
printf("\nEnter no of rows->");
-
-
scanf("%d",&row2);
-
-
printf("\nEnter no. of col->");
-
-
scanf("%d",&col2);
-
-
mat2=(float*)malloc(row2*col2*sizeof(float));
-
-
printf("\n************** Enter Matrx B data*******************\n");
-
-
get_mat(mat2,row2,col2);
-
-
printf("\nEnter Choice: \n");
-
-
printf("\n(1)Adding the matrices\n");
-
-
printf("\n(2)Multiplying matrices\n");
-
-
scanf("%d",&ch);
-
-
switch(ch)
-
-
{
-
-
case 1:
-
-
if((row1==row2)&&(col1==col2)){
-
-
mat3=(float*)malloc(row1*col1*sizeof(float));
-
-
add_mat(mat1,mat2,mat3,row1,col1);
-
-
printf("\n****Result of addition***********\n");
-
-
for(i=0;i<row1;i++)
-
-
{
-
-
for(j=0;j<col1;j++)
-
-
{ printf("%f\t",*(mat3+k)); k++; }
-
-
printf("\n");
-
-
}
-
-
}
-
-
else{printf("\n<<<<<<<<Error!matrix cannot be added>>>>>>>>>>>>>>>\n");}
-
-
break;
-
-
-
-
case 2:
-
-
if(row2==col1){
-
-
mat3=(float*)malloc(row1*col2*sizeof(float));
-
-
mul_mat(mat1,mat2,mat3,row1,row2,col1,col2);
-
-
printf("\n<<<<<<<<<<<<Multiplication result>>>>>>>>>>>\n");
-
-
for(i=0;i<row1;i++)
-
-
{
-
-
for(j=0;j<col2;j++)
-
-
{ printf("%f\t",*(mat3+k)); k++; }
-
-
printf("\n");
-
-
}
-
-
}
-
-
else{printf("\nERROR! Cannot be Multipied\n");}
-
-
break;
-
-
-
-
default:
-
-
printf("\n*******Nothing to be done!***********\n");
-
-
break;
-
-
};
-
-
system("PAUSE");
-
-
return 0;
-
-
}
-
-
void get_mat(float *x,int r,int c)
-
-
{
-
-
int i,j,k;
-
-
k=0;
-
-
for(i=0;i<r;i++)
-
-
{printf("\n........row=%d,col=%d.............\n",i,j);
-
-
-
-
for(j=0;j<c;j++)
-
-
{scanf("%f",(x+k));
-
-
k++;
-
-
}
-
-
}
-
-
printf("\n<<<<<Matrix entered>>>>>>>>>>>>\n");
-
-
k=0;
-
-
for(i=0;i<r;i++)
-
-
{//printf("\n........row=%d,col=%d.............\n",i,j);
-
-
for(j=0;j<c;j++)
-
-
{printf("%f\t",*(x+k));
-
-
k++;
-
-
}
-
-
printf("\n");
-
-
}
-
-
-
-
}
-
/* Matrix Addition Function */
-
-
void add_mat(float *x,float *y,float *z,int r,int c)
-
-
{
-
-
int i,j,k;
-
-
k=0;
-
-
for(i=0;i<r;i++)
-
-
{
-
-
for(j=0;j<c;j++)
-
-
{ *(z+k)=*(x+k)+*(y+k);
-
-
k++; }
-
-
}
-
-
-
-
}
-
/* Matrix Multiplication function */
-
-
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)
-
-
{
-
-
int i,j,k1,k2,k3;
-
-
float sum;
-
-
sum=0.00;
-
-
k1=k2=k3=0;
-
-
if(c1==r2)
-
-
{
-
-
while(k3<r1*c2)
-
-
{
-
-
for(i=0;i<r1;i++){
-
-
for(j=0;j<r2;j++){
-
-
for(k1=0;k1<c2;k1++){
-
-
sum+=*(x+i*c1+k1)*(*(y+k1*c2+j)) ;
-
-
}
-
-
*(z+k3)=sum;
-
-
k3++;
-
-
sum=0;
-
-
}
-
-
-
-
}
-
-
}
-
-
}
-
-
else{printf("\nInside Error!\n");
-
-
}
-
-
-
-
}
Thank you very much!!!! helps alot
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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);
|
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. ...
|
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.
|
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?
|
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)...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
| |