아두이노 코딩-195: “2주가 지나서도 포기하게 될지도 모를 당신을 위하여” Matrix Multiplication 알고리듬

in #kr6 years ago (edited)

noname01.png

매트릭스 덧셈, 뺄셈 연산의 법칙:

두 개의 매트릭스의 규격 이 동일해야 매트릭스를 더하거나 뺄셈이 가능하다.
다음의 두 매트릭스 A와 B에 대하여 A+B 와 A-B를 계산하여라.

noname02.png

연습문제

  1. 두 매트릭스의 합 A+B 와 B+A 는 같은가?
  2. 두 매트릭스 A-B 와 B-A 는 같은가?
  3. 매트릭스 A 와 같은 규격에 0이들어 있는 매트릭스를 더하면 결과는 A이다?
  4. 매트릭스 A에 –A 매트릭스를 더하면 결과 매트릭스에는 죄다 0 이 들어 있다?

게재된 매트릭스 연산 알고리듬 예제는 필자의 오래된 공업수학책 223페이지에서 참고하여 가져왔으며 지금은 이 원서의 한글 번역판이 대학교재로 보급되어 있다.

noname06.png

A 와 Transpose AT의 곱셈
2X3 매트릭스 A 와 3x2 AT를 곱하여 최종 2X2 매트릭스 C 값을 계산하여라.

noname04.png

두 매트릭스를 곱하면 2x2 매트릭스가 얻어진다. 다음과 같이 어레이 C를 선언하고 0을 입력해두자.

int C[2][2] = { { 0, 0 }, { 0, 0} }

A 와 AT를 곱하기 위한 어레이 연산은 다음과 같이 정리된다.
즉 매트릭스 C는 3개의 개별 연산 결과의 합이다.

C[0][0]= A[0][0]AT[0][0]+A[0][1]AT[1][0]+A[0][2]AT[2][0]
C[0][1]= A[0][0]
AT[0][1]+A[0][1]AT[1][1]+A[0][2]AT[2][1]
C[1][0]= A[1][0]AT[0][0]+A[1][1]AT[1][0]+A[1][2]AT[2][0]
C[1][1]= A[1][0]
AT[0][1]+A[1][1]AT[1][1]+A[1][2]AT[2][1]

이 계산 과정을 다음과 같이 for loop 로 표현해보자.
for (int m=0; m<2; m++) {
for (int n=0; n<2; n++) {
for (int p=0; p<3; p++) {
C[m][n] = C[m][n] + A[m][p]*AT[p][n]

아래에 첨부된 matrix_C 코드를 컴파일 업로딩해서 실행시켜 보자.

첨부된 matrix_C 코드를 직접 수정하여 두 매트릭스 A와 B를 곱하여 2x3 C 매트릭스를 계산하는 코드를 작성하여 계산하여라.

noname05.png

//matrix_C: A와 AT 곱하기 연산

int A[2][3]{{5,-8,1},{4,0,0}};
int AT[3][2]{ {0, 0},{ 0,0},{0,0} };
int C[2][2] = { { 0, 0 }, { 0, 0} };

void setup() {
Serial.begin(9600);

for (int i=0 ; i<2 ; i++) {
for (int j=0 ; j<3 ; j++) {

Serial.print("j = ");
Serial.print(j);
Serial.print(" i = ");
Serial.print(i);
Serial.print(" : ");
Serial.println(A[i][j]);
}
}

//Tranpose AT 계산
for (int k = 0 ; k<3 ; k++) {
for (int l = 0 ; l<2 ; l++) {
AT[k][l]=A[l][k];
Serial.print("k = ");
Serial.print(k);
Serial.print(" l = ");
Serial.print(l);
Serial.print(" : ");
Serial.println(AT[k][l]);
}
}
//매트릭스 A 와 AT 곱샘 계산
for (int m=0; m<2; m++) {
for (int n=0; n<2; n++) {
for (int p=0; p<3; p++) {
C[m][n] = C[m][n] + A[m][p]*AT[p][n];
}
Serial.print("m = ");
Serial.print(m);
Serial.print(" n = ");
Serial.print(n);
Serial.print(" : ");
Serial.println(AT[m][n]);
}
}
}

void loop() {

}

Sort:  

Congratulations @codingart! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published a post every day of the week

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Support SteemitBoard's project! Vote for its witness and get one more award!

짱짱맨 호출에 응답하여 보팅하였습니다.

Yuhh with this code it looks clear and easy! I still remember how I tried to solve this tasks with matrix on paper on my first course in university 🤔

Posted using Partiko Android

You looks pretty genius! Now, I forgot much of knowledge on matrix for long term after degree.
Now, I enjoys simple C/C++/Python coding using Arduino IDE and Anaconda instead of VisualStudio.
I feel myself like a salmon along the river going back to home lake.

Agree with this code makes it really easy. But when you need to solve this matrix at your exams and you haven't enought time it was like really big trouble. So when my children will grow up I hope that I can help them to solve such tasks if it will need them in their future education.

Posted using Partiko Android

My daughter studies computer science. Now she learns basic C coding and else. before entering Data Structure, Discrete Math and Algorithm, I am gonna help to enhance understanding on easy algorithm and, furthermore, near future, she may try to study machine learning including Tensorflow, linear regression, and so on.

GPU로 하면 ㅋㅋ 3d 연산이 되거나 머신러닝이되는 행렬은 놀라워!!!!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 62716.82
ETH 2447.73
USDT 1.00
SBD 2.65