6 by 6 matrix multiplicaiton with C

  • Thread starter parazit
  • Start date
  • Tags
    Matrix
In summary: I see.In this way, the user enters the name of the matrix and the program will generate all the values for that matrix.Also, some elements are pre-defined. Okay.
  • #1
parazit
75
3
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:
Technology news on Phys.org
  • #2
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
My question is how to define the matrices and how to assign that fixed and calculated values as their specified elements.
 
  • #4
Okay then I answered your question in my previous post.
 
  • #5
please explain it in an explanatory way. I do not understand how to do it.
 
Last edited:
  • #6
Okay, write down step by step what it is that you need to do. Then I help you with how to do them.
 
  • #7
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
Are you familiar with coding in C?

parazit said:
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
Sourabh N said:
Are you familiar with coding in C?

Not perfect but I know something.


Sourabh N said:
Do you know how to define variables, in particular matrices, in C?

I do not know how to define a matrix.



Sourabh N said:
Are you familiar with a "for" loop?

Like I said before, I know something but not perfectly.
 
  • #10
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
A matrix in C is represented by a 2 dimensional array. http://www.technoexam.com/c-language-lecture-study-notes-tutorials-material/two-dimensional-array.asp 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
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
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
parazit said:
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
parazit said:
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
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
I'm still waiting for some help.

Can anybody help me ?
 
  • #18
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
Thank you Borek.

Borek said:
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 ?

Borek said:
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
parazit said:
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
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
parazit said:
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.
parazit said:
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.

parazit said:
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
Anyway, thank you so much for your advises.

I'll try to handle by my own.
 

1. What is a 6 by 6 matrix?

A 6 by 6 matrix is a mathematical structure that has 6 rows and 6 columns. It is used to represent and manipulate data in various fields such as mathematics, physics, and computer science.

2. How do you multiply two 6 by 6 matrices?

To multiply two 6 by 6 matrices, you need to use the matrix multiplication rule, which involves multiplying the corresponding elements in each row of the first matrix with the corresponding elements in each column of the second matrix and then adding the products together. This process is repeated for each element in the resulting matrix.

3. What is the significance of 6 by 6 matrix multiplication in C?

In C, 6 by 6 matrix multiplication is commonly used in linear algebra and numerical computation. It is used to solve equations and perform transformations in computer graphics, machine learning, and scientific computing.

4. What are the advantages of using C for 6 by 6 matrix multiplication?

C is a high-level programming language that is efficient in handling large amounts of data and performing complex calculations. It also has a wide range of libraries and functions specifically designed for matrix operations, making it a popular choice for 6 by 6 matrix multiplication.

5. Can 6 by 6 matrix multiplication with C be parallelized?

Yes, 6 by 6 matrix multiplication with C can be parallelized to improve its performance and speed. This involves breaking down the matrix into smaller sub-matrices and distributing the computation across multiple processors or cores, which can significantly reduce the overall execution time.

Similar threads

  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
385
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
1K
  • Math Proof Training and Practice
Replies
25
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
919
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
17
Views
4K
Replies
22
Views
2K
  • Linear and Abstract Algebra
Replies
4
Views
985
Back
Top