6 by 6 matrix multiplicaiton with C

  • Thread starter parazit
  • Start date
  • #1
75
3

Main Question or Discussion Point

Hi friends.

I'm trying to define five 6 by 6 matrices with C code. I need to multiply them than. Those matrices are different from each other. Each matrix has fixed elements but some elements of that matrices depends on other mathematical operations.
For example imagine I have a matrix which is 6 by 6 and its elements are
M(1,1) = cos α
M(1,2) = ρ (sin α)
M(1,3) = 0
M(1,4) = 0
M(1,5) = 0
M(1,6) = ρ (1-cos α)
M(2,1) = (-sin α) /ρ
M(2,2) = cos α
M(2,3) = 0
M(2,4) = 0
M(2,5) = 0
M(2,6) = sin α
M(3,1) = 0
M(3,2) = 0
M(3,3) = 1
M(3,4) = ρ.α
M(3,5) = 0
M(3,6) = 0
M(4,1) = 0
M(4,2) = 0
M(4,3) = 0
M(4,4) = 1
M(4,5) = 0
M(4,6) = 0
M(5,1) = sin α
M(5,2) = ρ (1-cos α)
M(5,3) = 0
M(5,4) = 0
M(5,5) = 1
M(5,6) = ρ (α- sin α)
M(6,1) = 0
M(6,2) = 0
M(6,3) = 0
M(6,4) = 0
M(6,5) = 0
M(6,6) = 1

In here ρ and α are calculated by other mathematical operations.
For example ;
ρ=p/(b*0.2998)

The program will ask to the user about p and b, than calculate ρ and put that value to the matrix. All five matrices have some elements like that. They need to be defined like that and multiplied to obtain a final matrix.

Can anybody help me ?
 
Last edited:

Answers and Replies

  • #2
631
0
What exactly is your question?

A way to define the matrix is to first assign zeros to all elements with a 'for' loop, then assign values to particular elements according to the formulas you have got there.
 
  • #3
75
3
My question is how to define the matrices and how to assign that fixed and calculated values as their specified elements.
 
  • #4
631
0
Okay then I answered your question in my previous post.
 
  • #5
75
3
please explain it in an explanatory way. I do not understand how to do it.
 
Last edited:
  • #6
631
0
Okay, write down step by step what it is that you need to do. Then I help you with how to do them.
 
  • #7
75
3
First, I need to define 6 by 6 matrix and give a name to it and do the same thing for other four matrices. In the end I need to have five 6 by 6 matrices.

Second, I need to define the matrices variables. Some are fixed as 0 or 1 however some are derived from other mathematical operations.
 
  • #8
631
0
Are you familiar with coding in C?

First, I need to define 6 by 6 matrix and give a name to it and do the same thing for other four matrices. In the end I need to have five 6 by 6 matrices.
Do you know how to define variables, in particular matrices, in C?

Second, I need to define the matrices variables. Some are fixed as 0 or 1 however some are derived from other mathematical operations.
Are you familiar with a "for" loop?
 
  • #9
75
3
Are you familiar with coding in C?
Not perfect but I know something.


Do you know how to define variables, in particular matrices, in C?
I do not know how to define a matrix.



Are you familiar with a "for" loop?
Like I said before, I know something but not perfectly.
 
  • #10
631
0
Okay.

First you should learn about loops. Play around with a couple of for loops to get some understanding. Let me know if you have any trouble/are comfortable.
 
  • #11
631
0
A matrix in C is represented by a 2 dimensional array. Here's a tutorial on 2 dimensional arrays. Try out the example given there. Once you have this done, we can apply all this to your question.
 
  • #12
75
3
I think I got it. In that example "int a[3][3]" provides a definition of 3x3 matrix with integer elements and named as "a"

Is that right? If so, since my matrices are 6x6 and the elements are double, I need to rewrite it as "double matrixname [6][6]" Is that right ?

One more thing. In here -with a for loop- the elements of the matrix entered by the user. However, in my case, user enters nothing directly related about matrices. As I posted on my first message, user will enter some variables and the mathematical operations of that results will be the element of matrices. Also some elements are pre-defined.

How am I pass this ?
 
  • #13
75
3
Maybe I should define them in the code rather than asking from the user ?
Something like that works ?

double a,b,c;

printf("enter b : "%d);
scanf("%d",b);

printf("enter c : "%d);
scanf("%d",c);
a=b*c;

double matrixname [6][6] ={{1, 2, 3, a, 5, 6},{2, 3, 4, 5, 6, 1}, {3, 4, 5, 6, 1, 2}, {4, 5, 6, 1, 2, 3}, {5, 6, 1, 2, 3, 4}, {6, 1, 2, 3, 4, 5}};

printf ("double matrixname [6][6]") ;
 
  • #14
33,079
4,782
Maybe I should define them in the code rather than asking from the user ?
Something like that works ?

double a,b,c;

printf("enter b : "%d);
scanf("%d",b);

printf("enter c : "%d);
scanf("%d",c);
a=b*c;

double matrixname [6][6] ={{1, 2, 3, a, 5, 6},{2, 3, 4, 5, 6, 1}, {3, 4, 5, 6, 1, 2}, {4, 5, 6, 1, 2, 3}, {5, 6, 1, 2, 3, 4}, {6, 1, 2, 3, 4, 5}};

printf ("double matrixname [6][6]") ;
There are several problems with the code here.
1. printf("enter b : "%d);
This should be printf("enter b: ");
2. scanf("%d",b);
This should be scanf("%lf",&b);
The conversion specifier for a double is %lf. Also, when you use scanf, you need to provide the address of the variable you are inputting to.
3. printf ("double matrixname [6][6]") ;
This will print the string "double matrixname [6][6]". It will NOT print the value of matrix[6][6].
 
  • #15
631
0
Maybe I should define them in the code rather than asking from the user ?
Something like that works ?

double a,b,c;

printf("enter b : "%d);
scanf("%d",b);

printf("enter c : "%d);
scanf("%d",c);
a=b*c;

double matrixname [6][6] ={{1, 2, 3, a, 5, 6},{2, 3, 4, 5, 6, 1}, {3, 4, 5, 6, 1, 2}, {4, 5, 6, 1, 2, 3}, {5, 6, 1, 2, 3, 4}, {6, 1, 2, 3, 4, 5}};

printf ("double matrixname [6][6]") ;
You're on the right track (but have syntax errors, as Mark44 points out).

Since most values in your matrix are 0's, assign 0 to all of them (build up on the example given in the link).

Now ask the user for 'p' and 'b', then calculate ρ and a, and assign each non-zero matrix element the corresponding value, like this:
M[1][1] = cos a;
M[6][6] = 1;
.
.
.
 
  • #16
75
3
Thanks guys.
Let me try to continue. So now, I need to define nine 6 by 6 matrices since I want to multiply five 6 by 6 matrices.

Maybe it's better to write it like that ;
Name of Matrix 1: dsi
Name of Matrix 2: mein
Name of Matrix 3: mbdy
Name of Matrix 4: meex
Name of Matrix 5: dsf
Name of Matrix 6: dm ==> dm=mein.dsi
Name of Matrix 7: dmm ==> dmm=dm.mbdy
Name of Matrix 8: dmx ==> dmx=dmm.meex
Name of Matrix 9: dmf ==> dmf=dmx.dsf

My final matrix after all multiplication process will be dmf, right ?

And as Sourabh N said, I need to define all that matrices initillay as zero matrices with the following code ;

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
	double dsi[6][6], i, j;
	for(i=0; i<6; i++)
	{
		for(j=0; j<6; j++)
		{
		i=0;
                j=0;
		}
And the others will be defined like that, am I right?

Than I need to define non-zero elements of the matrices like,
Code:
dsi[1][2]=si ;
dsi[1][5]=cos α;
where si is an input entered by user and α is a calculated variable which depends other inputs mathematical operations. Of course I need to define that values in the code first like,
Code:
int si ;
double α;
an so on.

So far right or am I wrong ?
 
  • #17
75
3
I'm still waiting for some help.

Can anybody help me ?
 
  • #18
Borek
Mentor
28,297
2,681
Some parts are OK, but you are mostly wrong.

You don't need nine matrices, you can store some of the intermediate results in one helper matrix. This way you need less memory. In the case of 6*6 matrices it doesn't matter much, but you should learn to think this way always, memory and processor time are resources that you want to save.

Code:
for(i=0; i<6; i++)
	{
		for(j=0; j<6; j++)
		{
		i=0;
                j=0;
		}
It is not doing what you think it is doing. You want to zero matrix elements, not indices.

Code:
dsi[1][5]=cos α;
No such thing as α, you can use only standard ASCII characters.

Also note dsi[1][5] is most likely not the element you are interested in, indices start with 0 in C (so to get first element from the array a you need a[0], not a[1] - that would be the second element).
 
  • #19
75
3
Thank you Borek.

Some parts are OK, but you are mostly wrong.

You don't need nine matrices, you can store some of the intermediate results in one helper matrix. This way you need less memory. In the case of 6*6 matrices it doesn't matter much, but you should learn to think this way always, memory and processor time are resources that you want to save.



It is not doing what you think it is doing. You want to zero matrix elements, not indices.
Then how am I suppose to get it ?

No such thing as α, you can use only standard ASCII characters.
I know at least that one. That is just for representation. Is that allowed to use more than one letter to define a variable ?
 
  • #20
Borek
Mentor
28,297
2,681
Then how am I suppose to get it ?
You can use i and j as indices - dsi[j].

I know at least that one. That is just for representation. Is that allowed to use more than one letter to define a variable ?
So you have named your matrix dsi but you wonder if variable name can have more than one letter? Matrix is a variable, just of different type.
 
  • #21
75
3
Thank you so much dear Borek.

The things that I wrote here is just for representation, please see this. I'm just asking is that possible. I did not defined anything yet in an editor.

I believe, first I need to figure out how to solve the problem.

I'm a bit in hard time. You know, nobody borns as a programmer and I'm trying to run without knowing how to walk since I do not have enough time.

Anyway, thanks for all yours recommends friends.

I'll try to do something. If somebody wants to help in a clear, explanatory way, thanks to him. I'll be waiting.
 
  • #22
33,079
4,782
Name of Matrix 1: dsi
Name of Matrix 2: mein
Name of Matrix 3: mbdy
Name of Matrix 4: meex
Name of Matrix 5: dsf
Name of Matrix 6: dm ==> dm=mein.dsi
Name of Matrix 7: dmm ==> dmm=dm.mbdy
Name of Matrix 8: dmx ==> dmx=dmm.meex
Name of Matrix 9: dmf ==> dmf=dmx.dsf
Maybe these variable names are meaningful to you, but they aren't to me. There is no reason you have to use cryptic names for variables in C. In the usual software life cycle, one programmer writes some code, and months or years later, another programmer is tasked with updating the code or fixing bugs in it. Using incomprehensible variable names makes software maintenance much more difficult and costly.
Code:
dsi[1][5]=cos α;
Borek already mentioned that you can't use variable names that include Greek letters. Another problem with the code here is that you are missing the parentheses around the argument to the cos function. Mathematically, you can write cos x, but in C you have to use parentheses, as in cos(x). Don't forget to #include the header file, math.h.

I'm a bit in hard time. You know, nobody borns as a programmer and I'm trying to run without knowing how to walk since I do not have enough time.
I'm not sure you even know how to walk, yet, speaking figuratively. First you have to learn how to crawl, then how to walk before you can run. It would be useful for you to "learn to crawl" by tackling some much simpler problems, rather than attempt to jump into a program that requires knowledge of input and output function, arrays, and loops.
 
  • #23
75
3
Anyway, thank you so much for your advises.

I'll try to handle by my own.
 

Related Threads for: 6 by 6 matrix multiplicaiton with C

  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
4
Views
6K
  • Last Post
Replies
17
Views
15K
Replies
5
Views
2K
Replies
5
Views
5K
Replies
8
Views
2K
Replies
10
Views
1K
Top